Skip to content

Commit

Permalink
buildRustCrate: refactor colored logging
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kolloch committed Mar 9, 2020
1 parent e2920d9 commit 04e7462
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 35 deletions.
7 changes: 2 additions & 5 deletions 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,
Expand Down Expand Up @@ -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}'
Expand Down
8 changes: 5 additions & 3 deletions 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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pkgs/build-support/rust/build-rust-crate/default.nix
Expand Up @@ -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.
Expand All @@ -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; };
Expand Down
10 changes: 9 additions & 1 deletion 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}
Expand Down Expand Up @@ -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
}
70 changes: 48 additions & 22 deletions 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 $@
''}
$@
Expand Down

0 comments on commit 04e7462

Please sign in to comment.