72a316142e282aa3a4841ba9303755f2384222b7
[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
43 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
44 set(CMP0048 1)
45
46 # Default compilation options
47 ############################################################################
48 link_libraries(-Wl,--as-needed -Wl,--gc-sections)
49 set(COMPILE_OPTIONS -Wall
50  -Wextra
51  -Wconversion
52  -Wno-unused-parameter
53  -Wno-sign-compare
54  -Wno-sign-conversion
55  -Werror=implicit-function-declaration
56  -ffunction-sections
57  -fdata-sections
58  -fPIC CACHE STRING "Compilation flags")
59
60 set(COMPILE_OPTIONS_GNU -Werror=maybe-uninitialized CACHE STRING "GNU Compile specific options")
61 set(COMPILE_OPTIONS_CLANG -Werror=uninitialized CACHE STRING "CLang compile specific options")
62
63 # Compilation OPTIONS depending on language
64 #########################################
65 foreach(option ${COMPILE_OPTIONS})
66         add_compile_options(${option})
67 endforeach()
68
69 if (${CMAKE_C_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
70         foreach(option ${COMPILE_OPTIONS_GNU})
71                 add_compile_options(${option})
72         endforeach()
73 elseif(${CMAKE_C_COMPILER_ID} STREQUAL "Clang" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
74         foreach(option ${COMPILE_OPTIONS_CLANG})
75                 add_compile_options(${option})
76         endforeach()
77 endif()
78
79 foreach(option ${C_COMPILE_OPTIONS})
80         add_compile_options($<$<COMPILE_LANGUAGE:C>:${option}>)
81 endforeach()
82 foreach(option ${CXX_COMPILE_OPTIONS})
83         add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${option}>)
84 endforeach()
85
86 # Compilation option depending on CMAKE_BUILD_TYPE
87 ##################################################
88 set(PROFILING_COMPILE_OPTIONS -g -O0 -pg -Wp,-U_FORTIFY_SOURCE CACHE STRING "Compilation flags for PROFILING build type.")
89 set(DEBUG_COMPILE_OPTIONS -g -ggdb CACHE STRING "Compilation flags for DEBUG build type.")
90 set(TEST_COMPILE_OPTIONS -g -ggdb CACHE STRING "Compilation flags for TEST build type.")
91 set(COVERAGE_COMPILE_OPTIONS -g --coverage CACHE STRING "Compilation flags for COVERAGE build type.")
92 set(RELEASE_COMPILE_OPTIONS -O2 -D_FORTIFY_SOURCE=2 CACHE STRING "Compilation flags for RELEASE build type.")
93 foreach(option ${PROFILING_COMPILE_OPTIONS})
94         add_compile_options($<$<CONFIG:PROFILING>:${option}>)
95 endforeach()
96 foreach(option ${DEBUG_COMPILE_OPTIONS})
97         add_compile_options($<$<CONFIG:DEBUG>:${option}>)
98 endforeach()
99 foreach(option ${TEST_COMPILE_OPTIONS})
100         add_compile_options($<$<CONFIG:TEST>:${option}>)
101 endforeach()
102 foreach(option ${COVERAGE_COMPILE_OPTIONS})
103         add_compile_options($<$<CONFIG:COVERAGE>:${option}>)
104 endforeach()
105 foreach(option ${RELEASE_COMPILE_OPTIONS})
106         add_compile_options($<$<CONFIG:RELEASE>:${option}>)
107 endforeach()
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}_LDFLAGS})
116         add_compile_options (${${XPREFIX}_CFLAGS})
117 endforeach(PKG_CONFIG)
118
119 # Optional LibEfence Malloc debug library
120 IF(CMAKE_BUILD_TYPE MATCHES DEBUG AND USE_EFENCE)
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 AND USE_EFENCE)
128 IF(CMAKE_BUILD_TYPE MATCHES COVERAGE)
129         list (APPEND link_libraries -coverage)
130 ENDIF(CMAKE_BUILD_TYPE MATCHES COVERAGE)
131
132 # set default include directories
133 INCLUDE_DIRECTORIES(${EXTRA_INCLUDE_DIRS})
134
135 # Default Linkflag
136 set(PKG_TEMPLATE_PREFIX "\"${CMAKE_SOURCE_DIR}/${PROJECT_APP_TEMPLATES_DIR}\"" CACHE PATH "Default Package Templates Directory")
137 set(BARE_PKG_TEMPLATE_PREFIX "${CMAKE_SOURCE_DIR}/${PROJECT_APP_TEMPLATES_DIR}" CACHE PATH "Default Package Templates Directory")
138
139 if(NOT BINDINGS_LINK_FLAG)
140         set(BINDINGS_LINK_FLAG "-Wl,--version-script=${PKG_TEMPLATE_PREFIX}/cmake/export.map")
141 endif()