comparison python/zcc.py @ 342:86b02c98a717 devel

Moved target directory
author Windel Bouwman
date Sat, 01 Mar 2014 15:40:31 +0100
parents d1ecc493384e
children 442fb043d149
comparison
equal deleted inserted replaced
341:4d204f6f7d4e 342:86b02c98a717
2 2
3 import sys 3 import sys
4 import os 4 import os
5 import argparse 5 import argparse
6 import logging 6 import logging
7 import yaml
8 7
9 from ppci.buildtasks import Compile, Assemble, Link 8 from ppci.buildtasks import Compile, Assemble, Link
10 from ppci.tasks import TaskRunner 9 from ppci.tasks import TaskRunner
11 from ppci.report import RstFormatter 10 from ppci.report import RstFormatter
12 from ppci.objectfile import ObjectFile 11 from ppci.objectfile import ObjectFile
13 from target.target_list import target_list 12 from ppci.target.target_list import target_list
13 from ppci.recipe import RecipeLoader
14 import ppci 14 import ppci
15 15
16 16
17 def logLevel(s): 17 def logLevel(s):
18 """ Converts a string to a valid logging level """ 18 """ Converts a string to a valid logging level """
49 help='Specify a file to write the compile report to', 49 help='Specify a file to write the compile report to',
50 type=argparse.FileType('w')) 50 type=argparse.FileType('w'))
51 return parser 51 return parser
52 52
53 53
54 class RecipeLoader:
55 """ Loads a recipe into a runner from a dictionary or file """
56 def __init__(self):
57 self.directive_handlers = {}
58 for a in dir(self):
59 if a.startswith('handle_'):
60 f = getattr(self, a)
61 self.directive_handlers[a[7:]] = f
62
63 def load_file(self, recipe_file, runner):
64 """ Loads a recipe dictionary into a task runner """
65 self.recipe_dir = os.path.abspath(os.path.dirname(recipe_file))
66 with open(recipe_file, 'r') as f:
67 recipe = yaml.load(f)
68 self.runner = runner
69 self.load_dict(recipe)
70
71 def relpath(self, filename):
72 return os.path.join(self.recipe_dir, filename)
73
74 def openfile(self, filename):
75 return open(self.relpath(filename), 'r')
76
77 def handle_compile(self, value):
78 sources = [self.openfile(s) for s in value['sources']]
79 includes = [self.openfile(i) for i in value['includes']]
80 target = targets[value['machine']]
81 output = ObjectFile()
82 task = Compile(sources, includes, target, output)
83 self.runner.add_task(task)
84 return task
85
86 def handle_assemble(self, value):
87 asm_src = self.openfile(value['source'])
88 target = targets[value['machine']]
89 output = ObjectFile()
90 task = Assemble(asm_src, target, output)
91 self.runner.add_task(task)
92 return task
93
94 def handle_link(self, value):
95 inputs = value['inputs']
96 objs = []
97 for i in inputs:
98 task = self.load_dict(i)
99 objs.append(task.output)
100 layout = value['layout']
101 output = self.relpath(value['output'])
102 self.runner.add_task(Link(objs, layout, output))
103
104 def handle_apps(self, value):
105 for a in value:
106 self.load_dict(a)
107
108 def load_dict(self, recipe):
109 for command, value in recipe.items():
110 return self.directive_handlers[command](value)
111
112 54
113 def main(args): 55 def main(args):
114 # Configure some logging: 56 # Configure some logging:
115 logging.getLogger().setLevel(logging.DEBUG) 57 logging.getLogger().setLevel(logging.DEBUG)
116 ch = logging.StreamHandler() 58 ch = logging.StreamHandler()