nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib, stdenv, llvm_meta
2, monorepoSrc, runCommand
3, cmake, ninja, python3, fixDarwinDylibNames, version
4, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else libcxxabi
5, libcxxabi, libcxxrt
6, enableShared ? !stdenv.hostPlatform.isStatic
7
8# If headersOnly is true, the resulting package would only include the headers.
9# Use this to break the circular dependency between libcxx and libcxxabi.
10#
11# Some context:
12# https://reviews.llvm.org/rG1687f2bbe2e2aaa092f942d4a97d41fad43eedfb
13, headersOnly ? false
14}:
15
16let
17 basename = "libcxx";
18in
19
20assert stdenv.isDarwin -> cxxabi.libName == "c++abi";
21
22stdenv.mkDerivation rec {
23 pname = basename + lib.optionalString headersOnly "-headers";
24 inherit version;
25
26 src = runCommand "${pname}-src-${version}" {} ''
27 mkdir -p "$out"
28 cp -r ${monorepoSrc}/cmake "$out"
29 cp -r ${monorepoSrc}/${basename} "$out"
30 mkdir -p "$out/libcxxabi"
31 cp -r ${monorepoSrc}/libcxxabi/include "$out/libcxxabi"
32 mkdir -p "$out/llvm"
33 cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
34 cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
35 cp -r ${monorepoSrc}/third-party "$out"
36 cp -r ${monorepoSrc}/runtimes "$out"
37 '';
38
39 sourceRoot = "${src.name}/runtimes";
40
41 outputs = [ "out" ] ++ lib.optional (!headersOnly) "dev";
42
43 prePatch = ''
44 cd ../${basename}
45 chmod -R u+w .
46 '';
47
48 patches = [
49 ./gnu-install-dirs.patch
50 ] ++ lib.optionals stdenv.hostPlatform.isMusl [
51 ../../libcxx-0001-musl-hacks.patch
52 ];
53
54 postPatch = ''
55 cd ../runtimes
56 '';
57
58 preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
59 patchShebangs utils/cat_files.py
60 '';
61
62 nativeBuildInputs = [ cmake ninja python3 ]
63 ++ lib.optional stdenv.isDarwin fixDarwinDylibNames;
64
65 buildInputs = lib.optionals (!headersOnly) [ cxxabi ];
66
67 cmakeFlags = let
68 # See: https://libcxx.llvm.org/BuildingLibcxx.html#cmdoption-arg-libcxx-cxx-abi-string
69 libcxx_cxx_abi_opt = {
70 "c++abi" = "system-libcxxabi";
71 "cxxrt" = "libcxxrt";
72 }.${cxxabi.libName} or (throw "unknown cxxabi: ${cxxabi.libName} (${cxxabi.pname})");
73 in [
74 "-DLLVM_ENABLE_RUNTIMES=libcxx"
75 "-DLIBCXX_CXX_ABI=${if headersOnly then "none" else libcxx_cxx_abi_opt}"
76 ] ++ lib.optional (!headersOnly && cxxabi.libName == "c++abi") "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${cxxabi.dev}/include/c++/v1"
77 ++ lib.optional (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) "-DLIBCXX_HAS_MUSL_LIBC=1"
78 ++ lib.optional (stdenv.hostPlatform.useLLVM or false) "-DLIBCXX_USE_COMPILER_RT=ON"
79 ++ lib.optionals stdenv.hostPlatform.isWasm [
80 "-DLIBCXX_ENABLE_THREADS=OFF"
81 "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
82 "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
83 ] ++ lib.optional (!enableShared) "-DLIBCXX_ENABLE_SHARED=OFF"
84 # If we're only building the headers we don't actually *need* a functioning
85 # C/C++ compiler:
86 ++ lib.optionals (headersOnly) [
87 "-DCMAKE_C_COMPILER_WORKS=ON"
88 "-DCMAKE_CXX_COMPILER_WORKS=ON"
89 ];
90
91 ninjaFlags = lib.optional headersOnly "generate-cxx-headers";
92 installTargets = lib.optional headersOnly "install-cxx-headers";
93
94 passthru = {
95 isLLVM = true;
96 inherit cxxabi;
97 };
98
99 meta = llvm_meta // {
100 homepage = "https://libcxx.llvm.org/";
101 description = "C++ standard library";
102 longDescription = ''
103 libc++ is an implementation of the C++ standard library, targeting C++11,
104 C++14 and above.
105 '';
106 # "All of the code in libc++ is dual licensed under the MIT license and the
107 # UIUC License (a BSD-like license)":
108 license = with lib.licenses; [ mit ncsa ];
109 };
110}