Hog v10.58.0
pre-synthesis.tcl
Go to the documentation of this file.
1 #!/usr/bin/env tclsh
2 # Copyright 2018-2026 The University of Birmingham
3 # Copyright 2018-2026 Max-Planck-Institute for Physics
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 # @file
18 # The pre synthesis script checks the status of your git repository and stores into a set of variables that are fed as generics to the HDL project.
19 # This script is automatically integrated into the Vivado/Quartus workflow by the Create Project script.
20 
21 ##nagelfar variable quartus
22 ##nagelfar variable project
23 
24 set tcl_path [file normalize "[file dirname [info script]]/.."]
25 source $tcl_path/hog.tcl
26 
27 # Import tcllib
28 if {[IsSynplify] || [IsDiamond]} {
29  if {[info exists env(HOG_TCLLIB_PATH)]} {
30  lappend auto_path $env(HOG_TCLLIB_PATH)
31  } else {
32  puts "ERROR: To run Hog with Microsemi Libero SoC, you need to define the HOG_TCLLIB_PATH variable."
33  return
34  }
35 }
36 
37 if {[catch {package require struct::matrix} ERROR]} {
38  puts "$ERROR\n If you are running this script on tclsh, you can fix this by installing 'tcllib'"
39  return
40 }
41 
42 
43 if {[IsISE]} {
44  # Vivado + PlanAhead
45  set project [file tail $project_name]
46  set old_path [file normalize "../../Projects/${project_name}/$project.runs/synth_1"]
47  file mkdir $old_path
48 } else {
49  set old_path [pwd]
50 }
51 
52 if {[info exists env(HOG_EXTERNAL_PATH)]} {
53  set ext_path $env(HOG_EXTERNAL_PATH)
54  Msg Info "Found environment variable HOG_EXTERNAL_PATH, setting path for external files to $ext_path..."
55 } else {
56  set ext_path ""
57 }
58 
59 if {[IsXilinx]} {
60  # Vivado + PlanAhead
61  if {[IsISE]} {
62  set proj_file "[get_property DIRECTORY [current_project]]/$project.prr"
63  } else {
64  set proj_file [get_property parent.project_path [current_project]]
65  }
66  set proj_dir [file normalize [file dirname $proj_file]]
67  set proj_name [file rootname [file tail $proj_file]]
68 } elseif {[IsQuartus]} {
69  # Quartus
70  set proj_name [lindex $quartus(args) 1]
71  #set proj_dir [file normalize "$repo_path/Projects/$proj_name"]
72  set proj_dir [pwd]
73  set proj_file [file normalize "$proj_dir/$proj_name.qpf"]
74  # Test generated files
75  set hogQsysFileName [file normalize "$proj_dir/.hog/.hogQsys.md5"]
76  if {[file exists $hogQsysFileName] != 0} {
77  set hogQsysFile [open $hogQsysFileName r]
78  set hogQsysFileLines [split [read $hogQsysFile] "\n"]
79  foreach line $hogQsysFileLines {
80  set fileEntry [split $line "\t"]
81  set fileEntryName [lindex $fileEntry 0]
82  if {$fileEntryName != ""} {
83  if {[file exists $fileEntryName]} {
84  set newMd5Sum [Md5Sum $fileEntryName]
85  set oldMd5Sum [lindex $fileEntry 1]
86  if {$newMd5Sum != $oldMd5Sum} {
87  Msg Warning "The checksum for file $fileEntryName is not equal to the one saved in $hogQsysFileName.\n\
88  New checksum $newMd5Sum, old checksum $oldMd5Sum. \
89  Please check the any changes in the file are correctly propagated to git!"
90  }
91  } else {
92  Msg Warning "File $fileEntryName not found... Will not check Md5Sum!"
93  }
94  }
95  }
96  }
97 } elseif {[IsSynplify]} {
98  set proj_dir [file normalize [file dirname "[project_data -dir]/../.."]]
99  set proj_name [file tail $proj_dir]
100  set project $proj_name
101 } elseif {[IsDiamond]} {
102  set proj_dir [file normalize "[pwd]/.."]
103  set proj_name [file tail $proj_dir]
104  set project $proj_name
105 } else {
106  #Tclssh
107  set proj_file $old_path/[file tail $old_path].xpr
108  set proj_dir [file normalize [file dirname $proj_file]]
109  set proj_name [file rootname [file tail $proj_file]]
110  Msg CriticalWarning "You seem to be running locally on tclsh, so this is a debug message. \
111  The project file will be set to $proj_file and was derived from the path you launched this script from: $old_path. \
112  If you want this script to work properly in debug mode, please launch it from the top folder of one project, \
113  for example Repo/Projects/fpga1/ or Repo/Top/fpga1/"
114 }
115 
116 Notify Info "Starting synthesis of $proj_name."
117 
118 # Go to repository path
119 set repo_path [file normalize "$tcl_path/../.."]
120 cd $repo_path
121 
122 set group [GetGroupName $proj_dir "$tcl_path/../.."]
123 
124 # Calculating flavour if any
125 set flavour [string map {. ""} [file extension $proj_name]]
126 if {$flavour != ""} {
127  if {[string is integer $flavour]} {
128  Msg Info "Project $proj_name has flavour = $flavour, the generic variable FLAVOUR will be set to $flavour"
129  } else {
130  Msg Warning "Project name has a unexpected non numeric extension, flavour will be set to -1"
131  set flavour -1
132  }
133 } else {
134  set flavour -1
135 }
136 
137 ######## Reset files before synthesis ###########
138 ResetRepoFiles "./Projects/hog_reset_files"
139 
140 # Getting all the versions and SHAs of the repository
141 set top_dir [file normalize $repo_path/Top/$group/$proj_name]
142 lassign [GetRepoVersions $top_dir $repo_path $ext_path] \
143 commit version hog_hash hog_ver \
144 top_hash top_ver \
145 libs hashes vers \
146 cons_ver cons_hash ext_names ext_hashes \
147 xml_hash xml_ver user_ip_repos user_ip_hashes user_ip_vers \
148 cheby_hash cheby_ver
149 
150 
151 set describe [GetHogDescribe $top_dir $repo_path]
152 set dst_dir [file normalize "bin/$group/$proj_name\-$describe"]
153 Msg Info "Creating $dst_dir..."
154 file mkdir $dst_dir/reports
155 
156 
157 #check list files and project properties
158 set confDict [dict create]
159 set allow_fail_on_check 0
160 set allow_fail_on_conf 0
161 set allow_fail_on_list 0
162 set allow_fail_on_git 0
163 set full_diff_log 0
164 if {[file exists "$tcl_path/../../Top/$group/$proj_name/hog.conf"]} {
165  set confDict [ReadConf "$tcl_path/../../Top/$group/$proj_name/hog.conf"]
166  set allow_fail_on_check [DictGet [DictGet $confDict "hog"] "ALLOW_FAIL_ON_CHECK" 0]
167  set allow_fail_on_conf [DictGet [DictGet $confDict "hog"] "ALLOW_FAIL_ON_CONF" 0]
168  set allow_fail_on_list [DictGet [DictGet $confDict "hog"] "ALLOW_FAIL_ON_LIST" 0]
169  set allow_fail_on_git [DictGet [DictGet $confDict "hog"] "ALLOW_FAIL_ON_GIT" 0]
170  set full_diff_log [DictGet [DictGet $confDict "hog"] "FULL_DIFF_LOG" 0]
171 }
172 
173 
174 set this_commit [GetSHA]
175 
176 if {[IsVivado] || [IsSynplify] || [IsDiamond]} {
177  Msg Info "Running list file checker..."
178  ##nagelfar ignore
179  if {![string equal ext_path ""]} {
180  set argv [list "-ext_path" "$ext_path" "-project" "$group/$proj_name" "-outDir" "$dst_dir" "-log" "[expr {!$allow_fail_on_check}]"]
181  } else {
182  set argv [list "-project" "$group/$proj_name" "-outDir" "$dst_dir" "-log" "[expr {!$allow_fail_on_check}]"]
183  }
184  source $tcl_path/utils/check_list_files.tcl
185  if {[file exists "$dst_dir/diff_list_and_conf.txt"]} {
186  # SrcListErrorCnt, ConListErrorCnt and ConfErrorCnt are set by check_list_files.tcl
187  set list_mismatch [expr {$SrcListErrorCnt + $ConListErrorCnt > 0}]
188  set conf_mismatch [expr {$ConfErrorCnt > 0}]
189  if {($list_mismatch && !$allow_fail_on_list) || ($conf_mismatch && !$allow_fail_on_conf)} {
190  Msg CriticalWarning "Project list or hog.conf mismatch, will use current SHA ($this_commit) and version will be set to 0."
191  set commit 0000000
192  set version 00000000
193  } elseif {$list_mismatch || $conf_mismatch} {
194  Msg Warning "Project list or hog.conf mismatch found, but ignored because ALLOW_FAIL_ON_LIST/ALLOW_FAIL_ON_CONF is set in hog.conf."
195  }
196  }
197 } elseif {[IsQuartus]} {
198  # Quartus
199  #TO BE IMPLEMENTED
200 } else {
201  #Tclssh
202 }
203 
204 Msg Info "Evaluating non committed changes..."
205 set found_uncommitted 0
206 set diff [Git diff]
207 set diff_stat [Git "diff --stat"]
208 if {$diff != ""} {
209  set found_uncommitted 1
210  Msg Warning "Found non committed changes:..."
211  if {$full_diff_log} {
212  Msg Status "$diff"
213  } else {
214  Msg Status "$diff_stat"
215  }
216  set fp [open "$dst_dir/diff_presynthesis.txt" w+]
217  puts $fp "$diff"
218  close $fp
219  Msg CriticalWarning "Repository is not clean, will use current SHA ($this_commit) and create a dirty bitfile..."
220 }
221 
222 lassign [GetHogFiles -ext_path "$ext_path" "$tcl_path/../../Top/$group/$proj_name/list/" "$tcl_path/../../"] listLibraries listProperties
223 
224 if {!$allow_fail_on_git} {
225  foreach library [dict keys $listLibraries] {
226  set fileNames [dict get $listLibraries $library]
227  foreach fileName $fileNames {
228  if {[FileCommitted $fileName] == 0} {
229  set fp [open "$dst_dir/diff_presynthesis.txt" a+]
230  set found_uncommitted 1
231  puts $fp "\n[Relative $tcl_path/../../ $fileName] is not in the git repository"
232  Msg CriticalWarning "[Relative $tcl_path/../../ $fileName] is not in the git repository. \
233  Will use current SHA ($this_commit) and version will be set to 0."
234  close $fp
235  }
236  }
237  }
238 }
239 if {$found_uncommitted == 0} {
240  Msg Info "No uncommitted changes found."
241 } else {
242  set commit 0000000
243  set version 00000000
244 }
245 
246 
247 # Check if repository has v0.0.1 tag
248 lassign [GitRet "tag -l v0.0.1"] status result
249 if {$status == 1} {
250  Msg CriticalWarning "Repository does not have an initial v0.0.1 tag yet. Please create it with \"git tag v0.0.1\" "
251 }
252 
253 
254 Msg Info "Git describe for $commit is: $describe"
255 
256 if {$commit == 0} {
257  set commit $this_commit
258 } else {
259  Msg Info "Found last SHA for $proj_name: $commit"
260  if {$commit != $this_commit} {
261  set count [Git "rev-list --count $commit..$this_commit"]
262  Msg Info "The commit in which project $proj_name was last modified is $commit, that is $count commits older than current commit $this_commit."
263  }
264 }
265 
266 if {$xml_hash != ""} {
267  set xml_dst [file normalize $old_path/../xml]
268  Msg Info "Creating XML directory $xml_dst..."
269  file mkdir $xml_dst
270  Msg Info "Copying xml files to $xml_dst and replacing placeholders with xml version $xml_ver..."
271  CopyIPbusXMLs ./Top/$group/$proj_name/ $repo_path $xml_dst [HexVersionToString $xml_ver] $xml_hash
272  set use_ipbus 1
273 } else {
274  set use_ipbus 0
275 }
276 
277 if {$cheby_hash != ""} {
278  set cheby_dst [file normalize $old_path/../cheby]
279  Msg Info "Creating Cheby directory $cheby_dst..."
280  file mkdir $cheby_dst
281  Msg Info "Verifying Cheby outputs against the .chb list files..."
282  set cheby_tool "cheby"
283  if {[info exists env(HOG_CHEBY_TOOL)]} {
284  set cheby_tool $env(HOG_CHEBY_TOOL)
285  }
286  CopyChebyFiles ./Top/$group/$proj_name/ $repo_path $cheby_dst \
287  [HexVersionToString $cheby_ver] $cheby_hash 0 "all" $cheby_tool 0 0
288  set use_cheby 1
289 } else {
290  set use_cheby 0
291 }
292 
293 #number of threads
294 if {![IsDiamond]} {
295  set maxThreads [GetMaxThreads [file normalize ./Top/$group/$proj_name/]]
296 
297  if {$maxThreads != 1} {
298  Msg Warning "Multithreading enabled. Bitfile will not be deterministic. Number of threads: $maxThreads"
299  } else {
300  Msg Info "Disabling multithreading to assure deterministic bitfile"
301  }
302 
303  if {[IsXilinx]} {
304  ### Vivado
305  set_param general.maxThreads $maxThreads
306  } elseif {[IsQuartus]} {
307  # QUARTUS
308  if {[catch {package require ::quartus::project} ERROR]} {
309  Msg Error "$ERROR\n Can not find package ::quartus::project"
310  cd $old_path
311  return 1
312  }
313  set this_dir [pwd]
314  cd $proj_dir
315  project_open $proj_name -current_revision
316  cd $this_dir
317  set_global_assignment -name NUM_PARALLEL_PROCESSORS $maxThreads
318  project_close
319  } elseif {[IsSynplify]} {
320  set_option -max_parallel_jobs $maxThreads
321  }
322 }
323 set clock_seconds [clock seconds]
324 set tt [clock format $clock_seconds -format {%d/%m/%Y at %H:%M:%S}]
325 
326 if {[GitVersion 2.9.3]} {
327  set date [Git "log -1 --format=%cd --date=format:%d%m%Y $commit"]
328  set timee [Git "log -1 --format=%cd --date=format:00%H%M%S $commit"]
329 } else {
330  Msg Warning "Found Git version older than 2.9.3. Using current date and time instead of commit time."
331  set date [clock format $clock_seconds -format {%d%m%Y}]
332  set timee [clock format $clock_seconds -format {00%H%M%S}]
333 }
334 
335 ##### Passing Hog generic to top file
336 if {[IsXilinx] || [IsSynplify] || [IsDiamond]} {
337  ### VIVADO
338  set proj_group_and_name "$group/$proj_name"
339  # set global generic variables
340  if {[IsDiamond]} {
341  prj_project open $proj_dir/$proj_name.ldf
342  }
343  WriteGenerics "synth" $repo_path $proj_group_and_name $date $timee $commit $version \
344  $top_hash $top_ver $hog_hash $hog_ver \
345  $cons_ver $cons_hash $libs $vers \
346  $hashes $ext_names $ext_hashes \
347  $user_ip_repos $user_ip_vers $user_ip_hashes \
348  $flavour $xml_ver $xml_hash $cheby_ver $cheby_hash
349 
350  if {[IsDiamond]} {
351  prj_project save
352  prj_project close
353  }
354  set status_file [file normalize "$old_path/../versions.txt"]
355 } elseif {[IsQuartus]} {
356  #Quartus
357  if {[catch {package require ::quartus::project} ERROR]} {
358  Msg Error "$ERROR\n Can not find package ::quartus::project"
359  cd $old_path
360  return 1
361  }
362  set this_dir [pwd]
363  cd $proj_dir
364  project_open $proj_name -current_revision
365  cd $this_dir
366 
367  set zero_ttb 00000000
368  set hog_generic_prefix [GetHogGenericPrefix "$group/$proj_name" $repo_path]
369 
370  binary scan [binary format H* [string map {{'} {}} $date]] B32 bits
371  set_parameter -name [HogGenericName GLOBAL_DATE $hog_generic_prefix] $bits
372  binary scan [binary format H* [string map {{'} {}} $timee]] B32 bits
373  set_parameter -name [HogGenericName GLOBAL_TIME $hog_generic_prefix] $bits
374  binary scan [binary format H* [string map {{'} {}} $version]] B32 bits
375  set_parameter -name [HogGenericName GLOBAL_VER $hog_generic_prefix] $bits
376  binary scan [binary format H* [string map {{'} {}} $commit]] B32 bits
377  set_parameter -name [HogGenericName GLOBAL_SHA $hog_generic_prefix] $bits
378  binary scan [binary format H* [string map {{'} {}} $top_hash]] B32 bits
379  set_parameter -name [HogGenericName TOP_SHA $hog_generic_prefix] $bits
380  binary scan [binary format H* [string map {{'} {}} $top_ver]] B32 bits
381  set_parameter -name [HogGenericName TOP_VER $hog_generic_prefix] $bits
382  binary scan [binary format H* [string map {{'} {}} $hog_hash]] B32 bits
383  set_parameter -name [HogGenericName HOG_SHA $hog_generic_prefix] $bits
384  binary scan [binary format H* [string map {{'} {}} $hog_ver]] B32 bits
385  set_parameter -name [HogGenericName HOG_VER $hog_generic_prefix] $bits
386  binary scan [binary format H* [string map {{'} {}} $cons_ver]] B32 bits
387  set_parameter -name [HogGenericName CON_VER $hog_generic_prefix] $bits
388  binary scan [binary format H* [string map {{'} {}} $cons_hash]] B32 bits
389  set_parameter -name [HogGenericName CON_SHA $hog_generic_prefix] $bits
390 
391  if {$use_ipbus == 1} {
392  binary scan [binary format H* [string map {{'} {}} $xml_ver]] B32 bits
393  set_parameter -name [HogGenericName XML_VER $hog_generic_prefix] $bits
394  binary scan [binary format H* [string map {{'} {}} $xml_hash]] B32 bits
395  set_parameter -name [HogGenericName XML_SHA $hog_generic_prefix] $bits
396  }
397 
398  if {$use_cheby == 1} {
399  binary scan [binary format H* [string map {{'} {}} $cheby_ver]] B32 bits
400  set_parameter -name CHEBY_VER $bits
401  binary scan [binary format H* [string map {{'} {}} $cheby_hash]] B32 bits
402  set_parameter -name CHEBY_SHA $bits
403  }
404 
405  #set project specific lists
406  foreach l $libs v $vers h $hashes {
407  binary scan [binary format H* [string map {{'} {}} $v]] B32 bits
408  set_parameter -name [HogGenericName "[string toupper $l]_VER" $hog_generic_prefix] $bits
409  binary scan [binary format H* [string map {{'} {}} $h]] B32 bits
410  set_parameter -name [HogGenericName "[string toupper $l]_SHA" $hog_generic_prefix] $bits
411  }
412 
413  foreach e $ext_names h $ext_hashes {
414  binary scan [binary format H* [string map {{'} {}} $h]] B32 bits
415  set_parameter -name [HogGenericName "[string toupper $e]_SHA" $hog_generic_prefix] $bits
416  }
417 
418  if {$flavour != -1} {
419  set_parameter -name [HogGenericName FLAVOUR $hog_generic_prefix] $flavour
420  }
421 
422  if {![file exists "$old_path/output_files"]} {
423  file mkdir "$old_path/output_files"
424  }
425 
426  set status_file "$old_path/output_files/versions.txt"
427  project_close
428 } else {
429  ### Tcl Shell
430  puts "Hog:DEBUG GLOBAL_DATE=$date GLOBAL_TIME=$timee"
431  puts "Hog:DEBUG GLOBAL_SHA=$commit TOP_SHA=$top_hash"
432  puts "Hog:DEBUG CON_VER=$cons_ver CON_SHA=$cons_hash"
433  puts "Hog:DEBUG XML_SHA=$xml_hash GLOBAL_VER=$version TOP_VER=$top_ver"
434  puts "Hog:DEBUG XML_VER=$xml_ver HOG_SHA=$hog_hash HOG_VER=$hog_ver"
435  puts "Hog:DEBUG CHEBY_SHA=$cheby_hash CHEBY_VER=$cheby_ver"
436  puts "Hog:DEBUG LIBS: $libs $vers $hashes"
437  puts "Hog:DEBUG EXT: $ext_names $ext_hashes"
438  puts "Hog:DEBUG FLAVOUR: $flavour"
439  set status_file "$old_path/versions.txt"
440 }
441 Msg Info "Opening version file $status_file..."
442 set status_file [open $status_file "w+"]
443 
444 Msg Status " ------------------------- PRE SYNTHESIS -------------------------"
445 Msg Status " $tt"
446 Msg Status " Firmware date and time: $date, $timee"
447 if {$flavour != -1} {
448  Msg Status " Project flavour: $flavour"
449 }
450 
451 set version [HexVersionToString $version]
452 if {$group != ""} {
453  puts $status_file "## $group/$proj_name Version Table\n"
454 } else {
455  puts $status_file "## $proj_name Version Table\n"
456 }
457 
458 struct::matrix m
459 m add columns 7
460 
461 m add row "| \"**File set**\" | \"**Commit SHA**\" | **Version** |"
462 m add row "| --- | --- | --- |"
463 Msg Status " Global SHA: $commit, VER: $version"
464 m add row "| Global | $commit | $version |"
465 
466 set cons_ver [HexVersionToString $cons_ver]
467 Msg Status " Constraints SHA: $cons_hash, VER: $cons_ver"
468 m add row "| Constraints | $cons_hash | $cons_ver |"
469 
470 if {$use_ipbus == 1} {
471  set xml_ver [HexVersionToString $xml_ver]
472  Msg Status " IPbus XML SHA: $xml_hash, VER: $xml_ver"
473  m add row "| \"IPbus XML\" | $xml_hash | $xml_ver |"
474 }
475 if {$use_cheby == 1} {
476  set cheby_ver [HexVersionToString $cheby_ver]
477  Msg Status " Cheby SHA: $cheby_hash, VER: $cheby_ver"
478  m add row "| \"Cheby\" | $cheby_hash | $cheby_ver |"
479 }
480 set top_ver [HexVersionToString $top_ver]
481 Msg Status " Top SHA: $top_hash, VER: $top_ver"
482 m add row "| \"Top Directory\" | $top_hash | $top_ver |"
483 
484 set hog_ver [HexVersionToString $hog_ver]
485 Msg Status " Hog SHA: $hog_hash, VER: $hog_ver"
486 m add row "| Hog | $hog_hash | $hog_ver |"
487 
488 Msg Status " --- Libraries ---"
489 foreach l $libs v $vers h $hashes {
490  set v [HexVersionToString $v]
491  Msg Status " $l SHA: $h, VER: $v"
492  m add row "| \"**Lib:** $l\" | $h | $v |"
493 }
494 
495 if {[llength $user_ip_repos] > 0} {
496  Msg Status " --- User IP Repositories ---"
497  foreach r $user_ip_repos v $user_ip_vers h $user_ip_hashes {
498  set v [HexVersionToString $v]
499  set repo_name [file tail $r]
500  Msg Status " $repo_name SHA: $h, VER: $v"
501  m add row "| \"**Repo:** $repo_name\" | $h | $v |"
502  }
503 }
504 
505 if {[llength $ext_names] > 0} {
506  Msg Status " --- External Libraries ---"
507  foreach e $ext_names eh $ext_hashes {
508  Msg Status " $e SHA: $eh"
509  m add row "| \"**Ext:** $e\" | $eh | \" \" |"
510  }
511 }
512 
513 Msg Status " -----------------------------------------------------------------"
514 
515 puts $status_file [m format 2string]
516 puts $status_file "\n\n"
517 close $status_file
518 
519 if {[IsXilinx]} {
520  CheckYmlRef [file normalize $tcl_path/../..] true
521 }
522 
523 set user_pre_synthesis_file "./Top/$group/$proj_name/pre-synthesis.tcl"
524 if {[file exists $user_pre_synthesis_file]} {
525  Msg Info "Sourcing user pre-synthesis file $user_pre_synthesis_file"
526  source $user_pre_synthesis_file
527 }
528 
529 cd $old_path
530 
531 Msg Info "All done."