Skip to content

Commit 0484820

Browse files
committed
Refactor setting compiler warnings
1 parent b6abe8e commit 0484820

File tree

4 files changed

+43
-40
lines changed

4 files changed

+43
-40
lines changed

CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ option(ECAL_INSTALL_SAMPLE_SOURCES "Install the sources of eCAL
202202
option(ECAL_USE_NPCAP "Enable the eCAL Npcap Receiver (i.e. the Win10 performance fix)" OFF)
203203
option(ECAL_USE_CLOCKLOCK_MUTEX "Use native mutex with monotonic clock (requires glibc >= 2.30)" OFF)
204204

205+
set(ECAL_EXTRA_COMPILER_ARGS "" CACHE STRING "; separated list of additional compiler arguments to use for eCAL code")
206+
include(./cmake/helper_functions/ecal_compiler_warnings.cmake)
207+
ecal_get_default_compiler_warnings(ecal_default_warnings)
208+
list(APPEND ecal_default_warnings ${ECAL_EXTRA_COMPILER_ARGS})
209+
set(ECAL_COMPILER_WARNINGS "${ecal_default_warnings}" CACHE STRING "Warning flags used for eCAL code")
210+
mark_as_advanced(FORCE ECAL_COMPILER_WARNINGS)
211+
205212
# --------------------------------------------------------
206213
# ecal core configuration
207214
# --------------------------------------------------------
@@ -583,7 +590,6 @@ install(FILES ${eCAL_config} ${eCAL_config_version}
583590

584591
install(FILES
585592
cmake/helper_functions/ecal_add_functions.cmake
586-
cmake/helper_functions/ecal_compiler_warnings.cmake
587593
cmake/helper_functions/ecal_helper_functions.cmake
588594
cmake/helper_functions/ecal_install_functions.cmake
589595
DESTINATION ${${PROJECT_NAME}_install_cmake_dir}/helper_functions

cmake/helper_functions/ecal_add_functions.cmake

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,17 @@
1818

1919
include_guard(GLOBAL)
2020

21-
include("${CMAKE_CURRENT_LIST_DIR}/ecal_compiler_warnings.cmake")
21+
# ECAL_COMPILER_WARNINGS is set by the root CMakeLists and may be user customized
22+
add_library(_ecal_warnings INTERFACE)
23+
target_compile_options(_ecal_warnings INTERFACE
24+
"$<$<COMPILE_LANGUAGE:C,CXX>:${ECAL_COMPILER_WARNINGS}>"
25+
)
26+
27+
function(ecal_add_compiler_warnings TARGET_NAME)
28+
target_link_libraries("${TARGET_NAME}" PRIVATE
29+
"$<BUILD_INTERFACE:_ecal_warnings>"
30+
)
31+
endfunction()
2232

2333
# This function will set the output names of the target according to eCAL conventions.
2434
function(ecal_add_app_console TARGET_NAME)
Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
include_guard(GLOBAL)
22

3-
add_library(_ecal_warnings INTERFACE)
3+
function(ecal_get_default_compiler_warnings cxx_out_var)
4+
set(cxx_compiler_flags "")
45

5-
set(cxx_compiler_flags "")
6+
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
7+
message(STATUS "MSVC detected - Adding flags")
8+
set(cxx_compiler_flags "/MP" "/W4")
9+
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|Clang")
10+
message(STATUS "Setting GNU/Clang flags")
11+
set(cxx_compiler_flags "-Wall" "-Wextra")
12+
else()
13+
message(WARNING "Unknown compiler, will not set warning flags")
14+
endif()
615

7-
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
8-
message(STATUS "MSVC detected - Adding flags")
9-
set(cxx_compiler_flags "/MP" "/W4")
10-
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|Clang")
11-
message(STATUS "Setting GNU/Clang flags")
12-
set(cxx_compiler_flags "-Wall" "-Wextra")
13-
else()
14-
message(WARNING "Unknown compiler, will not set warning flags")
15-
endif()
16-
17-
target_compile_options(_ecal_warnings INTERFACE
18-
"$<$<COMPILE_LANGUAGE:C,CXX>:${cxx_compiler_flags}>"
19-
)
20-
21-
unset(cxx_compiler_flags)
22-
23-
function(ecal_add_compiler_warnings TARGET_NAME)
24-
target_link_libraries("${TARGET_NAME}" PRIVATE
25-
"$<BUILD_INTERFACE:_ecal_warnings>"
26-
)
16+
set("${cxx_out_var}" "${cxx_compiler_flags}" PARENT_SCOPE)
2717
endfunction()

ecal/service/CMakeLists.txt

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,23 @@
1919
cmake_minimum_required(VERSION 3.16)
2020
project(ecal_service)
2121

22-
# Root CMakeLists sets CMAKE_MODULE_PATH to include the correct path
23-
# If the module cannot be found warnings basic warnings will be applied instead
24-
include(helper_functions/ecal_compiler_warnings OPTIONAL RESULT_VARIABLE _ecal_warnings_found)
25-
if("${_ecal_warnings_found}" STREQUAL "NOTFOUND")
26-
message(AUTHOR_WARNING
27-
" helper_functions/ecal_compiler_warnings.cmake not found.\n"
28-
" Add the path to CMAKE_MODULE_PATH to use warnings specified by the main eCAL repo."
29-
)
30-
add_library(_ecal_service_warnings INTERFACE)
31-
set(cxx_compiler_flags "")
22+
if(TARGET _ecal_warnings)
23+
# _ecal_warnings is the internal eCAL target containing the warning flags
24+
# and will be absent if this is being built standalone
25+
add_library(_ecal_service_warnings ALIAS _ecal_warnings)
26+
else()
27+
# Otherwise, use a fallback implementation
3228
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
33-
set(cxx_compiler_flags "/MP" "/W4")
29+
set(cxx_compiler_flags "/MP" "/W4")
3430
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|Clang")
35-
set(cxx_compiler_flags "-Wall" "-Wextra")
31+
set(cxx_compiler_flags "-Wall" "-Wextra")
3632
else()
37-
message(WARNING "Unknown compiler, will not set warning flags")
33+
message(WARNING "Unknown compiler, will not set warning flags")
3834
endif()
39-
target_compile_options(_ecal_service_warnings INTERFACE "${cxx_compiler_flags}")
40-
else()
41-
add_library(_ecal_service_warnings ALIAS _ecal_warnings)
35+
36+
set(ECAL_SERVICE_COMPILER_WARNINGS "${cxx_compiler_flags}" CACHE STRING "Warning flags used for eCAL service code")
37+
add_library(_ecal_service_warnings INTERFACE)
38+
target_compile_options(_ecal_service_warnings INTERFACE "${ECAL_SERVICE_COMPILER_WARNINGS}")
4239
endif()
4340

4441
# Main library

0 commit comments

Comments
 (0)