|
| 1 | +##===----------------------------------------------------------------------===## |
| 2 | +# |
| 3 | +# Build OMPT unit testing library: ompTest |
| 4 | +# |
| 5 | +##===----------------------------------------------------------------------===## |
| 6 | + |
| 7 | +cmake_minimum_required(VERSION 3.20) |
| 8 | +project(omptest LANGUAGES CXX) |
| 9 | + |
| 10 | +option(LIBOMPTEST_BUILD_STANDALONE |
| 11 | + "Build ompTest 'standalone', i.e. w/o GoogleTest." |
| 12 | + ${OPENMP_STANDALONE_BUILD}) |
| 13 | +option(LIBOMPTEST_BUILD_UNITTESTS |
| 14 | + "Build ompTest's unit tests, requires GoogleTest." OFF) |
| 15 | + |
| 16 | +# In absence of corresponding OMPT support: exit early |
| 17 | +if(NOT ${LIBOMP_OMPT_SUPPORT}) |
| 18 | + return() |
| 19 | +endif() |
| 20 | + |
| 21 | +include(CMakePackageConfigHelpers) |
| 22 | + |
| 23 | +set(OMPTEST_HEADERS |
| 24 | + ./include/AssertMacros.h |
| 25 | + ./include/InternalEvent.h |
| 26 | + ./include/InternalEventCommon.h |
| 27 | + ./include/Logging.h |
| 28 | + ./include/OmptAliases.h |
| 29 | + ./include/OmptAsserter.h |
| 30 | + ./include/OmptAssertEvent.h |
| 31 | + ./include/OmptCallbackHandler.h |
| 32 | + ./include/OmptTester.h |
| 33 | + ./include/OmptTesterGlobals.h |
| 34 | +) |
| 35 | + |
| 36 | +add_library(omptest |
| 37 | + SHARED |
| 38 | + |
| 39 | + ${OMPTEST_HEADERS} |
| 40 | + ./src/InternalEvent.cpp |
| 41 | + ./src/InternalEventOperators.cpp |
| 42 | + ./src/Logging.cpp |
| 43 | + ./src/OmptAsserter.cpp |
| 44 | + ./src/OmptAssertEvent.cpp |
| 45 | + ./src/OmptCallbackHandler.cpp |
| 46 | + ./src/OmptTester.cpp |
| 47 | +) |
| 48 | + |
| 49 | +# Target: ompTest library |
| 50 | +# On (implicit) request of GoogleTest, link against the one provided with LLVM. |
| 51 | +if ((NOT LIBOMPTEST_BUILD_STANDALONE) OR LIBOMPTEST_BUILD_UNITTESTS) |
| 52 | + # Check if standalone build was requested together with unittests |
| 53 | + if (LIBOMPTEST_BUILD_STANDALONE) |
| 54 | + # Emit warning: this build actually depends on LLVM's GoogleTest |
| 55 | + message(WARNING "LIBOMPTEST_BUILD_STANDALONE and LIBOMPTEST_BUILD_UNITTESTS" |
| 56 | + " requested simultaneously.\n" |
| 57 | + "Linking against LLVM's GoogleTest library archives.\n" |
| 58 | + "Disable LIBOMPTEST_BUILD_UNITTESTS to perform an actual" |
| 59 | + " standalone build.") |
| 60 | + # Explicitly disable LIBOMPTEST_BUILD_STANDALONE |
| 61 | + set(LIBOMPTEST_BUILD_STANDALONE OFF) |
| 62 | + endif() |
| 63 | + |
| 64 | + # Make sure target llvm_gtest is available |
| 65 | + if (NOT TARGET llvm_gtest) |
| 66 | + message(FATAL_ERROR "Required target not found: llvm_gtest") |
| 67 | + endif() |
| 68 | + |
| 69 | + # Add llvm_gtest as dependency |
| 70 | + add_dependencies(omptest llvm_gtest) |
| 71 | + |
| 72 | + # Link llvm_gtest as whole-archive to expose required symbols |
| 73 | + set(GTEST_LINK_CMD "-Wl,--whole-archive" llvm_gtest |
| 74 | + "-Wl,--no-whole-archive" LLVMSupport) |
| 75 | + |
| 76 | + # Add GoogleTest-based header |
| 77 | + target_sources(omptest PRIVATE ./include/OmptTesterGoogleTest.h) |
| 78 | + |
| 79 | + # Add LLVM-provided GoogleTest include directories. |
| 80 | + target_include_directories(omptest PRIVATE |
| 81 | + ${LLVM_THIRD_PARTY_DIR}/unittest/googletest/include) |
| 82 | + |
| 83 | + # TODO: Re-visit ABI breaking checks, disable for now. |
| 84 | + target_compile_definitions(omptest PUBLIC |
| 85 | + -DLLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING) |
| 86 | + |
| 87 | + # Link against gtest and gtest_main |
| 88 | + target_link_libraries(omptest PRIVATE ${GTEST_LINK_CMD}) |
| 89 | +else() |
| 90 | + # Add 'standalone' compile definitions |
| 91 | + target_compile_definitions(omptest PRIVATE |
| 92 | + -DOPENMP_LIBOMPTEST_BUILD_STANDALONE) |
| 93 | + |
| 94 | + # Add 'standalone' source files |
| 95 | + target_sources(omptest PRIVATE |
| 96 | + ./include/OmptTesterStandalone.h |
| 97 | + ./src/OmptTesterStandalone.cpp) |
| 98 | +endif() |
| 99 | + |
| 100 | +if(TARGET cxx-headers) |
| 101 | + add_dependencies(omptest cxx-headers) |
| 102 | +endif() |
| 103 | + |
| 104 | +if(TARGET cxx_shared) |
| 105 | + add_dependencies(omptest cxx_shared) |
| 106 | +endif() |
| 107 | + |
| 108 | +if(TARGET cxxabi_shared) |
| 109 | + add_dependencies(omptest cxxabi_shared) |
| 110 | +endif() |
| 111 | + |
| 112 | +# Add common include directories. |
| 113 | +target_include_directories(omptest PUBLIC |
| 114 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> |
| 115 | + $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${LIBOMP_HEADERS_INSTALL_PATH}/omptest> |
| 116 | +) |
| 117 | + |
| 118 | +target_compile_features(omptest PRIVATE cxx_std_17) |
| 119 | + |
| 120 | +# Create and install package configuration files. |
| 121 | +configure_package_config_file( |
| 122 | + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/omptest-config.cmake.in |
| 123 | + ${CMAKE_CURRENT_BINARY_DIR}/cmake/omptest-config.cmake |
| 124 | + INSTALL_DESTINATION "${OPENMP_INSTALL_LIBDIR}/cmake/openmp/omptest" |
| 125 | +) |
| 126 | + |
| 127 | +install(FILES ${omptest_BINARY_DIR}/cmake/omptest-config.cmake |
| 128 | + DESTINATION "${OPENMP_INSTALL_LIBDIR}/cmake/openmp/omptest") |
| 129 | + |
| 130 | +# Install libomptest header files: Copy header-files from include dir |
| 131 | +install(DIRECTORY ./include/ |
| 132 | + DESTINATION "${LIBOMP_HEADERS_INSTALL_PATH}/omptest" |
| 133 | + FILES_MATCHING PATTERN "*.h") |
| 134 | + |
| 135 | +# Install library and export targets. |
| 136 | +# Note: find_package(omptest) may require setting of PATH_SUFFIXES |
| 137 | +# Example: "lib/cmake/openmp/omptest", this is due to the install location |
| 138 | +install(TARGETS omptest |
| 139 | + EXPORT OPENMPomptest |
| 140 | + LIBRARY COMPONENT omptest |
| 141 | + DESTINATION "${OPENMP_INSTALL_LIBDIR}" |
| 142 | + INCLUDES DESTINATION "${LIBOMP_HEADERS_INSTALL_PATH}/omptest") |
| 143 | + |
| 144 | +# Allow to link omptest by using: target_link_libraries( ... omptest::omptest) |
| 145 | +# Additionally, it automatically propagates the include directory. |
| 146 | +install(EXPORT OPENMPomptest |
| 147 | + DESTINATION "${OPENMP_INSTALL_LIBDIR}/cmake/openmp/omptest" |
| 148 | + NAMESPACE omptest:: |
| 149 | + FILE omptest-targets.cmake) |
| 150 | + |
| 151 | +# Discover unit tests (added to check-openmp) |
| 152 | +if(LIBOMPTEST_BUILD_UNITTESTS) |
| 153 | + add_subdirectory(test) |
| 154 | +endif() |
0 commit comments