summaryrefslogtreecommitdiffstats
path: root/extras/file_to_object.py
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-03-17 17:31:05 +0100
committerDavid Robillard <d@drobilla.net>2019-03-17 17:31:05 +0100
commit406f89271452fdb573c7e28113b1ed08ff2b4eda (patch)
treed2dcbaf61f3749f73dc7a5e10d3fc6cd5e6e129a /extras/file_to_object.py
parent7983a5aae615290d04fd43cbc2752f8cf4a46d10 (diff)
downloadsuil-406f89271452fdb573c7e28113b1ed08ff2b4eda.tar.gz
suil-406f89271452fdb573c7e28113b1ed08ff2b4eda.tar.bz2
suil-406f89271452fdb573c7e28113b1ed08ff2b4eda.zip
Squashed 'waflib/' changes from 915dcb1..e7a29b6
e7a29b6 Upgrade to waf 2.0.15 8280f9d Add command for running executables from the build directory 8073c1a Make make_simple_dox() safe in case of exception 70d03b8 Avoid use of global counter hacks for configuration display b7d689a Rewrite test framework 94deadf Automatically add options and move add_flags() to options context f4259ee Reduce system include path noise 927b608 Automatically display configuration header c44b8f3 Set line justification from a constant in the wscript a48e26f Automatically detect if wscript has a test hook ef66724 Save runtime variables in the environment 63bcbcd Clean up TestContext b1d9505 Add ExecutionContext for setting runtime environment 387c1df Add show_diff() and test_file_equals() utilities 29d4d29 Fix in-tree library paths 9fde01f Add custom configuration context 6d3612f Add lib_path_name constant git-subtree-dir: waflib git-subtree-split: e7a29b6b9b2f842314244c23c14d8f8f560904e1
Diffstat (limited to 'extras/file_to_object.py')
-rw-r--r--extras/file_to_object.py137
1 files changed, 137 insertions, 0 deletions
diff --git a/extras/file_to_object.py b/extras/file_to_object.py
new file mode 100644
index 0000000..1393b51
--- /dev/null
+++ b/extras/file_to_object.py
@@ -0,0 +1,137 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# Tool to embed file into objects
+
+__author__ = __maintainer__ = "Jérôme Carretero <cJ-waf@zougloub.eu>"
+__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:
+
+- Destination is named like source, with extension renamed to .o
+ eg. some.file -> some.o
+
+"""
+
+import os
+from waflib import Task, TaskGen, Errors
+
+def filename_c_escape(x):
+ return x.replace("\\", "\\\\")
+
+class file_to_object_s(Task.Task):
+ color = 'CYAN'
+ vars = ['DEST_CPU', 'DEST_BINFMT']
+
+ def run(self):
+ name = []
+ for i, x in enumerate(self.inputs[0].name):
+ if x.isalnum():
+ name.append(x)
+ else:
+ name.append('_')
+ file = self.inputs[0].abspath()
+ size = os.path.getsize(file)
+ if self.env.DEST_CPU in ('x86_64', 'ia', 'aarch64'):
+ unit = 'quad'
+ align = 8
+ elif self.env.DEST_CPU in ('x86','arm', 'thumb', 'm68k'):
+ unit = 'long'
+ align = 4
+ else:
+ raise Errors.WafError("Unsupported DEST_CPU, please report bug!")
+
+ file = filename_c_escape(file)
+ name = "_binary_" + "".join(name)
+ rodata = ".section .rodata"
+ if self.env.DEST_BINFMT == "mac-o":
+ name = "_" + name
+ rodata = ".section __TEXT,__const"
+
+ with open(self.outputs[0].abspath(), 'w') as f:
+ f.write(\
+"""
+ .global %(name)s_start
+ .global %(name)s_end
+ .global %(name)s_size
+ %(rodata)s
+%(name)s_start:
+ .incbin "%(file)s"
+%(name)s_end:
+ .align %(align)d
+%(name)s_size:
+ .%(unit)s 0x%(size)x
+""" % locals())
+
+class file_to_object_c(Task.Task):
+ color = 'CYAN'
+ def run(self):
+ name = []
+ for i, x in enumerate(self.inputs[0].name):
+ if x.isalnum():
+ name.append(x)
+ else:
+ name.append('_')
+ file = self.inputs[0].abspath()
+ size = os.path.getsize(file)
+
+ name = "_binary_" + "".join(name)
+
+ data = self.inputs[0].read('rb')
+ lines, line = [], []
+ for idx_byte, byte in enumerate(data):
+ line.append(byte)
+ if len(line) > 15 or idx_byte == size-1:
+ lines.append(", ".join(("0x%02x" % ord(x)) for x in line))
+ line = []
+ data = ",\n ".join(lines)
+
+ self.outputs[0].write(\
+"""
+unsigned long %(name)s_size = %(size)dL;
+char const %(name)s_start[] = {
+ %(data)s
+};
+char const %(name)s_end[] = {};
+""" % locals())
+
+@TaskGen.feature('file_to_object')
+@TaskGen.before_method('process_source')
+def tg_file_to_object(self):
+ bld = self.bld
+ sources = self.to_nodes(self.source)
+ targets = []
+ for src in sources:
+ if bld.env.F2O_METHOD == ["asm"]:
+ tgt = src.parent.find_or_declare(src.name + '.f2o.s')
+ tsk = self.create_task('file_to_object_s', src, tgt)
+ tsk.cwd = src.parent.abspath() # verify
+ else:
+ tgt = src.parent.find_or_declare(src.name + '.f2o.c')
+ tsk = self.create_task('file_to_object_c', src, tgt)
+ tsk.cwd = src.parent.abspath() # verify
+ targets.append(tgt)
+ self.source = targets
+
+def configure(conf):
+ conf.load('gas')
+ conf.env.F2O_METHOD = ["c"]
+