annotate python/ppci/buildtasks.py @ 342:86b02c98a717 devel

Moved target directory
author Windel Bouwman
date Sat, 01 Mar 2014 15:40:31 +0100
parents b00219172a42
children 3bb7dcfe5529
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
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
8
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
9 from .c3 import Builder
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
10 from .irutils import Verifier
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
11 from .codegen import CodeGenerator
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
12 from .transform import CleanPass, RemoveAddZero
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 336
diff changeset
13 from .tasks import Task, TaskError
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 336
diff changeset
14 from . import DiagnosticsManager, CompilerError
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
15 from .assembler import Assembler
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
16 from .objectfile import ObjectFile
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
17 from .linker import Linker
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 337
diff changeset
18 from .outstream import BinaryOutputStream
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
19
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
20
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
21 class BuildTask(Task):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
22 """ Base task for all kind of weird build tasks """
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
23 def __init__(self, name):
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
24 super().__init__(name)
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
25 self.logger = logging.getLogger('buildtask')
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
26
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
27
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
28 class Assemble(BuildTask):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
29 """ Task that can runs the assembler over the source and enters the
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
30 output into an object file """
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
31 def __init__(self, source, target, output_object):
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
32 super().__init__('Assemble')
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
33 self.source = source
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
34 self.output = output_object
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 337
diff changeset
35 self.ostream = BinaryOutputStream(self.output)
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
36 self.assembler = Assembler(target, self.ostream)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
37
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
38 def run(self):
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
39 self.ostream.selectSection('code')
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
40 self.assembler.assemble(self.source)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
41
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
42
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
43 class Compile(BuildTask):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
44 """ Task that compiles C3 source for some target into an object file """
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
45 def __init__(self, sources, includes, target, output_object):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
46 super().__init__('Compile')
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
47 self.sources = sources
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
48 self.includes = includes
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
49 self.target = target
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
50 self.output = output_object
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
51
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
52 def run(self):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
53 self.logger.debug('Compile started')
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
54 diag = DiagnosticsManager()
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
55 c3b = Builder(diag, self.target)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
56 cg = CodeGenerator(self.target)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
57
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
58 for ircode in c3b.build(self.sources, self.includes):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
59 if not ircode:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
60 return
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
61
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
62 d = {'ircode':ircode}
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
63 self.logger.debug('Verifying code {}'.format(ircode), extra=d)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
64 Verifier().verify(ircode)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
65
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
66 # Optimization passes:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
67 CleanPass().run(ircode)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
68 Verifier().verify(ircode)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
69 RemoveAddZero().run(ircode)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
70 Verifier().verify(ircode)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
71 CleanPass().run(ircode)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
72 Verifier().verify(ircode)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
73
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
74 # Code generation:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
75 d = {'ircode':ircode}
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
76 self.logger.debug('Starting code generation for {}'.format(ircode), extra=d)
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 337
diff changeset
77 o = BinaryOutputStream(self.output)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
78 cg.generate(ircode, o)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
79
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
80 if not c3b.ok:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
81 diag.printErrors()
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
82 raise TaskError('Compile errors')
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
83
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
84
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
85 class Link(BuildTask):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
86 """ Link together a collection of object files """
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
87 def __init__(self, objects, layout, output_file):
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
88 super().__init__('Link')
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
89 self.objects = objects
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
90 self.linker = Linker()
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
91 self.duration = 0.1337
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
92 self.layout = layout
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
93 self.output_file = output_file
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
94
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
95 def run(self):
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 336
diff changeset
96 try:
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 336
diff changeset
97 output_obj = self.linker.link(self.objects, self.layout)
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 336
diff changeset
98 except CompilerError as e:
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 336
diff changeset
99 raise TaskError(e.msg)
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
100 code = output_obj.get_section('code').data
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
101 with open(self.output_file, 'wb') as f:
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
102 f.write(code)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
103
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
104
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
105 class ObjCopy(BuildTask):
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
106 def __init__(self, objects, output_file):
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
107 super().__init__('ObjCopy')
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
108