diff python/ppci/buildtasks.py @ 366:39bf68bf1891

Fix sample tests and deterministic build
author Windel Bouwman
date Fri, 21 Mar 2014 09:43:01 +0100
parents 5477e499b039
children 9667d78ba79e
line wrap: on
line diff
--- a/python/ppci/buildtasks.py	Wed Mar 19 22:32:04 2014 +0100
+++ b/python/ppci/buildtasks.py	Fri Mar 21 09:43:01 2014 +0100
@@ -16,8 +16,38 @@
 from .objectfile import ObjectFile
 from .linker import Linker
 from .outstream import BinaryOutputStream, MasterOutputStream, LoggerOutputStream
+from .target.target_list import targets
+from .target import Target
 
 
+def fix_target(tg):
+    """ Try to return an instance of the Target class """
+    if isinstance(tg, Target):
+        return tg
+    elif isinstance(tg, str):
+        if tg in targets:
+            return targets[tg]
+        else:
+            raise TaskError('Target {} not found'.format(tg))
+    raise TaskError('Invalid target {}'.format(tg))
+
+
+def fix_file(f):
+    """ Determine if argument is a file like object or make it so! """
+    if hasattr(f, 'read'):
+        # Assume this is a file like object
+        return f
+    elif isinstance(f, str):
+        return open(f, 'r')
+    else:
+        raise TaskError('cannot use {} as input'.format(f))
+
+def fix_object(o):
+    if isinstance(o, ObjectFile):
+        return o
+    else:
+        raise TaskError('Cannot use {} as objectfile'.format(o))
+
 class BuildTask(Task):
     """ Base task for all kind of weird build tasks """
     def __init__(self, name):
@@ -30,23 +60,27 @@
         output into an object file """
     def __init__(self, source, target, output_object):
         super().__init__('Assemble')
-        self.source = source
+        self.source = fix_file(source)
         self.output = output_object
-        self.ostream = BinaryOutputStream(self.output)
-        self.assembler = Assembler(target)
+        o2 = BinaryOutputStream(self.output)
+        o1 = LoggerOutputStream()
+        self.ostream = MasterOutputStream([o1, o2])
+        self.assembler = Assembler(fix_target(target))
 
     def run(self):
+        self.logger.debug('Assembling into code section')
         self.ostream.select_section('code')
         self.assembler.assemble(self.source, self.ostream)
+        self.logger.debug('Assembling finished')
 
 
 class Compile(BuildTask):
     """ Task that compiles C3 source for some target into an object file """
     def __init__(self, sources, includes, target, output_object):
         super().__init__('Compile')
-        self.sources = sources
-        self.includes = includes
-        self.target = target
+        self.sources = list(map(fix_file, sources))
+        self.includes = list(map(fix_file, includes))
+        self.target = fix_target(target)
         self.output = output_object
 
     def run(self):
@@ -78,9 +112,7 @@
 
             o2 = BinaryOutputStream(self.output)
             o1 = LoggerOutputStream()
-            o = MasterOutputStream()
-            o.add_substream(o1)
-            o.add_substream(o2)
+            o = MasterOutputStream([o1, o2])
             cg.generate(ircode, o)
 
         if not c3b.ok:
@@ -92,7 +124,7 @@
     """ Link together a collection of object files """
     def __init__(self, objects, layout, output_file):
         super().__init__('Link')
-        self.objects = objects
+        self.objects = list(map(fix_object, objects))
         self.linker = Linker()
         self.duration = 0.1337
         self.layout = layout