Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{ lib
2, stdenv
3, python3
4, libffi
5, git
6, cmake
7, zlib
8, fetchgit
9, fetchFromGitHub
10, makeWrapper
11, runCommand
12, llvmPackages_9
13, glibc
14, ncurses
15}:
16
17let
18 # 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
21 cp -r ${llvmPackages_9.llvm.dev}/include $out
22 cd $out
23 chmod -R u+w include
24 git apply ${./fix-llvm-include.patch}
25 '';
26
27 unwrapped = stdenv.mkDerivation rec {
28 pname = "cling-unwrapped";
29 version = "0.9";
30
31 src = fetchgit {
32 url = "http://root.cern/git/clang.git";
33 rev = "cling-v0.9";
34 sha256 = "sha256-ft1NUIclSiZ9lN3Z3DJCWA0U9q/K1M0TKkZr+PjsFYk=";
35 };
36
37 clingSrc = fetchFromGitHub {
38 owner = "root-project";
39 repo = "cling";
40 rev = "v0.9";
41 sha256 = "0wx3fi19wfjcph5kclf8108i436y79ddwakrcf0lgxnnxhdjyd29";
42 };
43
44 prePatch = ''
45 echo "add_llvm_external_project(cling)" >> tools/CMakeLists.txt
46
47 cp -r $clingSrc ./tools/cling
48 chmod -R a+w ./tools/cling
49 '';
50
51 patches = [
52 ./no-clang-cpp.patch
53
54 # https://github.com/root-project/root/commit/286d96b12aad8688b9d8e4b3b5df843dcfb716a8
55 ./fix-llvm-dylib-usage.patch
56
57 ./force-install-cling-targets.patch
58 ];
59
60 nativeBuildInputs = [ python3 git cmake ];
61 buildInputs = [ libffi zlib ncurses ];
62
63 strictDeps = true;
64
65 cmakeFlags = [
66 "-DLLVM_BINARY_DIR=${llvmPackages_9.llvm.out}"
67 "-DLLVM_CONFIG=${llvmPackages_9.llvm.dev}/bin/llvm-config"
68 "-DLLVM_LIBRARY_DIR=${llvmPackages_9.llvm.lib}/lib"
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"
72 "-DLLVM_TOOL_CLING_BUILD=ON"
73
74 "-DLLVM_TARGETS_TO_BUILD=host;NVPTX"
75 "-DLLVM_ENABLE_RTTI=ON"
76
77 # Setting -DCLING_INCLUDE_TESTS=ON causes the cling/tools targets to be built;
78 # see cling/tools/CMakeLists.txt
79 "-DCLING_INCLUDE_TESTS=ON"
80 "-DCLANG-TOOLS=OFF"
81 # "--trace-expand"
82 ];
83
84 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 '';
88
89 meta = with lib; {
90 description = "The Interactive C++ Interpreter";
91 homepage = "https://root.cern/cling/";
92 license = with licenses; [ lgpl21 ncsa ];
93 maintainers = with maintainers; [ thomasjm ];
94 platforms = platforms.unix;
95 };
96 };
97
98 # 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)
101
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.
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++"
112 "-isystem" "${lib.getDev stdenv.cc.libc}/include"
113 "-I" "${lib.getDev unwrapped}/include"
114 "-I" "${lib.getLib unwrapped}/lib/clang/9.0.1/include"
115 ];
116
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
128in
129
130runCommand "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''