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']]
|
|
34 includes = [self.openfile(i) for i in value['includes']]
|
|
35 target = targets[value['machine']]
|
|
36 output = ObjectFile()
|
|
37 task = Compile(sources, includes, target, output)
|
|
38 self.runner.add_task(task)
|
|
39 return task
|
|
40
|
|
41 def handle_assemble(self, value):
|
|
42 asm_src = self.openfile(value['source'])
|
|
43 target = targets[value['machine']]
|
|
44 output = ObjectFile()
|
|
45 task = Assemble(asm_src, target, output)
|
|
46 self.runner.add_task(task)
|
|
47 return task
|
|
48
|
|
49 def handle_link(self, value):
|
|
50 inputs = value['inputs']
|
|
51 objs = []
|
|
52 for i in inputs:
|
|
53 task = self.load_dict(i)
|
|
54 objs.append(task.output)
|
|
55 layout = value['layout']
|
|
56 output = self.relpath(value['output'])
|
|
57 self.runner.add_task(Link(objs, layout, output))
|
|
58
|
|
59 def handle_apps(self, value):
|
|
60 for a in value:
|
|
61 self.load_dict(a)
|
|
62
|
|
63 def load_dict(self, recipe):
|
|
64 for command, value in recipe.items():
|
|
65 return self.directive_handlers[command](value)
|
|
66
|