Skip to content

Commit 627cf41

Browse files
authored
Add option to enable all compiler warnings in GCC/Clang (#5897)
* Add option to enable all compiler warnings in GCC/Clang * Fix -Wall for CUDA sources * Make -Wall private req for xgboost-r
1 parent 9b688ac commit 627cf41

File tree

6 files changed

+28
-1
lines changed

6 files changed

+28
-1
lines changed

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ option(R_LIB "Build shared library for R package" OFF)
3232
## Dev
3333
option(USE_DEBUG_OUTPUT "Dump internal training results like gradients and predictions to stdout.
3434
Should only be used for debugging." OFF)
35+
option(ENABLE_ALL_WARNINGS "Enable all compiler warnings. Only effective for GCC/Clang" OFF)
3536
option(GOOGLE_TEST "Build google tests" OFF)
3637
option(USE_DMLC_GTEST "Use google tests bundled with dmlc-core submodule" OFF)
3738
option(USE_NVTX "Build with cuda profiling annotations. Developers only." OFF)
@@ -79,6 +80,11 @@ endif (R_LIB AND GOOGLE_TEST)
7980
if (USE_AVX)
8081
message(SEND_ERROR "The option 'USE_AVX' is deprecated as experimental AVX features have been removed from XGBoost.")
8182
endif (USE_AVX)
83+
if (ENABLE_ALL_WARNINGS)
84+
if ((NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") AND (NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
85+
message(SEND_ERROR "ENABLE_ALL_WARNINGS is only available for Clang and GCC.")
86+
endif ((NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") AND (NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
87+
endif (ENABLE_ALL_WARNINGS)
8288

8389
#-- Sanitizer
8490
if (USE_SANITIZER)
@@ -130,6 +136,9 @@ if (MSVC)
130136
-D_CRT_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE)
131137
endif (TARGET dmlc_unit_tests)
132138
endif (MSVC)
139+
if (ENABLE_ALL_WARNINGS)
140+
target_compile_options(dmlc PRIVATE -Wall -Wextra)
141+
endif (ENABLE_ALL_WARNINGS)
133142
target_link_libraries(objxgboost PUBLIC dmlc)
134143

135144
# rabit
@@ -159,6 +168,9 @@ foreach(lib rabit rabit_base rabit_empty rabit_mock rabit_mock_static)
159168
if (HIDE_CXX_SYMBOLS) # Hide all C++ symbols from Rabit
160169
set_target_properties(${lib} PROPERTIES CXX_VISIBILITY_PRESET hidden)
161170
endif (HIDE_CXX_SYMBOLS)
171+
if (ENABLE_ALL_WARNINGS)
172+
target_compile_options(${lib} PRIVATE -Wall -Wextra)
173+
endif (ENABLE_ALL_WARNINGS)
162174
endif (TARGET ${lib})
163175
endforeach()
164176

R-package/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ file(GLOB_RECURSE R_SOURCES
66
${CMAKE_CURRENT_LIST_DIR}/src/*.c)
77
# Use object library to expose symbols
88
add_library(xgboost-r OBJECT ${R_SOURCES})
9+
if (ENABLE_ALL_WARNINGS)
10+
target_compile_options(xgboost-r PRIVATE -Wall -Wextra)
11+
endif (ENABLE_ALL_WARNINGS)
912
target_compile_definitions(xgboost-r
1013
PUBLIC
1114
-DXGBOOST_STRICT_R_MODE=1

jvm-packages/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ find_package(JNI REQUIRED)
22

33
add_library(xgboost4j SHARED
44
${PROJECT_SOURCE_DIR}/jvm-packages/xgboost4j/src/native/xgboost4j.cpp)
5+
if (ENABLE_ALL_WARNINGS)
6+
target_compile_options(xgboost4j PUBLIC -Wall -Wextra)
7+
endif (ENABLE_ALL_WARNINGS)
58
target_link_libraries(xgboost4j PRIVATE objxgboost)
69
target_include_directories(xgboost4j
710
PRIVATE

src/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ if (MSVC)
7272
)
7373
endif (MSVC)
7474

75+
if (ENABLE_ALL_WARNINGS)
76+
target_compile_options(objxgboost PUBLIC
77+
$<IF:$<COMPILE_LANGUAGE:CUDA>,-Xcompiler=-Wall -Xcompiler=-Wextra,-Wall -Wextra>)
78+
endif (ENABLE_ALL_WARNINGS)
79+
7580
set_target_properties(objxgboost PROPERTIES
7681
POSITION_INDEPENDENT_CODE ON
7782
CXX_STANDARD 14

tests/ci_build/build_via_cmake.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -e
44
rm -rf build
55
mkdir build
66
cd build
7-
cmake .. "$@" -DGOOGLE_TEST=ON -DUSE_DMLC_GTEST=ON -DCMAKE_VERBOSE_MAKEFILE=ON -GNinja
7+
cmake .. "$@" -DGOOGLE_TEST=ON -DUSE_DMLC_GTEST=ON -DCMAKE_VERBOSE_MAKEFILE=ON -DENABLE_ALL_WARNINGS=ON -GNinja
88
ninja clean
99
time ninja -v
1010
cd ..

tests/cpp/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ if (MSVC)
6363
-D_CRT_SECURE_NO_DEPRECATE
6464
)
6565
endif (MSVC)
66+
if (ENABLE_ALL_WARNINGS)
67+
target_compile_options(testxgboost PUBLIC
68+
$<IF:$<COMPILE_LANGUAGE:CUDA>,-Xcompiler=-Wall -Xcompiler=-Wextra,-Wall -Wextra>)
69+
endif (ENABLE_ALL_WARNINGS)
6670

6771
target_include_directories(testxgboost
6872
PRIVATE

0 commit comments

Comments
 (0)