From c7da80a05dd6cc41b8c46f9b2e8f48f910299143 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 16 Dec 2020 17:11:05 +0000 Subject: [PATCH] chore(ci): use rustflags instead of rustc wrappers (#5559) * ci: use rustflags instead of rustc wrappers Signed-off-by: Jorge Aparicio * remove the wrapper scripts Signed-off-by: Jorge Aparicio --- .cargo/config | 20 ++++---- .../aarch64-unknown-linux-musl.dockerfile | 2 - .../armv7-unknown-linux-musleabihf.dockerfile | 2 - scripts/cross/wrappers/armv7_rustc.sh | 45 ----------------- scripts/cross/wrappers/linker.sh | 48 ------------------- scripts/cross/wrappers/musl_rustc.sh | 45 ----------------- .../x86_64-unknown-linux-musl.dockerfile | 2 - 7 files changed, 10 insertions(+), 154 deletions(-) delete mode 100755 scripts/cross/wrappers/armv7_rustc.sh delete mode 100755 scripts/cross/wrappers/linker.sh delete mode 100755 scripts/cross/wrappers/musl_rustc.sh diff --git a/.cargo/config b/.cargo/config index 01d2b59f10953..73f7b9a505b09 100644 --- a/.cargo/config +++ b/.cargo/config @@ -4,19 +4,19 @@ rustflags = ["-C", "link-args=-rdynamic"] [target.aarch64-unknown-linux-gnu] rustflags = ["-C", "link-args=-rdynamic"] + [target.'cfg(target_env = "musl")'] -# Because the `cross` Docker container sets `CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER`, the -# `linker` key does not work in this file, so we use `rustflags` instead. -# NOTE: This means that any custom `RUSTFLAGS` variable must include this value! -rustflags = "-Clinker=scripts/cross/wrappers/linker.sh" +# Turn off self-contained linking mode. This makes rustc rely on the installed musl toolchain to +# find and link against C and C++ runtime objects, instead of linking against the bundled C ones. +rustflags = "-Clink-self-contained=no" -# x86_64-specific libstdc++ search path. When building for musl, Cargo will merge this with the -# `rustflags` key above. +# for these musl targets libstdc++ will be linked statically (that's configured in e.g. leveldb-sys) +# we provide the location of that library here [target.x86_64-unknown-linux-musl] rustflags = "-Lnative=/usr/local/x86_64-linux-musl/lib" +[target.i686-unknown-linux-musl] +rustflags = "-Lnative=/usr/local/i686-linux-musl/lib" + [target.aarch64-unknown-linux-musl] -# Deliberately empty. -# See `scripts/cross/wrappers/{linker,rustc}.sh`. -# Basically, we need to carefully edit the ordering of links. -# Rust doesn't let us have great control of that here, so for now we use those wrappers. +rustflags = "-Lnative=/usr/local/aarch64-linux-musl/lib" diff --git a/scripts/cross/aarch64-unknown-linux-musl.dockerfile b/scripts/cross/aarch64-unknown-linux-musl.dockerfile index 2e9a433c475f8..3401d3883da9c 100644 --- a/scripts/cross/aarch64-unknown-linux-musl.dockerfile +++ b/scripts/cross/aarch64-unknown-linux-musl.dockerfile @@ -1,3 +1 @@ FROM rustembedded/cross:aarch64-unknown-linux-musl - -ENV RUSTC="scripts/cross/wrappers/musl_rustc.sh" diff --git a/scripts/cross/armv7-unknown-linux-musleabihf.dockerfile b/scripts/cross/armv7-unknown-linux-musleabihf.dockerfile index bd24216f3e6c4..3a78d417b71e9 100644 --- a/scripts/cross/armv7-unknown-linux-musleabihf.dockerfile +++ b/scripts/cross/armv7-unknown-linux-musleabihf.dockerfile @@ -1,3 +1 @@ FROM rustembedded/cross:armv7-unknown-linux-musleabihf - -ENV RUSTC="scripts/cross/wrappers/armv7_rustc.sh" diff --git a/scripts/cross/wrappers/armv7_rustc.sh b/scripts/cross/wrappers/armv7_rustc.sh deleted file mode 100755 index 2518ae0c03146..0000000000000 --- a/scripts/cross/wrappers/armv7_rustc.sh +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env bash - -# This is a wrapper script around `rustc`, which passes custom arguments to rustc. -# -# Normally, custom rustc arguments would be configured by setting the `rustflags` key in -# `.cargo/config.toml`, but that key is overridden by the `RUSTFLAGS` env var, which is set *inside* -# the `cross` Docker container for AArch64, so we unfortunately need this custom rustc wrapper. -# -# To make matters worse, it is not possible to override `rustc` only for a specific target, so we -# have to make sure that we *only* pass the custom arguments when the compilation target is AArch64. -# -# Ideally, `rustflags` from `.cargo/config` and the `RUSTFLAGS` env var would be merged, which is -# tracked in this Cargo issue: https://github.com/rust-lang/cargo/issues/5376 - -set -e - -SELF=$(dirname "$0") - -LIBSTDCXX_PATH="/usr/local/arm-linux-musleabihf/lib" -LINKER="${SELF}/linker.sh" - -# We don't get passed the target in any env var, so we'd have to parse cli args and look for the -# `--target` flag :( -TARGET="" -ARGS=() -for ARG in "$@"; do - # FIXME: --target=bla is valid too, but we don't handle that - - if [[ "${TARGET}" == "next" ]]; then - TARGET="${ARG}" - fi - - if [[ "${ARG}" == "--target" ]]; then - TARGET="next" - fi - - ARGS+=("${ARG}") -done - -if [[ "${TARGET}" == "armv7-unknown-linux-musleabihf" ]]; then - # Pass `-Clinker` last to override the previous `-Clinker`. - rustc "-Lnative=${LIBSTDCXX_PATH}" "$@" "-Clinker=${LINKER}" -else - rustc "$@" -fi diff --git a/scripts/cross/wrappers/linker.sh b/scripts/cross/wrappers/linker.sh deleted file mode 100755 index b6048bd805018..0000000000000 --- a/scripts/cross/wrappers/linker.sh +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env bash - -# This is a wrapper script around the system linker, which injects libgcc startup objects to allow -# statically linking Rust apps against C++ libraries while targeting musl. -# -# It should work as-is when using `cross` to cross-compile to musl. If not, it can be reconfigured -# via the environment variables shown below. Unfortunately it has to rely on a lot of hard-coded -# paths and arguments in order to do its job, so it may stop working when `cross` updates its Docker -# environment, or `rustc` changes the arguments it passes to the linker. -# -# Also see this link for compiler docs on which crt objects are used when: -# https://doc.rust-lang.org/nightly/nightly-rustc/rustc_target/spec/crt_objects/index.html -# -# Note that the name of this wrapper is significant: rustc automatically detects the "linker flavor" -# based on the linker executable name, and if it ends with `-ld` (which used to be the case), it -# will expect to invoke GNU LD directly. We want to use GCC instead, because it knows the search -# paths for locating the C++ runtime libraries. GCC is the default "flavor" on musl, so we just have -# to use a "neutral" wrapper name. - -set -o errexit - -# Object to inject after the predefined crt start objects. -INJECT_BEGIN=${RUST_MUSL_INJECT_BEGIN:-crtbeginS.o} - -# Object to inject before the predefined crt end objects. -INJECT_END=${RUST_MUSL_INJECT_BEGIN:-crtendS.o} - -# NB: We link the -S version of the objects because Rust produces position-independent executables. -# The non-S version fails to link in that case. - -# The linker to forward to. Must accept GCC-style arguments (so must not be LD directly). -LINKER='' -if [ -x "$(command -v x86_64-linux-musl-gcc)" ]; then - LINKER=x86_64-linux-musl-gcc -elif [ -x "$(command -v i686-linux-musl-gcc)" ]; then - LINKER=i686-linux-musl-gcc -elif [ -x "$(command -v aarch64-linux-musl-gcc)" ]; then - LINKER=aarch64-linux-musl-gcc -elif [ -x "$(command -v arm-linux-musleabihf-gcc)" ]; then - LINKER=arm-linux-musleabihf-gcc -else - LINKER=${RUST_MUSL_LINKER} -fi - -args=("-l:$INJECT_BEGIN" "$@" "-l:$INJECT_END") - -echo invoking real linker: "${LINKER}" "${args[@]}" >&2 -"${LINKER}" "${args[@]}" diff --git a/scripts/cross/wrappers/musl_rustc.sh b/scripts/cross/wrappers/musl_rustc.sh deleted file mode 100755 index 5fb992810ae3e..0000000000000 --- a/scripts/cross/wrappers/musl_rustc.sh +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env bash - -# This is a wrapper script around `rustc`, which passes custom arguments to rustc. -# -# Normally, custom rustc arguments would be configured by setting the `rustflags` key in -# `.cargo/config.toml`, but that key is overridden by the `RUSTFLAGS` env var, which is set *inside* -# the `cross` Docker container for AArch64, so we unfortunately need this custom rustc wrapper. -# -# To make matters worse, it is not possible to override `rustc` only for a specific target, so we -# have to make sure that we *only* pass the custom arguments when the compilation target is AArch64. -# -# Ideally, `rustflags` from `.cargo/config` and the `RUSTFLAGS` env var would be merged, which is -# tracked in this Cargo issue: https://github.com/rust-lang/cargo/issues/5376 - -set -e - -SELF=$(dirname "$0") - -LIBSTDCXX_PATH="/usr/local/aarch64-linux-musl/lib" -LINKER="${SELF}/linker.sh" - -# We don't get passed the target in any env var, so we'd have to parse cli args and look for the -# `--target` flag :( -TARGET="" -ARGS=() -for ARG in "$@"; do - # FIXME: --target=bla is valid too, but we don't handle that - - if [[ "${TARGET}" == "next" ]]; then - TARGET="${ARG}" - fi - - if [[ "${ARG}" == "--target" ]]; then - TARGET="next" - fi - - ARGS+=("${ARG}") -done - -if [[ "${TARGET}" == "aarch64-unknown-linux-musl" ]]; then - # Pass `-Clinker` last to override the previous `-Clinker`. - rustc "-Lnative=${LIBSTDCXX_PATH}" "$@" "-Clinker=${LINKER}" -else - rustc "$@" -fi diff --git a/scripts/cross/x86_64-unknown-linux-musl.dockerfile b/scripts/cross/x86_64-unknown-linux-musl.dockerfile index 8a20062b1fc3c..84f48313fcf41 100644 --- a/scripts/cross/x86_64-unknown-linux-musl.dockerfile +++ b/scripts/cross/x86_64-unknown-linux-musl.dockerfile @@ -1,3 +1 @@ FROM rustembedded/cross:x86_64-unknown-linux-musl - -ENV RUSTC="scripts/cross/wrappers/musl_rustc.sh"