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" "0ihnbdl058gvl2wdy45p5am55bq8ifx8m9mhcsgj9ax8yxlzvvvh";
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 "-DCLANGD_BUILD_XPC=OFF"
32 "-DLLVM_ENABLE_RTTI=ON"
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 ./xpc.patch
50 # Backport for -static-pie, which the latter touches, and which is nice in
51 # its own right.
52 ./static-pie.patch
53 # Backport for the `--unwindlib=[libgcc|compiler-rt]` flag, which is
54 # needed for our bootstrapping to not interfere with C.
55 ./unwindlib.patch
56 # https://reviews.llvm.org/D51899
57 ./compiler-rt-baremetal.patch
58 # make clang -xhip use $PATH to find executables
59 ./HIP-use-PATH-8.patch
60 ./gnu-install-dirs.patch
61 (substituteAll {
62 src = ../../clang-6-10-LLVMgold-path.patch;
63 libllvmLibdir = "${libllvm.lib}/lib";
64 })
65 ];
66
67 postPatch = ''
68 sed -i -e 's/DriverArgs.hasArg(options::OPT_nostdlibinc)/true/' \
69 -e 's/Args.hasArg(options::OPT_nostdlibinc)/true/' \
70 lib/Driver/ToolChains/*.cpp
71 '' + lib.optionalString stdenv.hostPlatform.isMusl ''
72 sed -i -e 's/lgcc_s/lgcc_eh/' lib/Driver/ToolChains/*.cpp
73 '' + lib.optionalString stdenv.hostPlatform.isDarwin ''
74 substituteInPlace tools/extra/clangd/CMakeLists.txt \
75 --replace "NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB" FALSE
76 '';
77
78 outputs = [ "out" "lib" "dev" "python" ];
79
80 postInstall = ''
81 ln -sv $out/bin/clang $out/bin/cpp
82
83 # Move libclang to 'lib' output
84 moveToOutput "lib/libclang.*" "$lib"
85 substituteInPlace $out/lib/cmake/clang/ClangTargets-release.cmake \
86 --replace "\''${_IMPORT_PREFIX}/lib/libclang." "$lib/lib/libclang."
87
88 mkdir -p $python/bin $python/share/clang/
89 mv $out/bin/{git-clang-format,scan-view} $python/bin
90 if [ -e $out/bin/set-xcode-analyzer ]; then
91 mv $out/bin/set-xcode-analyzer $python/bin
92 fi
93 mv $out/share/clang/*.py $python/share/clang
94 rm $out/bin/c-index-test
95 patchShebangs $python/bin
96
97 mkdir -p $dev/bin
98 cp bin/clang-tblgen $dev/bin
99 '';
100
101 passthru = {
102 inherit libllvm;
103 isClang = true;
104 hardeningUnsupportedFlags = [ "fortify3" ];
105 };
106
107 meta = llvm_meta // {
108 homepage = "https://clang.llvm.org/";
109 description = "A C language family frontend for LLVM";
110 longDescription = ''
111 The Clang project provides a language front-end and tooling
112 infrastructure for languages in the C language family (C, C++, Objective
113 C/C++, OpenCL, CUDA, and RenderScript) for the LLVM project.
114 It aims to deliver amazingly fast compiles, extremely useful error and
115 warning messages and to provide a platform for building great source
116 level tools. The Clang Static Analyzer and clang-tidy are tools that
117 automatically find bugs in your code, and are great examples of the sort
118 of tools that can be built using the Clang frontend as a library to
119 parse C/C++ code.
120 '';
121 mainProgram = "clang";
122 };
123 } // lib.optionalAttrs enableManpages {
124 pname = "clang-manpages";
125
126 buildPhase = ''
127 make docs-clang-man
128 '';
129
130 installPhase = ''
131 mkdir -p $out/share/man/man1
132 # Manually install clang manpage
133 cp docs/man/*.1 $out/share/man/man1/
134 '';
135
136 outputs = [ "out" ];
137
138 doCheck = false;
139
140 meta = llvm_meta // {
141 description = "man page for Clang ${version}";
142 };
143 });
144in self