diff options
Diffstat (limited to 'pkgs/build-support/rust/build-rust-crate/configure-crate.nix')
-rw-r--r-- | pkgs/build-support/rust/build-rust-crate/configure-crate.nix | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/pkgs/build-support/rust/build-rust-crate/configure-crate.nix b/pkgs/build-support/rust/build-rust-crate/configure-crate.nix index ed810053709..a95b356646e 100644 --- a/pkgs/build-support/rust/build-rust-crate/configure-crate.nix +++ b/pkgs/build-support/rust/build-rust-crate/configure-crate.nix @@ -1,5 +1,6 @@ -{ lib, stdenv, echo_colored, noisily, mkRustcDepArgs }: -{ build +{ lib, stdenv, echo_colored, noisily, mkRustcDepArgs, mkRustcFeatureArgs }: +{ + build , buildDependencies , colors , completeBuildDeps @@ -30,6 +31,9 @@ let version_ = lib.splitString "-" crateVersion; optLevel = if release then 3 else 0; completeDepsDir = lib.concatStringsSep " " completeDeps; completeBuildDepsDir = lib.concatStringsSep " " completeBuildDeps; + envFeatures = lib.concatStringsSep " " ( + map (f: lib.replaceChars ["-"] ["_"] (lib.toUpper f)) crateFeatures + ); in '' ${echo_colored colors} ${noisily colors verbose} @@ -39,7 +43,7 @@ in '' noisily cd "${workspace_member}" ''} ${lib.optionalString (workspace_member == null) '' - echo_colored "Searching for matching Cargo.toml (${crateName})" + echo_colored "Searching for matching Cargo.toml (${crateName})" local cargo_toml_dir=$(matching_cargo_toml_dir "${crateName}") if [ -z "$cargo_toml_dir" ]; then echo_error "ERROR configuring ${crateName}: No matching Cargo.toml in $(pwd) found." >&2 @@ -49,7 +53,7 @@ in '' ''} runHook preConfigure - + symlink_dependency() { # $1 is the nix-store path of a dependency # $2 is the target path @@ -161,14 +165,24 @@ in '' EXTRA_BUILD_FLAGS="$EXTRA_BUILD_FLAGS $(tr '\n' ' ' < target/link.build)" fi noisily rustc --crate-name build_script_build $BUILD --crate-type bin ${rustcOpts} \ - ${crateFeatures} --out-dir target/build/${crateName} --emit=dep-info,link \ + ${mkRustcFeatureArgs crateFeatures} --out-dir target/build/${crateName} --emit=dep-info,link \ -L dependency=target/buildDeps ${buildDeps} --cap-lints allow $EXTRA_BUILD_FLAGS --color ${colors} mkdir -p target/build/${crateName}.out export RUST_BACKTRACE=1 BUILD_OUT_DIR="-L $OUT_DIR" mkdir -p $OUT_DIR - target/build/${crateName}/build_script_build > target/build/${crateName}.opt + + ( + # Features should be set as environment variable for build scripts: + # https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts + for feature in ${envFeatures}; do + export CARGO_FEATURE_$feature=1 + done + + target/build/${crateName}/build_script_build > target/build/${crateName}.opt + ) + set +e EXTRA_BUILD=$(sed -n "s/^cargo:rustc-flags=\(.*\)/\1/p" target/build/${crateName}.opt | tr '\n' ' ' | sort -u) EXTRA_FEATURES=$(sed -n "s/^cargo:rustc-cfg=\(.*\)/--cfg \1/p" target/build/${crateName}.opt | tr '\n' ' ') @@ -179,9 +193,9 @@ in '' export $env done - CRATENAME=$(echo ${crateName} | sed -e "s/\(.*\)-sys$/\U\1/") + CRATENAME=$(echo ${crateName} | sed -e "s/\(.*\)-sys$/\U\1/" -e "s/-/_/g") grep -P "^cargo:(?!(rustc-|warning=|rerun-if-changed=|rerun-if-env-changed))" target/build/${crateName}.opt \ - | sed -e "s/cargo:\([^=]*\)=\(.*\)/export DEP_$(echo $CRATENAME)_\U\1\E=\2/" > target/env + | awk -F= "/^cargo:/ { sub(/^cargo:/, \"\", \$1); gsub(/-/, \"_\", \$1); print \"export \" toupper(\"DEP_$(echo $CRATENAME)_\" \$1) \"=\" \$2 }" > target/env set -e fi runHook postConfigure |