[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
This commit is contained in:
Jonas Hahnfeld 2017-11-29 19:31:48 +00:00
parent 3e921d3c52
commit 5af381acad
12 changed files with 93 additions and 101 deletions

View File

@ -1,5 +1,8 @@
cmake_minimum_required(VERSION 2.8 FATAL_ERROR) 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. # llvm/runtimes/ will set OPENMP_STANDALONE_BUILD.
if (OPENMP_STANDALONE_BUILD OR "${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") if (OPENMP_STANDALONE_BUILD OR "${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
set(OPENMP_STANDALONE_BUILD TRUE) set(OPENMP_STANDALONE_BUILD TRUE)
@ -10,9 +13,25 @@ if (OPENMP_STANDALONE_BUILD OR "${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_S
set(CMAKE_BUILD_TYPE Release) set(CMAKE_BUILD_TYPE Release)
endif() 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.") 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() endif()
# Check and set up common compiler flags.
include(config-ix)
include(HandleOpenMPOptions)
# Build host runtime library.
add_subdirectory(runtime) add_subdirectory(runtime)
@ -20,12 +39,19 @@ set(ENABLE_LIBOMPTARGET ON)
# Currently libomptarget cannot be compiled on Windows or MacOS X. # Currently libomptarget cannot be compiled on Windows or MacOS X.
# Since the device plugins are only supported on Linux anyway, # Since the device plugins are only supported on Linux anyway,
# there is no point in trying to compile libomptarget on other OSes. # 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) set(ENABLE_LIBOMPTARGET OFF)
endif() endif()
option(OPENMP_ENABLE_LIBOMPTARGET "Enable building libomptarget for offloading." option(OPENMP_ENABLE_LIBOMPTARGET "Enable building libomptarget for offloading."
${ENABLE_LIBOMPTARGET}) ${ENABLE_LIBOMPTARGET})
if (OPENMP_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) add_subdirectory(libomptarget)
endif() endif()

View File

@ -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)

View File

@ -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)

View File

@ -82,7 +82,7 @@ Specify the C++ compiler
-DCMAKE_BUILD_TYPE=Release|Debug|RelWithDebInfo -DCMAKE_BUILD_TYPE=Release|Debug|RelWithDebInfo
Build type can be Release, Debug, or RelWithDebInfo. Build type can be Release, Debug, or RelWithDebInfo.
-DLIBOMPTARGET_ENABLE_WERROR=true|false -DOPENMP_ENABLE_WERROR=true|false
Should consider warnings as errors. Should consider warnings as errors.
-DLIBOMPTARGET_LLVM_LIT_EXECUTABLE="" -DLIBOMPTARGET_LLVM_LIT_EXECUTABLE=""

View File

@ -19,22 +19,10 @@ endif()
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules ${CMAKE_MODULE_PATH}) set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules ${CMAKE_MODULE_PATH})
if(OPENMP_STANDALONE_BUILD) 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. # Build all libraries into a common place so that tests can find them.
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) 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() endif()
# Compiler flag checks.
include(config-ix)
# Message utilities. # Message utilities.
include(LibomptargetUtils) include(LibomptargetUtils)
@ -56,55 +44,41 @@ set (LIBOMPTARGET_SYSTEM_TARGETS "")
set(LIBOMPTARGET_BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) set(LIBOMPTARGET_BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(LIBOMPTARGET_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) set(LIBOMPTARGET_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
# We need C++11 support. libomptarget_say("Building offloading runtime library libomptarget.")
if(LIBOMPTARGET_HAVE_STD_CPP11_FLAG)
libomptarget_say("Building offloading runtime library libomptarget.")
# Enable support for C++11. # If building this library in debug mode, we define a macro to enable
add_definitions(-std=c++11) # dumping progress messages at runtime.
string( TOLOWER "${CMAKE_BUILD_TYPE}" LIBOMPTARGET_CMAKE_BUILD_TYPE)
if(LIBOMPTARGET_ENABLE_WERROR AND LIBOMPTARGET_HAVE_WERROR_FLAG) if(LIBOMPTARGET_CMAKE_BUILD_TYPE MATCHES debug)
add_definitions(-Werror) add_definitions(-DOMPTARGET_DEBUG)
endif() add_definitions(-g)
add_definitions(-O0)
# If building this library in debug mode, we define a macro to enable endif()
# 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()
# Build offloading plugins and device RTLs if they are available. set(src_files
add_subdirectory(plugins) src/omptarget.cpp
)
# Add tests.
add_subdirectory(test) include_directories(src/)
else(LIBOMPTARGET_HAVE_STD_CPP11_FLAG) # Build libomptarget library with libdl dependency.
libomptarget_say("Not building offloading runtime library libomptarget: host compiler must have c++11 support.") add_library(omptarget SHARED ${src_files})
endif(LIBOMPTARGET_HAVE_STD_CPP11_FLAG) 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)

View File

@ -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)

View File

@ -37,7 +37,7 @@ if(CMAKE_SYSTEM_PROCESSOR MATCHES "${tmachine}$")
# Install plugin under the lib destination folder. # Install plugin under the lib destination folder.
install(TARGETS "omptarget.rtl.${tmachine_libname}" install(TARGETS "omptarget.rtl.${tmachine_libname}"
LIBRARY DESTINATION lib${LIBOMPTARGET_LIBDIR_SUFFIX}) LIBRARY DESTINATION lib${OPENMP_LIBDIR_SUFFIX})
target_link_libraries( target_link_libraries(
"omptarget.rtl.${tmachine_libname}" "omptarget.rtl.${tmachine_libname}"

View File

@ -29,7 +29,7 @@ if(LIBOMPTARGET_DEP_LIBELF_FOUND)
add_library(omptarget.rtl.cuda SHARED src/rtl.cpp) add_library(omptarget.rtl.cuda SHARED src/rtl.cpp)
# Install plugin under the lib destination folder. # 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 target_link_libraries(omptarget.rtl.cuda
${LIBOMPTARGET_DEP_CUDA_LIBRARIES} ${LIBOMPTARGET_DEP_CUDA_LIBRARIES}

View File

@ -32,14 +32,9 @@ if(${OPENMP_STANDALONE_BUILD})
libomp_get_architecture(LIBOMP_DETECTED_ARCH) libomp_get_architecture(LIBOMP_DETECTED_ARCH)
set(LIBOMP_ARCH ${LIBOMP_DETECTED_ARCH} CACHE STRING set(LIBOMP_ARCH ${LIBOMP_DETECTED_ARCH} CACHE STRING
"The architecture to build for (x86_64/i386/arm/ppc64/ppc64le/aarch64/mic/mips/mips64).") "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. # Should assertions be enabled? They are on by default.
set(LIBOMP_ENABLE_ASSERTIONS TRUE CACHE BOOL set(LIBOMP_ENABLE_ASSERTIONS TRUE CACHE BOOL
"enable assertions?") "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 else() # Part of LLVM build
# Determine the native architecture from LLVM. # Determine the native architecture from LLVM.
string(TOLOWER "${LLVM_TARGET_ARCH}" LIBOMP_NATIVE_ARCH) string(TOLOWER "${LLVM_TARGET_ARCH}" LIBOMP_NATIVE_ARCH)
@ -68,9 +63,7 @@ else() # Part of LLVM build
# last ditch effort # last ditch effort
libomp_get_architecture(LIBOMP_ARCH) libomp_get_architecture(LIBOMP_ARCH)
endif () endif ()
set(LIBOMP_LIBDIR_SUFFIX ${LLVM_LIBDIR_SUFFIX})
set(LIBOMP_ENABLE_ASSERTIONS ${LLVM_ENABLE_ASSERTIONS}) set(LIBOMP_ENABLE_ASSERTIONS ${LLVM_ENABLE_ASSERTIONS})
set(LIBOMP_ENABLE_WERROR ${LLVM_ENABLE_WERROR})
endif() endif()
libomp_check_variable(LIBOMP_ARCH 32e x86_64 32 i386 arm ppc64 ppc64le aarch64 mic mips mips64) libomp_check_variable(LIBOMP_ARCH 32e x86_64 32 i386 arm ppc64 ppc64le aarch64 mic mips mips64)

View File

@ -26,12 +26,8 @@ endmacro()
# Gets flags common to both the C and C++ compiler # Gets flags common to both the C and C++ compiler
function(libomp_get_c_and_cxxflags_common flags) function(libomp_get_c_and_cxxflags_common flags)
set(flags_local) 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-exceptions LIBOMP_HAVE_FNO_EXCEPTIONS_FLAG)
libomp_append(flags_local -fno-rtti LIBOMP_HAVE_FNO_RTTI_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-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-function LIBOMP_HAVE_WNO_UNUSED_FUNCTION_FLAG)
libomp_append(flags_local -Wno-unused-local-typedef LIBOMP_HAVE_WNO_UNUSED_LOCAL_TYPEDEF_FLAG) libomp_append(flags_local -Wno-unused-local-typedef LIBOMP_HAVE_WNO_UNUSED_LOCAL_TYPEDEF_FLAG)

View File

@ -47,11 +47,9 @@ function(libomp_check_architecture_flag flag retval)
endfunction() endfunction()
# Checking C, CXX, Linker Flags # 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-exceptions LIBOMP_HAVE_FNO_EXCEPTIONS_FLAG)
check_cxx_compiler_flag(-fno-rtti LIBOMP_HAVE_FNO_RTTI_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("-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-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-local-typedef LIBOMP_HAVE_WNO_UNUSED_LOCAL_TYPEDEF_FLAG)
check_c_compiler_flag(-Wunused-value LIBOMP_HAVE_WNO_UNUSED_VALUE_FLAG) check_c_compiler_flag(-Wunused-value LIBOMP_HAVE_WNO_UNUSED_VALUE_FLAG)

View File

@ -286,22 +286,22 @@ if(${OPENMP_STANDALONE_BUILD})
set(LIBOMP_HEADERS_INSTALL_PATH include) set(LIBOMP_HEADERS_INSTALL_PATH include)
else() else()
string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION ${PACKAGE_VERSION}) 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() endif()
if(WIN32) if(WIN32)
install(TARGETS omp RUNTIME DESTINATION bin) 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 # Create aliases (regular copies) of the library for backwards compatibility
set(LIBOMP_ALIASES "libiomp5md") set(LIBOMP_ALIASES "libiomp5md")
foreach(alias IN LISTS LIBOMP_ALIASES) foreach(alias IN LISTS LIBOMP_ALIASES)
install(CODE "execute_process(COMMAND \"\${CMAKE_COMMAND}\" -E copy \"${LIBOMP_LIB_FILE}\" install(CODE "execute_process(COMMAND \"\${CMAKE_COMMAND}\" -E copy \"${LIBOMP_LIB_FILE}\"
\"${alias}${LIBOMP_LIBRARY_SUFFIX}\" WORKING_DIRECTORY \${CMAKE_INSTALL_PREFIX}/bin)") \"${alias}${LIBOMP_LIBRARY_SUFFIX}\" WORKING_DIRECTORY \${CMAKE_INSTALL_PREFIX}/bin)")
install(CODE "execute_process(COMMAND \"\${CMAKE_COMMAND}\" -E copy \"${LIBOMP_IMP_LIB_FILE}\" 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() endforeach()
else() 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}) if(${LIBOMP_INSTALL_ALIASES})
# Create aliases (symlinks) of the library for backwards compatibility # Create aliases (symlinks) of the library for backwards compatibility
@ -309,7 +309,7 @@ else()
foreach(alias IN LISTS LIBOMP_ALIASES) foreach(alias IN LISTS LIBOMP_ALIASES)
install(CODE "execute_process(COMMAND \"\${CMAKE_COMMAND}\" -E create_symlink \"${LIBOMP_LIB_FILE}\" install(CODE "execute_process(COMMAND \"\${CMAKE_COMMAND}\" -E create_symlink \"${LIBOMP_LIB_FILE}\"
\"${alias}${LIBOMP_LIBRARY_SUFFIX}\" WORKING_DIRECTORY \"${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() endforeach()
endif() endif()
endif() endif()