Hog v10.58.0
sigasi.tcl
Go to the documentation of this file.
1 # Note: This is mostly for the benefit of CI/CD pipelines as if using a code editor
2 # with Sigasi integration then code quality and formatting are easy to access
3 
4 # The CLI functionality requires a paid license.
5 
6 # These proceedures should be available regardless of which synthesis tool is used.
7 
8 namespace eval Tools::Sigasi {
9  Tools::RegisterTool [namespace current] {
10  name "sigasi-cli"
11  vendor "Sigasi"
12  ref_name {sigasi}
13  description "Sigasi-cli toolchain"
14  }
15 
16  RegisterCommand export {
17  aliases {sigasi-e e}
18  description " exports a CSV file for sigasi project creation "
19  requires_proj true
20  options {
21  {new "Create a new style Sigasi project file"}
22  }
23  script { Tools::Sigasi::sigasi-export}
24  }
25  RegisterCommand format {
26  aliases {sigasi-f f}
27  requires_proj true
28  description " sigasi-cli wrapper to run repo formatting settings across all files in the repo "
29  script { Tools::Sigasi::sigasi-format }
30  }
31  RegisterCommand document {
32  aliases {sigasi-d doc d docs}
33  description " sigasi-cli wrapper to create basic documentation of the project "
34  requires_proj true
35  script {Tools::Sigasi::sigasi-document}
36  }
37  RegisterCommand lint {
38  aliases {sigasi-l l}
39  description " sigasi-cli wrapper to generate a code quality report "
40  requires_proj true
41  script {Tools::Sigasi::sigasi-lint}
42  }
43 
44  proc Initialize {} {
45  puts "Initializing Sigasi tool..."
46  if { [catch {exec which sigasi-cli} ] } {
47  puts "sigasi-cli not available -- check sigasi-cli is in PATH"
48  exit 1
49  } else {
50  if { [catch {exec -- sigasi-cli --license } ] } {
51  puts "check SIGASI_LM_LICENSE_FILE is set"
52  exit 1
53  }
54  }
55  # check that the rest of Hog is existing as expected
56  if { ![namespace exists ::Repo] } {
57  Msg Error " sigasi has been called before Repo namespace has been created "
58  }
59  }
60 
61  # tests if sigasi project file exists. If it does not exist it creates a new project.
62  proc _test_sigasi_project {repo_path} {
63  if { ![file exists $repo_path/project.sigasi] } {
64  Msg Info "attempting to create associated sigasi project file"
66  return "yes"
67  }
68  return "no"
69  }
70 
71  # create a default dict. Then we can merge in the repo.conf dict (overwriting) followed by the project dict (overwriting)
72  proc _create_sigasi_default_dict {} {
73  dict set sigasi_defaults DIAGRAMS "LINKED"
74  dict set sigasi_defaults DESIGN_UNITS_PER_PAGE 25
75  dict set sigasi_defaults DIAGRAM_NODE_LIMIT 2500
76  dict set sigasi_defaults DOC_DIR "./sigasi-docs"
77  dict set sigasi_defaults REPORT_FORMAT "plain"
78  dict set sigasi_defaults REPORT_OUTFILE "./sigasi_report.txt"
79  dict set sigasi_defaults VHDL_VERSION 2008
80  dict set sigasi_defaults KEYWORDS "lowercase"
81  dict set sigasi_defaults TAB_WIDTH 4
82 
83  return $sigasi_defaults
84  }
85 
86  # create default and merge with repo and project dicts from config files
87  proc get_sigasi_conf {} {
88  set default_dict [_create_sigasi_default_dict]
89  set out_dict $default_dict
90 
91  Msg Info " retrieving the sigasi configuration settings "
92  if { [Repo::Exists config sigasi ] } {
93  set sigasi_dict [ ::tobj::native [ Repo::Get config sigasi ]]
94  Msg Debug "debug merge 1 $sigasi_dict "
95  set out_dict [dict merge $default_dict $sigasi_dict]
96  }
97 
98  set config [ CurrentProject::Get config ]
99  if { [ CurrentProject::Exists config sigasi ] } {
100  set sigasi_dict [ ::tobj::native [ CurrentProject::Get config sigasi ]]
101  Msg Debug "debug merge 2 $sigasi_dict "
102  set out_dict [dict merge $out_dict $sigasi_dict]
103  }
104 
105  return $out_dict
106  }
107 
108  proc sigasi-export {} {
109  if {[Launcher::GetOr options new 0] == 1} {
111  exit 0
112  }
113  set repo_path [Repo::Get repo_path]
114  set project_name [Launcher::Get project_name]
115  Msg Info "Creating Sigasi CSV files for project $project_name..."
116 
117  set project_files [ ::tobj::native [ CurrentProject::Get project_files ] ]
118  # puts " debug project_files $project_files"
119 
120  set csv_file [open "$repo_path/sigasi_$project_name.csv" w]
121 
122  foreach source_file $project_files {
123  if {
124  [file extension $source_file] eq ".vhd"
125  || [file extension $source_file] eq ".vhdl"
126  || [file extension $source_file] eq ".sv"
127  || [file extension $source_file] eq ".v"
128  } {
129  set file_dict [dict get $project_files $source_file]
130  puts $csv_file [concat [file rootname [dict get $file_dict libraries]] "," $source_file]
131  }
132  }
133 
134  close $csv_file
135  Msg Info "Sigasi CSV file created: sigasi_$project_name.csv"
136  Msg Info "You can use the python script provided by Sigasi to convert the generated csv file into a Sigasi project."
137  Msg Info "More info at: \
138  https://www.sigasi.com/knowledge/how_tos/generating-sigasi-project-vivado-project/#2-generate-the-sigasi-project-files-from-the-csv-file"
139  exit 0
140  }
141 
142  proc _new_sigasi-export {} {
143  set repo_path [Repo::Get repo_path]
144  set project_name [Launcher::Get project_name]
145  Msg Info "Creating new style Sigasi project file for project $project_name..."
146 
147  set project_files [ ::tobj::native [ CurrentProject::Get project_files ] ]
148 
149  DataStore::create Sigasi
150  Sigasi::Set name $project_name
151  Sigasi::Set targets ""
152  Sigasi::Set targets hdl
153  Sigasi::Set targets hdl libraryMapping
154  Sigasi::Set targets hdl libraryMapping [::tobj::create Dict {}]
155 
156  foreach source_file $project_files {
157  if {
158  [file extension $source_file] eq ".vhd"
159  || [file extension $source_file] eq ".vhdl"
160  || [file extension $source_file] eq ".sv"
161  || [file extension $source_file] eq ".v"
162  } {
163  set file_dict [dict get $project_files $source_file]
164  set lib [file rootname [dict get $file_dict libraries]]
165  Sigasi::Set targets hdl libraryMapping $source_file $lib
166  # puts " $source_file $lib "
167  }
168  }
169 
170  Sigasi::SaveJsonToFile "$repo_path/project.sigasi" 1
171  Msg Info "Sigasi project file created: $repo_path/project.sigasi"
172  }
173 
174  proc sigasi-lint {} {
175 
176  set repo_path [Repo::Get repo_path]
177  set project_name [Launcher::Get project_name]
178  set remove_project_file [_test_sigasi_project $repo_path]
179  Msg Info "Generating a code quality report for project $project_name..."
180 
181  file mkdir $repo_path/sigasi_lint
182  set outfile "$repo_path/sigasi_lint/$project_name"
183 
184  set options_list ""
185 
186  set sigasi_conf [get_sigasi_conf]
187  #Msg Info "sigasi config dict: $sigasi_conf"
188 
189  if {[dict exists $sigasi_conf LIBRARY_DATABASE]} {
190  set libraries [dict get $sigasi_conf LIBRARY_DATABASE]
191  foreach lib [split $libraries ,] {
192  lappend options_list "--library-database=[string trim $lib]"
193  }
194  }
195 
196  set report_format [dict get $sigasi_conf REPORT_FORMAT]
197  lappend options_list "--$report_format"
198 
199  if { $report_format == "json" || $report_format == "sonarqube" } {
200  set filetype ".json"
201  } elseif { $report_format == "warnings-ng" } {
202  set filetype ".xml"
203  } else {
204  set filetype ".txt"
205  }
206 
207  if {[dict exists $sigasi_conf REPORT_ABSOLUTE]} {
208  lappend options_list "--absolute"
209  }
210  if {[dict exists $sigasi_conf REPORT_SUPPRESSED]} {
211  lappend options_list "--include-suppressed"
212  }
213  if {[dict exists $sigasi_conf FAIL]} {
214  lappend options_list "--fail-on-[dict get $sigasi_conf FAIL]"
215  }
216 
217  lappend options_list "--out=$outfile$filetype"
218 
219  puts "sigasi-cli verify $repo_path $options_list "
220  if { [catch { exec -- sigasi-cli verify $repo_path {*}$options_list } ] } {
221  Msg Error "sigasi-cli verify failed - please review $outfile$filetype for issues"
222  exit 1
223  }
224 
225  Msg Info "Despite passing there maybe minor code violations caught. See $outfile$filetype"
226  if { $remove_project_file == "yes" } {
227  file delete $repo_path/project.sigasi
228  }
229  exit 0
230  }
231 
232  proc sigasi-format {} {
233 
234  set repo_path [Repo::Get repo_path]
235  set project_name [Launcher::Get project_name]
236  Msg Info "Formatting VHDL files for project $project_name ..."
237 
238  set options_list ""
239  set ignore_list ""
240 
241  set sigasi_conf [get_sigasi_conf]
242  Msg Info "sigasi config dict: $sigasi_conf"
243 
244  if {[dict exists $sigasi_conf TABS]} {
245  lappend options_list ""
246  } else {
247  lappend options_list "--spaces-for-tabs"
248  }
249  if {[dict exists $sigasi_conf PRESERVE_NEWLINES]} {
250  lappend options_list "--preserve-newlines"
251  }
252  if {[dict exists $sigasi_conf NO_ALIGN]} {
253  lappend options_list "--no-align"
254  }
255 
256  lappend options_list "--vhdl-version=[dict get $sigasi_conf VHDL_VERSION]"
257  lappend options_list "--tab-width=[dict get $sigasi_conf TAB_WIDTH]"
258  lappend options_list "--keywords=[dict get $sigasi_conf KEYWORDS]"
259 
260  if {[dict exists $sigasi_conf IGNORE_DIR]} {
261  set ignore [dict get $sigasi_conf IGNORE_DIR]
262  foreach dir [split $ignore ,] {
263  lappend ignore_list "$repo_path/[string trim $dir]"
264  }
265  }
266 
267  set project_files [ ::tobj::native [ CurrentProject::Get project_files ] ]
268  # Msg Debug " debug project_files $project_files"
269 
270  foreach source_file $project_files {
271  set skip 0
272  foreach dir $ignore_list {
273  if { [string match $dir* $source_file] } {
274  set skip 1
275  }
276  }
277  if { $skip == 1 } {
278  puts "skipping $source_file due to IGNORE_DIR setting"
279  } else {
280  if {[file extension $source_file] == ".vhd" ||
281  [file extension $source_file] == ".vhdl" } {
282  puts "sigasi-cli format $options_list $source_file"
283  exec -- sigasi-cli format {*}$options_list $source_file
284  }
285  }
286  }
287  exit 0
288  }
289 
290  proc sigasi-document {} {
291 
292  set repo_path [Repo::Get repo_path]
293  set project_name [Launcher::Get project_name]
294  set remove_project_file [_test_sigasi_project $repo_path]
295  Msg Info "Generating project documentation for $project_name..."
296 
297  set options_list ""
298 
299  set sigasi_conf [get_sigasi_conf]
300  Msg Info "sigasi config dict: $sigasi_conf"
301 
302  if {[dict exists $sigasi_conf LIBRARY_DATABASE]} {
303  set libraries [dict get $sigasi_conf LIBRARY_DATABASE]
304  foreach lib [split $libraries ,] {
305  lappend options_list "--library-database=[string trim $lib]"
306  }
307  }
308 
309  lappend options_list "--diagram-node-limit=[dict get $sigasi_conf DIAGRAM_NODE_LIMIT]"
310  lappend options_list "--design-units-per-page=[dict get $sigasi_conf DESIGN_UNITS_PER_PAGE]"
311  lappend options_list "--diagrams=[dict get $sigasi_conf DIAGRAMS]"
312  if {[dict exists $sigasi_conf TOP_LEVEL]} {
313  lappend options_list "--top-level=[dict get $sigasi_conf TOP_LEVEL]"
314  }
315  set outdir "$repo_path/sigasi_doc/$project_name"
316  lappend options_list "--output-directory=$outdir"
317 
318  puts "sigasi-cli document $repo_path $options_list "
319  try {
320  set results [exec -- sigasi-cli document $repo_path {*}$options_list 2>@1 ]
321  set status 0
322  } trap CHILDSTATUS {results} {
323  Msg Error "Generation of documentation has failed. Please debug sigasi command outside of HOG"
324  exit 1
325  }
326  Msg Info "Documentation successfully saved to $outdir"
327  if { $remove_project_file == "yes" } {
328  file delete $repo_path/project.sigasi
329  }
330  exit 0
331  }
332 }