serene/nix/libcxxabi/default.nix

130 lines
4.0 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,
cmake,
ninja,
python3,
runCommand,
fetchpatch,
cxx-headers,
llvm_libunwind,
utils,
llvm_source,
}:
stdenv.mkDerivation rec {
pname = "llvm_libcxxabi";
inherit (llvm_source) version;
src = runCommand "${pname}-src-${version}" {} ''
mkdir -p "$out"
cp -r ${llvm_source}/cmake "$out"
cp -r ${llvm_source}/libcxxabi "$out"
mkdir -p "$out/libcxx/src"
cp -r ${llvm_source}/libcxx/cmake "$out/libcxx"
cp -r ${llvm_source}/libcxx/include "$out/libcxx"
cp -r ${llvm_source}/libcxx/src/include "$out/libcxx/src"
mkdir -p "$out/llvm"
cp -r ${llvm_source}/llvm/cmake "$out/llvm"
cp -r ${llvm_source}/llvm/utils "$out/llvm"
cp -r ${llvm_source}/runtimes "$out"
'';
sourceRoot = "${src.name}/runtimes";
outputs = [ "out" "dev" ];
postUnpack = lib.optionalString stdenv.isDarwin ''
export TRIPLE=x86_64-apple-darwin
'';
prePatch = ''
cd ../libcxxabi
chmod -R u+w .
'';
patches = [
./gnu-install-dirs.patch
# Patch to allow libcxxabi standalone installation
# `LIBCXXABI_LIBCXX_INCLUDES` is not doing anything in the
# vanilla source from the llvm-project, and it will be removed
# in the future.
./standalone.patch
#./test.patch
];
postPatch = ''
cd ../runtimes
'';
nativeBuildInputs = [ cmake ninja python3 ];
buildInputs = [ llvm_libunwind ];
ninjaFlags = [ "-v" ];
cmakeFlags = [
"-DLLVM_ENABLE_RUNTIMES=libcxxabi"
"-DLIBCXXABI_LIBCXX_INCLUDES=${cxx-headers}/include/c++/v1"
# `libcxxabi`'s build does not need a toolchain with a c++ stdlib attached
# (we specify the headers it should use explicitly above).
#
# CMake however checks for this anyways; this flag tells it not to. See:
# https://github.com/llvm/llvm-project/blob/4bd3f3759259548e159aeba5c76efb9a0864e6fa/llvm/runtimes/CMakeLists.txt#L243
"-DCMAKE_CXX_COMPILER_WORKS=ON"
] ++ lib.optionals (!stdenv.hostPlatform.isWasm) [
"-DLLVM_ENABLE_LIBCXX=ON"
"-DLIBCXXABI_USE_LLVM_UNWINDER=ON"
# libcxxabi's CMake looks as though it treats -nostdlib++ as implying -nostdlib,
# but that does not appear to be the case for example when building
# pkgsLLVM.libcxxabi (which uses clangNoCompilerRtWithLibc).
"-DCMAKE_EXE_LINKER_FLAGS=-nostdlib"
"-DCMAKE_SHARED_LINKER_FLAGS=-nostdlib"
"-DLIBCXXABI_USE_COMPILER_RT=ON"
] ++ lib.optionals stdenv.hostPlatform.isWasm [
"-DCMAKE_C_COMPILER_WORKS=ON"
"-DCMAKE_CXX_COMPILER_WORKS=ON"
"-DLIBCXXABI_ENABLE_THREADS=OFF"
"-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
"-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
# We care only about the static libcxxabi
"-DLIBCXXABI_ENABLE_SHARED=OFF"
];
preInstall = lib.optionalString stdenv.isDarwin ''
for file in lib/*.dylib; do
# this should be done in CMake, but having trouble figuring out
# the magic combination of necessary CMake variables
# if you fancy a try, take a look at
# https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling
install_name_tool -id $out/$file $file
done
'';
postInstall = ''
mkdir -p "$dev/include"
install -m 644 ../../libcxxabi/include/${if stdenv.isDarwin then "*" else "cxxabi.h"} "$dev/include"
'';
passthru = {
libName = "c++abi";
};
inherit (utils) meta;
}