Mercurial > lcfOS
annotate python/ppci/buildtasks.py @ 348:442fb043d149
Added log option to zcc
author | Windel Bouwman |
---|---|
date | Sat, 08 Mar 2014 15:32:33 +0100 |
parents | 3bb7dcfe5529 |
children | 5477e499b039 |
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 | |
348 | 18 from .outstream import BinaryOutputStream, MasterOutputStream, LoggerOutputStream |
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 | |
342 | 35 self.ostream = BinaryOutputStream(self.output) |
346 | 36 self.assembler = Assembler(target) |
329 | 37 |
38 def run(self): | |
348 | 39 self.ostream.select_section('code') |
346 | 40 self.assembler.assemble(self.source, self.ostream) |
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) |
348 | 77 |
78 o2 = BinaryOutputStream(self.output) | |
79 o1 = LoggerOutputStream() | |
80 o = MasterOutputStream() | |
81 o.add_substream(o1) | |
82 o.add_substream(o2) | |
334 | 83 cg.generate(ircode, o) |
329 | 84 |
85 if not c3b.ok: | |
86 diag.printErrors() | |
87 raise TaskError('Compile errors') | |
88 | |
89 | |
331 | 90 class Link(BuildTask): |
334 | 91 """ 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
|
92 def __init__(self, objects, layout, output_file): |
329 | 93 super().__init__('Link') |
334 | 94 self.objects = objects |
95 self.linker = Linker() | |
96 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
|
97 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
|
98 self.output_file = output_file |
334 | 99 |
100 def run(self): | |
337 | 101 try: |
102 output_obj = self.linker.link(self.objects, self.layout) | |
103 except CompilerError as e: | |
104 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
|
105 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
|
106 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
|
107 f.write(code) |
329 | 108 |
109 | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
110 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
|
111 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
|
112 super().__init__('ObjCopy') |
329 | 113 |