Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cc-wrapper: convert cc-wrapper.sh to cc-wrapper.nix #266838

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{ lib
, stdenv
, writeScript
}:

writeScript "cc-wrapper.sh" ''
#! @shell@
set -eu -o pipefail +o posix
shopt -s nullglob

if (( "${NIX_DEBUG:-0}" >= 7 )); then
if (( "''${NIX_DEBUG:-0}" >= 7 )); then
set -x
fi

Expand Down Expand Up @@ -34,10 +40,10 @@ expandResponseParams "$@"

declare -ag positionalArgs=()
declare -i n=0
nParams=${#params[@]}
nParams=''${#params[@]}
while (( "$n" < "$nParams" )); do
p=${params[n]}
p2=${params[n+1]:-} # handle `p` being last one
p=''${params[n]}
p2=''${params[n+1]:-} # handle `p` being last one
n+=1

case "$p" in
Expand All @@ -61,8 +67,8 @@ while (( "$n" < "$nParams" )); do
# interpreted as a "non flag" arg:
if [[ -v "params[$n]" ]]; then nonFlagArgs=1; fi

positionalArgs=("${params[@]:$n}")
params=("${params[@]:0:$((n - 1))}")
positionalArgs=("''${params[@]:$n}")
params=("''${params[@]:0:$((n - 1))}")
break;
;;
-?*) ;;
Expand All @@ -80,19 +86,19 @@ if [ "$nonFlagArgs" = 0 ]; then
fi

# Optionally filter out paths not refering to the store.
if [[ "${NIX_ENFORCE_PURITY:-}" = 1 && -n "$NIX_STORE" ]]; then
if [[ "''${NIX_ENFORCE_PURITY:-}" = 1 && -n "$NIX_STORE" ]]; then
kept=()
nParams=${#params[@]}
nParams=''${#params[@]}
declare -i n=0
while (( "$n" < "$nParams" )); do
p=${params[n]}
p2=${params[n+1]:-} # handle `p` being last one
p=''${params[n]}
p2=''${params[n+1]:-} # handle `p` being last one
n+=1

skipNext=false
path=""
case "$p" in
-[IL]/*) path=${p:2} ;;
-[IL]/*) path=''${p:2} ;;
-[IL] | -isystem) path=$p2 skipNext=true ;;
esac

Expand All @@ -105,32 +111,32 @@ if [[ "${NIX_ENFORCE_PURITY:-}" = 1 && -n "$NIX_STORE" ]]; then
kept+=("$p")
done
# Old bash empty array hack
params=(${kept+"${kept[@]}"})
params=(''${kept+"''${kept[@]}"})
fi

# Flirting with a layer violation here.
if [ -z "${NIX_BINTOOLS_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then
if [ -z "''${NIX_BINTOOLS_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then
source @bintools@/nix-support/add-flags.sh
fi

# Put this one second so libc ldflags take priority.
if [ -z "${NIX_CC_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then
if [ -z "''${NIX_CC_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then
source @out@/nix-support/add-flags.sh
fi

# Clear march/mtune=native -- they bring impurity.
if [ "$NIX_ENFORCE_NO_NATIVE_@suffixSalt@" = 1 ]; then
kept=()
# Old bash empty array hack
for p in ${params+"${params[@]}"}; do
for p in ''${params+"''${params[@]}"}; do
if [[ "$p" = -m*=native ]]; then
skip "$p"
else
kept+=("$p")
fi
done
# Old bash empty array hack
params=(${kept+"${kept[@]}"})
params=(''${kept+"''${kept[@]}"})
fi

if [[ "$isCxx" = 1 ]]; then
Expand Down Expand Up @@ -170,11 +176,11 @@ fi
source @out@/nix-support/add-hardening.sh

# Add the flags for the C compiler proper.
extraAfter=(${hardeningCFlagsAfter[@]+"${hardeningCFlagsAfter[@]}"} $NIX_CFLAGS_COMPILE_@suffixSalt@)
extraBefore=(${hardeningCFlagsBefore[@]+"${hardeningCFlagsBefore[@]}"} $NIX_CFLAGS_COMPILE_BEFORE_@suffixSalt@)
extraAfter=(''${hardeningCFlagsAfter[@]+"''${hardeningCFlagsAfter[@]}"} $NIX_CFLAGS_COMPILE_@suffixSalt@)
extraBefore=(''${hardeningCFlagsBefore[@]+"''${hardeningCFlagsBefore[@]}"} $NIX_CFLAGS_COMPILE_BEFORE_@suffixSalt@)

if [ "$dontLink" != 1 ]; then
linkType=$(checkLinkType $NIX_LDFLAGS_BEFORE_@suffixSalt@ "${params[@]}" ${NIX_CFLAGS_LINK_@suffixSalt@:-} $NIX_LDFLAGS_@suffixSalt@)
linkType=$(checkLinkType $NIX_LDFLAGS_BEFORE_@suffixSalt@ "''${params[@]}" ''${NIX_CFLAGS_LINK_@suffixSalt@:-} $NIX_LDFLAGS_@suffixSalt@)

# Add the flags that should only be passed to the compiler when
# linking.
Expand All @@ -189,7 +195,7 @@ if [ "$dontLink" != 1 ]; then
extraBefore+=("-Wl,-dynamic-linker=$NIX_DYNAMIC_LINKER_@suffixSalt@")
fi
for i in $(filterRpathFlags "$linkType" $NIX_LDFLAGS_@suffixSalt@); do
if [ "${i:0:3}" = -L/ ]; then
if [ "''${i:0:3}" = -L/ ]; then
extraAfter+=("$i")
else
extraAfter+=("-Wl,$i")
Expand Down Expand Up @@ -221,19 +227,19 @@ fi

# Finally, if we got any positional args, append them to `extraAfter`
# now:
if [[ "${#positionalArgs[@]}" -gt 0 ]]; then
extraAfter+=(-- "${positionalArgs[@]}")
if [[ "''${#positionalArgs[@]}" -gt 0 ]]; then
extraAfter+=(-- "''${positionalArgs[@]}")
fi

# Optionally print debug info.
if (( "${NIX_DEBUG:-0}" >= 1 )); then
if (( "''${NIX_DEBUG:-0}" >= 1 )); then
# Old bash workaround, see ld-wrapper for explanation.
echo "extra flags before to @prog@:" >&2
printf " %q\n" ${extraBefore+"${extraBefore[@]}"} >&2
printf " %q\n" ''${extraBefore+"''${extraBefore[@]}"} >&2
echo "original flags to @prog@:" >&2
printf " %q\n" ${params+"${params[@]}"} >&2
printf " %q\n" ''${params+"''${params[@]}"} >&2
echo "extra flags after to @prog@:" >&2
printf " %q\n" ${extraAfter+"${extraAfter[@]}"} >&2
printf " %q\n" ''${extraAfter+"''${extraAfter[@]}"} >&2
fi

PATH="$path_backup"
Expand All @@ -245,17 +251,19 @@ if [[ -e @out@/nix-support/cc-wrapper-hook ]]; then
source @out@/nix-support/cc-wrapper-hook
fi

if (( "${NIX_CC_USE_RESPONSE_FILE:-@use_response_file_by_default@}" >= 1 )); then
responseFile=$(mktemp "${TMPDIR:-/tmp}/cc-params.XXXXXX")
if (( "''${NIX_CC_USE_RESPONSE_FILE:-@use_response_file_by_default@}" >= 1 )); then
responseFile=$(mktemp "''${TMPDIR:-/tmp}/cc-params.XXXXXX")
trap 'rm -f -- "$responseFile"' EXIT
printf "%q\n" \
${extraBefore+"${extraBefore[@]}"} \
${params+"${params[@]}"} \
${extraAfter+"${extraAfter[@]}"} > "$responseFile"
''${extraBefore+"''${extraBefore[@]}"} \
''${params+"''${params[@]}"} \
''${extraAfter+"''${extraAfter[@]}"} > "$responseFile"
@prog@ "@$responseFile"
else
exec @prog@ \
${extraBefore+"${extraBefore[@]}"} \
${params+"${params[@]}"} \
${extraAfter+"${extraAfter[@]}"}
''${extraBefore+"''${extraBefore[@]}"} \
''${params+"''${params[@]}"} \
''${extraAfter+"''${extraAfter[@]}"}
fi
''

5 changes: 4 additions & 1 deletion pkgs/build-support/cc-wrapper/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{ name ? ""
, lib
, stdenvNoCC
, writeScript
, cc ? null, libc ? null, bintools, coreutils ? null, shell ? stdenvNoCC.shell
, zlib ? null
, nativeTools, noLibc ? false, nativeLibc, nativePrefix ? ""
Expand Down Expand Up @@ -281,7 +282,9 @@ stdenv.mkDerivation {
src=$PWD
'';

wrapper = ./cc-wrapper.sh;
# callPackage not used because cc-wrapper is part of the stdenv
# bootstrap; careful control of inputs is necessary
wrapper = import ./cc-wrapper.nix { inherit lib stdenv writeScript; };

installPhase =
''
Expand Down
4 changes: 2 additions & 2 deletions pkgs/stdenv/darwin/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ let
inherit (prevStage.llvmPackages) libcxx;

inherit lib;
inherit (prevStage) coreutils gnugrep;
inherit (prevStage) coreutils gnugrep writeScript;

stdenvNoCC = prevStage.ccWrapperStdenv;
};
Expand Down Expand Up @@ -1173,7 +1173,7 @@ in
inherit (self.llvmPackages) libcxx;

inherit lib;
inherit (self) stdenvNoCC coreutils gnugrep;
inherit (self) stdenvNoCC coreutils gnugrep writeScript;

shell = self.bash + "/bin/bash";
};
Expand Down
1 change: 1 addition & 0 deletions pkgs/stdenv/freebsd/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ in

cc = lib.makeOverridable (import ../../build-support/cc-wrapper) {
inherit lib;
inherit (prevStage) writeScript;
nativeTools = true;
nativePrefix = "/usr";
nativeLibc = true;
Expand Down
3 changes: 2 additions & 1 deletion pkgs/stdenv/linux/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ let
isGNU = true;
libc = getLibc prevStage;
inherit lib;
inherit (prevStage) coreutils gnugrep;
inherit (prevStage) coreutils gnugrep writeScript;
stdenvNoCC = prevStage.ccWrapperStdenv;
fortify-headers = prevStage.fortify-headers;
}).overrideAttrs(a: lib.optionalAttrs (prevStage.gcc-unwrapped.passthru.isXgcc or false) {
Expand Down Expand Up @@ -568,6 +568,7 @@ in
libc = getLibc self;
inherit lib;
inherit (self) stdenvNoCC coreutils gnugrep;
inherit (prevStage) writeScript;
shell = self.bash + "/bin/bash";
fortify-headers = self.fortify-headers;
};
Expand Down
6 changes: 6 additions & 0 deletions pkgs/stdenv/native/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ in
nativeTools = true;
nativeLibc = true;
inherit lib nativePrefix;
writeScript = (import ../../build-support/trivial {
inherit lib stdenv stdenvNoCC;
lndir = null;
runtimeShell = null;
shellcheck-minimal = throw "no shellcheck in first bootstrap stage";
}).writeScript;
bintools = import ../../build-support/bintools-wrapper {
name = "bintools";
inherit lib stdenvNoCC nativePrefix;
Expand Down
2 changes: 1 addition & 1 deletion pkgs/stdenv/nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ bootStages ++ [
nativeTools = false;
nativePrefix = lib.optionalString hostPlatform.isSunOS "/usr";
nativeLibc = true;
inherit (prevStage) stdenvNoCC binutils coreutils gnugrep;
inherit (prevStage) stdenvNoCC binutils coreutils gnugrep writeScript;
cc = prevStage.gcc.cc;
isGNU = true;
shell = prevStage.bash + "/bin/sh";
Expand Down