-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
302 lines (223 loc) · 9.88 KB
/
CMakeLists.txt
File metadata and controls
302 lines (223 loc) · 9.88 KB
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
# ------------------------------------------------------------
# This file is part of hcs-crypt of headcode.space
#
# The 'LICENSE.txt' file in the project root holds the software license.
# Copyright (C) 2020-2021 headcode.space e.U.
# Oliver Maurhart <info@headcode.space>, https://www.headcode.space
# ------------------------------------------------------------
# ------------------------------------------------------------
# tl;dr - How to create a package - copy-paste to a shell:
#
# 1. git clone --recurse-submodules https://gitlab.com/headcode.space/crypt.git
# 2. cd crypt
# 3. mkdir build
# 4. cd buld
# 5.1) cmake -D CMAKE_BUILD_TYPE=Release -D CPACK_GENERATOR=DEB .. # for DEB packages
# - xor -
# 5.2) cmake -D CMAKE_BUILD_TYPE=Release -D CPACK_GENERATOR=RPM .. # for RPM packages
# 6. CORES=`grep 'cpu cores' /proc/cpuinfo | awk '{ print $4 }' | head -n 1` make -j $CORES
# 7. make package
# ------------------------------------------------------------
project(hcs-crypt C CXX)
cmake_minimum_required(VERSION 3.13.4)
cmake_policy(SET CMP0075 NEW)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake;${HCS_CMAKE}")
if (CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE "Debug")
endif ()
message(STATUS "CMake build type: ${CMAKE_BUILD_TYPE}")
ENABLE_TESTING()
# ------------------------------------------------------------
# Project Version
set(VERSION_MAJOR "0")
set(VERSION_MINOR "0")
set(VERSION_PATCH "1")
set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
# ------------------------------------------------------------
# prime CMake includes
include(CheckIncludeFile)
include(CheckIncludeFileCXX)
include(CheckIncludeFiles)
include(CheckLibraryExists)
include(run-gcovr)
# ------------------------------------------------------------
# set global compiler flags
add_definitions(-DVERSION_MAJOR=${VERSION_MAJOR})
add_definitions(-DVERSION_MINOR=${VERSION_MINOR})
add_definitions(-DVERSION_PATCH=${VERSION_PATCH})
add_definitions(-DVERSION=\"${VERSION}\")
add_definitions(-D_GNU_SOURCE)
add_definitions(-D_DEFAULT_SOURCE)
# ---- fix C++ standard used ----
set(CMAKE_CXX_STANDARD 17)
if ((CMAKE_COMPILER_IS_GNUCC) OR (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang"))
set(COMPILER_WARNINGS "-Wall -Wextra -pedantic -pedantic-errors")
set(COMPILER_FLAGS_DEBUG "-O0 -g -ggdb3 -fno-builtin -fPIC")
set(COMPILER_FLAGS_RELEASE "-O3 -Werror -Werror -fno-builtin -fPIC")
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9)
set(LINKER_FLAGS "-pthread")
endif ()
set(LINKER_FLAGS "-rdynamic ${LINKER_FLAGS}")
endif ()
if (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
message(STATUS "Turning on compiler command export for CLang: ${CMAKE_BINARY_DIR}/compile_commands.json")
endif ()
# ---- callgrind integration ----
option(CALLGRIND_ENABLED "enable callgrind" off)
if (CALLGRIND_ENABLED)
message(STATUS "callgrind enabled")
message(WARNING "callgrind enabled: make package will create defective package!")
set(VALGRIND_NEEDED "1")
set(CALLGRIND_OPTIONS "")
else ()
message(STATUS "callgrind disabled")
endif ()
# ---- final compiler settings ----
set(CMAKE_C_FLAGS_DEBUG "${COMPILER_WARNINGS} ${COMPILER_FLAGS_DEBUG} ${COMPILER_FLAGS_PROFILING}")
set(CMAKE_CXX_FLAGS_DEBUG "${COMPILER_WARNINGS} ${COMPILER_FLAGS_DEBUG} ${COMPILER_FLAGS_PROFILING}")
set(CMAKE_C_FLAGS_RELEASE "${COMPILER_WARNINGS} ${COMPILER_FLAGS_RELEASE} ${COMPILER_FLAGS_PROFILING}")
set(CMAKE_CXX_FLAGS_RELEASE "${COMPILER_WARNINGS} ${COMPILER_FLAGS_RELEASE} ${COMPILER_FLAGS_PROFILING}")
set(CMAKE_EXE_LINKER_FLAGS "${LINKER_FLAGS} ${LINKER_FLAGS_PROFILING}")
# ------------------------------------------------------------
# libtommath
#
# Download https://github.com/libtom/libtommath as
#
# $ git submodule add https://github.com/libtom/libtommath 3rd/libtommath
if (NOT EXISTS "${CMAKE_SOURCE_DIR}/3rd/libtommath/makefile.unix")
message(FATAL_ERROR "The git submodule for libtommath has not been downloaded!")
endif ()
message(STATUS "Found libtommath")
execute_process(COMMAND make -f makefile.unix libtommath.a
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/3rd/libtommath"
)
include_directories(${CMAKE_SOURCE_DIR}/3rd/libtommath)
# ------------------------------------------------------------
# libtomcrypt... as default working horse
#
# Download https://github.com/libtom/libtomcrypt.git as
#
# $ git submodule add https://github.com/libtom/libtomcrypt.git 3rd/libtomcrypt
if (NOT EXISTS "${CMAKE_SOURCE_DIR}/3rd/libtomcrypt/makefile.unix")
message(FATAL_ERROR "The git submodule for libtomcrypt has not been downloaded!")
endif ()
message(STATUS "Found libtomcrypt")
execute_process(COMMAND make -f makefile.unix libtomcrypt.a
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/3rd/libtomcrypt"
)
include_directories(${CMAKE_SOURCE_DIR}/3rd/libtomcrypt/src/headers)
link_libraries(${CMAKE_SOURCE_DIR}/3rd/libtomcrypt/libtomcrypt.a)
# ------------------------------------------------------------
# OpenSSL as alternative provider: https://www.openssl.org/
#
option(WITH_OPENSSL "Add OpenSSL as algorithm provider." on)
if (WITH_OPENSSL)
message(STATUS "Building hcs-crypto with OpenSSL.")
find_package(OpenSSL REQUIRED COMPONENTS Crypto)
include_directories(${OPENSSL_INCLUDE_DIR})
link_libraries(${OPENSSL_CRYPTO_LIBRARY})
add_compile_definitions(OPENSSL)
endif ()
# ------------------------------------------------------------
# cxxopts
#
# Download https://github.com/jarro2783/cxxopts as
#
# $ git submodule add https://github.com/jarro2783/cxxopts 3rd/cxxopts
if (NOT EXISTS "${CMAKE_SOURCE_DIR}/3rd/cxxopts/CMakeLists.txt")
message(FATAL_ERROR "The git submodule for cxxopts has not been downloaded!")
endif ()
message(STATUS "Found cxxopts")
include_directories(${CMAKE_SOURCE_DIR}/3rd/cxxopts/include)
# ------------------------------------------------------------
# headcode/benchmark as submodule
include(hcs-benchmark)
message(STATUS "HCS_BENCHMARK_INCLUDE_DIR: ${HCS_BENCHMARK_INCLUDE_DIR}")
include_directories(${HCS_BENCHMARK_INCLUDE_DIR})
# ------------------------------------------------------------
# headcode/mem as submodule
include(hcs-mem)
message(STATUS "HCS_MEM_INCLUDE_DIR: ${HCS_MEM_INCLUDE_DIR}")
include_directories(${HCS_MEM_INCLUDE_DIR})
# ------------------------------------------------------------
# headcode/logger as submodule
include(hcs-logger)
message(STATUS "HCS_LOGGER_INCLUDE_DIR: ${HCS_LOGGER_INCLUDE_DIR}")
include_directories(${HCS_LOGGER_INCLUDE_DIR})
if ("${HCS_LOGGER_LIB}" STREQUAL "")
message(FATAL_ERROR "Could not build or locate hcs-logger library.")
endif ()
set(CMAKE_REQUIRED_LIBRARIES "${HCS_LOGGER_LIB};${CMAKE_REQUIRED_LIBRARIES}")
# ------------------------------------------------------------
# Doxygen
find_package(Doxygen)
if (DOXYGEN_FOUND STREQUAL "NO")
message(FATAL_ERROR "Doxygen not found. Please get a copy http://www.doxygen.org")
endif ()
configure_file(${CMAKE_SOURCE_DIR}/Doxyfile ${CMAKE_BINARY_DIR}/Doxyfile)
add_custom_target(doxygen ALL
${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
COMMENT "Doxygen running ...")
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES api-doc)
add_custom_target(doc)
add_dependencies(doc doxygen)
install(DIRECTORY ${CMAKE_BINARY_DIR}/html
DESTINATION share/doc/hcs-crypt-${VERSION_MAJOR}.${VERSION_MINOR}
COMPONENT documentation
)
# ------------------------------------------------------------
# Workload
add_subdirectory(src)
if (BUILD_TESTING)
message(STATUS "Building tests")
find_package(Python3 REQUIRED COMPONENTS Interpreter)
add_subdirectory(test)
else()
message(STATUS "Building without tests")
message(STATUS "To enable tests invoke cmake with \"-D BUILD_TESTING=on\" arguments.")
endif ()
# ------------------------------------------------------------
# Installing stuff
install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/headcode/crypt DESTINATION include/headcode COMPONENT header)
# ---- CPack global definitions ----
set(CPACK_PACKAGE_NAME "headcode-crypt")
set(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${VERSION_PATCH})
set(CPACK_PACKAGE_DESCRIPTION "Tiny and easy to use crypto library in C++.")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "This is a tiny and easy to use library to run crypto algroithms in C++.")
set(CPACK_PACKAGE_CONTACT "Oliver Maurhart <oliver.maurhart@headcode.space>")
set(CPACK_PACKAGE_VENDOR "headcode.space")
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}-${CMAKE_SYSTEM_PROCESSOR}")
set(CPACK_SOURCE_IGNORE_FILES "./build/*;/.git/;/.idea/;/.scannerwork/;/.sonar/;/sonar-project.properties")
set(CPACK_SOURCE_GENERATOR "TGZ")
if ("${CPACK_GENERATOR}" STREQUAL "DEB")
if (WITH_OPENSSL)
set(CPACK_DEBIAN_PACKAGE_DEPENDS "openssl")
else()
set(CPACK_DEBIAN_PACKAGE_DEPENDS "")
endif()
set(PACKAGE_DEB_CONTROL_DIR "tools/package/deb")
include(cmake/deb-package.cmake)
# only works inside a GitLab CI/CD runner
set(GITLAB_PACKAGE_BASE_URL "https://gitlab.com/api/v4/projects/22433969/packages/generic/hcs-crypt")
set(PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}.deb")
set(PACKAGE_FILE "${CMAKE_BINARY_DIR}/${PACKAGE_FILE_NAME}")
include(gitlab-package-push)
endif()
if ("${CPACK_GENERATOR}" STREQUAL "RPM")
if (WITH_OPENSSL)
set(CPACK_RPM_PACKAGE_REQUIRES "openssl")
else()
set(CPACK_RPM_PACKAGE_REQUIRES "")
endif()
set(PACKAGE_RPM_CONTROL_DIR "tools/package/rpm")
include(cmake/rpm-package.cmake)
# only works inside a GitLab CI/CD runner
set(GITLAB_PACKAGE_BASE_URL "https://gitlab.com/api/v4/projects/22433969/packages/generic/hcs-crypt")
set(PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}.rpm")
set(PACKAGE_FILE "${CMAKE_BINARY_DIR}/${PACKAGE_FILE_NAME}")
include(gitlab-package-push)
endif()
include(CPack)