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