From 51755b0c00881bfe1602db0eb689efd2c66bcc20 Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Sun, 29 Sep 2024 13:54:47 -0400 Subject: [PATCH] {bintools,cc}-wrapper: use a fallback SDK when `DEVELOPER_DIR` is not set It is unusual to invoke a wrapped compiler without setting `DEVELOPER_DIR`, but it can happen when a user adds a compiler to their installed packages or when a package intentionally invokes the compiler without an environment (such as the GHC binary packages). --- .../bintools-wrapper/add-darwin-ldflags-before.sh | 2 +- pkgs/build-support/bintools-wrapper/default.nix | 4 ++++ pkgs/build-support/cc-wrapper/add-flags.sh | 2 +- pkgs/build-support/cc-wrapper/default.nix | 4 ++++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pkgs/build-support/bintools-wrapper/add-darwin-ldflags-before.sh b/pkgs/build-support/bintools-wrapper/add-darwin-ldflags-before.sh index afcfce8ea9d5..79f0546403f7 100644 --- a/pkgs/build-support/bintools-wrapper/add-darwin-ldflags-before.sh +++ b/pkgs/build-support/bintools-wrapper/add-darwin-ldflags-before.sh @@ -84,7 +84,7 @@ mangleVarSingle DEVELOPER_DIR ${role_suffixes[@]+"${role_suffixes[@]}"} # Allow wrapped bintools to do something useful when no `DEVELOPER_DIR` is set, which can happen when # the compiler is run outside of a stdenv or intentionally in an environment with no environment variables set. -DEVELOPER_DIR=${DEVELOPER_DIR_@suffixSalt@} +DEVELOPER_DIR=${DEVELOPER_DIR_@suffixSalt@:-@fallback_sdk@} # Darwin looks for frameworks in the SDK located at `DEVELOPER_DIR`. extraBefore+=("-F$DEVELOPER_DIR/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks") diff --git a/pkgs/build-support/bintools-wrapper/default.nix b/pkgs/build-support/bintools-wrapper/default.nix index b64e97345ab5..4a6457350102 100644 --- a/pkgs/build-support/bintools-wrapper/default.nix +++ b/pkgs/build-support/bintools-wrapper/default.nix @@ -10,6 +10,7 @@ , stdenvNoCC , runtimeShell , bintools ? null, libc ? null, coreutils ? null, gnugrep ? null +, apple-sdk ? null , netbsd ? null , sharedLibraryLoader ? if libc == null then @@ -412,6 +413,9 @@ stdenvNoCC.mkDerivation { inherit dynamicLinker targetPrefix suffixSalt coreutils_bin; inherit bintools_bin libc_bin libc_dev libc_lib; default_hardening_flags_str = builtins.toString defaultHardeningFlags; + } // lib.optionalAttrs (apple-sdk != null && stdenvNoCC.targetPlatform.isDarwin) { + # Wrapped compilers should do something useful even when no SDK is provided at `DEVELOPER_DIR`. + fallback_sdk = apple-sdk.__spliced.buildTarget or apple-sdk; }; meta = diff --git a/pkgs/build-support/cc-wrapper/add-flags.sh b/pkgs/build-support/cc-wrapper/add-flags.sh index 6683337b77b9..cd5396f45f8b 100644 --- a/pkgs/build-support/cc-wrapper/add-flags.sh +++ b/pkgs/build-support/cc-wrapper/add-flags.sh @@ -90,7 +90,7 @@ if [ "@darwinMinVersion@" ]; then # Allow wrapped compilers to do something useful when no `DEVELOPER_DIR` is set, which can happen when # the compiler is run outside of a stdenv or intentionally in an environment with no environment variables set. - DEVELOPER_DIR=${DEVELOPER_DIR_@suffixSalt@} + DEVELOPER_DIR=${DEVELOPER_DIR_@suffixSalt@:-@fallback_sdk@} # xcbuild needs `SDKROOT` to be the name of the SDK, which it sets in its own wrapper, # but compilers expect it to point to the absolute path. diff --git a/pkgs/build-support/cc-wrapper/default.nix b/pkgs/build-support/cc-wrapper/default.nix index 01f91b408e38..ed55e37cb76a 100644 --- a/pkgs/build-support/cc-wrapper/default.nix +++ b/pkgs/build-support/cc-wrapper/default.nix @@ -10,6 +10,7 @@ , stdenvNoCC , runtimeShell , cc ? null, libc ? null, bintools, coreutils ? null +, apple-sdk ? null , zlib ? null , nativeTools, noLibc ? false, nativeLibc, nativePrefix ? "" , propagateDoc ? cc != null && cc ? man @@ -749,6 +750,9 @@ stdenvNoCC.mkDerivation { inherit libc_bin libc_dev libc_lib; inherit darwinPlatformForCC darwinMinVersion darwinMinVersionVariable; default_hardening_flags_str = builtins.toString defaultHardeningFlags; + } // lib.optionalAttrs (apple-sdk != null && stdenvNoCC.targetPlatform.isDarwin) { + # Wrapped compilers should do something useful even when no SDK is provided at `DEVELOPER_DIR`. + fallback_sdk = apple-sdk.__spliced.buildTarget or apple-sdk; }; meta =