Mercurial > lcfOS
annotate python/ppci/buildtasks.py @ 337:b00219172a42
Added cool lm3s811 qemu project
author | Windel Bouwman |
---|---|
date | Thu, 20 Feb 2014 20:04:52 +0100 |
parents | d1ecc493384e |
children | 86b02c98a717 |
rev | line source |
---|---|
329 | 1 |
2 """ | |
3 Defines task classes that can compile, link etc.. | |
4 Task can depend upon one another. | |
5 """ | |
6 | |
7 import logging | |
8 | |
9 from .c3 import Builder | |
10 from .irutils import Verifier | |
11 from .codegen import CodeGenerator | |
12 from .transform import CleanPass, RemoveAddZero | |
337 | 13 from .tasks import Task, TaskError |
14 from . import DiagnosticsManager, CompilerError | |
334 | 15 from .assembler import Assembler |
16 from .objectfile import ObjectFile | |
17 from .linker import Linker | |
18 import outstream | |
329 | 19 |
335 | 20 |
331 | 21 class BuildTask(Task): |
334 | 22 """ Base task for all kind of weird build tasks """ |
331 | 23 def __init__(self, name): |
24 super().__init__(name) | |
25 self.logger = logging.getLogger('buildtask') | |
26 | |
27 | |
28 class Assemble(BuildTask): | |
334 | 29 """ Task that can runs the assembler over the source and enters the |
30 output into an object file """ | |
31 def __init__(self, source, target, output_object): | |
329 | 32 super().__init__('Assemble') |
334 | 33 self.source = source |
34 self.output = output_object | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
35 self.ostream = outstream.BinaryOutputStream(self.output) |
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 | 37 |
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 | 40 self.assembler.assemble(self.source) |
329 | 41 |
42 | |
331 | 43 class Compile(BuildTask): |
334 | 44 """ Task that compiles C3 source for some target into an object file """ |
329 | 45 def __init__(self, sources, includes, target, output_object): |
46 super().__init__('Compile') | |
47 self.sources = sources | |
48 self.includes = includes | |
49 self.target = target | |
50 self.output = output_object | |
51 | |
52 def run(self): | |
334 | 53 self.logger.debug('Compile started') |
329 | 54 diag = DiagnosticsManager() |
55 c3b = Builder(diag, self.target) | |
56 cg = CodeGenerator(self.target) | |
57 | |
58 for ircode in c3b.build(self.sources, self.includes): | |
59 if not ircode: | |
60 return | |
61 | |
62 d = {'ircode':ircode} | |
334 | 63 self.logger.debug('Verifying code {}'.format(ircode), extra=d) |
329 | 64 Verifier().verify(ircode) |
65 | |
66 # Optimization passes: | |
67 CleanPass().run(ircode) | |
68 Verifier().verify(ircode) | |
69 RemoveAddZero().run(ircode) | |
70 Verifier().verify(ircode) | |
71 CleanPass().run(ircode) | |
72 Verifier().verify(ircode) | |
73 | |
74 # Code generation: | |
75 d = {'ircode':ircode} | |
334 | 76 self.logger.debug('Starting code generation for {}'.format(ircode), extra=d) |
335 | 77 o = outstream.BinaryOutputStream(self.output) |
334 | 78 cg.generate(ircode, o) |
329 | 79 |
80 if not c3b.ok: | |
81 diag.printErrors() | |
82 raise TaskError('Compile errors') | |
83 | |
84 | |
331 | 85 class Link(BuildTask): |
334 | 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 | 88 super().__init__('Link') |
334 | 89 self.objects = objects |
90 self.linker = Linker() | |
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 | 94 |
95 def run(self): | |
337 | 96 try: |
97 output_obj = self.linker.link(self.objects, self.layout) | |
98 except CompilerError as e: | |
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 | 103 |
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 | 108 |