···11+diff --git a/tools/llvm-config/llvm-config.cpp b/tools/llvm-config/llvm-config.cpp
22+index 94d426b..37f7794 100644
33+--- a/tools/llvm-config/llvm-config.cpp
44++++ b/tools/llvm-config/llvm-config.cpp
55+@@ -333,6 +333,21 @@ int main(int argc, char **argv) {
66+ ActiveIncludeOption = "-I" + ActiveIncludeDir;
77+ }
88+99++ /// Nix-specific multiple-output handling: override ActiveLibDir if --link-shared
1010++ if (!IsInDevelopmentTree) {
1111++ bool WantShared = true;
1212++ for (int i = 1; i < argc; ++i) {
1313++ StringRef Arg = argv[i];
1414++ if (Arg == "--link-shared")
1515++ WantShared = true;
1616++ else if (Arg == "--link-static")
1717++ WantShared = false; // the last one wins
1818++ }
1919++
2020++ if (WantShared)
2121++ ActiveLibDir = std::string("@lib@") + "/lib" + LLVM_LIBDIR_SUFFIX;
2222++ }
2323++
2424+ /// We only use `shared library` mode in cases where the static library form
2525+ /// of the components provided are not available; note however that this is
2626+ /// skipped if we're run from within the build dir. However, once installed,
+153
pkgs/development/compilers/llvm/5/llvm.nix
···11+{ stdenv
22+, fetch
33+, fetchpatch
44+, perl
55+, groff
66+, cmake
77+, python
88+, libffi
99+, binutils
1010+, libxml2
1111+, valgrind
1212+, ncurses
1313+, version
1414+, release_version
1515+, zlib
1616+, compiler-rt_src
1717+, libcxxabi
1818+, debugVersion ? false
1919+, enableManpages ? false
2020+, enableSharedLibraries ? true
2121+, darwin
2222+}:
2323+2424+let
2525+ src = fetch "llvm" "1nin64vz21hyng6jr19knxipvggaqlkl2l9jpd5czbc4c2pcnpg3";
2626+2727+ # Used when creating a version-suffixed symlink of libLLVM.dylib
2828+ shortVersion = with stdenv.lib;
2929+ concatStringsSep "." (take 2 (splitString "." release_version));
3030+in stdenv.mkDerivation rec {
3131+ name = "llvm-${version}";
3232+3333+ unpackPhase = ''
3434+ unpackFile ${src}
3535+ mv llvm-${version}* llvm
3636+ sourceRoot=$PWD/llvm
3737+ unpackFile ${compiler-rt_src}
3838+ mv compiler-rt-* $sourceRoot/projects/compiler-rt
3939+ '';
4040+4141+ outputs = [ "out" ]
4242+ ++ stdenv.lib.optional enableSharedLibraries "lib"
4343+ ++ stdenv.lib.optional enableManpages "man";
4444+4545+ nativeBuildInputs = [ perl groff cmake python ]
4646+ ++ stdenv.lib.optional enableManpages python.pkgs.sphinx;
4747+4848+ buildInputs = [ libxml2 libffi ]
4949+ ++ stdenv.lib.optionals stdenv.isDarwin [ libcxxabi ];
5050+5151+ propagatedBuildInputs = [ ncurses zlib ];
5252+5353+ # TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
5454+ # to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
5555+ # can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
5656+ # get no binary cache for the entire platform. If you really find yourself wanting the TSAN, make this controllable by
5757+ # a flag and turn the flag off during the stdenv build.
5858+ postPatch = stdenv.lib.optionalString stdenv.isDarwin ''
5959+ substituteInPlace ./projects/compiler-rt/cmake/config-ix.cmake \
6060+ --replace 'set(COMPILER_RT_HAS_TSAN TRUE)' 'set(COMPILER_RT_HAS_TSAN FALSE)'
6161+ ''
6262+ # Patch llvm-config to return correct library path based on --link-{shared,static}.
6363+ + stdenv.lib.optionalString (enableSharedLibraries) ''
6464+ substitute '${./llvm-outputs.patch}' ./llvm-outputs.patch --subst-var lib
6565+ patch -p1 < ./llvm-outputs.patch
6666+ '' + ''
6767+ # FileSystem permissions tests fail with various special bits
6868+ substituteInPlace unittests/Support/CMakeLists.txt \
6969+ --replace "Path.cpp" ""
7070+ rm unittests/Support/Path.cpp
7171+7272+ # Revert compiler-rt commit that makes codesign mandatory
7373+ patch -p1 -i ${./compiler-rt-codesign.patch} -d projects/compiler-rt
7474+ '';
7575+7676+ # hacky fix: created binaries need to be run before installation
7777+ preBuild = ''
7878+ mkdir -p $out/
7979+ ln -sv $PWD/lib $out
8080+ '';
8181+8282+ cmakeFlags = with stdenv; [
8383+ "-DCMAKE_BUILD_TYPE=${if debugVersion then "Debug" else "Release"}"
8484+ "-DLLVM_INSTALL_UTILS=ON" # Needed by rustc
8585+ "-DLLVM_BUILD_TESTS=ON"
8686+ "-DLLVM_ENABLE_FFI=ON"
8787+ "-DLLVM_ENABLE_RTTI=ON"
8888+ "-DCOMPILER_RT_INCLUDE_TESTS=OFF" # FIXME: requires clang source code
8989+ ]
9090+ ++ stdenv.lib.optional enableSharedLibraries
9191+ "-DLLVM_LINK_LLVM_DYLIB=ON"
9292+ ++ stdenv.lib.optionals enableManpages [
9393+ "-DLLVM_BUILD_DOCS=ON"
9494+ "-DLLVM_ENABLE_SPHINX=ON"
9595+ "-DSPHINX_OUTPUT_MAN=ON"
9696+ "-DSPHINX_OUTPUT_HTML=OFF"
9797+ "-DSPHINX_WARNINGS_AS_ERRORS=OFF"
9898+ ]
9999+ ++ stdenv.lib.optional (!isDarwin)
100100+ "-DLLVM_BINUTILS_INCDIR=${stdenv.lib.getDev binutils}/include"
101101+ ++ stdenv.lib.optionals (isDarwin) [
102102+ "-DLLVM_ENABLE_LIBCXX=ON"
103103+ "-DCAN_TARGET_i386=false"
104104+ ];
105105+106106+ postBuild = ''
107107+ rm -fR $out
108108+109109+ paxmark m bin/{lli,llvm-rtdyld}
110110+ paxmark m unittests/ExecutionEngine/MCJIT/MCJITTests
111111+ paxmark m unittests/ExecutionEngine/Orc/OrcJITTests
112112+ paxmark m unittests/Support/SupportTests
113113+ paxmark m bin/lli-child-target
114114+ '';
115115+116116+ preCheck = ''
117117+ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD/lib
118118+ '';
119119+120120+ postInstall = stdenv.lib.optionalString enableManpages ''
121121+ moveToOutput "share/man" "$man"
122122+ ''
123123+ + stdenv.lib.optionalString enableSharedLibraries ''
124124+ moveToOutput "lib/libLLVM-*" "$lib"
125125+ moveToOutput "lib/libLLVM${stdenv.hostPlatform.extensions.sharedLibrary}" "$lib"
126126+ substituteInPlace "$out/lib/cmake/llvm/LLVMExports-${if debugVersion then "debug" else "release"}.cmake" \
127127+ --replace "\''${_IMPORT_PREFIX}/lib/libLLVM-" "$lib/lib/libLLVM-"
128128+ ''
129129+ + stdenv.lib.optionalString (stdenv.isDarwin && enableSharedLibraries) ''
130130+ substituteInPlace "$out/lib/cmake/llvm/LLVMExports-${if debugVersion then "debug" else "release"}.cmake" \
131131+ --replace "\''${_IMPORT_PREFIX}/lib/libLLVM.dylib" "$lib/lib/libLLVM.dylib"
132132+ install_name_tool -id $lib/lib/libLLVM.dylib $lib/lib/libLLVM.dylib
133133+ install_name_tool -change @rpath/libLLVM.dylib $lib/lib/libLLVM.dylib $out/bin/llvm-config
134134+ ln -s $lib/lib/libLLVM.dylib $lib/lib/libLLVM-${shortVersion}.dylib
135135+ ln -s $lib/lib/libLLVM.dylib $lib/lib/libLLVM-${release_version}.dylib
136136+ '';
137137+138138+ doCheck = stdenv.isLinux && (!stdenv.isi686);
139139+140140+ checkTarget = "check-all";
141141+142142+ enableParallelBuilding = true;
143143+144144+ passthru.src = src;
145145+146146+ meta = {
147147+ description = "Collection of modular and reusable compiler and toolchain technologies";
148148+ homepage = http://llvm.org/;
149149+ license = stdenv.lib.licenses.ncsa;
150150+ maintainers = with stdenv.lib.maintainers; [ lovek323 raskin viric dtzWill ];
151151+ platforms = stdenv.lib.platforms.all;
152152+ };
153153+}