2 namespace eval ListFile {
16 namespace eval HogFileObj {
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 \
23 filename [
tstr [
file tail $path]] \
25 symlink_target [
tstr [
expr {$is_link ? [GetLinkedFile $path] : ""}]] \
26 libraries [tlist create] \
27 filesets [tlist create] \
28 props [tdict create] \
31 if {[dict exists $args -library]} {
32 tdict set hog_file libraries [tlist create [
tstr [dict get $args -library]]]
34 if {[dict exists $args -fileset]} {
35 tdict set hog_file filesets [tlist create [
tstr [dict get $args -fileset]]]
37 if {[dict exists $args -props]} {
38 tdict set hog_file props [dict get $args -props]
40 if {[dict exists $args -user]} {
41 tdict set hog_file user [dict get $args -user]
47 proc Merge {dstVar src} {
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
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
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 }
63 tdict set dst props $dst_props
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] : ""}]
78 set result [tdict create]
79 tdict for {path fobj} $hog_files {
82 tlist foreach lib [tdict get $fobj libraries] {
83 if {[regexp -- $lib_pat [tobj value $lib]]} { set ok 1; break }
85 if {!$ok} { continue }
89 tlist foreach fs [tdict get $fobj filesets] {
90 if {[regexp -- $fs_pat [tobj value $fs]]} { set ok 1; break }
92 if {!$ok} { continue }
94 if {$file_pat ne "" && ![regexp -- $file_pat $path]} { continue }
95 tdict set result $path $fobj
101 namespace eval Codec {
122 proc RegisterCodec {pattern codec_dict} {
124 set row [dict create \
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]] : ""}] \
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]}
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]}
149 proc _FindCodec {ext tool} {
151 set tool [
string toupper $tool]
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}
161 return [
expr {$tool_match ne "" ? $tool_match : $global_match}]
165 proc IsRegistered {file {tool ""}} {
166 return [
expr {[_FindCodec [file extension $file] $tool] ne ""}]
170 proc ExtractMeta {raw_tokens default_lib} {
171 set file_lib $default_lib
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]
184 return [list $file_lib $ref_path $remaining]
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]
197 tdict set props $p [
tstr "1"]
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} {
203 foreach item [
split $val ","] {
204 set item [
string trim $item]
205 if {$item ne ""} {
lappend items [
tstr $item]}
207 tdict set props $key [tlist create {*}$items]
209 tdict set props $key [
tstr $val]
220 proc Decode {file list_ext file_lib fileset raw_tokens tool} {
221 set ext [
file extension $file]
226 tdict set hog_file libraries [tlist create [
tstr "others.src"]]
233 set pre [dict get $codec decoder_pre]
234 if {$pre ne ""} {
eval $pre}
235 if {$_done} {
return $hog_file}
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]]]
245 set post [dict get $codec decoder_post]
246 if {$post ne ""} {
eval $post}
255 proc EncodeProp {key v} {
256 if {[tobj type $v] eq "List"} {
258 tlist foreach item $v { lappend items [tobj value $item] }
259 return "${key}=[
join $items ","]"
261 set val [tobj value $v]
262 if {$val eq "1"} {
return $key}
263 return "${key}=${val}"
267 proc EncodeProps {hog_file {tool ""}} {
268 set ext [tdict getval $hog_file ext]
274 set pre [
expr {$codec ne "" ? [dict get $codec encoder_pre] : ""}]
275 if {$pre ne ""} {
eval $pre}
276 if {$_done} {
return $tokens}
279 tdict for {k v} [tdict get $hog_file props] {
280 lappend tokens [EncodeProp $k $v]
284 set post [
expr {$codec ne "" ? [dict get $codec encoder_post] : ""}]
285 if {$post ne ""} {
eval $post}
291 proc Encode {hog_file root_path list_ext {default_lib ""} {tool ""}} {
292 set path [tdict getval $hog_file path]
294 set tokens [list $rel]
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}"}
306 set tokens [
concat $tokens [
EncodeProps $hog_file $tool]]
307 return [
join $tokens " "]
315 library_rule {${file_lib}${list_ext}}
318 set props [tdict get $hog_file props]
320 foreach year {93 1987 1993 2008 2019} {
321 if {[tdict exists $props $year]} {
323 set props [tdict remove $props $year]
327 tdict set props std [tstr $std]
328 tdict set hog_file props $props
333 tdict for {k v} [tdict get $hog_file props] {
335 lappend tokens [tobj value $v]
337 lappend tokens [EncodeProp $k $v]
346 RegisterCodec {\.svp?$} {library_rule {${file_lib}${list_ext}}}
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]
361 tdict set hog_file props $props
366 set props [tdict get $hog_file props]
367 if {[tdict exists $props generated_vhd]} {
368 lappend tokens [tdict getval $props generated_vhd]
370 tdict for {k v} $props {
371 if {$k eq "generated_vhd"} { continue }
372 lappend tokens [EncodeProp $k $v]
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"}]}}
383 RegisterCodec {\.c$|\.cpp$|\.hpp?$} {library_rule {${file_lib}${list_ext}}}
384 RegisterCodec {\.cfg$} {library_rule {${file_lib}${list_ext}}}