b2162623bc2796359a4b4efaf13dc12ff78e0c13
[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         else()
41                 file(STRINGS /usr/include/linux/version.h LINUX_VERSION_CODE_LINE REGEX "LINUX_VERSION_CODE")
42         endif()
43
44         string(REGEX MATCH "[0-9]+" LINUX_VERSION_CODE ${LINUX_VERSION_CODE_LINE})
45         math(EXPR a "${LINUX_VERSION_CODE} >> 16")
46         math(EXPR b "(${LINUX_VERSION_CODE} >> 8) & 255")
47         math(EXPR c "(${LINUX_VERSION_CODE} & 255)")
48
49         set(KERNEL_VERSION "${a}.${b}.${c}")
50         message (STATUS "${Cyan}-- Check kernel_minimal_version (found kernel version ${KERNEL_VERSION})${ColourReset}")
51
52         if (KERNEL_VERSION VERSION_LESS ${kernel_minimal_version})
53                 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.")
54         endif (KERNEL_VERSION VERSION_LESS ${kernel_minimal_version})
55 endif(kernel_minimal_version)
56
57 INCLUDE(FindPkgConfig)
58 INCLUDE(CheckIncludeFiles)
59 INCLUDE(CheckLibraryExists)
60 INCLUDE(GNUInstallDirs)
61
62 # Default compilation options
63 ############################################################################
64 link_libraries(-Wl,--as-needed -Wl,--gc-sections)
65 add_compile_options(-Wall -Wextra -Wconversion)
66 add_compile_options(-Wno-unused-parameter) # frankly not using a parameter does it care?
67 add_compile_options(-Wno-sign-compare -Wno-sign-conversion)
68 add_compile_options(-Werror=maybe-uninitialized)
69 add_compile_options(-Werror=implicit-function-declaration)
70 add_compile_options(-ffunction-sections -fdata-sections)
71 add_compile_options(-fPIC)
72 add_compile_options(-g)
73
74 set(CMAKE_C_FLAGS_PROFILING   "-g -O2 -pg -Wp,-U_FORTIFY_SOURCE" CACHE STRING "Flags for profiling")
75 set(CMAKE_C_FLAGS_DEBUG       "-g -O2 -ggdb -Wp,-U_FORTIFY_SOURCE" CACHE STRING "Flags for debugging")
76 set(CMAKE_C_FLAGS_RELEASE     "-O2" CACHE STRING "Flags for releasing")
77 set(CMAKE_C_FLAGS_CCOV        "-g -O2 --coverage" CACHE STRING "Flags for coverage test")
78
79 set(CMAKE_CXX_FLAGS_PROFILING    "-g -O0 -pg -Wp,-U_FORTIFY_SOURCE")
80 set(CMAKE_CXX_FLAGS_DEBUG        "-g -O0 -ggdb -Wp,-U_FORTIFY_SOURCE")
81 set(CMAKE_CXX_FLAGS_RELEASE      "-g -O2")
82 set(CMAKE_CXX_FLAGS_CCOV "-g -O2 --coverage")
83
84 # Env variable overload default
85 if(DEFINED ENV{INSTALL_PREFIX})
86         set (INSTALL_PREFIX $ENV{INSTALL_PREFIX})
87 else()
88         set(INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/Install" CACHE PATH "The path where to install")
89 endif()
90 set(CMAKE_INSTALL_PREFIX ${INSTALL_PREFIX} CACHE STRING "Installation Prefix")
91
92 # (BUG!!!) as PKG_CONFIG_PATH does not work [should be en env variable]
93 set(PKG_CONFIG_USE_CMAKE_PREFIX_PATH ON CACHE BOOLEAN "Flag for using prefix path")
94
95 # Loop on required package and add options
96 foreach (PKG_CONFIG ${PKG_REQUIRED_LIST})
97         string(REGEX REPLACE "[<>]?=.*$" "" XPREFIX ${PKG_CONFIG})
98         PKG_CHECK_MODULES(${XPREFIX} REQUIRED ${PKG_CONFIG})
99
100         INCLUDE_DIRECTORIES(${${XPREFIX}_INCLUDE_DIRS})
101         list (APPEND link_libraries ${${XPREFIX}_LDFLAGS})
102         add_compile_options (${${XPREFIX}_CFLAGS})
103 endforeach(PKG_CONFIG)
104
105 # Optional LibEfence Malloc debug library
106 IF(CMAKE_BUILD_TYPE MATCHES DEBUG)
107 CHECK_LIBRARY_EXISTS(efence malloc "" HAVE_LIBEFENCE)
108 IF(HAVE_LIBEFENCE)
109         MESSAGE(STATUS "Linking with ElectricFence for debugging purposes...")
110         SET(libefence_LIBRARIES "-lefence")
111         list (APPEND link_libraries ${libefence_LIBRARIES})
112 ENDIF(HAVE_LIBEFENCE)
113 ENDIF(CMAKE_BUILD_TYPE MATCHES DEBUG)
114
115 # set default include directories
116 INCLUDE_DIRECTORIES(${EXTRA_INCLUDE_DIRS})
117
118 # Default Linkflag
119 if(NOT BINDINGS_LINK_FLAG)
120         set(BINDINGS_LINK_FLAG "-Wl,--version-script=${PKG_TEMPLATE_PREFIX}/cmake/export.map")
121 endif()