| 1 |
######################################################################## |
| 2 |
# Experimental CMake build script for Google Test. |
| 3 |
# |
| 4 |
# Consider this a prototype. It will change drastically. For now, |
| 5 |
# this is only for people on the cutting edge. |
| 6 |
# |
| 7 |
# To run the tests for Google Test itself on Linux, use 'make test' or |
| 8 |
# ctest. You can select which tests to run using 'ctest -R regex'. |
| 9 |
# For more options, run 'ctest --help'. |
| 10 |
######################################################################## |
| 11 |
# |
| 12 |
# Project-wide settings |
| 13 |
|
| 14 |
set(LLVM_SUBPROJECT_TITLE "Third-Party/Google Test") |
| 15 |
|
| 16 |
if(LLVM_RUNTIMES_BUILD) |
| 17 |
# This instance of GTest is use for unittests for the runtime libraries. It |
| 18 |
# must not link to LLVMSupport (used for llvm::raw_ostream and llvm::cl |
| 19 |
# support) of the host build: It may be a different architecture and/or |
| 20 |
# using a different C++ ABI such as libcxx/libstdc++. |
| 21 |
set(GTEST_LLVM_COMPONENTS "") |
| 22 |
|
| 23 |
# We cannot use llvm_gtest for the target; it would clash with |
| 24 |
# find_package(LLVM) with LLVM_EXPORT_GTEST=ON. Instead, we define an alias |
| 25 |
# default_gtest that points to llvm_gtest in the LLVM build and |
| 26 |
# runtimes_gtest in an runtimes build. |
| 27 |
set(gtest_name "runtimes_gtest") |
| 28 |
|
| 29 |
# Override locally; never install the runtimes-GTest. |
| 30 |
set(LLVM_INSTALL_GTEST OFF) |
| 31 |
|
| 32 |
# Build the library containing main() so unittests need less boilerplate. |
| 33 |
# UnitTestMain/TestMain.cpp always needs LLVMSupport, use GTest's original |
| 34 |
# main when unavailable. |
| 35 |
set(gtest_main_src googletest/src/gtest_main.cc) |
| 36 |
else() |
| 37 |
set(GTEST_LLVM_COMPONENTS "Support") |
| 38 |
set(gtest_name "llvm_gtest") |
| 39 |
set(gtest_main_src UnitTestMain/TestMain.cpp) |
| 40 |
endif() |
| 41 |
|
| 42 |
if(WIN32) |
| 43 |
add_definitions(-DGTEST_OS_WINDOWS=1) |
| 44 |
endif() |
| 45 |
|
| 46 |
# Google Test requires headers which need _ALL_SOURCE to build on AIX |
| 47 |
if (UNIX AND "${CMAKE_SYSTEM_NAME}" MATCHES "AIX") |
| 48 |
remove_definitions("-D_XOPEN_SOURCE=700") |
| 49 |
add_definitions("-D_ALL_SOURCE") |
| 50 |
endif() |
| 51 |
|
| 52 |
if(SUPPORTS_VARIADIC_MACROS_FLAG) |
| 53 |
add_definitions("-Wno-variadic-macros") |
| 54 |
endif() |
| 55 |
if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG) |
| 56 |
add_definitions("-Wno-gnu-zero-variadic-macro-arguments") |
| 57 |
endif() |
| 58 |
if(CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG) |
| 59 |
add_definitions("-Wno-covered-switch-default") |
| 60 |
endif() |
| 61 |
|
| 62 |
set(LLVM_REQUIRES_RTTI 1) |
| 63 |
add_definitions( -DGTEST_HAS_RTTI=0 ) |
| 64 |
|
| 65 |
if (HAVE_LIBPTHREAD) |
| 66 |
list(APPEND LIBS pthread) |
| 67 |
endif() |
| 68 |
|
| 69 |
# Install GTest only if requested. |
| 70 |
set(BUILDTREE_ONLY BUILDTREE_ONLY) |
| 71 |
if (LLVM_INSTALL_GTEST) |
| 72 |
set(BUILDTREE_ONLY "") |
| 73 |
endif () |
| 74 |
|
| 75 |
add_llvm_library("${gtest_name}" |
| 76 |
googletest/src/gtest-all.cc |
| 77 |
googlemock/src/gmock-all.cc |
| 78 |
|
| 79 |
LINK_LIBS |
| 80 |
${LIBS} |
| 81 |
|
| 82 |
LINK_COMPONENTS |
| 83 |
${GTEST_LLVM_COMPONENTS} |
| 84 |
|
| 85 |
# This is a library meant only for the build tree. |
| 86 |
${BUILDTREE_ONLY} |
| 87 |
) |
| 88 |
|
| 89 |
# The googletest and googlemock sources don't presently use the 'override' |
| 90 |
# keyword, which leads to lots of warnings from -Wsuggest-override. Disable |
| 91 |
# that warning here for any targets that link to gtest. |
| 92 |
if(CXX_SUPPORTS_SUGGEST_OVERRIDE_FLAG) |
| 93 |
add_definitions("-Wno-suggest-override") |
| 94 |
set_target_properties("${gtest_name}" PROPERTIES INTERFACE_COMPILE_OPTIONS "-Wno-suggest-override") |
| 95 |
endif() |
| 96 |
|
| 97 |
if (NOT LLVM_ENABLE_THREADS) |
| 98 |
target_compile_definitions("${gtest_name}" PUBLIC GTEST_HAS_PTHREAD=0) |
| 99 |
endif () |
| 100 |
|
| 101 |
target_include_directories("${gtest_name}" |
| 102 |
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/googletest/include> |
| 103 |
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/googlemock/include> |
| 104 |
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/> |
| 105 |
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/llvm-gtest/> |
| 106 |
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/llvm-gmock/> |
| 107 |
PRIVATE googletest googlemock |
| 108 |
) |
| 109 |
|
| 110 |
if(LLVM_RUNTIMES_BUILD) |
| 111 |
target_compile_definitions("${gtest_name}" PUBLIC GTEST_NO_LLVM_SUPPORT=1) |
| 112 |
else() |
| 113 |
# When used from the buildtree, also force use of buildtree LLVM headers, |
| 114 |
# (instead locally installed version) |
| 115 |
# FIXME: Shouldn't this be done for all LLVM libraries? Currently, LLVM uses a |
| 116 |
# big giant `include_directories( ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})` |
| 117 |
# which CMake does not add to the import library. |
| 118 |
target_include_directories("${gtest_name}" BEFORE |
| 119 |
PUBLIC $<BUILD_INTERFACE:${LLVM_SOURCE_DIR}/include> |
| 120 |
$<BUILD_INTERFACE:${LLVM_BINARY_DIR}/include> |
| 121 |
) |
| 122 |
endif() |
| 123 |
|
| 124 |
|
| 125 |
add_llvm_library("${gtest_name}_main" |
| 126 |
${gtest_main_src} |
| 127 |
|
| 128 |
LINK_LIBS |
| 129 |
"${gtest_name}" |
| 130 |
|
| 131 |
LINK_COMPONENTS |
| 132 |
${GTEST_LLVM_COMPONENTS} |
| 133 |
|
| 134 |
${BUILDTREE_ONLY} |
| 135 |
) |
| 136 |
|
| 137 |
if (LLVM_INSTALL_GTEST) |
| 138 |
install(DIRECTORY googletest/include/gtest/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/llvm-gtest/gtest/" COMPONENT "${gtest_name}") |
| 139 |
install(DIRECTORY googlemock/include/gmock/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/llvm-gmock/gmock/" COMPONENT "${gtest_name}") |
| 140 |
endif() |
| 141 |
|
| 142 |
# When LLVM_LINK_LLVM_DYLIB is enabled, libLLVM.so is added to the interface |
| 143 |
# link libraries for gtest and gtest_main. This means that any target, like |
| 144 |
# unittests for example, that links against gtest will be forced to link |
| 145 |
# against libLLVM.so. In some cases we may want to statically unittests if they |
| 146 |
# need access to symbols that are marked private in libLLVM.so. The only |
| 147 |
# way we can make this work is to remove libLLVM.so from the list of interface |
| 148 |
# link libraries for gtest and then make gtest users responsible for explicitly |
| 149 |
# adding libLLVM.so to their targets link libraries if they need it. |
| 150 |
|
| 151 |
function (gtest_remove_dylib_from_link_interface target) |
| 152 |
get_target_property(interface_libs ${target} INTERFACE_LINK_LIBRARIES) |
| 153 |
if (interface_libs) |
| 154 |
list(REMOVE_ITEM interface_libs LLVM) |
| 155 |
set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${interface_libs}") |
| 156 |
endif() |
| 157 |
endfunction() |
| 158 |
|
| 159 |
if (NOT LLVM_RUNTIMES_BUILD) |
| 160 |
gtest_remove_dylib_from_link_interface("${gtest_name}") |
| 161 |
gtest_remove_dylib_from_link_interface("${gtest_name}_main") |
| 162 |
endif () |
| 163 |
|
| 164 |
# The build processing this file always uses this GTest for unittests. |
| 165 |
# Projects that do not use the LLVM_ENABLE_PROJECTS mechanism, but are |
| 166 |
# standalone-builds using find_package(LLVM) can define these aliases |
| 167 |
# explicitly. |
| 168 |
add_library(default_gtest ALIAS "${gtest_name}") |
| 169 |
add_library(default_gtest_main ALIAS "${gtest_name}_main") |