···1-{ lib
2-, stdenv
3-, python3
004, libffi
5-, git
6-, cmake
7-, zlib
8-, fetchgit
9-, fetchFromGitHub
10, makeWrapper
0011, runCommand
12-, llvmPackages_9
13-, glibc
14-, ncurses
0000000000000000015}:
1617let
0018 # The LLVM 9 headers have a couple bugs we need to patch
19 fixedLlvmDev = runCommand "llvm-dev-${llvmPackages_9.llvm.version}" { buildInputs = [git]; } ''
20 mkdir $out
···58 ];
5960 nativeBuildInputs = [ python3 git cmake ];
61- buildInputs = [ libffi zlib ncurses ];
6263 strictDeps = true;
64···69 "-DLLVM_MAIN_INCLUDE_DIR=${fixedLlvmDev}/include"
70 "-DLLVM_TABLEGEN_EXE=${llvmPackages_9.llvm.out}/bin/llvm-tblgen"
71 "-DLLVM_TOOLS_BINARY_DIR=${llvmPackages_9.llvm.out}/bin"
072 "-DLLVM_TOOL_CLING_BUILD=ON"
7374 "-DLLVM_TARGETS_TO_BUILD=host;NVPTX"
···78 # see cling/tools/CMakeLists.txt
79 "-DCLING_INCLUDE_TESTS=ON"
80 "-DCLANG-TOOLS=OFF"
81- # "--trace-expand"
000082 ];
830084 postInstall = lib.optionalString (!stdenv.isDarwin) ''
85 mkdir -p $out/share/Jupyter
86 cp -r /build/clang/tools/cling/tools/Jupyter/kernel $out/share/Jupyter
87 '';
008889 meta = with lib; {
90 description = "The Interactive C++ Interpreter";
···94 platforms = platforms.unix;
95 };
96 };
00000000009798 # The flags passed to the wrapped cling should
99 # a) prevent it from searching for system include files and libs, and
100- # b) provide it with the include files and libs it needs (C and C++ standard library)
0101102- # These are also exposed as cling.flags/cling.compilerIncludeFlags because it's handy to be
103- # able to pass them to tools that wrap Cling, particularly Jupyter kernels such as xeus-cling
104- # and the built-in jupyter-cling-kernel. Both of these use Cling as a library by linking against
105- # libclingJupyter.so, so the makeWrapper approach to wrapping the binary doesn't work.
106 # Thus, if you're packaging a Jupyter kernel, you either need to pass these flags as extra
107 # args to xcpp (for xeus-cling) or put them in the environment variable CLING_OPTS
108- # (for jupyter-cling-kernel)
109 flags = [
110 "-nostdinc"
111 "-nostdinc++"
000000112 "-isystem" "${lib.getDev stdenv.cc.libc}/include"
113- "-I" "${lib.getDev unwrapped}/include"
114- "-I" "${lib.getLib unwrapped}/lib/clang/9.0.1/include"
0115 ];
116117- # Autodetect the include paths for the compiler used to build Cling, in the same way Cling does at
118- # https://github.com/root-project/cling/blob/v0.7/lib/Interpreter/CIFactory.cpp#L107:L111
119- # Note: it would be nice to just put the compiler in Cling's PATH and let it do this by itself, but
120- # unfortunately passing -nostdinc/-nostdinc++ disables Cling's autodetection logic.
121- compilerIncludeFlags = runCommand "compiler-include-flags.txt" {} ''
122- export LC_ALL=C
123- ${stdenv.cc}/bin/c++ -xc++ -E -v /dev/null 2>&1 | sed -n -e '/^.include/,''${' -e '/^ \/.*++/p' -e '}' > tmp
124- sed -e 's/^/-isystem /' -i tmp
125- tr '\n' ' ' < tmp > $out
126- '';
127-128in
129130runCommand "cling-${unwrapped.version}" {
131 nativeBuildInputs = [ makeWrapper ];
132- inherit unwrapped flags compilerIncludeFlags;
133 inherit (unwrapped) meta;
134} ''
135 makeWrapper $unwrapped/bin/cling $out/bin/cling \
136- --add-flags "$(cat "$compilerIncludeFlags")" \
137 --add-flags "$flags"
138''
···1+{ cmake
2+, fetchFromGitHub
3+, fetchgit
4+, git
5+, lib
6, libffi
7+, llvmPackages_9
00008, makeWrapper
9+, ncurses
10+, python3
11, runCommand
12+, zlib
13+14+# *NOT* from LLVM 9!
15+# The compiler used to compile Cling may affect the runtime include and lib
16+# directories it expects to be run with. Cling builds against (a fork of) Clang,
17+# so we prefer to use Clang as the compiler as well for consistency.
18+# It would be cleanest to use LLVM 9's clang, but it errors. So, we use a later
19+# version of Clang to compile, but we check out the Cling fork of Clang 9 to
20+# build Cling against.
21+, clangStdenv
22+23+# For runtime C++ standard library
24+, gcc-unwrapped
25+26+# Build with debug symbols
27+, debug ? false
28+29+# Build with libc++ (LLVM) rather than stdlibc++ (GCC).
30+# This is experimental and not all features work.
31+, useLLVMLibcxx ? false
32}:
3334let
35+ stdenv = clangStdenv;
36+37 # The LLVM 9 headers have a couple bugs we need to patch
38 fixedLlvmDev = runCommand "llvm-dev-${llvmPackages_9.llvm.version}" { buildInputs = [git]; } ''
39 mkdir $out
···77 ];
7879 nativeBuildInputs = [ python3 git cmake ];
80+ buildInputs = [ libffi ncurses zlib ];
8182 strictDeps = true;
83···88 "-DLLVM_MAIN_INCLUDE_DIR=${fixedLlvmDev}/include"
89 "-DLLVM_TABLEGEN_EXE=${llvmPackages_9.llvm.out}/bin/llvm-tblgen"
90 "-DLLVM_TOOLS_BINARY_DIR=${llvmPackages_9.llvm.out}/bin"
91+ "-DLLVM_BUILD_TOOLS=Off"
92 "-DLLVM_TOOL_CLING_BUILD=ON"
9394 "-DLLVM_TARGETS_TO_BUILD=host;NVPTX"
···98 # see cling/tools/CMakeLists.txt
99 "-DCLING_INCLUDE_TESTS=ON"
100 "-DCLANG-TOOLS=OFF"
101+ ] ++ lib.optionals debug [
102+ "-DCMAKE_BUILD_TYPE=Debug"
103+ ] ++ lib.optionals useLLVMLibcxx [
104+ "-DLLVM_ENABLE_LIBCXX=ON"
105+ "-DLLVM_ENABLE_LIBCXXABI=ON"
106 ];
107108+ CPPFLAGS = if useLLVMLibcxx then [ "-stdlib=libc++" ] else [];
109+110 postInstall = lib.optionalString (!stdenv.isDarwin) ''
111 mkdir -p $out/share/Jupyter
112 cp -r /build/clang/tools/cling/tools/Jupyter/kernel $out/share/Jupyter
113 '';
114+115+ dontStrip = debug;
116117 meta = with lib; {
118 description = "The Interactive C++ Interpreter";
···122 platforms = platforms.unix;
123 };
124 };
125+126+ # Runtime flags for the C++ standard library
127+ cxxFlags = if useLLVMLibcxx then [
128+ "-I" "${lib.getDev llvmPackages_9.libcxx}/include/c++/v1"
129+ "-L" "${llvmPackages_9.libcxx}/lib"
130+ "-l" "${llvmPackages_9.libcxx}/lib/libc++.so"
131+ ] else [
132+ "-I" "${gcc-unwrapped}/include/c++/${gcc-unwrapped.version}"
133+ "-I" "${gcc-unwrapped}/include/c++/${gcc-unwrapped.version}/x86_64-unknown-linux-gnu"
134+ ];
135136 # The flags passed to the wrapped cling should
137 # a) prevent it from searching for system include files and libs, and
138+ # b) provide it with the include files and libs it needs (C and C++ standard library plus
139+ # its own stuff)
140141+ # These are also exposed as cling.flags because it's handy to be able to pass them to tools
142+ # that wrap Cling, particularly Jupyter kernels such as xeus-cling and the built-in
143+ # jupyter-cling-kernel, which use Cling as a library.
0144 # Thus, if you're packaging a Jupyter kernel, you either need to pass these flags as extra
145 # args to xcpp (for xeus-cling) or put them in the environment variable CLING_OPTS
146+ # (for jupyter-cling-kernel).
147 flags = [
148 "-nostdinc"
149 "-nostdinc++"
150+151+ "-isystem" "${lib.getLib unwrapped}/lib/clang/9.0.1/include"
152+ ]
153+ ++ cxxFlags
154+ ++ [
155+ # System libc
156 "-isystem" "${lib.getDev stdenv.cc.libc}/include"
157+158+ # cling includes
159+ "-isystem" "${lib.getDev unwrapped}/include"
160 ];
16100000000000162in
163164runCommand "cling-${unwrapped.version}" {
165 nativeBuildInputs = [ makeWrapper ];
166+ inherit unwrapped flags;
167 inherit (unwrapped) meta;
168} ''
169 makeWrapper $unwrapped/bin/cling $out/bin/cling \
0170 --add-flags "$flags"
171''