豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 40464d9

Browse files
feat: buildscript to embed targetted platform
1 parent 87d03c7 commit 40464d9

File tree

7 files changed

+111
-27
lines changed

7 files changed

+111
-27
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ categories = ["starter", "seed"]
1010
keywords = ["integrity", "seed", "starter", "docker", "release"]
1111
repository = "https://github.com/kristof-mattei/rust-seed"
1212
include = ["src/**", "/LICENSE", "/LICENSE-*"]
13+
build = "src/build.rs"
1314

1415
[dependencies]
1516
color-eyre = "0.6.5"

Dockerfile

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ RUN cargo init --name ${APPLICATION_NAME}
4747

4848
COPY ./.cargo ./Cargo.toml ./Cargo.lock ./
4949

50+
RUN echo "fn main() {}" > ./src/build.rs
51+
5052
# We use `fetch` to pre-download the files to the cache
5153
# Notice we do this in the target arch specific branch
5254
# We do this because we want to do it after `setup-env.sh`,
@@ -63,18 +65,21 @@ RUN --mount=type=cache,target=/build/target/${TARGET},sharing=locked \
6365
--mount=type=cache,id=cargo-git,target=/usr/local/cargo/git/db \
6466
--mount=type=cache,id=cargo-registry-index,target=/usr/local/cargo/registry/index \
6567
--mount=type=cache,id=cargo-registry-cache,target=/usr/local/cargo/registry/cache \
66-
/build-scripts/build.sh build --release --target ${TARGET} --target-dir ./target/${TARGET}
68+
/build-scripts/build.sh build --release --target-dir ./target/${TARGET}
6769

6870
# Rust full build
6971
FROM rust-cargo-build AS rust-build
7072

73+
# to expose into `build.sh`
74+
ARG TARGETVARIANT
75+
7176
WORKDIR /build
7277

7378
# now we copy in the source which is more prone to changes and build it
7479
COPY ./src ./src
7580

76-
# ensure cargo picks up on the change
77-
RUN touch ./src/main.rs
81+
# ensure cargo picks up on the fact that we copied in our code
82+
RUN touch ./src/main.rs ./src/build.rs
7883

7984
ENV PATH="/output/bin:$PATH"
8085

@@ -83,7 +88,7 @@ RUN --mount=type=cache,target=/build/target/${TARGET},sharing=locked \
8388
--mount=type=cache,id=cargo-git,target=/usr/local/cargo/git/db \
8489
--mount=type=cache,id=cargo-registry-index,target=/usr/local/cargo/registry/index \
8590
--mount=type=cache,id=cargo-registry-cache,target=/usr/local/cargo/registry/cache \
86-
/build-scripts/build.sh install --path . --locked --target ${TARGET} --target-dir ./target/${TARGET} --root /output
91+
/build-scripts/build.sh install --path . --locked --target-dir ./target/${TARGET} --root /output
8792

8893
# Container user setup
8994
FROM --platform=${BUILDPLATFORM} alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 AS passwd-build
@@ -99,8 +104,6 @@ RUN cat /etc/passwd | grep appuser > /tmp/passwd_appuser
99104
FROM scratch
100105

101106
ARG APPLICATION_NAME
102-
ARG TARGETARCH
103-
ARG TARGETVARIANT
104107

105108
COPY --from=passwd-build /tmp/group_appuser /etc/group
106109
COPY --from=passwd-build /tmp/passwd_appuser /etc/passwd
@@ -110,8 +113,6 @@ COPY --from=rust-build /output/bin/${APPLICATION_NAME} /app/entrypoint
110113
USER appuser
111114

112115
ENV RUST_BACKTRACE=full
113-
ENV TARGETARCH=${TARGETARCH}
114-
ENV TARGETVARIANT=${TARGETVARIANT}
115116

116117
WORKDIR /app
117118

build-all.sh

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ set -e
44
build() {
55
APPLICATION_NAME=$1
66
PLATFORM=$2
7-
docker build \
7+
docker buildx \
8+
build \
89
--file Dockerfile . \
9-
--tag $APPLICATION_NAME:latest-${2//\//-} \
10+
--tag $APPLICATION_NAME:latest \
1011
--build-arg APPLICATION_NAME=$APPLICATION_NAME \
1112
--platform $PLATFORM \
1213
--progress=plain
1314
}
1415

15-
name=$(basename ${PWD})
16-
17-
build $name linux/amd64/v3
18-
build $name linux/amd64/v2
19-
build $name linux/amd64
20-
build $name linux/arm64
16+
build $(basename ${PWD}) linux/amd64/v3,linux/amd64/v2,linux/amd64,linux/arm64

build-scripts/build.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ case $TARGET in
2525
;;
2626
esac
2727

28-
rust_flags="-Clink-self-contained=yes -Clinker=rust-lld ${target_cpu}"
28+
rustflags="-Clink-self-contained=yes -Clinker=rust-lld ${target_cpu}"
2929

3030
# replace - with _ in the Rust target
3131
target_lower=${TARGET//-/_}
@@ -41,4 +41,4 @@ declare -x "${cxx_var}=${cpp_compiler}"
4141
cargo_target_linker_var=CARGO_TARGET_${target_upper}_LINKER
4242
declare -x "${cargo_target_linker_var}=${c_compiler}"
4343

44-
RUSTFLAGS=$rust_flags cargo $@
44+
RUSTFLAGS=$rustflags cargo $@ --target ${TARGET}

src/build.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use std::env;
2+
use std::ffi::OsStr;
3+
4+
fn main() {
5+
export_var(
6+
"COMPILE_TIME_HOST",
7+
&env::var("HOST").expect("HOST is set for build scripts"),
8+
);
9+
export_var(
10+
"COMPILE_TIME_TARGET",
11+
&env::var("TARGET").expect("TARGET is set for build scripts"),
12+
);
13+
export_var(
14+
"COMPILE_TIME_TARGET_CPU",
15+
&extract_target_cpu_from_rustflags(
16+
&env::var_os("CARGO_ENCODED_RUSTFLAGS").unwrap_or_default(),
17+
)
18+
.unwrap_or_default(),
19+
);
20+
21+
println!("cargo:rerun-if-changed-env=HOST");
22+
println!("cargo:rerun-if-changed-env=TARGET");
23+
println!("cargo:rerun-if-changed-env=CARGO_ENCODED_RUSTFLAGS");
24+
}
25+
26+
fn extract_target_cpu_from_rustflags(rustflags: &OsStr) -> Option<String> {
27+
const ASCII_SEPARATOR: char = '\x1f';
28+
29+
let rustflags = rustflags.to_string_lossy();
30+
31+
let mut cpu = None;
32+
33+
for flag in rustflags.split(ASCII_SEPARATOR) {
34+
let stripped_c = flag.strip_prefix("-C").unwrap_or(flag);
35+
36+
if let Some(target_cpu) = stripped_c.strip_prefix("target-cpu=") {
37+
cpu = Some(target_cpu);
38+
}
39+
}
40+
41+
cpu.map(str::to_owned)
42+
}
43+
44+
fn export_var(name: &str, value: &str) {
45+
println!("cargo:rustc-env={}={}", name, value);
46+
}

src/build_env.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
pub const COMPILE_TIME_HOST: &str = env!("COMPILE_TIME_HOST");
2+
pub const COMPILE_TIME_TARGET: &str = env!("COMPILE_TIME_TARGET");
3+
pub const COMPILE_TIME_TARGET_CPU: Option<&str> = const {
4+
let cttc = env!("COMPILE_TIME_TARGET_CPU");
5+
6+
if cttc.is_empty() { None } else { Some(cttc) }
7+
};
8+
9+
pub struct BuildEnv {
10+
host: &'static str,
11+
target: &'static str,
12+
target_cpu: Option<&'static str>,
13+
}
14+
15+
pub fn get_build_env() -> BuildEnv {
16+
BuildEnv {
17+
host: COMPILE_TIME_HOST,
18+
target: COMPILE_TIME_TARGET,
19+
target_cpu: COMPILE_TIME_TARGET_CPU,
20+
}
21+
}
22+
23+
impl std::fmt::Display for BuildEnv {
24+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25+
write!(f, "Built on `{}`, targeting `{}`", self.host, self.target)?;
26+
27+
if let Some(target_cpu) = self.target_cpu {
28+
write!(f, " (target-cpu `{}`)", target_cpu)?;
29+
}
30+
31+
Ok(())
32+
}
33+
}

src/main.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
use color_eyre::config::HookBuilder;
2+
use color_eyre::eyre;
3+
4+
use crate::build_env::get_build_env;
5+
mod build_env;
6+
17
#[global_allocator]
28
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
39

@@ -13,21 +19,22 @@ fn quz() -> &'static str {
1319
"Quz"
1420
}
1521

16-
#[expect(clippy::todo, reason = "Seed code")]
17-
fn main() -> Result<(), color_eyre::Report> {
18-
color_eyre::install()?;
22+
fn i_will_error() -> Result<(), eyre::Report> {
23+
Err(eyre::Report::msg("I promised you, I'd error!"))
24+
}
25+
26+
fn main() -> Result<(), eyre::Report> {
27+
HookBuilder::default()
28+
.capture_span_trace_by_default(true)
29+
.install()?;
1930

2031
println!("{}", foo());
2132
println!("{}", bar());
2233
println!("{}", quz());
2334

24-
println!(
25-
"BUILT FOR {} {}",
26-
std::env::var("TARGETARCH").unwrap(),
27-
std::env::var("TARGETVARIANT").unwrap()
28-
);
35+
println!("Build setup: {}", get_build_env());
2936

30-
todo!("TODO");
37+
i_will_error()
3138
}
3239

3340
#[cfg(test)]

0 commit comments

Comments
 (0)