Hog v10.58.0
project.tcl
Go to the documentation of this file.
1 # HogProjectObj
2 # project_name tstr - full path relative to Top/, e.g. "group/myproj"
3 # project tstr - file tail (e.g. "myproj")
4 # group_name tstr - directory group (file dirname of project_name)
5 # design tstr - same as project_name (legacy compat)
6 # project_path tstr - absolute path to Top/<project_name>
7 # conf_path tstr - absolute path to hog.conf
8 # list_path tstr - absolute path to the list/ directory
9 # build_dir tstr - absolute path to Projects/<project_name>
10 # top_name tstr - file rootname of project tail
11 # synth_top_module tstr - conventional RTL top ("top_<top_name>")
12 # config tdict - hog.conf sections (ide, parameters, generics…)
13 # list_files tlist - absolute paths to .src/.sim/.con/.ipb files
14 # libraries tlist - union of all library names across parsed files
15 # filesets tlist - union of all fileset names
16 # project_files tdict - path -> HogFileObj for every parsed source file
17 # user tdict - reserved for caller/tool metadata; Hog never writes here
18 
19 namespace eval HogProjectObj {
20 
21  proc New {project_name {repo_path .} args} {
22  set top_path [file normalize [file join $repo_path Top]]
23  set proj_path [file normalize [file join $top_path $project_name]]
24  set top_name [file rootname [file tail $project_name]]
25 
26  set p [tdict create \
27  project_name [tstr $project_name] \
28  project [tstr [file tail $project_name]] \
29  group_name [tstr [file dirname $project_name]] \
30  design [tstr $project_name] \
31  project_path [tstr $proj_path] \
32  conf_path [tstr [file normalize [file join $proj_path hog.conf]]] \
33  list_path [tstr [file normalize [file join $proj_path list]]] \
34  build_dir [tstr [file normalize [file join $repo_path Projects $project_name]]] \
35  top_name [tstr $top_name] \
36  synth_top_module [tstr "top_${top_name}"] \
37  config [tdict create] \
38  list_files [tlist create] \
39  libraries [tlist create] \
40  filesets [tlist create] \
41  project_files [tdict create] \
42  user [tdict create] \
43  ]
44  if {[dict exists $args -config]} { tdict set p config [dict get $args -config] }
45  if {[dict exists $args -user]} { tdict set p user [dict get $args -user] }
46  return $p
47  }
48 
49 }
50 
51 
52 namespace eval Projects {
53 
54  # Load a project from disk — reads hog.conf and returns a HogProjectObj.
55  proc GetInfo {project_name {repo_path .}} {
56  set p [HogProjectObj::New $project_name $repo_path]
57  set conf_path [tdict getval $p conf_path]
58  if {[file exists $conf_path]} {
59  set PROPERTIES [ReadConf $conf_path]
60  dict for {section content} $PROPERTIES {
61  dict for {key val} $content {
62  Msg Debug "Setting property $key to $val for section $section"
63  tdict set p config $section $key $val
64  }
65  }
66  }
67  return $p
68  }
69 
70  # Return a tdict of all projects in the repo (project_name → HogProjectObj).
71  proc GetAll {{repo_path .}} {
72  set top_path [file normalize $repo_path/Top]
73  set ret [tdict create]
74  foreach conf [lsort [findFiles [file normalize $top_path] hog.conf]] {
75  set name [Relative $top_path [file dirname $conf]]
76  tdict set ret $name [GetInfo $name $repo_path]
77  }
78  return $ret
79  }
80 
81  # Populate list_files, libraries, filesets, project_files on hog_project.
82  # Mutates the project dict in-place via upvar.
83  # This lets us lazy load the files as needed rather than parsing every
84  # projects listfile at once
85  proc LoadListFiles {hog_projectVar {ext_path ""} {tool ""}} {
86  upvar 1 $hog_projectVar p
87 
88  set list_path [tdict getval $p list_path]
89  set repo_path [Repo::Get repo_path]
90 
91  if {$tool eq ""} {
92  set _conf [file normalize [file join [file dirname $list_path] hog.conf]]
93  if {[file exists $_conf]} {
94  set tool [string tolower [lindex [GetIDEFromConf $_conf] 0]]
95  }
96  }
97 
98  foreach list_file [lsort [glob -nocomplain -directory $list_path "*"]] {
99  set ext [file extension $list_file]
100  if {$ext ni {.src .con .sim .ext .ipb}} { continue }
101  if {$ext eq ".ext" && $ext_path eq ""} {
102  Msg Warning "Skipping $list_file: no ext_path provided."
103  continue
104  }
105  set root_path [expr {$ext eq ".ext" ? $ext_path : $repo_path}]
106  set lib [file rootname [file tail $list_file]]
107  set lf_list [tdict get $p list_files]
108  tlist append lf_list [tstr [file normalize $list_file]]
109  tdict set p list_files $lf_list
110  _ParseListFile p $list_file $root_path $lib "" $tool
111  }
112 
113  # Auto-discover HLS components in hog.conf without a .src list file.
114  set proj_conf [file normalize [file join [file dirname $list_path] hog.conf]]
115  if {[file exists $proj_conf]} {
116  set hls_configs [GetHlsConfigsFromProjConf $proj_conf $repo_path]
117  set tracked [dict create]
118  tdict for {fp _} [tdict get $p project_files] { dict set tracked $fp 1 }
119  dict for {comp_name cfg_abs} $hls_configs {
120  if {[dict exists $tracked $cfg_abs]} { continue }
121  set lib_name "${comp_name}.src"
122  _AddFile p [ListFile::HogFileObj::New $cfg_abs -library $lib_name -fileset sources_1]
123  dict set tracked $cfg_abs 1
124  foreach hls_extra [ExpandHlsConfigFiles $cfg_abs] {
125  if {[dict exists $tracked $hls_extra]} { continue }
126  _AddFile p [ListFile::HogFileObj::New $hls_extra -library $lib_name -fileset sources_1]
127  dict set tracked $hls_extra 1
128  }
129  }
130  }
131  }
132 
133  # Merge a HogFileObj into the project's libraries/filesets/project_files.
134  proc _AddFile {hog_projectVar hog_file} {
135  upvar 1 $hog_projectVar p
136 
137  set path [tobj value [tdict get $hog_file path]]
138  set libs [tdict get $p libraries]
139  set fsets [tdict get $p filesets]
140  set files [tdict get $p project_files]
141 
142  tlist foreach lib [tdict get $hog_file libraries] {
143  if {![tlist elemExists $libs [tobj value $lib]]} { tlist append libs $lib }
144  }
145  tlist foreach fs [tdict get $hog_file filesets] {
146  if {![tlist elemExists $fsets [tobj value $fs]]} { tlist append fsets $fs }
147  }
148 
149  if {[tdict exists $files $path]} {
150  set existing [tdict get $files $path]
151  ListFile::HogFileObj::Merge existing $hog_file
152  tdict set files $path $existing
153  } else {
154  tdict set files $path $hog_file
155  }
156 
157  tdict set p libraries $libs
158  tdict set p filesets $fsets
159  tdict set p project_files $files
160  }
161 
162  # Parse a single list file into the project.
163  proc _ParseListFile {hog_projectVar list_file path lib fileset tool} {
164  upvar 1 $hog_projectVar p
165 
166  set ext [file extension $list_file]
167  if {$fileset eq ""} {
168  switch $ext {
169  .sim { set fileset $lib }
170  .con { set fileset "constrs_1" }
171  default { set fileset "sources_1" }
172  }
173  }
174 
175  set fp [open $list_file r]
176  set data [ExtractFilesSection [split [read $fp] "\n"]]
177  close $fp
178 
179  foreach line $data {
180  if {[regexp {^[\t\s]*$} $line] || [regexp {^[\t\s]*#} $line]} { continue }
181 
182  set tokens [regexp -all -inline {\S+} $line]
183  set srcfile "$path/[lindex $tokens 0]"
184  set srcfiles [glob -nocomplain $srcfile]
185 
186  if {$srcfiles != $srcfile && ![string equal $srcfiles ""]} {
187  Msg Debug "Wildcard source expanded from $srcfile to $srcfiles"
188  } elseif {![file exists $srcfile]} {
189  Msg CriticalWarning "File: $srcfile (from list file: $list_file) does not exist."
190  continue
191  }
192 
193  foreach vhdlfile $srcfiles {
194  if {![file exists $vhdlfile]} { continue }
195  set vhdlfile [file normalize $vhdlfile]
196  set fext [file extension $vhdlfile]
197  set raw_tokens [lrange $tokens 1 end]
198 
199  lassign [ListFile::Codec::ExtractMeta $raw_tokens $lib] file_lib ref_path remaining
200 
201  if {$fext eq $ext} {
202  set ref_path [expr {$ref_path eq "" ? $path : [file normalize $path/$ref_path]}]
203  _ParseListFile p $vhdlfile $ref_path $file_lib $fileset $tool
204  continue
205  }
206 
207  if {$fext in {.src .sim .con}} {
208  Msg Error "$vhdlfile cannot be included in $list_file; $fext must be their own list file."
209  }
210 
211  set hog_file [ListFile::Codec::Decode $vhdlfile $ext $file_lib $fileset $remaining $tool]
212  _AddFile p $hog_file
213 
214  if {$ext eq ".src" && $fext eq ".cfg"} {
215  set lib_name [tlist getval [tdict get $hog_file libraries] 0]
216  foreach hls_extra [ExpandHlsConfigFiles $vhdlfile] {
217  if {$hls_extra eq $vhdlfile} { continue }
218  Msg Debug "HLS cfg expansion: adding $hls_extra to $lib_name"
219  _AddFile p [ListFile::HogFileObj::New $hls_extra -library $lib_name -fileset $fileset]
220  }
221  }
222  }
223  }
224  }
225 
226  # Query a loaded HogProjectObj's project_files with optional regex filters.
227  #
228  # Options:
229  # -library <regex> match files whose library name matches regex
230  # -fileset <regex> match files whose fileset name matches regex
231  # -file <regex> match files whose absolute path matches regex
232  # -symlinks <bool> if 1 (default), append symlink_target paths (-as paths only)
233  # -as paths|objects return a list of path strings (default) or HogFileObj tdicts
234  proc GetProjectFiles {hog_project args} {
235  set lib_pat [expr {[dict exists $args -library] ? [dict get $args -library] : ""}]
236  set fs_pat [expr {[dict exists $args -fileset] ? [dict get $args -fileset] : ""}]
237  set file_pat [expr {[dict exists $args -file] ? [dict get $args -file] : ""}]
238  set symlinks [expr {[dict exists $args -symlinks] ? [dict get $args -symlinks] : 1}]
239  set as [expr {[dict exists $args -as] ? [dict get $args -as] : "objects"}]
240 
241  set result [expr {$as eq "tdict" ? [tdict create] : {}}]
242  tdict for {fp hog_file} [tdict get $hog_project project_files] {
243  if {$lib_pat ne ""} {
244  set ok 0
245  tlist foreach lib [tdict get $hog_file libraries] {
246  if {[regexp -- $lib_pat [tobj value $lib]]} { set ok 1; break }
247  }
248  if {!$ok} { continue }
249  }
250  if {$fs_pat ne ""} {
251  set ok 0
252  tlist foreach fs [tdict get $hog_file filesets] {
253  if {[regexp -- $fs_pat [tobj value $fs]]} { set ok 1; break }
254  }
255  if {!$ok} { continue }
256  }
257  if {$file_pat ne "" && ![regexp -- $file_pat $fp]} { continue }
258 
259  if {$as eq "tdict"} {
260  tdict set result $fp $hog_file
261  } elseif {$as eq "objects"} {
262  lappend result $hog_file
263  } else {
264  lappend result $fp
265  if {$symlinks} {
266  set target [tdict getval $hog_file symlink_target]
267  if {$target ne ""} { lappend result $target }
268  }
269  }
270  }
271  return $result
272  }
273 
274  # All tracked paths for SHA computation: list files (+ their symlink targets)
275  # plus all project_files
276  proc SHAFiles {hog_project} {
277  set paths {}
278  tlist foreachval lf [tdict get $hog_project list_files] {
279  lappend paths $lf
280  if {[file exists $lf] && [file type $lf] eq "link"} {
281  lappend paths [GetLinkedFile $lf]
282  }
283  }
284  lappend paths {*}[GetProjectFiles $hog_project -as paths]
285  return $paths
286  }
287 }