view python/ppci/recipe.py @ 353:b8ad45b3a573

Started with strings
author Windel Bouwman
date Sun, 09 Mar 2014 18:49:10 +0100
parents 86b02c98a717
children c2ddc8a36f5e
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):
        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.runner = runner
        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)