170 lines · plain
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' or8# ctest. You can select which tests to run using 'ctest -R regex'.9# For more options, run 'ctest --help'.10########################################################################11#12# Project-wide settings13 14set(LLVM_SUBPROJECT_TITLE "Third-Party/Google Test")15 16if(LLVM_RUNTIMES_BUILD)17 # This instance of GTest is use for unittests for the runtime libraries. It18 # must not link to LLVMSupport (used for llvm::raw_ostream and llvm::cl19 # support) of the host build: It may be a different architecture and/or20 # 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 with24 # find_package(LLVM) with LLVM_EXPORT_GTEST=ON. Instead, we define an alias25 # default_gtest that points to llvm_gtest in the LLVM build and26 # 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 original34 # main when unavailable.35 set(gtest_main_src googletest/src/gtest_main.cc)36else()37 set(GTEST_LLVM_COMPONENTS "Support")38 set(gtest_name "llvm_gtest")39 set(gtest_main_src UnitTestMain/TestMain.cpp)40endif()41 42if(WIN32)43 add_definitions(-DGTEST_OS_WINDOWS=1)44endif()45 46# Google Test requires headers which need _ALL_SOURCE to build on AIX47if (UNIX AND "${CMAKE_SYSTEM_NAME}" MATCHES "AIX")48 remove_definitions("-D_XOPEN_SOURCE=700")49 add_definitions("-D_ALL_SOURCE")50endif()51 52if(SUPPORTS_VARIADIC_MACROS_FLAG)53 add_definitions("-Wno-variadic-macros")54endif()55if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG)56 add_definitions("-Wno-gnu-zero-variadic-macro-arguments")57endif()58if(CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)59 add_definitions("-Wno-covered-switch-default")60endif()61 62set(LLVM_REQUIRES_RTTI 1)63add_definitions( -DGTEST_HAS_RTTI=0 )64 65if (HAVE_LIBPTHREAD)66 list(APPEND LIBS pthread)67endif()68 69# Install GTest only if requested.70set(BUILDTREE_ONLY BUILDTREE_ONLY)71if (LLVM_INSTALL_GTEST)72 set(BUILDTREE_ONLY "")73endif ()74 75add_llvm_library("${gtest_name}"76 googletest/src/gtest-all.cc77 googlemock/src/gmock-all.cc78 79 LINK_LIBS80 ${LIBS}81 82 LINK_COMPONENTS83 ${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. Disable91# that warning here for any targets that link to gtest.92if(CXX_SUPPORTS_SUGGEST_OVERRIDE_FLAG)93 add_definitions("-Wno-suggest-override")94 set_target_properties("${gtest_name}" PROPERTIES INTERFACE_COMPILE_OPTIONS "-Wno-suggest-override")95endif()96 97if (NOT LLVM_ENABLE_THREADS)98 target_compile_definitions("${gtest_name}" PUBLIC GTEST_HAS_PTHREAD=0)99endif ()100 101target_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 googlemock108 )109 110if(LLVM_RUNTIMES_BUILD)111 target_compile_definitions("${gtest_name}" PUBLIC GTEST_NO_LLVM_SUPPORT=1)112else()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 a116 # 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}" BEFORE119 PUBLIC $<BUILD_INTERFACE:${LLVM_SOURCE_DIR}/include>120 $<BUILD_INTERFACE:${LLVM_BINARY_DIR}/include>121 )122endif()123 124 125add_llvm_library("${gtest_name}_main"126 ${gtest_main_src}127 128 LINK_LIBS129 "${gtest_name}"130 131 LINK_COMPONENTS132 ${GTEST_LLVM_COMPONENTS}133 134 ${BUILDTREE_ONLY}135)136 137if (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}")140endif()141 142# When LLVM_LINK_LLVM_DYLIB is enabled, libLLVM.so is added to the interface143# link libraries for gtest and gtest_main. This means that any target, like144# unittests for example, that links against gtest will be forced to link145# against libLLVM.so. In some cases we may want to statically unittests if they146# need access to symbols that are marked private in libLLVM.so. The only147# way we can make this work is to remove libLLVM.so from the list of interface148# link libraries for gtest and then make gtest users responsible for explicitly149# adding libLLVM.so to their targets link libraries if they need it.150 151function (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()157endfunction()158 159if (NOT LLVM_RUNTIMES_BUILD)160 gtest_remove_dylib_from_link_interface("${gtest_name}")161 gtest_remove_dylib_from_link_interface("${gtest_name}_main")162endif ()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 are166# standalone-builds using find_package(LLVM) can define these aliases167# explicitly.168add_library(default_gtest ALIAS "${gtest_name}")169add_library(default_gtest_main ALIAS "${gtest_name}_main")170