lol

cling: tidy/simplify + add experimental libc++ support (#247253)

authored by

Tom McLaughlin and committed by
GitHub
bbfcab42 0e860bc0

+67 -34
+67 -34
pkgs/development/interpreters/cling/default.nix
··· 1 - { lib 2 - , stdenv 3 - , python3 1 + { cmake 2 + , fetchFromGitHub 3 + , fetchgit 4 + , git 5 + , lib 4 6 , libffi 5 - , git 6 - , cmake 7 - , zlib 8 - , fetchgit 9 - , fetchFromGitHub 7 + , llvmPackages_9 10 8 , makeWrapper 9 + , ncurses 10 + , python3 11 11 , runCommand 12 - , llvmPackages_9 13 - , glibc 14 - , ncurses 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 15 32 }: 16 33 17 34 let 35 + stdenv = clangStdenv; 36 + 18 37 # The LLVM 9 headers have a couple bugs we need to patch 19 38 fixedLlvmDev = runCommand "llvm-dev-${llvmPackages_9.llvm.version}" { buildInputs = [git]; } '' 20 39 mkdir $out ··· 58 77 ]; 59 78 60 79 nativeBuildInputs = [ python3 git cmake ]; 61 - buildInputs = [ libffi zlib ncurses ]; 80 + buildInputs = [ libffi ncurses zlib ]; 62 81 63 82 strictDeps = true; 64 83 ··· 69 88 "-DLLVM_MAIN_INCLUDE_DIR=${fixedLlvmDev}/include" 70 89 "-DLLVM_TABLEGEN_EXE=${llvmPackages_9.llvm.out}/bin/llvm-tblgen" 71 90 "-DLLVM_TOOLS_BINARY_DIR=${llvmPackages_9.llvm.out}/bin" 91 + "-DLLVM_BUILD_TOOLS=Off" 72 92 "-DLLVM_TOOL_CLING_BUILD=ON" 73 93 74 94 "-DLLVM_TARGETS_TO_BUILD=host;NVPTX" ··· 78 98 # see cling/tools/CMakeLists.txt 79 99 "-DCLING_INCLUDE_TESTS=ON" 80 100 "-DCLANG-TOOLS=OFF" 81 - # "--trace-expand" 101 + ] ++ lib.optionals debug [ 102 + "-DCMAKE_BUILD_TYPE=Debug" 103 + ] ++ lib.optionals useLLVMLibcxx [ 104 + "-DLLVM_ENABLE_LIBCXX=ON" 105 + "-DLLVM_ENABLE_LIBCXXABI=ON" 82 106 ]; 83 107 108 + CPPFLAGS = if useLLVMLibcxx then [ "-stdlib=libc++" ] else []; 109 + 84 110 postInstall = lib.optionalString (!stdenv.isDarwin) '' 85 111 mkdir -p $out/share/Jupyter 86 112 cp -r /build/clang/tools/cling/tools/Jupyter/kernel $out/share/Jupyter 87 113 ''; 114 + 115 + dontStrip = debug; 88 116 89 117 meta = with lib; { 90 118 description = "The Interactive C++ Interpreter"; ··· 94 122 platforms = platforms.unix; 95 123 }; 96 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 + ]; 97 135 98 136 # The flags passed to the wrapped cling should 99 137 # 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) 138 + # b) provide it with the include files and libs it needs (C and C++ standard library plus 139 + # its own stuff) 101 140 102 - # 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. 141 + # 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. 106 144 # Thus, if you're packaging a Jupyter kernel, you either need to pass these flags as extra 107 145 # args to xcpp (for xeus-cling) or put them in the environment variable CLING_OPTS 108 - # (for jupyter-cling-kernel) 146 + # (for jupyter-cling-kernel). 109 147 flags = [ 110 148 "-nostdinc" 111 149 "-nostdinc++" 150 + 151 + "-isystem" "${lib.getLib unwrapped}/lib/clang/9.0.1/include" 152 + ] 153 + ++ cxxFlags 154 + ++ [ 155 + # System libc 112 156 "-isystem" "${lib.getDev stdenv.cc.libc}/include" 113 - "-I" "${lib.getDev unwrapped}/include" 114 - "-I" "${lib.getLib unwrapped}/lib/clang/9.0.1/include" 157 + 158 + # cling includes 159 + "-isystem" "${lib.getDev unwrapped}/include" 115 160 ]; 116 161 117 - # 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 - 128 162 in 129 163 130 164 runCommand "cling-${unwrapped.version}" { 131 165 nativeBuildInputs = [ makeWrapper ]; 132 - inherit unwrapped flags compilerIncludeFlags; 166 + inherit unwrapped flags; 133 167 inherit (unwrapped) meta; 134 168 } '' 135 169 makeWrapper $unwrapped/bin/cling $out/bin/cling \ 136 - --add-flags "$(cat "$compilerIncludeFlags")" \ 137 170 --add-flags "$flags" 138 171 ''