Change compilation flags setup.
authorRomain Forlot <romain.forlot@iot.bzh>
Thu, 10 Aug 2017 09:51:35 +0000 (11:51 +0200)
committerRomain Forlot <romain.forlot@iot.bzh>
Thu, 10 Aug 2017 09:51:35 +0000 (11:51 +0200)
Use CMAKE cache variables other than defaults ones to don't be in
conflict and iterates over them with add_compile_options command

Change-Id: I5e62ab1a340aca15eafeb687998c06e13a571357
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
cmake/cmake.d/04-build_options.cmake
cmake/config.cmake.sample

index b0257f9..7f990b2 100644 (file)
@@ -61,29 +61,42 @@ INCLUDE(GNUInstallDirs)
 # Default compilation options
 ############################################################################
 link_libraries(-Wl,--as-needed -Wl,--gc-sections)
-add_compile_options(-Wall -Wextra -Wconversion)
-add_compile_options(-Wno-unused-parameter) # frankly not using a parameter does it care?
-add_compile_options(-Wno-sign-compare -Wno-sign-conversion)
-add_compile_options(-Werror=maybe-uninitialized)
-add_compile_options(-Werror=implicit-function-declaration)
-add_compile_options(-ffunction-sections -fdata-sections)
-add_compile_options(-fPIC)
-
-# TODO: make more visible readable compile (macro ? split ?)
+set(COMPILE_OPTIONS "-Wall" "-Wextra" "-Wconversion" "-Wno-unused-parameter" "-Wno-sign-compare" "-Wno-sign-conversion" "-Werror=maybe-uninitialized" "-Werror=implicit-function-declaration" "-ffunction-sections" "-fdata-sections" "-fPIC" CACHE STRING "Compilation flags")
+foreach(option ${COMPILE_OPTIONS})
+       add_compile_options($<$<CONFIG:PROFILING>:${option}>)
+endforeach()
+
+# Compilation OPTIONS depending on language
+#########################################
+
+foreach(option ${COMPILE_OPTIONS})
+       add_compile_options(${option})
+endforeach()
+foreach(option ${C_COMPILE_OPTIONS})
+       add_compile_options($<$<COMPILE_LANGUAGE:C>:${option}>)
+endforeach()
+foreach(option ${CXX_COMPILE_OPTIONS})
+       add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${option}>)
+endforeach()
+
 # Compilation option depending on CMAKE_BUILD_TYPE
 ##################################################
-add_compile_options($<$<CONFIG:PROFILING>:-g>)
-add_compile_options($<$<CONFIG:PROFILING>:-O0>)
-add_compile_options($<$<CONFIG:PROFILING>:-pg>)
-add_compile_options($<$<CONFIG:PROFILING>:-Wp,-U_FORTIFY_SOURCE>)
-
-add_compile_options($<$<CONFIG:DEBUG>:-g>)
-add_compile_options($<$<CONFIG:DEBUG>:-ggdb>)
-add_compile_options($<$<CONFIG:DEBUG>:-Wp,-U_FORTIFY_SOURCE>)
-
-add_compile_options($<$<CONFIG:CCOV>:-g>)
-add_compile_options($<$<CONFIG:CCOV>:-O2>)
-add_compile_options($<$<CONFIG:CCOV>:--coverage>)
+set(PROFILING_COMPILE_OPTIONS "-g" "-O0" "-pg" "-Wp,-U_FORTIFY_SOURCE" CACHE STRING "Compilation flags for PROFILING build type.")
+set(DEBUG_COMPILE_OPTIONS "-g" "-ggdb" "-Wp,-U_FORTIFY_SOURCE" CACHE STRING "Compilation flags for DEBUG build type.")
+set(CCOV_COMPILE_OPTIONS "-g" "-O2" "--coverage" CACHE STRING "Compilation flags for CCOV build type.")
+set(RELEASE_COMPILE_OPTIONS "-g" "-O2" CACHE STRING "Compilation flags for RELEASE build type.")
+foreach(option ${PROFILING_COMPILE_OPTIONS})
+       add_compile_options($<$<CONFIG:PROFILING>:${option}>)
+endforeach()
+foreach(option ${DEBUG_COMPILE_OPTIONS})
+       add_compile_options($<$<CONFIG:PROFILING>:${option}>)
+endforeach()
+foreach(option ${CCOV_COMPILE_OPTIONS})
+       add_compile_options($<$<CONFIG:PROFILING>:${option}>)
+endforeach()
+foreach(option ${RELEASE_COMPILE_OPTIONS})
+       add_compile_options($<$<CONFIG:PROFILING>:${option}>)
+endforeach()
 
 # Env variable overload default
 if(DEFINED ENV{INSTALL_PREFIX})
@@ -99,8 +112,8 @@ foreach (PKG_CONFIG ${PKG_REQUIRED_LIST})
        PKG_CHECK_MODULES(${XPREFIX} REQUIRED ${PKG_CONFIG})
 
        INCLUDE_DIRECTORIES(${${XPREFIX}_INCLUDE_DIRS})
-       list (APPEND link_libraries ${${XPREFIX}_LDFLAGS})
-       add_compile_options (${${XPREFIX}_CFLAGS})
+       list (APPEND link_libraries ${${XPREFIX}_LDOPTIONS})
+       add_compile_options (${${XPREFIX}_COPTIONS})
 endforeach(PKG_CONFIG)
 
 # Optional LibEfence Malloc debug library
index 23f1e80..f5ac42a 100644 (file)
@@ -80,8 +80,17 @@ set (PKG_REQUIRED_LIST
 
 # Compilation options definition
 # Use CMake generator expressions to specify only for a specific language
-# -----------------------------------------------------------------------
-#add_compile_options()
+# Values are prefilled with default options that is currently used.
+# Either separate options with ";", or each options must be quoted separately
+# DO NOT PUT ALL OPTION QUOTED AT ONCE , COMPILATION COULD FAILED !
+# ----------------------------------------------------------------------------
+#set(COMPILE_OPTIONS "-Wall" "-Wextra" "-Wconversion" "-Wno-unused-parameter" "-Wno-sign-compare" "-Wno-sign-conversion" "-Werror=maybe-uninitialized" "-Werror=implicit-function-declaration" "-ffunction-sections" "-fdata-sections" "-fPIC" CACHE STRING "Compilation flags")
+#set(C_COMPILE_OPTIONS "" CACHE STRING "Compilation flags for C language.")
+#set(CXX_COMPILE_OPTIONS "" CACHE STRING "Compilation flags for C++ language.")
+#set(PROFILING_COMPILE_OPTIONS "-g" "-O0" "-pg" "-Wp,-U_FORTIFY_SOURCE" CACHE STRING "Compilation flags for PROFILING build type.")
+#set(DEBUG_COMPILE_OPTIONS "-g" "-ggdb" "-Wp,-U_FORTIFY_SOURCE" CACHE STRING "Compilation flags for DEBUG build type.")
+#set(CCOV_COMPILE_OPTIONS "-g" "-O2" "--coverage" CACHE STRING "Compilation flags for CCOV build type.")
+#set(RELEASE_COMPILE_OPTIONS "-g" "-O2" CACHE STRING "Compilation flags for RELEASE build type.")
 
 # Print a helper message when every thing is finished
 # ----------------------------------------------------