2014-09-01 04:37:07 +02:00
|
|
|
#!/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
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2015-10-20 14:18:07 +02:00
|
|
|
import os
|
2015-10-11 11:32:27 +02:00
|
|
|
from waflib import Task, TaskGen, Errors
|
2015-03-03 00:22:07 +01:00
|
|
|
|
|
|
|
def filename_c_escape(x):
|
|
|
|
return x.replace("\\", "\\\\")
|
|
|
|
|
2015-02-20 18:04:34 +01:00
|
|
|
class file_to_object_s(Task.Task):
|
2014-09-01 04:37:07 +02:00
|
|
|
color = 'CYAN'
|
2016-04-16 13:04:12 +02:00
|
|
|
vars = ['DEST_CPU', 'DEST_BINFMT']
|
2015-01-13 19:27:38 +01:00
|
|
|
|
2014-09-08 21:27:45 +02:00
|
|
|
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'
|
2014-09-08 23:36:49 +02:00
|
|
|
align = 8
|
2014-09-08 21:27:45 +02:00
|
|
|
elif self.env.DEST_CPU in ('x86','arm', 'thumb', 'm68k'):
|
|
|
|
unit = 'long'
|
2014-09-08 23:36:49 +02:00
|
|
|
align = 4
|
2014-09-08 21:27:45 +02:00
|
|
|
else:
|
|
|
|
raise Errors.WafError("Unsupported DEST_CPU, please report bug!")
|
|
|
|
|
2015-03-03 00:22:07 +01:00
|
|
|
file = filename_c_escape(file)
|
2014-09-08 21:27:45 +02:00
|
|
|
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:
|
2014-09-08 23:36:49 +02:00
|
|
|
.align %(align)d
|
2014-09-08 21:27:45 +02:00
|
|
|
%(name)s_size:
|
|
|
|
.%(unit)s 0x%(size)x
|
|
|
|
""" % locals())
|
2014-09-01 04:37:07 +02:00
|
|
|
|
2015-02-20 18:04:34 +01:00
|
|
|
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)
|
|
|
|
|
2015-10-20 14:18:07 +02:00
|
|
|
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)
|
2015-02-20 18:04:34 +01:00
|
|
|
|
2015-10-20 14:18:07 +02:00
|
|
|
self.outputs[0].write(\
|
2015-02-20 18:04:34 +01:00
|
|
|
"""
|
|
|
|
unsigned long %(name)s_size = %(size)dL;
|
|
|
|
char const %(name)s_start[] = {
|
|
|
|
%(data)s
|
|
|
|
};
|
2015-10-20 14:18:07 +02:00
|
|
|
char const %(name)s_end[] = {};
|
2015-02-20 18:04:34 +01:00
|
|
|
""" % locals())
|
|
|
|
|
2014-09-01 04:37:07 +02:00
|
|
|
@TaskGen.feature('file_to_object')
|
|
|
|
@TaskGen.before_method('process_source')
|
|
|
|
def tg_file_to_object(self):
|
|
|
|
bld = self.bld
|
2015-03-19 03:25:05 +01:00
|
|
|
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')
|
2015-10-11 11:32:27 +02:00
|
|
|
tsk = self.create_task('file_to_object_s', src, tgt)
|
|
|
|
tsk.cwd = src.parent.abspath() # verify
|
2015-03-19 03:25:05 +01:00
|
|
|
else:
|
|
|
|
tgt = src.parent.find_or_declare(src.name + '.f2o.c')
|
2015-10-11 11:32:27 +02:00
|
|
|
tsk = self.create_task('file_to_object_c', src, tgt)
|
|
|
|
tsk.cwd = src.parent.abspath() # verify
|
2015-03-19 03:25:05 +01:00
|
|
|
targets.append(tgt)
|
|
|
|
self.source = targets
|
2014-09-01 04:37:07 +02:00
|
|
|
|
|
|
|
def configure(conf):
|
2014-09-08 21:27:45 +02:00
|
|
|
conf.load('gas')
|
2015-10-13 17:46:28 +02:00
|
|
|
conf.env.F2O_METHOD = ["c"]
|
2014-09-08 21:27:45 +02:00
|
|
|
|