/
main.py
773 lines (657 loc) · 22.2 KB
/
main.py
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
#!/usr/bin/env python
# Copyright FuseSoC contributors
# Licensed under the 2-Clause BSD License, see LICENSE for details.
# SPDX-License-Identifier: BSD-2-Clause
import argparse
import os
import shutil
import signal
import subprocess
import sys
import warnings
from fusesoc import __version__
# Check if this is run from a local installation
fusesocdir = os.path.abspath(
os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")
)
if os.path.exists(os.path.join(fusesocdir, "fusesoc")):
sys.path[0:0] = [fusesocdir]
import logging
from edalize import get_edatool
from fusesoc.config import Config
from fusesoc.coremanager import CoreManager, DependencyError
from fusesoc.edalizer import Edalizer
from fusesoc.librarymanager import Library
from fusesoc.utils import Launcher, setup_logging, yaml_fread
from fusesoc.vlnv import Vlnv
logger = logging.getLogger(__name__)
REPOS = [
("orpsoc-cores", "https://github.com/openrisc/orpsoc-cores", "old base library"),
("fusesoc-cores", "https://github.com/fusesoc/fusesoc-cores", "new base library"),
]
def _get_core(cm, name):
core = None
try:
core = cm.get_core(Vlnv(name))
except RuntimeError as e:
logger.error(str(e))
exit(1)
except DependencyError as e:
logger.error(
"'"
+ name
+ "' or any of its dependencies requires '"
+ e.value
+ "', but this core was not found"
)
exit(1)
return core
def abort_handler(signal, frame):
print("")
logger.info("****************************")
logger.info("**** FuseSoC aborted ****")
logger.info("****************************")
print("")
sys.exit(0)
signal.signal(signal.SIGINT, abort_handler)
def pgm(cm, args):
warnings.warn(
"The 'pgm' subcommand is deprecated and will be removed in the next "
"release. Use 'fusesoc run --target=synth --run' instead.",
FutureWarning,
)
do_configure = False
do_build = False
do_run = True
flags = {"target": "synth", "tool": None}
run_backend(
cm,
"build",
do_configure,
do_build,
do_run,
flags,
None,
args.system,
args.backendargs,
None,
False,
)
def fetch(cm, args):
core = _get_core(cm, args.core)
try:
core.setup()
except RuntimeError as e:
logger.error("Failed to fetch '{}': {}".format(core.name, str(e)))
exit(1)
def init(cm, args):
warnings.warn(
"The 'init' subcommand is deprecated and will be removed in the next "
"release. It was intended to fetch the FuseSoC standard library. This can be done with 'fusesoc library add fusesoc_cores https://github.com/fusesoc/fusesoc-cores' instead.",
FutureWarning,
)
# Fix Python 2.x.
global input
try:
input = raw_input
except NameError:
pass
xdg_config_home = os.environ.get("XDG_CONFIG_HOME") or os.path.join(
os.path.expanduser("~"), ".config"
)
config_file = os.path.join(xdg_config_home, "fusesoc", "fusesoc.conf")
if os.path.exists(config_file):
logger.warning(f"'{config_file}' already exists. Aborting")
exit(1)
# TODO. Prepend cores_root to file if it doesn't exist
f = open(config_file, "w+")
else:
logger.info(f"Writing configuration file to '{config_file}'")
if not os.path.exists(os.path.dirname(config_file)):
os.makedirs(os.path.dirname(config_file))
f = open(config_file, "w+")
config = Config(file=f)
_repo_paths = []
for repo in REPOS:
name = repo[0]
uri = repo[1]
default_dir = os.path.join(cm._lm.library_root, name)
prompt = "Directory to use for {} ({}) [{}] : "
if args.y:
location = None
else:
location = input(prompt.format(repo[0], repo[2], default_dir))
if not location:
location = default_dir
if os.path.exists(location):
logger.warning(
"'{}' already exists. This library will not be added to fusesoc.conf".format(
location
)
)
# TODO: Prompt for overwrite
else:
logger.info(f"Initializing {name}")
try:
library = Library(name, location, "git", uri, True)
config.add_library(library)
except RuntimeError as e:
logger.error("Init failed: " + str(e))
exit(1)
logger.info("FuseSoC is ready to use!")
def list_paths(cm, args):
cores_root = [x.location for x in cm.get_libraries()]
print("\n".join(cores_root))
def add_library(cm, args):
sync_uri = vars(args)["sync-uri"]
if args.location:
location = args.location
elif vars(args).get("global", False):
location = os.path.join(cm._lm.library_root, args.name)
else:
location = os.path.join("fusesoc_libraries", args.name)
if "sync-type" in vars(args):
sync_type = vars(args)["sync-type"]
else:
sync_type = None
# Check if it's a dir. Otherwise fall back to git repo
if not sync_type:
if os.path.isdir(sync_uri):
sync_type = "local"
else:
sync_type = "git"
if sync_type == "local":
logger.info(
"Interpreting sync-uri '{}' as location for local provider.".format(
sync_uri
)
)
location = os.path.abspath(sync_uri)
auto_sync = not args.no_auto_sync
library = Library(args.name, location, sync_type, sync_uri, auto_sync)
if args.config:
config = Config(file=args.config)
elif vars(args)["global"]:
xdg_config_home = os.environ.get("XDG_CONFIG_HOME") or os.path.join(
os.path.expanduser("~"), ".config"
)
config_file = os.path.join(xdg_config_home, "fusesoc", "fusesoc.conf")
config = Config(path=config_file)
else:
config = Config(path="fusesoc.conf")
try:
config.add_library(library)
except RuntimeError as e:
logger.error("`add library` failed: " + str(e))
exit(1)
def library_list(cm, args):
lengths = [4, 8, 9, 8, 9]
for lib in cm.get_libraries():
lengths[0] = max(lengths[0], len(lib.name))
lengths[1] = max(lengths[1], len(lib.location))
lengths[2] = max(lengths[2], len(lib.sync_type))
lengths[3] = max(lengths[3], len(lib.sync_uri or ""))
print(
"{} : {} : {} : {} : {}".format(
"Name".ljust(lengths[0]),
"Location".ljust(lengths[1]),
"Sync type".ljust(lengths[2]),
"Sync URI".ljust(lengths[3]),
"Auto sync".ljust(lengths[4]),
)
)
for lib in cm.get_libraries():
print(
"{} : {} : {} : {} : {}".format(
lib.name.ljust(lengths[0]),
lib.location.ljust(lengths[1]),
lib.sync_type.ljust(lengths[2]),
(lib.sync_uri or "N/A").ljust(lengths[3]),
("y" if lib.auto_sync else "n").ljust(lengths[4]),
)
)
def list_cores(cm, args):
cores = cm.get_cores()
print("\nAvailable cores:\n")
if not cores:
cores_root = cm.get_libraries()
if cores_root:
logger.error("No cores found in any library")
else:
logger.error("No libraries registered")
exit(1)
maxlen = max(map(len, cores.keys()))
print("Core".ljust(maxlen) + " Cache status Description")
print("=" * 80)
for name in sorted(cores.keys()):
core = cores[name]
print(
name.ljust(maxlen)
+ " : "
+ core.cache_status().rjust(10)
+ " : "
+ (core.description or "<No description>")
)
def gen_list(cm, args):
cores = cm.get_generators()
if not cores:
print("\nNo available generators\n")
else:
print("\nAvailable generators:\n")
maxlen = max(map(len, cores.keys()))
print("Core".ljust(maxlen) + " Generator")
print("=" * (maxlen + 12))
for core in sorted(cores.keys()):
for generator_name, generator_data in cores[core].items():
print(
"{} : {} : {}".format(
core.ljust(maxlen),
generator_name,
generator_data.description or "<No description>",
)
)
def gen_show(cm, args):
cores = cm.get_generators()
for core in sorted(cores.keys()):
for generator_name, generator_data in cores[core].items():
if generator_name == args.generator:
print(
"""
Core : {}
Generator : {}
Description : {}
Usage :
{}""".format(
core,
generator_name,
generator_data.description or "<No description>",
generator_data.usage or "",
)
)
def core_info(cm, args):
core = _get_core(cm, args.core)
print(core.info())
def run(cm, args):
stages = (args.setup, args.build, args.run)
# Always run setup if build is true
args.setup |= args.build
# Run all stages by default if no stage flags are set
if stages == (False, False, False):
do_configure = True
do_build = True
do_run = True
elif stages == (True, False, True):
logger.error("Configure and run without build is invalid")
exit(1)
else:
do_configure = args.setup
do_build = args.build
do_run = args.run
flags = {"tool": args.tool, "target": args.target}
for flag in args.flag:
if flag[0] == "+":
flags[flag[1:]] = True
elif flag[0] == "-":
flags[flag[1:]] = False
else:
flags[flag] = True
run_backend(
cm,
not args.no_export,
do_configure,
do_build,
do_run,
flags,
args.system_name,
args.system,
args.backendargs,
args.build_root,
args.verbose,
)
# Clean out old work root
def prepare_work_root(work_root):
if os.path.exists(work_root):
for f in os.listdir(work_root):
if os.path.isdir(os.path.join(work_root, f)):
shutil.rmtree(os.path.join(work_root, f))
else:
os.remove(os.path.join(work_root, f))
else:
os.makedirs(work_root)
def run_backend(
cm,
export,
do_configure,
do_build,
do_run,
flags,
system_name,
system,
backendargs,
build_root_arg,
verbose,
):
tool_error = (
"No tool was supplied on command line or found in '{}' core description"
)
core = _get_core(cm, system)
try:
tool = core.get_tool(flags)
except SyntaxError as e:
logger.error(str(e))
exit(1)
if not tool:
logger.error(tool_error.format(system))
exit(1)
flags["tool"] = tool
build_root = build_root_arg or os.path.join(
cm.config.build_root, core.name.sanitized_name
)
logger.debug(f"Setting build_root to {build_root}")
if export:
export_root = os.path.join(build_root, "src")
else:
export_root = None
try:
work_root = os.path.join(build_root, core.get_work_root(flags))
except SyntaxError as e:
logger.error(e.msg)
exit(1)
edam_file = os.path.join(work_root, core.name.sanitized_name + ".eda.yml")
if not os.path.exists(edam_file):
do_configure = True
try:
backend_class = get_edatool(tool)
except ImportError:
logger.error(f"Backend {tool!r} not found")
exit(1)
edalizer = Edalizer(
toplevel=core.name,
flags=flags,
core_manager=cm,
work_root=work_root,
export_root=export_root,
system_name=system_name,
)
if do_configure:
try:
prepare_work_root(work_root)
edam = edalizer.run()
parsed_args = edalizer.parse_args(backend_class, backendargs, edam)
edalizer.add_parsed_args(backend_class, parsed_args)
except SyntaxError as e:
logger.error(e.msg)
exit(1)
except RuntimeError as e:
logger.error("Setup failed : {}".format(str(e)))
exit(1)
edalizer.to_yaml(edam_file)
else:
edam = yaml_fread(edam_file)
parsed_args = edalizer.parse_args(backend_class, backendargs, edam)
# Frontend/backend separation
try:
backend = backend_class(edam=edam, work_root=work_root, verbose=verbose)
except RuntimeError as e:
logger.error(str(e))
exit(1)
except FileNotFoundError as e:
logger.error(f'Could not find EDA API file "{e.filename}"')
exit(1)
if do_configure:
try:
backend.configure([])
print("")
except RuntimeError as e:
logger.error("Failed to configure the system")
logger.error(str(e))
exit(1)
if do_build:
try:
backend.build()
except RuntimeError as e:
logger.error("Failed to build {} : {}".format(str(core.name), str(e)))
exit(1)
if do_run:
try:
backend.run(parsed_args)
except RuntimeError as e:
logger.error("Failed to run {} : {}".format(str(core.name), str(e)))
exit(1)
def update(cm, args):
if "warn" in args:
logger.warning(args.warn)
cm._lm.update(args.libraries)
def init_logging(verbose, monochrome, log_file=None):
level = logging.DEBUG if verbose else logging.INFO
setup_logging(level, monochrome, log_file)
if verbose:
logger.debug("Verbose output")
else:
logger.debug("Concise output")
if monochrome:
logger.debug("Monochrome output")
else:
logger.debug("Colorful output")
def init_coremanager(config, args_cores_root):
logger.debug("Initializing core manager")
cm = CoreManager(config)
args_libs = [Library(acr, acr) for acr in args_cores_root]
# Add libraries from config file, env var and command-line
for library in config.libraries + args_libs:
try:
cm.add_library(library)
except (RuntimeError, OSError) as e:
_s = "Failed to register library '{}'"
logger.warning(_s.format(str(e)))
return cm
def get_parser():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
# Global actions
parser.add_argument(
"--version",
help="Display the FuseSoC version",
action="version",
version=__version__,
)
# Global options
parser.add_argument(
"--cores-root",
help="Add additional directories containing cores",
default=[],
action="append",
)
parser.add_argument(
"--config", help="Specify the config file to use", type=argparse.FileType("r")
)
parser.add_argument(
"--monochrome",
help="Don't use color for messages",
action="store_true",
default=not sys.stdout.isatty(),
)
parser.add_argument("--verbose", help="More info messages", action="store_true")
parser.add_argument("--log-file", help="Write log messages to file")
# init subparser
parser_init = subparsers.add_parser(
"init", help="Initialize the FuseSoC core libraries. DEPRECATED"
)
parser_init.add_argument(
"-y", action="store_true", help="Skip user input and use default settings"
)
parser_init.set_defaults(func=init)
# pgm subparser
parser_pgm = subparsers.add_parser(
"pgm",
help="Program an FPGA with a system configuration. DEPRECATED, use 'run' instead.",
)
parser_pgm.add_argument("system")
parser_pgm.add_argument("backendargs", nargs=argparse.REMAINDER)
parser_pgm.set_defaults(func=pgm)
# fetch subparser
parser_fetch = subparsers.add_parser(
"fetch", help="Fetch a remote core and its dependencies to local cache"
)
parser_fetch.add_argument("core")
parser_fetch.set_defaults(func=fetch)
# core subparser
parser_core = subparsers.add_parser(
"core", help="Subcommands for dealing with cores"
)
core_subparsers = parser_core.add_subparsers()
parser_core.set_defaults(subparser=parser_core)
# core list subparser
parser_core_list = core_subparsers.add_parser("list", help="List available cores")
parser_core_list.set_defaults(func=list_cores)
# core show subparser
parser_core_show = core_subparsers.add_parser(
"show", help="Show information about a core"
)
parser_core_show.add_argument("core", help="Name of the core to show")
parser_core_show.set_defaults(func=core_info)
# list-cores subparser
parser_list_cores = subparsers.add_parser("list-cores", help="List available cores")
parser_list_cores.set_defaults(func=list_cores)
# core-info subparser
parser_core_info = subparsers.add_parser(
"core-info", help="Display details about a core"
)
parser_core_info.add_argument("core")
parser_core_info.set_defaults(func=core_info)
# gen subparser
parser_gen = subparsers.add_parser(
"gen", help="Run or show information about generators"
)
parser_gen.set_defaults(subparser=parser_gen)
gen_subparsers = parser_gen.add_subparsers()
# gen list subparser
parser_gen_list = gen_subparsers.add_parser(
"list", help="List available generators"
)
parser_gen_list.set_defaults(func=gen_list)
# gen show subparser
parser_gen_show = gen_subparsers.add_parser(
"show", help="Show information about a generator"
)
parser_gen_show.add_argument("generator", help="Name of the generator to show")
parser_gen_show.set_defaults(func=gen_show)
# list-paths subparser
parser_list_paths = subparsers.add_parser(
"list-paths", help="Display the search order for core root paths"
)
parser_list_paths.set_defaults(func=list_paths)
# library subparser
parser_library = subparsers.add_parser(
"library", help="Subcommands for dealing with library management"
)
library_subparsers = parser_library.add_subparsers()
parser_library.set_defaults(subparser=parser_library)
# library add subparser
parser_library_add = library_subparsers.add_parser(
"add", help="Add new library to fusesoc.conf"
)
parser_library_add.add_argument("name", help="A friendly name for the library")
parser_library_add.add_argument(
"sync-uri", help="The URI source for the library (can be a file system path)"
)
parser_library_add.add_argument(
"--location",
help="The location to store the library into (defaults to $XDG_DATA_HOME/[name])",
)
parser_library_add.add_argument(
"--sync-type",
help="The provider type for the library. Defaults to 'git'.",
choices=["git", "local"],
)
parser_library_add.add_argument(
"--no-auto-sync",
action="store_true",
help="Disable automatic updates of the library",
)
parser_library_add.add_argument(
"--global",
action="store_true",
help="Use the global FuseSoc config file in $XDG_CONFIG_HOME/fusesoc/fusesoc.conf",
)
parser_library_add.set_defaults(func=add_library)
# library list subparser
parser_library_list = library_subparsers.add_parser(
"list", help="List core libraries"
)
parser_library_list.set_defaults(func=library_list)
# library update subparser
parser_library_update = library_subparsers.add_parser(
"update", help="Update the FuseSoC core libraries"
)
parser_library_update.add_argument(
"libraries", nargs="*", help="The libraries to update (defaults to all)"
)
parser_library_update.set_defaults(func=update)
# run subparser
parser_run = subparsers.add_parser("run", help="Start a tool flow")
parser_run.add_argument(
"--no-export",
action="store_true",
help="Reference source files from their current location instead of exporting to a build tree",
)
parser_run.add_argument(
"--build-root", help="Output directory for build. Defaults to build/$VLNV"
)
parser_run.add_argument("--setup", action="store_true", help="Execute setup stage")
parser_run.add_argument("--build", action="store_true", help="Execute build stage")
parser_run.add_argument("--run", action="store_true", help="Execute run stage")
parser_run.add_argument("--target", help="Override default target")
parser_run.add_argument("--tool", help="Override default tool for target")
parser_run.add_argument(
"--flag",
help="Set custom use flags. Can be specified multiple times",
action="append",
default=[],
)
parser_run.add_argument(
"--system-name", help="Override default VLNV name for system"
)
parser_run.add_argument("system", help="Select a system to operate on")
parser_run.add_argument(
"backendargs", nargs=argparse.REMAINDER, help="arguments to be sent to backend"
)
parser_run.set_defaults(func=run)
# update subparser
parser_update = subparsers.add_parser(
"update", help="Update the FuseSoC core libraries"
)
parser_update.add_argument(
"libraries",
nargs="*",
help="The libraries (or core roots) to update (defaults to all)",
)
parser_update.set_defaults(func=update)
parser_update.set_defaults(
warn="'fusesoc update' is deprecated. Use 'fusesoc library update' instead"
)
return parser
def parse_args(argv):
parser = get_parser()
args = parser.parse_args(argv)
if hasattr(args, "func"):
return args
if hasattr(args, "subparser"):
args.subparser.print_help()
else:
parser.print_help()
return None
def fusesoc(args):
init_logging(args.verbose, args.monochrome, args.log_file)
config = Config(file=args.config)
cm = init_coremanager(config, args.cores_root)
# Run the function
args.func(cm, args)
def main():
args = parse_args(sys.argv[1:])
if not args:
exit(0)
logger.debug("Command line arguments: " + str(sys.argv))
fusesoc(args)
if __name__ == "__main__":
main()