summary refs log tree commit diff
path: root/pkgs/build-support/rust/build-rust-crate
diff options
context:
space:
mode:
authorPeter Kolloch <info@eigenvalue.net>2020-03-01 12:58:26 +0100
committerPeter Kolloch <info@eigenvalue.net>2020-03-09 14:26:28 +0100
commit04e7462ee61c3f0477b40204f4d90e6b6d5d0a32 (patch)
tree74ef6cde9cefea6e15cdfcbb27cf58143307bcc8 /pkgs/build-support/rust/build-rust-crate
parente2920d957ac60f4cab3f3f21cc7281209b2fe535 (diff)
downloadnixpkgs-04e7462ee61c3f0477b40204f4d90e6b6d5d0a32.tar
nixpkgs-04e7462ee61c3f0477b40204f4d90e6b6d5d0a32.tar.gz
nixpkgs-04e7462ee61c3f0477b40204f4d90e6b6d5d0a32.tar.bz2
nixpkgs-04e7462ee61c3f0477b40204f4d90e6b6d5d0a32.tar.lz
nixpkgs-04e7462ee61c3f0477b40204f4d90e6b6d5d0a32.tar.xz
nixpkgs-04e7462ee61c3f0477b40204f4d90e6b6d5d0a32.tar.zst
nixpkgs-04e7462ee61c3f0477b40204f4d90e6b6d5d0a32.zip
buildRustCrate: refactor colored logging
* Make errors include the crate name and make them much more prominent.
* Move more code into lib.sh
* Already source generated logging code and lib.sh in configure
Diffstat (limited to 'pkgs/build-support/rust/build-rust-crate')
-rw-r--r--pkgs/build-support/rust/build-rust-crate/build-crate.nix7
-rw-r--r--pkgs/build-support/rust/build-rust-crate/configure-crate.nix8
-rw-r--r--pkgs/build-support/rust/build-rust-crate/default.nix8
-rw-r--r--pkgs/build-support/rust/build-rust-crate/lib.sh10
-rw-r--r--pkgs/build-support/rust/build-rust-crate/log.nix70
5 files changed, 68 insertions, 35 deletions
diff --git a/pkgs/build-support/rust/build-rust-crate/build-crate.nix b/pkgs/build-support/rust/build-rust-crate/build-crate.nix
index 2dcca75e299..9759235e30e 100644
--- a/pkgs/build-support/rust/build-rust-crate/build-crate.nix
+++ b/pkgs/build-support/rust/build-rust-crate/build-crate.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, echo_build_heading, noisily, mkRustcDepArgs, rust }:
+{ lib, stdenv, mkRustcDepArgs, rust }:
 { crateName,
   dependencies,
   crateFeatures, crateRenames, libName, release, libPath,
@@ -35,16 +35,13 @@
     build_bin = if buildTests then "build_bin_test" else "build_bin";
   in ''
     runHook preBuild
-    ${echo_build_heading colors}
-    ${noisily colors verbose}
-
+ 
     # 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}
 
     CRATE_NAME='${lib.replaceStrings ["-"] ["_"] libName}'
 
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 c146ffef5ff..bbe7090aed3 100644
--- a/pkgs/build-support/rust/build-rust-crate/configure-crate.nix
+++ b/pkgs/build-support/rust/build-rust-crate/configure-crate.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, echo_build_heading, noisily, mkRustcDepArgs }:
+{ lib, stdenv, echo_colored, noisily, mkRustcDepArgs }:
 { build
 , buildDependencies
 , colors
@@ -32,9 +32,11 @@ let version_ = lib.splitString "-" crateVersion;
     completeBuildDepsDir = lib.concatStringsSep " " completeBuildDeps;
 in ''
   cd ${workspace_member}
-  runHook preConfigure
-  ${echo_build_heading colors}
+  ${echo_colored colors}
   ${noisily colors verbose}
+  source ${./lib.sh}
+
+  runHook preConfigure
   symlink_dependency() {
     # $1 is the nix-store path of a dependency
     # $2 is the target path
diff --git a/pkgs/build-support/rust/build-rust-crate/default.nix b/pkgs/build-support/rust/build-rust-crate/default.nix
index 94b64a1225c..35cb49293e8 100644
--- a/pkgs/build-support/rust/build-rust-crate/default.nix
+++ b/pkgs/build-support/rust/build-rust-crate/default.nix
@@ -4,7 +4,7 @@
 # This can be useful for deploying packages with NixOps, and to share
 # binary dependencies between projects.
 
-{ lib, stdenv, defaultCrateOverrides, fetchCrate, rustc, rust }:
+{ lib, stdenv, defaultCrateOverrides, fetchCrate, rustc, rust, cargo, jq }:
 
 let
     # This doesn't appear to be officially documented anywhere yet.
@@ -29,14 +29,14 @@ let
            " --extern ${name}=${dep.lib}/lib/lib${extern}-${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}")
       ) dependencies;
 
-   inherit (import ./log.nix { inherit lib; }) noisily echo_build_heading;
+   inherit (import ./log.nix { inherit lib; }) noisily echo_colored;
 
    configureCrate = import ./configure-crate.nix {
-     inherit lib stdenv echo_build_heading noisily mkRustcDepArgs;
+     inherit lib stdenv echo_colored noisily mkRustcDepArgs;
    };
 
    buildCrate = import ./build-crate.nix {
-     inherit lib stdenv echo_build_heading noisily mkRustcDepArgs rust;
+     inherit lib stdenv mkRustcDepArgs rust;
    };
 
    installCrate = import ./install-crate.nix { inherit stdenv; };
diff --git a/pkgs/build-support/rust/build-rust-crate/lib.sh b/pkgs/build-support/rust/build-rust-crate/lib.sh
index d4d9317496f..35e804aa104 100644
--- a/pkgs/build-support/rust/build-rust-crate/lib.sh
+++ b/pkgs/build-support/rust/build-rust-crate/lib.sh
@@ -1,3 +1,11 @@
+echo_build_heading() {
+  if (( $# == 1 )); then
+    echo_colored "Building $1"
+  else
+    echo_colored "Building $1 ($2)"
+  fi
+}
+
 build_lib() {
   lib_src=$1
   echo_build_heading $lib_src ${libName}
@@ -132,7 +140,7 @@ search_for_bin_path() {
   done
 
   if [[ -z "$BIN_PATH" ]]; then
-    echo "failed to find file for binary target: $BIN_NAME" >&2
+    echo_error "ERROR: failed to find file for binary target: $BIN_NAME" >&2
     exit 1
   fi
 }
diff --git a/pkgs/build-support/rust/build-rust-crate/log.nix b/pkgs/build-support/rust/build-rust-crate/log.nix
index 25181c787e2..a7e2cb4f463 100644
--- a/pkgs/build-support/rust/build-rust-crate/log.nix
+++ b/pkgs/build-support/rust/build-rust-crate/log.nix
@@ -1,30 +1,56 @@
 { lib }:
-{
-  echo_build_heading = colors: ''
-    echo_build_heading() {
-     start=""
-     end=""
-     ${lib.optionalString (colors == "always") ''
-       start="$(printf '\033[0;1;32m')" #set bold, and set green.
-       end="$(printf '\033[0m')" #returns to "normal"
-     ''}
-     if (( $# == 1 )); then
-       echo "$start""Building $1""$end"
-     else
-       echo "$start""Building $1 ($2)""$end"
-     fi
+
+let echo_colored_body = start_escape:
+      # Body of a function that behaves like "echo" but 
+      # has the output colored by the given start_escape
+      # sequence. E.g.
+      #
+      # * echo_x "Building ..."
+      # * echo_x -n "Running "
+      #
+      # This is more complicated than apparent at first sight 
+      # because:
+      #   * The color markers and the text must be print
+      #     in the same echo statement. Otherise, other
+      #     intermingled text from concurrent builds will 
+      #     be colored as well.
+      #   * We need to preserve the trailing newline of the
+      #     echo if and only if it is present. Bash likes
+      #     to strip those if we capture the output of echo
+      #     in a variable.  
+      #   * Leading "-" will be interpreted by test as an
+      #     option for itself. Therefore, we prefix it with
+      #     an x in `[[ "x$1" =~ ^x- ]]`.
+      ''
+      local echo_args="";
+      while [[ "x$1" =~ ^x- ]]; do
+        echo_args+=" $1"
+        shift
+      done
+      
+      local start_escape="$(printf '${start_escape}')"
+      local reset="$(printf '\033[0m')"
+      echo $echo_args $start_escape"$@"$reset
+      '';
+  echo_conditional_colored_body = colors: start_escape:
+      if colors == "always" 
+      then (echo_colored_body start_escape)
+      else ''echo "$@"'';
+in {
+  echo_colored = colors: ''
+    echo_colored() {
+      ${echo_conditional_colored_body colors ''\033[0;1;32m''}
     }
-  '';
+
+    echo_error() {
+      ${echo_conditional_colored_body colors ''\033[0;1;31m''}
+    }
+   '';
+
   noisily = colors: verbose: ''
     noisily() {
-      start=""
-      end=""
-      ${lib.optionalString (colors == "always") ''
-        start="$(printf '\033[0;1;32m')" #set bold, and set green.
-        end="$(printf '\033[0m')" #returns to "normal"
-      ''}
   	  ${lib.optionalString verbose ''
-        echo -n "$start"Running "$end"
+        echo_colored -n "Running " 
         echo $@
   	  ''}
   	  $@