annotate python/ppci/recipe.py @ 348:442fb043d149

Added log option to zcc
author Windel Bouwman
date Sat, 08 Mar 2014 15:32:33 +0100
parents 86b02c98a717
children b8ad45b3a573
rev   line source
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
1 import os
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
2 import yaml
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
3
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
4 from .buildtasks import Compile, Assemble, Link
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
5 from .objectfile import ObjectFile
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
6 from .target.target_list import target_list
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
7
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
8
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
9 targets = {t.name: t for t in target_list}
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
10 targetnames = list(targets.keys())
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
11
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
12 class RecipeLoader:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
13 """ Loads a recipe into a runner from a dictionary or file """
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
14 def __init__(self):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
15 self.directive_handlers = {}
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
16 for a in dir(self):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
17 if a.startswith('handle_'):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
18 f = getattr(self, a)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
19 self.directive_handlers[a[7:]] = f
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
20
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
21 def load_file(self, recipe_file, runner):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
22 """ Loads a recipe dictionary into a task runner """
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
23 self.recipe_dir = os.path.abspath(os.path.dirname(recipe_file))
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
24 with open(recipe_file, 'r') as f:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
25 recipe = yaml.load(f)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
26 self.runner = runner
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
27 self.load_dict(recipe)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
28
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
29 def relpath(self, filename):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
30 return os.path.join(self.recipe_dir, filename)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
31
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
32 def openfile(self, filename):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
33 return open(self.relpath(filename), 'r')
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
34
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
35 def handle_compile(self, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
36 sources = [self.openfile(s) for s in value['sources']]
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
37 includes = [self.openfile(i) for i in value['includes']]
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
38 target = targets[value['machine']]
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
39 output = ObjectFile()
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
40 task = Compile(sources, includes, target, output)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
41 self.runner.add_task(task)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
42 return task
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
43
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
44 def handle_assemble(self, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
45 asm_src = self.openfile(value['source'])
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
46 target = targets[value['machine']]
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
47 output = ObjectFile()
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
48 task = Assemble(asm_src, target, output)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
49 self.runner.add_task(task)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
50 return task
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
51
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
52 def handle_link(self, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
53 inputs = value['inputs']
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
54 objs = []
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
55 for i in inputs:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
56 task = self.load_dict(i)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
57 objs.append(task.output)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
58 layout = value['layout']
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
59 output = self.relpath(value['output'])
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
60 self.runner.add_task(Link(objs, layout, output))
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
61
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
62 def handle_apps(self, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
63 for a in value:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
64 self.load_dict(a)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
65
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
66 def load_dict(self, recipe):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
67 for command, value in recipe.items():
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
68 return self.directive_handlers[command](value)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
69
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
70
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
71