292
|
1 #!/usr/bin/env python
|
104
|
2
|
287
|
3 import sys
|
331
|
4 import os
|
287
|
5 import argparse
|
|
6 import logging
|
331
|
7 import yaml
|
287
|
8
|
329
|
9 from ppci.buildtasks import Compile
|
|
10 from ppci.tasks import TaskRunner
|
331
|
11 from ppci.report import RstFormatter
|
205
|
12 import outstream
|
323
|
13 from target.target_list import target_list
|
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])',
|
|
32 type=logLevel, default='WARN')
|
|
33 sub_parsers = parser.add_subparsers(title='commands',
|
|
34 description='possible commands', dest='command')
|
|
35 recipe_parser = sub_parsers.add_parser('recipe', help="Bake recipe")
|
|
36 recipe_parser.add_argument('recipe_file', help='recipe file')
|
|
37
|
|
38 compile_parser = sub_parsers.add_parser('compile', help="compile source")
|
|
39 compile_parser.add_argument('source', type=argparse.FileType('r'),
|
|
40 help='the source file to build', nargs="+")
|
|
41 compile_parser.add_argument('-i', '--imp', type=argparse.FileType('r'),
|
|
42 help='Possible import module', action='append', default=[])
|
|
43 compile_parser.add_argument('--target', help="Backend selection",
|
|
44 choices=targetnames, required=True)
|
|
45 compile_parser.add_argument('-o', '--output', help='Output file',
|
|
46 metavar='filename')
|
|
47 compile_parser.add_argument('--report',
|
|
48 help='Specify a file to write the compile report to',
|
|
49 type=argparse.FileType('w'))
|
|
50 return parser
|
|
51
|
287
|
52
|
331
|
53 class RecipeLoader:
|
|
54 def load_file(self, recipe_file, runner):
|
|
55 """ Loads a recipe dictionary into a task runner """
|
|
56 self.recipe_dir = os.path.abspath(os.path.dirname(recipe_file))
|
|
57 with open(recipe_file, 'r') as f:
|
|
58 recipe = yaml.load(f)
|
|
59 self.load_dict(recipe, runner)
|
|
60
|
|
61 def relpath(self, filename):
|
|
62 return os.path.join(self.recipe_dir, filename)
|
|
63
|
|
64 def openfile(self, filename):
|
|
65 return open(self.relpath(filename), 'r')
|
|
66
|
|
67 def load_dict(self, recipe, runner):
|
|
68 for command, value in recipe.items():
|
|
69 if command == 'compile':
|
|
70 sources = [self.openfile(s) for s in value['sources']]
|
|
71 includes = [self.openfile(i) for i in value['includes']]
|
|
72 target = targets[value['machine']]
|
|
73 output = outstream.TextOutputStream()
|
|
74 runner.add_task(Compile(sources, includes, target, output))
|
|
75 elif command == 'link':
|
|
76 self.load_dict(value['inputs'], runner)
|
|
77 #runner.add_task(Link())
|
|
78 elif command == 'assemble':
|
|
79 pass
|
|
80 elif command == 'apps':
|
|
81 for a in value:
|
|
82 self.load_dict(a, runner)
|
|
83 else:
|
|
84 raise NotImplementedError(command)
|
104
|
85
|
288
|
86
|
249
|
87 def main(args):
|
315
|
88 # Configure some logging:
|
|
89 logging.getLogger().setLevel(logging.DEBUG)
|
|
90 ch = logging.StreamHandler()
|
331
|
91 ch.setFormatter(logging.Formatter(ppci.logformat))
|
315
|
92 ch.setLevel(args.log)
|
|
93 logging.getLogger().addHandler(ch)
|
329
|
94
|
331
|
95 runner = TaskRunner()
|
|
96 if args.command == 'compile':
|
|
97 tg = targets[args.target]
|
|
98 outs = outstream.TextOutputStream()
|
|
99 runner.add_task(Compile(args.source, args.imp, tg, outs))
|
|
100 elif args.command == 'recipe':
|
|
101 recipe_loader = RecipeLoader()
|
|
102 recipe_loader.load_file(args.recipe_file, runner)
|
|
103 else:
|
|
104 raise NotImplementedError('Invalid option')
|
105
|
105
|
331
|
106 res = runner.run_tasks()
|
104
|
107
|
315
|
108 logging.getLogger().removeHandler(ch)
|
331
|
109 return res
|
246
|
110
|
288
|
111
|
207
|
112 if __name__ == '__main__':
|
331
|
113 parser = make_parser()
|
213
|
114 arguments = parser.parse_args()
|
331
|
115 if not arguments.command:
|
|
116 parser.print_usage()
|
|
117 sys.exit(1)
|
276
|
118 sys.exit(main(arguments))
|