Skip to content
Merged
12 changes: 12 additions & 0 deletions cmake/Modules/AddGTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

function (build_gtest gtest_name)
cmake_parse_arguments(ARG "LLVM_SUPPORT" "" "" ${ARGN})

if (ARG_LLVM_SUPPORT)
set(GTEST_USE_LLVM 1)
else ()
set(GTEST_USE_LLVM 0)
endif ()
add_subdirectory("${LLVM_THIRD_PARTY_DIR}/unittest" "${CMAKE_BINARY_DIR}/third-party/${gtest_name}_gtest")
endfunction ()

4 changes: 2 additions & 2 deletions flang-rt/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ if (CMAKE_CROSSCOMPILING)
return ()
endif ()

if (NOT TARGET llvm_gtest)
if (NOT TARGET default_gtest)
message(WARNING "Flang-RT unittests disabled due to GTest being unavailable; "
"Try LLVM_INSTALL_GTEST=ON for the LLVM build")
"FLANG_RT_INCLUDE_TESTS=ON requires LLVM_INCLUDE_TESTS=ON")
return ()
endif ()

Expand Down
21 changes: 0 additions & 21 deletions flang-rt/unittests/Evaluate/ISO-Fortran-binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "flang-rt/runtime/descriptor.h"
#include "flang/Common/ISO_Fortran_binding_wrapper.h"
#include "flang/Testing/testing.h"
#include "llvm/Support/raw_ostream.h"
#include <type_traits>

using namespace Fortran::runtime;
Expand Down Expand Up @@ -73,26 +72,9 @@ static void AddNoiseToCdesc(CFI_cdesc_t *dv, CFI_rank_t rank) {
}
}

#ifdef VERBOSE
static void DumpTestWorld(const void *bAddr, CFI_attribute_t attr,
CFI_type_t ty, std::size_t eLen, CFI_rank_t rank,
const CFI_index_t *eAddr) {
llvm::outs() << " base_addr: ";
llvm::outs().write_hex(reinterpret_cast<std::intptr_t>(bAddr))
<< " attribute: " << static_cast<int>(attr)
<< " type: " << static_cast<int>(ty) << " elem_len: " << eLen
<< " rank: " << static_cast<int>(rank) << " extent: ";
llvm::outs().write_hex(reinterpret_cast<std::intptr_t>(eAddr)) << '\n';
llvm::outs().flush();
}
#endif

static void check_CFI_establish(CFI_cdesc_t *dv, void *base_addr,
CFI_attribute_t attribute, CFI_type_t type, std::size_t elem_len,
CFI_rank_t rank, const CFI_index_t extents[]) {
#ifdef VERBOSE
DumpTestWorld(base_addr, attribute, type, elem_len, rank, extent);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not even compile (extents)

#endif
// CFI_establish reqs from F2018 section 18.5.5
int retCode{
CFI_establish(dv, base_addr, attribute, type, elem_len, rank, extents)};
Expand Down Expand Up @@ -305,9 +287,6 @@ static void check_CFI_allocate(CFI_cdesc_t *dv,
const CFI_type_t type{dv->type};
const void *base_addr{dv->base_addr};
const int version{dv->version};
#ifdef VERBOSE
DumpTestWorld(base_addr, attribute, type, elem_len, rank, nullptr);
#endif
int retCode{CFI_allocate(dv, lower_bounds, upper_bounds, elem_len)};
Descriptor *desc = reinterpret_cast<Descriptor *>(dv);
if (retCode == CFI_SUCCESS) {
Expand Down
7 changes: 4 additions & 3 deletions flang-rt/unittests/Runtime/AccessTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include "CrashHandlerFixture.h"
#include "gtest/gtest.h"
#include "flang/Runtime/extensions.h"
#include "llvm/ADT/Twine.h"

#include <cstring>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
Expand Down Expand Up @@ -82,8 +82,9 @@ static const char *temp_directory_path() {

static std::string createTemporaryFile(
const char *name, const AccessType &accessType) {
std::string path =
(llvm::Twine{temp_directory_path()} + "/" + addPIDSuffix(name)).str();
std::ostringstream pathS;
pathS << temp_directory_path() << "/" << addPIDSuffix(name);
std::string path = pathS.str();

// O_CREAT | O_EXCL enforces that this file is newly created by this call.
// This feels risky. If we don't have permission to create files in the
Expand Down
11 changes: 5 additions & 6 deletions flang-rt/unittests/Runtime/CrashHandlerFixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
char buffer[1000];
std::vsnprintf(buffer, sizeof buffer, message, ap);
va_end(ap);
llvm::errs()
<< "Test "
<< ::testing::UnitTest::GetInstance()->current_test_info()->name()
<< " crashed in file "
<< (sourceFile ? sourceFile : "unknown source file") << '(' << sourceLine
<< "): " << buffer << '\n';
std::cerr << "Test "
<< ::testing::UnitTest::GetInstance()->current_test_info()->name()
<< " crashed in file "
<< (sourceFile ? sourceFile : "unknown source file") << '('
<< sourceLine << "): " << buffer << '\n';
std::exit(EXIT_FAILURE);
}

Expand Down
4 changes: 2 additions & 2 deletions flang-rt/unittests/Runtime/Descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ TEST(Descriptor, FixedStride) {
extent[0] = 8;
descriptor.Establish(integer, four, data, 1, extent);
ASSERT_EQ(descriptor.rank(), 1);
ASSERT_EQ(descriptor.Elements(), 8);
ASSERT_EQ(descriptor.ElementBytes(), four);
ASSERT_EQ(descriptor.Elements(), 8u);
ASSERT_EQ(descriptor.ElementBytes(), static_cast<unsigned>(four));
ASSERT_EQ(descriptor.GetDimension(0).LowerBound(), 0);
ASSERT_EQ(descriptor.GetDimension(0).ByteStride(), four);
ASSERT_EQ(descriptor.GetDimension(0).Extent(), 8);
Expand Down
1 change: 0 additions & 1 deletion flang-rt/unittests/Runtime/ExternalIOTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "flang/Runtime/io-api.h"
#include "flang/Runtime/main.h"
#include "flang/Runtime/stop.h"
#include "llvm/Support/raw_ostream.h"
#include <cstring>
#include <string_view>

Expand Down
4 changes: 2 additions & 2 deletions libc/benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ function(add_libc_benchmark_unittest target_name)
)
target_link_libraries(${target_name}
PRIVATE
llvm_gtest_main
llvm_gtest
default_gtest_main
default_gtest
${LIBC_BENCHMARKS_UNITTEST_DEPENDS}
)
llvm_update_compile_flags(${target_name})
Expand Down
7 changes: 4 additions & 3 deletions llvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1340,9 +1340,10 @@ if( LLVM_INCLUDE_UTILS )
add_subdirectory(utils/mlgo-utils)
add_subdirectory(utils/llvm-test-mustache-spec)
if( LLVM_INCLUDE_TESTS )
set(LLVM_SUBPROJECT_TITLE "Third-Party/Google Test")
add_subdirectory(${LLVM_THIRD_PARTY_DIR}/unittest ${CMAKE_CURRENT_BINARY_DIR}/third-party/unittest)
set(LLVM_SUBPROJECT_TITLE)
include(AddGTest)
build_gtest(llvm_gtest LLVM_SUPPORT)
add_library(default_gtest ALIAS llvm_gtest)
add_library(default_gtest_main ALIAS llvm_gtest_main)
endif()
else()
if ( LLVM_INCLUDE_TESTS )
Expand Down
2 changes: 1 addition & 1 deletion llvm/cmake/modules/AddLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1802,7 +1802,7 @@ function(add_unittest test_suite test_name)
# libpthreads overrides some standard library symbols, so main
# executable must be linked with it in order to provide consistent
# API for all shared libaries loaded by this executable.
target_link_libraries(${test_name} PRIVATE llvm_gtest_main llvm_gtest ${LLVM_PTHREAD_LIB})
target_link_libraries(${test_name} PRIVATE default_gtest_main default_gtest ${LLVM_PTHREAD_LIB})

add_dependencies(${test_suite} ${test_name})
endfunction()
Expand Down
8 changes: 8 additions & 0 deletions runtimes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ endif()
# This can be used to detect whether we're in the runtimes build.
set(LLVM_RUNTIMES_BUILD ON)

# Make GTest available to all runtimes
if (LLVM_INCLUDE_TESTS)
include(AddGTest)
build_gtest(runtimes_gtest)
add_library(default_gtest ALIAS runtimes_gtest)
add_library(default_gtest_main ALIAS runtimes_gtest_main)
endif ()

foreach(entry ${runtimes})
get_filename_component(projName ${entry} NAME)

Expand Down
53 changes: 39 additions & 14 deletions third-party/unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
#
# Project-wide settings

set(LLVM_SUBPROJECT_TITLE "Third-Party/Google Test")

if (GTEST_USE_LLVM)
set(GTEST_LLVM_COMPONENTS "Support") # For llvm::raw_ostream
else ()
# Override locally; never install a non-LLVM GTest
set(LLVM_INSTALL_GTEST OFF)
endif ()

if(WIN32)
add_definitions(-DGTEST_OS_WINDOWS=1)
endif()
Expand Down Expand Up @@ -48,15 +57,15 @@ if (LLVM_INSTALL_GTEST)
set(BUILDTREE_ONLY "")
endif ()

add_llvm_library(llvm_gtest
add_llvm_library("${gtest_name}"
googletest/src/gtest-all.cc
googlemock/src/gmock-all.cc

LINK_LIBS
${LIBS}

LINK_COMPONENTS
Support # Depends on llvm::raw_ostream
${GTEST_LLVM_COMPONENTS}

# This is a library meant only for the build tree.
${BUILDTREE_ONLY}
Expand All @@ -67,15 +76,15 @@ add_llvm_library(llvm_gtest
# that warning here for any targets that link to gtest.
if(CXX_SUPPORTS_SUGGEST_OVERRIDE_FLAG)
add_definitions("-Wno-suggest-override")
set_target_properties(llvm_gtest PROPERTIES INTERFACE_COMPILE_OPTIONS "-Wno-suggest-override")
set_target_properties("${gtest_name}" PROPERTIES INTERFACE_COMPILE_OPTIONS "-Wno-suggest-override")
endif()

if (NOT LLVM_ENABLE_THREADS)
target_compile_definitions(llvm_gtest PUBLIC GTEST_HAS_PTHREAD=0)
target_compile_definitions("${gtest_name}" PUBLIC GTEST_HAS_PTHREAD=0)
endif ()

# Top-level include directory required for "llvm/Support/raw_os_ostream.h"
target_include_directories(llvm_gtest
target_include_directories("${gtest_name}"
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/googletest/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/googlemock/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/>
Expand All @@ -89,16 +98,32 @@ target_include_directories(llvm_gtest
# FIXME: Shouldn't this be done for all LLVM libraries? Currently, LLVM uses a
# big giant `include_directories( ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})`
# which CMake does not add to the import library.
target_include_directories(llvm_gtest BEFORE
PUBLIC $<BUILD_INTERFACE:${LLVM_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${LLVM_BINARY_DIR}/include>
)
if (GTEST_USE_LLVM)
target_include_directories("${gtest_name}" BEFORE
PUBLIC $<BUILD_INTERFACE:${LLVM_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${LLVM_BINARY_DIR}/include>
)
else ()
target_compile_definitions("${gtest_name}" PUBLIC GTEST_NO_LLVM_SUPPORT=1)
endif ()

add_subdirectory(UnitTestMain)
# Library that contains main()
if (GTEST_USE_LLVM)
add_subdirectory(UnitTestMain)
else ()
add_llvm_library("${gtest_name}_main"
googletest/src/gtest_main.cc

LINK_LIBS
"${gtest_name}"

${BUILDTREE_ONLY}
)
endif ()

if (LLVM_INSTALL_GTEST)
install(DIRECTORY googletest/include/gtest/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/llvm-gtest/gtest/" COMPONENT llvm_gtest)
install(DIRECTORY googlemock/include/gmock/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/llvm-gmock/gmock/" COMPONENT llvm_gtest)
install(DIRECTORY googletest/include/gtest/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/llvm-gtest/gtest/" COMPONENT "${gtest_name}")
install(DIRECTORY googlemock/include/gmock/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/llvm-gmock/gmock/" COMPONENT "${gtest_name}")
endif()

# When LLVM_LINK_LLVM_DYLIB is enabled, libLLVM.so is added to the interface
Expand All @@ -118,5 +143,5 @@ function (gtest_remove_dylib_from_link_interface target)
endif()
endfunction()

gtest_remove_dylib_from_link_interface(llvm_gtest)
gtest_remove_dylib_from_link_interface(llvm_gtest_main)
gtest_remove_dylib_from_link_interface("${gtest_name}")
gtest_remove_dylib_from_link_interface("${gtest_name}_main")
4 changes: 2 additions & 2 deletions third-party/unittest/UnitTestMain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ if (LLVM_INSTALL_GTEST)
set(BUILDTREE_ONLY "")
endif ()

add_llvm_library(llvm_gtest_main
add_llvm_library("${gtest_name}_main"
TestMain.cpp

LINK_LIBS
llvm_gtest
"${gtest_name}"

LINK_COMPONENTS
Support # Depends on llvm::cl
Expand Down
Loading