Skip to content

Commit feeff8a

Browse files
committed
[llvm] Use GNUInstallDirs to support custom installation dirs
This is the patch for LLVM proper in my series for adding GNUInstallDirs support in all project. Additionally: Create a new `CACHE STRING` variable, `LLVM_EXAMPLES_INSTALL_DIR`, to control where the examples are installed on analogy with the other variables. --- This patch supersedes D28234, which tried to do the same thing but hand-rolled without GNUInstallDirs. This patch nearly reverts commit 3 0fc88bf1dc15a72e2d9809d28019d386b7a7cc0, which was a revert of a prior attempt." (I had to add a space here or else Phabricator detects a reference cycle and won't let me do the form submit.) Reviewed By: compnerd Differential Revision: https://reviews.llvm.org/D100810
1 parent d6d3000 commit feeff8a

12 files changed

+81
-27
lines changed

llvm/CMakeLists.txt

+14-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
cmake_minimum_required(VERSION 3.13.4)
44

5+
include(GNUInstallDirs)
6+
57
# CMP0116: Ninja generators transform `DEPFILE`s from `add_custom_command()`
68
# New in CMake 3.20. https://cmake.org/cmake/help/latest/policy/CMP0116.html
79
if(POLICY CMP0116)
@@ -289,13 +291,18 @@ endif()
289291

290292
set(LLVM_LIBDIR_SUFFIX "" CACHE STRING "Define suffix of library directory name (32/64)" )
291293

292-
set(LLVM_TOOLS_INSTALL_DIR "bin" CACHE STRING "Path for binary subdirectory (defaults to 'bin')")
294+
set(LLVM_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE STRING
295+
"Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
293296
mark_as_advanced(LLVM_TOOLS_INSTALL_DIR)
294297

295298
set(LLVM_UTILS_INSTALL_DIR "${LLVM_TOOLS_INSTALL_DIR}" CACHE STRING
296299
"Path to install LLVM utilities (enabled by LLVM_INSTALL_UTILS=ON) (defaults to LLVM_TOOLS_INSTALL_DIR)")
297300
mark_as_advanced(LLVM_UTILS_INSTALL_DIR)
298301

302+
set(LLVM_EXAMPLES_INSTALL_DIR "examples" CACHE STRING
303+
"Path for examples subdirectory (enabled by LLVM_BUILD_EXAMPLES=ON) (defaults to 'examples')")
304+
mark_as_advanced(LLVM_EXAMPLES_INSTALL_DIR)
305+
299306
# They are used as destination of target generators.
300307
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
301308
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
@@ -611,9 +618,9 @@ option (LLVM_ENABLE_SPHINX "Use Sphinx to generate llvm documentation." OFF)
611618
option (LLVM_ENABLE_OCAMLDOC "Build OCaml bindings documentation." ON)
612619
option (LLVM_ENABLE_BINDINGS "Build bindings." ON)
613620

614-
set(LLVM_INSTALL_DOXYGEN_HTML_DIR "share/doc/llvm/doxygen-html"
621+
set(LLVM_INSTALL_DOXYGEN_HTML_DIR "${CMAKE_INSTALL_DOCDIR}/llvm/doxygen-html"
615622
CACHE STRING "Doxygen-generated HTML documentation install directory")
616-
set(LLVM_INSTALL_OCAMLDOC_HTML_DIR "share/doc/llvm/ocaml-html"
623+
set(LLVM_INSTALL_OCAMLDOC_HTML_DIR "${CMAKE_INSTALL_DOCDIR}/llvm/ocaml-html"
617624
CACHE STRING "OCamldoc-generated HTML documentation install directory")
618625

619626
option (LLVM_BUILD_EXTERNAL_COMPILER_RT
@@ -1128,7 +1135,7 @@ endif()
11281135

11291136
if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
11301137
install(DIRECTORY include/llvm include/llvm-c
1131-
DESTINATION include
1138+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
11321139
COMPONENT llvm-headers
11331140
FILES_MATCHING
11341141
PATTERN "*.def"
@@ -1139,7 +1146,7 @@ if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
11391146
)
11401147

11411148
install(DIRECTORY ${LLVM_INCLUDE_DIR}/llvm ${LLVM_INCLUDE_DIR}/llvm-c
1142-
DESTINATION include
1149+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
11431150
COMPONENT llvm-headers
11441151
FILES_MATCHING
11451152
PATTERN "*.def"
@@ -1153,13 +1160,13 @@ if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
11531160

11541161
if (LLVM_INSTALL_MODULEMAPS)
11551162
install(DIRECTORY include/llvm include/llvm-c
1156-
DESTINATION include
1163+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
11571164
COMPONENT llvm-headers
11581165
FILES_MATCHING
11591166
PATTERN "module.modulemap"
11601167
)
11611168
install(FILES include/llvm/module.install.modulemap
1162-
DESTINATION include/llvm
1169+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/llvm"
11631170
COMPONENT llvm-headers
11641171
RENAME "module.extern.modulemap"
11651172
)

llvm/cmake/modules/AddLLVM.cmake

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
include(GNUInstallDirs)
12
include(LLVMDistributionSupport)
23
include(LLVMProcessSources)
34
include(LLVM-Config)
@@ -839,7 +840,7 @@ macro(add_llvm_library name)
839840
${export_to_llvmexports}
840841
LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
841842
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
842-
RUNTIME DESTINATION bin COMPONENT ${name})
843+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT ${name})
843844

844845
if (NOT LLVM_ENABLE_IDE)
845846
add_llvm_install_targets(install-${name}
@@ -1272,7 +1273,7 @@ macro(add_llvm_example name)
12721273
endif()
12731274
add_llvm_executable(${name} ${ARGN})
12741275
if( LLVM_BUILD_EXAMPLES )
1275-
install(TARGETS ${name} RUNTIME DESTINATION examples)
1276+
install(TARGETS ${name} RUNTIME DESTINATION "${LLVM_EXAMPLES_INSTALL_DIR}")
12761277
endif()
12771278
set_target_properties(${name} PROPERTIES FOLDER "Examples")
12781279
endmacro(add_llvm_example name)

llvm/cmake/modules/AddSphinxTarget.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function (add_sphinx_target builder project)
8686
endif()
8787
elseif (builder STREQUAL html)
8888
string(TOUPPER "${project}" project_upper)
89-
set(${project_upper}_INSTALL_SPHINX_HTML_DIR "share/doc/${project}/html"
89+
set(${project_upper}_INSTALL_SPHINX_HTML_DIR "${CMAKE_INSTALL_DOCDIR}/${project}/html"
9090
CACHE STRING "HTML documentation install directory for ${project}")
9191

9292
# '/.' indicates: copy the contents of the directory directly into

llvm/cmake/modules/CMakeLists.txt

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
include(ExtendPath)
12
include(LLVMDistributionSupport)
23
include(FindPrefixFromConfig)
34

@@ -111,7 +112,7 @@ file(COPY .
111112

112113
find_prefix_from_config(LLVM_CONFIG_CODE LLVM_INSTALL_PREFIX "${LLVM_INSTALL_PACKAGE_DIR}")
113114

114-
set(LLVM_CONFIG_MAIN_INCLUDE_DIR "\${LLVM_INSTALL_PREFIX}/include")
115+
extend_path(LLVM_CONFIG_MAIN_INCLUDE_DIR "\${LLVM_INSTALL_PREFIX}" "${CMAKE_INSTALL_INCLUDEDIR}")
115116
# This is the same as the above because the handwritten and generated headers
116117
# are combined in one directory at install time.
117118
set(LLVM_CONFIG_INCLUDE_DIR "${LLVM_CONFIG_MAIN_INCLUDE_DIR}")
@@ -121,16 +122,16 @@ set(LLVM_CONFIG_INCLUDE_DIRS
121122
)
122123
list(REMOVE_DUPLICATES LLVM_CONFIG_INCLUDE_DIRS)
123124

124-
set(LLVM_CONFIG_LIBRARY_DIR "\${LLVM_INSTALL_PREFIX}/lib\${LLVM_LIBDIR_SUFFIX}")
125+
extend_path(LLVM_CONFIG_LIBRARY_DIR "\${LLVM_INSTALL_PREFIX}" "lib\${LLVM_LIBDIR_SUFFIX}")
125126
set(LLVM_CONFIG_LIBRARY_DIRS
126127
"${LLVM_CONFIG_LIBRARY_DIR}"
127128
# FIXME: Should there be other entries here?
128129
)
129130
list(REMOVE_DUPLICATES LLVM_CONFIG_LIBRARY_DIRS)
130131

131132
set(LLVM_CONFIG_BINARY_DIR "\${LLVM_INSTALL_PREFIX}")
132-
set(LLVM_CONFIG_CMAKE_DIR "\${LLVM_INSTALL_PREFIX}/${LLVM_INSTALL_PACKAGE_DIR}")
133-
set(LLVM_CONFIG_TOOLS_BINARY_DIR "\${LLVM_INSTALL_PREFIX}/bin")
133+
extend_path(LLVM_CONFIG_CMAKE_DIR "\${LLVM_INSTALL_PREFIX}" "${LLVM_INSTALL_PACKAGE_DIR}")
134+
extend_path(LLVM_CONFIG_TOOLS_BINARY_DIR "\${LLVM_INSTALL_PREFIX}" "${LLVM_TOOLS_INSTALL_DIR}")
134135

135136
# Generate a default location for lit
136137
if (LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS)

llvm/cmake/modules/LLVMInstallSymlink.cmake

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
# DESTDIR environment variable may be unset at configuration time.
33
# See PR8397.
44

5+
include(GNUInstallDirs)
6+
57
function(install_symlink name target outdir)
68
set(DESTDIR $ENV{DESTDIR})
7-
set(bindir "${DESTDIR}${CMAKE_INSTALL_PREFIX}/${outdir}/")
9+
set(bindir "${DESTDIR}${CMAKE_INSTALL_PREFIX}/${outdir}")
810

911
message(STATUS "Creating ${name}")
1012

llvm/docs/CMake.rst

+40-4
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,22 @@ manual, or execute ``cmake --help-variable VARIABLE_NAME``.
252252
Sets the C++ standard to conform to when building LLVM. Possible values are
253253
14, 17, 20. LLVM Requires C++ 14 or higher. This defaults to 14.
254254

255+
**CMAKE_INSTALL_BINDIR**:PATH
256+
The path to install executables, relative to the *CMAKE_INSTALL_PREFIX*.
257+
Defaults to "bin".
258+
259+
**CMAKE_INSTALL_INCLUDEDIR**:PATH
260+
The path to install header files, relative to the *CMAKE_INSTALL_PREFIX*.
261+
Defaults to "include".
262+
263+
**CMAKE_INSTALL_DOCDIR**:PATH
264+
The path to install documentation, relative to the *CMAKE_INSTALL_PREFIX*.
265+
Defaults to "share/doc".
266+
267+
**CMAKE_INSTALL_MANDIR**:PATH
268+
The path to install manpage files, relative to the *CMAKE_INSTALL_PREFIX*.
269+
Defaults to "share/man".
270+
255271
.. _LLVM-related variables:
256272

257273
LLVM-related variables
@@ -598,12 +614,12 @@ enabled sub-projects. Nearly all of these variable names begin with
598614
**LLVM_INSTALL_OCAMLDOC_HTML_DIR**:STRING
599615
The path to install OCamldoc-generated HTML documentation to. This path can
600616
either be absolute or relative to the CMAKE_INSTALL_PREFIX. Defaults to
601-
`share/doc/llvm/ocaml-html`.
617+
``${CMAKE_INSTALL_DOCDIR}/llvm/ocaml-html``.
602618

603619
**LLVM_INSTALL_SPHINX_HTML_DIR**:STRING
604620
The path to install Sphinx-generated HTML documentation to. This path can
605621
either be absolute or relative to the CMAKE_INSTALL_PREFIX. Defaults to
606-
`share/doc/llvm/html`.
622+
``${CMAKE_INSTALL_DOCDIR}/llvm/html``.
607623

608624
**LLVM_INSTALL_UTILS**:BOOL
609625
If enabled, utility binaries like ``FileCheck`` and ``not`` will be installed
@@ -627,8 +643,8 @@ enabled sub-projects. Nearly all of these variable names begin with
627643

628644
**LLVM_INSTALL_DOXYGEN_HTML_DIR**:STRING
629645
The path to install Doxygen-generated HTML documentation to. This path can
630-
either be absolute or relative to the CMAKE_INSTALL_PREFIX. Defaults to
631-
`share/doc/llvm/doxygen-html`.
646+
either be absolute or relative to the *CMAKE_INSTALL_PREFIX*. Defaults to
647+
``${CMAKE_INSTALL_DOCDIR}/llvm/doxygen-html``.
632648

633649
**LLVM_LINK_LLVM_DYLIB**:BOOL
634650
If enabled, tools will be linked with the libLLVM shared library. Defaults
@@ -764,6 +780,26 @@ enabled sub-projects. Nearly all of these variable names begin with
764780
If enabled then sphinx documentation warnings will be treated as
765781
errors. Defaults to ON.
766782

783+
Advanced variables
784+
~~~~~~~~~~~~~~~~~~
785+
786+
These are niche, and changing them from their defaults is more likely to cause
787+
things to go wrong. They are also unstable across LLVM versions.
788+
789+
**LLVM_TOOLS_INSTALL_DIR**:STRING
790+
The path to install the main LLVM tools, relative to the *CMAKE_INSTALL_PREFIX*.
791+
Defaults to *CMAKE_INSTALL_BINDIR*.
792+
793+
**LLVM_UTILS_INSTALL_DIR**:STRING
794+
The path to install auxiliary LLVM utilities, relative to the *CMAKE_INSTALL_PREFIX*.
795+
Only matters if *LLVM_INSTALL_UTILS* is enabled.
796+
Defaults to *LLVM_TOOLS_INSTALL_DIR*.
797+
798+
**LLVM_EXAMPLES_INSTALL_DIR**:STRING
799+
The path for examples of using LLVM, relative to the *CMAKE_INSTALL_PREFIX*.
800+
Only matters if *LLVM_BUILD_EXAMPLES* is enabled.
801+
Defaults to "examples".
802+
767803
CMake Caches
768804
============
769805

llvm/examples/Bye/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ if (NOT WIN32)
1414
BUILDTREE_ONLY
1515
)
1616

17-
install(TARGETS ${name} RUNTIME DESTINATION examples)
17+
install(TARGETS ${name} RUNTIME DESTINATION "${LLVM_EXAMPLES_INSTALL_DIR}")
1818
set_target_properties(${name} PROPERTIES FOLDER "Examples")
1919
endif()

llvm/tools/llvm-config/BuildVariables.inc.in

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define LLVM_CXXFLAGS "@LLVM_CXXFLAGS@"
2424
#define LLVM_BUILDMODE "@LLVM_BUILDMODE@"
2525
#define LLVM_LIBDIR_SUFFIX "@LLVM_LIBDIR_SUFFIX@"
26+
#define LLVM_INSTALL_INCLUDEDIR "@CMAKE_INSTALL_INCLUDEDIR@"
2627
#define LLVM_TARGETS_BUILT "@LLVM_TARGETS_BUILT@"
2728
#define LLVM_SYSTEM_LIBS "@LLVM_SYSTEM_LIBS@"
2829
#define LLVM_BUILD_SYSTEM "@LLVM_BUILD_SYSTEM@"

llvm/tools/llvm-config/llvm-config.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,16 @@ int main(int argc, char **argv) {
357357
("-I" + ActiveIncludeDir + " " + "-I" + ActiveObjRoot + "/include");
358358
} else {
359359
ActivePrefix = CurrentExecPrefix;
360-
ActiveIncludeDir = ActivePrefix + "/include";
361-
SmallString<256> path(LLVM_TOOLS_INSTALL_DIR);
362-
sys::fs::make_absolute(ActivePrefix, path);
363-
ActiveBinDir = std::string(path.str());
360+
{
361+
SmallString<256> Path(LLVM_INSTALL_INCLUDEDIR);
362+
sys::fs::make_absolute(ActivePrefix, Path);
363+
ActiveIncludeDir = std::string(Path.str());
364+
}
365+
{
366+
SmallString<256> Path(LLVM_TOOLS_INSTALL_DIR);
367+
sys::fs::make_absolute(ActivePrefix, Path);
368+
ActiveBinDir = std::string(Path.str());
369+
}
364370
ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
365371
ActiveCMakeDir = ActiveLibDir + "/cmake/llvm";
366372
ActiveIncludeOption = "-I" + ActiveIncludeDir;

llvm/tools/lto/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ add_llvm_library(${LTO_LIBRARY_NAME} ${LTO_LIBRARY_TYPE} INSTALL_WITH_TOOLCHAIN
3333
${SOURCES} DEPENDS intrinsics_gen)
3434

3535
install(FILES ${LLVM_MAIN_INCLUDE_DIR}/llvm-c/lto.h
36-
DESTINATION include/llvm-c
36+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/llvm-c"
3737
COMPONENT LTO)
3838

3939
if (APPLE)

llvm/tools/opt-viewer/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set (files
88

99
foreach (file ${files})
1010
install(PROGRAMS ${file}
11-
DESTINATION share/opt-viewer
11+
DESTINATION "${CMAKE_INSTALL_DATADIR}/opt-viewer"
1212
COMPONENT opt-viewer)
1313
endforeach (file)
1414

llvm/tools/remarks-shlib/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if(LLVM_ENABLE_PIC)
1919
endif()
2020

2121
install(FILES ${LLVM_MAIN_INCLUDE_DIR}/llvm-c/Remarks.h
22-
DESTINATION include/llvm-c
22+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/llvm-c"
2323
COMPONENT Remarks)
2424

2525
if (APPLE)

0 commit comments

Comments
 (0)