11diff --git a/CMakeLists.txt b/CMakeLists.txt
2- index 45399dc0537e..5d946e9e6583 100644
2+ index 7e25e0407db2..72f031a82b75 100644
33--- a/CMakeLists.txt
44+++ b/CMakeLists.txt
5- @@ -942 ,7 +942 ,7 @@ if (NOT TENSORFLOW_AOT_PATH STREQUAL "")
5+ @@ -995 ,7 +995 ,7 @@ if (NOT TENSORFLOW_AOT_PATH STREQUAL "")
66 add_subdirectory(${TENSORFLOW_AOT_PATH}/xla_aot_runtime_src
77 ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/tf_runtime)
88 install(TARGETS tf_xla_runtime EXPORT LLVMExports
@@ -12,10 +12,10 @@ index 45399dc0537e..5d946e9e6583 100644
1212 # Once we add more modules, we should handle this more automatically.
1313 if (DEFINED LLVM_OVERRIDE_MODEL_HEADER_INLINERSIZEMODEL)
1414diff --git a/cmake/modules/AddLLVM.cmake b/cmake/modules/AddLLVM.cmake
15- index 057431208322..56f0dcb258da 100644
15+ index 93e6d67551de..8d367457af5a 100644
1616--- a/cmake/modules/AddLLVM.cmake
1717+++ b/cmake/modules/AddLLVM.cmake
18- @@ -844 ,8 +844 ,8 @@ macro(add_llvm_library name)
18+ @@ -874 ,8 +874 ,8 @@ macro(add_llvm_library name)
1919 get_target_export_arg(${name} LLVM export_to_llvmexports ${umbrella})
2020 install(TARGETS ${name}
2121 ${export_to_llvmexports}
@@ -26,7 +26,7 @@ index 057431208322..56f0dcb258da 100644
2626 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT ${name})
2727
2828 if (NOT LLVM_ENABLE_IDE)
29- @@ -2007 ,7 +2007 ,7 @@ function(llvm_install_library_symlink name dest type)
29+ @@ -2043 ,7 +2043 ,7 @@ function(llvm_install_library_symlink name dest type)
3030 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
3131 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
3232
@@ -35,7 +35,7 @@ index 057431208322..56f0dcb258da 100644
3535 if(WIN32 AND "${type}" STREQUAL "SHARED")
3636 set(output_dir "${CMAKE_INSTALL_BINDIR}")
3737 endif()
38- @@ -2271,15 +2271,15 @@ function(llvm_setup_rpath name)
38+ @@ -2312,16 +2312,37 @@ function(llvm_setup_rpath name)
3939
4040 if (APPLE)
4141 set(_install_name_dir INSTALL_NAME_DIR "@rpath")
@@ -49,7 +49,30 @@ index 057431208322..56f0dcb258da 100644
4949- set(_install_rpath "${LLVM_LIBRARY_OUTPUT_INTDIR}" "${CMAKE_INSTALL_PREFIX}/lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
5050+ set(_install_rpath "${LLVM_LIBRARY_OUTPUT_INTDIR}" "${CMAKE_INSTALL_FULL_LIBDIR}${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
5151 elseif(UNIX)
52- - set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
52+ - set(_build_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
53+ - set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}")
54+ + # Note that we add `extra_libdir` (aka `LLVM_LIBRARY_DIR` in our case) back
55+ + # to `_install_rpath` here.
56+ + #
57+ + # In nixpkgs we do not build and install LLVM alongside rdeps of LLVM (i.e.
58+ + # clang); instead LLVM is its own package and thus lands at its own nix
59+ + # store path. This makes it so that the default relative rpath (`../lib/`)
60+ + # does not point at the LLVM shared objects.
61+ + #
62+ + # More discussion here:
63+ + # - https://github.com/NixOS/nixpkgs/pull/235624#discussion_r1220150329
64+ + # - https://reviews.llvm.org/D146918 (16.0.5+)
65+ + #
66+ + # Note that we leave `extra_libdir` in `_build_rpath`: without FHS there is
67+ + # no potential that this will result in us pulling in the "wrong" LLVM.
68+ + # Adding this to the build rpath means we aren't forced to use
69+ + # `installCheckPhase` instead of `checkPhase` (i.e. binaries in the build
70+ + # dir, pre-install, will have the right rpath for LLVM).
71+ + #
72+ + # As noted in the differential above, an alternative solution is to have
73+ + # all rdeps of nixpkgs' LLVM (that use the AddLLVM.cmake machinery) set
74+ + # `CMAKE_INSTALL_RPATH`.
75+ + set(_build_rpath "\$ORIGIN/../${CMAKE_INSTALL_LIBDIR}${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
5376+ set(_install_rpath "\$ORIGIN/../${CMAKE_INSTALL_LIBDIR}${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
5477 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
5578 set_property(TARGET ${name} APPEND_STRING PROPERTY
@@ -71,10 +94,10 @@ index 891c9e6d618c..8d963f3b0069 100644
7194 list(APPEND ocaml_flags "-ldopt" "-Wl,-rpath,${ocaml_rpath}")
7295
7396diff --git a/cmake/modules/CMakeLists.txt b/cmake/modules/CMakeLists.txt
74- index d4b0ab959148..26ed981fd09f 100644
97+ index d99af79aa38e..21e794224b99 100644
7598--- a/cmake/modules/CMakeLists.txt
7699+++ b/cmake/modules/CMakeLists.txt
77- @@ -128 ,7 +128 ,7 @@ set(LLVM_CONFIG_INCLUDE_DIRS
100+ @@ -127 ,7 +127 ,7 @@ set(LLVM_CONFIG_INCLUDE_DIRS
78101 )
79102 list(REMOVE_DUPLICATES LLVM_CONFIG_INCLUDE_DIRS)
80103
@@ -84,7 +107,7 @@ index d4b0ab959148..26ed981fd09f 100644
84107 "${LLVM_CONFIG_LIBRARY_DIR}"
85108 # FIXME: Should there be other entries here?
86109diff --git a/docs/CMake.rst b/docs/CMake.rst
87- index 879b7b231d4c..9c31d14e8950 100644
110+ index 7926de258ec8..5ae01adc3905 100644
88111--- a/docs/CMake.rst
89112+++ b/docs/CMake.rst
90113@@ -250,7 +250,7 @@ description is in `LLVM-related variables`_ below.
@@ -120,10 +143,10 @@ index 370005cd8d7d..7e790bc52111 100644
120143 #define LLVM_INSTALL_PACKAGE_DIR "@LLVM_INSTALL_PACKAGE_DIR@"
121144 #define LLVM_TARGETS_BUILT "@LLVM_TARGETS_BUILT@"
122145diff --git a/tools/llvm-config/llvm-config.cpp b/tools/llvm-config/llvm-config.cpp
123- index 2c6c55f89d38..f6d2068a0827 100644
146+ index b1d795a0a349..de6cb1514f05 100644
124147--- a/tools/llvm-config/llvm-config.cpp
125148+++ b/tools/llvm-config/llvm-config.cpp
126- @@ -369 ,7 +369 ,11 @@ int main(int argc, char **argv) {
149+ @@ -366 ,7 +366 ,11 @@ int main(int argc, char **argv) {
127150 sys::fs::make_absolute(ActivePrefix, Path);
128151 ActiveBinDir = std::string(Path.str());
129152 }
0 commit comments