Change compile flag and clearer to read
[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 minimal version
37 if (kernel_minimal_version)
38         if(DEFINED ENV{SDKTARGETSYSROOT})
39                 file(STRINGS $ENV{SDKTARGETSYSROOT}/usr/include/linux/version.h LINUX_VERSION_CODE_LINE REGEX "LINUX_VERSION_CODE")
40         elseif(DEFINED ENV{PKG_CONFIG_SYSROOT_DIR})
41                 file(STRINGS $ENV{PKG_CONFIG_SYSROOT_DIR}/usr/include/linux/version.h LINUX_VERSION_CODE_LINE REGEX "LINUX_VERSION_CODE")
42         else()
43                 file(STRINGS /usr/include/linux/version.h LINUX_VERSION_CODE_LINE REGEX "LINUX_VERSION_CODE")
44         endif()
45
46         string(REGEX MATCH "[0-9]+" LINUX_VERSION_CODE ${LINUX_VERSION_CODE_LINE})
47         math(EXPR a "${LINUX_VERSION_CODE} >> 16")
48         math(EXPR b "(${LINUX_VERSION_CODE} >> 8) & 255")
49         math(EXPR c "(${LINUX_VERSION_CODE} & 255)")
50
51         set(KERNEL_VERSION "${a}.${b}.${c}")
52         message (STATUS "${Cyan}-- Check kernel_minimal_version (found kernel version ${KERNEL_VERSION})${ColourReset}")
53
54         if (KERNEL_VERSION VERSION_LESS ${kernel_minimal_version})
55                 message(FATAL_ERROR "${Red}**** FATAL: Require at least ${kernel_minimal_version} please use a recent kernel or source your SDK environment then clean and reconfigure your CMake project.")
56         endif (KERNEL_VERSION VERSION_LESS ${kernel_minimal_version})
57 endif(kernel_minimal_version)
58
59 INCLUDE(FindPkgConfig)
60 INCLUDE(CheckIncludeFiles)
61 INCLUDE(CheckLibraryExists)
62 INCLUDE(GNUInstallDirs)
63
64 # Default compilation options
65 ############################################################################
66 link_libraries(-Wl,--as-needed -Wl,--gc-sections)
67 add_compile_options(-Wall -Wextra -Wconversion)
68 add_compile_options(-Wno-unused-parameter) # frankly not using a parameter does it care?
69 add_compile_options(-Wno-sign-compare -Wno-sign-conversion)
70 add_compile_options(-Werror=maybe-uninitialized)
71 add_compile_options(-Werror=implicit-function-declaration)
72 add_compile_options(-ffunction-sections -fdata-sections)
73 add_compile_options(-fPIC)
74
75 # TODO: make more visible readable compile (macro ? split ?)
76 # Compilation option depending on CMAKE_BUILD_TYPE
77 ##################################################
78 add_compile_options($<$<CONFIG:PROFILING>:-g>)
79 add_compile_options($<$<CONFIG:PROFILING>:-O0>)
80 add_compile_options($<$<CONFIG:PROFILING>:-pg>)
81 add_compile_options($<$<CONFIG:PROFILING>:-Wp,-U_FORTIFY_SOURCE>)
82
83 add_compile_options($<$<CONFIG:DEBUG>:-g>)
84 add_compile_options($<$<CONFIG:DEBUG>:-O0>)
85 add_compile_options($<$<CONFIG:DEBUG>:-ggdb>)
86 add_compile_options($<$<CONFIG:DEBUG>:-Wp,-U_FORTIFY_SOURCE>)
87
88 add_compile_options($<$<CONFIG:CCOV>:-g>)
89 add_compile_options($<$<CONFIG:CCOV>:-O2>)
90 add_compile_options($<$<CONFIG:CCOV>:--coverage>)
91
92 # Env variable overload default
93 if(DEFINED ENV{INSTALL_PREFIX})
94         set(INSTALL_PREFIX $ENV{INSTALL_PREFIX} CACHE PATH "The path where to install")
95 else()
96         set(INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/Install" CACHE PATH "The path where to install")
97 endif()
98 set(CMAKE_INSTALL_PREFIX ${INSTALL_PREFIX} CACHE STRING "Installation Prefix")
99
100 # Loop on required package and add options
101 foreach (PKG_CONFIG ${PKG_REQUIRED_LIST})
102         string(REGEX REPLACE "[<>]?=.*$" "" XPREFIX ${PKG_CONFIG})
103         PKG_CHECK_MODULES(${XPREFIX} REQUIRED ${PKG_CONFIG})
104
105         INCLUDE_DIRECTORIES(${${XPREFIX}_INCLUDE_DIRS})
106         list (APPEND link_libraries ${${XPREFIX}_LDFLAGS})
107         add_compile_options (${${XPREFIX}_CFLAGS})
108 endforeach(PKG_CONFIG)
109
110 # Optional LibEfence Malloc debug library
111 IF(CMAKE_BUILD_TYPE MATCHES DEBUG)
112 CHECK_LIBRARY_EXISTS(efence malloc "" HAVE_LIBEFENCE)
113 IF(HAVE_LIBEFENCE)
114         MESSAGE(STATUS "Linking with ElectricFence for debugging purposes...")
115         SET(libefence_LIBRARIES "-lefence")
116         list (APPEND link_libraries ${libefence_LIBRARIES})
117 ENDIF(HAVE_LIBEFENCE)
118 ENDIF(CMAKE_BUILD_TYPE MATCHES DEBUG)
119
120 # set default include directories
121 INCLUDE_DIRECTORIES(${EXTRA_INCLUDE_DIRS})
122
123 # Default Linkflag
124 if(NOT BINDINGS_LINK_FLAG)
125         set(BINDINGS_LINK_FLAG "-Wl,--version-script=${PKG_TEMPLATE_PREFIX}/cmake/export.map")
126 endif()