annotate python/ppci/buildtasks.py @ 377:9667d78ba79e

Switched to xml for project description
author Windel Bouwman
date Fri, 11 Apr 2014 15:47:50 +0200
parents 39bf68bf1891
children 6df89163e114
rev   line source
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
1
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
2 """
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
3 Defines task classes that can compile, link etc..
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
4 Task can depend upon one another.
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
5 """
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
6
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
7 import logging
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
8 import json
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
9
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
10 from .tasks import Task, TaskError, register_task
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
11 from .buildfunctions import c3compile, link, assemble
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
12 from pyyacc import ParserException
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
13 from . import CompilerError
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
14
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
15
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
16 @register_task("empty")
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
17 class EmptyTask(Task):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
18 """ Basic task that does nothing """
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
19 def run(self):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
20 pass
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
21
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
22
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
23 @register_task("echo")
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
24 class EchoTask(Task):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
25 """ Simple task that echoes a message """
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
26 def run(self):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
27 message = self.arguments['message']
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
28 print(message)
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 354
diff changeset
29
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 354
diff changeset
30
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
31 @register_task("property")
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
32 class Property(Task):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
33 """ Sets a property to a value """
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
34 def run(self):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
35 name = self.arguments['name']
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
36 value = self.arguments['value']
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
37 self.target.project.set_property(name, value)
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
38
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
39
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
40 @register_task("assemble")
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
41 class AssembleTask(Task):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
42 """ Task that can runs the assembler over the source and enters the
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
43 output into an object file """
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
44
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
45 def run(self):
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
46 target = self.get_argument('target')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
47 source = self.relpath(self.get_argument('source'))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
48 output_filename = self.relpath(self.get_argument('output'))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
49
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
50 try:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
51 output = assemble(source, target)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
52 except ParserException as e:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
53 raise TaskError('Error during assembly:' + str(e))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
54 except CompilerError as e:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
55 raise TaskError('Error during assembly:' + str(e))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
56 with open(output_filename, 'w') as f:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
57 output.save(f)
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 354
diff changeset
58 self.logger.debug('Assembling finished')
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
59
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
60
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
61 @register_task("compile")
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
62 class C3cTask(Task):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
63 """ Task that compiles C3 source for some target into an object file """
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
64 def run(self):
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
65 target = self.get_argument('target')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
66 sources = self.open_file_set(self.arguments['sources'])
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
67 output_filename = self.relpath(self.get_argument('output'))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
68 if 'includes' in self.arguments:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
69 includes = self.open_file_set(self.arguments['includes'])
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
70 else:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
71 includes = []
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
72
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
73 output = c3compile(sources, includes, target)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
74 # Store output:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
75 with open(output_filename, 'w') as f:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
76 output.save(f)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
77
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
78
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
79 def make_num(txt):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
80 if txt.startswith('0x'):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
81 return int(txt[2:], 16)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
82 else:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
83 return int(txt)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
84
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
85
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
86 def load_layout(filename):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
87 """ Load a linker layout file which contains directives where sections
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
88 must be placed into memory. """
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
89 try:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
90 with open(filename, 'r') as f:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
91 layout = json.load(f)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
92 except OSError as e:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
93 raise TaskError(str(e))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
94 for s in layout:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
95 layout[s] = make_num(layout[s])
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
96 return layout
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
97
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
98
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
99 @register_task("link")
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
100 class LinkTask(Task):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
101 """ Link together a collection of object files """
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
102 def run(self):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
103 layout = load_layout(self.relpath(self.get_argument('layout')))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
104 objects = self.open_file_set(self.get_argument('objects'))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
105 output_file = self.relpath(self.get_argument('output'))
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
106
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 336
diff changeset
107 try:
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
108 output_obj = link(objects, layout)
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 336
diff changeset
109 except CompilerError as e:
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 336
diff changeset
110 raise TaskError(e.msg)
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
111 # TODO: use layout here:
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
112 code = output_obj.get_section('code').data
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
113 with open(output_file, 'wb') as f:
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
114 f.write(code)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
115
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
116
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
117 class ObjCopyTask(Task):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
118 def run(self):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
119 pass
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
120