Skip to content

Commit

Permalink
[Meta]: Modernize setting the C++ version in CMake
Browse files Browse the repository at this point in the history
Replaced setting CMAKE_CXX_STANDARD and CMAKE_CXX_STANDARD_REQUIRED with
set_target_properties to avoid unintentionally affecting top level
consuming projects.
  • Loading branch information
ad3154 committed Oct 28, 2023
1 parent 2f5af28 commit 3db7e80
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 46 deletions.
15 changes: 5 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ project(
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

# Make CTest available which adds the option BUILD_TESTING
include(CTest)
if(BUILD_TESTING
Expand Down Expand Up @@ -124,6 +114,11 @@ if(BUILD_TESTING)
test/maintain_power_tests.cpp)

add_executable(unit_tests ${TEST_SRC})
set_target_properties(
unit_tests
PROPERTIES CXX_STANDARD 14
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON)
target_link_libraries(
unit_tests
PRIVATE GTest::gtest_main ${PROJECT_NAME}::Isobus
Expand Down
9 changes: 6 additions & 3 deletions examples/diagnostic_protocol/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
cmake_minimum_required(VERSION 3.16)
project(diagnostic_protocol_example)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT BUILD_EXAMPLES)
find_package(isobus REQUIRED)
endif()
find_package(Threads REQUIRED)

add_executable(DiagnosticProtocolExampleTarget main.cpp)

set_target_properties(
DiagnosticProtocolExampleTarget
PROPERTIES CXX_STANDARD 14
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON)

target_link_libraries(
DiagnosticProtocolExampleTarget
PRIVATE isobus::Isobus isobus::HardwareIntegration Threads::Threads
Expand Down
10 changes: 7 additions & 3 deletions examples/guidance/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
cmake_minimum_required(VERSION 3.16)
project(guidance_example)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT BUILD_EXAMPLES)
find_package(isobus REQUIRED)
endif()
find_package(Threads REQUIRED)

add_executable(GuidanceExampleTarget main.cpp console_logger.cpp)

set_target_properties(
GuidanceExampleTarget
PROPERTIES CXX_STANDARD 14
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON)

target_link_libraries(
GuidanceExampleTarget PRIVATE isobus::Isobus isobus::HardwareIntegration
isobus::Utility Threads::Threads)
9 changes: 6 additions & 3 deletions examples/nmea2000/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
cmake_minimum_required(VERSION 3.16)
project(nmea2000_example)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT BUILD_EXAMPLES)
find_package(isobus REQUIRED)
endif()
find_package(Threads REQUIRED)

add_executable(NMEA2KExampleTarget main.cpp)

set_target_properties(
NMEA2KExampleTarget
PROPERTIES CXX_STANDARD 14
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON)

target_link_libraries(
NMEA2KExampleTarget PRIVATE isobus::Isobus isobus::HardwareIntegration
Threads::Threads isobus::Utility)
10 changes: 7 additions & 3 deletions examples/pgn_requests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
cmake_minimum_required(VERSION 3.16)
project(pgn_requests_example)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT BUILD_EXAMPLES)
find_package(isobus REQUIRED)
endif()
find_package(Threads REQUIRED)

add_executable(PGNRequestExampleTarget main.cpp)

set_target_properties(
PGNRequestExampleTarget
PROPERTIES CXX_STANDARD 14
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON)

target_link_libraries(
PGNRequestExampleTarget PRIVATE isobus::Isobus isobus::HardwareIntegration
Threads::Threads isobus::Utility)
10 changes: 7 additions & 3 deletions examples/task_controller_client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
cmake_minimum_required(VERSION 3.16)
project(task_controller_client_example)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT BUILD_EXAMPLES)
find_package(isobus REQUIRED)
endif()
Expand All @@ -13,6 +10,13 @@ add_executable(
TaskControllerClientExample
main.cpp console_logger.cpp section_control_implement_sim.cpp
section_control_implement_sim.hpp)

set_target_properties(
TaskControllerClientExample
PROPERTIES CXX_STANDARD 14
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON)

target_link_libraries(
TaskControllerClientExample
PRIVATE isobus::Isobus isobus::HardwareIntegration Threads::Threads
Expand Down
10 changes: 7 additions & 3 deletions examples/transport_layer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
cmake_minimum_required(VERSION 3.16)
project(transport_layer_example)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT BUILD_EXAMPLES)
find_package(isobus REQUIRED)
endif()
find_package(Threads REQUIRED)

add_executable(TransportLayerExampleTarget main.cpp)

set_target_properties(
TransportLayerExampleTarget
PROPERTIES CXX_STANDARD 14
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON)

target_link_libraries(
TransportLayerExampleTarget
PRIVATE isobus::Isobus isobus::HardwareIntegration Threads::Threads
Expand Down
10 changes: 7 additions & 3 deletions examples/virtual_terminal/aux_functions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
cmake_minimum_required(VERSION 3.16)
project(vt_aux_functions_example)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT BUILD_EXAMPLES)
find_package(isobus REQUIRED)
endif()
find_package(Threads REQUIRED)

add_executable(VTAuxFunctionsExample main.cpp console_logger.cpp
object_pool_ids.h)

set_target_properties(
VTAuxFunctionsExample
PROPERTIES CXX_STANDARD 14
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON)

target_link_libraries(
VTAuxFunctionsExample PRIVATE isobus::Isobus isobus::HardwareIntegration
Threads::Threads isobus::Utility)
Expand Down
10 changes: 7 additions & 3 deletions examples/virtual_terminal/aux_inputs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
cmake_minimum_required(VERSION 3.16)
project(vt_aux_inputs_example)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT BUILD_EXAMPLES)
find_package(isobus REQUIRED)
endif()
find_package(Threads REQUIRED)

add_executable(VTAuxInputsExample main.cpp console_logger.cpp object_pool_ids.h)

set_target_properties(
VTAuxInputsExample
PROPERTIES CXX_STANDARD 14
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON)

target_link_libraries(
VTAuxInputsExample PRIVATE isobus::Isobus isobus::HardwareIntegration
Threads::Threads isobus::Utility)
Expand Down
10 changes: 7 additions & 3 deletions examples/virtual_terminal/version3_object_pool/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
cmake_minimum_required(VERSION 3.16)
project(vt3_version_3_object_pool_example)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT BUILD_EXAMPLES)
find_package(isobus REQUIRED)
endif()
find_package(Threads REQUIRED)

add_executable(VT3ExampleTarget main.cpp console_logger.cpp objectPoolObjects.h)

set_target_properties(
VT3ExampleTarget
PROPERTIES CXX_STANDARD 14
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON)

target_link_libraries(
VT3ExampleTarget PRIVATE isobus::Isobus isobus::HardwareIntegration
Threads::Threads isobus::Utility)
Expand Down
8 changes: 5 additions & 3 deletions hardware_integration/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
cmake_minimum_required(VERSION 3.16)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Set source and include directories
set(HARDWARE_INTEGRATION_SRC_DIR "src")
set(HARDWARE_INTEGRATION_INCLUDE_DIR "include/isobus/hardware_integration")
Expand Down Expand Up @@ -120,6 +117,11 @@ prepend(HARDWARE_INTEGRATION_INCLUDE ${HARDWARE_INTEGRATION_INCLUDE_DIR}
add_library(HardwareIntegration ${HARDWARE_INTEGRATION_SRC}
${HARDWARE_INTEGRATION_INCLUDE})
add_library(${PROJECT_NAME}::HardwareIntegration ALIAS HardwareIntegration)
set_target_properties(
HardwareIntegration
PROPERTIES CXX_STANDARD 14
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON)
target_link_libraries(HardwareIntegration PRIVATE ${PROJECT_NAME}::Utility
${PROJECT_NAME}::Isobus)

Expand Down
9 changes: 6 additions & 3 deletions isobus/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
cmake_minimum_required(VERSION 3.16)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Set library public name
set(ISOBUS_PUBLIC_NAME "ISOBUS")

Expand Down Expand Up @@ -87,6 +84,12 @@ prepend(ISOBUS_INCLUDE ${ISOBUS_INCLUDE_DIR} ${ISOBUS_INCLUDE})
add_library(Isobus ${ISOBUS_SRC} ${ISOBUS_INCLUDE})
add_library(${PROJECT_NAME}::Isobus ALIAS Isobus)

set_target_properties(
Isobus
PROPERTIES CXX_STANDARD 14
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON)

# Specify the include directory to be exported for other moduels to use. The
# PUBLIC keyword here allows other libraries or exectuables to link to this
# library and use its functionality.
Expand Down
9 changes: 6 additions & 3 deletions utility/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
cmake_minimum_required(VERSION 3.16)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Set source and include directories
set(UTILITY_SRC_DIR "src")
set(UTILITY_INCLUDE_DIR "include/isobus/utility")
Expand All @@ -26,6 +23,12 @@ prepend(UTILITY_INCLUDE ${UTILITY_INCLUDE_DIR} ${UTILITY_INCLUDE})
add_library(Utility ${UTILITY_SRC} ${UTILITY_INCLUDE})
add_library(${PROJECT_NAME}::Utility ALIAS Utility)

set_target_properties(
Utility
PROPERTIES CXX_STANDARD 14
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON)

# Specify the include directory to be exported for other moduels to use. The
# PUBLIC keyword here allows other libraries or exectuables to link to this
# library and use its functionality.
Expand Down

0 comments on commit 3db7e80

Please sign in to comment.