CMake: Allow user build flags to override default build flags
authorJoão Valverde <joao.valverde@tecnico.ulisboa.pt>
Sun, 24 Sep 2017 01:30:08 +0000 (02:30 +0100)
committerPeter Wu <peter@lekensteyn.nl>
Fri, 13 Oct 2017 21:32:18 +0000 (21:32 +0000)
Autotools has the very useful feature by design of allowing the user
to override the default build flags (you break it you keep it).

Apparently CMake applies COMPILE_OPTIONS target property after
CMAKE_{C,CXX}_FLAGS so that doesn't work here. Prepend our flags to those
variables instead to make it work then.

Specific target flag overrides can still be added with COMPILER_OPTIONS
(e.g: generated files with -Wno-warning) but this is less effective and
then we're back at the point where this overrides user flags. It's less
of a concern though.

Change-Id: I44761a79be4289238e02d4e781fef0099628817b
Reviewed-on: https://code.wireshark.org/review/23675
Petri-Dish: João Valverde <j@v6e.pt>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: João Valverde <j@v6e.pt>
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
20 files changed:
CMakeLists.txt
capchild/CMakeLists.txt
caputils/CMakeLists.txt
cmake/modules/WiresharkPlugin.cmake
codecs/CMakeLists.txt
epan/CMakeLists.txt
epan/crypt/CMakeLists.txt
epan/dfilter/CMakeLists.txt
epan/dissectors/CMakeLists.txt
epan/ftypes/CMakeLists.txt
epan/wmem/CMakeLists.txt
epan/wslua/CMakeLists.txt
randpkt_core/CMakeLists.txt
tools/lemon/CMakeLists.txt
ui/CMakeLists.txt
ui/gtk/CMakeLists.txt
ui/qt/CMakeLists.txt
wiretap/CMakeLists.txt
writecap/CMakeLists.txt
wsutil/CMakeLists.txt

index 7bafe80decc3f526c43a92f7ec508a85acb53f33..9ceeaf452098847c1df3527e802e379888fc3211 100644 (file)
@@ -693,10 +693,10 @@ foreach(THIS_FLAG ${C_WARN_TESTS})
        message(STATUS "Checking for c-compiler flag: ${THIS_FLAG}")
        check_c_compiler_flag("${C_FLAG_TESTS} ${${F}}" ${V})
        if (${${V}})
-               set(ADDED_WARN_C_FLAGS ${ADDED_WARN_C_FLAGS} ${${F}})
+               set(ADDED_WARN_C_FLAGS "${ADDED_WARN_C_FLAGS} ${${F}}")
        endif()
 endforeach()
-set(WS_WARNINGS_C_FLAGS ${ADDED_WARN_C_FLAGS})
+set(CMAKE_C_FLAGS "${ADDED_WARN_C_FLAGS} ${CMAKE_C_FLAGS}")
 
 foreach(THIS_FLAG ${CXX_WARN_TESTS})
        string( REGEX REPLACE "[^a-zA-Z0-9_]+" "_" F ${THIS_FLAG} )
@@ -705,10 +705,10 @@ foreach(THIS_FLAG ${CXX_WARN_TESTS})
        message(STATUS "Checking for c++-compiler flag: ${THIS_FLAG}")
        check_cxx_compiler_flag("${CXX_FLAG_TESTS} ${${F}}" ${V})
        if (${${V}})
-               set(ADDED_WARN_CXX_FLAGS ${ADDED_WARN_CXX_FLAGS} ${${F}})
+               set(ADDED_WARN_CXX_FLAGS "${ADDED_WARN_CXX_FLAGS} ${${F}}")
        endif()
 endforeach()
-set(WS_WARNINGS_CXX_FLAGS ${ADDED_WARN_CXX_FLAGS})
+set(CMAKE_CXX_FLAGS "${ADDED_WARN_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")
 
 include(CMakePushCheckState)
 
@@ -773,14 +773,14 @@ if( NOT CMAKE_C_COMPILER_ID MATCHES "MSVC")
        #
        check_c_compiler_flag(-fvisibility=hidden FVHIDDEN)
        if(FVHIDDEN)
-               set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
+               set(CMAKE_C_FLAGS "-fvisibility=hidden ${CMAKE_C_FLAGS}")
        else()
                #
                # OK, try the Sun^WOracle C -xldscope=hidden
                #
                check_c_compiler_flag(-xldscope=hidden XLDSCOPEHIDDEN)
                if(XLDSCOPEHIDDEN)
-                       set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -xldscope=hidden")
+                       set(CMAKE_C_FLAGS "-xldscope=hidden ${CMAKE_C_FLAGS}")
                else()
                        #
                        # Anything else?
@@ -1203,9 +1203,9 @@ if (Qt5Widgets_FOUND)
                if(NOT CXX__std_c__11_VALID)
                        message(FATAL_ERROR "Qt ${Qt5Widgets_VERSION} requires C++ 11")
                endif()
-               set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
+               set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
        endif()
-       set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
+       set(CMAKE_CXX_FLAGS "${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS} ${CMAKE_CXX_FLAGS}")
        set (QT_FOUND ON)
        set (QT_LIBRARIES ${Qt5Widgets_LIBRARIES} ${Qt5PrintSupport_LIBRARIES})
        if(Qt5Multimedia_FOUND)
@@ -1550,10 +1550,8 @@ set_package_properties(NGHTTP2 PROPERTIES
        PURPOSE "Header decompression in HTTP2"
 )
 
-string(REPLACE ";" " " _warnings_c_flags "${WS_WARNINGS_C_FLAGS}")
-message(STATUS "C-Flags: ${CMAKE_C_FLAGS} ${_warnings_c_flags}")
-string(REPLACE ";" " " _warnings_cxx_flags "${WS_WARNINGS_CXX_FLAGS}")
-message(STATUS "CXX-Flags: ${CMAKE_CXX_FLAGS} ${_warnings_cxx_flags}")
+message(STATUS "C-Flags: ${CMAKE_C_FLAGS}")
+message(STATUS "CXX-Flags: ${CMAKE_CXX_FLAGS}")
 message(STATUS "Warnings as errors: ${WERROR_COMMON_FLAGS}")
 
 feature_summary(WHAT ALL)
@@ -2213,9 +2211,10 @@ if(BUILD_wireshark AND QT_FOUND)
        add_executable(wireshark WIN32 MACOSX_BUNDLE wireshark-qt.cpp ${wireshark_FILES} ${EXTRA_BUNDLE_FILES})
        add_dependencies(wireshark version)
        set(PROGLIST ${PROGLIST} wireshark)
-       set_target_properties(wireshark PROPERTIES COMPILE_OPTIONS "${WS_WARNINGS_CXX_FLAGS}")
-       set_target_properties(wireshark PROPERTIES LINK_FLAGS "${WS_LINK_FLAGS}")
-       set_target_properties(wireshark PROPERTIES FOLDER "Executables")
+       set_target_properties(wireshark PROPERTIES
+               LINK_FLAGS "${WS_LINK_FLAGS}"
+               FOLDER "Executables"
+       )
        if(ENABLE_APPLICATION_BUNDLE OR WIN32)
                set_target_properties(wireshark PROPERTIES OUTPUT_NAME Wireshark)
        endif()
@@ -2293,7 +2292,6 @@ endif()
 # Common properties for CLI executables
 macro(set_extra_executable_properties _executable _folder)
        set_target_properties(${_executable} PROPERTIES
-               COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
                LINK_FLAGS "${WS_LINK_FLAGS}"
                FOLDER ${_folder}
        )
@@ -2321,7 +2319,6 @@ macro(set_extcap_executable_properties _executable)
 
        if(WIN32)
                set_target_properties(${_executable} PROPERTIES
-                       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
                        LINK_FLAGS "${WS_LINK_FLAGS}"
                        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/run/extcap
                        RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/run/Debug/extcap
@@ -2331,7 +2328,6 @@ macro(set_extcap_executable_properties _executable)
                )
        else()
                set_target_properties(${_executable} PROPERTIES
-                       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
                        LINK_FLAGS "${WS_LINK_FLAGS}"
                        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/run/extcap
                )
@@ -2373,9 +2369,10 @@ if(BUILD_wireshark_gtk AND GTK_FOUND)
        add_executable(wireshark-gtk WIN32 ${wireshark_FILES})
        add_dependencies(wireshark-gtk version)
        set(PROGLIST ${PROGLIST} wireshark-gtk)
-       set_target_properties(wireshark-gtk PROPERTIES COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}")
-       set_target_properties(wireshark-gtk PROPERTIES LINK_FLAGS "${WS_LINK_FLAGS}")
-       set_target_properties(wireshark-gtk PROPERTIES FOLDER "Executables")
+       set_target_properties(wireshark-gtk PROPERTIES
+               LINK_FLAGS "${WS_LINK_FLAGS}"
+               FOLDER "Executables"
+       )
        target_link_libraries(wireshark-gtk ${wireshark_gtk_LIBS})
        install(TARGETS wireshark-gtk RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
 
index b470a4db0882ec047bc98361a0733bf080a1023a..1a71b4f6a802208d1798499a686b760840df4f0c 100644 (file)
@@ -43,7 +43,6 @@ add_library(capchild STATIC
 )
 
 set_target_properties(capchild PROPERTIES
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
        LINK_FLAGS "${WS_LINK_FLAGS}"
        FOLDER "Libs")
 
index 453660149547ebc4c9f01aed43460f36f96b25ed..ec0c6a7c47cd92a5719d879a83a8049ff3b5c45f 100644 (file)
@@ -64,7 +64,6 @@ add_library(caputils STATIC
 )
 
 set_target_properties(caputils PROPERTIES
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
        LINK_FLAGS "${WS_LINK_FLAGS}"
        FOLDER "Libs")
 
index 04b11377f29916521685031b8ff35c7d1680e71b..488175fca693e543b74e14bf5406b5440acd7eb8 100644 (file)
@@ -33,7 +33,6 @@ macro(ADD_PLUGIN_LIBRARY _plugin)
 
        set_target_properties(${_plugin} PROPERTIES
                PREFIX ""
-               COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
                LINK_FLAGS "${WS_LINK_FLAGS}"
                FOLDER "Plugins"
        )
index ba2e263505c6622eaa5ee062d1f797b1ad838ce1..6f7f51cb4ca63b4733d050418532558049101893 100644 (file)
@@ -71,7 +71,6 @@ set(FULL_SO_VERSION "0.0.0")
 set_target_properties(wscodecs PROPERTIES
   PREFIX "lib"
   COMPILE_DEFINITIONS "WS_BUILD_DLL"
-  COMPILE_OPTIONS "${WS_WS_WARNINGS_C_FLAGS}"
   LINK_FLAGS "${WS_LINK_FLAGS}"
   VERSION ${FULL_SO_VERSION} SOVERSION 0
   FOLDER "DLLs"
index adadd689e5a1bcec8c165fadbb0ee2a5057073d4..b5d1d2a20bb5ae6dfad389924c2e6ff5702ed171 100644 (file)
@@ -357,10 +357,11 @@ add_dependencies(epan version)
 
 set(FULL_SO_VERSION "0.0.0")
 
-set_target_properties(epan PROPERTIES COMPILE_DEFINITIONS "WS_BUILD_DLL")
-set_target_properties(epan PROPERTIES COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}")
-set_target_properties(epan PROPERTIES LINK_FLAGS "${WS_LINK_FLAGS}")
-set_target_properties(epan PROPERTIES VERSION ${FULL_SO_VERSION} SOVERSION 0)
+set_target_properties(epan PROPERTIES
+       COMPILE_DEFINITIONS "WS_BUILD_DLL"
+       LINK_FLAGS "${WS_LINK_FLAGS}"
+       VERSION ${FULL_SO_VERSION} SOVERSION 0
+)
 
 if(ENABLE_APPLICATION_BUNDLE)
        set_target_properties(epan PROPERTIES
@@ -444,7 +445,6 @@ target_link_libraries(exntest ${GLIB2_LIBRARIES})
 set_target_properties(exntest PROPERTIES
        FOLDER "Tests"
        COMPILE_DEFINITIONS "WS_BUILD_DLL"
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
 )
 
 add_executable(oids_test EXCLUDE_FROM_ALL oids_test.c)
@@ -452,7 +452,6 @@ target_link_libraries(oids_test epan ${ZLIB_LIBRARIES})
 set_target_properties(oids_test PROPERTIES
        FOLDER "Tests"
        COMPILE_DEFINITIONS "WS_BUILD_DLL"
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
 )
 
 add_executable(reassemble_test EXCLUDE_FROM_ALL reassemble_test.c)
@@ -466,7 +465,6 @@ target_link_libraries(tvbtest epan)
 set_target_properties(tvbtest PROPERTIES
        FOLDER "Tests"
        COMPILE_DEFINITIONS "WS_BUILD_DLL"
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
 )
 
 CHECKAPI(
index a06aa604826d644d30626b393ea78057390119d6..b76188f406146aad303c2ae8cd644ffc3a1f4505 100644 (file)
@@ -48,7 +48,6 @@ add_library(crypt OBJECT
 set_target_properties(crypt PROPERTIES
        FOLDER "Libs/epan/crypt"
        COMPILE_DEFINITIONS "WS_BUILD_DLL"
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
 )
 
 CHECKAPI(
index 4a2ebad0f90e6f17be798e666f044077dab83bd1..3f60b771f7a9db16a060ddd3fe6a502c06fe11b7 100644 (file)
@@ -70,7 +70,6 @@ add_library(dfilter OBJECT
 set_target_properties(dfilter PROPERTIES
        FOLDER "Libs/epan/dfilter"
        COMPILE_DEFINITIONS "WS_BUILD_DLL"
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
 )
 
 if(NOT WIN32)
index 0b5c5921a2327ca20f6bb4bfb9511e80385304a0..1aa7b854267428e7d81097e0ba1a585010c8b678 100644 (file)
@@ -196,7 +196,6 @@ add_library(dissectors-corba OBJECT
 set_target_properties(dissectors-corba PROPERTIES
        FOLDER "Libs/epan/dissectors"
        COMPILE_DEFINITIONS "WS_BUILD_DLL"
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
 )
 
 if(WIN32 AND "${CMAKE_GENERATOR}" STREQUAL "Visual Studio 12 2013 Win64")
@@ -1857,7 +1856,6 @@ add_library(dissectors OBJECT
 set_target_properties(dissectors PROPERTIES
        FOLDER "Libs/epan/dissectors"
        COMPILE_DEFINITIONS "WS_BUILD_DLL"
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
 )
 
 if(NOT WIN32)
index 1ca5e213ee2e0752e36523089888998ac3b76750..fb9bac2fd519c9ef90fa32f015bbc2e049003fa2 100644 (file)
@@ -58,7 +58,6 @@ add_library(ftypes OBJECT
 set_target_properties(ftypes PROPERTIES
        FOLDER "Libs/epan/ftypes"
        COMPILE_DEFINITIONS "WS_BUILD_DLL"
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
 )
 
 if(NOT WIN32)
index 87a077472da71fd70de6694724153cefb2d3929b..710f023f7419daa6c034ad9c563b84bdec7d459c 100644 (file)
@@ -74,7 +74,6 @@ add_library(wmem OBJECT
 set_target_properties(wmem PROPERTIES
        FOLDER "Libs/epan/wmem"
        COMPILE_DEFINITIONS "WS_BUILD_DLL"
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
 )
 
 add_executable(wmem_test EXCLUDE_FROM_ALL wmem_test.c $<TARGET_OBJECTS:wmem>)
@@ -84,7 +83,6 @@ target_link_libraries(wmem_test ${GLIB2_LIBRARIES} wsutil)
 set_target_properties(wmem_test PROPERTIES
        FOLDER "Tests"
        COMPILE_DEFINITIONS "WS_BUILD_DLL"
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
 )
 
 if(NOT WIN32)
index 473558d709a530fce94660d494363d572b6b712c..b32eaa0269223af2319cfa5aef2249057f985e90 100644 (file)
@@ -160,7 +160,6 @@ add_library(wslua OBJECT
 set_target_properties(wslua PROPERTIES
        FOLDER "Libs/epan/wslua"
        COMPILE_DEFINITIONS "WS_BUILD_DLL"
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
 )
 
 CHECKAPI(
index dc88b07e55fc14f9f6b9d2369381a6917058c9c5..f7d5069e8df37007eba506a9fd153a15fa28ffb2 100644 (file)
@@ -41,7 +41,6 @@ add_library(randpkt_core STATIC
 )
 
 set_target_properties(randpkt_core PROPERTIES
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
        LINK_FLAGS "${WS_LINK_FLAGS}"
        FOLDER "Libs")
 
index 43a8e9d41b669013ca11c8a3be47ada8be2c3b29..a598e820b604d746bfccc726e5420846bbbb6a6d 100644 (file)
@@ -38,9 +38,11 @@ set(lemon_LIBS
 )
 
 add_executable(lemon ${lemon_FILES})
-set_target_properties(lemon PROPERTIES FOLDER "tools")
-set_target_properties(lemon PROPERTIES COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}")
-set_target_properties(lemon PROPERTIES LINK_FLAGS "${NO_SANITIZE_LDFLAGS}")
+
+set_target_properties(lemon PROPERTIES
+       FOLDER "tools"
+       LINK_FLAGS "${NO_SANITIZE_LDFLAGS}"
+)
 
 target_link_libraries(lemon ${lemon_LIBS})
 
index 7c7ac38792a884817ac7055965bcd209e8c72ca9..275c8eda43a66a250b11bbc8d6662b7337fe3310 100644 (file)
@@ -92,9 +92,10 @@ add_library(ui STATIC
        ${GENERATED_FILES}
 )
 
-set_target_properties(ui PROPERTIES COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}")
-set_target_properties(ui PROPERTIES LINK_FLAGS "${WS_LINK_FLAGS}")
-set_target_properties(ui PROPERTIES FOLDER "UI")
+set_target_properties(ui PROPERTIES
+       LINK_FLAGS "${WS_LINK_FLAGS}"
+       FOLDER "UI"
+)
 
 CHECKAPI(
        NAME
index b64973da5a677f706328d47fa8592c222abdc120..381fa528e1de8f3e6298beb67b53c7d379200447 100644 (file)
@@ -315,9 +315,9 @@ add_library(gtkui STATIC
        ${PIXBUF_SRC}
 )
 set_target_properties(gtkui PROPERTIES
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
        LINK_FLAGS "${WS_LINK_FLAGS}"
-       FOLDER "UI")
+       FOLDER "UI"
+)
 
 CHECKAPI(
        NAME
index 3822e050f0c4698298b8a95b843bdf126bfb63f4..22e7702f58ead0358e31d5e0d06de375c4f59f49 100644 (file)
@@ -617,9 +617,10 @@ add_library(qtui STATIC
        wireshark-tap-register.c
 )
 add_dependencies(qtui translations)
-set_target_properties(qtui PROPERTIES COMPILE_OPTIONS "${WS_WARNINGS_CXX_FLAGS}")
-set_target_properties(qtui PROPERTIES LINK_FLAGS "${WS_LINK_FLAGS}")
-set_target_properties(qtui PROPERTIES FOLDER "UI")
+set_target_properties(qtui PROPERTIES
+       LINK_FLAGS "${WS_LINK_FLAGS}"
+       FOLDER "UI"
+)
 
 #
 # Editor modelines  -  http://www.wireshark.org/tools/modelines.html
index 20a41de35569c8d1a7910f00cadf65cf487fefca..c04afb3cedcf8dcb86e0ab618b3347f3f93db02c 100644 (file)
@@ -128,7 +128,6 @@ set(FULL_SO_VERSION "0.0.0")
 set_target_properties(wiretap PROPERTIES
        PREFIX "lib"
        COMPILE_DEFINITIONS "WS_BUILD_DLL"
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
        LINK_FLAGS "${WS_LINK_FLAGS}"
        VERSION ${FULL_SO_VERSION} SOVERSION 0
        FOLDER "DLLs"
index 42483f93af61f4dc99064870bebefa5e328c2479..8d6ce4b88fdfff023db1b2f8283be2e2d83b5a8d 100644 (file)
@@ -39,9 +39,9 @@ add_library(writecap STATIC
 )
 
 set_target_properties(writecap PROPERTIES
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
        LINK_FLAGS "${WS_LINK_FLAGS}"
-       FOLDER "Libs")
+       FOLDER "Libs"
+)
 
 #
 # Editor modelines  -  http://www.wireshark.org/tools/modelines.html
index 43a574d6789dc263c26f6c8c3128fdddbfac71eb..4f117a757a1df2ba0a2f8c591b18cc2a88366ac9 100644 (file)
@@ -276,10 +276,10 @@ set(FULL_SO_VERSION "0.0.0")
 set_target_properties(wsutil PROPERTIES
        PREFIX "lib"
        COMPILE_DEFINITIONS "WS_BUILD_DLL"
-       COMPILE_OPTIONS "${WS_WARNINGS_C_FLAGS}"
        LINK_FLAGS "${WS_LINK_FLAGS}"
        VERSION ${FULL_SO_VERSION} SOVERSION 0
-       FOLDER "DLLs")
+       FOLDER "DLLs"
+)
 
 if(ENABLE_APPLICATION_BUNDLE)
        set_target_properties(wsutil PROPERTIES