diff options
author | Alyssa Ross <hi@alyssa.is> | 2023-11-21 16:12:21 +0100 |
---|---|---|
committer | Alyssa Ross <hi@alyssa.is> | 2023-11-21 16:12:48 +0100 |
commit | 048a4cd441a59cbf89defb18bb45c9f0b4429b35 (patch) | |
tree | f8f5850ff05521ab82d65745894714a8796cbfb6 /pkgs/stdenv/adapters.nix | |
parent | 030c5028b07afcedce7c5956015c629486cc79d9 (diff) | |
parent | 4c2d05dd6435d449a3651a6dd314d9411b5f8146 (diff) | |
download | nixpkgs-rootfs.tar nixpkgs-rootfs.tar.gz nixpkgs-rootfs.tar.bz2 nixpkgs-rootfs.tar.lz nixpkgs-rootfs.tar.xz nixpkgs-rootfs.tar.zst nixpkgs-rootfs.zip |
Signed-off-by: Alyssa Ross <hi@alyssa.is>
Diffstat (limited to 'pkgs/stdenv/adapters.nix')
-rw-r--r-- | pkgs/stdenv/adapters.nix | 199 |
1 files changed, 184 insertions, 15 deletions
diff --git a/pkgs/stdenv/adapters.nix b/pkgs/stdenv/adapters.nix index f29bdf671c8..dd298719071 100644 --- a/pkgs/stdenv/adapters.nix +++ b/pkgs/stdenv/adapters.nix @@ -42,6 +42,50 @@ rec { stdenv.override (prev: { allowedRequisites = null; extraBuildInputs = (prev.extraBuildInputs or []) ++ pkgs; }); + # Override the libc++ dynamic library used in the stdenv to use the one from the platform’s + # default stdenv. This allows building packages and linking dependencies with different + # compiler versions while still using the same libc++ implementation for compatibility. + # + # Note that this adapter still uses the headers from the new stdenv’s libc++. This is necessary + # because older compilers may not be able to parse the headers from the default stdenv’s libc++. + overrideLibcxx = stdenv: + assert stdenv.cc.libcxx != null; + let + llvmLibcxxVersion = lib.getVersion llvmLibcxx; + stdenvLibcxxVersion = lib.getVersion stdenvLibcxx; + + stdenvLibcxx = pkgs.stdenv.cc.libcxx; + stdenvCxxabi = pkgs.stdenv.cc.libcxx.cxxabi; + + llvmLibcxx = stdenv.cc.libcxx; + llvmCxxabi = stdenv.cc.libcxx.cxxabi; + + libcxx = pkgs.runCommand "${stdenvLibcxx.name}-${llvmLibcxxVersion}" { + outputs = [ "out" "dev" ]; + inherit cxxabi; + isLLVM = true; + } '' + mkdir -p "$dev/nix-support" + ln -s '${stdenvLibcxx}' "$out" + echo '${stdenvLibcxx}' > "$dev/nix-support/propagated-build-inputs" + ln -s '${lib.getDev llvmLibcxx}/include' "$dev/include" + ''; + + cxxabi = pkgs.runCommand "${stdenvCxxabi.name}-${llvmLibcxxVersion}" { + outputs = [ "out" "dev" ]; + inherit (stdenvCxxabi) libName; + } '' + mkdir -p "$dev/nix-support" + ln -s '${stdenvCxxabi}' "$out" + echo '${stdenvCxxabi}' > "$dev/nix-support/propagated-build-inputs" + ln -s '${lib.getDev llvmCxxabi}/include' "$dev/include" + ''; + in + overrideCC stdenv (stdenv.cc.override { + inherit libcxx; + extraPackages = [ cxxabi pkgs.pkgsTargetTarget."llvmPackages_${lib.versions.major llvmLibcxxVersion}".compiler-rt ]; + }); + # Override the setup script of stdenv. Useful for testing new # versions of the setup script without causing a rebuild of # everything. @@ -60,12 +104,13 @@ rec { mkDerivationFromStdenv = withOldMkDerivation old (stdenv: mkDerivationSuper: args: if stdenv.hostPlatform.isDarwin then throw "Cannot build fully static binaries on Darwin/macOS" - else (mkDerivationSuper args).overrideAttrs(finalAttrs: { - NIX_CFLAGS_LINK = toString (finalAttrs.NIX_CFLAGS_LINK or "") + " -static"; - } // lib.optionalAttrs (!(finalAttrs.dontAddStaticConfigureFlags or false)) { - configureFlags = (finalAttrs.configureFlags or []) ++ [ - "--disable-shared" # brrr... - ]; + else (mkDerivationSuper args).overrideAttrs (args: { + NIX_CFLAGS_LINK = toString (args.NIX_CFLAGS_LINK or "") + " -static"; + } // lib.optionalAttrs (!(args.dontAddStaticConfigureFlags or false)) { + configureFlags = (args.configureFlags or []) ++ [ + "--disable-shared" # brrr... + ]; + cmakeFlags = (args.cmakeFlags or []) ++ ["-DCMAKE_SKIP_INSTALL_RPATH=On"]; })); } // lib.optionalAttrs (stdenv0.hostPlatform.libc == "glibc") { extraBuildInputs = (old.extraBuildInputs or []) ++ [ @@ -192,18 +237,15 @@ rec { useMoldLinker = stdenv: let bintools = stdenv.cc.bintools.override { extraBuildCommands = '' - wrap ld.mold ${../build-support/bintools-wrapper/ld-wrapper.sh} ${pkgs.mold}/bin/ld.mold - wrap ld ${../build-support/bintools-wrapper/ld-wrapper.sh} ${pkgs.mold}/bin/ld.mold + wrap ${stdenv.cc.bintools.targetPrefix}ld.mold ${../build-support/bintools-wrapper/ld-wrapper.sh} ${pkgs.mold}/bin/ld.mold + wrap ${stdenv.cc.bintools.targetPrefix}ld ${../build-support/bintools-wrapper/ld-wrapper.sh} ${pkgs.mold}/bin/ld.mold ''; }; in stdenv.override (old: { - cc = stdenv.cc.override { - inherit bintools; - }; - allowedRequisites = - lib.mapNullable (rs: rs ++ [ bintools pkgs.mold (lib.getLib pkgs.mimalloc) (lib.getLib pkgs.openssl) ]) (stdenv.allowedRequisites or null); - # gcc >12.1.0 supports '-fuse-ld=mold' - # the wrap ld above in bintools supports gcc <12.1.0 and shouldn't harm >12.1.0 + allowedRequisites = null; + cc = stdenv.cc.override { inherit bintools; }; + # gcc >12.1.0 supports '-fuse-ld=mold' + # the wrap ld above in bintools supports gcc <12.1.0 and shouldn't harm >12.1.0 # https://github.com/rui314/mold#how-to-use } // lib.optionalAttrs (stdenv.cc.isClang || (stdenv.cc.isGNU && lib.versionAtLeast stdenv.cc.version "12")) { mkDerivationFromStdenv = extendMkDerivationArgs old (args: { @@ -248,4 +290,131 @@ rec { env = (args.env or {}) // { NIX_CFLAGS_COMPILE = toString (args.env.NIX_CFLAGS_COMPILE or "") + " ${toString compilerFlags}"; }; }); }); + + # Overriding the SDK changes the Darwin SDK used to build the package, which: + # * Ensures that the compiler and bintools have the correct Libsystem version; and + # * Replaces any SDK references with those in the SDK corresponding to the requested SDK version. + # + # `sdkVersion` can be any of the following: + # * A version string indicating the requested SDK version; or + # * An attrset consisting of either or both of the following fields: darwinSdkVersion and darwinMinVersion. + overrideSDK = stdenv: sdkVersion: + let + inherit ( + { inherit (stdenv.hostPlatform) darwinMinVersion darwinSdkVersion; } + // (if lib.isAttrs sdkVersion then sdkVersion else { darwinSdkVersion = sdkVersion; }) + ) darwinMinVersion darwinSdkVersion; + + sdk = pkgs.darwin."apple_sdk_${lib.replaceStrings [ "." ] [ "_" ] darwinSdkVersion}"; + # TODO: Make this unconditional after #229210 has been merged, + # and the 10.12 SDK is updated to follow the new structure. + Libsystem = if darwinSdkVersion == "10.12" then pkgs.darwin.Libsystem else sdk.Libsystem; + + replacePropagatedFrameworks = pkg: + let + propagatedInputs = pkg.propagatedBuildInputs; + mappedInputs = map mapPackageToSDK propagatedInputs; + + env = { + inherit (pkg) outputs; + # Map old frameworks to new ones and the package’s outputs to their original outPaths. + # Also map any packages that have propagated frameworks to their proxy packages using + # the requested SDK version. These mappings are rendered into tab-separated files to be + # parsed and read back with `read`. + dependencies = lib.concatMapStrings (pair: "${pair.fst}\t${pair.snd}\n") (lib.zipLists propagatedInputs mappedInputs); + pkgOutputs = lib.concatMapStrings (output: "${output}\t${(lib.getOutput output pkg).outPath}\n") pkg.outputs; + passAsFile = [ "dependencies" "pkgOutputs" ]; + }; + in + # Only remap the package’s propagated inputs if there are any and if any of them were themselves remapped. + if lib.length propagatedInputs > 0 && propagatedInputs != mappedInputs + then pkgs.runCommand pkg.name env '' + # Iterate over the outputs in the package being replaced to make sure the proxy is + # a fully functional replacement. This is like `symlinkJoin` except for outputs and + # the contents of `nix-support`, which will be customized for the requested SDK. + while IFS=$'\t\n' read -r outputName pkgOutputPath; do + mkdir -p "''${!outputName}" + + for targetPath in "$pkgOutputPath"/*; do + targetName=$(basename "$targetPath") + + # `nix-support` is special-cased because any propagated inputs need their SDK + # frameworks replaced with those from the requested SDK. + if [ "$targetName" == "nix-support" ]; then + mkdir "''${!outputName}/nix-support" + + for file in "$targetPath"/*; do + fileName=$(basename "$file") + + if [ "$fileName" == "propagated-build-inputs" ]; then + cp "$file" "''${!outputName}/nix-support/$fileName" + + while IFS=$'\t\n' read -r oldFramework newFramework; do + substituteInPlace "''${!outputName}/nix-support/$fileName" \ + --replace "$oldFramework" "$newFramework" + done < "$dependenciesPath" + fi + done + else + ln -s "$targetPath" "''${!outputName}/$targetName" + fi + done + done < "$pkgOutputsPath" + '' + else pkg; + + # Remap a framework from one SDK version to another. + mapPackageToSDK = pkg: + let + name = lib.getName pkg; + framework = lib.removePrefix "apple-framework-" name; + in + /**/ if pkg == null then pkg + else if name != framework then sdk.frameworks."${framework}" + else replacePropagatedFrameworks pkg; + + mapRuntimeToSDK = pkg: + # Only remap xcbuild for now, which exports the SDK used to build it. + if pkg != null && lib.isAttrs pkg && lib.getName pkg == "xcodebuild" + then pkg.override { stdenv = overrideSDK stdenv { inherit darwinMinVersion darwinSdkVersion; }; } + else pkg; + + mapInputsToSDK = inputs: args: + let + runsAtBuild = lib.flip lib.elem [ + "depsBuildBuild" + "depsBuildBuildPropagated" + "nativeBuildInputs" + "propagatedNativeBuildInputs" + "depsBuildTarget" + "depsBuildTargetPropagated" + ]; + atBuildInputs = lib.filter runsAtBuild inputs; + atRuntimeInputs = lib.subtractLists atBuildInputs inputs; + in + lib.genAttrs atRuntimeInputs (input: map mapPackageToSDK (args."${input}" or [ ])) + // lib.genAttrs atBuildInputs (input: map mapRuntimeToSDK (args."${input}" or [ ])); + + mkCC = cc: cc.override { + bintools = cc.bintools.override { libc = Libsystem; }; + libc = Libsystem; + }; + in + # TODO: make this work across all input types and not just propagatedBuildInputs + stdenv.override (old: { + buildPlatform = old.buildPlatform // { inherit darwinMinVersion darwinSdkVersion; }; + hostPlatform = old.hostPlatform // { inherit darwinMinVersion darwinSdkVersion; }; + targetPlatform = old.targetPlatform // { inherit darwinMinVersion darwinSdkVersion; }; + + allowedRequisites = null; + cc = mkCC old.cc; + + extraBuildInputs = [sdk.frameworks.CoreFoundation ]; + mkDerivationFromStdenv = extendMkDerivationArgs old (mapInputsToSDK [ + "buildInputs" + "nativeBuildInputs" + "propagatedNativeBuildInputs" + "propagatedBuildInputs" + ]); + }); } |