view python/ppci/recipe.py @ 357:818be710e13d

Added acceptance function to burg
author Windel Bouwman
date Fri, 14 Mar 2014 15:14:29 +0100
parents c2ddc8a36f5e
children 39bf68bf1891
line wrap: on
line source

import os
import yaml

from .buildtasks import Compile, Assemble, Link
from .objectfile import ObjectFile
from .target.target_list import target_list


targets = {t.name: t for t in target_list}
targetnames = list(targets.keys())


class RecipeLoader:
    """ Loads a recipe into a runner from a dictionary or file """
    def __init__(self, runner):
        self.runner = runner
        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):
        """ Loads a recipe dictionary from file """
        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)

    def relpath(self, filename):
        return os.path.join(self.recipe_dir, filename)

    def openfile(self, filename):
        return open(self.relpath(filename), 'r')

    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)
        layout = value['layout']
        output = self.relpath(value['output'])
        self.runner.add_task(Link(objs, layout, output))

    def handle_apps(self, value):
        for a in value:
            self.load_dict(a)

    def load_dict(self, recipe):
        for command, value in recipe.items():
            return self.directive_handlers[command](value)