annotate python/ppci/recipe.py @ 366:39bf68bf1891

Fix sample tests and deterministic build
author Windel Bouwman
date Fri, 21 Mar 2014 09:43:01 +0100
parents c2ddc8a36f5e
children 577ed7fb3fe4
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
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 355
diff changeset
6 from .target.target_list import targets
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
7
353
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 342
diff changeset
8
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
9 class RecipeLoader:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
10 """ Loads a recipe into a runner from a dictionary or file """
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 353
diff changeset
11 def __init__(self, runner):
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 353
diff changeset
12 self.runner = runner
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
13 self.directive_handlers = {}
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
14 for a in dir(self):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
15 if a.startswith('handle_'):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
16 f = getattr(self, a)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
17 self.directive_handlers[a[7:]] = f
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
18
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 353
diff changeset
19 def load_file(self, recipe_file):
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 353
diff changeset
20 """ Loads a recipe dictionary from file """
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
21 self.recipe_dir = os.path.abspath(os.path.dirname(recipe_file))
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
22 with open(recipe_file, 'r') as f:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
23 recipe = yaml.load(f)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
24 self.load_dict(recipe)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
25
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
26 def relpath(self, filename):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
27 return os.path.join(self.recipe_dir, filename)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
28
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
29 def openfile(self, filename):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
30 return open(self.relpath(filename), 'r')
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
31
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
32 def handle_compile(self, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
33 sources = [self.openfile(s) for s in value['sources']]
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
34 includes = [self.openfile(i) for i in value['includes']]
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
35 target = targets[value['machine']]
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
36 output = ObjectFile()
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
37 task = Compile(sources, includes, target, output)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
38 self.runner.add_task(task)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
39 return task
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
40
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
41 def handle_assemble(self, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
42 asm_src = self.openfile(value['source'])
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
43 target = targets[value['machine']]
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
44 output = ObjectFile()
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
45 task = Assemble(asm_src, target, output)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
46 self.runner.add_task(task)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
47 return task
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
48
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
49 def handle_link(self, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
50 inputs = value['inputs']
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
51 objs = []
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
52 for i in inputs:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
53 task = self.load_dict(i)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
54 objs.append(task.output)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
55 layout = value['layout']
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
56 output = self.relpath(value['output'])
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
57 self.runner.add_task(Link(objs, layout, output))
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
58
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
59 def handle_apps(self, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
60 for a in value:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
61 self.load_dict(a)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
62
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
63 def load_dict(self, recipe):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
64 for command, value in recipe.items():
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
65 return self.directive_handlers[command](value)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
66