···1+## How to upgrade llvm_git
2+3+- Run `update-git.py`.
4+ This will set the github revision and sha256 for `llvmPackages_git.llvm` to whatever the latest chromium build is using.
5+ For a more recent, commit run `nix-prefetch-github` and change the rev and sha256 accordingly.
6+7+- That was the easy part.
8+ The hard part is updating the patch files.
9+10+ The general process is:
11+12+ 1. Try to build `llvmPackages_git.llvm` and associated packages such as
13+ `clang` and `compiler-rt`. You can use the `-L` and `--keep-failed` flags to make
14+ debugging patch errors easy, e.g., `nix build .#llvmPackages_git.clang -L --keep-failed`
15+16+ 2. The build will error out with something similar to this:
17+ ```sh
18+ ...
19+ clang-unstable> patching sources
20+ clang-unstable> applying patch /nix/store/nndv6gq6w608n197fndvv5my4a5zg2qi-purity.patch
21+ clang-unstable> patching file lib/Driver/ToolChains/Gnu.cpp
22+ clang-unstable> Hunk #1 FAILED at 487.
23+ clang-unstable> 1 out of 1 hunk FAILED -- saving rejects to file lib/Driver/ToolChains/Gnu.cpp.rej
24+ note: keeping build directory '/tmp/nix-build-clang-unstable-2022-25-07.drv-17'
25+ error: builder for '/nix/store/zwi123kpkyz52fy7p6v23azixd807r8c-clang-unstable-2022-25-07.drv' failed with exit code 1;
26+ last 8 log lines:
27+ > unpacking sources
28+ > unpacking source archive /nix/store/mrxadx11wv1ckjr2208qgxp472pmmg6g-clang-src-unstable-2022-25-07
29+ > source root is clang-src-unstable-2022-25-07/clang
30+ > patching sources
31+ > applying patch /nix/store/nndv6gq6w608n197fndvv5my4a5zg2qi-purity.patch
32+ > patching file lib/Driver/ToolChains/Gnu.cpp
33+ > Hunk #1 FAILED at 487.
34+ > 1 out of 1 hunk FAILED -- saving rejects to file lib/Driver/ToolChains/Gnu.cpp.rej
35+ For full logs, run 'nix log /nix/store/zwi123kpkyz52fy7p6v23azixd807r8c-clang-unstable-2022-25-07.drv'.
36+ note: keeping build directory '/tmp/nix-build-compiler-rt-libc-unstable-2022-25-07.drv-20'
37+ error: 1 dependencies of derivation '/nix/store/ndbbh3wrl0l39b22azf46f1n7zlqwmag-clang-wrapper-unstable-2022-25-07.drv' failed to build
38+ ```
39+40+ Notice the `Hunk #1 Failed at 487` line.
41+ The lines above show us that the `purity.patch` failed on `lib/Driver/ToolChains/Gnu.cpp` when compiling `clang`.
42+43+ 3. The task now is to cross reference the hunks in the purity patch with
44+ `lib/Driver/ToolCahins/Gnu.cpp.orig` to see why the patch failed.
45+ The `.orig` file will be in the build directory referenced in the line `note: keeping build directory ...`;
46+ this message results from the `--keep-failed` flag.
47+48+ 4. Now you should be able to open whichever patch failed, and the `foo.orig` file that it failed on.
49+ Correct the patch by adapting it to the new code and be mindful of whitespace;
50+ which can be an easily missed reason for failures.
51+ For cases where the hunk is no longer needed you can simply remove it from the patch.
52+53+ This is fine for small corrections, but when more serious changes are needed its better to use git.
54+55+ 1. Clone the LLVM monorepo at https://github.com/llvm/llvm-project/
56+57+ 2. Check out the revision we were using before.
58+59+ 3. Use `patch -p1 < path/to-path` in the project subdirectories to apply the patches and commit.
60+61+ 4. Use `git rebase HEAD^ --onto <dest>` to rebase the patches onto the new revision we are trying to build, and fix all conflicts.
62+63+ 5. Use `git diff HEAD^:<project> HEAD:<project>` to get subdir diff to write back to Nixpkgs.
64+65+## Information on our current patch sets
66+67+### "GNU Install Dirs" patches
68+69+Use CMake's [`GNUInstallDirs`](https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html) to support multiple outputs.
70+71+Previously, LLVM Just hard-coded `bin`, `include`, and `lib${LLVM_TARGET_PREFIX}`.
72+We are making it use these variables.
73+74+For the older LLVM versions, these patches live in https://github.com/Ericson2314/llvm-project branches `split-prefix`.
75+Instead of applying the patches to the worktree per the above instructions, one can checkout those directly and rebase those instead.
76+77+For newer LLVM versions, enough has has been upstreamed,
78+(see https://reviews.llvm.org/differential/query/5UAfpj_9zHwY/ for my progress upstreaming),
79+that I have just assembled new gnu-install-dirs patches from the remaining unmerged patches instead of rebasing from the prior LLVM's gnu install dirs patch.
···1-diff --git a/CMakeLists.txt b/CMakeLists.txt
2-index c5003b5efa1d..4fffb9721284 100644
3---- a/CMakeLists.txt
4-+++ b/CMakeLists.txt
5-@@ -5,6 +5,8 @@
6-7- cmake_minimum_required(VERSION 3.13.4)
8-9-+include(GNUInstallDirs)
10-+
11- # Check if compiler-rt is built as a standalone project.
12- if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR COMPILER_RT_STANDALONE_BUILD)
13- project(CompilerRT C CXX ASM)
14diff --git a/cmake/base-config-ix.cmake b/cmake/base-config-ix.cmake
15-index 1ada0ab30ba0..b4be6c4a3c73 100644
16--- a/cmake/base-config-ix.cmake
17+++ b/cmake/base-config-ix.cmake
18-@@ -66,7 +66,7 @@ if (LLVM_TREE_AVAILABLE)
19- else()
20- # Take output dir and install path from the user.
21- set(COMPILER_RT_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE PATH
22-- "Path where built compiler-rt libraries should be stored.")
23-+ "Path where built compiler-rt build artifacts should be stored.")
24- set(COMPILER_RT_EXEC_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin CACHE PATH
25- "Path where built compiler-rt executables should be stored.")
26- set(COMPILER_RT_INSTALL_PATH "" CACHE PATH
27-@@ -98,23 +98,23 @@ endif()
28 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
29 set(COMPILER_RT_OUTPUT_LIBRARY_DIR
30 ${COMPILER_RT_OUTPUT_DIR}/lib)
···40 set(COMPILER_RT_INSTALL_LIBRARY_DIR "${default_install_path}" CACHE PATH
41 "Path where built compiler-rt libraries should be installed.")
42 endif()
43--extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" bin)
44-+extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" "${CMAKE_INSTALL_BINDIR}")
45- set(COMPILER_RT_INSTALL_BINARY_DIR "${default_install_path}" CACHE PATH
46- "Path where built compiler-rt executables should be installed.")
47--extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" include)
48-+extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" "${CMAKE_INSTALL_INCLUDEDIR}")
49- set(COMPILER_RT_INSTALL_INCLUDE_DIR "${default_install_path}" CACHE PATH
50- "Path where compiler-rt headers should be installed.")
51--extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" share)
52-+extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" "${CMAKE_INSTALL_DATADIR}")
53- set(COMPILER_RT_INSTALL_DATA_DIR "${default_install_path}" CACHE PATH
54- "Path where compiler-rt data files should be installed.")
55-
···00000000000001diff --git a/cmake/base-config-ix.cmake b/cmake/base-config-ix.cmake
2+index 8a6219568b3f..30ee68a47ccf 100644
3--- a/cmake/base-config-ix.cmake
4+++ b/cmake/base-config-ix.cmake
5+@@ -100,13 +100,13 @@ endif()
0000000006 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
7 set(COMPILER_RT_OUTPUT_LIBRARY_DIR
8 ${COMPILER_RT_OUTPUT_DIR}/lib)
···18 set(COMPILER_RT_INSTALL_LIBRARY_DIR "${default_install_path}" CACHE PATH
19 "Path where built compiler-rt libraries should be installed.")
20 endif()
0000000000000
···1+--- a/CMakeLists.txt
2++++ b/CMakeLists.txt
3+@@ -131,10 +131,21 @@ if (NOT LIBCXXABI_ENABLE_SHARED AND NOT LIBCXXABI_ENABLE_STATIC)
4+ message(FATAL_ERROR "libc++abi must be built as either a shared or static library.")
5+ endif()
6+7+-# TODO: Remove this, which shouldn't be necessary since we know we're being built
8+-# side-by-side with libc++.
9+ set(LIBCXXABI_LIBCXX_INCLUDES "" CACHE PATH
10+ "Specify path to libc++ includes.")
11++if (NOT libcxx IN_LIST LLVM_ENABLE_RUNTIMES)
12++ if (NOT IS_DIRECTORY ${LIBCXXABI_LIBCXX_INCLUDES})
13++ message(FATAL_ERROR
14++ "LIBCXXABI_LIBCXX_INCLUDES=${LIBCXXABI_LIBCXX_INCLUDES} is not a valid directory. "
15++ "Please provide the path to where the libc++ headers have been installed.")
16++ endif()
17++ add_library(cxx-headers INTERFACE)
18++ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
19++ target_compile_options(cxx-headers INTERFACE /I "${LIBCXXABI_LIBCXX_INCLUDES}")
20++ else()
21++ target_compile_options(cxx-headers INTERFACE -I "${LIBCXXABI_LIBCXX_INCLUDES}")
22++ endif()
23++endif()
24+25+ set(LIBCXXABI_HERMETIC_STATIC_LIBRARY_DEFAULT OFF)
26+ if (WIN32)
27+--- a/test/CMakeLists.txt
28++++ b/test/CMakeLists.txt
29+@@ -61,9 +61,13 @@ if (LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY)
30+ list(APPEND LIBCXXABI_TEST_DEPS cxx_external_threads)
31+ endif()
32+33+-list(APPEND LIBCXXABI_TEST_DEPS cxx)
34+-if (LIBCXXABI_USE_LLVM_UNWINDER AND TARGET unwind)
35+- list(APPEND LIBCXXABI_TEST_DEPS unwind)
36++if(libcxx IN_LIST LLVM_ENABLE_RUNTIMES)
37++ list(APPEND LIBCXXABI_TEST_DEPS cxx)
38++endif()
39++if(libunwind IN_LIST LLVM_ENABLE_RUNTIMES)
40++ if (LIBCXXABI_USE_LLVM_UNWINDER AND TARGET unwind)
41++ list(APPEND LIBCXXABI_TEST_DEPS unwind)
42++ endif()
43+ endif()
44+45+ set(AUTO_GEN_COMMENT "## Autogenerated by libcxxabi configuration.\n# Do not edit!")
···2526 patches = [
27 ./gnu-install-dirs.patch
28- # On Darwin the llvm-config is perhaps not working fine as the
29- # LLVM_MAIN_SRC_DIR is not getting set correctly, and the build fails as
30- # the include path is not correct.
31- ./fix-root-src-dir.patch
32 ];
3334 nativeBuildInputs = [ cmake ];
35 buildInputs = [ libllvm libxml2 ];
3637- cmakeFlags = lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
0038 "-DLLVM_TABLEGEN_EXE=${buildLlvmTools.llvm}/bin/llvm-tblgen"
39 ];
40
···1-diff --git a/CMakeLists.txt b/CMakeLists.txt
2-index 79d451965ed4..78188978d6de 100644
3---- a/CMakeLists.txt
4-+++ b/CMakeLists.txt
5-@@ -12,6 +12,8 @@ set(CMAKE_MODULE_PATH
6- # If we are not building as part of LLVM, build LLDB as a standalone project,
7- # using LLVM as an external library.
8- if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
9-+ include(GNUInstallDirs)
10-+
11- project(lldb)
12- include(LLDBStandalone)
13-14diff --git a/cmake/modules/AddLLDB.cmake b/cmake/modules/AddLLDB.cmake
15index 3291a7c808e1..b27d27ce6a87 100644
16--- a/cmake/modules/AddLLDB.cmake
···1diff --git a/CMakeLists.txt b/CMakeLists.txt
2-index 7f11a05f5622..fb90f8f6a49b 100644
3--- a/CMakeLists.txt
4+++ b/CMakeLists.txt
5-@@ -8,6 +8,8 @@ if (OPENMP_STANDALONE_BUILD OR "${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_S
6- set(OPENMP_STANDALONE_BUILD TRUE)
7- project(openmp C CXX)
8-9-+ include(GNUInstallDirs)
10-+
11- # CMAKE_BUILD_TYPE was not set, default to Release.
12- if (NOT CMAKE_BUILD_TYPE)
13- set(CMAKE_BUILD_TYPE Release)
14-@@ -19,7 +21,7 @@ if (OPENMP_STANDALONE_BUILD OR "${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_S
15 set(OPENMP_LIBDIR_SUFFIX "" CACHE STRING
16 "Suffix of lib installation directory, e.g. 64 => lib64")
17 # Do not use OPENMP_LIBDIR_SUFFIX directly, use OPENMP_INSTALL_LIBDIR.
···2021 # Group test settings.
22 set(OPENMP_TEST_C_COMPILER ${CMAKE_C_COMPILER} CACHE STRING
23-@@ -30,7 +32,7 @@ if (OPENMP_STANDALONE_BUILD OR "${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_S
24 else()
25 set(OPENMP_ENABLE_WERROR ${LLVM_ENABLE_WERROR})
26 # If building in tree, we honor the same install suffix LLVM uses.
···2930 if (NOT MSVC)
31 set(OPENMP_TEST_C_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
32-index 0e1ce2afd154..8b3810f83713 100644
33---- a/libomptarget/plugins/amdgpu/CMakeLists.txt
34-+++ b/libomptarget/plugins/amdgpu/CMakeLists.txt
35-@@ -80,7 +80,7 @@ add_library(omptarget.rtl.amdgpu SHARED
36-37- # Install plugin under the lib destination folder.
38- # When we build for debug, OPENMP_LIBDIR_SUFFIX get set to -debug
39--install(TARGETS omptarget.rtl.amdgpu LIBRARY DESTINATION "lib${OPENMP_LIBDIR_SUFFIX}")
40-+install(TARGETS omptarget.rtl.amdgpu LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}${OPENMP_LIBDIR_SUFFIX}")
41- set_property(TARGET omptarget.rtl.amdgpu PROPERTY INSTALL_RPATH_USE_LINK_PATH ON)
42-43- if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
44-diff --git a/libomptarget/plugins/ve/CMakeLists.txt b/libomptarget/plugins/ve/CMakeLists.txt
45-index 16ce0891ca23..db30ee9c769f 100644
46---- a/libomptarget/plugins/ve/CMakeLists.txt
47-+++ b/libomptarget/plugins/ve/CMakeLists.txt
48-@@ -32,7 +32,7 @@ if(${LIBOMPTARGET_DEP_VEO_FOUND})
49-50- # Install plugin under the lib destination folder.
51- install(TARGETS "omptarget.rtl.${tmachine_libname}"
52-- LIBRARY DESTINATION lib${OPENMP_LIBDIR_SUFFIX})
53-+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}${OPENMP_LIBDIR_SUFFIX})
54-55- target_link_libraries(
56- "omptarget.rtl.${tmachine_libname}"
57-diff --git a/runtime/src/CMakeLists.txt b/runtime/src/CMakeLists.txt
58-index e4f4e6e1e73f..1164b3b22b0e 100644
59---- a/runtime/src/CMakeLists.txt
60-+++ b/runtime/src/CMakeLists.txt
61-@@ -346,13 +346,13 @@ add_dependencies(libomp-micro-tests libomp-test-deps)
62- # We want to install libomp in DESTDIR/CMAKE_INSTALL_PREFIX/lib
63- # We want to install headers in DESTDIR/CMAKE_INSTALL_PREFIX/include
64- if(${OPENMP_STANDALONE_BUILD})
65-- set(LIBOMP_HEADERS_INSTALL_PATH include)
66-+ set(LIBOMP_HEADERS_INSTALL_PATH "${CMAKE_INSTALL_INCLUDEDIR}")
67- else()
68- string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION ${PACKAGE_VERSION})
69- set(LIBOMP_HEADERS_INSTALL_PATH "${OPENMP_INSTALL_LIBDIR}/clang/${CLANG_VERSION}/include")
70- endif()
71- if(WIN32)
72-- install(TARGETS omp RUNTIME DESTINATION bin)
73-+ install(TARGETS omp RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
74- install(TARGETS ${LIBOMP_IMP_LIB_TARGET} ARCHIVE DESTINATION "${OPENMP_INSTALL_LIBDIR}")
75- # Create aliases (regular copies) of the library for backwards compatibility
76- set(LIBOMP_ALIASES "libiomp5md")
77-diff --git a/tools/multiplex/CMakeLists.txt b/tools/multiplex/CMakeLists.txt
78-index 64317c112176..4002784da736 100644
79---- a/tools/multiplex/CMakeLists.txt
80-+++ b/tools/multiplex/CMakeLists.txt
81-@@ -4,7 +4,7 @@ if(LIBOMP_OMPT_SUPPORT)
82- add_library(ompt-multiplex INTERFACE)
83- target_include_directories(ompt-multiplex INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
84-85-- install(FILES ompt-multiplex.h DESTINATION include)
86-+ install(FILES ompt-multiplex.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
87-88- add_subdirectory(tests)
89- endif()
···1diff --git a/CMakeLists.txt b/CMakeLists.txt
2+index b6ddbe90516d..311ab1d50e7f 100644
3--- a/CMakeLists.txt
4+++ b/CMakeLists.txt
5+@@ -29,7 +29,7 @@ if (OPENMP_STANDALONE_BUILD)
0000000006 set(OPENMP_LIBDIR_SUFFIX "" CACHE STRING
7 "Suffix of lib installation directory, e.g. 64 => lib64")
8 # Do not use OPENMP_LIBDIR_SUFFIX directly, use OPENMP_INSTALL_LIBDIR.
···1112 # Group test settings.
13 set(OPENMP_TEST_C_COMPILER ${CMAKE_C_COMPILER} CACHE STRING
14+@@ -40,7 +40,7 @@ if (OPENMP_STANDALONE_BUILD)
15 else()
16 set(OPENMP_ENABLE_WERROR ${LLVM_ENABLE_WERROR})
17 # If building in tree, we honor the same install suffix LLVM uses.
···2021 if (NOT MSVC)
22 set(OPENMP_TEST_C_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
0000000000000000000000000000000000000000000000000000000000