18 # The targetPrefix is prepended to binary names to allow multiple binuntils on the PATH to both be usable.
19 targetPrefix = lib.optionalString (
20 stdenv.targetPlatform != stdenv.hostPlatform
21 ) "${stdenv.targetPlatform.config}-";
23 # First version with all the required files
24 xnu = fetchFromGitHub {
26 owner = "apple-oss-distributions";
28 rev = "xnu-7195.50.7.100.1";
29 hash = "sha256-uHmAOm6k9ZXWfyqHiDSpm+tZqUbERlr6rXSJ4xNACkM=";
32 stdenv.mkDerivation (finalAttrs: {
33 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 = [
135 ++ lib.optionals stdenv.hostPlatform.isDarwin [ darwin.objc4 ]
136 ++ lib.optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64) [ memstreamHook ];
138 mesonBuildType = "release";
141 (lib.mesonOption "b_ndebug" "if-release")
142 ] ++ lib.optionals (targetPrefix != "") [ (lib.mesonOption "target_prefix" targetPrefix) ];
145 ln -s ${targetPrefix}libtool "$out/bin/${targetPrefix}ranlib"
146 ln -s nm-classic.1 "''${!outputMan}/share/man/man1/nm.1"
147 ln -s otool-classic.1 "''${!outputMan}/share/man/man1/otool.1"
148 ln -s size-classic.1 "''${!outputMan}/share/man/man1/size.1"
150 # Move GNU as to its own output to prevent it from being used accidentally.
151 moveToOutput bin/gas "$gas"
152 moveToOutput libexec "$gas"
153 for arch in arm i386 x86_64; do
154 mv "$gas/libexec/as/$arch/as-$arch" "$gas/libexec/as/$arch/as"
158 __structuredAttrs = true;
161 inherit targetPrefix;
162 updateScript = gitUpdater { rev-prefix = "cctools-"; };
166 description = "The classic linker for Darwin";
167 homepage = "https://opensource.apple.com/releases/";
168 license = with lib.licenses; [
172 maintainers = with lib.maintainers; [ reckenrode ];
173 platforms = lib.platforms.darwin;