annotate python/zcc.py @ 331:a78b41ff6ad2

Added better recipe files
author Windel Bouwman
date Fri, 07 Feb 2014 12:39:59 +0100
parents 8f6f3ace4e78
children 87feb8a23b4d
rev   line source
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
1 #!/usr/bin/env python
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
2
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
3 import sys
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
4 import os
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
5 import argparse
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
6 import logging
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
7 import yaml
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
8
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents: 323
diff changeset
9 from ppci.buildtasks import Compile
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents: 323
diff changeset
10 from ppci.tasks import TaskRunner
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
11 from ppci.report import RstFormatter
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
12 import outstream
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
13 from target.target_list import target_list
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
14 import ppci
281
4496cae24d7f Improved logview
Windel Bouwman
parents: 276
diff changeset
15
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
16
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
17 def logLevel(s):
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
18 """ Converts a string to a valid logging level """
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
19 numeric_level = getattr(logging, s.upper(), None)
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
20 if not isinstance(numeric_level, int):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
21 raise ValueError('Invalid log level: {}'.format(s))
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
22 return numeric_level
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
23
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
24
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
25 targets = {t.name: t for t in target_list}
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
26 targetnames = list(targets.keys())
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
27
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
28 def make_parser():
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
29 parser = argparse.ArgumentParser(description='lcfos Compiler')
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
30
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
31 parser.add_argument('--log', help='Log level (INFO,DEBUG,[WARN])',
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
32 type=logLevel, default='WARN')
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
33 sub_parsers = parser.add_subparsers(title='commands',
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
34 description='possible commands', dest='command')
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
35 recipe_parser = sub_parsers.add_parser('recipe', help="Bake recipe")
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
36 recipe_parser.add_argument('recipe_file', help='recipe file')
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
37
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
38 compile_parser = sub_parsers.add_parser('compile', help="compile source")
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
39 compile_parser.add_argument('source', type=argparse.FileType('r'),
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
40 help='the source file to build', nargs="+")
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
41 compile_parser.add_argument('-i', '--imp', type=argparse.FileType('r'),
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
42 help='Possible import module', action='append', default=[])
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
43 compile_parser.add_argument('--target', help="Backend selection",
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
44 choices=targetnames, required=True)
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
45 compile_parser.add_argument('-o', '--output', help='Output file',
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
46 metavar='filename')
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
47 compile_parser.add_argument('--report',
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
48 help='Specify a file to write the compile report to',
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
49 type=argparse.FileType('w'))
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
50 return parser
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
51
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
52
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
53 class RecipeLoader:
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
54 def load_file(self, recipe_file, runner):
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
55 """ Loads a recipe dictionary into a task runner """
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
56 self.recipe_dir = os.path.abspath(os.path.dirname(recipe_file))
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
57 with open(recipe_file, 'r') as f:
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
58 recipe = yaml.load(f)
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
59 self.load_dict(recipe, runner)
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
60
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
61 def relpath(self, filename):
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
62 return os.path.join(self.recipe_dir, filename)
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
63
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
64 def openfile(self, filename):
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
65 return open(self.relpath(filename), 'r')
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
66
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
67 def load_dict(self, recipe, runner):
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
68 for command, value in recipe.items():
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
69 if command == 'compile':
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
70 sources = [self.openfile(s) for s in value['sources']]
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
71 includes = [self.openfile(i) for i in value['includes']]
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
72 target = targets[value['machine']]
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
73 output = outstream.TextOutputStream()
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
74 runner.add_task(Compile(sources, includes, target, output))
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
75 elif command == 'link':
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
76 self.load_dict(value['inputs'], runner)
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
77 #runner.add_task(Link())
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
78 elif command == 'assemble':
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
79 pass
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
80 elif command == 'apps':
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
81 for a in value:
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
82 self.load_dict(a, runner)
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
83 else:
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
84 raise NotImplementedError(command)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
85
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
86
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
87 def main(args):
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
88 # Configure some logging:
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
89 logging.getLogger().setLevel(logging.DEBUG)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
90 ch = logging.StreamHandler()
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
91 ch.setFormatter(logging.Formatter(ppci.logformat))
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
92 ch.setLevel(args.log)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
93 logging.getLogger().addHandler(ch)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents: 323
diff changeset
94
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
95 runner = TaskRunner()
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
96 if args.command == 'compile':
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
97 tg = targets[args.target]
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
98 outs = outstream.TextOutputStream()
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
99 runner.add_task(Compile(args.source, args.imp, tg, outs))
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
100 elif args.command == 'recipe':
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
101 recipe_loader = RecipeLoader()
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
102 recipe_loader.load_file(args.recipe_file, runner)
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
103 else:
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
104 raise NotImplementedError('Invalid option')
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
105
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
106 res = runner.run_tasks()
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
107
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
108 logging.getLogger().removeHandler(ch)
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
109 return res
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
110
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
111
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
112 if __name__ == '__main__':
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
113 parser = make_parser()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
114 arguments = parser.parse_args()
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
115 if not arguments.command:
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
116 parser.print_usage()
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
117 sys.exit(1)
276
Windel Bouwman
parents: 272
diff changeset
118 sys.exit(main(arguments))