summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2019-10-20 23:50:12 -0400
committerDan Halbert <halbert@halwitz.org>2019-10-20 23:50:12 -0400
commit7b79ac37399927070e1931022f5759393202ed3d (patch)
tree3ae5f0200b13562c880ea10fe24a2e185d0e4bd2 /tools
parentbe8136dc6d09e2c3af5c20c881662ebf51a778d8 (diff)
Parameterize linker script
Diffstat (limited to 'tools')
-rwxr-xr-xtools/gen_ld_files.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/tools/gen_ld_files.py b/tools/gen_ld_files.py
new file mode 100755
index 000000000..fc80d46c9
--- /dev/null
+++ b/tools/gen_ld_files.py
@@ -0,0 +1,47 @@
+#! /usr/bin/env python3
+import argparse
+
+import os
+import os.path
+import sys
+import re
+from string import Template
+
+parser = argparse.ArgumentParser(description='Apply #define values to .template.ld file.')
+parser.add_argument('template_files', metavar='TEMPLATE_FILE', type=argparse.FileType('r'),
+ nargs='+', help="template filename: <something>.template.ld")
+parser.add_argument('--defines', type=argparse.FileType('r'), required=True)
+parser.add_argument('--out_dir', required=True)
+
+args = parser.parse_args()
+
+defines = {}
+
+# We're looking for lines like this:
+# <expression> ///DEFINE_VALUE NAME_OF_VALUE <optional Python lambda expression to transform value>
+VALUE_LINE_RE = re.compile(r'^([^/].*); ///DEFINE_VALUE (\w+)(.*)$')
+
+for line in args.defines:
+ match = VALUE_LINE_RE.match(line.strip())
+ if match:
+ value = match.group(1).strip()
+ name = match.group(2)
+ lambda_exp = match.group(3).strip()
+ # Apply the given lambda to the value if it is present, else just store the value.
+ defines[match.group(2)] = eval(lambda_exp)(value) if lambda_exp else value
+
+#print(defines)
+fail = False
+
+for template_file in args.template_files:
+ ld_template_basename = os.path.basename(template_file.name)
+ ld_pathname = os.path.join(args.out_dir, ld_template_basename.replace('.template.ld', '.ld'))
+ with open(ld_pathname, 'w') as output:
+ try:
+ output.write(Template(template_file.read()).substitute(defines))
+ except KeyError as e:
+ print("ERROR: {}: No #define for '{}'".format(ld_pathname, e.args[0]), file=sys.stderr)
+ fail = True
+
+if fail:
+ sys.exit(1)