annotate python/ppci/recipe.py @ 357:818be710e13d

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