Skip to content

Commit

Permalink
make: move -L to build.rs
Browse files Browse the repository at this point in the history
Per the comment for `RUSTC_FLAGS_FOR_BIN`:

> Ultimately, this should move to the Cargo.toml, for example when
> rust-lang/cargo#7811 is merged into Cargo.

this moves setting the linker script search path to build.rs, which is
enabled by the addition of the `cargo:rustc-link-arg=FLAG` instruction
for build scripts.

This removes the need to special case the -L flag so that it is only
used when creating a binary and not when compiling dependencies, since
that is exactly what the new build script instruction handles.
  • Loading branch information
bradjc committed Feb 9, 2022
1 parent db7cb5f commit 32adefa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 33 deletions.
40 changes: 7 additions & 33 deletions boards/Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,6 @@ ifeq ($(CI),true)
RUSTC_FLAGS_TOCK += -D warnings
endif

# The following flags should only be passed to the board's binary crate, but
# not to any of its dependencies (the kernel, capsules, chips, etc.). The
# dependencies wouldn't use it, but because the link path is different for each
# board, Cargo wouldn't be able to cache builds of the dependencies.
#
# Indeed, as far as Cargo is concerned, building the kernel with
# `-C link-arg=-L/tock/boards/imix` is different than building the kernel with
# `-C link-arg=-L/tock/boards/hail`, so Cargo would have to rebuild the kernel
# for each board instead of caching it per board (even if in reality the same
# kernel is built because the link-arg isn't used by the kernel).
#
# Ultimately, this should move to the Cargo.toml, for example when
# https://github.com/rust-lang/cargo/pull/7811 is merged into Cargo.
#
# The difference between `RUSTC_FLAGS_TOCK` and `RUSTC_FLAGS_FOR_BIN` is that
# the former is forwarded to all the dependencies (being passed to cargo via
# the `RUSTFLAGS` environment variable), whereas the latter is only applied to
# the final binary crate (being passed as parameter to `cargo rustc`).
RUSTC_FLAGS_FOR_BIN ?= \
-C link-arg=-L$(abspath .) \

# http://stackoverflow.com/questions/10858261/abort-makefile-if-variable-not-set
# Check that given variables are set and all have non-empty values, print an
# error otherwise.
Expand Down Expand Up @@ -326,27 +305,22 @@ lst: $(TARGET_PATH)/release/$(PLATFORM).lst
show-target:
$(info $(TARGET))

# This rule is a copy of the rule used to build the release target, but
# `cargo rustc` has been replaced with `cargo bloat`. `cargo bloat` replicates the
# interface of `cargo build`, rather than `cargo rustc`, so we need to move
# `RUSTC_FLAGS_FOR_BIN` into the `RUSTFLAGS` environment variable. This only means
# that cargo cannot reuse built dependencies built using `cargo bloat`.
# See the discussion on `RUSTC_FLAGS_FOR_BIN` above for additional details.
# To pass additional flags to `cargo bloat`, populate the
# CARGO_BLOAT_FLAGS environment variable, e.g. call
# This rule is a copy of the rule used to build the release target, but `cargo
# rustc` has been replaced with `cargo bloat`. To pass additional flags to
# `cargo bloat`, populate the CARGO_BLOAT_FLAGS environment variable, e.g. call
# `CARGO_BLOAT_FLAGS=--crates make cargobloat`
.PHONY: cargobloat
cargobloat:
$(Q) $(CARGO) install cargo-bloat > /dev/null 2>&1 || echo 'Error: Failed to install cargo-bloat'
$(Q)RUSTFLAGS="$(RUSTC_FLAGS_TOCK) $(RUSTC_FLAGS_FOR_BIN)" CARGO_FLAGS="-Z build-std=core" $(CARGO) bloat $(CARGO_FLAGS_TOCK_NO_BUILD_STD) --bin $(PLATFORM) --release $(CARGO_BLOAT_FLAGS)
$(Q)RUSTFLAGS="$(RUSTC_FLAGS_TOCK)" CARGO_FLAGS="-Z build-std=core" $(CARGO) bloat $(CARGO_FLAGS_TOCK_NO_BUILD_STD) --bin $(PLATFORM) --release $(CARGO_BLOAT_FLAGS)

# To pass additional flags to `cargo bloat`, populate the
# CARGO_BLOAT_FLAGS environment variable, e.g. call
# `CARGO_BLOAT_FLAGS=--crates make cargobloatnoinline`
.PHONY: cargobloatnoinline
cargobloatnoinline:
$(Q) $(CARGO) install cargo-bloat > \dev\null 2>&1 || echo 'Error: Failed to install cargo-bloat'
$(Q)RUSTFLAGS="$(RUSTC_FLAGS_TOCK) -C inline-threshold=0 $(RUSTC_FLAGS_FOR_BIN)" $(CARGO) bloat $(CARGO_FLAGS_TOCK) --bin $(PLATFORM) --release $(CARGO_BLOAT_FLAGS)
$(Q)RUSTFLAGS="$(RUSTC_FLAGS_TOCK) -C inline-threshold=0" $(CARGO) bloat $(CARGO_FLAGS_TOCK) --bin $(PLATFORM) --release $(CARGO_BLOAT_FLAGS)

.PHONY: stack-analysis
#stack_analysis: RUSTC_FLAGS_TOCK += -Z emit-stack-sizes
Expand Down Expand Up @@ -389,10 +363,10 @@ $(TOCK_ROOT_DIRECTORY)tools/sha256sum/target/debug/sha256sum:

.PHONY: $(TARGET_PATH)/release/$(PLATFORM)
$(TARGET_PATH)/release/$(PLATFORM):
$(Q)RUSTFLAGS="$(RUSTC_FLAGS_TOCK)" $(CARGO) rustc $(CARGO_FLAGS_TOCK) --bin $(PLATFORM) --release -- $(RUSTC_FLAGS_FOR_BIN)
$(Q)RUSTFLAGS="$(RUSTC_FLAGS_TOCK)" $(CARGO) rustc $(CARGO_FLAGS_TOCK) --bin $(PLATFORM) --release
$(Q)$(SIZE) $(SIZE_FLAGS) $@

.PHONY: $(TARGET_PATH)/debug/$(PLATFORM)
$(TARGET_PATH)/debug/$(PLATFORM):
$(Q)RUSTFLAGS="$(RUSTC_FLAGS_TOCK)" $(CARGO) rustc $(CARGO_FLAGS_TOCK) --bin $(PLATFORM) -- $(RUSTC_FLAGS_FOR_BIN)
$(Q)RUSTFLAGS="$(RUSTC_FLAGS_TOCK)" $(CARGO) rustc $(CARGO_FLAGS_TOCK) --bin $(PLATFORM)
$(Q)$(SIZE) $(SIZE_FLAGS) $@
4 changes: 4 additions & 0 deletions boards/hail/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ fn main() {
println!("cargo:rerun-if-changed=layout.ld");
println!("cargo:rerun-if-changed=chip_layout.ld");
println!("cargo:rerun-if-changed=../kernel_layout.ld");
println!(
"cargo:rustc-link-arg-bins=-L{}",
std::env::var("CARGO_MANIFEST_DIR").unwrap_or(".".to_string())
);
}
4 changes: 4 additions & 0 deletions boards/imix/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
fn main() {
println!("cargo:rerun-if-changed=layout.ld");
println!("cargo:rerun-if-changed=../kernel_layout.ld");
println!(
"cargo:rustc-link-arg-bins=-L{}",
std::env::var("CARGO_MANIFEST_DIR").unwrap_or(".".to_string())
);
}

0 comments on commit 32adefa

Please sign in to comment.