17 # The targetPrefix is prepended to binary names to allow multiple binuntils on the PATH to both be usable.
18 targetPrefix = lib.optionalString (
19 stdenv.targetPlatform != stdenv.hostPlatform
20 ) "${stdenv.targetPlatform.config}-";
22 # First version with all the required files
23 xnu = fetchFromGitHub {
25 owner = "apple-oss-distributions";
27 rev = "xnu-7195.50.7.100.1";
28 hash = "sha256-uHmAOm6k9ZXWfyqHiDSpm+tZqUbERlr6rXSJ4xNACkM=";
31 stdenv.mkDerivation (finalAttrs: {
32 pname = "${targetPrefix}cctools";
43 src = fetchFromGitHub {
44 owner = "apple-oss-distributions";
46 rev = "cctools-${finalAttrs.version}";
47 hash = "sha256-JiKCP6U+xxR4mk4TXWv/mEo9Idg+QQqUYmB/EeRksCE=";
50 xcodeHash = "sha256-5RBbGrz1UKV0wt2Uk7RIHdfgWH8sgw/jy7hfTVrtVuM=";
55 # Verify that the Xcode project has not changed unexpectedly.
56 hashType=$(echo $xcodeHash | cut -d- -f1)
57 expectedHash=$(echo $xcodeHash | cut -d- -f2)
58 hash=$(openssl "$hashType" -binary "$sourceRoot/cctools.xcodeproj/project.pbxproj" | base64)
60 if [ "$hash" != "$expectedHash" ]; then
61 echo 'error: hash mismatch in cctools.xcodeproj/project.pbxproj'
62 echo " specified: $xcodeHash"
63 echo " got: $hashType-$hash"
65 echo 'Upstream Xcode project has changed. Update `meson.build` with any changes, then update `xcodeHash`.'
66 echo 'Use `nix-hash --flat --sri --type sha256 cctools.xcodeproj/project.pbxproj` to regenerate it.'
72 # Fix compile errors in redo_prebinding.c
73 ./0001-Fix-build-issues-with-misc-redo_prebinding.c.patch
74 # Use libcd_is_blob_a_linker_signature as defined in the libcodedirectory.h header
75 ./0002-Rely-on-libcd_is_blob_a_linker_signature.patch
76 # cctools uses availability checks for `utimensat`, but it checks the wrong version.
77 # Also, provide a definition to avoid implicit function definition errors.
78 ./0003-Fix-utimensat-compatability-with-the-10.12-SDK.patch
79 # Use the nixpkgs clang’s path as the prefix.
80 ./0004-Use-nixpkgs-clang-with-the-assembler-driver.patch
81 # Make sure cctools can find ld64 in the store
82 ./0005-Find-ld64-in-the-store.patch
83 # `ranlib` is a symlink to `libtool`. Make sure its detection works when it is used in cross-compilation.
84 ./0006-Support-target-prefixes-in-ranlib-detection.patch
88 substitute ${./meson.build} meson.build \
90 cp ${./meson.options} meson.options
92 # Make sure as’s clang driver uses clang from nixpkgs and finds the drivers in the store.
93 substituteInPlace as/driver.c \
94 --subst-var-by clang-unwrapped '${lib.getBin buildPackages.clang.cc}' \
95 --subst-var-by gas '${placeholder "gas"}'
97 # Need to set the path to make sure cctools can find ld64 in the store.
98 substituteInPlace libstuff/execute.c \
99 --subst-var-by ld64_path '${lib.getBin ld64}/bin/ld'
101 # Set the target prefix for `ranlib`
102 substituteInPlace misc/libtool.c \
103 --subst-var-by targetPrefix '${targetPrefix}'
105 # The version of this file distributed with cctools defines several CPU types missing from the 10.12 SDK.
106 ln -s machine-cctools.h include/mach/machine.h
108 # Use libxar from nixpkgs
109 for cctool_src in misc/nm.c otool/print_bitcode.c; do
110 substituteInPlace $cctool_src \
111 --replace-fail 'makestr(prefix, "../lib/libxar.dylib", NULL)' '"${lib.getLib xar}/lib/libxar.dylib"' \
112 --replace-fail '/usr/lib/libxar.dylib' '${lib.getLib xar}/lib/libxar.dylib'
115 # Use libLTO.dylib from nixpkgs LLVM
116 substituteInPlace libstuff/llvm.c \
117 --replace-fail 'getenv("LIBLTO_PATH")' '"${lib.getLib llvm}/lib/libLTO.dylib"'
119 cp ../xnu-src/EXTERNAL_HEADERS/mach-o/fixup-chains.h include/mach-o/fixup-chains.h
124 nativeBuildInputs = [
133 ] ++ lib.optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64) [ memstreamHook ];
135 mesonBuildType = "release";
138 (lib.mesonOption "b_ndebug" "if-release")
139 ] ++ lib.optionals (targetPrefix != "") [ (lib.mesonOption "target_prefix" targetPrefix) ];
142 ln -s ${targetPrefix}libtool "$out/bin/${targetPrefix}ranlib"
143 ln -s nm-classic.1 "''${!outputMan}/share/man/man1/nm.1"
144 ln -s otool-classic.1 "''${!outputMan}/share/man/man1/otool.1"
145 ln -s size-classic.1 "''${!outputMan}/share/man/man1/size.1"
147 # Move GNU as to its own output to prevent it from being used accidentally.
148 moveToOutput bin/gas "$gas"
149 moveToOutput libexec "$gas"
150 for arch in arm i386 x86_64; do
151 mv "$gas/libexec/as/$arch/as-$arch" "$gas/libexec/as/$arch/as"
154 # Move libtool to its own output to allow packages to add it without pulling in all of cctools
155 moveToOutput bin/${targetPrefix}libtool "$libtool"
156 ln -s "$libtool/bin/${targetPrefix}libtool" "$out/bin/${targetPrefix}libtool"
159 __structuredAttrs = true;
162 inherit targetPrefix;
163 updateScript = gitUpdater { rev-prefix = "cctools-"; };
167 description = "The classic linker for Darwin";
168 homepage = "https://opensource.apple.com/releases/";
169 license = with lib.licenses; [
173 maintainers = lib.teams.darwin.members;
174 platforms = lib.platforms.darwin;