| 1 |
#!/usr/bin/python3 |
| 2 |
""" |
| 3 |
Wrapper around cargo to have it build using Debian settings. |
| 4 |
|
| 5 |
SPDX-FileCopyrightText: 2022-2024 Jonas Smedegaard <[email protected]> |
| 6 |
SPDX-FileCopyrightText: 2015-2016 Luca Bruno <[email protected]> |
| 7 |
SPDX-FileCopyrightText: 2017-2019 Vasudeva Kamath <[email protected]> |
| 8 |
SPDX-FileCopyrightText: 2016-2019 Ximin Luo <[email protected]> |
| 9 |
|
| 10 |
SPDX-License-Identifier: (Apache-2.0 or MIT) and GPL-3-or-later |
| 11 |
|
| 12 |
Usage: |
| 13 |
export PATH=/path/to/dir/of/this/script:$PATH |
| 14 |
export CARGO_HOME=debian/cargo_home |
| 15 |
cargo prepare-debian /path/to/local/registry |
| 16 |
cargo build |
| 17 |
cargo test |
| 18 |
cargo install |
| 19 |
cargo clean |
| 20 |
[rm -rf /path/to/local/registry] |
| 21 |
|
| 22 |
The "prepare-debian" subcommand writes a config file to $CARGO_HOME |
| 23 |
that makes the subsequent invocations use our Debian flags, |
| 24 |
creates /path/to/local/registry and symlinks |
| 25 |
the contents of /usr/share/cargo/registry into it. You are then responsible for |
| 26 |
cleaning it up afterwards (a simple `rm -rf` should do). |
| 27 |
|
| 28 |
Make sure you add "Build-Depends: python3:native" if you use this directly. |
| 29 |
If using this only indirectly via dh-rust, then you only need "Build-Depends: |
| 30 |
dh-rust"; this is a general principle when declaring dependencies. |
| 31 |
|
| 32 |
If CARGO_HOME doesn't end with debian/cargo_home, then this script does nothing |
| 33 |
and passes through directly to cargo. |
| 34 |
|
| 35 |
Otherwise, you *must* set the following environment variables: |
| 36 |
|
| 37 |
- (required only for `cargo install`) DEB_CARGO_CRATE |
| 38 |
${crate}_${version} of whatever you're building. |
| 39 |
|
| 40 |
- CFLAGS CXXFLAGS CPPFLAGS LDFLAGS [*] |
| 41 |
- DEB_HOST_GNU_TYPE DEB_HOST_RUST_TYPE [*] |
| 42 |
|
| 43 |
- (required only for `cargo install`) DESTDIR |
| 44 |
DESTDIR to install build artifacts under. If running via dh-rust, this will |
| 45 |
be set automatically by debhelper, see `dh_auto_install` for details. |
| 46 |
|
| 47 |
- (optional) DEB_BUILD_OPTIONS DEB_BUILD_PROFILES |
| 48 |
|
| 49 |
- (optional) DEB_CARGO_INSTALL_PREFIX |
| 50 |
Prefix to install build artifacts under. Default: /usr. Sometimes you might |
| 51 |
want to change this to /usr/lib/cargo if the binary clashes with something |
| 52 |
else, and then symlink it into /usr/bin under an alternative name. |
| 53 |
|
| 54 |
- (optional) DEB_CARGO_CRATE_IN_REGISTRY |
| 55 |
Whether the crate is in the local-registry (1) or cwd (0, empty, default). |
| 56 |
|
| 57 |
For the envvars marked [*], it is easiest to set these in your d/rules via: |
| 58 |
|
| 59 |
include /usr/share/dpkg/architecture.mk |
| 60 |
include /usr/share/dpkg/buildflags.mk |
| 61 |
include /usr/share/rustc/architecture.mk |
| 62 |
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS |
| 63 |
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE |
| 64 |
""" |
| 65 |
|
| 66 |
import os |
| 67 |
import os.path |
| 68 |
import shutil |
| 69 |
import subprocess |
| 70 |
import sys |
| 71 |
|
| 72 |
FLAGS = "CFLAGS CXXFLAGS CPPFLAGS LDFLAGS" |
| 73 |
ARCHES = "DEB_HOST_GNU_TYPE DEB_HOST_RUST_TYPE" |
| 74 |
SYSTEM_REGISTRY = "/usr/share/cargo/registry" |
| 75 |
|
| 76 |
def log(*args): |
| 77 |
print("debian cargo wrapper:", *args, file=sys.stderr, flush=True) |
| 78 |
|
| 79 |
def logrun(*args, **kwargs): |
| 80 |
log("running subprocess", args, kwargs) |
| 81 |
return subprocess.run(*args, **kwargs) |
| 82 |
|
| 83 |
def sourcepath(p=None): |
| 84 |
return os.path.join(os.getcwd(), p) if p else os.getcwd() |
| 85 |
|
| 86 |
def prepare_debian(cargo_home, registry, host_gnu_type, ldflags): |
| 87 |
registry_path = sourcepath(registry) |
| 88 |
log("linking %s/* into %s/" % (SYSTEM_REGISTRY, registry_path)) |
| 89 |
os.makedirs(registry_path, exist_ok=True) |
| 90 |
crates = os.listdir(SYSTEM_REGISTRY) if os.path.isdir(SYSTEM_REGISTRY) else [] |
| 91 |
for c in crates: |
| 92 |
target = os.path.join(registry_path, c) |
| 93 |
if not os.path.islink(target): |
| 94 |
os.symlink(os.path.join(SYSTEM_REGISTRY, c), target) |
| 95 |
|
| 96 |
rustflags = "-C debuginfo=2 --cap-lints warn".split() |
| 97 |
rustflags.extend(["-C", "linker=%s-gcc" % host_gnu_type]) |
| 98 |
for f in ldflags: |
| 99 |
rustflags.extend(["-C", "link-arg=%s" % f]) |
| 100 |
rustflags.extend(["--remap-path-prefix", "%s=%s" % (registry_path, SYSTEM_REGISTRY)]) |
| 101 |
|
| 102 |
# TODO: we cannot enable this until dh_shlibdeps works correctly; atm we get: |
| 103 |
# dpkg-shlibdeps: warning: can't extract name and version from library name 'libstd-XXXXXXXX.so' |
| 104 |
# and the resulting cargo.deb does not depend on the correct version of libstd-rust-1.XX |
| 105 |
# We probably need to add override_dh_makeshlibs to d/rules of rustc |
| 106 |
#rustflags.extend(["-C", "prefer-dynamic"]) |
| 107 |
|
| 108 |
os.makedirs(cargo_home, exist_ok=True) |
| 109 |
with open("%s/config.toml" % cargo_home, "w") as fp: |
| 110 |
fp.write("""[source.crates-io] |
| 111 |
replace-with = "dh-cargo-registry" |
| 112 |
|
| 113 |
[source.dh-cargo-registry] |
| 114 |
directory = "{0}" |
| 115 |
|
| 116 |
[target.'cfg(all())'] |
| 117 |
rustflags = {1} |
| 118 |
""".format(registry_path, repr(rustflags))) |
| 119 |
|
| 120 |
return 0 |
| 121 |
|
| 122 |
def install(destdir, cratespec, host_rust_type, profile, crate_in_registry, install_prefix, *args): |
| 123 |
crate, version = cratespec.rsplit("_", 1) |
| 124 |
builddir = os.getenv("DEB_BUILDDIR", "target") |
| 125 |
log("installing into destdir '%s' prefix '%s'" % (destdir, install_prefix)) |
| 126 |
install_target = destdir + install_prefix |
| 127 |
path_args = [] if "--path" in args else ["--path", sourcepath()] |
| 128 |
logrun(["env", "RUST_BACKTRACE=1", |
| 129 |
# set CARGO_TARGET_DIR so build products are saved in target/ |
| 130 |
# normally `cargo install` deletes them when it exits |
| 131 |
"CARGO_TARGET_DIR=" + sourcepath(builddir), |
| 132 |
"/usr/bin/cargo"] + list(args) + |
| 133 |
([crate, "--vers", version] if crate_in_registry else path_args) + |
| 134 |
["--root", install_target], check=True) |
| 135 |
logrun(["rm", "-f", "%s/.crates.toml" % install_target]) |
| 136 |
logrun(["rm", "-f", "%s/.crates2.json" % install_target]) |
| 137 |
|
| 138 |
# if there was a custom build output, symlink it to debian/cargo_out_dir |
| 139 |
# hopefully cargo will provide a better solution in future https://github.com/rust-lang/cargo/issues/5457 |
| 140 |
r = logrun('''ls -td "%s/%s/%s/build/%s"-*/out 2>/dev/null | head -n1''' |
| 141 |
% (builddir, host_rust_type, profile, crate), shell=True, stdout=subprocess.PIPE).stdout |
| 142 |
r = r.decode("utf-8").rstrip() |
| 143 |
if r: |
| 144 |
logrun(["ln", "-sfT", "../%s" % r, "debian/cargo_out_dir"], check=True) |
| 145 |
return 0 |
| 146 |
|
| 147 |
def main(*args): |
| 148 |
cargo_home = os.getenv("CARGO_HOME", "") |
| 149 |
if not cargo_home.endswith("debian/cargo_home"): |
| 150 |
log(f"WARNING: falling back to simply calling upstream cargo, because CARGO_HOME does not end with debian/cargo_home: ", cargo_home) |
| 151 |
os.execv("/usr/bin/cargo", ["cargo"] + list(args)) |
| 152 |
|
| 153 |
if any(f not in os.environ for f in FLAGS.split()): |
| 154 |
raise ValueError("not all of %s set; did you call dpkg-buildflags?" % FLAGS) |
| 155 |
|
| 156 |
if any(f not in os.environ for f in ARCHES.split()): |
| 157 |
raise ValueError("not all of %s set; did you include architecture.mk?" % ARCHES) |
| 158 |
|
| 159 |
build_options = os.getenv("DEB_BUILD_OPTIONS", "").split() |
| 160 |
build_profiles = os.getenv("DEB_BUILD_PROFILES", "").split() |
| 161 |
|
| 162 |
parallel = [] |
| 163 |
lto = 0 |
| 164 |
for o in build_options: |
| 165 |
if o.startswith("parallel="): |
| 166 |
parallel = ["-j" + o[9:]] |
| 167 |
elif o.startswith("optimize="): |
| 168 |
opt_arg = o[9:] |
| 169 |
for arg in opt_arg.split(","): |
| 170 |
if opt_arg == "-lto": |
| 171 |
lto = -1 |
| 172 |
elif opt_arg == "+lto": |
| 173 |
lto = 1 |
| 174 |
else: |
| 175 |
log(f"WARNING: unhandled optimization flag: {opt_arg}") |
| 176 |
|
| 177 |
nodoc = "nodoc" in build_options or "nodoc" in build_profiles |
| 178 |
nocheck = "nocheck" in build_options or "nocheck" in build_profiles |
| 179 |
noopt = "noopt" in build_options |
| 180 |
verbose = [] |
| 181 |
if not "terse" in build_options: |
| 182 |
verbose = ["--verbose", "--verbose"] |
| 183 |
|
| 184 |
if noopt: |
| 185 |
profile = os.getenv("CARGO_PROFILE", "dev") |
| 186 |
else: |
| 187 |
profile = os.getenv("CARGO_PROFILE", "release") |
| 188 |
|
| 189 |
# ensure test-specific flags are applied regardless of profile in use |
| 190 |
test_flags = [ |
| 191 |
"--config", f"profile.{profile}.debug-assertions=true", |
| 192 |
"--config", f"profile.{profile}.overflow-checks=true", |
| 193 |
# TODO: maybe use -Zpanic-abort-tests when stabilized |
| 194 |
"--config", f"profile.{profile}.panic=\"unwind\"", |
| 195 |
] |
| 196 |
|
| 197 |
# note this is actually the "build target" type, see rustc's README.Debian |
| 198 |
# for full details of the messed-up terminology here |
| 199 |
host_rust_type = os.getenv("DEB_HOST_RUST_TYPE", "") |
| 200 |
host_gnu_type = os.getenv("DEB_HOST_GNU_TYPE", "") |
| 201 |
|
| 202 |
log("options, profiles, parallel, lto:", build_options, build_profiles, parallel, lto) |
| 203 |
log("rust_type, gnu_type:", ", ".join([host_rust_type, host_gnu_type])) |
| 204 |
|
| 205 |
if args[0] == "prepare-debian": |
| 206 |
registry = args[1] |
| 207 |
return prepare_debian(cargo_home, registry, |
| 208 |
host_gnu_type, |
| 209 |
os.getenv("LDFLAGS", "").split()) |
| 210 |
|
| 211 |
newargs = [] |
| 212 |
subcmd = None |
| 213 |
for a in args: |
| 214 |
if (subcmd is None) and (a in ("build", "rustc", "install")): |
| 215 |
subcmd = a |
| 216 |
newargs.extend(["-Zavoid-dev-deps", a] + ["--profile", profile] + verbose + |
| 217 |
parallel + ["--target", host_rust_type]) |
| 218 |
elif (subcmd is None) and (a in ("doc", "test", "bench")): |
| 219 |
subcmd = a |
| 220 |
newargs.extend(["-Zavoid-dev-deps", a] + ["--profile", profile] + verbose + |
| 221 |
parallel + ["--target", host_rust_type] + test_flags) |
| 222 |
elif (subcmd is None) and (a in ("check", "run")): |
| 223 |
subcmd = a |
| 224 |
newargs.extend([a] + ["--profile", profile] + verbose + ["--target", host_rust_type]) |
| 225 |
elif (subcmd is None) and (a == "clean"): |
| 226 |
subcmd = a |
| 227 |
newargs.extend([a] + verbose) |
| 228 |
else: |
| 229 |
newargs.append(a) |
| 230 |
|
| 231 |
if subcmd is not None and "--verbose" in newargs and "--quiet" in newargs: |
| 232 |
newargs.remove("--quiet") |
| 233 |
|
| 234 |
if nodoc and subcmd == "doc": |
| 235 |
return 0 |
| 236 |
if nocheck and subcmd in ("test", "bench"): |
| 237 |
return 0 |
| 238 |
|
| 239 |
if lto == 1: |
| 240 |
newargs.append("--config profile.release.lto = \"thin\"") |
| 241 |
elif lto == -1: |
| 242 |
newargs.append("--config profile.release.lto = false") |
| 243 |
|
| 244 |
if subcmd == "clean": |
| 245 |
logrun(["env", "RUST_BACKTRACE=1", "/usr/bin/cargo"] + list(newargs), check=True) |
| 246 |
if os.path.exists(cargo_home): |
| 247 |
shutil.rmtree(cargo_home) |
| 248 |
return 0 |
| 249 |
|
| 250 |
cargo_config = "%s/config.toml" % cargo_home |
| 251 |
if not os.path.exists(cargo_config): |
| 252 |
raise ValueError("does not exist: %s, did you run `cargo prepare-debian <registry>`?" % cargo_config) |
| 253 |
|
| 254 |
if subcmd == "install": |
| 255 |
return install(os.getenv("DESTDIR", ""), |
| 256 |
os.environ["DEB_CARGO_CRATE"], |
| 257 |
host_rust_type, profile, |
| 258 |
os.getenv("DEB_CARGO_CRATE_IN_REGISTRY", "") == "1", |
| 259 |
os.getenv("DEB_CARGO_INSTALL_PREFIX", "/usr"), |
| 260 |
*newargs) |
| 261 |
else: |
| 262 |
return logrun(["env", "RUST_BACKTRACE=1", "/usr/bin/cargo"] + list(newargs)).returncode |
| 263 |
|
| 264 |
if __name__ == "__main__": |
| 265 |
sys.exit(main(*sys.argv[1:])) |