Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: NixOS/nixpkgs
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2ff742e970de
Choose a base ref
...
head repository: NixOS/nixpkgs
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 9f03cb856264
Choose a head ref
  • 10 commits
  • 6 files changed
  • 1 contributor

Commits on Dec 11, 2019

  1. buildRustCrate: builtins -> lib where possible

    We can just use `lib` instead of `builtins` in all cases but the
    `hashString` case. Also changed a few lines to make use of some optional
    helpers from lib.
    andir committed Dec 11, 2019
    Copy the full SHA
    0aac0e8 View commit details
  2. buildRustCrate: move the color loggign & remove some runtime checks

    The expression is already long and confusing enough without the color
    stuff sprinkled in. Moving it to a dedicated file makes sense.
    
    I switched a bit of the color support code to pure Nix since there
    wasn't much point in doing that in bash while we can just do it in Nix.
    andir committed Dec 11, 2019
    Copy the full SHA
    50b2ef2 View commit details
  3. buildRustCrate: use tr instead of sed (it reads a bit nicer)

    I already have a few changes in here that will trigger rebuilds so I
    might as well do that substitution now.
    andir committed Dec 11, 2019
    Copy the full SHA
    db55d1f View commit details
  4. buildRustCrate: document and cleanup the symbol seeding

    That code had been in the derivation for a while but no explanation was
    given why that is needed. It might be helpful to our future selfs to
    document why things are done the way they are.
    andir committed Dec 11, 2019
    Copy the full SHA
    f4aeabd View commit details
  5. buildRustCrate: rename makeDeps function to mkRustcDepArgs

    This should carry the function better then `makeDeps` as it isn't
    producing deps but the rustc arguments required to link against those.
    andir committed Dec 11, 2019
    Copy the full SHA
    d37f001 View commit details
  6. buildRustCrate: reflow the way extraRustcOpts is constructed

    This should make it more obvious that we have three parts to it and not
    just one long gibberish string that makes up all of it.
    andir committed Dec 11, 2019
    Copy the full SHA
    5ad8326 View commit details

Commits on Dec 12, 2019

  1. buildRustCrate: use less bash for the build script

    We can get rid of a bunch of workarounds that were in the build script
    before by just passing on the `crateBin` attribute.
    
    Before we converted the list of attributes to a string only to convert
    it back in bash during the build phase. We can do the entire looping
    through builds in Nix and thus need no conversion and parsing of
    attributes over and over again.
    
    The big part that still remains bash is the heuristic that cargo
    introduced and that we can't do at eval time.
    andir committed Dec 12, 2019
    Copy the full SHA
    6ad22f5 View commit details
  2. buildRustCrate: deduplicate dependency override code

    The previous lines were only different in the kind of dependencies but
    otherwise exactly the same. It makes the entire thing a bit more
    readable by moving this into a function that takes care of this.
    andir committed Dec 12, 2019
    Copy the full SHA
    3f49d7a View commit details
  3. buildRustCrate: move common build functions to a dedicated file

    This means we aren't rebuilding hat file for each crate we are building
    and the buildPhase expression is a lot easier to comprehent.
    andir committed Dec 12, 2019
    Copy the full SHA
    2eaaf7a View commit details

Commits on Jan 2, 2020

  1. Merge pull request #75563 from andir/cleanup-buildRustCrate

    Cleanup buildRustCrate expression
    andir authored Jan 2, 2020
    Copy the full SHA
    9f03cb8 View commit details
175 changes: 50 additions & 125 deletions pkgs/build-support/rust/build-rust-crate/build-crate.nix
Original file line number Diff line number Diff line change
@@ -1,151 +1,76 @@
{ lib, stdenv, echo_build_heading, noisily, makeDeps, rust }:
{ lib, stdenv, echo_build_heading, noisily, mkRustcDepArgs, rust }:
{ crateName,
dependencies,
crateFeatures, crateRenames, libName, release, libPath,
crateType, metadata, crateBin, hasCrateBin,
extraRustcOpts, verbose, colors }:
extraRustcOpts, verbose, colors,
}:

let

deps = makeDeps dependencies crateRenames;
rustcOpts =
lib.lists.foldl' (opts: opt: opts + " " + opt)
(if release then "-C opt-level=3" else "-C debuginfo=2")
(["-C codegen-units=$NIX_BUILD_CORES"] ++ extraRustcOpts);
baseRustcOpts =
[(if release then "-C opt-level=3" else "-C debuginfo=2")]
++ ["-C codegen-units=$NIX_BUILD_CORES"]
++ [(mkRustcDepArgs dependencies crateRenames)]
++ [crateFeatures]
++ extraRustcOpts
++ lib.optional (stdenv.hostPlatform != stdenv.buildPlatform) "--target ${rust.toRustTarget stdenv.hostPlatform} -C linker=${stdenv.hostPlatform.config}-gcc"
;
rustcMeta = "-C metadata=${metadata} -C extra-filename=-${metadata}";


# build the final rustc arguments that can be different between different
# crates
libRustcOpts = lib.concatStringsSep " " (
baseRustcOpts
++ [rustcMeta]
++ (map (x: "--crate-type ${x}") crateType)
);

binRustcOpts = lib.concatStringsSep " " (
baseRustcOpts
);

in ''
runHook preBuild
norm=""
bold=""
green=""
boldgreen=""
if [[ "${colors}" == "always" ]]; then
norm="$(printf '\033[0m')" #returns to "normal"
bold="$(printf '\033[0;1m')" #set bold
green="$(printf '\033[0;32m')" #set green
boldgreen="$(printf '\033[0;1;32m')" #set bold, and set green.
fi
${echo_build_heading colors}
${noisily colors verbose}
build_lib() {
lib_src=$1
echo_build_heading $lib_src ${libName}
# configure & source common build functions
LIB_RUSTC_OPTS="${libRustcOpts}"
BIN_RUSTC_OPTS="${binRustcOpts}"
LIB_EXT="${stdenv.hostPlatform.extensions.sharedLibrary}"
LIB_PATH="${libPath}"
LIB_NAME="${libName}"
source ${./lib.sh}
noisily rustc --crate-name $CRATE_NAME $lib_src \
${lib.strings.concatStrings (map (x: " --crate-type ${x}") crateType)} \
${rustcOpts} ${rustcMeta} ${crateFeatures} --out-dir target/lib \
--emit=dep-info,link -L dependency=target/deps ${deps} --cap-lints allow \
$BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES --color ${colors}
CRATE_NAME='${lib.replaceStrings ["-"] ["_"] libName}'
EXTRA_LIB=" --extern $CRATE_NAME=target/lib/lib$CRATE_NAME-${metadata}.rlib"
if [ -e target/deps/lib$CRATE_NAME-${metadata}${stdenv.hostPlatform.extensions.sharedLibrary} ]; then
EXTRA_LIB="$EXTRA_LIB --extern $CRATE_NAME=target/lib/lib$CRATE_NAME-${metadata}${stdenv.hostPlatform.extensions.sharedLibrary}"
fi
}
setup_link_paths
build_bin() {
crate_name=$1
crate_name_=$(echo $crate_name | sed -e "s/-/_/g")
main_file=""
if [[ ! -z $2 ]]; then
main_file=$2
fi
echo_build_heading $@
noisily rustc --crate-name $crate_name_ $main_file --crate-type bin ${rustcOpts}\
${crateFeatures} --out-dir target/bin --emit=dep-info,link -L dependency=target/deps \
$LINK ${deps}$EXTRA_LIB --cap-lints allow \
$BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES --color ${colors} \
${if stdenv.hostPlatform != stdenv.buildPlatform then "--target ${rust.toRustTarget stdenv.hostPlatform} -C linker=${stdenv.hostPlatform.config}-gcc" else ""}
if [ "$crate_name_" != "$crate_name" ]; then
mv target/bin/$crate_name_ target/bin/$crate_name
fi
}
EXTRA_LIB=""
CRATE_NAME=$(echo ${libName} | sed -e "s/-/_/g")
if [[ -e target/link_ ]]; then
EXTRA_BUILD="$(cat target/link_) $EXTRA_BUILD"
fi
if [[ -e "${libPath}" ]]; then
build_lib ${libPath}
if [[ -e "$LIB_PATH" ]]; then
build_lib $LIB_PATH
elif [[ -e src/lib.rs ]]; then
build_lib src/lib.rs
elif [[ -e src/${libName}.rs ]]; then
build_lib src/${libName}.rs
elif [[ -e "src/$LIB_NAME.rs" ]]; then
build_lib src/$LIB_NAME.rs
fi
echo "$EXTRA_LINK_SEARCH" | while read i; do
if [[ ! -z "$i" ]]; then
for library in $i; do
echo "-L $library" >> target/link
L=$(echo $library | sed -e "s#$(pwd)/target/build#$lib/lib#")
echo "-L $L" >> target/link.final
done
fi
done
echo "$EXTRA_LINK" | while read i; do
if [[ ! -z "$i" ]]; then
for library in $i; do
echo "-l $library" >> target/link
echo "-l $library" >> target/link.final
done
fi
done
if [[ -e target/link ]]; then
sort -u target/link.final > target/link.final.sorted
mv target/link.final.sorted target/link.final
sort -u target/link > target/link.sorted
mv target/link.sorted target/link
tr '\n' ' ' < target/link > target/link_
LINK=$(cat target/link_)
fi
${lib.optionalString (crateBin != "") ''
printf "%s\n" "${crateBin}" | head -n1 | tr -s ',' '\n' | while read -r BIN_NAME BIN_PATH; do
${lib.optionalString (lib.length crateBin > 0) (lib.concatMapStringsSep "\n" (bin: ''
mkdir -p target/bin
# filter empty entries / empty "lines"
if [[ -z "$BIN_NAME" ]]; then
continue
fi
if [[ -z "$BIN_PATH" ]]; then
# heuristic to "guess" the correct source file as found in cargo:
# https://github.com/rust-lang/cargo/blob/90fc9f620190d5fa3c80b0c8c65a1e1361e6b8ae/src/cargo/util/toml/targets.rs#L308-L325
# the first two cases are the "new" default IIRC
BIN_NAME_=$(echo $BIN_NAME | sed -e 's/-/_/g')
FILES=( "src/bin/$BIN_NAME.rs" "src/bin/$BIN_NAME/main.rs" "src/bin/$BIN_NAME_.rs" "src/bin/$BIN_NAME_/main.rs" "src/bin/main.rs" "src/main.rs" )
if ! [ -e "${libPath}" -o -e src/lib.rs -o -e "src/${libName}.rs" ]; then
# if this is not a library the following path is also valid
FILES=( "src/$BIN_NAME.rs" "src/$BIN_NAME_.rs" "''${FILES[@]}" )
fi
for file in "''${FILES[@]}";
do
echo "checking file $file"
# first file that exists wins
if [[ -e "$file" ]]; then
BIN_PATH="$file"
break
fi
done
if [[ -z "$BIN_PATH" ]]; then
echo "failed to find file for binary target: $BIN_NAME" >&2
exit 1
fi
fi
BIN_NAME='${bin.name or crateName}'
${if !bin ? path then ''
BIN_PATH=""
search_for_bin_path "$BIN_NAME"
'' else ''
BIN_PATH='${bin.path}'
''}
build_bin "$BIN_NAME" "$BIN_PATH"
done
''}
'') crateBin)}
${lib.optionalString (crateBin == "" && !hasCrateBin) ''
# If crateBin is empty and hasCrateBin is not set then we must try to
# detect some kind of bin target based on some files that might exist.
${lib.optionalString (lib.length crateBin == 0 && !hasCrateBin) ''
if [[ -e src/main.rs ]]; then
mkdir -p target/bin
build_bin ${crateName} src/main.rs
14 changes: 7 additions & 7 deletions pkgs/build-support/rust/build-rust-crate/configure-crate.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ lib, stdenv, echo_build_heading, noisily, makeDeps }:
{ lib, stdenv, echo_build_heading, noisily, mkRustcDepArgs }:
{ build
, buildDependencies
, colors
@@ -20,12 +20,12 @@
, verbose
, workspace_member }:
let version_ = lib.splitString "-" crateVersion;
versionPre = if lib.tail version_ == [] then "" else builtins.elemAt version_ 1;
versionPre = if lib.tail version_ == [] then "" else lib.elemAt version_ 1;
version = lib.splitVersion (lib.head version_);
rustcOpts = lib.lists.foldl' (opts: opt: opts + " " + opt)
rustcOpts = lib.foldl' (opts: opt: opts + " " + opt)
(if release then "-C opt-level=3" else "-C debuginfo=2")
(["-C codegen-units=$NIX_BUILD_CORES"] ++ extraRustcOpts);
buildDeps = makeDeps buildDependencies crateRenames;
buildDeps = mkRustcDepArgs buildDependencies crateRenames;
authors = lib.concatStringsSep ":" crateAuthors;
optLevel = if release then 3 else 0;
completeDepsDir = lib.concatStringsSep " " completeDeps;
@@ -90,9 +90,9 @@ in ''
export HOST="${stdenv.hostPlatform.config}"
export PROFILE=${if release then "release" else "debug"}
export OUT_DIR=$(pwd)/target/build/${crateName}.out
export CARGO_PKG_VERSION_MAJOR=${builtins.elemAt version 0}
export CARGO_PKG_VERSION_MINOR=${builtins.elemAt version 1}
export CARGO_PKG_VERSION_PATCH=${builtins.elemAt version 2}
export CARGO_PKG_VERSION_MAJOR=${lib.elemAt version 0}
export CARGO_PKG_VERSION_MINOR=${lib.elemAt version 1}
export CARGO_PKG_VERSION_PATCH=${lib.elemAt version 2}
export CARGO_PKG_VERSION_PRE="${versionPre}"
export CARGO_PKG_HOMEPAGE="${crateHomepage}"
export NUM_JOBS=1
Loading