Skip to content

Commit dbfe67f

Browse files
authored
Can be installed as CppUnit for drop in replacement (#38)
1 parent 22b61b5 commit dbfe67f

3 files changed

Lines changed: 67 additions & 19 deletions

File tree

CMakeLists.txt

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ project(
88

99
option(build_testing "Should we build our examples and tests" OFF)
1010
option(EnableMainHelperClasses
11-
"Aims to allow for drop in replacement. Users should prefer refactoring the main function"
12-
OFF)
13-
option(InstallAsCppUnit "Install the package as 'cppunit' to allow drop-in replacement" OFF)
11+
"Aims to allow for drop in replacement. Users should prefer refactoring the main function"
12+
OFF
13+
)
14+
15+
option(InstallAsCppUnit
16+
"Install the package as 'cppunit' to allow drop-in replacement"
17+
OFF
18+
)
1419

1520
if(build_testing)
1621
enable_testing()
@@ -66,6 +71,36 @@ install(
6671
DESTINATION "share/cmake/${PROJECT_NAME}"
6772
)
6873

74+
if (InstallAsCppUnit)
75+
message(STATUS "CppUnit2Gtest will be installed as 'cppunit' to allow drop-in replacement")
76+
add_library(cppunit ALIAS CppUnit2Gtest)
77+
add_library(CppUnit ALIAS CppUnit2Gtest)
78+
install(
79+
FILES
80+
"${CMAKE_CURRENT_BINARY_DIR}/CppUnitConfig.cmake"
81+
"${CMAKE_CURRENT_BINARY_DIR}/CppUnitConfigVersion.cmake"
82+
DESTINATION "share/cmake/CppUnit"
83+
)
84+
install(TARGETS CppUnit2Gtest
85+
EXPORT CppUnitTargets
86+
INCLUDES DESTINATION include
87+
)
88+
install(EXPORT CppUnitTargets
89+
FILE CppUnitTargets.cmake
90+
NAMESPACE CppUnit::
91+
DESTINATION "share/cmake/CppUnit"
92+
)
93+
configure_package_config_file(
94+
${CMAKE_CURRENT_SOURCE_DIR}/cmake/Config.cmake.in
95+
"${CMAKE_CURRENT_BINARY_DIR}/CppUnitConfig.cmake"
96+
INSTALL_DESTINATION "share/cmake/CppUnit"
97+
)
98+
write_basic_package_version_file(
99+
CppUnitConfigVersion.cmake
100+
VERSION ${PACKAGE_VERSION}
101+
COMPATIBILITY AnyNewerVersion
102+
)
103+
endif()
69104
# install the header as expected
70105
install(
71106
FILES "${CMAKE_CURRENT_SOURCE_DIR}/CppUnit2Gtest.hpp"
@@ -85,7 +120,7 @@ create_header_alias(\${SOURCE_FILE} \${INSTALL_INCLUDE_DIR}/cppunit/config/Sourc
85120
create_header_alias(\${SOURCE_FILE} \${INSTALL_INCLUDE_DIR}/cppunit/TestAssert.h)
86121
")
87122
if (EnableMainHelperClasses)
88-
install(CODE "
123+
install(CODE "
89124
set(INSTALL_INCLUDE_DIR \${CMAKE_INSTALL_PREFIX}/include)
90125
set(SOURCE_FILE CppUnit2Gtest.hpp)
91126
include(tests/CreateSymlink.cmake)

tests/CMakeLists.txt

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,25 @@ option(BuildUnityTests
1717
option(MutationTesting "Experimental, for mutation testing" OFF)
1818
option(BuildExternalTests "Build and add tests from external libs" OFF)
1919

20-
if (NOT (BuildExamples OR BuildInternalTests OR BuildUnityTests))
20+
if (NOT (BuildExamples OR BuildInternalTests OR BuildUnityTests OR BuildExternalTests))
2121
message(FATAL_ERROR "Asked to build tests but no tests were given")
2222
endif()
2323

24-
set(CMAKE_CXX_STANDARD 17)
24+
25+
if (BuildExternalTests)
26+
if(NOT EnableMainHelperClasses)
27+
message(FATAL_ERROR "EnableMainHelperClasses must be set for external tests")
28+
endif()
29+
message(STATUS "Building external tests")
30+
if (build_testing)
31+
message(WARNING "Building external tests with internal package, not recommended")
32+
endif()
33+
add_subdirectory(external_tests)
34+
endif()
35+
36+
2537
if (BuildExamples)
38+
set(CMAKE_CXX_STANDARD 17)
2639
list(APPEND CppUnitFiles
2740
"examples/Money.cpp"
2841
"examples/Simple.cpp"
@@ -87,7 +100,13 @@ if (BuildUnityTests)
87100
)
88101
endif()
89102

103+
# If no files are added (external tests only) skip!
104+
if (NOT CppUnitFiles)
105+
return()
106+
endif()
107+
90108
include(GetGtest.cmake)
109+
91110
add_executable(${PROJECT_NAME} ${CppUnitFiles})
92111
target_link_libraries(${PROJECT_NAME} PRIVATE GTest::GTest GTest::Main)
93112

@@ -129,17 +148,6 @@ else()
129148
)
130149
endif()
131150

132-
if (BuildExternalTests)
133-
if(NOT EnableMainHelperClasses)
134-
message(FATAL_ERROR "EnableMainHelperClasses must be set for external tests")
135-
endif()
136-
message(STATUS "Building external tests")
137-
if (build_testing)
138-
message(WARNING "Building external tests with internal package, not recommended")
139-
endif()
140-
add_subdirectory(external_tests)
141-
endif()
142-
143151
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR})
144152
# The gtest find tests function doesn't work because it relies on regex expressions
145153
# We have tests that look like CppUnit so it won't add them.

tests/external_tests/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ if (NOT TARGET CppUnit2Gtest::CppUnit2Gtest)
1313
find_package(CppUnit2Gtest REQUIRED)
1414
endif()
1515

16-
# Alias it to have CMake understand
1716
if (NOT TARGET cppunit)
18-
add_library(cppunit ALIAS CppUnit2Gtest::CppUnit2Gtest)
17+
set(cppunit_ROOT ${CppUnit2Gtest_ROOT})
18+
set(CppUnit_ROOT ${CppUnit2Gtest_ROOT})
19+
set(CPPUNIT_ROOT ${CppUnit2Gtest_ROOT})
20+
# less likely to help
21+
set(cppunit_DIR ${CppUnit2Gtest_DIR})
22+
set(CppUnit_DIR ${CppUnit2Gtest_DIR})
23+
set(CPPUNIT_DIR ${CppUnit2Gtest_DIR})
1924
endif()
2025

2126
# Tests

0 commit comments

Comments
 (0)