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 13, 2021
1 parent d1aebdb commit 3e88071
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 6 deletions.
47 changes: 45 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ 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)
if (MSVC AND (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.29.30035)) # msvc 16.10-pre3
set(FMT_CAN_MODULE ON)
endif ()
endif ()
if (FMT_CAN_MODULE)
if (MSVC)
set(FMT_BMI "${CMAKE_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 "The compiler doesn't support modules.")
endif ()

# Get version from core.h
file(READ include/fmt/core.h core_h)
Expand Down Expand Up @@ -188,7 +206,11 @@ 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)

set(FMT_INTERFACE_UNIT src/fmt.cc)
if (FMT_MODULE)
set(FMT_SOURCES ${FMT_INTERFACE_UNIT})
elseif (FMT_OS)
set(FMT_SOURCES src/format.cc src/os.cc)
else()
set(FMT_SOURCES src/format.cc)
Expand All @@ -214,6 +236,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 +282,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 ${FMT_INTERFACE_UNIT} ${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 +335,11 @@ if (FMT_INSTALL)
${project_config}
INSTALL_DESTINATION ${FMT_CMAKE_DIR})

set(INSTALL_TARGETS fmt fmt-header-only)
if (FMT_MODULE)
set(INSTALL_TARGETS fmt)
else()
set(INSTALL_TARGETS fmt fmt-header-only)
endif()

# Install the library and headers.
install(TARGETS ${INSTALL_TARGETS} EXPORT ${targets_export_name}
Expand Down
31 changes: 27 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,16 +29,22 @@ endfunction()
# Adds a test.
# Usage: add_fmt_test(name srcs...)
function(add_fmt_test name)
cmake_parse_arguments(ADD_FMT_TEST "HEADER_ONLY" "" "" ${ARGN})
set(options "HEADER_ONLY" "MODULE" "COMPILE_FAIL")
cmake_parse_arguments(ADD_FMT_TEST "${options}" "" "" ${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 ()
add_fmt_executable(${name} ${sources})
target_link_libraries(${name} ${libs})
Expand All @@ -50,7 +56,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 +91,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
6 changes: 6 additions & 0 deletions test/module-compile-fail.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import fmt;

// the detail namespace must be invisible
using namespace fmt::detail;

int main() {}
17 changes: 17 additions & 0 deletions test/module-test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.

#include "gtest-extra.h" // EXPECT_WRITE
import fmt;

TEST(module_test, namespace) {
using namespace fmt;
}

0 comments on commit 3e88071

Please sign in to comment.