annotate python/ppci/buildtasks.py @ 381:6df89163e114

Fix section and ldr pseudo instruction
author Windel Bouwman
date Sat, 26 Apr 2014 17:41:56 +0200
parents 9667d78ba79e
children 173e20a47fda
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))
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
56 except OSError as e:
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
57 raise TaskError('Error:' + str(e))
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
58 with open(output_filename, 'w') as f:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
59 output.save(f)
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 354
diff changeset
60 self.logger.debug('Assembling finished')
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
61
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
62
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
63 @register_task("compile")
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
64 class C3cTask(Task):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
65 """ Task that compiles C3 source for some target into an object file """
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
66 def run(self):
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
67 target = self.get_argument('target')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
68 sources = self.open_file_set(self.arguments['sources'])
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
69 output_filename = self.relpath(self.get_argument('output'))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
70 if 'includes' in self.arguments:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
71 includes = self.open_file_set(self.arguments['includes'])
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
72 else:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
73 includes = []
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
74
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
75 output = c3compile(sources, includes, target)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
76 # Store output:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
77 with open(output_filename, 'w') as f:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
78 output.save(f)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
79
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
80
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
81 def make_num(txt):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
82 if txt.startswith('0x'):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
83 return int(txt[2:], 16)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
84 else:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
85 return int(txt)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
86
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
87
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
88 def load_layout(filename):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
89 """ Load a linker layout file which contains directives where sections
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
90 must be placed into memory. """
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
91 try:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
92 with open(filename, 'r') as f:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
93 layout = json.load(f)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
94 except OSError as e:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
95 raise TaskError(str(e))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
96 for s in layout:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
97 layout[s] = make_num(layout[s])
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
98 return layout
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
99
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
100
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
101 @register_task("link")
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
102 class LinkTask(Task):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
103 """ Link together a collection of object files """
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
104 def run(self):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
105 layout = load_layout(self.relpath(self.get_argument('layout')))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
106 objects = self.open_file_set(self.get_argument('objects'))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
107 output_file = self.relpath(self.get_argument('output'))
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
108
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 336
diff changeset
109 try:
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
110 output_obj = link(objects, layout)
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 336
diff changeset
111 except CompilerError as e:
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 336
diff changeset
112 raise TaskError(e.msg)
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
113 # 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
114 code = output_obj.get_section('code').data
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
115 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
116 f.write(code)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
117
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
118
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
119 class ObjCopyTask(Task):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
120 def run(self):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 366
diff changeset
121 pass
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
122