Skip to content

Commit ff106b6

Browse files
authored
Do not add tests if project is included via add_subdirectory/FetchContent (#8)
1 parent 919a61a commit ff106b6

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ project(serial_cpp VERSION ${SERIAL_CPP_MAJOR_VERSION}.${SERIAL_CPP_MINOR_VERSIO
1515
option(BUILD_SHARED_LIBS "Build libraries as shared as opposed to static" OFF)
1616
option(BUILD_TESTING "Build tests" OFF)
1717
option(serial_cpp_INSTALL "Enable generation of serial_cpp install targets" ON)
18+
mark_as_advanced(serial_cpp_INSTALL)
19+
option(serial_cpp_FORCE_RESPECT_BUILD_TESTING "If also BUILD_TESTING is ON, enable tests even if project was included via add_subdirectory/FetchContent" OFF)
20+
mark_as_advanced(serial_cpp_FORCE_RESPECT_BUILD_TESTING)
21+
22+
# Detect if project is top level or not (fall back for CMake < 3.21)
23+
if(CMAKE_VERSION VERSION_LESS "3.21.0")
24+
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
25+
set(PROJECT_IS_TOP_LEVEL ON)
26+
else()
27+
set(PROJECT_IS_TOP_LEVEL OFF)
28+
endif()
29+
endif()
30+
1831

1932
# Default install locations
2033
include(GNUInstallDirs)
@@ -120,6 +133,10 @@ endif()
120133

121134
## Tests
122135
include(CTest)
123-
if(BUILD_TESTING)
136+
# Tests are enabled if BUILD_TESTING is enabled and PROJECT_IS_TOP_LEVEL is ON,
137+
# or if serial_cpp_FORCE_RESPECT_BUILD_TESTING is ON, so if you include the project
138+
# via FetchContent or add_subdirectory and if you want to compile tests, you
139+
# need to set both BUILD_TESTING and serial_cpp_FORCE_RESPECT_BUILD_TESTING to ON
140+
if(BUILD_TESTING OR (PROJECT_IS_TOP_LEVEL OR serial_cpp_FORCE_RESPECT_BUILD_TESTING))
124141
add_subdirectory(tests)
125142
endif()

package.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<package>
33
<name>serial_cpp</name>
4-
<version>1.3.1</version>
4+
<version>1.3.2</version>
55
<description>
66
serial_cpp is a cross-platform, simple to use library for using serial ports on computers.
77
This library provides a C++, object oriented interface for interacting with RS-232

0 commit comments

Comments
 (0)