-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
302 lines (252 loc) · 12.2 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
###############################################################################
# Copyright (C) 2018 Charly Lamothe #
# #
# This file is part of LibSecureStorage. #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
###############################################################################
# --- PROJECT ---
PROJECT(securestorage C)
cmake_minimum_required(VERSION 3.8)
# --- GLOBAL OPTIONS ---
set(CMAKE_VERBOSE_MAKEFILE on)
SET(ROOT_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE STRING "Default root path of the current build directory")
# --- DEPENDENCIES OPTIONS ---
SET(LIBEI_SYSTEM TRUE CACHE BOOL "Default LIBEI installation type")
set(LIBEI_INSTALL ${ROOT_BUILD_DIR}/libei/install CACHE STRING "Default LIBEI install path")
SET(LIBUEUM_SYSTEM TRUE CACHE BOOL "Default LIBUEUM installation type")
set(LIBUEUM_INSTALL ${ROOT_BUILD_DIR}/libueum/install CACHE STRING "Default LIBUEUM install path")
SET(LIBUECM_SYSTEM TRUE CACHE BOOL "Default LIBUECM installation type")
set(LIBUECM_INSTALL ${ROOT_BUILD_DIR}/libuecm/install CACHE STRING "Default LIBUECM install path")
SET(OPENSSL_SYSTEM TRUE CACHE BOOL "Default OPENSSL installation type")
SET(ZLIB_SYSTEM TRUE CACHE BOOL "Default ZLIB installation type")
# --- PATH ---
set(${CMAKE_CURRENT_SOURCE_DIR} ..)
set(SRC_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src")
set(TESTS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests")
set(EXAMPLES_PATH "${CMAKE_CURRENT_SOURCE_DIR}/examples")
if (CMAKE_BUILD_TYPE STREQUAL "Release")
set(LIBRARY_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/bin/release")
set(EXAMPLES_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/bin/release/examples")
set(TESTS_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/bin/release/tests")
elseif (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(LIBRARY_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/bin/debug")
set(EXAMPLES_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/bin/debug/examples")
set(TESTS_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/bin/debug/tests")
endif ()
# --- COMPILE OPTIONS ---
set(CMAKE_C_FLAGS "-fPIC")
# Compile options for Microsoft Visual Studio
if (MSVC)
# Add O2 optimization if build type is Release
if (CMAKE_BUILD_TYPE STREQUAL "Release")
add_compile_options("$<$<CONFIG:RELEASE>:/O2>")
endif ()
add_compile_options("/W4")
if (MSVC_VERSION GREATER_EQUAL 1900)
add_compile_options("/permissive-") # Add /permissive- (kind of -pedantic) available since Visual Studio 2017
endif ()
add_compile_options("/W4")
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) # This variable is used to initialize the property on each target as it is created
set(CMAKE_C_FLAGS "${CMAKE_ENABLE_C11}") # Compile with C11 rules
# Use secure functions by defaualt and suppress warnings about
# deprecated" functions
# TODO: try to remove this flags and fix the issue raised
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_NONSTDC_NO_WARNINGS=1 /D _CRT_SECURE_NO_WARNINGS=1")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") # If compiler is Clang
if (CMAKE_BUILD_TYPE STREQUAL "Release") # Release build mode
# Set all this flags explicitly, even if some of them may be covered by -Wall, in order to fine-tune the warning flags we want
# Note that -Weverything isn't available in some version of Mac and iOS
# Source: https://github.com/macmade/SeriousCode
add_compile_options("-Wall" "-Wbad-function-cast" "-Wcast-align" "-Wconversion" "-Wdeclaration-after-statement"
"-Wdeprecated-implementations" "-Wextra" "-Wfloat-equal" "-Wformat=2" "-Wformat-nonliteral" "-Wfour-char-constants"
"-Wimplicit-atomic-properties" "-Wmissing-braces" "-Wmissing-declarations" "-Wmissing-field-initializers"
"-Wmissing-format-attribute" "-Wmissing-noreturn" "-Wmissing-prototypes" "-Wnested-externs" "-Wnewline-eof"
"-Wold-style-definition" "-Woverlength-strings" "-Wparentheses" "-Wpointer-arith" "-Wredundant-decls"
"-Wreturn-type" "-Wsequence-point" "-Wshadow" "-Wshorten-64-to-32" "-Wsign-compare" "-Wsign-conversion"
"-Wstrict-prototypes" "-Wstrict-selector-match" "-Wswitch -Wswitch-default" "-Wswitch-enum" "-Wundeclared-selector"
"-Wuninitialized" "-Wunknown-pragmas" "-Wunreachable-code" "-Wunused-function" "-Wunused-label" "-Wunused-parameter"
"-Wunused-value" "-Wunused-variable" "-Wwrite-strings")
else ()
# Minimum flags for debug mode
add_compile_options("-Wall" "-Werror" "-Wextra" "-g")
endif ()
else ()
# Unix plateform
if (UNIX)
add_compile_options("-std=gnu11" "-pthread" "-ldl")
if (CMAKE_BUILD_TYPE STREQUAL "Release") # Release build mode
if (CMAKE_COMPILER_IS_GNUCC) # GNU compiler
add_compile_options("-O2") # O2 optimization
set(CMAKE_EXE_LINKER_FLAGS "-s") # Strip binary (only in release mode as it prevent debugging)
# Hardening options
## -fstack-protector-strong
### Stack smashing protector
## -D_FORTIFY_SOURCE=2
### Compile-time protection against static sized buffer overflows.
### No known regressions or performance loss
## -Wl,-z,now
### Disable lazy binding
## -Wl,-z,relro
### Read-only segments after relocation
add_compile_options("-D_FORTIFY_SOURCE=2" "-Wl,-z,now" "-Wl,-z,relro")
if (CMAKE_COMPILER_IS_GNUCC VERSION_GREATER 4.9)
add_compile_options("-fstack-protector-strong")
endif ()
endif ()
else () # Debug mode
add_compile_options("-g") # Add debug symbols
endif ()
endif ()
# It should work on any unix platform ?
if (CMAKE_BUILD_TYPE STREQUAL "Release") # Release mode
add_compile_options("$<$<CONFIG:RELEASE>:-O2>" "-fno-builtin")
endif ()
## -Werror=format-security
### Reject potentially unsafe format string arguments
## -Werror=implicit-function-declaration
### Reject missing function prototypes
#add_compile_options("-Wall" "-Wextra" "-Werror" "-pedantic" "-Werror=format-security" "-Werror=implicit-function-declaration")
add_compile_options("-Wall" "-Wextra" "-pedantic" "-Werror=format-security" "-Werror=implicit-function-declaration")
endif ()
if (WIN32)
macro(get_WIN32_WINNT version)
if (WIN32 AND CMAKE_SYSTEM_VERSION)
set(ver ${CMAKE_SYSTEM_VERSION})
string(REPLACE "." "" ver ${ver})
string(REGEX REPLACE "([0-9])" "0\\1" ver ${ver})
set(${version} "0x${ver}")
endif ()
endmacro()
get_WIN32_WINNT(ver)
add_definitions(-D_WIN32_WINNT=${ver})
endif ()
# CCache
find_program(CCACHE_PROGRAM ccache)
if (CCACHE_PROGRAM)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK "${CCACHE_PROGRAM}")
endif()
# --- SEARCH DEPENDENCIES ---
# External dependencies
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/external)
# Location where external projects will be downloaded
set (DOWNLOAD_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/downloads"
CACHE PATH "Location where external projects will be downloaded.")
mark_as_advanced(DOWNLOAD_LOCATION)
# LibErrorInterceptor
include(libei)
# LibUnknownEchoUtilsModule
include(libueum)
include(openssl)
# ZLib
include(zlib)
# LibUnknownEchoCryptoModule
include(libuecm)
# Search pthread
if (UNIX)
find_package (Threads)
endif ()
if (NOT ZLIB_SYSTEM)
list(APPEND LIBSECURESTORAGE_DEPENDENCIES zlib)
endif ()
if (NOT LIBEI_SYSTEM)
list(APPEND LIBSECURESTORAGE_DEPENDENCIES libei)
endif ()
if (NOT LIBUEUM_SYSTEM)
list(APPEND LIBSECURESTORAGE_DEPENDENCIES libueum)
endif ()
if (NOT LIBUECM_SYSTEM)
list(APPEND LIBSECURESTORAGE_DEPENDENCIES libuecm)
endif ()
if (NOT OPENSSL_SYSTEM)
list(APPEND LIBSECURESTORAGE_DEPENDENCIES openssl)
endif ()
set(LIBSECURESTORAGE_LIBRARIES
${LIBERRORINTERCEPTOR_LIBRARIES}
${LIBUNKNOWNECHOCRYPTOMODULE_LIBRARIES}
${LIBUNKNOWNECHOUTILSMODULE_LIBRARIES}
${OPENSSL_LIBRARIES}
${ZLIB_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
)
set(LIBSECURESTORAGE_INCLUDE_DIR
${ZLIB_INCLUDE_DIR}
${LIBERRORINTERCEPTOR_INCLUDE_DIR}
${LIBUNKNOWNECHOUTILSMODULE_INCLUDE_DIR}
${OPENSSL_INCLUDE_DIR}
${LIBUNKNOWNECHOCRYPTOMODULE_INCLUDE_DIR}
)
# --- BUILD SSTORAGE ---
set(PROJECT_NAME_STATIC "${PROJECT_NAME}_static")
set(PROJECT_NAME_SHARED "${PROJECT_NAME}_shared")
# Load source files
file(GLOB_RECURSE PROGRAM_TARGET_SRC_FILES "${SRC_PATH}/*.*")
# Build shared library
add_library(${PROJECT_NAME_SHARED} SHARED ${PROGRAM_TARGET_SRC_FILES})
if (LIBSECURESTORAGE_DEPENDENCIES)
add_dependencies(${PROJECT_NAME_SHARED} ${LIBSECURESTORAGE_DEPENDENCIES})
endif ()
target_include_directories(${PROJECT_NAME_SHARED} PUBLIC "${SRC_PATH}")
target_include_directories(${PROJECT_NAME_SHARED} PUBLIC ${LIBSECURESTORAGE_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME_SHARED} ${LIBSECURESTORAGE_LIBRARIES})
set_target_properties(${PROJECT_NAME_SHARED} PROPERTIES OUTPUT_NAME sstorage)
# Build static library
add_library(${PROJECT_NAME_STATIC} STATIC ${PROGRAM_TARGET_SRC_FILES})
if (LIBSECURESTORAGE_DEPENDENCIES)
add_dependencies(${PROJECT_NAME_STATIC} ${LIBSECURESTORAGE_DEPENDENCIES})
endif ()
target_include_directories(${PROJECT_NAME_STATIC} PUBLIC "${SRC_PATH}")
target_include_directories(${PROJECT_NAME_STATIC} PUBLIC ${LIBSECURESTORAGE_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME_STATIC} ${LIBSECURESTORAGE_LIBRARIES})
set_target_properties(${PROJECT_NAME_STATIC} PROPERTIES OUTPUT_NAME sstorage_static)
# --- INSTALL ---
# Copy includes
install(DIRECTORY ${CMAKE_SOURCE_DIR}/src/ DESTINATION ${CMAKE_INSTALL_PREFIX}/include FILES_MATCHING PATTERN "*.h")
# Copy static library
install (
TARGETS ${PROJECT_NAME_STATIC}
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
)
if (UNIX)
install (
TARGETS ${PROJECT_NAME_SHARED}
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
)
elseif (WIN32)
install (
TARGETS ${PROJECT_NAME_SHARED}
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
)
endif ()
# Build examples
file(GLOB files "${EXAMPLES_PATH}/*.c")
foreach (filePath ${files} )
get_filename_component(executableName ${filePath} NAME_WE)
message(STATUS "Build Program : ${executableName}")
add_executable(${executableName} ${filePath})
if (LIBSECURESTORAGE_DEPENDENCIES)
add_dependencies(${executableName} ${LIBSECURESTORAGE_DEPENDENCIES})
endif ()
target_link_libraries(${executableName} ${PROJECT_NAME_STATIC})
target_include_directories(${executableName} PUBLIC ${LIBSECURESTORAGE_INCLUDE_DIR})
target_link_libraries(${executableName} ${LIBSECURESTORAGE_LIBRARIES})
if (UNIX)
target_link_libraries(${executableName} "-ldl")
endif ()
set_target_properties(${executableName} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${EXAMPLES_OUTPUT_PATH})
endforeach ()