summary refs log tree commit diff
path: root/pkgs/build-support/rust/build-rust-crate/configure-crate.nix
diff options
context:
space:
mode:
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.nix60
1 files changed, 45 insertions, 15 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 013b99a77b4b2..61c39c6b21c0c 100644
--- a/pkgs/build-support/rust/build-rust-crate/configure-crate.nix
+++ b/pkgs/build-support/rust/build-rust-crate/configure-crate.nix
@@ -56,32 +56,63 @@ in ''
     i=$1
     ln -s -f $i/lib/*.rlib $2 #*/
     ln -s -f $i/lib/*.so $i/lib/*.dylib $2 #*/
-    if [ -e "$i/lib/link" ]; then
-        cat $i/lib/link >> target/link
-        cat $i/lib/link >> target/link.final
-    fi
     if [ -e $i/env ]; then
         source $i/env
     fi
   }
 
+  # The following steps set up the dependencies of the crate. Two
+  # kinds of dependencies are distinguished: build dependencies
+  # (used by the build script) and crate dependencies. For each
+  # dependency we have to:
+  #
+  # - Make its Rust library available to rustc. This is done by
+  #   symlinking all library dependencies into a directory that
+  #   can be provided to rustc.
+  # - Accumulate linking flags. These flags are largely used for
+  #   linking native libraries.
+  #
+  # The crate link flags are added to the `link` and `link.final`
+  # files. The `link` file is used for linkage in the current
+  # crate. The `link.final` file will be copied to the output and can
+  # be used by downstream crates to get the linker flags of this
+  # crate.
+
   mkdir -p target/{deps,lib,build,buildDeps}
   chmod uga+w target -R
   echo ${extraLinkFlags} > target/link
   echo ${extraLinkFlags} > target/link.final
+
+  # Prepare crate dependencies
   for i in ${completeDepsDir}; do
     symlink_dependency $i target/deps
+    if [ -e "$i/lib/link" ]; then
+      cat $i/lib/link >> target/link
+      cat $i/lib/link >> target/link.final
+    fi
   done
+
+  # Prepare crate build dependencies that are used for the build script.
   for i in ${completeBuildDepsDir}; do
-     symlink_dependency $i target/buildDeps
+    symlink_dependency $i target/buildDeps
+    if [ -e "$i/lib/link" ]; then
+      cat $i/lib/link >> target/link.build
+    fi
   done
-  if [[ -e target/link ]]; then
-    sort -u target/link > target/link.sorted
-    mv target/link.sorted target/link
-    sort -u target/link.final > target/link.final.sorted
-    mv target/link.final.sorted target/link.final
-    tr '\n' ' ' < target/link > target/link_
+
+  # Remove duplicate linker flags from the build dependencies.
+  if [[ -e target/link.build ]]; then
+    sort -uo target/link.build target/link.build
   fi
+
+  # Remove duplicate linker flags from the dependencies.
+  sort -uo target/link target/link
+  tr '\n' ' ' < target/link > target/link_
+
+  # Remove duplicate linker flags from the that are written
+  # to the derivation's output.
+  sort -uo target/link.final target/link.final
+
   EXTRA_BUILD=""
   BUILD_OUT_DIR=""
   export CARGO_PKG_NAME=${crateName}
@@ -120,15 +151,14 @@ in ''
   elif [[ -e "build.rs" ]]; then
      BUILD="build.rs"
   fi
+
+  # Compile and run the build script, when available.
   if [[ ! -z "$BUILD" ]] ; then
      echo_build_heading "$BUILD" ${libName}
      mkdir -p target/build/${crateName}
      EXTRA_BUILD_FLAGS=""
-     if [ -e target/link_ ]; then
-       EXTRA_BUILD_FLAGS=$(cat target/link_)
-     fi
      if [ -e target/link.build ]; then
-       EXTRA_BUILD_FLAGS="$EXTRA_BUILD_FLAGS $(cat target/link.build)"
+       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 \