nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib, stdenv, llvm_meta, fetch, cmake, python3, fixDarwinDylibNames, version
2, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else libcxxabi
3, libcxxabi, libcxxrt
4, enableShared ? !stdenv.hostPlatform.isStatic
5}:
6
7assert stdenv.isDarwin -> cxxabi.pname == "libcxxabi";
8
9stdenv.mkDerivation {
10 pname = "libcxx";
11 inherit version;
12
13 src = fetch "libcxx" "0y4vc9z36c1zlq15cnibdzxnc1xi5glbc6klnm8a41q3db4541kz";
14
15 postUnpack = ''
16 unpackFile ${libcxxabi.src}
17 export LIBCXXABI_INCLUDE_DIR="$PWD/$(ls -d libcxxabi-${version}*)/include"
18 '';
19
20 outputs = [ "out" "dev" ];
21
22 patches = [
23 ./gnu-install-dirs.patch
24 ] ++ lib.optionals stdenv.hostPlatform.isMusl [
25 ../../libcxx-0001-musl-hacks.patch
26 ];
27
28 # Prevent errors like "error: 'foo' is unavailable: introduced in macOS yy.zz"
29 postPatch = ''
30 substituteInPlace include/__config \
31 --replace "# define _LIBCPP_USE_AVAILABILITY_APPLE" ""
32 '';
33
34 prePatch = ''
35 substituteInPlace lib/CMakeLists.txt --replace "/usr/lib/libc++" "\''${LIBCXX_LIBCXXABI_LIB_PATH}/libc++"
36 '';
37
38 preConfigure = ''
39 # Get headers from the cxxabi source so we can see private headers not installed by the cxxabi package
40 cmakeFlagsArray=($cmakeFlagsArray -DLIBCXX_CXX_ABI_INCLUDE_PATHS="$LIBCXXABI_INCLUDE_DIR")
41 '' + lib.optionalString stdenv.hostPlatform.isMusl ''
42 patchShebangs utils/cat_files.py
43 '';
44 nativeBuildInputs = [ cmake ]
45 ++ lib.optional (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) python3
46 ++ lib.optional stdenv.hostPlatform.isDarwin fixDarwinDylibNames;
47
48 buildInputs = [ cxxabi ];
49
50 cmakeFlags = [
51 "-DLIBCXX_LIBCPPABI_VERSION=2"
52 "-DLIBCXX_CXX_ABI=${cxxabi.pname}"
53 ] ++ lib.optional (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) "-DLIBCXX_HAS_MUSL_LIBC=1"
54 ++ lib.optional (cxxabi.pname == "libcxxabi") "-DLIBCXX_LIBCXXABI_LIB_PATH=${cxxabi}/lib"
55 ++ lib.optional (stdenv.hostPlatform.useLLVM or false) "-DLIBCXX_USE_COMPILER_RT=ON"
56 ++ lib.optionals stdenv.hostPlatform.isWasm [
57 "-DLIBCXX_ENABLE_THREADS=OFF"
58 "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
59 "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
60 ] ++ lib.optional (!enableShared) "-DLIBCXX_ENABLE_SHARED=OFF";
61
62 preInstall = lib.optionalString (stdenv.isDarwin) ''
63 for file in lib/*.dylib; do
64 if [ -L "$file" ]; then continue; fi
65
66 baseName=$(basename $(${stdenv.cc.targetPrefix}otool -D $file | tail -n 1))
67 installName="$out/lib/$baseName"
68 abiName=$(echo "$baseName" | sed -e 's/libc++/libc++abi/')
69
70 for other in $(${stdenv.cc.targetPrefix}otool -L $file | awk '$1 ~ "/libc\\+\\+abi" { print $1 }'); do
71 ${stdenv.cc.targetPrefix}install_name_tool -change $other ${cxxabi}/lib/$abiName $file
72 done
73 done
74 '';
75
76 passthru = {
77 isLLVM = true;
78 inherit cxxabi;
79 };
80
81 meta = llvm_meta // {
82 homepage = "https://libcxx.llvm.org/";
83 description = "C++ standard library";
84 longDescription = ''
85 libc++ is an implementation of the C++ standard library, targeting C++11,
86 C++14 and above.
87 '';
88 # "All of the code in libc++ is dual licensed under the MIT license and the
89 # UIUC License (a BSD-like license)":
90 license = with lib.licenses; [ mit ncsa ];
91 };
92}