Skip to content

Commit

Permalink
Add a test for the module
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielaE committed May 16, 2021
1 parent 7d4c92f commit c04eb25
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 6 deletions.
43 changes: 41 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ option(FMT_TEST "Generate the test target." ${FMT_MASTER_PROJECT})
option(FMT_FUZZ "Generate the fuzz target." OFF)
option(FMT_CUDA_TEST "Generate the cuda-test target." OFF)
option(FMT_OS "Include core requiring OS (Windows/Posix) " ON)
option(FMT_MODULE "Build a module instead of a traditional library." OFF)

set(FMT_CAN_MODULE OFF)
if (CMAKE_CXX_STANDARD GREATER_EQUAL 20 AND
(MSVC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.29.30035)) # msvc 16.10-pre3
set(FMT_CAN_MODULE ON)
endif ()
if (FMT_CAN_MODULE)
if (MSVC)
set(FMT_BMI "${FMT_BINARY_DIR}/fmt.ifc")
set(BMI_BUILD_FLAGS /interface /ifcOutput ${FMT_BMI})
set(BMI_USE_FLAGS /reference fmt=${FMT_BMI})
endif ()
else ()
set(FMT_MODULE OFF)
message(STATUS "Module support is disabled.")
endif ()

# Get version from core.h
file(READ include/fmt/core.h core_h)
Expand Down Expand Up @@ -188,7 +205,9 @@ endfunction()
# Define the fmt library, its includes and the needed defines.
add_headers(FMT_HEADERS args.h chrono.h color.h compile.h core.h format.h
format-inl.h locale.h os.h ostream.h printf.h ranges.h)
if (FMT_OS)
if (FMT_MODULE)
set(FMT_SOURCES src/fmt.cc)
elseif (FMT_OS)
set(FMT_SOURCES src/format.cc src/os.cc)
else()
set(FMT_SOURCES src/format.cc)
Expand All @@ -214,6 +233,9 @@ endif ()
if (FMT_PEDANTIC)
target_compile_options(fmt PRIVATE ${PEDANTIC_COMPILE_FLAGS})
endif ()
if (FMT_MODULE)
target_compile_options(fmt PRIVATE ${BMI_BUILD_FLAGS} INTERFACE ${BMI_USE_FLAGS})
endif ()

target_compile_features(fmt INTERFACE ${FMT_REQUIRED_FEATURES})

Expand Down Expand Up @@ -257,6 +279,20 @@ target_include_directories(fmt-header-only INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${FMT_INC_DIR}>)

if (FMT_CAN_MODULE AND FMT_TEST)
set (tstmod "modfmt")
add_library(${tstmod} STATIC src/fmt.cc ${FMT_HEADERS})
target_compile_options(${tstmod} PRIVATE ${BMI_BUILD_FLAGS} INTERFACE ${BMI_USE_FLAGS})
target_compile_features(${tstmod} PUBLIC ${FMT_REQUIRED_FEATURES})
target_include_directories(${tstmod} PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${FMT_INC_DIR}>)
set_property(TARGET ${tstmod} APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${FMT_BMI})
set_source_files_properties(${FMT_BMI} PROPERTIES GENERATED ON)
set_target_properties(${tstmod} PROPERTIES
LABEL ${CMAKE_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${tstmod}${CMAKE_STATIC_LIBRARY_SUFFIX})
endif ()

# Install targets.
if (FMT_INSTALL)
include(CMakePackageConfigHelpers)
Expand Down Expand Up @@ -296,7 +332,10 @@ if (FMT_INSTALL)
${project_config}
INSTALL_DESTINATION ${FMT_CMAKE_DIR})

set(INSTALL_TARGETS fmt fmt-header-only)
set(INSTALL_TARGETS fmt)
if (NOT FMT_MODULE)
set(INSTALL_TARGETS ${INSTALL_TARGETS} fmt-header-only)
endif()

# Install the library and headers.
install(TARGETS ${INSTALL_TARGETS} EXPORT ${targets_export_name}
Expand Down
34 changes: 30 additions & 4 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ add_subdirectory(gtest)

set(TEST_MAIN_SRC test-main.cc gtest-extra.cc gtest-extra.h util.cc)
add_library(test-main STATIC ${TEST_MAIN_SRC})
target_link_libraries(test-main gtest fmt)
target_link_libraries(test-main PUBLIC gtest PRIVATE fmt)

include(CheckCXXCompilerFlag)

Expand All @@ -29,17 +29,26 @@ endfunction()
# Adds a test.
# Usage: add_fmt_test(name srcs...)
function(add_fmt_test name)
cmake_parse_arguments(ADD_FMT_TEST "HEADER_ONLY" "" "" ${ARGN})
cmake_parse_arguments(ADD_FMT_TEST "HEADER_ONLY;MODULE;COMPILE_FAIL" "" "" ${ARGN})

set(sources ${name}.cc ${ADD_FMT_TEST_UNPARSED_ARGUMENTS})
set(libs test-main)
if (ADD_FMT_TEST_HEADER_ONLY)
set(sources ${sources} ${TEST_MAIN_SRC} ../src/os.cc)
set(libs gtest fmt-header-only)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wno-weak-vtables)
endif ()
elseif (ADD_FMT_TEST_MODULE)
set(libs test-main ${tstmod})
get_target_property(bmi-dep ${tstmod} LABEL)
set_source_files_properties(${name}.cc PROPERTIES OBJECT_DEPENDS ${bmi-dep})
else ()
set(libs test-main fmt)
endif ()
if (ADD_FMT_TEST_COMPILE_FAIL)
list (REMOVE_ITEM libs test-main)
endif ()

add_fmt_executable(${name} ${sources})
target_link_libraries(${name} ${libs})

Expand All @@ -50,7 +59,19 @@ function(add_fmt_test name)
if (FMT_WERROR)
target_compile_options(${name} PRIVATE ${WERROR_FLAG})
endif ()
add_test(NAME ${name} COMMAND ${name})

if (ADD_FMT_TEST_COMPILE_FAIL)
set_target_properties(${name} PROPERTIES
EXCLUDE_FROM_ALL TRUE
EXCLUDE_FROM_DEFAULT_BUILD TRUE)
add_test(NAME ${name} COMMAND ${CMAKE_COMMAND}
--build .
--target ${name}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set_tests_properties(${name} PROPERTIES WILL_FAIL TRUE)
else ()
add_test(NAME ${name} COMMAND ${name})
endif ()
endfunction()

add_fmt_test(args-test)
Expand All @@ -73,6 +94,11 @@ add_fmt_test(printf-test)
add_fmt_test(ranges-test)
add_fmt_test(scan-test)

if (FMT_CAN_MODULE)
add_fmt_test(module-test MODULE)
add_fmt_test(module-compile-fail MODULE COMPILE_FAIL)
endif ()

if (NOT MSVC)
# FMT_ENFORCE_COMPILE_STRING is not supported under MSVC due to compiler bugs.
add_fmt_test(enforce-checks-test)
Expand Down
23 changes: 23 additions & 0 deletions test/module-compile-fail.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2012 - present, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
//
// Copyright (c) 2021 - present, Daniela Engert
// All Rights Reserved
// {fmt} module.

import fmt;

// the non-exported namespace 'detail' must be invisible [module.interface]/2
using namespace fmt::detail;

#if defined(FMT_HIDE_MODULE_BUGS) && \
defined(_MSC_FULL_VER) && _MSC_FULL_VER <= 192930035
// bug in msvc 16.10-pre3:
// the namespace is visible even when it is neither
// implicitly nor explicitly exported
# error namespace 'fmt::detail' is visible!
#endif

int main() {}
40 changes: 40 additions & 0 deletions test/module-test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Formatting library for C++ - module tests
//
// Copyright (c) 2012 - present, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
//
// Copyright (c) 2021 - present, Daniela Engert
// All Rights Reserved
// {fmt} module.

import fmt;

// check for macros leaking from BMI
static bool macro_leaked =
#if defined(FMT_CORE_H_) || defined(FMT_FORMAT_H)
true;
#else
false;
#endif

#include "gtest/gtest.h"

// an implicitly exported namespace must be visible [module.interface]/2.2
TEST(module_test, namespace) {
using namespace fmt;
ASSERT_TRUE(true);
}

// macros must not be imported from a *named* module [cpp.import]/5.1
TEST(module_test, macros) {
#if defined(FMT_HIDE_MODULE_BUGS) && \
defined(_MSC_FULL_VER) && _MSC_FULL_VER <= 192930035
// bug in msvc 16.10-pre3:
// include-guard macros leak from BMI
// and even worse: they cannot be #undef-ined
macro_leaked = false;
#endif
EXPECT_FALSE(macro_leaked);
}

0 comments on commit c04eb25

Please sign in to comment.