nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib, stdenv, llvm_meta, fetch, substituteAll, cmake, libxml2, libllvm, version, clang-tools-extra_src, python3
2, buildLlvmTools
3, fixDarwinDylibNames
4, enableManpages ? false
5, enablePolly ? false # TODO: get this info from llvm (passthru?)
6}:
7
8let
9 self = stdenv.mkDerivation ({
10 pname = "clang";
11 inherit version;
12
13 src = fetch "cfe" "0vc4i87qwxnw9lci4ayws9spakg0z6w5w670snj9f8g5m9rc8zg9";
14
15 unpackPhase = ''
16 unpackFile $src
17 mv cfe-${version}* clang
18 sourceRoot=$PWD/clang
19 unpackFile ${clang-tools-extra_src}
20 mv clang-tools-extra-* $sourceRoot/tools/extra
21 '';
22
23 nativeBuildInputs = [ cmake python3 ]
24 ++ lib.optional enableManpages python3.pkgs.sphinx
25 ++ lib.optional stdenv.hostPlatform.isDarwin fixDarwinDylibNames;
26
27 buildInputs = [ libxml2 libllvm ];
28
29 cmakeFlags = [
30 "-DCMAKE_CXX_FLAGS=-std=c++11"
31 "-DLLVM_ENABLE_RTTI=ON"
32 "-DLLVM_CONFIG_PATH=${libllvm.dev}/bin/llvm-config${lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) "-native"}"
33 ] ++ lib.optionals enableManpages [
34 "-DCLANG_INCLUDE_DOCS=ON"
35 "-DLLVM_ENABLE_SPHINX=ON"
36 "-DSPHINX_OUTPUT_MAN=ON"
37 "-DSPHINX_OUTPUT_HTML=OFF"
38 "-DSPHINX_WARNINGS_AS_ERRORS=OFF"
39 ] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
40 "-DLLVM_TABLEGEN_EXE=${buildLlvmTools.llvm}/bin/llvm-tblgen"
41 "-DCLANG_TABLEGEN=${buildLlvmTools.libclang.dev}/bin/clang-tblgen"
42 ] ++ lib.optionals enablePolly [
43 "-DWITH_POLLY=ON"
44 "-DLINK_POLLY_INTO_TOOLS=ON"
45 ];
46
47 patches = [
48 ./purity.patch
49 # make clang -xhip use $PATH to find executables
50 ./HIP-use-PATH-7.patch
51 # Backport for the `--unwindlib=[libgcc|compiler-rt]` flag, which is
52 # needed for our bootstrapping to not interfere with C.
53 ./unwindlib.patch
54 ./gnu-install-dirs.patch
55 (substituteAll {
56 src = ../../clang-6-10-LLVMgold-path.patch;
57 libllvmLibdir = "${libllvm.lib}/lib";
58 })
59 ];
60
61 postPatch = ''
62 sed -i -e 's/DriverArgs.hasArg(options::OPT_nostdlibinc)/true/' \
63 -e 's/Args.hasArg(options::OPT_nostdlibinc)/true/' \
64 lib/Driver/ToolChains/*.cpp
65 '' + lib.optionalString stdenv.hostPlatform.isMusl ''
66 sed -i -e 's/lgcc_s/lgcc_eh/' lib/Driver/ToolChains/*.cpp
67 '' + lib.optionalString stdenv.hostPlatform.isDarwin ''
68 substituteInPlace tools/extra/clangd/CMakeLists.txt \
69 --replace "NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB" FALSE
70 '';
71
72 outputs = [ "out" "lib" "dev" "python" ];
73
74 postInstall = ''
75 ln -sv $out/bin/clang $out/bin/cpp
76
77 # Move libclang to 'lib' output
78 moveToOutput "lib/libclang.*" "$lib"
79 substituteInPlace $out/lib/cmake/clang/ClangTargets-release.cmake \
80 --replace "\''${_IMPORT_PREFIX}/lib/libclang." "$lib/lib/libclang."
81
82 mkdir -p $python/bin $python/share/clang/
83 mv $out/bin/{git-clang-format,scan-view} $python/bin
84 if [ -e $out/bin/set-xcode-analyzer ]; then
85 mv $out/bin/set-xcode-analyzer $python/bin
86 fi
87 mv $out/share/clang/*.py $python/share/clang
88 rm $out/bin/c-index-test
89 patchShebangs $python/bin
90
91 mkdir -p $dev/bin
92 cp bin/clang-tblgen $dev/bin
93 '';
94
95 passthru = {
96 inherit libllvm;
97 isClang = true;
98 hardeningUnsupportedFlags = [ "fortify3" ];
99 };
100
101 meta = llvm_meta // {
102 homepage = "https://clang.llvm.org/";
103 description = "A C language family frontend for LLVM";
104 longDescription = ''
105 The Clang project provides a language front-end and tooling
106 infrastructure for languages in the C language family (C, C++, Objective
107 C/C++, OpenCL, CUDA, and RenderScript) for the LLVM project.
108 It aims to deliver amazingly fast compiles, extremely useful error and
109 warning messages and to provide a platform for building great source
110 level tools. The Clang Static Analyzer and clang-tidy are tools that
111 automatically find bugs in your code, and are great examples of the sort
112 of tools that can be built using the Clang frontend as a library to
113 parse C/C++ code.
114 '';
115 mainProgram = "clang";
116 };
117 } // lib.optionalAttrs enableManpages {
118 pname = "clang-manpages";
119
120 buildPhase = ''
121 make docs-clang-man
122 '';
123
124 installPhase = ''
125 mkdir -p $out/share/man/man1
126 # Manually install clang manpage
127 cp docs/man/*.1 $out/share/man/man1/
128 '';
129
130 outputs = [ "out" ];
131
132 doCheck = false;
133
134 meta = llvm_meta // {
135 description = "man page for Clang ${version}";
136 };
137 });
138in self