Fix: missing gcov symbol in compiled binaries
[apps/app-templates.git] / cmake / cmake.d / 01-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 # (BUG!!!) as PKG_CONFIG_PATH does not work [should be en env variable]
28 set(PKG_CONFIG_USE_CMAKE_PREFIX_PATH ON CACHE BOOLEAN "Flag for using prefix path")
29
30 INCLUDE(FindPkgConfig)
31 INCLUDE(CheckIncludeFiles)
32 INCLUDE(CheckLibraryExists)
33 INCLUDE(GNUInstallDirs)
34
35 if(NOT CMAKE_BUILD_TYPE)
36         if(BUILD_TYPE)
37                 set(CMAKE_BUILD_TYPE ${BUILD_TYPE} CACHE STRING "the type of build" FORCE)
38         else()
39                 set(CMAKE_BUILD_TYPE RELEASE CACHE STRING "the type of build" FORCE)
40         endif()
41 endif()
42 if(NOT DEFINED BUILD_TEST_WGT)
43         set(BUILD_TEST_WGT FALSE)
44 endif()
45
46 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
47 set(CMP0048 1)
48
49 # Default compilation options
50 ############################################################################
51 link_libraries(-Wl,--as-needed -Wl,--gc-sections)
52 set(COMPILE_OPTIONS -Wall
53  -Wextra
54  -Wconversion
55  -Wno-unused-parameter
56  -Wno-sign-compare
57  -Wno-sign-conversion
58  -Werror=implicit-function-declaration
59  -ffunction-sections
60  -fdata-sections
61  -fPIC CACHE STRING "Compilation flags")
62
63 set(COMPILE_OPTIONS_GNU -Werror=maybe-uninitialized CACHE STRING "GNU Compile specific options")
64 set(COMPILE_OPTIONS_CLANG -Werror=uninitialized CACHE STRING "CLang compile specific options")
65
66 # Compilation OPTIONS depending on language
67 #########################################
68 foreach(option ${COMPILE_OPTIONS})
69         add_compile_options(${option})
70 endforeach()
71
72 if (${CMAKE_C_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
73         foreach(option ${COMPILE_OPTIONS_GNU})
74                 add_compile_options(${option})
75         endforeach()
76 elseif(${CMAKE_C_COMPILER_ID} STREQUAL "Clang" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
77         foreach(option ${COMPILE_OPTIONS_CLANG})
78                 add_compile_options(${option})
79         endforeach()
80 endif()
81
82 foreach(option ${C_COMPILE_OPTIONS})
83         add_compile_options($<$<COMPILE_LANGUAGE:C>:${option}>)
84 endforeach()
85 foreach(option ${CXX_COMPILE_OPTIONS})
86         add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${option}>)
87 endforeach()
88
89 # Compilation option depending on CMAKE_BUILD_TYPE
90 ##################################################
91 set(PROFILING_COMPILE_OPTIONS -g -O0 -pg -Wp,-U_FORTIFY_SOURCE CACHE STRING "Compilation flags for PROFILING build type.")
92 set(DEBUG_COMPILE_OPTIONS -g -ggdb CACHE STRING "Compilation flags for DEBUG build type.")
93 set(COVERAGE_COMPILE_OPTIONS -g --coverage CACHE STRING "Compilation flags for COVERAGE build type.")
94 set(RELEASE_COMPILE_OPTIONS -O2 -D_FORTIFY_SOURCE=2 CACHE STRING "Compilation flags for RELEASE build type.")
95 foreach(option ${PROFILING_COMPILE_OPTIONS})
96         add_compile_options($<$<CONFIG:PROFILING>:${option}>)
97 endforeach()
98 foreach(option ${DEBUG_COMPILE_OPTIONS})
99         add_compile_options($<$<CONFIG:DEBUG>:${option}>)
100 endforeach()
101 foreach(option ${COVERAGE_COMPILE_OPTIONS})
102         add_compile_options($<$<CONFIG:COVERAGE>:${option}>)
103 endforeach()
104 foreach(option ${RELEASE_COMPILE_OPTIONS})
105         add_compile_options($<$<CONFIG:RELEASE>:${option}>)
106 endforeach()
107
108 # Loop on required package and add options
109 foreach (PKG_CONFIG ${PKG_REQUIRED_LIST})
110         string(REGEX REPLACE "[<>]?=.*$" "" XPREFIX ${PKG_CONFIG})
111         PKG_CHECK_MODULES(${XPREFIX} REQUIRED ${PKG_CONFIG})
112
113         INCLUDE_DIRECTORIES(${${XPREFIX}_INCLUDE_DIRS})
114         list(APPEND link_libraries ${${XPREFIX}_LDFLAGS})
115         add_compile_options (${${XPREFIX}_CFLAGS})
116 endforeach(PKG_CONFIG)
117
118 # Optional LibEfence Malloc debug library
119 IF(CMAKE_BUILD_TYPE MATCHES DEBUG AND USE_EFENCE)
120 CHECK_LIBRARY_EXISTS(efence malloc "" HAVE_LIBEFENCE)
121 IF(HAVE_LIBEFENCE)
122         MESSAGE(STATUS "Linking with ElectricFence for debugging purposes...")
123         SET(libefence_LIBRARIES "-lefence")
124         list (APPEND link_libraries ${libefence_LIBRARIES})
125 ENDIF(HAVE_LIBEFENCE)
126 ENDIF(CMAKE_BUILD_TYPE MATCHES DEBUG AND USE_EFENCE)
127 IF(${CMAKE_BUILD_TYPE} MATCHES COVERAGE)
128         list (APPEND link_libraries -coverage)
129 ENDIF(${CMAKE_BUILD_TYPE} MATCHES COVERAGE)
130
131 # set default include directories
132 INCLUDE_DIRECTORIES(${EXTRA_INCLUDE_DIRS})
133
134 # Default Linkflag
135 set(PKG_TEMPLATE_PREFIX "\"${CMAKE_SOURCE_DIR}/${PROJECT_APP_TEMPLATES_DIR}\"" CACHE PATH "Default Package Templates Directory")
136 set(BARE_PKG_TEMPLATE_PREFIX "${CMAKE_SOURCE_DIR}/${PROJECT_APP_TEMPLATES_DIR}" CACHE PATH "Default Package Templates Directory")
137
138 if(NOT BINDINGS_LINK_FLAG)
139         set(BINDINGS_LINK_FLAG "-Wl,--version-script=${PKG_TEMPLATE_PREFIX}/cmake/export.map")
140 endif()