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