diff python/zcc.py @ 334:6f4753202b9a

Added more recipes
author Windel Bouwman
date Thu, 13 Feb 2014 22:02:08 +0100
parents 87feb8a23b4d
children d1ecc493384e
line wrap: on
line diff
--- a/python/zcc.py	Sun Feb 09 15:27:57 2014 +0100
+++ b/python/zcc.py	Thu Feb 13 22:02:08 2014 +0100
@@ -6,10 +6,10 @@
 import logging
 import yaml
 
-from ppci.buildtasks import Compile
+from ppci.buildtasks import Compile, Assemble, Link
 from ppci.tasks import TaskRunner
 from ppci.report import RstFormatter
-import outstream
+from ppci.objectfile import ObjectFile
 from target.target_list import target_list
 import ppci
 
@@ -29,7 +29,7 @@
     parser = argparse.ArgumentParser(description='lcfos Compiler')
 
     parser.add_argument('--log', help='Log level (INFO,DEBUG,[WARN])',
-                        type=logLevel, default='WARN')
+                        type=logLevel, default='INFO')
     parser.add_argument('--display-build-steps', action='store_true')
     sub_parsers = parser.add_subparsers(title='commands',
          description='possible commands', dest='command')
@@ -52,12 +52,21 @@
 
 
 class RecipeLoader:
+    """ Loads a recipe into a runner from a dictionary or file """
+    def __init__(self):
+        self.directive_handlers = {}
+        for a in dir(self):
+            if a.startswith('handle_'):
+                f = getattr(self, a)
+                self.directive_handlers[a[7:]] = f
+
     def load_file(self, recipe_file, runner):
         """ Loads a recipe dictionary into a task runner """
         self.recipe_dir = os.path.abspath(os.path.dirname(recipe_file))
         with open(recipe_file, 'r') as f:
             recipe = yaml.load(f)
-        self.load_dict(recipe, runner)
+        self.runner = runner
+        self.load_dict(recipe)
 
     def relpath(self, filename):
         return os.path.join(self.recipe_dir, filename)
@@ -65,24 +74,38 @@
     def openfile(self, filename):
         return open(self.relpath(filename), 'r')
 
-    def load_dict(self, recipe, runner):
+    def handle_compile(self, value):
+        sources = [self.openfile(s) for s in value['sources']]
+        includes = [self.openfile(i) for i in value['includes']]
+        target = targets[value['machine']]
+        output = ObjectFile()
+        task = Compile(sources, includes, target, output)
+        self.runner.add_task(task)
+        return task
+
+    def handle_assemble(self, value):
+        asm_src = self.openfile(value['source'])
+        target = targets[value['machine']]
+        output = ObjectFile()
+        task = Assemble(asm_src, target, output)
+        self.runner.add_task(task)
+        return task
+
+    def handle_link(self, value):
+        inputs = value['inputs']
+        objs = []
+        for i in inputs:
+            task = self.load_dict(i)
+            objs.append(task.output)
+        self.runner.add_task(Link(objs, None))
+
+    def handle_apps(self, value):
+        for a in value:
+            self.load_dict(a)
+
+    def load_dict(self, recipe):
         for command, value in recipe.items():
-            if command == 'compile':
-                sources = [self.openfile(s) for s in value['sources']]
-                includes = [self.openfile(i) for i in value['includes']]
-                target = targets[value['machine']]
-                output = outstream.TextOutputStream()
-                runner.add_task(Compile(sources, includes, target, output))
-            elif command == 'link':
-                self.load_dict(value['inputs'], runner)
-                #runner.add_task(Link())
-            elif command == 'assemble':
-                pass
-            elif command == 'apps':
-                for a in value:
-                    self.load_dict(a, runner)
-            else:
-                raise NotImplementedError(command)
+            return self.directive_handlers[command](value)
 
 
 def main(args):
@@ -96,8 +119,8 @@
     runner = TaskRunner()
     if args.command == 'compile':
         tg = targets[args.target]
-        outs = outstream.TextOutputStream()
-        runner.add_task(Compile(args.source, args.imp, tg, outs))
+        output = ObjectFile()
+        runner.add_task(Compile(args.source, args.imp, tg, output))
     elif args.command == 'recipe':
         recipe_loader = RecipeLoader()
         recipe_loader.load_file(args.recipe_file, runner)