[CMake] Refactor common settings and flags
These are needed by both libraries, so we can do that in a
common namespace and unify configuration parameters.
Also make sure that the user isn't requesting libomptarget
if the library cannot be built on the system. Issue an error
in that case.
Differential Revision: https://reviews.llvm.org/D40081
llvm-svn: 319342
GitOrigin-RevId: 5af381acade8a285ff2e3c8c8e6018d5432116d3
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8795f61..ebf9abb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,5 +1,8 @@
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
+# Add cmake directory to search for custom cmake functions.
+set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
+
# llvm/runtimes/ will set OPENMP_STANDALONE_BUILD.
if (OPENMP_STANDALONE_BUILD OR "${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
set(OPENMP_STANDALONE_BUILD TRUE)
@@ -10,9 +13,25 @@
set(CMAKE_BUILD_TYPE Release)
endif()
+ # Group common settings.
+ set(OPENMP_ENABLE_WERROR FALSE CACHE BOOL
+ "Enable -Werror flags to turn warnings into errors for supporting compilers.")
+ set(OPENMP_LIBDIR_SUFFIX "" CACHE STRING
+ "suffix of lib installation directory, e.g. 64 => lib64")
+
set(OPENMP_LLVM_TOOLS_DIR "" CACHE PATH "Path to LLVM tools for testing.")
+else()
+ set(OPENMP_ENABLE_WERROR ${LLVM_ENABLE_WERROR})
+ # If building in tree, we honor the same install suffix LLVM uses.
+ set(OPENMP_LIBDIR_SUFFIX ${LLVM_LIBDIR_SUFFIX})
endif()
+# Check and set up common compiler flags.
+include(config-ix)
+include(HandleOpenMPOptions)
+
+
+# Build host runtime library.
add_subdirectory(runtime)
@@ -20,12 +39,19 @@
# Currently libomptarget cannot be compiled on Windows or MacOS X.
# Since the device plugins are only supported on Linux anyway,
# there is no point in trying to compile libomptarget on other OSes.
-if (WIN32 OR ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
+if (APPLE OR WIN32 OR NOT OPENMP_HAVE_STD_CPP11_FLAG)
set(ENABLE_LIBOMPTARGET OFF)
endif()
option(OPENMP_ENABLE_LIBOMPTARGET "Enable building libomptarget for offloading."
${ENABLE_LIBOMPTARGET})
if (OPENMP_ENABLE_LIBOMPTARGET)
+ # Check that the library can acutally be built.
+ if (APPLE OR WIN32)
+ message(FATAL_ERROR "libomptarget cannot be built on Windows and MacOS X!")
+ elseif (NOT OPENMP_HAVE_STD_CPP11_FLAG)
+ message(FATAL_ERROR "Host compiler must support C++11 to build libomptarget!")
+ endif()
+
add_subdirectory(libomptarget)
endif()
diff --git a/cmake/HandleOpenMPOptions.cmake b/cmake/HandleOpenMPOptions.cmake
new file mode 100644
index 0000000..5e5215d
--- /dev/null
+++ b/cmake/HandleOpenMPOptions.cmake
@@ -0,0 +1,16 @@
+if (${OPENMP_STANDALONE_BUILD})
+ # From HandleLLVMOptions.cmake
+ function(append_if condition value)
+ if (${condition})
+ foreach(variable ${ARGN})
+ set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
+ endforeach(variable)
+ endif()
+ endfunction()
+endif()
+
+if (${OPENMP_ENABLE_WERROR})
+ append_if(OPENMP_HAVE_WERROR_FLAG "-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
+endif()
+
+append_if(OPENMP_HAVE_STD_CPP11_FLAG "-std=c++11" CMAKE_CXX_FLAGS)
\ No newline at end of file
diff --git a/cmake/config-ix.cmake b/cmake/config-ix.cmake
new file mode 100644
index 0000000..912cbd0
--- /dev/null
+++ b/cmake/config-ix.cmake
@@ -0,0 +1,6 @@
+include(CheckCCompilerFlag)
+include(CheckCXXCompilerFlag)
+
+check_c_compiler_flag(-Werror OPENMP_HAVE_WERROR_FLAG)
+
+check_cxx_compiler_flag(-std=c++11 OPENMP_HAVE_STD_CPP11_FLAG)
\ No newline at end of file
diff --git a/libomptarget/Build_With_CMake.txt b/libomptarget/Build_With_CMake.txt
index 32cb90b..f784b86 100644
--- a/libomptarget/Build_With_CMake.txt
+++ b/libomptarget/Build_With_CMake.txt
@@ -82,7 +82,7 @@
-DCMAKE_BUILD_TYPE=Release|Debug|RelWithDebInfo
Build type can be Release, Debug, or RelWithDebInfo.
--DLIBOMPTARGET_ENABLE_WERROR=true|false
+-DOPENMP_ENABLE_WERROR=true|false
Should consider warnings as errors.
-DLIBOMPTARGET_LLVM_LIT_EXECUTABLE=""
diff --git a/libomptarget/CMakeLists.txt b/libomptarget/CMakeLists.txt
index ef4c182..bff2e59 100644
--- a/libomptarget/CMakeLists.txt
+++ b/libomptarget/CMakeLists.txt
@@ -19,22 +19,10 @@
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules ${CMAKE_MODULE_PATH})
if(OPENMP_STANDALONE_BUILD)
- set(LIBOMPTARGET_ENABLE_WERROR FALSE CACHE BOOL
- "Enable -Werror flags to turn warnings into errors for supporting compilers.")
- set(LIBOMPTARGET_LIBDIR_SUFFIX "" CACHE STRING
- "suffix of lib installation directory, e.g. 64 => lib64")
-
# Build all libraries into a common place so that tests can find them.
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
-else()
- set(LIBOMPTARGET_ENABLE_WERROR ${LLVM_ENABLE_WERROR})
- # If building in tree, we honor the same install suffix LLVM uses.
- set(LIBOMPTARGET_LIBDIR_SUFFIX ${LLVM_LIBDIR_SUFFIX})
endif()
-# Compiler flag checks.
-include(config-ix)
-
# Message utilities.
include(LibomptargetUtils)
@@ -56,55 +44,41 @@
set(LIBOMPTARGET_BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(LIBOMPTARGET_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
-# We need C++11 support.
-if(LIBOMPTARGET_HAVE_STD_CPP11_FLAG)
-
- libomptarget_say("Building offloading runtime library libomptarget.")
+libomptarget_say("Building offloading runtime library libomptarget.")
- # Enable support for C++11.
- add_definitions(-std=c++11)
-
- if(LIBOMPTARGET_ENABLE_WERROR AND LIBOMPTARGET_HAVE_WERROR_FLAG)
- add_definitions(-Werror)
- endif()
-
- # If building this library in debug mode, we define a macro to enable
- # dumping progress messages at runtime.
- string( TOLOWER "${CMAKE_BUILD_TYPE}" LIBOMPTARGET_CMAKE_BUILD_TYPE)
- if(LIBOMPTARGET_CMAKE_BUILD_TYPE MATCHES debug)
- add_definitions(-DOMPTARGET_DEBUG)
- add_definitions(-g)
- add_definitions(-O0)
- endif()
-
- set(src_files
- src/omptarget.cpp
- )
-
- include_directories(src/)
-
- # Build libomptarget library with libdl dependency.
- add_library(omptarget SHARED ${src_files})
- target_link_libraries(omptarget
- ${CMAKE_DL_LIBS}
- "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/exports")
-
- # Install libomptarget under the lib destination folder.
- install(TARGETS omptarget LIBRARY DESTINATION lib${LIBOMPTARGET_LIBDIR_SUFFIX})
-
- # Retrieve the path to the resulting library so that it can be used for
- # testing.
- get_target_property(LIBOMPTARGET_LIBRARY_DIR omptarget LIBRARY_OUTPUT_DIRECTORY)
- if(NOT LIBOMPTARGET_LIBRARY_DIR)
- set(LIBOMPTARGET_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
- endif()
+# If building this library in debug mode, we define a macro to enable
+# dumping progress messages at runtime.
+string( TOLOWER "${CMAKE_BUILD_TYPE}" LIBOMPTARGET_CMAKE_BUILD_TYPE)
+if(LIBOMPTARGET_CMAKE_BUILD_TYPE MATCHES debug)
+ add_definitions(-DOMPTARGET_DEBUG)
+ add_definitions(-g)
+ add_definitions(-O0)
+endif()
- # Build offloading plugins and device RTLs if they are available.
- add_subdirectory(plugins)
-
- # Add tests.
- add_subdirectory(test)
-
-else(LIBOMPTARGET_HAVE_STD_CPP11_FLAG)
- libomptarget_say("Not building offloading runtime library libomptarget: host compiler must have c++11 support.")
-endif(LIBOMPTARGET_HAVE_STD_CPP11_FLAG)
+set(src_files
+ src/omptarget.cpp
+)
+
+include_directories(src/)
+
+# Build libomptarget library with libdl dependency.
+add_library(omptarget SHARED ${src_files})
+target_link_libraries(omptarget
+ ${CMAKE_DL_LIBS}
+ "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/exports")
+
+# Install libomptarget under the lib destination folder.
+install(TARGETS omptarget LIBRARY DESTINATION lib${OPENMP_LIBDIR_SUFFIX})
+
+# Retrieve the path to the resulting library so that it can be used for
+# testing.
+get_target_property(LIBOMPTARGET_LIBRARY_DIR omptarget LIBRARY_OUTPUT_DIRECTORY)
+if(NOT LIBOMPTARGET_LIBRARY_DIR)
+ set(LIBOMPTARGET_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
+endif()
+
+# Build offloading plugins and device RTLs if they are available.
+add_subdirectory(plugins)
+
+# Add tests.
+add_subdirectory(test)
diff --git a/libomptarget/cmake/Modules/config-ix.cmake b/libomptarget/cmake/Modules/config-ix.cmake
deleted file mode 100644
index 9144270..0000000
--- a/libomptarget/cmake/Modules/config-ix.cmake
+++ /dev/null
@@ -1,17 +0,0 @@
-#
-#//===----------------------------------------------------------------------===//
-#//
-#// The LLVM Compiler Infrastructure
-#//
-#// This file is dual licensed under the MIT and the University of Illinois Open
-#// Source Licenses. See LICENSE.txt for details.
-#//
-#//===----------------------------------------------------------------------===//
-#
-
-include(CheckCCompilerFlag)
-include(CheckCXXCompilerFlag)
-
-# Checking C, CXX
-check_cxx_compiler_flag(-std=c++11 LIBOMPTARGET_HAVE_STD_CPP11_FLAG)
-check_c_compiler_flag(-Werror LIBOMPTARGET_HAVE_WERROR_FLAG)
diff --git a/libomptarget/plugins/CMakeLists.txt b/libomptarget/plugins/CMakeLists.txt
index 286c231..6c24d0e 100644
--- a/libomptarget/plugins/CMakeLists.txt
+++ b/libomptarget/plugins/CMakeLists.txt
@@ -37,7 +37,7 @@
# Install plugin under the lib destination folder.
install(TARGETS "omptarget.rtl.${tmachine_libname}"
- LIBRARY DESTINATION lib${LIBOMPTARGET_LIBDIR_SUFFIX})
+ LIBRARY DESTINATION lib${OPENMP_LIBDIR_SUFFIX})
target_link_libraries(
"omptarget.rtl.${tmachine_libname}"
diff --git a/libomptarget/plugins/cuda/CMakeLists.txt b/libomptarget/plugins/cuda/CMakeLists.txt
index a84083c..83ece54 100644
--- a/libomptarget/plugins/cuda/CMakeLists.txt
+++ b/libomptarget/plugins/cuda/CMakeLists.txt
@@ -29,7 +29,7 @@
add_library(omptarget.rtl.cuda SHARED src/rtl.cpp)
# Install plugin under the lib destination folder.
- install(TARGETS omptarget.rtl.cuda LIBRARY DESTINATION lib${LIBOMPTARGET_LIBDIR_SUFFIX})
+ install(TARGETS omptarget.rtl.cuda LIBRARY DESTINATION lib${OPENMP_LIBDIR_SUFFIX})
target_link_libraries(omptarget.rtl.cuda
${LIBOMPTARGET_DEP_CUDA_LIBRARIES}
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt
index 3a64c18..4a659b9 100644
--- a/runtime/CMakeLists.txt
+++ b/runtime/CMakeLists.txt
@@ -32,14 +32,9 @@
libomp_get_architecture(LIBOMP_DETECTED_ARCH)
set(LIBOMP_ARCH ${LIBOMP_DETECTED_ARCH} CACHE STRING
"The architecture to build for (x86_64/i386/arm/ppc64/ppc64le/aarch64/mic/mips/mips64).")
- # Allow user to choose a suffix for the installation directory.
- set(LIBOMP_LIBDIR_SUFFIX "" CACHE STRING
- "suffix of lib installation directory e.g., 64 => lib64")
# Should assertions be enabled? They are on by default.
set(LIBOMP_ENABLE_ASSERTIONS TRUE CACHE BOOL
"enable assertions?")
- set(LIBOMP_ENABLE_WERROR FALSE CACHE BOOL
- "Enable -Werror flags to turn warnings into errors for supporting compilers.")
else() # Part of LLVM build
# Determine the native architecture from LLVM.
string(TOLOWER "${LLVM_TARGET_ARCH}" LIBOMP_NATIVE_ARCH)
@@ -68,9 +63,7 @@
# last ditch effort
libomp_get_architecture(LIBOMP_ARCH)
endif ()
- set(LIBOMP_LIBDIR_SUFFIX ${LLVM_LIBDIR_SUFFIX})
set(LIBOMP_ENABLE_ASSERTIONS ${LLVM_ENABLE_ASSERTIONS})
- set(LIBOMP_ENABLE_WERROR ${LLVM_ENABLE_WERROR})
endif()
libomp_check_variable(LIBOMP_ARCH 32e x86_64 32 i386 arm ppc64 ppc64le aarch64 mic mips mips64)
diff --git a/runtime/cmake/LibompHandleFlags.cmake b/runtime/cmake/LibompHandleFlags.cmake
index acaa1d5..4bdbf8f 100644
--- a/runtime/cmake/LibompHandleFlags.cmake
+++ b/runtime/cmake/LibompHandleFlags.cmake
@@ -26,12 +26,8 @@
# Gets flags common to both the C and C++ compiler
function(libomp_get_c_and_cxxflags_common flags)
set(flags_local)
- libomp_append(flags_local -std=c++11 LIBOMP_HAVE_STD_CPP11_FLAG)
libomp_append(flags_local -fno-exceptions LIBOMP_HAVE_FNO_EXCEPTIONS_FLAG)
libomp_append(flags_local -fno-rtti LIBOMP_HAVE_FNO_RTTI_FLAG)
- if(${LIBOMP_ENABLE_WERROR})
- libomp_append(flags_local -Werror LIBOMP_HAVE_WERROR_FLAG)
- endif()
libomp_append(flags_local -Wno-sign-compare LIBOMP_HAVE_WNO_SIGN_COMPARE_FLAG)
libomp_append(flags_local -Wno-unused-function LIBOMP_HAVE_WNO_UNUSED_FUNCTION_FLAG)
libomp_append(flags_local -Wno-unused-local-typedef LIBOMP_HAVE_WNO_UNUSED_LOCAL_TYPEDEF_FLAG)
diff --git a/runtime/cmake/config-ix.cmake b/runtime/cmake/config-ix.cmake
index eb886ca..a69585b 100644
--- a/runtime/cmake/config-ix.cmake
+++ b/runtime/cmake/config-ix.cmake
@@ -47,11 +47,9 @@
endfunction()
# Checking C, CXX, Linker Flags
-check_cxx_compiler_flag(-std=c++11 LIBOMP_HAVE_STD_CPP11_FLAG)
check_cxx_compiler_flag(-fno-exceptions LIBOMP_HAVE_FNO_EXCEPTIONS_FLAG)
check_cxx_compiler_flag(-fno-rtti LIBOMP_HAVE_FNO_RTTI_FLAG)
check_c_compiler_flag("-x c++" LIBOMP_HAVE_X_CPP_FLAG)
-check_c_compiler_flag(-Werror LIBOMP_HAVE_WERROR_FLAG)
check_c_compiler_flag(-Wunused-function LIBOMP_HAVE_WNO_UNUSED_FUNCTION_FLAG)
check_c_compiler_flag(-Wunused-local-typedef LIBOMP_HAVE_WNO_UNUSED_LOCAL_TYPEDEF_FLAG)
check_c_compiler_flag(-Wunused-value LIBOMP_HAVE_WNO_UNUSED_VALUE_FLAG)
diff --git a/runtime/src/CMakeLists.txt b/runtime/src/CMakeLists.txt
index cb2b184..8545fe2 100644
--- a/runtime/src/CMakeLists.txt
+++ b/runtime/src/CMakeLists.txt
@@ -286,22 +286,22 @@
set(LIBOMP_HEADERS_INSTALL_PATH include)
else()
string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION ${PACKAGE_VERSION})
- set(LIBOMP_HEADERS_INSTALL_PATH lib${LIBOMP_LIBDIR_SUFFIX}/clang/${CLANG_VERSION}/include)
+ set(LIBOMP_HEADERS_INSTALL_PATH lib${OPENMP_LIBDIR_SUFFIX}/clang/${CLANG_VERSION}/include)
endif()
if(WIN32)
install(TARGETS omp RUNTIME DESTINATION bin)
- install(TARGETS ompimp ARCHIVE DESTINATION lib${LIBOMP_LIBDIR_SUFFIX})
+ install(TARGETS ompimp ARCHIVE DESTINATION lib${OPENMP_LIBDIR_SUFFIX})
# Create aliases (regular copies) of the library for backwards compatibility
set(LIBOMP_ALIASES "libiomp5md")
foreach(alias IN LISTS LIBOMP_ALIASES)
install(CODE "execute_process(COMMAND \"\${CMAKE_COMMAND}\" -E copy \"${LIBOMP_LIB_FILE}\"
\"${alias}${LIBOMP_LIBRARY_SUFFIX}\" WORKING_DIRECTORY \${CMAKE_INSTALL_PREFIX}/bin)")
install(CODE "execute_process(COMMAND \"\${CMAKE_COMMAND}\" -E copy \"${LIBOMP_IMP_LIB_FILE}\"
- \"${alias}${LIBOMP_LIBRARY_SUFFIX}\" WORKING_DIRECTORY \${CMAKE_INSTALL_PREFIX}/lib${LIBOMP_LIBDIR_SUFFIX})")
+ \"${alias}${LIBOMP_LIBRARY_SUFFIX}\" WORKING_DIRECTORY \${CMAKE_INSTALL_PREFIX}/lib${OPENMP_LIBDIR_SUFFIX})")
endforeach()
else()
- install(TARGETS omp ${LIBOMP_INSTALL_KIND} DESTINATION lib${LIBOMP_LIBDIR_SUFFIX})
+ install(TARGETS omp ${LIBOMP_INSTALL_KIND} DESTINATION lib${OPENMP_LIBDIR_SUFFIX})
if(${LIBOMP_INSTALL_ALIASES})
# Create aliases (symlinks) of the library for backwards compatibility
@@ -309,7 +309,7 @@
foreach(alias IN LISTS LIBOMP_ALIASES)
install(CODE "execute_process(COMMAND \"\${CMAKE_COMMAND}\" -E create_symlink \"${LIBOMP_LIB_FILE}\"
\"${alias}${LIBOMP_LIBRARY_SUFFIX}\" WORKING_DIRECTORY
- \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/lib${LIBOMP_LIBDIR_SUFFIX})")
+ \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/lib${OPENMP_LIBDIR_SUFFIX})")
endforeach()
endif()
endif()