292
|
1 #!/usr/bin/env python
|
104
|
2
|
287
|
3 import sys
|
331
|
4 import os
|
287
|
5 import argparse
|
|
6 import logging
|
|
7
|
334
|
8 from ppci.buildtasks import Compile, Assemble, Link
|
329
|
9 from ppci.tasks import TaskRunner
|
331
|
10 from ppci.report import RstFormatter
|
334
|
11 from ppci.objectfile import ObjectFile
|
342
|
12 from ppci.target.target_list import target_list
|
|
13 from ppci.recipe import RecipeLoader
|
331
|
14 import ppci
|
281
|
15
|
289
|
16
|
253
|
17 def logLevel(s):
|
312
|
18 """ Converts a string to a valid logging level """
|
253
|
19 numeric_level = getattr(logging, s.upper(), None)
|
|
20 if not isinstance(numeric_level, int):
|
|
21 raise ValueError('Invalid log level: {}'.format(s))
|
|
22 return numeric_level
|
105
|
23
|
289
|
24
|
323
|
25 targets = {t.name: t for t in target_list}
|
292
|
26 targetnames = list(targets.keys())
|
290
|
27
|
331
|
28 def make_parser():
|
|
29 parser = argparse.ArgumentParser(description='lcfos Compiler')
|
|
30
|
|
31 parser.add_argument('--log', help='Log level (INFO,DEBUG,[WARN])',
|
334
|
32 type=logLevel, default='INFO')
|
332
|
33 parser.add_argument('--display-build-steps', action='store_true')
|
331
|
34 sub_parsers = parser.add_subparsers(title='commands',
|
|
35 description='possible commands', dest='command')
|
|
36 recipe_parser = sub_parsers.add_parser('recipe', help="Bake recipe")
|
|
37 recipe_parser.add_argument('recipe_file', help='recipe file')
|
|
38
|
|
39 compile_parser = sub_parsers.add_parser('compile', help="compile source")
|
|
40 compile_parser.add_argument('source', type=argparse.FileType('r'),
|
|
41 help='the source file to build', nargs="+")
|
|
42 compile_parser.add_argument('-i', '--imp', type=argparse.FileType('r'),
|
|
43 help='Possible import module', action='append', default=[])
|
|
44 compile_parser.add_argument('--target', help="Backend selection",
|
|
45 choices=targetnames, required=True)
|
|
46 compile_parser.add_argument('-o', '--output', help='Output file',
|
|
47 metavar='filename')
|
|
48 compile_parser.add_argument('--report',
|
|
49 help='Specify a file to write the compile report to',
|
|
50 type=argparse.FileType('w'))
|
|
51 return parser
|
|
52
|
287
|
53
|
288
|
54
|
249
|
55 def main(args):
|
315
|
56 # Configure some logging:
|
|
57 logging.getLogger().setLevel(logging.DEBUG)
|
|
58 ch = logging.StreamHandler()
|
331
|
59 ch.setFormatter(logging.Formatter(ppci.logformat))
|
315
|
60 ch.setLevel(args.log)
|
|
61 logging.getLogger().addHandler(ch)
|
329
|
62
|
331
|
63 runner = TaskRunner()
|
|
64 if args.command == 'compile':
|
|
65 tg = targets[args.target]
|
334
|
66 output = ObjectFile()
|
|
67 runner.add_task(Compile(args.source, args.imp, tg, output))
|
331
|
68 elif args.command == 'recipe':
|
|
69 recipe_loader = RecipeLoader()
|
|
70 recipe_loader.load_file(args.recipe_file, runner)
|
|
71 else:
|
|
72 raise NotImplementedError('Invalid option')
|
105
|
73
|
332
|
74 if args.display_build_steps:
|
|
75 runner.display()
|
|
76 res = 0
|
|
77 else:
|
|
78 res = runner.run_tasks()
|
104
|
79
|
315
|
80 logging.getLogger().removeHandler(ch)
|
331
|
81 return res
|
246
|
82
|
288
|
83
|
207
|
84 if __name__ == '__main__':
|
331
|
85 parser = make_parser()
|
213
|
86 arguments = parser.parse_args()
|
331
|
87 if not arguments.command:
|
|
88 parser.print_usage()
|
|
89 sys.exit(1)
|
276
|
90 sys.exit(main(arguments))
|