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
|
366
|
12 from ppci.target.target_list import targets, targetnames
|
342
|
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
|
331
|
25 def make_parser():
|
|
26 parser = argparse.ArgumentParser(description='lcfos Compiler')
|
|
27
|
|
28 parser.add_argument('--log', help='Log level (INFO,DEBUG,[WARN])',
|
334
|
29 type=logLevel, default='INFO')
|
332
|
30 parser.add_argument('--display-build-steps', action='store_true')
|
348
|
31 parser.add_argument('--report',
|
|
32 help='Specify a file to write the compile report to',
|
|
33 type=argparse.FileType('w'))
|
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 return parser
|
|
49
|
287
|
50
|
249
|
51 def main(args):
|
315
|
52 # Configure some logging:
|
|
53 logging.getLogger().setLevel(logging.DEBUG)
|
|
54 ch = logging.StreamHandler()
|
331
|
55 ch.setFormatter(logging.Formatter(ppci.logformat))
|
315
|
56 ch.setLevel(args.log)
|
|
57 logging.getLogger().addHandler(ch)
|
329
|
58
|
348
|
59 if args.report:
|
|
60 fh = logging.StreamHandler(args.report)
|
|
61 fh.setFormatter(RstFormatter())
|
|
62 logging.getLogger().addHandler(fh)
|
|
63
|
331
|
64 runner = TaskRunner()
|
|
65 if args.command == 'compile':
|
|
66 tg = targets[args.target]
|
334
|
67 output = ObjectFile()
|
|
68 runner.add_task(Compile(args.source, args.imp, tg, output))
|
331
|
69 elif args.command == 'recipe':
|
355
|
70 recipe_loader = RecipeLoader(runner)
|
|
71 recipe_loader.load_file(args.recipe_file)
|
331
|
72 else:
|
|
73 raise NotImplementedError('Invalid option')
|
105
|
74
|
332
|
75 if args.display_build_steps:
|
|
76 runner.display()
|
|
77 res = 0
|
|
78 else:
|
|
79 res = runner.run_tasks()
|
104
|
80
|
348
|
81 if args.report:
|
|
82 logging.getLogger().removeHandler(fh)
|
|
83 args.report.close()
|
|
84
|
315
|
85 logging.getLogger().removeHandler(ch)
|
331
|
86 return res
|
246
|
87
|
288
|
88
|
207
|
89 if __name__ == '__main__':
|
331
|
90 parser = make_parser()
|
213
|
91 arguments = parser.parse_args()
|
331
|
92 if not arguments.command:
|
|
93 parser.print_usage()
|
|
94 sys.exit(1)
|
276
|
95 sys.exit(main(arguments))
|