342
|
1 import os
|
|
2 import yaml
|
|
3
|
|
4 from .buildtasks import Compile, Assemble, Link
|
|
5 from .objectfile import ObjectFile
|
366
|
6 from .target.target_list import targets
|
342
|
7
|
353
|
8
|
342
|
9 class RecipeLoader:
|
|
10 """ Loads a recipe into a runner from a dictionary or file """
|
355
|
11 def __init__(self, runner):
|
|
12 self.runner = runner
|
342
|
13 self.directive_handlers = {}
|
|
14 for a in dir(self):
|
|
15 if a.startswith('handle_'):
|
|
16 f = getattr(self, a)
|
|
17 self.directive_handlers[a[7:]] = f
|
|
18
|
355
|
19 def load_file(self, recipe_file):
|
|
20 """ Loads a recipe dictionary from file """
|
342
|
21 self.recipe_dir = os.path.abspath(os.path.dirname(recipe_file))
|
|
22 with open(recipe_file, 'r') as f:
|
|
23 recipe = yaml.load(f)
|
|
24 self.load_dict(recipe)
|
|
25
|
|
26 def relpath(self, filename):
|
|
27 return os.path.join(self.recipe_dir, filename)
|
|
28
|
|
29 def openfile(self, filename):
|
|
30 return open(self.relpath(filename), 'r')
|
|
31
|
|
32 def handle_compile(self, value):
|
|
33 sources = [self.openfile(s) for s in value['sources']]
|
367
|
34 if 'includes' in value:
|
|
35 includes = [self.openfile(i) for i in value['includes']]
|
|
36 else:
|
|
37 includes = []
|
342
|
38 target = targets[value['machine']]
|
|
39 output = ObjectFile()
|
|
40 task = Compile(sources, includes, target, output)
|
|
41 self.runner.add_task(task)
|
|
42 return task
|
|
43
|
|
44 def handle_assemble(self, value):
|
|
45 asm_src = self.openfile(value['source'])
|
|
46 target = targets[value['machine']]
|
|
47 output = ObjectFile()
|
|
48 task = Assemble(asm_src, target, output)
|
|
49 self.runner.add_task(task)
|
|
50 return task
|
|
51
|
|
52 def handle_link(self, value):
|
|
53 inputs = value['inputs']
|
|
54 objs = []
|
|
55 for i in inputs:
|
|
56 task = self.load_dict(i)
|
|
57 objs.append(task.output)
|
|
58 layout = value['layout']
|
|
59 output = self.relpath(value['output'])
|
|
60 self.runner.add_task(Link(objs, layout, output))
|
|
61
|
|
62 def handle_apps(self, value):
|
|
63 for a in value:
|
|
64 self.load_dict(a)
|
|
65
|
|
66 def load_dict(self, recipe):
|
|
67 for command, value in recipe.items():
|
|
68 return self.directive_handlers[command](value)
|
|
69
|