Skip to content

Commit

Permalink
[CMake] Replace use of llvm-config with LLVM and Clang CMake packages
Browse files Browse the repository at this point in the history
Summary:
I did this for two reasons:
- Using the CMake packages simplifies building LLDB Standalone. This is for two
  reasons: 1) We were doing a decent amount of work that is already done in the
  LLVMConfig.cmake that we want to import, 2) We had to do some manual work to call
  llvm-config, parse its output, and populate variables that the build system
  uses.
- As far as I understand, using llvm-config makes it difficult if not impossible
  to cross-compile LLDB standalone.

Reviewers: sgraenitz, labath, zturner, JDevlieghere, davide, aprantl, stella.stamenova

Subscribers: mgorny, lldb-commits

Differential Revision: https://reviews.llvm.org/D56531

llvm-svn: 351863
  • Loading branch information
bulbazord committed Jan 22, 2019
1 parent b272ec1 commit b8ecd7e
Showing 1 changed file with 20 additions and 77 deletions.
97 changes: 20 additions & 77 deletions lldb/cmake/modules/LLDBStandalone.cmake
Expand Up @@ -5,73 +5,26 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)

option(LLVM_INSTALL_TOOLCHAIN_ONLY "Only include toolchain files in the 'install' target." OFF)

# Rely on llvm-config.
set(CONFIG_OUTPUT)
find_program(LLVM_CONFIG "llvm-config")
if(LLVM_CONFIG)
message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}")
set(CONFIG_COMMAND ${LLVM_CONFIG}
"--assertion-mode"
"--bindir"
"--libdir"
"--includedir"
"--prefix"
"--src-root"
"--cmakedir")
execute_process(
COMMAND ${CONFIG_COMMAND}
RESULT_VARIABLE HAD_ERROR
OUTPUT_VARIABLE CONFIG_OUTPUT
)
if(NOT HAD_ERROR)
string(REGEX REPLACE
"[ \t]*[\r\n]+[ \t]*" ";"
CONFIG_OUTPUT ${CONFIG_OUTPUT})
set(LLDB_PATH_TO_LLVM_BUILD "" CACHE PATH "Path to LLVM build tree")
set(LLDB_PATH_TO_CLANG_BUILD "" CACHE PATH "Path to Clang build tree")

else()
string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
message(STATUS "${CONFIG_COMMAND_STR}")
message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
endif()
else()
message(FATAL_ERROR "llvm-config not found -- ${LLVM_CONFIG}")
endif()
file(TO_CMAKE_PATH LLDB_PATH_TO_LLVM_BUILD "${LLDB_PATH_TO_LLVM_BUILD}")
file(TO_CMAKE_PATH LLDB_PATH_TO_CLANG_BUILD "${LLDB_PATH_TO_CLANG_BUILD}")

list(GET CONFIG_OUTPUT 0 ENABLE_ASSERTIONS)
list(GET CONFIG_OUTPUT 1 TOOLS_BINARY_DIR)
list(GET CONFIG_OUTPUT 2 LIBRARY_DIR)
list(GET CONFIG_OUTPUT 3 INCLUDE_DIR)
list(GET CONFIG_OUTPUT 4 LLVM_OBJ_ROOT)
list(GET CONFIG_OUTPUT 5 MAIN_SRC_DIR)
list(GET CONFIG_OUTPUT 6 LLVM_CMAKE_PATH)
find_package(LLVM REQUIRED CONFIG
HINTS "${LLDB_PATH_TO_LLVM_BUILD}" NO_CMAKE_FIND_ROOT_PATH)
find_package(Clang REQUIRED CONFIG
HINTS "${LLDB_PATH_TO_CLANG_BUILD}" NO_CMAKE_FIND_ROOT_PATH)

if(NOT MSVC_IDE)
set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
CACHE BOOL "Enable assertions")
# Assertions should follow llvm-config's.
mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
endif()
# We set LLVM_MAIN_INCLUDE_DIR so that it gets included in TableGen flags.
set(LLVM_MAIN_INCLUDE_DIR "${LLVM_BUILD_MAIN_INCLUDE_DIR}" CACHE PATH "Path to LLVM's source include dir")
# We set LLVM_CMAKE_PATH so that GetSVN.cmake is found correctly when building SVNVersion.inc
set(LLVM_CMAKE_PATH ${LLVM_CMAKE_DIR} CACHE PATH "Path to LLVM CMake modules")

set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin")
set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib")
set(LLVM_MAIN_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
set(LLVM_DIR ${LLVM_OBJ_ROOT}/cmake/modules/CMakeFiles CACHE PATH "Path to LLVM build tree CMake files")
set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
set(LLVM_EXTERNAL_LIT ${LLVM_TOOLS_BINARY_DIR}/llvm-lit CACHE PATH "Path to llvm-lit")

find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
NO_DEFAULT_PATH)

set(LLVMCONFIG_FILE "${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
if(EXISTS ${LLVMCONFIG_FILE})
file(TO_CMAKE_PATH "${LLVM_CMAKE_PATH}" LLVM_CMAKE_PATH)
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
include(${LLVMCONFIG_FILE})
else()
message(FATAL_ERROR "Not found: ${LLVMCONFIG_FILE}")
endif()

# They are used as destination of target generators.
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
Expand All @@ -82,6 +35,9 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
endif()

# We append the directory in which LLVMConfig.cmake lives. We expect LLVM's
# CMake modules to be in that directory as well.
list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")
include(AddLLVM)
include(TableGen)
include(HandleLLVMOptions)
Expand All @@ -99,28 +55,15 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
message("-- Found PythonInterp: ${PYTHON_EXECUTABLE}")
endif()

# Import CMake library targets from LLVM and Clang.
include("${LLVM_OBJ_ROOT}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm/LLVMConfig.cmake")
if (EXISTS "${LLVM_OBJ_ROOT}/lib${LLVM_LIBDIR_SUFFIX}/cmake/clang/ClangConfig.cmake")
include("${LLVM_OBJ_ROOT}/lib${LLVM_LIBDIR_SUFFIX}/cmake/clang/ClangConfig.cmake")
endif()

set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
set(LLVM_INCLUDE_TESTS ON CACHE INTERNAL "")

set(CMAKE_INCLUDE_CURRENT_DIR ON)
include_directories("${CMAKE_BINARY_DIR}/include" "${LLVM_MAIN_INCLUDE_DIR}")
# Next three include directories are needed when llvm-config is located in build directory.
# LLVM and Clang are assumed to be built together
if (EXISTS "${LLVM_OBJ_ROOT}/include")
include_directories("${LLVM_OBJ_ROOT}/include")
endif()
if (EXISTS "${LLVM_MAIN_SRC_DIR}/tools/clang/include")
include_directories("${LLVM_MAIN_SRC_DIR}/tools/clang/include")
endif()
if (EXISTS "${LLVM_OBJ_ROOT}/tools/clang/include")
include_directories("${LLVM_OBJ_ROOT}/tools/clang/include")
endif()
include_directories(
"${CMAKE_BINARY_DIR}/include"
"${LLVM_INCLUDE_DIRS}"
"${CLANG_INCLUDE_DIRS}")

link_directories("${LLVM_LIBRARY_DIR}")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
Expand Down

0 comments on commit b8ecd7e

Please sign in to comment.