···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,
+171
pkgs/development/compilers/llvm/6/llvm.nix
···11+{ stdenv
22+, fetch
33+, fetchpatch
44+, perl
55+, groff
66+, cmake
77+, python
88+, libffi
99+, libbfd
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" "1c07i0b61j69m578lgjkyayg419sh7sn40xb3j112nr2q2gli9sz";
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+4444+ nativeBuildInputs = [ perl groff cmake python ]
4545+ ++ stdenv.lib.optional enableManpages python.pkgs.sphinx;
4646+4747+ buildInputs = [ libxml2 libffi ]
4848+ ++ stdenv.lib.optionals stdenv.isDarwin [ libcxxabi ];
4949+5050+ propagatedBuildInputs = [ ncurses zlib ];
5151+5252+ # TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
5353+ # to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
5454+ # can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
5555+ # get no binary cache for the entire platform. If you really find yourself wanting the TSAN, make this controllable by
5656+ # a flag and turn the flag off during the stdenv build.
5757+ postPatch = stdenv.lib.optionalString stdenv.isDarwin ''
5858+ substituteInPlace ./projects/compiler-rt/cmake/config-ix.cmake \
5959+ --replace 'set(COMPILER_RT_HAS_TSAN TRUE)' 'set(COMPILER_RT_HAS_TSAN FALSE)'
6060+6161+ substituteInPlace cmake/modules/AddLLVM.cmake \
6262+ --replace 'set(_install_name_dir INSTALL_NAME_DIR "@rpath")' "set(_install_name_dir INSTALL_NAME_DIR "$lib/lib")" \
6363+ --replace 'set(_install_rpath "@loader_path/../lib" ''${extra_libdir})' ""
6464+ ''
6565+ # Patch llvm-config to return correct library path based on --link-{shared,static}.
6666+ + stdenv.lib.optionalString (enableSharedLibraries) ''
6767+ substitute '${./llvm-outputs.patch}' ./llvm-outputs.patch --subst-var lib
6868+ patch -p1 < ./llvm-outputs.patch
6969+ '' + ''
7070+ # FileSystem permissions tests fail with various special bits
7171+ substituteInPlace unittests/Support/CMakeLists.txt \
7272+ --replace "Path.cpp" ""
7373+ rm unittests/Support/Path.cpp
7474+7575+ # Revert compiler-rt commit that makes codesign mandatory
7676+ patch -p1 -i ${./compiler-rt-codesign.patch} -d projects/compiler-rt
7777+ '' + stdenv.lib.optionalString stdenv.isAarch64 ''
7878+ patch -p0 < ${../aarch64.patch}
7979+ '';
8080+8181+ # hacky fix: created binaries need to be run before installation
8282+ preBuild = ''
8383+ mkdir -p $out/
8484+ ln -sv $PWD/lib $out
8585+ '';
8686+8787+ cmakeFlags = with stdenv; [
8888+ "-DCMAKE_BUILD_TYPE=${if debugVersion then "Debug" else "Release"}"
8989+ "-DLLVM_INSTALL_UTILS=ON" # Needed by rustc
9090+ "-DLLVM_BUILD_TESTS=ON"
9191+ "-DLLVM_ENABLE_FFI=ON"
9292+ "-DLLVM_ENABLE_RTTI=ON"
9393+ "-DCOMPILER_RT_INCLUDE_TESTS=OFF" # FIXME: requires clang source code
9494+ ]
9595+ ++ stdenv.lib.optional enableSharedLibraries
9696+ "-DLLVM_LINK_LLVM_DYLIB=ON"
9797+ ++ stdenv.lib.optionals enableManpages [
9898+ "-DLLVM_BUILD_DOCS=ON"
9999+ "-DLLVM_ENABLE_SPHINX=ON"
100100+ "-DSPHINX_OUTPUT_MAN=ON"
101101+ "-DSPHINX_OUTPUT_HTML=OFF"
102102+ "-DSPHINX_WARNINGS_AS_ERRORS=OFF"
103103+ ]
104104+ ++ stdenv.lib.optional (!isDarwin)
105105+ "-DLLVM_BINUTILS_INCDIR=${libbfd.dev}/include"
106106+ ++ stdenv.lib.optionals (isDarwin) [
107107+ "-DLLVM_ENABLE_LIBCXX=ON"
108108+ "-DCAN_TARGET_i386=false"
109109+ ];
110110+111111+ postBuild = ''
112112+ rm -fR $out
113113+114114+ paxmark m bin/{lli,llvm-rtdyld}
115115+ paxmark m unittests/ExecutionEngine/MCJIT/MCJITTests
116116+ paxmark m unittests/ExecutionEngine/Orc/OrcJITTests
117117+ paxmark m unittests/Support/SupportTests
118118+ paxmark m bin/lli-child-target
119119+ '';
120120+121121+ preCheck = ''
122122+ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD/lib
123123+ '';
124124+125125+ postInstall = stdenv.lib.optionalString enableSharedLibraries ''
126126+ moveToOutput "lib/libLLVM-*" "$lib"
127127+ moveToOutput "lib/libLLVM${stdenv.hostPlatform.extensions.sharedLibrary}" "$lib"
128128+ substituteInPlace "$out/lib/cmake/llvm/LLVMExports-${if debugVersion then "debug" else "release"}.cmake" \
129129+ --replace "\''${_IMPORT_PREFIX}/lib/libLLVM-" "$lib/lib/libLLVM-"
130130+ ''
131131+ + stdenv.lib.optionalString (stdenv.isDarwin && enableSharedLibraries) ''
132132+ substituteInPlace "$out/lib/cmake/llvm/LLVMExports-${if debugVersion then "debug" else "release"}.cmake" \
133133+ --replace "\''${_IMPORT_PREFIX}/lib/libLLVM.dylib" "$lib/lib/libLLVM.dylib"
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+} // stdenv.lib.optionalAttrs enableManpages {
154154+ name = "llvm-manpages-${version}";
155155+156156+ buildPhase = ''
157157+ make docs-llvm-man
158158+ '';
159159+160160+ propagatedBuildInputs = [];
161161+162162+ installPhase = ''
163163+ make -C docs install
164164+ '';
165165+166166+ outputs = [ "out" ];
167167+168168+ doCheck = false;
169169+170170+ meta.description = "man pages for LLVM ${version}";
171171+})