From a4d0442e45ebbb0a4309bef004e3ee88da348c0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Carretero?= Date: Sun, 31 Aug 2014 22:37:07 -0400 Subject: [PATCH] extras: add file_to_object tool --- playground/embedded_resources/wscript | 31 ++------------ waflib/extras/file_to_object.py | 60 +++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 waflib/extras/file_to_object.py diff --git a/playground/embedded_resources/wscript b/playground/embedded_resources/wscript index 643ac125..3a872cbd 100644 --- a/playground/embedded_resources/wscript +++ b/playground/embedded_resources/wscript @@ -13,45 +13,22 @@ from waflib.TaskGen import feature, before_method def options(opt): opt.load('compiler_c') + opt.load('file_to_object') def configure(cfg): cfg.load('compiler_c') - cfg.find_program('ld') - -class resource_ldr(Task.Task): - def run(tsk): - dst = tsk.outputs[0] - src = tsk.inputs[0] - cmd = tsk.env.LD + [ - '-r', - '-b', 'binary', - '-o', dst.abspath(), - src.name, - ] - tsk.exec_command(cmd, cwd=src.parent.abspath()) - -@feature('resource_embed') -@before_method('process_source') -def ld_binary(self): - src = self.path.find_resource(self.source) - dst = src.change_ext('.o') - tsk = self.create_task('resource_ldr', src, dst) - try: - self.compiled_tasks.append(tsk) - except AttributeError: - self.compiled_tasks = [tsk] - self.source = [] + cfg.load('file_to_object') def build(bld): bld( name='example', source='main.c', - features='resource_embed', + features='file_to_object', ) bld( target = 'app', features = 'c cprogram', source = 'main.c', use='example', - ) + ) diff --git a/waflib/extras/file_to_object.py b/waflib/extras/file_to_object.py new file mode 100644 index 00000000..1d902ac1 --- /dev/null +++ b/waflib/extras/file_to_object.py @@ -0,0 +1,60 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Tool to embed file into objects + +__author__ = __maintainer__ = "Jérôme Carretero " +__copyright__ = "Jérôme Carretero, 2014" + +""" + +This tool allows to embed file contents in object files (.o). +It is not exactly portable, and the file contents are reachable +using various non-portable fashions. +The goal here is to provide a functional interface to the embedding +of file data in objects. +See the ``playground/embedded_resources`` example for an example. + +Usage:: + + bld( + name='pipeline', + # ^ Reference this in use="..." for things using the generated code + features='file_to_object', + source='some.file', + # ^ Name of the file to embed in binary section. + ) + +Known issues: + +- Currently only handles elf files with GNU ld. + +- Destination is named like source, with extension renamed to .o + eg. some.file -> some.o + +""" + +from waflib import Task, Utils, TaskGen + +class file_to_object(Task.Task): + run_str = '${LD} -r -b binary -o ${TGT[0].abspath()} ${SRC[0].name}' + color = 'CYAN' + +@TaskGen.feature('file_to_object') +@TaskGen.before_method('process_source') +def tg_file_to_object(self): + bld = self.bld + src = self.to_nodes(self.source) + assert len(src) == 1 + src = src[0] + tgt = src.change_ext('.o') + task = self.create_task('file_to_object', + src, tgt, cwd=src.parent.abspath()) + try: + self.compiled_tasks.append(task) + except AttributeError: + self.compiled_tasks = [task] + self.source = [] + +def configure(conf): + conf.load('gcc') + conf.env.LD = [ conf.env.CC[0].replace('gcc', 'ld') ]