Mercurial > lcfOS
annotate python/ppci/buildtasks.py @ 354:5477e499b039
Added some sort of string functionality
author | Windel Bouwman |
---|---|
date | Thu, 13 Mar 2014 18:59:06 +0100 |
parents | 442fb043d149 |
children | 39bf68bf1891 |
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: | |
354 | 60 # Something went wrong, do not continue the code generation |
61 continue | |
329 | 62 |
63 d = {'ircode':ircode} | |
334 | 64 self.logger.debug('Verifying code {}'.format(ircode), extra=d) |
329 | 65 Verifier().verify(ircode) |
66 | |
67 # Optimization passes: | |
68 CleanPass().run(ircode) | |
69 Verifier().verify(ircode) | |
70 RemoveAddZero().run(ircode) | |
71 Verifier().verify(ircode) | |
72 CleanPass().run(ircode) | |
73 Verifier().verify(ircode) | |
74 | |
75 # Code generation: | |
76 d = {'ircode':ircode} | |
334 | 77 self.logger.debug('Starting code generation for {}'.format(ircode), extra=d) |
348 | 78 |
79 o2 = BinaryOutputStream(self.output) | |
80 o1 = LoggerOutputStream() | |
81 o = MasterOutputStream() | |
82 o.add_substream(o1) | |
83 o.add_substream(o2) | |
334 | 84 cg.generate(ircode, o) |
329 | 85 |
86 if not c3b.ok: | |
87 diag.printErrors() | |
88 raise TaskError('Compile errors') | |
89 | |
90 | |
331 | 91 class Link(BuildTask): |
334 | 92 """ 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
|
93 def __init__(self, objects, layout, output_file): |
329 | 94 super().__init__('Link') |
334 | 95 self.objects = objects |
96 self.linker = Linker() | |
97 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
|
98 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
|
99 self.output_file = output_file |
334 | 100 |
101 def run(self): | |
337 | 102 try: |
103 output_obj = self.linker.link(self.objects, self.layout) | |
104 except CompilerError as e: | |
105 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
|
106 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
|
107 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
|
108 f.write(code) |
329 | 109 |
110 | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
111 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
|
112 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
|
113 super().__init__('ObjCopy') |
329 | 114 |