serene/nix/libcxx/default.nix

129 lines
4.2 KiB
Nix

# Serene Programming Language
#
# Copyright (c) 2019-2023 Sameer Rahmani <lxsameer@gnu.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
{ lib,
stdenv,
runCommand,
cmake,
ninja,
python3,
fixDarwinDylibNames,
#cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else llvm_libcxxabi,
#libcxxrt,
llvm_libcxxabi,
llvm_libunwind,
llvm_source,
utils,
# If headersOnly is true, the resulting package would only include the headers.
# Use this to break the circular dependency between libcxx and libcxxabi.
#
# Some context:
# https://reviews.llvm.org/rG1687f2bbe2e2aaa092f942d4a97d41fad43eedfb
headersOnly ? false
}:
let
basename = "llvm_libcxx";
in
assert stdenv.isDarwin -> llvm_libcxxabi.libName == "c++abi";
stdenv.mkDerivation rec {
pname = basename + lib.optionalString headersOnly "-headers";
inherit (llvm_source) version;
src = runCommand "${pname}-src-${version}" {} ''
mkdir -p "$out"
cp -r ${llvm_source}/cmake "$out"
cp -r ${llvm_source}/libcxx "$out"
mkdir -p "$out/libcxxabi"
cp -r ${llvm_source}/libcxxabi/include "$out/libcxxabi"
mkdir -p "$out/llvm"
cp -r ${llvm_source}/llvm/cmake "$out/llvm"
cp -r ${llvm_source}/llvm/utils "$out/llvm"
cp -r ${llvm_source}/third-party "$out"
cp -r ${llvm_source}/runtimes "$out"
'';
sourceRoot = "${src.name}/runtimes";
outputs = [ "out" ] ++ lib.optional (!headersOnly) "dev";
prePatch = ''
cd ../libcxx
chmod -R u+w .
'';
patches = [
./gnu-install-dirs.patch
];
postPatch = ''
cd ../runtimes
'';
preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
patchShebangs utils/cat_files.py
'';
nativeBuildInputs = [ cmake ninja python3 ]
++ lib.optional stdenv.isDarwin fixDarwinDylibNames;
buildInputs =
lib.optionals (!headersOnly) [ llvm_libcxxabi ]
++ lib.optionals (!stdenv.hostPlatform.isWasm) [ llvm_libunwind ];
cmakeFlags = let
# See: https://libcxx.llvm.org/BuildingLibcxx.html#cmdoption-arg-libcxx-cxx-abi-string
libcxx_cxx_abi_opt = {
"c++abi" = "system-libcxxabi";
"cxxrt" = "libcxxrt";
}.${llvm_libcxxabi.libName} or (throw "unknown cxxabi: ${llvm_libcxxabi.libName} (${llvm_libcxxabi.pname})");
in [
"-DLLVM_ENABLE_RUNTIMES=libcxx"
"-DLIBCXX_CXX_ABI=${if headersOnly then "none" else libcxx_cxx_abi_opt}"
"-DLIBCXX_ENABLE_SHARED=OFF"
"-DLIBCXX_USE_COMPILER_RT=ON"
# There's precedent for this in llvm-project/libcxx/cmake/caches.
# In a monorepo build you might do the following in the libcxxabi build:
# -DLLVM_ENABLE_PROJECTS=libcxxabi;libunwinder
# -DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY=On
# libcxx appears to require unwind and doesn't pull it in via other means.
"-DLIBCXX_ADDITIONAL_LIBRARIES=unwind"
] ++ lib.optional (!headersOnly && llvm_libcxxabi.libName == "c++abi")
"-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${llvm_libcxxabi.dev}/include/c++/v1"
++ lib.optional (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
"-DLIBCXX_HAS_MUSL_LIBC=1"
] ++ lib.optionals stdenv.hostPlatform.isWasm [
"-DLIBCXX_ENABLE_THREADS=OFF"
"-DLIBCXX_ENABLE_FILESYSTEM=OFF"
"-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
"-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
] ++ lib.optionals (headersOnly) [
# If we're only building the headers we don't actually *need* a functioning
# C/C++ compiler:
"-DCMAKE_C_COMPILER_WORKS=ON"
"-DCMAKE_CXX_COMPILER_WORKS=ON"
];
ninjaFlags = lib.optional headersOnly "generate-cxx-headers";
installTargets = lib.optional headersOnly "install-cxx-headers";
passthru = {
isLLVM = true;
inherit llvm_libcxxabi;
};
inherit (utils) meta;
}