Skip to content

Commit 8e18b43

Browse files
committed
feat: add rustc buildenv for rust self-packaging
1 parent 34d4df3 commit 8e18b43

File tree

5 files changed

+81
-80
lines changed

5 files changed

+81
-80
lines changed

Cargo.lock

Lines changed: 13 additions & 79 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

debian/changelog

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
debmagic (0.1.0) UNRELEASED; urgency=medium
1+
debmagic (0.0.1-alpha1) UNRELEASED; urgency=medium
22

33
* WIP: Initial release.
44

debian/rules

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env python3
2+
import shutil
3+
import os
24

35
import sys
46
from pathlib import Path
@@ -19,12 +21,23 @@ pkg = package(
1921
preset=[dhp],
2022
)
2123

24+
os.environ.update({
25+
"PATH": f"/usr/share/cargo/bin:{os.environ['PATH']}",
26+
"CARGO": "/usr/share/cargo/bin/cargo",
27+
"CARGO_HOME": f"{Path.cwd()}/debian/cargo_home",
28+
"CARGO_REGISTRY": f"{Path.cwd()}/debian/cargo_registry",
29+
"DEB_CARGO_CRATE": f"{pkg.build_env.DEB_SOURCE}_{pkg.build_env.DEB_VERSION_UPSTREAM}"
30+
})
31+
2232
packages = {
2333
"python3-debmagic-common": ("packages/debmagic-common", "debmagic-common"),
2434
"debmagic": ("packages/debmagic", "debmagic"),
2535
"debmagic-pkg": ("packages/debmagic-pkg", "debmagic-pkg"),
2636
}
2737

38+
debmagic_cargo_lock = Path("packages/debmagic/Cargo.lock")
39+
debmagic_cargo_lock_saved = Path("packages/debmagic/Cargo.lock.saved")
40+
2841
def dh_auto(build: Build, stage: str, use_destdir: bool = False):
2942
for pkg_name, (path, python_pkg_name) in packages.items():
3043
destdir = f" --destdir debian/{pkg_name} " if use_destdir else ""
@@ -38,6 +51,8 @@ def dh_auto_configure(build: Build):
3851

3952
@dhp.override
4053
def dh_auto_build(build: Build):
54+
if debmagic_cargo_lock.is_file():
55+
shutil.move(debmagic_cargo_lock, debmagic_cargo_lock_saved)
4156
dh_auto(build, "dh_auto_build")
4257

4358

@@ -53,6 +68,8 @@ def dh_auto_test(build: Build):
5368

5469
@dhp.override
5570
def dh_auto_clean(build: Build):
71+
if debmagic_cargo_lock_saved.is_file():
72+
shutil.move(debmagic_cargo_lock_saved, debmagic_cargo_lock)
5673
dh_auto(build, "dh_auto_clean")
5774

5875

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Implement the build environment logic provided by /usr/share/rustc/architecture.mk
3+
"""
4+
5+
6+
def _rust_cpu(cpu: str, arch: str) -> str:
7+
# $(subst i586,i686,...)
8+
cpu = cpu.replace("i586", "i686")
9+
10+
if "-riscv64-" in f"-{arch}-":
11+
return cpu.replace("riscv64", "riscv64gc")
12+
elif "-armhf-" in f"-{arch}-":
13+
return cpu.replace("arm", "armv7")
14+
elif "-armel-" in f"-{arch}-":
15+
return cpu.replace("arm", "armv5te")
16+
17+
return cpu
18+
19+
20+
def _rust_os(system: str, arch_os: str) -> str:
21+
if "-hurd-" in f"-{arch_os}-":
22+
return system.replace("gnu", "hurd-gnu")
23+
return system
24+
25+
26+
def _get_rust_type(prefix: str, env_vars: dict[str, str]) -> str:
27+
cpu = env_vars.get(f"{prefix}_GNU_CPU", "")
28+
arch = env_vars.get(f"{prefix}_ARCH", "")
29+
system = env_vars.get(f"{prefix}_GNU_SYSTEM", "")
30+
arch_os = env_vars.get(f"{prefix}_ARCH_OS", "")
31+
32+
r_cpu = _rust_cpu(cpu, arch)
33+
r_os = _rust_os(system, arch_os)
34+
35+
return f"{r_cpu}-unknown-{r_os}"
36+
37+
38+
def build_rustc_build_env(env: dict[str, str]):
39+
for machine in ["BUILD", "HOST", "TARGET"]:
40+
var_name = f"DEB_{machine}_RUST_TYPE"
41+
env[var_name] = _get_rust_type(f"DEB_{machine}", env)
42+
43+
# Fallback for older dpkg versions (ifeq check)
44+
if env["DEB_TARGET_RUST_TYPE"] == "-unknown-":
45+
env["DEB_TARGET_RUST_TYPE"] = env["DEB_HOST_RUST_TYPE"]

packages/debmagic-pkg/src/debmagic/v0/_dpkg/build_env.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from debmagic.common.models.package_version import PackageVersion
77
from debmagic.common.utils import run_cmd
88

9+
from ._rustc_build_env import build_rustc_build_env
10+
911

1012
def _cmd(cmd: str, input_data: str | None = None, env: dict[str, str] | None = None, cwd: Path | None = None) -> str:
1113
return run_cmd(cmd, check=True, env=env, input=input_data, text=True, capture_output=True).stdout.strip()
@@ -75,4 +77,7 @@ def get_pkg_env(package_dir: Path, maint_options: str | None = None) -> tuple[di
7577

7678
result["ELF_PACKAGE_METADATA"] = json.dumps(elf_meta, separators=(",", ":"))
7779

80+
# rustc/architecture.mk
81+
build_rustc_build_env(result)
82+
7883
return result, version

0 commit comments

Comments
 (0)