diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.13.4) +include(GNUInstallDirs) + # CMP0116: Ninja generators transform `DEPFILE`s from `add_custom_command()` # New in CMake 3.20. https://cmake.org/cmake/help/latest/policy/CMP0116.html if(POLICY CMP0116) @@ -70,7 +72,7 @@ # List of all known projects in the mono repo set(LLVM_KNOWN_PROJECTS "${LLVM_ALL_PROJECTS};${LLVM_EXTRA_PROJECTS}") set(LLVM_ENABLE_PROJECTS "" CACHE STRING - "Semicolon-separated list of projects to build (${LLVM_KNOWN_PROJECTS}), or \"all\".") + "Semicolon-separated list of projects to build (${LLVM_KNOWN_PROJECTS}), or \"all\".") foreach(proj ${LLVM_ENABLE_PROJECTS}) if (NOT proj STREQUAL "all" AND NOT proj STREQUAL "llvm" AND NOT "${proj}" IN_LIST LLVM_KNOWN_PROJECTS) MESSAGE(FATAL_ERROR "${proj} isn't a known project: ${LLVM_KNOWN_PROJECTS}") @@ -159,7 +161,7 @@ find_program(CCACHE_PROGRAM ccache) if(CCACHE_PROGRAM) set(LLVM_CCACHE_MAXSIZE "" CACHE STRING "Size of ccache") - set(LLVM_CCACHE_DIR "" CACHE STRING "Directory to keep ccached data") + set(LLVM_CCACHE_DIR "" CACHE PATH "Directory to keep ccached data") set(LLVM_CCACHE_PARAMS "CCACHE_CPP2=yes CCACHE_HASHDIR=yes" CACHE STRING "Parameters to pass through to ccache") @@ -197,10 +199,10 @@ endif() # Add path for custom modules -set(CMAKE_MODULE_PATH - ${CMAKE_MODULE_PATH} +list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_CURRENT_SOURCE_DIR}/cmake" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules" + "${CMAKE_CURRENT_SOURCE_DIR}/../cmake/Modules" ) # Generate a CompilationDatabase (compile_commands.json file) for our build, @@ -283,13 +285,18 @@ set(LLVM_LIBDIR_SUFFIX "" CACHE STRING "Define suffix of library directory name (32/64)" ) -set(LLVM_TOOLS_INSTALL_DIR "bin" CACHE STRING "Path for binary subdirectory (defaults to 'bin')") +set(LLVM_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH + "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')") mark_as_advanced(LLVM_TOOLS_INSTALL_DIR) -set(LLVM_UTILS_INSTALL_DIR "${LLVM_TOOLS_INSTALL_DIR}" CACHE STRING +set(LLVM_UTILS_INSTALL_DIR "${LLVM_TOOLS_INSTALL_DIR}" CACHE PATH "Path to install LLVM utilities (enabled by LLVM_INSTALL_UTILS=ON) (defaults to LLVM_TOOLS_INSTALL_DIR)") mark_as_advanced(LLVM_UTILS_INSTALL_DIR) +set(LLVM_EXAMPLES_INSTALL_DIR "examples" CACHE PATH + "Path for examples subdirectory (enabled by LLVM_BUILD_EXAMPLES=ON) (defaults to 'examples')") +mark_as_advanced(LLVM_EXAMPLES_INSTALL_DIR) + # They are used as destination of target generators. set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin) set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX}) @@ -343,7 +350,7 @@ CACHE STRING "Semicolon-separated list of targets to build, or \"all\".") set(LLVM_EXPERIMENTAL_TARGETS_TO_BUILD "" - CACHE STRING "Semicolon-separated list of experimental targets to build.") + CACHE STRING "Semicolon-separated list of experimental targets to build.") option(BUILD_SHARED_LIBS "Build all libraries as shared libraries instead of static" OFF) @@ -396,7 +403,7 @@ set(LLVM_ENABLE_CURL "OFF" CACHE STRING "Use libcurl for the HTTP client if available. Can be ON, OFF, or FORCE_ON") -set(LLVM_Z3_INSTALL_DIR "" CACHE STRING "Install directory of the Z3 solver.") +set(LLVM_Z3_INSTALL_DIR "" CACHE PATH "Install directory of the Z3 solver.") option(LLVM_ENABLE_Z3_SOLVER "Enable Support for the Z3 constraint solver in LLVM." @@ -605,10 +612,10 @@ option (LLVM_ENABLE_OCAMLDOC "Build OCaml bindings documentation." ON) option (LLVM_ENABLE_BINDINGS "Build bindings." ON) -set(LLVM_INSTALL_DOXYGEN_HTML_DIR "share/doc/llvm/doxygen-html" - CACHE STRING "Doxygen-generated HTML documentation install directory") -set(LLVM_INSTALL_OCAMLDOC_HTML_DIR "share/doc/llvm/ocaml-html" - CACHE STRING "OCamldoc-generated HTML documentation install directory") +set(LLVM_INSTALL_DOXYGEN_HTML_DIR "${CMAKE_INSTALL_DOCDIR}/llvm/doxygen-html" + CACHE PATH "Doxygen-generated HTML documentation install directory") +set(LLVM_INSTALL_OCAMLDOC_HTML_DIR "${CMAKE_INSTALL_DOCDIR}/llvm/ocaml-html" + CACHE PATH "OCamldoc-generated HTML documentation install directory") option (LLVM_BUILD_EXTERNAL_COMPILER_RT "Build compiler-rt as an external project." OFF) @@ -1122,7 +1129,7 @@ if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY) install(DIRECTORY include/llvm include/llvm-c - DESTINATION include + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" COMPONENT llvm-headers FILES_MATCHING PATTERN "*.def" @@ -1133,7 +1140,7 @@ ) install(DIRECTORY ${LLVM_INCLUDE_DIR}/llvm ${LLVM_INCLUDE_DIR}/llvm-c - DESTINATION include + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" COMPONENT llvm-headers FILES_MATCHING PATTERN "*.def" @@ -1147,13 +1154,13 @@ if (LLVM_INSTALL_MODULEMAPS) install(DIRECTORY include/llvm include/llvm-c - DESTINATION include + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" COMPONENT llvm-headers FILES_MATCHING PATTERN "module.modulemap" ) install(FILES include/llvm/module.install.modulemap - DESTINATION include/llvm + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/llvm" COMPONENT llvm-headers RENAME "module.extern.modulemap" ) diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -1,3 +1,4 @@ +include(GNUInstallDirs) include(LLVMDistributionSupport) include(LLVMProcessSources) include(LLVM-Config) @@ -837,7 +838,7 @@ ${export_to_llvmexports} LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name} ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name} - RUNTIME DESTINATION bin COMPONENT ${name}) + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT ${name}) if (NOT LLVM_ENABLE_IDE) add_llvm_install_targets(install-${name} @@ -1270,7 +1271,7 @@ endif() add_llvm_executable(${name} ${ARGN}) if( LLVM_BUILD_EXAMPLES ) - install(TARGETS ${name} RUNTIME DESTINATION examples) + install(TARGETS ${name} RUNTIME DESTINATION "${LLVM_EXAMPLES_INSTALL_DIR}") endif() set_target_properties(${name} PROPERTIES FOLDER "Examples") endmacro(add_llvm_example name) diff --git a/llvm/cmake/modules/AddSphinxTarget.cmake b/llvm/cmake/modules/AddSphinxTarget.cmake --- a/llvm/cmake/modules/AddSphinxTarget.cmake +++ b/llvm/cmake/modules/AddSphinxTarget.cmake @@ -86,8 +86,8 @@ endif() elseif (builder STREQUAL html) string(TOUPPER "${project}" project_upper) - set(${project_upper}_INSTALL_SPHINX_HTML_DIR "share/doc/${project}/html" - CACHE STRING "HTML documentation install directory for ${project}") + set(${project_upper}_INSTALL_SPHINX_HTML_DIR "${CMAKE_INSTALL_DOCDIR}/${project}/html" + CACHE PATH "HTML documentation install directory for ${project}") # '/.' indicates: copy the contents of the directory directly into # the specified destination, without recreating the last component diff --git a/llvm/cmake/modules/CMakeLists.txt b/llvm/cmake/modules/CMakeLists.txt --- a/llvm/cmake/modules/CMakeLists.txt +++ b/llvm/cmake/modules/CMakeLists.txt @@ -1,3 +1,4 @@ +include(ExtendPath) include(LLVMDistributionSupport) set(LLVM_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm) @@ -96,24 +97,37 @@ PATTERN CMakeFiles EXCLUDE ) +# # Generate LLVMConfig.cmake for the install tree. -set(LLVM_CONFIG_CODE " -# Compute the installation prefix from this LLVMConfig.cmake file location. -get_filename_component(LLVM_INSTALL_PREFIX \"\${CMAKE_CURRENT_LIST_FILE}\" PATH)") -# Construct the proper number of get_filename_component(... PATH) -# calls to compute the installation prefix. -string(REGEX REPLACE "/" ";" _count "${LLVM_INSTALL_PACKAGE_DIR}") -foreach(p ${_count}) - set(LLVM_CONFIG_CODE "${LLVM_CONFIG_CODE} -get_filename_component(LLVM_INSTALL_PREFIX \"\${LLVM_INSTALL_PREFIX}\" PATH)") -endforeach(p) -set(LLVM_CONFIG_INCLUDE_DIRS "\${LLVM_INSTALL_PREFIX}/include") +# + +# Find common prefix if relative path, CMAKE_INSTALL_PREFIX if absolute +if(IS_ABSOLUTE "${LLVM_INSTALL_PACKAGE_DIR}") + set(LLVM_CONFIG_CODE + "# Installation prefix is fixed absolute path" + "set(LLVM_INSTALL_PREFIX \"${CMAKE_INSTALL_PREFIX}\"") +else() + set(LLVM_CONFIG_CODE + "# Compute the installation prefix from this LLVMConfig.cmake file location." + "get_filename_component(LLVM_INSTALL_PREFIX \"\${CMAKE_CURRENT_LIST_FILE}\" PATH)") + # Construct the proper number of get_filename_component(... PATH) + # calls to compute the installation prefix. + string(REGEX REPLACE "/" ";" _count "${LLVM_INSTALL_PACKAGE_DIR}") + foreach(p ${_count}) + list(APPEND LLVM_CONFIG_CODE + "get_filename_component(LLVM_INSTALL_PREFIX \"\${LLVM_INSTALL_PREFIX}\" PATH)") + endforeach(p) +endif() +string(REPLACE ";" "\n" LLVM_CONFIG_CODE "${LLVM_CONFIG_CODE}") + +# Set path variables for config file +extend_path(LLVM_CONFIG_INCLUDE_DIRS "\${LLVM_INSTALL_PREFIX}" "${CMAKE_INSTALL_INCLUDEDIR}") set(LLVM_CONFIG_INCLUDE_DIR "${LLVM_CONFIG_INCLUDE_DIRS}") set(LLVM_CONFIG_MAIN_INCLUDE_DIR "${LLVM_CONFIG_INCLUDE_DIRS}") set(LLVM_CONFIG_BINARY_DIR "\${LLVM_INSTALL_PREFIX}") -set(LLVM_CONFIG_CMAKE_DIR "\${LLVM_INSTALL_PREFIX}/${LLVM_INSTALL_PACKAGE_DIR}") -set(LLVM_CONFIG_TOOLS_BINARY_DIR "\${LLVM_INSTALL_PREFIX}/bin") -set(LLVM_CONFIG_LIBRARY_DIR "\${LLVM_INSTALL_PREFIX}/lib\${LLVM_LIBDIR_SUFFIX}") +extend_path(LLVM_CONFIG_CMAKE_DIR "\${LLVM_INSTALL_PREFIX}" "${LLVM_INSTALL_PACKAGE_DIR}") +extend_path(LLVM_CONFIG_TOOLS_BINARY_DIR "\${LLVM_INSTALL_PREFIX}" "${LLVM_TOOLS_INSTALL_DIR}") +extend_path(LLVM_CONFIG_LIBRARY_DIR "\${LLVM_INSTALL_PREFIX}" "lib\${LLVM_LIBDIR_SUFFIX}") # Generate a default location for lit if (LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS) diff --git a/llvm/cmake/modules/LLVMInstallSymlink.cmake b/llvm/cmake/modules/LLVMInstallSymlink.cmake --- a/llvm/cmake/modules/LLVMInstallSymlink.cmake +++ b/llvm/cmake/modules/LLVMInstallSymlink.cmake @@ -2,9 +2,11 @@ # DESTDIR environment variable may be unset at configuration time. # See PR8397. +include(GNUInstallDirs) + function(install_symlink name target outdir) set(DESTDIR $ENV{DESTDIR}) - set(bindir "${DESTDIR}${CMAKE_INSTALL_PREFIX}/${outdir}/") + set(bindir "${DESTDIR}${CMAKE_INSTALL_PREFIX}/${outdir}") message(STATUS "Creating ${name}") diff --git a/llvm/docs/CMake.rst b/llvm/docs/CMake.rst --- a/llvm/docs/CMake.rst +++ b/llvm/docs/CMake.rst @@ -252,6 +252,22 @@ Sets the C++ standard to conform to when building LLVM. Possible values are 14, 17, 20. LLVM Requires C++ 14 or higher. This defaults to 14. +**CMAKE_INSTALL_BINDIR**:PATH + The path to install executables, relative to the *CMAKE_INSTALL_PREFIX*. + Defaults to "bin". + +**CMAKE_INSTALL_INCLUDEDIR**:PATH + The path to install header files, relative to the *CMAKE_INSTALL_PREFIX*. + Defaults to "include". + +**CMAKE_INSTALL_DOCDIR**:PATH + The path to install documentation, relative to the *CMAKE_INSTALL_PREFIX*. + Defaults to "share/doc". + +**CMAKE_INSTALL_MANDIR**:PATH + The path to install manpage files, relative to the *CMAKE_INSTALL_PREFIX*. + Defaults to "share/man". + .. _LLVM-related variables: LLVM-related variables @@ -595,15 +611,15 @@ Install symliks from the cctools tool names to the corresponding LLVM tools. For example, lipo will be symlinked to llvm-lipo. -**LLVM_INSTALL_OCAMLDOC_HTML_DIR**:STRING +**LLVM_INSTALL_OCAMLDOC_HTML_DIR**:PATH The path to install OCamldoc-generated HTML documentation to. This path can either be absolute or relative to the CMAKE_INSTALL_PREFIX. Defaults to - `share/doc/llvm/ocaml-html`. + ``${CMAKE_INSTALL_DOCDIR}/llvm/ocaml-html``. -**LLVM_INSTALL_SPHINX_HTML_DIR**:STRING +**LLVM_INSTALL_SPHINX_HTML_DIR**:PATH The path to install Sphinx-generated HTML documentation to. This path can either be absolute or relative to the CMAKE_INSTALL_PREFIX. Defaults to - `share/doc/llvm/html`. + ``${CMAKE_INSTALL_DOCDIR}/llvm/html``. **LLVM_INSTALL_UTILS**:BOOL If enabled, utility binaries like ``FileCheck`` and ``not`` will be installed @@ -627,8 +643,8 @@ **LLVM_INSTALL_DOXYGEN_HTML_DIR**:STRING The path to install Doxygen-generated HTML documentation to. This path can - either be absolute or relative to the CMAKE_INSTALL_PREFIX. Defaults to - `share/doc/llvm/doxygen-html`. + either be absolute or relative to the ``CMAKE_INSTALL_PREFIX``. Defaults to + ``${CMAKE_INSTALL_DOCDIR}/llvm/doxygen-html``. **LLVM_LINK_LLVM_DYLIB**:BOOL If enabled, tools will be linked with the libLLVM shared library. Defaults @@ -764,6 +780,26 @@ If enabled then sphinx documentation warnings will be treated as errors. Defaults to ON. +Advanced variables +~~~~~~~~~~~~~~~~~~ + +These are niche, and changing them from their defaults is more likely to cause +things to go wrong. They are also unstable across LLVM versions. + +**LLVM_TOOLS_INSTALL_DIR**:PATH + The path to install the main LLVM tools, relative to the *CMAKE_INSTALL_PREFIX*. + Defaults to *CMAKE_INSTALL_BINDIR*. + +**LLVM_UTILS_INSTALL_DIR**:PATH + The path to install auxiliary LLVM utilities, relative to the *CMAKE_INSTALL_PREFIX*. + Only matters if *LLVM_INSTALL_UTILS* is enabled. + Defaults to *LLVM_TOOLS_INSTALL_DIR*. + +**LLVM_EXAMPLES_INSTALL_DIR**:PATH + The path for examples of using LLVM, relative to the *CMAKE_INSTALL_PREFIX*. + Only matters if *LLVM_BUILD_EXAMPLES* is enabled. + Defaults to "examples". + CMake Caches ============ diff --git a/llvm/examples/Bye/CMakeLists.txt b/llvm/examples/Bye/CMakeLists.txt --- a/llvm/examples/Bye/CMakeLists.txt +++ b/llvm/examples/Bye/CMakeLists.txt @@ -14,6 +14,6 @@ BUILDTREE_ONLY ) - install(TARGETS ${name} RUNTIME DESTINATION examples) + install(TARGETS ${name} RUNTIME DESTINATION "${LLVM_EXAMPLES_INSTALL_DIR}") set_target_properties(${name} PROPERTIES FOLDER "Examples") endif() diff --git a/llvm/tools/llvm-config/BuildVariables.inc.in b/llvm/tools/llvm-config/BuildVariables.inc.in --- a/llvm/tools/llvm-config/BuildVariables.inc.in +++ b/llvm/tools/llvm-config/BuildVariables.inc.in @@ -23,6 +23,7 @@ #define LLVM_CXXFLAGS "@LLVM_CXXFLAGS@" #define LLVM_BUILDMODE "@LLVM_BUILDMODE@" #define LLVM_LIBDIR_SUFFIX "@LLVM_LIBDIR_SUFFIX@" +#define LLVM_INSTALL_INCLUDEDIR "@CMAKE_INSTALL_INCLUDEDIR@" #define LLVM_TARGETS_BUILT "@LLVM_TARGETS_BUILT@" #define LLVM_SYSTEM_LIBS "@LLVM_SYSTEM_LIBS@" #define LLVM_BUILD_SYSTEM "@LLVM_BUILD_SYSTEM@" diff --git a/llvm/tools/llvm-config/llvm-config.cpp b/llvm/tools/llvm-config/llvm-config.cpp --- a/llvm/tools/llvm-config/llvm-config.cpp +++ b/llvm/tools/llvm-config/llvm-config.cpp @@ -357,10 +357,16 @@ ("-I" + ActiveIncludeDir + " " + "-I" + ActiveObjRoot + "/include"); } else { ActivePrefix = CurrentExecPrefix; - ActiveIncludeDir = ActivePrefix + "/include"; - SmallString<256> path(LLVM_TOOLS_INSTALL_DIR); - sys::fs::make_absolute(ActivePrefix, path); - ActiveBinDir = std::string(path.str()); + { + SmallString<256> Path(LLVM_INSTALL_INCLUDEDIR); + sys::fs::make_absolute(ActivePrefix, Path); + ActiveIncludeDir = std::string(Path.str()); + } + { + SmallString<256> Path(LLVM_TOOLS_INSTALL_DIR); + sys::fs::make_absolute(ActivePrefix, Path); + ActiveBinDir = std::string(Path.str()); + } ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX; ActiveCMakeDir = ActiveLibDir + "/cmake/llvm"; ActiveIncludeOption = "-I" + ActiveIncludeDir; diff --git a/llvm/tools/lto/CMakeLists.txt b/llvm/tools/lto/CMakeLists.txt --- a/llvm/tools/lto/CMakeLists.txt +++ b/llvm/tools/lto/CMakeLists.txt @@ -33,7 +33,7 @@ ${SOURCES} DEPENDS intrinsics_gen) install(FILES ${LLVM_MAIN_INCLUDE_DIR}/llvm-c/lto.h - DESTINATION include/llvm-c + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/llvm-c" COMPONENT LTO) if (APPLE) diff --git a/llvm/tools/opt-viewer/CMakeLists.txt b/llvm/tools/opt-viewer/CMakeLists.txt --- a/llvm/tools/opt-viewer/CMakeLists.txt +++ b/llvm/tools/opt-viewer/CMakeLists.txt @@ -8,7 +8,7 @@ foreach (file ${files}) install(PROGRAMS ${file} - DESTINATION share/opt-viewer + DESTINATION "${CMAKE_INSTALL_DATADIR}/opt-viewer" COMPONENT opt-viewer) endforeach (file) diff --git a/llvm/tools/remarks-shlib/CMakeLists.txt b/llvm/tools/remarks-shlib/CMakeLists.txt --- a/llvm/tools/remarks-shlib/CMakeLists.txt +++ b/llvm/tools/remarks-shlib/CMakeLists.txt @@ -19,7 +19,7 @@ endif() install(FILES ${LLVM_MAIN_INCLUDE_DIR}/llvm-c/Remarks.h - DESTINATION include/llvm-c + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/llvm-c" COMPONENT Remarks) if (APPLE)