Change compilation flags setup.
[staging/xdg-launcher.git] / cmake / cmake.d / 04-build_options.cmake
1 ###########################################################################
2 # Copyright 2015, 2016, 2017 IoT.bzh
3 #
4 # author: Fulup Ar Foll <fulup@iot.bzh>
5 # contrib: Romain Forlot <romain.forlot@iot.bzh>
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 ###########################################################################
19
20
21 #--------------------------------------------------------------------------
22 #  WARNING:
23 #     Do not change this cmake template
24 #     Customise your preferences in "./conf.d/cmake/config.cmake"
25 #--------------------------------------------------------------------------
26
27 # Check GCC minimal version
28 if (gcc_minimal_version)
29                 message (STATUS "${Cyan}-- Check gcc_minimal_version (found gcc version ${CMAKE_C_COMPILER_VERSION}) \
30                 (found g++ version ${CMAKE_CXX_COMPILER_VERSION})${ColourReset}")
31         if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${gcc_minimal_version} OR CMAKE_C_COMPILER_VERSION VERSION_LESS ${gcc_minimal_version})
32                 message(FATAL_ERROR "${Red}**** FATAL: Require at least gcc-${gcc_minimal_version} please set CMAKE_C[XX]_COMPILER")
33         endif()
34 endif(gcc_minimal_version)
35
36 # Check Kernel mandatory version, will fail the configuration if required version not matched.
37 if (kernel_mandatory_version)
38         message (STATUS "${Cyan}-- Check kernel_mandatory_version (found kernel version ${KERNEL_VERSION})${ColourReset}")
39         if (KERNEL_VERSION VERSION_LESS ${kernel_mandatory_version})
40                 message(FATAL_ERROR "${Red}**** FATAL: Require at least ${kernel_mandatory_version} please use a recent kernel or source your SDK environment then clean and reconfigure your CMake project.")
41         endif (KERNEL_VERSION VERSION_LESS ${kernel_mandatory_version})
42 endif(kernel_mandatory_version)
43
44 # Check Kernel minimal version just print a Warning about missing features
45 # and set a definition to be used as preprocessor condition in code to disable
46 # incompatibles features.
47 if (kernel_minimal_version)
48         message (STATUS "${Cyan}-- Check kernel_minimal_version (found kernel version ${KERNEL_VERSION})${ColourReset}")
49         if (KERNEL_VERSION VERSION_LESS ${kernel_minimal_version})
50                 message(WARNING "${Yellow}**** Warning: Some feature(s) require at least ${kernel_minimal_version}. Please use a recent kernel or source your SDK environment then clean and reconfigure your CMake project.${ColourReset}")
51         else (KERNEL_VERSION VERSION_LESS ${kernel_minimal_version})
52                 add_definitions(-DKERNEL_MINIMAL_VERSION_OK)
53         endif (KERNEL_VERSION VERSION_LESS ${kernel_minimal_version})
54 endif(kernel_minimal_version)
55
56 INCLUDE(FindPkgConfig)
57 INCLUDE(CheckIncludeFiles)
58 INCLUDE(CheckLibraryExists)
59 INCLUDE(GNUInstallDirs)
60
61 # Default compilation options
62 ############################################################################
63 link_libraries(-Wl,--as-needed -Wl,--gc-sections)
64 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")
65 foreach(option ${COMPILE_OPTIONS})
66         add_compile_options($<$<CONFIG:PROFILING>:${option}>)
67 endforeach()
68
69 # Compilation OPTIONS depending on language
70 #########################################
71
72 foreach(option ${COMPILE_OPTIONS})
73         add_compile_options(${option})
74 endforeach()
75 foreach(option ${C_COMPILE_OPTIONS})
76         add_compile_options($<$<COMPILE_LANGUAGE:C>:${option}>)
77 endforeach()
78 foreach(option ${CXX_COMPILE_OPTIONS})
79         add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${option}>)
80 endforeach()
81
82 # Compilation option depending on CMAKE_BUILD_TYPE
83 ##################################################
84 set(PROFILING_COMPILE_OPTIONS "-g" "-O0" "-pg" "-Wp,-U_FORTIFY_SOURCE" CACHE STRING "Compilation flags for PROFILING build type.")
85 set(DEBUG_COMPILE_OPTIONS "-g" "-ggdb" "-Wp,-U_FORTIFY_SOURCE" CACHE STRING "Compilation flags for DEBUG build type.")
86 set(CCOV_COMPILE_OPTIONS "-g" "-O2" "--coverage" CACHE STRING "Compilation flags for CCOV build type.")
87 set(RELEASE_COMPILE_OPTIONS "-g" "-O2" CACHE STRING "Compilation flags for RELEASE build type.")
88 foreach(option ${PROFILING_COMPILE_OPTIONS})
89         add_compile_options($<$<CONFIG:PROFILING>:${option}>)
90 endforeach()
91 foreach(option ${DEBUG_COMPILE_OPTIONS})
92         add_compile_options($<$<CONFIG:PROFILING>:${option}>)
93 endforeach()
94 foreach(option ${CCOV_COMPILE_OPTIONS})
95         add_compile_options($<$<CONFIG:PROFILING>:${option}>)
96 endforeach()
97 foreach(option ${RELEASE_COMPILE_OPTIONS})
98         add_compile_options($<$<CONFIG:PROFILING>:${option}>)
99 endforeach()
100
101 # Env variable overload default
102 if(DEFINED ENV{INSTALL_PREFIX})
103         set(INSTALL_PREFIX $ENV{INSTALL_PREFIX} CACHE PATH "The path where to install")
104 else()
105         set(INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/Install" CACHE PATH "The path where to install")
106 endif()
107 set(CMAKE_INSTALL_PREFIX ${INSTALL_PREFIX} CACHE STRING "Installation Prefix")
108
109 # Loop on required package and add options
110 foreach (PKG_CONFIG ${PKG_REQUIRED_LIST})
111         string(REGEX REPLACE "[<>]?=.*$" "" XPREFIX ${PKG_CONFIG})
112         PKG_CHECK_MODULES(${XPREFIX} REQUIRED ${PKG_CONFIG})
113
114         INCLUDE_DIRECTORIES(${${XPREFIX}_INCLUDE_DIRS})
115         list (APPEND link_libraries ${${XPREFIX}_LDOPTIONS})
116         add_compile_options (${${XPREFIX}_COPTIONS})
117 endforeach(PKG_CONFIG)
118
119 # Optional LibEfence Malloc debug library
120 IF(CMAKE_BUILD_TYPE MATCHES DEBUG)
121 CHECK_LIBRARY_EXISTS(efence malloc "" HAVE_LIBEFENCE)
122 IF(HAVE_LIBEFENCE)
123         MESSAGE(STATUS "Linking with ElectricFence for debugging purposes...")
124         SET(libefence_LIBRARIES "-lefence")
125         list (APPEND link_libraries ${libefence_LIBRARIES})
126 ENDIF(HAVE_LIBEFENCE)
127 ENDIF(CMAKE_BUILD_TYPE MATCHES DEBUG)
128
129 # set default include directories
130 INCLUDE_DIRECTORIES(${EXTRA_INCLUDE_DIRS})
131
132 # Default Linkflag
133 if(NOT BINDINGS_LINK_FLAG)
134         set(BINDINGS_LINK_FLAG "-Wl,--version-script=${PKG_TEMPLATE_PREFIX}/cmake/export.map")
135 endif()