Skip to content

Commit

Permalink
Move single header to a separate folder (#187)
Browse files Browse the repository at this point in the history
* Move single header to a separate folder

It is much better for tests and examples to directly depend on the
original sources as errors and warnings are properly reported.

Previously, errors are warnings were reported against the single include,
but had to be fixed in doctest_fwd.h and doctest.cpp.

* Fix cmake target "playground"
  • Loading branch information
dimztimz authored and onqtam committed Mar 2, 2019
1 parent a7af1d6 commit 0dbbd4f
Show file tree
Hide file tree
Showing 11 changed files with 5,348 additions and 5,346 deletions.
36 changes: 29 additions & 7 deletions CMakeLists.txt
Expand Up @@ -11,23 +11,44 @@ option(DOCTEST_WITH_TESTS "Build tests/examples" ON)
option(DOCTEST_WITH_MAIN_IN_STATIC_LIB "Build a static lib (cmake target) with a main entry point" ON)
option(DOCTEST_NO_INSTALL "Skip the installation process" OFF)


add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME} INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/>)

# hack to support building on XCode 6 and 7 - propagate the definition to everything
if(DEFINED DOCTEST_THREAD_LOCAL)
add_definitions(-DDOCTEST_THREAD_LOCAL=${DOCTEST_THREAD_LOCAL})
target_compile_definitions(${PROJECT_NAME} INTERFACE
DOCTEST_THREAD_LOCAL=${DOCTEST_THREAD_LOCAL})
endif()

add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME} INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/>)
# add a custom target that assembles the single header when any of the parts are touched

set(doctest_parts_folder "${CMAKE_CURRENT_SOURCE_DIR}/doctest/parts")

add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/single_include/doctest/doctest.h
DEPENDS
${doctest_parts_folder}/doctest_fwd.h
${doctest_parts_folder}/doctest.cpp
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/scripts/cmake/asemble_single_header.cmake
COMMENT "assembling the single header")

add_custom_target(assemble_single_header ALL DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/single_include/doctest/doctest.h)

################################################################################
## TESTS/EXAMPLES/HELPERS
################################################################################

if(${DOCTEST_WITH_MAIN_IN_STATIC_LIB})
include(scripts/cmake/common.cmake)
doctest_add_library(${PROJECT_NAME}_with_main STATIC EXCLUDE_FROM_ALL doctest/parts/doctest.cpp)
target_compile_definitions(${PROJECT_NAME}_with_main PRIVATE DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN)
target_link_libraries(${PROJECT_NAME}_with_main PUBLIC ${PROJECT_NAME})
add_library(${PROJECT_NAME}_with_main STATIC EXCLUDE_FROM_ALL doctest/parts/doctest.cpp)
target_compile_definitions(${PROJECT_NAME}_with_main PRIVATE
DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN)
# hack to support building on XCode 6 and 7 - propagate the definition to everything
if(DEFINED DOCTEST_THREAD_LOCAL)
target_compile_definitions(${PROJECT_NAME}_with_main PRIVATE
DOCTEST_THREAD_LOCAL=${DOCTEST_THREAD_LOCAL})
endif()
endif()

if(${DOCTEST_WITH_TESTS})
Expand Down Expand Up @@ -70,8 +91,9 @@ if (NOT ${DOCTEST_NO_INSTALL})
INCLUDES DESTINATION "${include_install_dir}"
)

# install the single include instead of original sources
install(
FILES "doctest/doctest.h"
FILES "single_include/doctest/doctest.h"
DESTINATION "${include_install_dir}/doctest"
)

Expand Down
5,283 changes: 2 additions & 5,281 deletions doctest/doctest.h

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions examples/all_features/CMakeLists.txt
Expand Up @@ -31,7 +31,8 @@ set(files_all
)

# add the executable
doctest_add_executable(${PROJECT_NAME} ${files_all})
add_executable(${PROJECT_NAME} ${files_all})
target_link_libraries(${PROJECT_NAME} doctest ${CMAKE_THREAD_LIBS_INIT})

# easy way to fix test coverage - disable colors and crash handling
target_compile_definitions(${PROJECT_NAME} PRIVATE
Expand Down Expand Up @@ -84,8 +85,9 @@ if(DEFINED ENV{CODE_COVERAGE})
return() # do not continue with the disabled example
endif()

doctest_add_executable(disabled ${files_all})
add_executable(disabled ${files_all})
target_compile_definitions(disabled PRIVATE DOCTEST_CONFIG_DISABLE)
target_link_libraries(disabled doctest ${CMAKE_THREAD_LIBS_INIT})

doctest_add_test(NAME disabled COMMAND $<TARGET_FILE:disabled>)

Expand Down
13 changes: 7 additions & 6 deletions examples/exe_with_static_libs/CMakeLists.txt
Expand Up @@ -7,14 +7,15 @@ include(../../scripts/cmake/common.cmake)
include_directories(../../)

#== create object libraries instead of static libraries
doctest_add_library(lib_1 OBJECT lib_1_src1.cpp lib_1_src2.cpp)
doctest_add_library(lib_2 OBJECT lib_2_src.cpp)
doctest_add_executable(${PROJECT_NAME} main.cpp $<TARGET_OBJECTS:lib_1> $<TARGET_OBJECTS:lib_2>)
add_library(lib_1 OBJECT lib_1_src1.cpp lib_1_src2.cpp)
add_library(lib_2 OBJECT lib_2_src.cpp)
add_executable(${PROJECT_NAME} main.cpp $<TARGET_OBJECTS:lib_1> $<TARGET_OBJECTS:lib_2>)
target_link_libraries(${PROJECT_NAME} doctest)

#== alternatively we could create static libraries and use "doctest_force_link_static_lib_in_target"
#doctest_add_library(lib_1 STATIC lib_1_src1.cpp lib_1_src2.cpp)
#doctest_add_library(lib_2 STATIC lib_2_src.cpp)
#doctest_add_executable(${PROJECT_NAME} main.cpp)
#add_library(lib_1 STATIC lib_1_src1.cpp lib_1_src2.cpp)
#add_library(lib_2 STATIC lib_2_src.cpp)
#add_executable(${PROJECT_NAME} main.cpp)
#target_link_libraries(${PROJECT_NAME} lib_1)
#target_link_libraries(${PROJECT_NAME} lib_2)
#include(doctest_force_link_static_lib_in_target.cmake)
Expand Down
9 changes: 5 additions & 4 deletions examples/executable_dll_and_plugin/CMakeLists.txt
Expand Up @@ -14,15 +14,16 @@ if(APPLE)
endif()
endif()

doctest_add_library(implementation SHARED implementation.cpp implementation_2.cpp)
add_library(implementation SHARED implementation.cpp implementation_2.cpp)
target_link_libraries(implementation PUBLIC doctest)

doctest_add_library(dll SHARED dll.cpp)
add_library(dll SHARED dll.cpp)
target_link_libraries(dll implementation)

doctest_add_library(plugin SHARED plugin.cpp)
add_library(plugin SHARED plugin.cpp)
target_link_libraries(plugin implementation)

doctest_add_executable(${PROJECT_NAME} main.cpp)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} dll)
target_link_libraries(${PROJECT_NAME} implementation)

Expand Down
24 changes: 13 additions & 11 deletions scripts/cmake/asemble_single_header.cmake
@@ -1,13 +1,15 @@
set(doctest_include_folder "${CMAKE_CURRENT_LIST_DIR}/../../doctest/")
set(doctest_src_folder "${CMAKE_CURRENT_LIST_DIR}/../../doctest")

file(READ ${doctest_include_folder}/parts/doctest_fwd.h fwd)
file(READ ${doctest_include_folder}/parts/doctest.cpp impl)
file(READ ${doctest_src_folder}/parts/doctest_fwd.h fwd)
file(READ ${doctest_src_folder}/parts/doctest.cpp impl)

file(WRITE ${doctest_include_folder}/doctest.h "// ======================================================================\n")
file(APPEND ${doctest_include_folder}/doctest.h "// == DO NOT MODIFY THIS FILE BY HAND - IT IS AUTO GENERATED BY CMAKE! ==\n")
file(APPEND ${doctest_include_folder}/doctest.h "// ======================================================================\n")
file(APPEND ${doctest_include_folder}/doctest.h "${fwd}\n")
file(APPEND ${doctest_include_folder}/doctest.h "#ifndef DOCTEST_SINGLE_HEADER\n")
file(APPEND ${doctest_include_folder}/doctest.h "#define DOCTEST_SINGLE_HEADER\n")
file(APPEND ${doctest_include_folder}/doctest.h "#endif // DOCTEST_SINGLE_HEADER\n")
file(APPEND ${doctest_include_folder}/doctest.h "\n${impl}")
set(doctest_out_folder "${CMAKE_CURRENT_LIST_DIR}/../../single_include/doctest")

file(WRITE ${doctest_out_folder}/doctest.h "// ======================================================================\n")
file(APPEND ${doctest_out_folder}/doctest.h "// == DO NOT MODIFY THIS FILE BY HAND - IT IS AUTO GENERATED BY CMAKE! ==\n")
file(APPEND ${doctest_out_folder}/doctest.h "// ======================================================================\n")
file(APPEND ${doctest_out_folder}/doctest.h "${fwd}\n")
file(APPEND ${doctest_out_folder}/doctest.h "#ifndef DOCTEST_SINGLE_HEADER\n")
file(APPEND ${doctest_out_folder}/doctest.h "#define DOCTEST_SINGLE_HEADER\n")
file(APPEND ${doctest_out_folder}/doctest.h "#endif // DOCTEST_SINGLE_HEADER\n")
file(APPEND ${doctest_out_folder}/doctest.h "\n${impl}")
30 changes: 0 additions & 30 deletions scripts/cmake/common.cmake
Expand Up @@ -60,17 +60,6 @@ function(doctest_add_test)
add_test(NAME ${ARG_NAME} COMMAND ${CMAKE_COMMAND} -DCOMMAND=${the_command} ${ADDITIONAL_FLAGS} -P ${CURRENT_LIST_DIR_CACHED}/exec_test.cmake)
endfunction()

function(doctest_add_executable name)
add_executable(${name} ${ARGN})
add_dependencies(${name} assemble_single_header)
target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT})
endfunction()

function(doctest_add_library name)
add_library(${name} ${ARGN})
add_dependencies(${name} assemble_single_header)
endfunction()

macro(add_compiler_flags)
foreach(flag ${ARGV})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
Expand Down Expand Up @@ -181,22 +170,3 @@ if(MSVC)
#/wd4623 # default constructor was implicitly defined as deleted
)
endif()

# add a custom target that assembles the single header when any of the parts are touched

set(doctest_include_folder "${CURRENT_LIST_DIR_CACHED}/../../doctest/")
set(doctest_parts_folder "${CURRENT_LIST_DIR_CACHED}/../../doctest/parts/")
if(WIN32)
STRING(REGEX REPLACE "/" "\\\\" doctest_include_folder ${doctest_include_folder})
STRING(REGEX REPLACE "/" "\\\\" doctest_parts_folder ${doctest_parts_folder})
endif()

add_custom_command(
OUTPUT ${doctest_include_folder}doctest.h
DEPENDS
${doctest_parts_folder}doctest_fwd.h
${doctest_parts_folder}doctest.cpp
COMMAND ${CMAKE_COMMAND} -P ${CURRENT_LIST_DIR_CACHED}/asemble_single_header.cmake
COMMENT "assembling the single header")

add_custom_target(assemble_single_header ALL DEPENDS ${doctest_include_folder}doctest.h)
5 changes: 2 additions & 3 deletions scripts/playground/CMakeLists.txt
Expand Up @@ -4,9 +4,8 @@ project(playground)

include(../cmake/common.cmake)

include_directories(../../doctest/)

doctest_add_executable(${PROJECT_NAME} main.cpp test.cpp)
add_executable(${PROJECT_NAME} main.cpp test.cpp)
target_link_libraries(${PROJECT_NAME} doctest)

doctest_add_test(NAME ${PROJECT_NAME} NO_OUTPUT COMMAND $<TARGET_FILE:${PROJECT_NAME}> -nv)

Expand Down
2 changes: 1 addition & 1 deletion scripts/playground/main.cpp
@@ -1,4 +1,4 @@
#include "parts/doctest.cpp"
#include <doctest/parts/doctest.cpp>

int main(int argc, char** argv) {
doctest::Context context;
Expand Down
2 changes: 1 addition & 1 deletion scripts/playground/test.cpp
@@ -1,4 +1,4 @@
#include "parts/doctest_fwd.h"
#include <doctest/parts/doctest_fwd.h>

DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
#include <iostream>
Expand Down

0 comments on commit 0dbbd4f

Please sign in to comment.