···11-{ lib
22-, stdenv
33-, python3
11+{ cmake
22+, fetchFromGitHub
33+, fetchgit
44+, git
55+, lib
46, libffi
55-, git
66-, cmake
77-, zlib
88-, fetchgit
99-, fetchFromGitHub
77+, llvmPackages_9
108, makeWrapper
99+, ncurses
1010+, python3
1111, runCommand
1212-, llvmPackages_9
1313-, glibc
1414-, ncurses
1212+, zlib
1313+1414+# *NOT* from LLVM 9!
1515+# The compiler used to compile Cling may affect the runtime include and lib
1616+# directories it expects to be run with. Cling builds against (a fork of) Clang,
1717+# so we prefer to use Clang as the compiler as well for consistency.
1818+# It would be cleanest to use LLVM 9's clang, but it errors. So, we use a later
1919+# version of Clang to compile, but we check out the Cling fork of Clang 9 to
2020+# build Cling against.
2121+, clangStdenv
2222+2323+# For runtime C++ standard library
2424+, gcc-unwrapped
2525+2626+# Build with debug symbols
2727+, debug ? false
2828+2929+# Build with libc++ (LLVM) rather than stdlibc++ (GCC).
3030+# This is experimental and not all features work.
3131+, useLLVMLibcxx ? false
1532}:
16331734let
3535+ stdenv = clangStdenv;
3636+1837 # The LLVM 9 headers have a couple bugs we need to patch
1938 fixedLlvmDev = runCommand "llvm-dev-${llvmPackages_9.llvm.version}" { buildInputs = [git]; } ''
2039 mkdir $out
···5877 ];
59786079 nativeBuildInputs = [ python3 git cmake ];
6161- buildInputs = [ libffi zlib ncurses ];
8080+ buildInputs = [ libffi ncurses zlib ];
62816382 strictDeps = true;
6483···6988 "-DLLVM_MAIN_INCLUDE_DIR=${fixedLlvmDev}/include"
7089 "-DLLVM_TABLEGEN_EXE=${llvmPackages_9.llvm.out}/bin/llvm-tblgen"
7190 "-DLLVM_TOOLS_BINARY_DIR=${llvmPackages_9.llvm.out}/bin"
9191+ "-DLLVM_BUILD_TOOLS=Off"
7292 "-DLLVM_TOOL_CLING_BUILD=ON"
73937494 "-DLLVM_TARGETS_TO_BUILD=host;NVPTX"
···7898 # see cling/tools/CMakeLists.txt
7999 "-DCLING_INCLUDE_TESTS=ON"
80100 "-DCLANG-TOOLS=OFF"
8181- # "--trace-expand"
101101+ ] ++ lib.optionals debug [
102102+ "-DCMAKE_BUILD_TYPE=Debug"
103103+ ] ++ lib.optionals useLLVMLibcxx [
104104+ "-DLLVM_ENABLE_LIBCXX=ON"
105105+ "-DLLVM_ENABLE_LIBCXXABI=ON"
82106 ];
83107108108+ CPPFLAGS = if useLLVMLibcxx then [ "-stdlib=libc++" ] else [];
109109+84110 postInstall = lib.optionalString (!stdenv.isDarwin) ''
85111 mkdir -p $out/share/Jupyter
86112 cp -r /build/clang/tools/cling/tools/Jupyter/kernel $out/share/Jupyter
87113 '';
114114+115115+ dontStrip = debug;
8811689117 meta = with lib; {
90118 description = "The Interactive C++ Interpreter";
···94122 platforms = platforms.unix;
95123 };
96124 };
125125+126126+ # Runtime flags for the C++ standard library
127127+ cxxFlags = if useLLVMLibcxx then [
128128+ "-I" "${lib.getDev llvmPackages_9.libcxx}/include/c++/v1"
129129+ "-L" "${llvmPackages_9.libcxx}/lib"
130130+ "-l" "${llvmPackages_9.libcxx}/lib/libc++.so"
131131+ ] else [
132132+ "-I" "${gcc-unwrapped}/include/c++/${gcc-unwrapped.version}"
133133+ "-I" "${gcc-unwrapped}/include/c++/${gcc-unwrapped.version}/x86_64-unknown-linux-gnu"
134134+ ];
9713598136 # The flags passed to the wrapped cling should
99137 # a) prevent it from searching for system include files and libs, and
100100- # b) provide it with the include files and libs it needs (C and C++ standard library)
138138+ # b) provide it with the include files and libs it needs (C and C++ standard library plus
139139+ # its own stuff)
101140102102- # These are also exposed as cling.flags/cling.compilerIncludeFlags because it's handy to be
103103- # able to pass them to tools that wrap Cling, particularly Jupyter kernels such as xeus-cling
104104- # and the built-in jupyter-cling-kernel. Both of these use Cling as a library by linking against
105105- # libclingJupyter.so, so the makeWrapper approach to wrapping the binary doesn't work.
141141+ # These are also exposed as cling.flags because it's handy to be able to pass them to tools
142142+ # that wrap Cling, particularly Jupyter kernels such as xeus-cling and the built-in
143143+ # jupyter-cling-kernel, which use Cling as a library.
106144 # Thus, if you're packaging a Jupyter kernel, you either need to pass these flags as extra
107145 # args to xcpp (for xeus-cling) or put them in the environment variable CLING_OPTS
108108- # (for jupyter-cling-kernel)
146146+ # (for jupyter-cling-kernel).
109147 flags = [
110148 "-nostdinc"
111149 "-nostdinc++"
150150+151151+ "-isystem" "${lib.getLib unwrapped}/lib/clang/9.0.1/include"
152152+ ]
153153+ ++ cxxFlags
154154+ ++ [
155155+ # System libc
112156 "-isystem" "${lib.getDev stdenv.cc.libc}/include"
113113- "-I" "${lib.getDev unwrapped}/include"
114114- "-I" "${lib.getLib unwrapped}/lib/clang/9.0.1/include"
157157+158158+ # cling includes
159159+ "-isystem" "${lib.getDev unwrapped}/include"
115160 ];
116161117117- # Autodetect the include paths for the compiler used to build Cling, in the same way Cling does at
118118- # https://github.com/root-project/cling/blob/v0.7/lib/Interpreter/CIFactory.cpp#L107:L111
119119- # Note: it would be nice to just put the compiler in Cling's PATH and let it do this by itself, but
120120- # unfortunately passing -nostdinc/-nostdinc++ disables Cling's autodetection logic.
121121- compilerIncludeFlags = runCommand "compiler-include-flags.txt" {} ''
122122- export LC_ALL=C
123123- ${stdenv.cc}/bin/c++ -xc++ -E -v /dev/null 2>&1 | sed -n -e '/^.include/,''${' -e '/^ \/.*++/p' -e '}' > tmp
124124- sed -e 's/^/-isystem /' -i tmp
125125- tr '\n' ' ' < tmp > $out
126126- '';
127127-128162in
129163130164runCommand "cling-${unwrapped.version}" {
131165 nativeBuildInputs = [ makeWrapper ];
132132- inherit unwrapped flags compilerIncludeFlags;
166166+ inherit unwrapped flags;
133167 inherit (unwrapped) meta;
134168} ''
135169 makeWrapper $unwrapped/bin/cling $out/bin/cling \
136136- --add-flags "$(cat "$compilerIncludeFlags")" \
137170 --add-flags "$flags"
138171''