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