-
Notifications
You must be signed in to change notification settings - Fork 39
/
script.rs
1125 lines (1081 loc) · 41.3 KB
/
script.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017-2018 ETH Zurich
// Fabian Schuiki <fschuiki@iis.ee.ethz.ch>
//! The `script` subcommand.
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use clap::builder::PossibleValue;
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
use indexmap::IndexSet;
use tera::{Context, Tera};
use tokio::runtime::Runtime;
use crate::error::*;
use crate::sess::{Session, SessionIo};
use crate::src::{SourceFile, SourceGroup};
use crate::target::{TargetSet, TargetSpec};
/// Assemble the `script` subcommand.
pub fn new() -> Command {
Command::new("script")
.about("Emit tool scripts for the package")
.arg(
Arg::new("target")
.short('t')
.long("target")
.help("Only include sources that match the given target")
.num_args(1)
.action(ArgAction::Append)
.value_parser(value_parser!(String)),
)
.arg(
Arg::new("no-default-target")
.long("no-default-target")
.help("Remove any default targets that may be added to the generated script")
.num_args(0)
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("format")
.help("Format of the generated script")
.required(true)
.num_args(1)
.value_parser([
PossibleValue::new("flist"),
PossibleValue::new("vsim"),
PossibleValue::new("vcs"),
PossibleValue::new("verilator"),
PossibleValue::new("synopsys"),
PossibleValue::new("formality"),
PossibleValue::new("riviera"),
PossibleValue::new("genus"),
PossibleValue::new("vivado"),
PossibleValue::new("vivado-sim"),
PossibleValue::new("precision"),
PossibleValue::new("template"),
PossibleValue::new("template_json"),
]),
)
.arg(
Arg::new("relative-path")
.long("relative-path")
.num_args(0)
.action(ArgAction::SetTrue)
.help("Use relative paths (flist generation only)"),
)
.arg(
Arg::new("define")
.short('D')
.long("define")
.help("Pass an additional define to all source files")
.num_args(1..)
.action(ArgAction::Append)
.value_parser(value_parser!(String)),
)
.arg(
Arg::new("vcom-arg")
.long("vcom-arg")
.help("Pass an argument to vcom calls (vsim/vhdlan/riviera only)")
.num_args(1..)
.action(ArgAction::Append)
.value_parser(value_parser!(String)),
)
.arg(
Arg::new("vlog-arg")
.long("vlog-arg")
.help("Pass an argument to vlog calls (vsim/vlogan/riviera only)")
.num_args(1..)
.action(ArgAction::Append)
.value_parser(value_parser!(String)),
)
.arg(
Arg::new("only-defines")
.long("only-defines")
.num_args(0)
.action(ArgAction::SetTrue)
.help("Only output commands to define macros (Vivado only)"),
)
.arg(
Arg::new("only-includes")
.long("only-includes")
.num_args(0)
.action(ArgAction::SetTrue)
.help("Only output commands to define include directories (Vivado only)"),
)
.arg(
Arg::new("only-sources")
.long("only-sources")
.num_args(0)
.action(ArgAction::SetTrue)
.help("Only output commands to define source files (Vivado only)"),
)
.arg(
Arg::new("no-simset")
.long("no-simset")
.num_args(0)
.action(ArgAction::SetTrue)
.help("Do not change `simset` fileset (Vivado only)"),
)
.arg(
Arg::new("vlogan-bin")
.long("vlogan-bin")
.help("Specify a `vlogan` command")
.num_args(1)
.default_value("vlogan")
.value_parser(value_parser!(String)),
)
.arg(
Arg::new("vhdlan-bin")
.long("vhdlan-bin")
.help("Specify a `vhdlan` command")
.num_args(1)
.default_value("vhdlan")
.value_parser(value_parser!(String)),
)
.arg(
Arg::new("no-abort-on-error")
.long("no-abort-on-error")
.num_args(0)
.action(ArgAction::SetTrue)
.help("Do not abort analysis/compilation on first caught error (only for programs that support early aborting)")
)
.arg(
Arg::new("compilation_mode")
.long("compilation-mode")
.help("Choose compilation mode option: separate/common")
.num_args(1)
.default_value("separate")
.value_parser([
PossibleValue::new("separate"),
PossibleValue::new("common"),
])
)
.arg(
Arg::new("package")
.short('p')
.long("package")
.help("Specify package to show sources for")
.num_args(1)
.action(ArgAction::Append)
.value_parser(value_parser!(String)),
)
.arg(
Arg::new("no_deps")
.short('n')
.long("no-deps")
.num_args(0)
.action(ArgAction::SetTrue)
.help("Exclude all dependencies, i.e. only top level or specified package(s)"),
)
.arg(
Arg::new("exclude")
.short('e')
.long("exclude")
.help("Specify package to exclude from sources")
.num_args(1)
.action(ArgAction::Append)
.value_parser(value_parser!(String)),
)
.arg(
Arg::new("template")
.long("template")
.required_if_eq("format", "template")
.help("Path to a file containing the tera template string to be formatted.")
.num_args(1)
.value_parser(value_parser!(String)),
)
}
fn get_package_strings<I>(packages: I) -> IndexSet<String>
where
I: IntoIterator,
I::Item: AsRef<str>,
{
packages
.into_iter()
.map(|t| t.as_ref().to_string().to_lowercase())
.collect()
}
/// Execute the `script` subcommand.
pub fn run(sess: &Session, matches: &ArgMatches) -> Result<()> {
let rt = Runtime::new()?;
let io = SessionIo::new(sess);
let mut srcs = rt.block_on(io.sources())?;
// Format-specific target specifiers.
let vivado_targets = &["vivado", "fpga", "xilinx"];
fn concat<T: Clone>(a: &[T], b: &[T]) -> Vec<T> {
a.iter().chain(b).cloned().collect()
}
let format = matches.get_one::<String>("format").unwrap();
let format_targets: Vec<&str> = if !matches.get_flag("no-default-target") {
match format.as_str() {
"flist" => vec!["flist"],
"vsim" => vec!["vsim", "simulation"],
"vcs" => vec!["vcs", "simulation"],
"verilator" => vec!["verilator", "synthesis"],
"synopsys" => vec!["synopsys", "synthesis"],
"formality" => vec!["synopsys", "synthesis", "formality"],
"riviera" => vec!["riviera", "simulation"],
"genus" => vec!["genus", "synthesis"],
"vivado" => concat(vivado_targets, &["synthesis"]),
"vivado-sim" => concat(vivado_targets, &["simulation"]),
"precision" => vec!["precision", "fpga", "synthesis"],
"template" => vec![],
"template_json" => vec![],
_ => unreachable!(),
}
} else {
vec![]
};
// Filter the sources by target.
let targets = matches
.get_many::<String>("target")
.map(|t| {
TargetSet::new(
t.map(|element| element.as_str())
.chain(format_targets.clone()),
)
})
.unwrap_or_else(|| TargetSet::new(format_targets));
srcs = srcs
.filter_targets(&targets)
.unwrap_or_else(|| SourceGroup {
package: Default::default(),
independent: true,
target: TargetSpec::Wildcard,
include_dirs: Default::default(),
export_incdirs: Default::default(),
defines: Default::default(),
files: Default::default(),
dependencies: Default::default(),
version: None,
});
// Filter the sources by specified packages.
let packages = &srcs.get_package_list(
sess,
&matches
.get_many::<String>("package")
.map(get_package_strings)
.unwrap_or_default(),
&matches
.get_many::<String>("exclude")
.map(get_package_strings)
.unwrap_or_default(),
matches.get_flag("no_deps"),
);
if matches.contains_id("package")
|| matches.contains_id("exclude")
|| matches.get_flag("no_deps")
{
srcs = srcs
.filter_packages(packages)
.unwrap_or_else(|| SourceGroup {
package: Default::default(),
independent: true,
target: TargetSpec::Wildcard,
include_dirs: Default::default(),
export_incdirs: Default::default(),
defines: Default::default(),
files: Default::default(),
dependencies: Default::default(),
version: None,
});
}
// Flatten the sources.
let srcs = srcs.flatten();
// Validate format-specific options.
if (matches.contains_id("vcom-arg") || matches.contains_id("vlog-arg"))
&& format != "vsim"
&& format != "vcs"
&& format != "riviera"
&& format != "template"
&& format != "template_json"
{
return Err(Error::new(
"vsim/vcs-only options can only be used for 'vcs', 'vsim' or 'riviera' format!",
));
}
if (matches.get_flag("only-defines")
|| matches.get_flag("only-includes")
|| matches.get_flag("only-sources")
|| matches.get_flag("no-simset"))
&& !format.starts_with("vivado")
&& format != "template"
&& format != "template_json"
{
return Err(Error::new(
"Vivado-only options can only be used for 'vivado' format!",
));
}
// Generate the corresponding output.
match format.as_str() {
"flist" => emit_template(sess, FLIST_TPL, matches, targets, srcs),
"vsim" => emit_template(sess, VSIM_TCL_TPL, matches, targets, srcs),
"vcs" => emit_template(sess, VCS_SH_TPL, matches, targets, srcs),
"verilator" => emit_template(sess, VERILATOR_SH_TPL, matches, targets, srcs),
"synopsys" => emit_template(sess, SYNOPSYS_TCL_TPL, matches, targets, srcs),
"formality" => emit_template(sess, FORMALITY_TCL_TPL, matches, targets, srcs),
"riviera" => emit_template(sess, RIVIERA_TCL_TPL, matches, targets, srcs),
"genus" => emit_template(sess, GENUS_TCL_TPL, matches, targets, srcs),
"vivado" => emit_template(sess, VIVADO_TCL_TPL, matches, targets, srcs),
"vivado-sim" => emit_template(sess, VIVADO_TCL_TPL, matches, targets, srcs),
"precision" => emit_template(sess, PRECISION_TCL_TPL, matches, targets, srcs),
"template" => {
let custom_tpl_path = Path::new(matches.get_one::<String>("template").unwrap());
let custom_tpl_str =
&String::from_utf8(fs::read(custom_tpl_path)?).map_err(|e| Error::chain("", e))?;
emit_template(sess, custom_tpl_str, matches, targets, srcs)
}
"template_json" => emit_template(sess, JSON, matches, targets, srcs),
_ => unreachable!(),
}
}
/// Subdivide the source files in a group.
///
/// The function `cateogrize` is used to assign a category to each source file.
/// Files with the same category that appear after each other will be kept in
/// the same source group. Files with different cateogries are split into
/// separate groups.
fn separate_files_in_group<F1, F2, T>(mut src: SourceGroup, categorize: F1, mut consume: F2)
where
F1: Fn(&SourceFile) -> Option<T>,
F2: FnMut(&SourceGroup, T, Vec<SourceFile>),
T: Eq,
{
let mut category = None;
let mut files = vec![];
for file in std::mem::take(&mut src.files) {
let new_category = categorize(&file);
if new_category.is_none() {
continue;
}
if category.is_some() && category != new_category && !files.is_empty() {
consume(&src, category.take().unwrap(), std::mem::take(&mut files));
}
files.push(file);
category = new_category;
}
if !files.is_empty() {
consume(&src, category.unwrap(), files);
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum SourceType {
Verilog,
Vhdl,
}
fn relativize_path(path: &std::path::Path, root: &std::path::Path) -> String {
if path.starts_with(root) {
format!(
"$ROOT/{}",
path.strip_prefix(root).unwrap().to_str().unwrap()
)
} else {
path.to_str().unwrap().to_string()
}
}
static HEADER_AUTOGEN: &str = "This script was generated automatically by bender.";
fn add_defines_from_matches(defines: &mut Vec<(String, Option<String>)>, matches: &ArgMatches) {
if let Some(d) = matches.get_many::<String>("define") {
defines.extend(d.map(|t| {
let mut parts = t.splitn(2, '=');
let name = parts.next().unwrap().trim(); // split always has at least one element
let value = parts.next().map(|v| v.trim().to_string());
(name.to_string(), value)
}));
}
}
static JSON: &str = "json";
fn emit_template(
sess: &Session,
template: &str,
matches: &ArgMatches,
targets: TargetSet,
srcs: Vec<SourceGroup>,
) -> Result<()> {
let mut tera_obj = Tera::default();
let mut tera_context = Context::new();
tera_context.insert("HEADER_AUTOGEN", HEADER_AUTOGEN);
tera_context.insert("root", sess.root);
// tera_context.insert("srcs", &srcs);
tera_context.insert("abort_on_error", &!matches.get_flag("no-abort-on-error"));
let mut defines: Vec<(String, Option<String>)> = vec![];
defines.extend(
targets
.iter()
.map(|t| (format!("TARGET_{}", t.to_uppercase()), None)),
);
add_defines_from_matches(&mut defines, matches);
defines.sort();
tera_context.insert("global_defines", &defines);
let mut all_defines = defines.clone();
let mut all_incdirs = vec![];
let mut all_files = vec![];
let mut all_verilog = vec![];
let mut all_vhdl = vec![];
for src in &srcs {
all_defines.extend(
src.defines
.iter()
.map(|(k, &v)| (k.to_string(), v.map(String::from))),
);
all_incdirs.append(&mut src.clone().get_incdirs());
all_files.append(&mut src.files.clone());
}
let all_defines = if !matches.get_flag("only-includes") && !matches.get_flag("only-sources") {
all_defines
} else {
vec![]
};
tera_context.insert("all_defines", &all_defines);
let all_incdirs: IndexSet<PathBuf> =
if !matches.get_flag("only-defines") && !matches.get_flag("only-sources") {
all_incdirs.into_iter().map(|p| p.to_path_buf()).collect()
} else {
IndexSet::new()
};
tera_context.insert("all_incdirs", &all_incdirs);
let all_files: IndexSet<PathBuf> =
if !matches.get_flag("only-defines") && !matches.get_flag("only-includes") {
all_files
.into_iter()
.filter_map(|file| match file {
SourceFile::File(p) => Some(p.to_path_buf()),
_ => None,
})
.collect()
} else {
IndexSet::new()
};
tera_context.insert("all_files", &all_files);
let mut split_srcs = vec![];
for src in srcs {
separate_files_in_group(
src,
|f| match f {
SourceFile::File(p) => match p.extension().and_then(std::ffi::OsStr::to_str) {
Some("sv") | Some("v") | Some("vp") => Some(SourceType::Verilog),
Some("vhd") | Some("vhdl") => Some(SourceType::Vhdl),
_ => None,
},
_ => None,
},
|src, ty, files| {
split_srcs.push(TplSrcStruct {
defines: {
let mut local_defines = defines.clone();
local_defines.extend(
src.defines
.iter()
.map(|(k, &v)| (k.to_string(), v.map(String::from))),
);
local_defines
},
incdirs: src
.clone()
.get_incdirs()
.iter()
.map(|p| p.to_path_buf())
.collect(),
files: files
.iter()
.map(|f| match f {
SourceFile::File(p) => p.to_path_buf(),
SourceFile::Group(_) => unreachable!(),
})
.collect(),
file_type: match ty {
SourceType::Verilog => "verilog".to_string(),
SourceType::Vhdl => "vhdl".to_string(),
},
});
},
);
}
for src in &split_srcs {
match src.file_type.as_str() {
"verilog" => {
all_verilog.append(&mut src.files.clone());
}
"vhdl" => {
all_vhdl.append(&mut src.files.clone());
}
_ => {}
}
}
let split_srcs = if !matches.get_flag("only-defines") && !matches.get_flag("only-includes") {
split_srcs
} else {
vec![]
};
tera_context.insert("srcs", &split_srcs);
let all_verilog: IndexSet<PathBuf> =
if !matches.get_flag("only-defines") && !matches.get_flag("only-includes") {
all_verilog.into_iter().collect()
} else {
IndexSet::new()
};
let all_vhdl: IndexSet<PathBuf> =
if !matches.get_flag("only-defines") && !matches.get_flag("only-includes") {
all_vhdl.into_iter().collect()
} else {
IndexSet::new()
};
tera_context.insert("all_verilog", &all_verilog);
tera_context.insert("all_vhdl", &all_vhdl);
let vlog_args: Vec<String> = if let Some(args) = matches.get_many::<String>("vlog-arg") {
args.map(Into::into).collect()
} else {
[].to_vec()
};
tera_context.insert("vlog_args", &vlog_args);
let vcom_args: Vec<String> = if let Some(args) = matches.get_many::<String>("vcom-arg") {
args.map(Into::into).collect()
} else {
[].to_vec()
};
tera_context.insert("vcom_args", &vcom_args);
tera_context.insert("vlogan_bin", &matches.get_one::<String>("vlogan-bin"));
tera_context.insert("vhdlan_bin", &matches.get_one::<String>("vhdlan-bin"));
tera_context.insert("relativize_path", &matches.get_flag("relative-path"));
tera_context.insert(
"compilation_mode",
&matches.get_one::<String>("compilation_mode"),
);
let vivado_filesets = if matches.get_flag("no-simset") {
vec![""]
} else {
vec!["", " -simset"]
};
tera_context.insert("vivado_filesets", &vivado_filesets);
if template == "json" {
println!("{:#}", tera_context.into_json());
return Ok(());
}
print!(
"{}",
tera_obj
.render_str(template, &tera_context)
.map_err(|e| { Error::chain("Failed to render template.", e) })?
);
Ok(())
}
#[derive(Debug, Serialize)]
struct TplSrcStruct {
defines: Vec<(String, Option<String>)>,
incdirs: Vec<PathBuf>,
files: Vec<PathBuf>,
file_type: String,
}
static FLIST_TPL: &str = "\
{% for incdir in all_incdirs %}\
{% if relativize_path %}\
+incdir+{{ incdir | replace(from=root, to='') }}\n\
{% else %}\
+incdir+{{ incdir }}\n\
{% endif %}\
{% endfor %}\
{% for define in all_defines %}\
+define+{{ define.0 | upper }}{% if define.1 %}={{ define.1 }}{% endif %}\n\
{% endfor %}\
{% for file in all_files %}\
{% if relativize_path %}\
{% if file is starting_with(root) %}\
{{ file | replace(from=root, to='') | trim_start_matches(pat='/') }}\n\
{% else %}\
{{ file }}\n\
{% endif %}\
{% else %}\
{{ file }}\n\
{% endif %}\
{% endfor %}";
static VSIM_TCL_TPL: &str = "\
# {{ HEADER_AUTOGEN }}
set ROOT \"{{ root }}\"
{% if compilation_mode == 'separate' %}\
{% for group in srcs %}\n\
{% if abort_on_error %}if {[catch { {% endif %}\
{% if group.file_type == 'verilog' %}vlog -incr -sv \\\n \
{% for tmp_arg in vlog_args %}\
{{ tmp_arg }} \\\n \
{% endfor %}\
{% for define in group.defines %}\
+define+{{ define.0 | upper }}{% if define.1 %}={{ define.1 }}{% endif %} \\\n \
{% endfor %}\
{% for incdir in group.incdirs %}\
\"+incdir+{{ incdir | replace(from=root, to='$ROOT') }}\" \\\n \
{% endfor %}\
{% elif group.file_type == 'vhdl' %}vcom -2008 \\\n \
{% for tmp_arg in vcom_args %}\
{{ tmp_arg }} \\\n \
{% endfor %}\
{% endif %}\
{% for file in group.files %}\
\"{{ file | replace(from=root, to='$ROOT') }}\" {% if not loop.last %}\\\n {% endif %}\
{% endfor %}\
{% if abort_on_error %}\\\n}]} {return 1}\
{% endif %}\n\
{% endfor %}\
{% else %}{# compilation_mode == 'common' #}\
{% for file in all_verilog %}\
{% if loop.first %}\
{% if abort_on_error %}if {[catch { {% endif %}\
vlog -incr -sv \\\n \
{% for tmp_arg in vlog_args %}\
{{ tmp_arg }} \\\n \
{% endfor %}\
{% for define in all_defines %}\
+define+{{ define.0 | upper }}{% if define.1 %}={{ define.1 }}{% endif %} \\\n \
{% endfor %}\
{% for incdir in all_incdirs %}\
\"+incdir+{{ incdir | replace(from=root, to='$ROOT') }}\" \\\n \
{% endfor %}\
{% endif %}\
\"{{ file | replace(from=root, to='$ROOT') }}\" {% if not loop.last %}\\\n {% endif %}\
{% if loop.last %}\
{% if abort_on_error %} \\\n}]} {return 1}\
{% endif %}\n\
{% endif %}\
{% endfor %}\
{% for file in all_vhdl %}\
{% if loop.first %}\
{% if abort_on_error %}if {[catch { {% endif %}\
vcom -2008 \\\n \
{% for tmp_arg in vcom_args %}\
{{ tmp_arg }} \\\n \
{% endfor %}\
{% endif %}\
\"{{ file | replace(from=root, to='$ROOT') }}\" {% if not loop.last %}\\\n {% endif %}\
{% if loop.last %}\n\
{% if abort_on_error %}\n}]} {return 1}\
{% endif %}\n\
{% endif %}\
{% endfor %}\
{% endif %}";
static VCS_SH_TPL: &str = "\
#!/usr/bin/env bash
# {{ HEADER_AUTOGEN }}
ROOT=\"{{ root }}\"
{% if compilation_mode == 'separate' %}\
{% for group in srcs %}\n\
{% if group.file_type == 'verilog' %}{{ vlogan_bin }} -sverilog \\\n \
-full64 \\\n \
{% for tmp_arg in vlog_args %}\
{{ tmp_arg }} \\\n \
{% endfor %}\
{% for define in group.defines %}\
+define+{{ define.0 | upper }}{% if define.1 %}={{ define.1 }}{% endif %} \\\n \
{% endfor %}\
{% for incdir in group.incdirs %}\
\"+incdir+{{ incdir | replace(from=root, to='$ROOT') }}\" \\\n \
{% endfor %}\
{% elif group.file_type == 'vhdl' %}{{ vhdlan_bin }} \\\n \
{% for tmp_arg in vcom_args %}\
{{ tmp_arg }} \\\n \
{% endfor %}\
{% endif %}\
{% for file in group.files %}\
\"{{ file | replace(from=root, to='$ROOT') }}\" {% if not loop.last %}\\\n {% endif %}\
{% endfor %}\n\
{% endfor %}
{% else %}{# compilation_mode == 'common' #}\
{% for file in all_verilog %}\
{% if loop.first %}{{ vlogan_bin }} -sverilog \\\n \
-full64 \\\n \
{% for tmp_arg in vlog_args %}\
{{ tmp_arg }} \\\n \
{% endfor %}\
{% for define in all_defines %}\
+define+{{ define.0 | upper }}{% if define.1 %}={{ define.1 }}{% endif %} \\\n \
{% endfor %}\
{% for incdir in all_incdirs %}\
\"+incdir+{{ incdir | replace(from=root, to='$ROOT') }}\" \\\n \
{% endfor %}\
{% endif %}\
\"{{ file | replace(from=root, to='$ROOT') }}\" {% if not loop.last %}\\\n {% endif %}\
{% if loop.last %}\n{% endif %}\
{% endfor %}\n\
{% for file in all_vhdl %}\
{% if loop.first %}{{ vhdlan_bin }} \\\n \
{% for tmp_arg in vcom_args %}\
{{ tmp_arg }} \\\n \
{% endfor %}\
{% endif %}\
\"{{ file | replace(from=root, to='$ROOT') }}\" {% if not loop.last %}\\\n {% endif %}\
{% if loop.last %}\n{% endif %}\
{% endfor %}\n\
{% endif %}";
static VERILATOR_SH_TPL: &str = "\
{% for group in srcs %}\
{% if group.file_type == 'verilog' %}\n\
{% for tmp_arg in vlog_args %}\
{{ tmp_arg }}\n\
{% endfor %}\
{% for define in group.defines %}\
+define+{{ define.0 | upper }}{% if define.1 %}={{ define.1 }}{% endif %}\n\
{% endfor %}\
{% for incdir in group.incdirs %}\
+incdir+{{ incdir | replace(from=root, to='$ROOT') }}\n\
{% endfor %}\
{% for file in group.files %}\
{{ file }}\n\
{% endfor %}\
{% endif %}\
{% endfor %}";
static SYNOPSYS_TCL_TPL: &str = "\
# {{HEADER_AUTOGEN}}
set ROOT \"{{ root }}\"
set search_path_initial $search_path
{% if compilation_mode == 'separate' %}\
{% for group in srcs %}\n\
set search_path $search_path_initial\n\
{% for incdir in group.incdirs %}\
lappend search_path \"$ROOT{{ incdir | replace(from=root, to='') }}\"\n\
{% endfor %}\n\
{% if abort_on_error %}if {[catch { {% endif %}analyze -format \
{% if group.file_type == 'verilog' %}sv{% elif group.file_type == 'vhdl' %}vhdl{% endif %} \\\n \
{% for define in group.defines %}\
{% if loop.first %}-define { \\\n {% endif %}\
{{ define.0 | upper }}{% if define.1 %}={{ define.1 }}{% endif %}\
{% if loop.last %} \\\n } \\\n {% else %} \\\n {% endif %}\
{% endfor %}\
[list \\\n \
{% for file in group.files %}\
{{ ' ' }}\"{{ file | replace(from=root, to='$ROOT') }}\" \\\n \
{% endfor %}\
]\n\
{% if abort_on_error %}}]} {return 1}\
{% endif %}\n\
{% endfor %}\n\
{% else %}{# compilation_mode == 'common' #}\
{% for file in all_verilog %}\
{% if loop.first %}\
set search_path $search_path_initial\n\
{% for incdir in all_incdirs %}\
lappend search_path \"$ROOT{{ incdir | replace(from=root, to='') }}\"\n\
{% endfor %}\n\
{% if abort_on_error %}if {[catch { {% endif %}analyze -format sv \\\n \
{% for define in all_defines %}\
{% if loop.first %}-define { \\\n {% endif %}\
{{ define.0 | upper }}{% if define.1 %}={{ define.1 }}{% endif %}\
{% if loop.last %} \\\n } \\\n {% else %} \\\n {% endif %}\
{% endfor %}\
[list \\\n \
{% endif %}\
{{ ' ' }}\"{{ file | replace(from=root, to='$ROOT') }}\" \\\n \
{% if loop.last %}\
]\n\
{% if abort_on_error %}}]} {return 1}\
{% endif %}\n\
{% endif %}\
{% endfor %}\n\
{% for file in all_vhdl %}\
{% if loop.first %}
{% if abort_on_error %}if {[catch { {% endif %}analyze -format vhdl \\\n \
[list \\\n \
{% endif %}\
{{ ' ' }}\"{{ file | replace(from=root, to='$ROOT') }}\" \\\n \
{% if loop.last %}\
]\n\
{% if abort_on_error %}}]} {return 1}\
{% endif %}\n\
{% endif %}\
{% endfor %}\n\
{% endif %}\
set search_path $search_path_initial\n";
static FORMALITY_TCL_TPL: &str = "\
# {{HEADER_AUTOGEN}}
set ROOT \"{{ root }}\"
set search_path_initial $search_path
{% if compilation_mode == 'separate' %}\
{% for group in srcs %}\n\
set search_path $search_path_initial\n\
{% for incdir in group.incdirs %}\
lappend search_path \"$ROOT{{ incdir | replace(from=root, to='') }}\"\n\
{% endfor %}\n\
{% if abort_on_error %}if {[catch { {% endif %}\
{% if group.file_type == 'verilog' %}read_sverilog{% elif group.file_type == 'vhdl' %}read_vhdl{% endif %} -r \\\n \
{% for define in group.defines %}\
{% if loop.first %}-define { \\\n {% endif %}\
{{ define.0 | upper }}{% if define.1 %}={{ define.1 }}{% endif %}\
{% if loop.last %} \\\n } \\\n {% else %} \\\n {% endif %}\
{% endfor %}\
[list \\\n \
{% for file in group.files %}\
{{ ' ' }}\"{{ file | replace(from=root, to='$ROOT') }}\" \\\n \
{% endfor %}\
]\n\
{% if abort_on_error %}}]} {return 1}\
{% endif %}\n\
{% endfor %}\n\
{% else %}{# compilation_mode == 'common' #}\
{% for file in all_verilog %}\
{% if loop.first %}\
set search_path $search_path_initial\n\
{% for incdir in all_incdirs %}\
lappend search_path \"$ROOT{{ incdir | replace(from=root, to='') }}\"\n\
{% endfor %}\n\
{% if abort_on_error %}if {[catch { {% endif %}read_sverilog -r \\\n \
{% for define in all_defines %}\
{% if loop.first %}-define { \\\n {% endif %}\
{{ define.0 | upper }}{% if define.1 %}={{ define.1 }}{% endif %}\
{% if loop.last %} \\\n } \\\n {% else %} \\\n {% endif %}\
{% endfor %}\
[list \\\n \
{% endif %}\
{{ ' ' }}\"{{ file | replace(from=root, to='$ROOT') }}\" \\\n \
{% if loop.last %}\
]\n\
{% if abort_on_error %}}]} {return 1}\
{% endif %}\n\
{% endif %}\
{% endfor %}\n\
{% for file in all_vhdl %}\
{% if loop.first %}
{% if abort_on_error %}if {[catch { {% endif %}read_vhdl -r \\\n \
[list \\\n \
{% endif %}\
{{ ' ' }}\"{{ file | replace(from=root, to='$ROOT') }}\" \\\n \
{% if loop.last %}\
]\n\
{% if abort_on_error %}}]} {return 1}\
{% endif %}\n\
{% endif %}\
{% endfor %}\n\
{% endif %}\
set search_path $search_path_initial\n";
static GENUS_TCL_TPL: &str = "\
# {{ HEADER_AUTOGEN }}
if [ info exists search_path ] {{ '{{' }}
set search_path_initial $search_path
{{ '}}' }}
set ROOT = \"{{ root }}\"
{% if compilation_mode == 'separate' %}\
{% for group in srcs %}\n\
set search_path $search_path_initial\n\
{% for incdir in group.incdirs %}\
lappend search_path \"$ROOT{{ incdir | replace(from=root, to='') }}\"\n\
{% endfor %}\
set_db init_hdl_search_path $search_path\n\n\
{% if group.file_type == 'verilog' %}read_hdl -language sv \\\n \
{% elif group.file_type == 'vhdl' %}read_hdl -language vhdl \\\n \
{% endif %}\
{% for define in group.defines %}\
{% if loop.first %}-define { \\\n {% endif %}\
{{ define.0 | upper }}{% if define.1 %}={{ define.1 }}{% endif %}\
{% if loop.last %} \\\n } \\\n {% else %} \\\n {% endif %}\
{% endfor %}\
[list \\\n \
{% for file in group.files %}\
{{ ' ' }}\"{{ file | replace(from=root, to='$ROOT') }}\" \\\n \
{% endfor %}\
]\n\
{% endfor %}
{% else %}{# compilation_mode == 'common' #}\
{% for file in all_verilog %}\
{% if loop.first %}\
set search_path $search_path_initial\n\
{% for incdir in all_incdirs %}\
lappend search_path \"$ROOT{{ incdir | replace(from=root, to='') }}\"\n\
{% endfor %}\n\
set_db init_hdl_search_path $search_path\n\n\
{% if abort_on_error %}if {[catch { {% endif %}read_hdl -language sv \\\n \
{% for define in all_defines %}\
{% if loop.first %}-define { \\\n {% endif %}\
{{ define.0 | upper }}{% if define.1 %}={{ define.1 }}{% endif %}\
{% if loop.last %} \\\n } \\\n {% else %} \\\n {% endif %}\
{% endfor %}\
[list \\\n \
{% endif %}\
{{ ' ' }}\"{{ file | replace(from=root, to='$ROOT') }}\" \\\n \
{% if loop.last %}\
]\n\
{% if abort_on_error %}}]} {return 1}\
{% endif %}\n\
{% endif %}\
{% endfor %}\n\
{% for file in all_vhdl %}\
{% if loop.first %}
{% if abort_on_error %}if {[catch { {% endif %}read_hdl -language vhdl \\\n \
[list \\\n \
{% endif %}\
{{ ' ' }}\"{{ file | replace(from=root, to='$ROOT') }}\" \\\n \
{% if loop.last %}\
]\n\
{% if abort_on_error %}}]} {return 1}\
{% endif %}\n\
{% endif %}\
{% endfor %}\n\
{% endif %}\
set search_path $search_path_initial\n";
static VIVADO_TCL_TPL: &str = "\
# {{ HEADER_AUTOGEN }}
set ROOT \"{{ root }}\"
{% if compilation_mode == 'separate' %}\
{% for group in srcs %}\
add_files -norecurse -fileset [current_fileset] [list \\\n \
{% for file in group.files %}\
{{ file | replace(from=root, to='$ROOT') }} \\\n{% if not loop.last %} {% endif %}\
{% endfor %}\
]\n\
{% endfor %}\
{% else %}{# compilation_mode == 'common' #}\
{% for file in all_files %}\
{% if loop.first %}\
add_files -norecurse -fileset [current_fileset] [list \\\n \
{% endif %}\
{{ file | replace(from=root, to='$ROOT') }} \\\n{% if not loop.last %} {% endif %}\
{% if loop.last %}\
]\n\
{% endif %}\
{% endfor %}\
{% endif %}\
{% for arg in vivado_filesets %}\
{% for incdir in all_incdirs %}\
{% if loop.first %}\nset_property include_dirs [list \\\n {% endif %}\
{{incdir | replace(from=root, to='$ROOT') }}\
{%if loop.last %} \\\n] [current_fileset{{ arg }}]\n{% else %} \\\n {% endif %}\
{% endfor %}\
{% endfor %}\
{% for arg in vivado_filesets %}\
{% for define in all_defines %}\
{% if loop.first %}\nset_property verilog_define [list \\\n {% endif %}\
{{ define.0 | upper }}{% if define.1 %}={{ define.1 }}{% endif %}\
{% if loop.last %} \\\n] [current_fileset{{ arg }}]\n{% else %} \\\n {% endif %}\
{% endfor %}\
{% endfor %}";
static RIVIERA_TCL_TPL: &str = "\
# {{ HEADER_AUTOGEN }}
set ROOT \"{{ root }}\"
vlib work
{% if compilation_mode == 'separate' %}\
{% for group in srcs %}\
{% if abort_on_error %}if {[catch { {% endif %}\
{% if group.file_type == 'verilog' %}vlog -sv \\\n \
{% for tmp_arg in vlog_args %}\
{{ tmp_arg }} \\\n \
{% endfor %}\
{% for define in group.defines %}\
+define+{{ define.0 | upper }}{% if define.1 %}={{ define.1 }}{% endif %} \\\n \
{% endfor %}\