|
| 1 | +#! /usr/bin/env python3 |
| 2 | + |
| 3 | +import fileinput |
| 4 | +import os |
| 5 | + |
| 6 | +# This script reads a template from standard input or the named file, and |
| 7 | +# outputs this with some template insertions, based on some values coming from |
| 8 | +# the make invocation. |
| 9 | + |
| 10 | +class Substr(): |
| 11 | + def __init__(self): |
| 12 | + self.subs = [] |
| 13 | + |
| 14 | + def add(self, name): |
| 15 | + value = os.environ.get(name) |
| 16 | + if value is None: |
| 17 | + raise Exception(f"Environment variable {name} not set") |
| 18 | + self.subs.append( (f"@{name}@", value) ) |
| 19 | + |
| 20 | + if name == "BUILDROOT": |
| 21 | + self.buildroot = value |
| 22 | + |
| 23 | + # Generate a substitution rules for a list of crates. |
| 24 | + def add_depcrates(self, name): |
| 25 | + crates = os.environ.get(name) |
| 26 | + if crates is None: |
| 27 | + raise Exception(f"Environment variable {name} not set") |
| 28 | + value = "" |
| 29 | + for crate in crates.split(): |
| 30 | + base = os.path.basename(crate) |
| 31 | + value += f"[dependencies.{base}]\npath = \"{self.buildroot}/{crate}\"\n" |
| 32 | + # TODO: Get the crate version from the crate's Cargo.toml file. |
| 33 | + value += f"version = \"0.1.0\"\n\n" |
| 34 | + self.subs.append( ("@DEPCRATES@", value) ) |
| 35 | + |
| 36 | + def add_deplinks(self, name): |
| 37 | + crates = os.environ.get(name) |
| 38 | + if crates is None: |
| 39 | + raise Exception(f"Environment variable {name} not set") |
| 40 | + value = "" |
| 41 | + for crate in crates.split(): |
| 42 | + base = os.path.basename(crate) |
| 43 | + base = base.replace("-", "_") |
| 44 | + if value != "": |
| 45 | + value += " " |
| 46 | + value += f"{base}::must_link();" |
| 47 | + self.subs.append( (f"@DEPLINKS@", value) ) |
| 48 | + |
| 49 | + def sub(self, line): |
| 50 | + for (k,v) in self.subs: |
| 51 | + line = line.replace(k, v) |
| 52 | + return line |
| 53 | + |
| 54 | +def subst(name): |
| 55 | + value = os.environ.get(name) |
| 56 | + |
| 57 | +subber = Substr(); |
| 58 | + |
| 59 | +subber.add("BUILDROOT") |
| 60 | +subber.add_depcrates("RUST_CRATES") |
| 61 | +subber.add_deplinks("RUST_CRATES") |
| 62 | + |
| 63 | +for line in fileinput.input(): |
| 64 | + line = subber.sub(line.rstrip()) |
| 65 | + print(line) |
0 commit comments