summaryrefslogtreecommitdiff
path: root/cmake
diff options
context:
space:
mode:
authorMartell Malone <martellmalone@gmail.com>2017-06-01 00:09:20 +0000
committerMartell Malone <martellmalone@gmail.com>2017-06-01 00:09:20 +0000
commit380a70a942cdad1ba468f7e0836d48a874837ab2 (patch)
treeeca2740bc890bb4b190e5f92bffa3307c25708c8 /cmake
parent7512331622d78a262298560330e4a39d69892203 (diff)
[libcxxabi] Rework CMakeLists.txt into modules
Refactor cmake to remove dependence on LLVM's cmake modules. This improves handling of cmake checks when cross compiling and brings libcxxabi in line with libcxx and other project modules. Differential revision: https://reviews.llvm.org/D33635 git-svn-id: https://llvm.org/svn/llvm-project/libcxxabi/trunk@304374 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'cmake')
-rw-r--r--cmake/Modules/HandleLibcxxabiFlags.cmake208
-rw-r--r--cmake/Modules/HandleOutOfTreeLLVM.cmake130
-rw-r--r--cmake/Modules/MacroEnsureOutOfSourceBuild.cmake18
3 files changed, 356 insertions, 0 deletions
diff --git a/cmake/Modules/HandleLibcxxabiFlags.cmake b/cmake/Modules/HandleLibcxxabiFlags.cmake
new file mode 100644
index 0000000..3eddd7b
--- /dev/null
+++ b/cmake/Modules/HandleLibcxxabiFlags.cmake
@@ -0,0 +1,208 @@
+# HandleLibcxxFlags - A set of macros used to setup the flags used to compile
+# and link libc++abi. These macros add flags to the following CMake variables.
+# - LIBCXXABI_COMPILE_FLAGS: flags used to compile libc++abi
+# - LIBCXXABI_LINK_FLAGS: flags used to link libc++abi
+# - LIBCXXABI_LIBRARIES: libraries to link libc++abi to.
+
+include(CheckCXXCompilerFlag)
+
+unset(add_flag_if_supported)
+
+# Mangle the name of a compiler flag into a valid CMake identifier.
+# Ex: --std=c++11 -> STD_EQ_CXX11
+macro(mangle_name str output)
+ string(STRIP "${str}" strippedStr)
+ string(REGEX REPLACE "^/" "" strippedStr "${strippedStr}")
+ string(REGEX REPLACE "^-+" "" strippedStr "${strippedStr}")
+ string(REGEX REPLACE "-+$" "" strippedStr "${strippedStr}")
+ string(REPLACE "-" "_" strippedStr "${strippedStr}")
+ string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}")
+ string(REPLACE "+" "X" strippedStr "${strippedStr}")
+ string(TOUPPER "${strippedStr}" ${output})
+endmacro()
+
+# Remove a list of flags from all CMake variables that affect compile flags.
+# This can be used to remove unwanted flags specified on the command line
+# or added in other parts of LLVM's cmake configuration.
+macro(remove_flags)
+ foreach(var ${ARGN})
+ string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
+ string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
+ string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
+ string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
+ string(REPLACE "${var}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
+ string(REPLACE "${var}" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
+ string(REPLACE "${var}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
+ string(REPLACE "${var}" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
+ string(REPLACE "${var}" "" CMAKE_SHARED_MODULE_FLAGS "${CMAKE_SHARED_MODULE_FLAGS}")
+ remove_definitions(${var})
+ endforeach()
+endmacro(remove_flags)
+
+macro(check_flag_supported flag)
+ mangle_name("${flag}" flagname)
+ check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG")
+endmacro()
+
+# Add a macro definition if condition is true.
+macro(define_if condition def)
+ if (${condition})
+ add_definitions(${def})
+ endif()
+endmacro()
+
+# Add a macro definition if condition is not true.
+macro(define_if_not condition def)
+ if (NOT ${condition})
+ add_definitions(${def})
+ endif()
+endmacro()
+
+# Add a macro definition to the __config_site file if the specified condition
+# is 'true'. Note that '-D${def}' is not added. Instead it is expected that
+# the build include the '__config_site' header.
+macro(config_define_if condition def)
+ if (${condition})
+ set(${def} ON)
+ set(LIBCXXABI_NEEDS_SITE_CONFIG ON)
+ endif()
+endmacro()
+
+macro(config_define_if_not condition def)
+ if (NOT ${condition})
+ set(${def} ON)
+ set(LIBCXXABI_NEEDS_SITE_CONFIG ON)
+ endif()
+endmacro()
+
+macro(config_define value def)
+ set(${def} ${value})
+ set(LIBCXXABI_NEEDS_SITE_CONFIG ON)
+endmacro()
+
+# Add a list of flags to all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS',
+# 'LIBCXXABI_COMPILE_FLAGS' and 'LIBCXXABI_LINK_FLAGS'.
+macro(add_target_flags)
+ foreach(value ${ARGN})
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${value}")
+ list(APPEND LIBCXXABI_COMPILE_FLAGS ${value})
+ list(APPEND LIBCXXABI_LINK_FLAGS ${value})
+ endforeach()
+endmacro()
+
+# If the specified 'condition' is true then add a list of flags to
+# all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBCXXABI_COMPILE_FLAGS'
+# and 'LIBCXXABI_LINK_FLAGS'.
+macro(add_target_flags_if condition)
+ if (${condition})
+ add_target_flags(${ARGN})
+ endif()
+endmacro()
+
+# Add a specified list of flags to both 'LIBCXXABI_COMPILE_FLAGS' and
+# 'LIBCXXABI_LINK_FLAGS'.
+macro(add_flags)
+ foreach(value ${ARGN})
+ list(APPEND LIBCXXABI_COMPILE_FLAGS ${value})
+ list(APPEND LIBCXXABI_LINK_FLAGS ${value})
+ endforeach()
+endmacro()
+
+# If the specified 'condition' is true then add a list of flags to both
+# 'LIBCXXABI_COMPILE_FLAGS' and 'LIBCXXABI_LINK_FLAGS'.
+macro(add_flags_if condition)
+ if (${condition})
+ add_flags(${ARGN})
+ endif()
+endmacro()
+
+# Add each flag in the list to LIBCXXABI_COMPILE_FLAGS and LIBCXXABI_LINK_FLAGS
+# if that flag is supported by the current compiler.
+macro(add_flags_if_supported)
+ foreach(flag ${ARGN})
+ mangle_name("${flag}" flagname)
+ check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG")
+ add_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag})
+ endforeach()
+endmacro()
+
+# Add a list of flags to 'LIBCXXABI_COMPILE_FLAGS'.
+macro(add_compile_flags)
+ foreach(f ${ARGN})
+ list(APPEND LIBCXXABI_COMPILE_FLAGS ${f})
+ endforeach()
+endmacro()
+
+# If 'condition' is true then add the specified list of flags to
+# 'LIBCXXABI_COMPILE_FLAGS'
+macro(add_compile_flags_if condition)
+ if (${condition})
+ add_compile_flags(${ARGN})
+ endif()
+endmacro()
+
+# For each specified flag, add that flag to 'LIBCXXABI_COMPILE_FLAGS' if the
+# flag is supported by the C++ compiler.
+macro(add_compile_flags_if_supported)
+ foreach(flag ${ARGN})
+ mangle_name("${flag}" flagname)
+ check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG")
+ add_compile_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag})
+ endforeach()
+endmacro()
+
+# For each specified flag, add that flag to 'LIBCXXABI_COMPILE_FLAGS' if the
+# flag is supported by the C compiler.
+macro(add_c_compile_flags_if_supported)
+ foreach(flag ${ARGN})
+ mangle_name("${flag}" flagname)
+ check_c_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG")
+ add_compile_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag})
+ endforeach()
+endmacro()
+
+# Add a list of flags to 'LIBCXXABI_LINK_FLAGS'.
+macro(add_link_flags)
+ foreach(f ${ARGN})
+ list(APPEND LIBCXXABI_LINK_FLAGS ${f})
+ endforeach()
+endmacro()
+
+# If 'condition' is true then add the specified list of flags to
+# 'LIBCXXABI_LINK_FLAGS'
+macro(add_link_flags_if condition)
+ if (${condition})
+ add_link_flags(${ARGN})
+ endif()
+endmacro()
+
+# For each specified flag, add that flag to 'LIBCXXABI_LINK_FLAGS' if the
+# flag is supported by the C++ compiler.
+macro(add_link_flags_if_supported)
+ foreach(flag ${ARGN})
+ mangle_name("${flag}" flagname)
+ check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG")
+ add_link_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag})
+ endforeach()
+endmacro()
+
+# Add a list of libraries or link flags to 'LIBCXXABI_LIBRARIES'.
+macro(add_library_flags)
+ foreach(lib ${ARGN})
+ list(APPEND LIBCXXABI_LIBRARIES ${lib})
+ endforeach()
+endmacro()
+
+# if 'condition' is true then add the specified list of libraries and flags
+# to 'LIBCXXABI_LIBRARIES'.
+macro(add_library_flags_if condition)
+ if(${condition})
+ add_library_flags(${ARGN})
+ endif()
+endmacro()
+
+# Turn a comma separated CMake list into a space separated string.
+macro(split_list listname)
+ string(REPLACE ";" " " ${listname} "${${listname}}")
+endmacro()
diff --git a/cmake/Modules/HandleOutOfTreeLLVM.cmake b/cmake/Modules/HandleOutOfTreeLLVM.cmake
new file mode 100644
index 0000000..1dcaf90
--- /dev/null
+++ b/cmake/Modules/HandleOutOfTreeLLVM.cmake
@@ -0,0 +1,130 @@
+macro(find_llvm_parts)
+# Rely on llvm-config.
+ set(CONFIG_OUTPUT)
+ if(NOT LLVM_CONFIG_PATH)
+ find_program(LLVM_CONFIG_PATH "llvm-config")
+ endif()
+ if(DEFINED LLVM_PATH)
+ set(LLVM_INCLUDE_DIR ${LLVM_INCLUDE_DIR} CACHE PATH "Path to llvm/include")
+ set(LLVM_PATH ${LLVM_PATH} CACHE PATH "Path to LLVM source tree")
+ set(LLVM_MAIN_SRC_DIR ${LLVM_PATH})
+ set(LLVM_CMAKE_PATH "${LLVM_PATH}/cmake/modules")
+ elseif(LLVM_CONFIG_PATH)
+ message(STATUS "Found LLVM_CONFIG_PATH as ${LLVM_CONFIG_PATH}")
+ set(LIBCXXABI_USING_INSTALLED_LLVM 1)
+ set(CONFIG_COMMAND ${LLVM_CONFIG_PATH}
+ "--includedir"
+ "--prefix"
+ "--src-root")
+ 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})
+ else()
+ string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
+ message(STATUS "${CONFIG_COMMAND_STR}")
+ message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
+ endif()
+
+ list(GET CONFIG_OUTPUT 0 INCLUDE_DIR)
+ list(GET CONFIG_OUTPUT 1 LLVM_OBJ_ROOT)
+ list(GET CONFIG_OUTPUT 2 MAIN_SRC_DIR)
+
+ set(LLVM_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
+ 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")
+
+ # --cmakedir is supported since llvm r291218 (4.0 release)
+ execute_process(
+ COMMAND ${LLVM_CONFIG_PATH} --cmakedir
+ RESULT_VARIABLE HAD_ERROR
+ OUTPUT_VARIABLE CONFIG_OUTPUT
+ ERROR_QUIET)
+ if(NOT HAD_ERROR)
+ string(STRIP "${CONFIG_OUTPUT}" LLVM_CMAKE_PATH)
+ else()
+ set(LLVM_CMAKE_PATH
+ "${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm")
+ endif()
+ else()
+ set(LLVM_FOUND OFF)
+ message(WARNING "UNSUPPORTED LIBCXXABI CONFIGURATION DETECTED: "
+ "llvm-config not found and LLVM_PATH not defined.\n"
+ "Reconfigure with -DLLVM_CONFIG_PATH=path/to/llvm-config "
+ "or -DLLVM_PATH=path/to/llvm-source-root.")
+ return()
+ endif()
+
+ if (EXISTS "${LLVM_CMAKE_PATH}")
+ list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
+ elseif (EXISTS "${LLVM_MAIN_SRC_DIR}/cmake/modules")
+ list(APPEND CMAKE_MODULE_PATH "${LLVM_MAIN_SRC_DIR}/cmake/modules")
+ else()
+ set(LLVM_FOUND OFF)
+ message(WARNING "Neither ${LLVM_CMAKE_PATH} nor ${LLVM_MAIN_SRC_DIR}/cmake/modules found")
+ return()
+ endif()
+
+ set(LLVM_FOUND ON)
+endmacro(find_llvm_parts)
+
+macro(configure_out_of_tree_llvm)
+ message(STATUS "Configuring for standalone build.")
+ set(LIBCXXABI_STANDALONE_BUILD 1)
+
+ find_llvm_parts()
+
+ # Add LLVM Functions --------------------------------------------------------
+ if (LLVM_FOUND AND LIBCXXABI_USING_INSTALLED_LLVM)
+ include(LLVMConfig) # For TARGET_TRIPLE
+ else()
+ if (WIN32)
+ set(LLVM_ON_UNIX 0)
+ set(LLVM_ON_WIN32 1)
+ else()
+ set(LLVM_ON_UNIX 1)
+ set(LLVM_ON_WIN32 0)
+ endif()
+ endif()
+ if (LLVM_FOUND)
+ include(AddLLVM OPTIONAL)
+ endif()
+
+ # LLVM Options --------------------------------------------------------------
+ if (NOT DEFINED LLVM_INCLUDE_TESTS)
+ set(LLVM_INCLUDE_TESTS ${LLVM_FOUND})
+ endif()
+ if (NOT DEFINED LLVM_INCLUDE_DOCS)
+ set(LLVM_INCLUDE_DOCS ${LLVM_FOUND})
+ endif()
+ if (NOT DEFINED LLVM_ENABLE_SPHINX)
+ set(LLVM_ENABLE_SPHINX OFF)
+ endif()
+
+ # Required LIT Configuration ------------------------------------------------
+ # Define the default arguments to use with 'lit', and an option for the user
+ # to override.
+ set(LIT_ARGS_DEFAULT "-sv --show-xfail --show-unsupported")
+ if (MSVC OR XCODE)
+ set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
+ endif()
+ set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
+
+ # Required doc configuration
+ if (LLVM_ENABLE_SPHINX)
+ find_package(Sphinx REQUIRED)
+ endif()
+
+ if (LLVM_ON_UNIX AND NOT APPLE)
+ set(LLVM_HAVE_LINK_VERSION_SCRIPT 1)
+ else()
+ set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
+ endif()
+endmacro(configure_out_of_tree_llvm)
+
+configure_out_of_tree_llvm()
diff --git a/cmake/Modules/MacroEnsureOutOfSourceBuild.cmake b/cmake/Modules/MacroEnsureOutOfSourceBuild.cmake
new file mode 100644
index 0000000..e75002b
--- /dev/null
+++ b/cmake/Modules/MacroEnsureOutOfSourceBuild.cmake
@@ -0,0 +1,18 @@
+# MACRO_ENSURE_OUT_OF_SOURCE_BUILD(<errorMessage>)
+
+macro( MACRO_ENSURE_OUT_OF_SOURCE_BUILD _errorMessage )
+
+string( COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" _insource )
+if( _insource )
+ message( SEND_ERROR "${_errorMessage}" )
+ message( FATAL_ERROR
+ "In-source builds are not allowed.
+ CMake would overwrite the makefiles distributed with libcxxabi.
+ Please create a directory and run cmake from there, passing the path
+ to this source directory as the last argument.
+ This process created the file `CMakeCache.txt' and the directory `CMakeFiles'.
+ Please delete them."
+ )
+endif( _insource )
+
+endmacro( MACRO_ENSURE_OUT_OF_SOURCE_BUILD )