annotate python/ppci/buildtasks.py @ 331:a78b41ff6ad2

Added better recipe files
author Windel Bouwman
date Fri, 07 Feb 2014 12:39:59 +0100
parents 8f6f3ace4e78
children 87feb8a23b4d
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
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
13 from .tasks import Task
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
14 from . import DiagnosticsManager
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
15
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
16
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
17 class BuildTask(Task):
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
18 def __init__(self, name):
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
19 super().__init__(name)
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
20 self.logger = logging.getLogger('buildtask')
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
21
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
22
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
23 class Assemble(BuildTask):
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
24 def __init__(self):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
25 super().__init__('Assemble')
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
26
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
27 def run(self):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
28 pass
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
29
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
30
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
31 class Compile(BuildTask):
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
32 """ Task that compiles source to some target """
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
33 def __init__(self, sources, includes, target, output_object):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
34 super().__init__('Compile')
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
35 self.sources = sources
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
36 self.includes = includes
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
37 self.target = target
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
38 self.output = output_object
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
39
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
40 def run(self):
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
41 self.logger.info('Zcc started {}'.format(self.sources))
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
42 diag = DiagnosticsManager()
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
43 c3b = Builder(diag, self.target)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
44 cg = CodeGenerator(self.target)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
45
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
46 for ircode in c3b.build(self.sources, self.includes):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
47 if not ircode:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
48 return
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
49
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
50 d = {'ircode':ircode}
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
51 self.logger.info('Verifying code {}'.format(ircode), extra=d)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
52 Verifier().verify(ircode)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
53
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
54 # Optimization passes:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
55 CleanPass().run(ircode)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
56 Verifier().verify(ircode)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
57 RemoveAddZero().run(ircode)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
58 Verifier().verify(ircode)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
59 CleanPass().run(ircode)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
60 Verifier().verify(ircode)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
61
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
62 # Code generation:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
63 d = {'ircode':ircode}
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
64 self.logger.info('Starting code generation for {}'.format(ircode), extra=d)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
65 cg.generate(ircode, self.output)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
66
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
67 # TODO: fixup references, do this in another way?
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
68 self.output.backpatch()
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
69 if not c3b.ok:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
70 diag.printErrors()
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
71 raise TaskError('Compile errors')
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
72
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
73
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
74 class Link(BuildTask):
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
75 def __init__(self, objects, output_file):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
76 super().__init__('Link')
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
77
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
78
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
79 class ObjCopy(Task):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
80 pass
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
81
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
82
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
83 def load_recipe(recipe_file, runner):
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
84 """ Loads a recipe dictionary into a task runner """
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
85 for command, value in recipe:
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
86 if command == 'compile':
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
87 sources = value['']
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
88 target = value['target']
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
89 runner.add_task(Compile())
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
90 else:
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 329
diff changeset
91 raise Exception()
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
92