base16-schemes: unstable-2024-06-21 -> unstable-2024-11-12
[NixPkgs.git] / pkgs / by-name / li / libtapi / package.nix
blob0e4f3db7611ea68e27ed27f94cf9b3244b8c1525
2   lib,
3   stdenv,
4   fetchFromGitHub,
5   fetchpatch,
6   cmake,
7   ninja,
8   python3,
9   zlib,
12 let
13   # libtapi is only supported building against Apple’s LLVM fork pinned to a specific revision.
14   # It can’t be built against upstream LLVM because it uses APIs that are specific to Apple’s fork.
15   # See: https://github.com/apple-oss-distributions/tapi/blob/main/Readme.md
17   # Apple’s LLVM fork uses its own versioning scheme.
18   # See: https://en.wikipedia.org/wiki/Xcode#Toolchain_versions
19   # Note: Can’t use a sparse checkout because the Darwin stdenv bootstrap can’t depend on fetchgit.
20   appleLlvm = {
21     version = "15.0.0"; # As reported by upstream’s `tapi --version`.
22     rev = "2b5ff47e44b059c03de5779479d01a133ab4d581"; # Per the TAPI repo.
23     hash = "sha256-X37zBbpSEWmqtdTXsd1t++gp+0ggA8YtB73fGKNaiR0=";
24   };
26 stdenv.mkDerivation (finalAttrs: {
27   pname = "libtapi";
28   version = "1500.0.12.3";
30   outputs = [
31     "out"
32     "bin"
33     "dev"
34   ];
36   srcs = [
37     (fetchFromGitHub {
38       name = "tapi-src";
39       owner = "apple-oss-distributions";
40       repo = "tapi";
41       rev = "tapi-${finalAttrs.version}";
42       hash = "sha256-YeaA2OeSY1fXYJHPJJ0TrVC1brspSvutBtPMPGX6Y1o=";
43     })
44     # libtapi can’t avoid pulling the whole repo even though it needs only a couple of folders because
45     # `fetchgit` can’t be used in the Darwin bootstrap.
46     (fetchFromGitHub {
47       name = "apple-llvm-src";
48       owner = "apple";
49       repo = "llvm-project";
50       inherit (appleLlvm) rev hash;
51     })
52   ];
54   patches = [
55     # Older versions of ld64 may not support `-no_exported_symbols`, so use it only
56     # when the linker supports it.
57     # Note: This can be dropped once the bootstrap tools are updated after the ld64 update.
58     ./0001-Check-for-no_exported_symbols-linker-support.patch
59     # The recommended upstream revision of Apple’s LLVM fork needs this patch, or
60     # `tapi stubify` will crash when generating stubs.
61     (fetchpatch {
62       url = "https://github.com/apple/llvm-project/commit/455bf3d1ccd6a52df5e38103532c1b8f49924edc.patch";
63       hash = "sha256-ujZcfdAls20JPIvjvO2Xv8st8cNTY/XTEQusICKBKSA";
64     })
65     # Updates `JSONReaderWriter` to work with the API change in the above patch.
66     ./0002-Pass-fileType-to-writeToStream.patch
67     # Fix build on Linux. GCC is more picky than clang about the field order.
68     ./0003-Match-designator-order-with-declaration-order.patch
69   ];
71   postPatch =
72     ''
73       # Enable building on non-Darwin platforms
74       substituteInPlace tapi/CMakeLists.txt \
75         --replace-fail 'message(FATAL_ERROR "Unsupported configuration.")' ""
77       # Remove the client limitation on linking to libtapi.dylib.
78       substituteInPlace tapi/tools/libtapi/CMakeLists.txt \
79         --replace-fail '-allowable_client ld' ""
80       # Replace hard-coded installation paths with standard ones.
81       declare -A installdirs=(
82         [bin]=BINDIR
83         [include]=INCLUDEDIR
84         [lib]=LIBDIR
85         [local/bin]=BINDIR
86         [local/share/man]=MANDIR
87         [share/man]=MANDIR
88       )
89       for dir in "''${!installdirs[@]}"; do
90         cmakevar=CMAKE_INSTALL_''${installdirs[$dir]}
91         for cmakelist in $(grep -rl "DESTINATION $dir" tapi); do
92           substituteInPlace "$cmakelist" \
93             --replace-fail "DESTINATION $dir" "DESTINATION \''${$cmakevar}"
94         done
95       done
96       # Doesn’t seem to exist publically.
97       substituteInPlace tapi/test/CMakeLists.txt \
98         --replace-fail tapi-configs ""
99     ''
100     + lib.optionalString stdenv.hostPlatform.isLinux ''
101       # Remove Darwin-specific versioning flags.
102       substituteInPlace tapi/tools/libtapi/CMakeLists.txt \
103           --replace-fail '-current_version ''${DYLIB_VERSION} -compatibility_version 1' ""
104     '';
106   preUnpack = ''
107     mkdir source
108   '';
110   sourceRoot = "source";
112   postUnpack = ''
113     chmod -R u+w apple-llvm-src tapi-src
114     mv apple-llvm-src/{clang,cmake,llvm,utils} source
115     mv tapi-src source/tapi
116   '';
118   strictDeps = true;
120   buildInputs = [ zlib ]; # Upstream links against zlib in their distribution.
122   nativeBuildInputs = [
123     cmake
124     ninja
125     python3
126   ];
128   cmakeDir = "../llvm";
130   cmakeFlags = [
131     (lib.cmakeFeature "LLVM_ENABLE_PROJECTS" "clang;tapi")
132     (lib.cmakeFeature "LLVM_EXTERNAL_PROJECTS" "tapi")
133     (lib.cmakeBool "TAPI_INCLUDE_DOCS" true)
134     # Matches the version string format reported by upstream `tapi`.
135     (lib.cmakeFeature "TAPI_REPOSITORY_STRING" "tapi-${finalAttrs.version}")
136     (lib.cmakeFeature "TAPI_FULL_VERSION" appleLlvm.version)
137     # Match the versioning used by Apple’s LLVM fork (primarily used for .so versioning).
138     (lib.cmakeFeature "LLVM_VERSION_MAJOR" (lib.versions.major appleLlvm.version))
139     (lib.cmakeFeature "LLVM_VERSION_MINOR" (lib.versions.minor appleLlvm.version))
140     (lib.cmakeFeature "LLVM_VERSION_PATCH" (lib.versions.patch appleLlvm.version))
141     (lib.cmakeFeature "LLVM_VERSION_SUFFIX" "")
142     # Upstream `tapi` does not link against ncurses. Disable it explicitly to make sure
143     # it is not detected incorrectly from the bootstrap tools tarball.
144     (lib.cmakeBool "LLVM_ENABLE_TERMINFO" false)
145     # Disabling the benchmarks avoids a failure during the configure phase because
146     # the sparse checkout does not include the benchmarks.
147     (lib.cmakeBool "LLVM_INCLUDE_BENCHMARKS" false)
148     # tapi’s tests expect to target macOS 13.0 and build both x86_64 and universal
149     # binaries regardless of the host platform.
150     (lib.cmakeBool "LLVM_INCLUDE_TESTS" false)
151     (lib.cmakeBool "TAPI_INCLUDE_TESTS" false)
152   ];
154   ninjaFlags = [
155     "libtapi"
156     "tapi-sdkdb"
157     "tapi"
158   ];
160   installTargets = [
161     "install-libtapi"
162     "install-tapi-docs"
163     "install-tapi-headers"
164     "install-tapi-sdkdb"
165     "install-tapi"
166   ];
168   postInstall = ''
169     # The man page is installed for these, but they’re not included in the source release.
170     rm $bin/share/man/man1/tapi-analyze.1 $bin/share/man/man1/tapi-api-verify.1
171   '';
173   meta = {
174     description = "Replaces the Mach-O Dynamic Library Stub files in Apple's SDKs to reduce the size";
175     homepage = "https://github.com/apple-oss-distributions/tapi/";
176     license = lib.licenses.ncsa;
177     mainProgram = "tapi";
178     maintainers = with lib.maintainers; [
179       matthewbauer
180       reckenrode
181     ];
182     platforms = lib.platforms.unix;
183   };