Hog v10.58.0
listfile.tcl
Go to the documentation of this file.
1 
2 namespace eval ListFile {
3 
4  # HogFileObj
5  #
6  # Shape (tdict):
7  # path tstr — absolute path to the file
8  # filename tstr — file name only
9  # ext tstr — file extension
10  # symlink_target tstr — resolved real path when file is a symlink; "" otherwise
11  # libraries tlist — library names this file belongs to
12  # filesets tlist — fileset names this file belongs to
13  # props tdict — key, value property pairs
14  # user tdict — open namespace for caller-defined metadata
15  #
16  namespace eval HogFileObj {
17 
18  proc New {path args} {
19  set ext [file extension $path]
20  set is_link [expr {[file exists $path] && [file type $path] eq "link"}]
21  set hog_file [tdict create \
22  path [tstr $path] \
23  filename [tstr [file tail $path]] \
24  ext [tstr $ext] \
25  symlink_target [tstr [expr {$is_link ? [GetLinkedFile $path] : ""}]] \
26  libraries [tlist create] \
27  filesets [tlist create] \
28  props [tdict create] \
29  user [tdict create] \
30  ]
31  if {[dict exists $args -library]} {
32  tdict set hog_file libraries [tlist create [tstr [dict get $args -library]]]
33  }
34  if {[dict exists $args -fileset]} {
35  tdict set hog_file filesets [tlist create [tstr [dict get $args -fileset]]]
36  }
37  if {[dict exists $args -props]} {
38  tdict set hog_file props [dict get $args -props]
39  }
40  if {[dict exists $args -user]} {
41  tdict set hog_file user [dict get $args -user]
42  }
43  return $hog_file
44  }
45 
46  # Merge src into dst: append missing libraries/filesets, add missing props.
47  proc Merge {dstVar src} {
48  upvar 1 $dstVar dst
49  tlist foreach lib [tdict get $src libraries] {
50  if {![tlist elemExists [tdict get $dst libraries] [tobj value $lib]]} {
51  tdict lappend dst libraries $lib
52  }
53  }
54  tlist foreach fs [tdict get $src filesets] {
55  if {![tlist elemExists [tdict get $dst filesets] [tobj value $fs]]} {
56  tdict lappend dst filesets $fs
57  }
58  }
59  set dst_props [tdict get $dst props]
60  tdict for {k v} [tdict get $src props] {
61  if {![tdict exists $dst_props $k]} { tdict set dst_props $k $v }
62  }
63  tdict set dst props $dst_props
64  }
65 
66  # Filter a tdict of HogFileObjs (path -> HogFileObj).
67  # Returns a new tdict containing only entries that satisfy all given patterns.
68  #
69  # Options:
70  # -library <regex> include only files whose library name matches
71  # -fileset <regex> include only files whose fileset matches
72  # -file <regex> include only files whose path matches
73  proc Filter {hog_files args} {
74  set lib_pat [expr {[dict exists $args -library] ? [dict get $args -library] : ""}]
75  set fs_pat [expr {[dict exists $args -fileset] ? [dict get $args -fileset] : ""}]
76  set file_pat [expr {[dict exists $args -file] ? [dict get $args -file] : ""}]
77 
78  set result [tdict create]
79  tdict for {path fobj} $hog_files {
80  if {$lib_pat ne ""} {
81  set ok 0
82  tlist foreach lib [tdict get $fobj libraries] {
83  if {[regexp -- $lib_pat [tobj value $lib]]} { set ok 1; break }
84  }
85  if {!$ok} { continue }
86  }
87  if {$fs_pat ne ""} {
88  set ok 0
89  tlist foreach fs [tdict get $fobj filesets] {
90  if {[regexp -- $fs_pat [tobj value $fs]]} { set ok 1; break }
91  }
92  if {!$ok} { continue }
93  }
94  if {$file_pat ne "" && ![regexp -- $file_pat $path]} { continue }
95  tdict set result $path $fobj
96  }
97  return $result
98  }
99  }
100 
101  namespace eval Codec {
102  variable _table {}
103 
104  # Register a codec for a file extension pattern.
105  #
106  # library_rule should return a string to set library
107  #
108  # tool Scope this codec to a specific tool (default: global).
109  #
110  # decoder Dict with optional keys:
111  # pre Script. Implicit locals: hog_file (HogFileObj), raw_tokens, file_lib, list_ext.
112  # Set `_done 1` to skip built-in decode.
113  # post Script. Implicit locals: hog_file (HogFileObj), raw_tokens.
114  # Called after library + prop parsing.
115  # encoder Dict with optional keys:
116  # pre Script. Implicit locals: hog_file (HogFileObj), tokens (list).
117  # Set `_done 1` to skip default prop encoding.
118  # post Script. Implicit locals: hog_file (HogFileObj), tokens (list).
119  # Called after default encoding to add/modify tokens.
120  #
121  # Every field is optional. Last registered codec wins on pattern+tool conflict.
122  proc RegisterCodec {pattern codec_dict} {
123  variable _table
124  set row [dict create \
125  pattern $pattern \
126  library_rule [expr {[dict exists $codec_dict library_rule] ? [dict get $codec_dict library_rule] : ""}] \
127  tool [expr {[dict exists $codec_dict tool] ? [string toupper [dict get $codec_dict tool]] : ""}] \
128  decoder_pre "" \
129  decoder_post "" \
130  encoder_pre "" \
131  encoder_post "" \
132  ]
133  if {[dict exists $codec_dict decoder]} {
134  set dec [dict get $codec_dict decoder]
135  if {[dict exists $dec pre]} { dict set row decoder_pre [dict get $dec pre] }
136  if {[dict exists $dec post]} { dict set row decoder_post [dict get $dec post] }
137  }
138  if {[dict exists $codec_dict encoder]} {
139  set enc [dict get $codec_dict encoder]
140  if {[dict exists $enc pre]} { dict set row encoder_pre [dict get $enc pre] }
141  if {[dict exists $enc post]} { dict set row encoder_post [dict get $enc post] }
142  }
143  lappend _table $row
144  }
145 
146  # Find the best matching codec for a file extension + tool.
147  # Tool-specific beats global; last registered wins within each tier.
148  # Returns the codec dict, or "" if nothing matches.
149  proc _FindCodec {ext tool} {
150  variable _table
151  set tool [string toupper $tool]
152  set global_match ""
153  set tool_match ""
154  foreach row $_table {
155  set pattern [dict get $row pattern]
156  set row_tool [dict get $row tool]
157  if {![regexp -- $pattern $ext]} { continue }
158  if {$row_tool ne "" && $row_tool ne $tool} { continue }
159  if {$row_tool ne ""} { set tool_match $row } else { set global_match $row }
160  }
161  return [expr {$tool_match ne "" ? $tool_match : $global_match}]
162  }
163 
164  # Check if a codec is registered for the given file.
165  proc IsRegistered {file {tool ""}} {
166  return [expr {[_FindCodec [file extension $file] $tool] ne ""}]
167  }
168 
169  # Extract lib= and path= meta tokens. Returns {file_lib ref_path remaining}.
170  proc ExtractMeta {raw_tokens default_lib} {
171  set file_lib $default_lib
172  set ref_path ""
173  set remaining {}
174  foreach p $raw_tokens {
175  regsub { *= *} $p "=" p
176  if {[string match "lib=*" $p]} {
177  set file_lib [string range $p 4 end]
178  } elseif {[string match "path=*" $p]} {
179  set ref_path [string range $p 5 end]
180  } else {
181  lappend remaining $p
182  }
183  }
184  return [list $file_lib $ref_path $remaining]
185  }
186 
187  # Parse raw property tokens into a tdict.
188  # Bare words become key="1";
189  # key=value pairs become key="value";
190  # Values containing commas become a tlist of tstr
191  proc ParseTokens {raw_tokens} {
192  set props [tdict create]
193  foreach p $raw_tokens {
194  regsub { *= *} $p "=" p
195  set pos [string first "=" $p]
196  if {$pos == -1} {
197  tdict set props $p [tstr "1"]
198  } else {
199  set key [string range $p 0 [expr {$pos-1}]]
200  set val [string range $p [expr {$pos+1}] end]
201  if {[string first "," $val] >= 0} {
202  set items [list]
203  foreach item [split $val ","] {
204  set item [string trim $item]
205  if {$item ne ""} { lappend items [tstr $item] }
206  }
207  tdict set props $key [tlist create {*}$items]
208  } else {
209  tdict set props $key [tstr $val]
210  }
211  }
212  }
213  return $props
214  }
215 
216 
217  # Decode a list-file entry into a HogFileObj.
218  # Scripts in the codec run in THIS proc's local scope — they can read/mutate
219  # $hog_file, $raw_tokens, $file_lib, $list_ext directly (no upvar needed).
220  proc Decode {file list_ext file_lib fileset raw_tokens tool} {
221  set ext [file extension $file]
222  set codec [_FindCodec $ext $tool]
223  set hog_file [ListFile::HogFileObj::New $file -fileset $fileset]
224 
225  if {$codec eq ""} {
226  tdict set hog_file libraries [tlist create [tstr "others.src"]]
227  tdict set hog_file props [ParseTokens $raw_tokens]
228  return $hog_file
229  }
230 
231  # Decoder pre: set `_done 1` to skip built-in decode.
232  set _done 0
233  set pre [dict get $codec decoder_pre]
234  if {$pre ne ""} { eval $pre }
235  if {$_done} { return $hog_file }
236 
237  # Built-in: apply library_rule and parse prop tokens.
238  set lib_rule [dict get $codec library_rule]
239  if {$lib_rule ne ""} {
240  tdict set hog_file libraries [tlist create [tstr [subst $lib_rule]]]
241  }
242  tdict set hog_file props [ParseTokens $raw_tokens]
243 
244  # Decoder post: modify entry after built-in.
245  set post [dict get $codec decoder_post]
246  if {$post ne ""} { eval $post }
247 
248  return $hog_file
249  }
250 
251  # Encode a single prop key+value into one list-file token.
252  # Flag (value "1"): bare key.
253  # List: key=item1,item2,...
254  # Scalar: key=value.
255  proc EncodeProp {key v} {
256  if {[tobj type $v] eq "List"} {
257  set items [list]
258  tlist foreach item $v { lappend items [tobj value $item] }
259  return "${key}=[join $items ","]"
260  }
261  set val [tobj value $v]
262  if {$val eq "1"} { return $key }
263  return "${key}=${val}"
264  }
265 
266  # Convert a HogFileObj's props to a list of raw tokens.
267  proc EncodeProps {hog_file {tool ""}} {
268  set ext [tdict getval $hog_file ext]
269  set codec [_FindCodec $ext $tool]
270  set tokens {}
271 
272  # Encoder pre: set `_done 1` to skip default prop encoding.
273  set _done 0
274  set pre [expr {$codec ne "" ? [dict get $codec encoder_pre] : ""}]
275  if {$pre ne ""} { eval $pre }
276  if {$_done} { return $tokens }
277 
278  # Default prop encoding via EncodeProp (flag / list / scalar).
279  tdict for {k v} [tdict get $hog_file props] {
280  lappend tokens [EncodeProp $k $v]
281  }
282 
283  # Encoder post: add/modify tokens after default.
284  set post [expr {$codec ne "" ? [dict get $codec encoder_post] : ""}]
285  if {$post ne ""} { eval $post }
286 
287  return $tokens
288  }
289 
290  # Encode a HogFileObj into a single list-file line.
291  proc Encode {hog_file root_path list_ext {default_lib ""} {tool ""}} {
292  set path [tdict getval $hog_file path]
293  set rel [Relative $root_path $path]
294  set tokens [list $rel]
295 
296  # lib= token: emit when the stored library differs from the default.
297  set lib_tlist [tdict get $hog_file libraries]
298  if {[tlist length $lib_tlist] > 0 && $default_lib ne ""} {
299  set lib_name [tlist getval $lib_tlist 0]
300  if {$lib_name ne "${default_lib}${list_ext}"} {
301  set lib_base [string range $lib_name 0 end-[string length $list_ext]]
302  if {$lib_base ne ""} { lappend tokens "lib=${lib_base}" }
303  }
304  }
305 
306  set tokens [concat $tokens [EncodeProps $hog_file $tool]]
307  return [join $tokens " "]
308  }
309 
310  ############################################################################
311  # Built in Codecs
312  ############################################################################
313 
314  RegisterCodec {\.vhdl?$} {
315  library_rule {${file_lib}${list_ext}}
316  decoder {
317  post {
318  set props [tdict get $hog_file props]
319  set std "2008"
320  foreach year {93 1987 1993 2008 2019} {
321  if {[tdict exists $props $year]} {
322  set std $year
323  set props [tdict remove $props $year]
324  break
325  }
326  }
327  tdict set props std [tstr $std]
328  tdict set hog_file props $props
329  }
330  }
331  encoder {
332  pre {
333  tdict for {k v} [tdict get $hog_file props] {
334  if {$k eq "std"} {
335  lappend tokens [tobj value $v]
336  } else {
337  lappend tokens [EncodeProp $k $v]
338  }
339  }
340  set _done 1
341  }
342  }
343  }
344 
345  RegisterCodec {\.v$} {library_rule {${file_lib}${list_ext}}}
346  RegisterCodec {\.svp?$} {library_rule {${file_lib}${list_ext}}}
347  RegisterCodec {\.xcix?$|\.ip$|\.bd$} {library_rule ips.src}
348 
349  RegisterCodec {\.xml$} {
350  library_rule xml.ipb
351  decoder {
352  post {
353  set props [tdict get $hog_file props]
354  if {[llength $raw_tokens] > 0} {
355  tdict set props generated_vhd [tstr [lindex $raw_tokens 0]]
356  set bare [lindex $raw_tokens 0]
357  if {[tdict exists $props $bare]} {
358  set props [tdict remove $props $bare]
359  }
360  }
361  tdict set hog_file props $props
362  }
363  }
364  encoder {
365  pre {
366  set props [tdict get $hog_file props]
367  if {[tdict exists $props generated_vhd]} {
368  lappend tokens [tdict getval $props generated_vhd]
369  }
370  tdict for {k v} $props {
371  if {$k eq "generated_vhd"} { continue }
372  lappend tokens [EncodeProp $k $v]
373  }
374  set _done 1
375  }
376  }
377  }
378 
379  RegisterCodec {\.xdc$} {library_rule {[expr {$list_ext eq ".con" ? "sources.con" : "others.src"}]}}
380  RegisterCodec {\.sdc$} {library_rule {[expr {$list_ext eq ".con" ? "sources.con" : "others.src"}]}}
381  RegisterCodec {\.tcl$} {library_rule {[expr {$list_ext eq ".con" ? "sources.con" : $list_ext eq ".sim" ? "others.sim" : "others.src"}]}}
382  RegisterCodec {\.udo?$} {library_rule others.sim}
383  RegisterCodec {\.c$|\.cpp$|\.hpp?$} {library_rule {${file_lib}${list_ext}}}
384  RegisterCodec {\.cfg$} {library_rule {${file_lib}${list_ext}}}
385  }
386 }