-
Notifications
You must be signed in to change notification settings - Fork 163
/
CMakeLists.txt
322 lines (268 loc) · 10.3 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#
# MetaCall Library by Parra Studios
# A library for providing a foreing function interface calls.
#
# Copyright (C) 2016 - 2024 Vicente Eduardo Ferrer Garcia <[email protected]>
#
# 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.
#
#
# CMake options
#
# CMake version
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
# Include cmake modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(WriterCompilerDetectionHeaderFound NOTFOUND)
include(ExternalProject)
include(GetGitRevisionDescription)
include(Custom)
# Set policies
set_policy(CMP0028 NEW) # ENABLE CMP0028: Double colon in target name means ALIAS or IMPORTED target.
set_policy(CMP0054 NEW) # ENABLE CMP0054: Only interpret if() arguments as variables or keywords when unquoted.
set_policy(CMP0042 NEW) # ENABLE CMP0042: MACOSX_RPATH is enabled by default.
set_policy(CMP0063 NEW) # ENABLE CMP0063: Honor visibility properties for all target types.
set_policy(CMP0135 NEW) # ENABLE CMP0135: Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24.
#
# Project description and (meta) information
#
# Get git revision
get_git_head_revision(GIT_REFSPEC GIT_SHA1)
string(SUBSTRING "${GIT_SHA1}" 0 12 GIT_REV)
# Meta information about the project
set(META_PROJECT_NAME "MetaCall")
set(META_PROJECT_DESCRIPTION "A library for providing inter-language foreign function interface calls")
set(META_AUTHOR_ORGANIZATION "MetaCall Inc.")
set(META_AUTHOR_DOMAIN "https://metacall.io/")
set(META_AUTHOR_MAINTAINER "[email protected]")
# Parse version
file(READ VERSION META_VERSION)
string(REPLACE "\n" "" META_VERSION ${META_VERSION})
string(REPLACE "." ";" META_VERSION_LIST ${META_VERSION})
list(GET META_VERSION_LIST 0 META_VERSION_MAJOR)
list(GET META_VERSION_LIST 1 META_VERSION_MINOR)
list(GET META_VERSION_LIST 2 META_VERSION_PATCH)
set(META_VERSION_REVISION "${GIT_REV}")
set(META_NAME_VERSION "${META_PROJECT_NAME} v${META_VERSION} (${META_VERSION_REVISION})")
#
# Project configuration options
#
# Project options
option(BUILD_SHARED_LIBS "Build shared instead of static libraries." ON) # TODO: Static libraries build is not implemented yet
option(OPTION_SELF_CONTAINED "Create a self-contained install with all dependencies." OFF) # TODO: Not implemented
option(OPTION_BUILD_TESTS "Build tests." ON)
option(OPTION_BUILD_BENCHMARKS "Build benchmarks." OFF)
option(OPTION_BUILD_DOCS "Build documentation." OFF)
option(OPTION_BUILD_EXAMPLES "Build examples." ON)
option(OPTION_BUILD_CLI "Build CLIs." ON)
option(OPTION_BUILD_LOADERS "Build loaders." ON)
option(OPTION_BUILD_EXTENSIONS "Build extensions." ON)
option(OPTION_BUILD_SCRIPTS "Build scripts." ON)
option(OPTION_BUILD_SERIALS "Build serials." ON)
option(OPTION_BUILD_DETOURS "Build detours." ON)
option(OPTION_BUILD_PORTS "Build ports." OFF)
option(OPTION_BUILD_LOG_PRETTY "Build logs in a human readable format." ON)
option(OPTION_BUILD_PIC "Build with position independent code." ON)
option(OPTION_BUILD_SECURITY "Build with stack-smashing protection and source fortify." ON)
option(OPTION_BUILD_GUIX "Disable all build system unreproductible operations." OFF)
option(OPTION_GIT_HOOKS "Disable git hooks when running in CI/CD." ON)
option(OPTION_FORK_SAFE "Enable fork safety." ON)
option(OPTION_THREAD_SAFE "Enable thread safety." OFF)
option(OPTION_COVERAGE "Enable coverage." OFF)
option(OPTION_MEMORY_TRACKER "Enable memory tracking for reflect data." ON)
# Build type
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Define the type of build." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "RelWithDebInfo" "MinSizeRel")
endif()
# Binary directory
if ("${CMAKE_GENERATOR}" MATCHES "^(Visual Studio)")
set(PROJECT_OUTPUT_DIR "${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}")
else()
set(PROJECT_OUTPUT_DIR "${CMAKE_BINARY_DIR}")
endif()
#
# Declare project
#
# Generate folders for IDE targets (e.g., VisualStudio solutions)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(IDE_FOLDER "")
# Declare project
project(${META_PROJECT_NAME} C CXX)
# Set output directories
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
# Create version file
file(WRITE "${PROJECT_BINARY_DIR}/VERSION" "${META_NAME_VERSION}")
#
# Define Export Headers
#
include(GenerateExportHeader)
# Patch export headers in order to produce "${target_upper}_EXPORTS" in API definition
function(GENERATE_EXPORT_HEADER)
# This function is patched again in Portability, due to Haiku workaround, review it if this func gets removed
set(target ${ARGV0})
string(TOUPPER ${target} target_upper)
set_target_properties(${target}
PROPERTIES DEFINE_SYMBOL "${target_upper}_EXPORTS"
)
# When the function is redefined, the old function can be accessed through underscore
_GENERATE_EXPORT_HEADER(${ARGN})
endfunction()
#
# Portability
#
include(Portability)
#
# clang-format and tidy
#
find_package(ClangFormat)
if(ClangFormat_FOUND)
include(ClangDevTools)
else()
message(WARNING "Linting disabled: clang-format executable not found")
endif()
#
# Compiler settings and options
#
include(CompileOptions)
#
# Coverage
#
if(OPTION_COVERAGE)
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
message(WARNING "OPTION_COVERAGE requires to be build with -DCMAKE_BUILD_TYPE=Debug, skipping coverage")
set(OPTION_COVERAGE OFF)
else()
include(Coverage)
endif()
endif()
#
# Deployment/installation setup
#
# Get project name
string(TOLOWER ${META_PROJECT_NAME} project)
# Check for system dir install
set(SYSTEM_DIR_INSTALL FALSE)
if("${CMAKE_INSTALL_PREFIX}" STREQUAL "/usr" OR "${CMAKE_INSTALL_PREFIX}" STREQUAL "/usr/local")
set(SYSTEM_DIR_INSTALL TRUE)
endif()
# Installation paths
if(UNIX AND SYSTEM_DIR_INSTALL OR OPTION_BUILD_GUIX)
# Install into the system (/usr/bin or /usr/local/bin)
set(INSTALL_ROOT "share/${project}") # /usr/[local]/share/<project>
set(INSTALL_CMAKE "share/${project}/cmake") # /usr/[local]/share/<project>/cmake
set(INSTALL_EXAMPLES "share/${project}") # /usr/[local]/share/<project>
set(INSTALL_DATA "share/${project}") # /usr/[local]/share/<project>
set(INSTALL_BIN "bin") # /usr/[local]/bin
set(INSTALL_SHARED "lib") # /usr/[local]/lib
set(INSTALL_LIB "lib") # /usr/[local]/lib
set(INSTALL_INCLUDE "include") # /usr/[local]/include
set(INSTALL_DOC "share/doc/${project}") # /usr/[local]/share/doc/<project>
set(INSTALL_SHORTCUTS "share/applications") # /usr/[local]/share/applications
set(INSTALL_ICONS "share/pixmaps") # /usr/[local]/share/pixmaps
set(INSTALL_INIT "/etc/init") # /etc/init (upstart init scripts)
else()
# Install into local directory
set(INSTALL_ROOT ".") # ./
set(INSTALL_CMAKE "cmake") # ./cmake
set(INSTALL_EXAMPLES ".") # ./
set(INSTALL_DATA ".") # ./
set(INSTALL_BIN ".") # ./
set(INSTALL_SHARED "lib") # ./lib
set(INSTALL_LIB "lib") # ./lib
set(INSTALL_INCLUDE "include") # ./include
set(INSTALL_DOC "doc") # ./doc
set(INSTALL_SHORTCUTS "misc") # ./misc
set(INSTALL_ICONS "misc") # ./misc
set(INSTALL_INIT "misc") # ./misc
endif()
# Set runtime path
set(CMAKE_SKIP_BUILD_RPATH FALSE) # Add absolute path to all dependencies for BUILD
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) # Use CMAKE_INSTALL_RPATH for INSTALL
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) # Add path to dependencies for INSTALL
if(SYSTEM_DIR_INSTALL)
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${INSTALL_LIB}")
else()
# Find libraries relative to binary
if(APPLE)
set(CMAKE_INSTALL_RPATH "@loader_path/${INSTALL_LIB}")
else()
set(CMAKE_INSTALL_RPATH "$ORIGIN/${INSTALL_LIB}")
endif()
endif()
#
# CTest configuration
#
if(OPTION_BUILD_TESTS OR OPTION_BUILD_BENCHMARKS)
enable_testing()
endif()
#
# Project module includes
#
add_subdirectory(source)
add_subdirectory(docs)
add_subdirectory(deploy)
#
# Project configuration generation
#
set(EXPORT_MODULE_NAMES
version
preprocessor
environment
format
threading
log
memory
portability
adt
filesystem
reflect
dynlink
detour
serial
configuration
loader
metacall
)
set(PROJECT_CONFIGURATION "metacall-config.cmake")
set(PROJECT_CONFIGURATION_VERSION "metacall-config-version.cmake")
set(PROJECT_CONFIGURATION_PATH "${PROJECT_BINARY_DIR}/${PROJECT_CONFIGURATION}")
set(PROJECT_CONFIGURATION_VERSION_PATH "${PROJECT_BINARY_DIR}/${PROJECT_CONFIGURATION_VERSION}")
configure_file("${CMAKE_SOURCE_DIR}/${PROJECT_CONFIGURATION}.in" "${PROJECT_CONFIGURATION_PATH}" @ONLY)
configure_file("${CMAKE_SOURCE_DIR}/${PROJECT_CONFIGURATION_VERSION}.in" "${PROJECT_CONFIGURATION_VERSION_PATH}" @ONLY)
#
# Deployment (global project files)
#
# Install version file
install(FILES "${PROJECT_BINARY_DIR}/VERSION" DESTINATION ${INSTALL_ROOT} COMPONENT runtime)
# Install cmake find script for the project
install(FILES ${PROJECT_CONFIGURATION_PATH} DESTINATION ${INSTALL_ROOT} COMPONENT dev)
install(FILES ${PROJECT_CONFIGURATION_VERSION_PATH} DESTINATION ${INSTALL_ROOT} COMPONENT dev)
# Install the project meta files
install(FILES AUTHORS DESTINATION ${INSTALL_ROOT} COMPONENT runtime)
install(FILES LICENSE DESTINATION ${INSTALL_ROOT} COMPONENT runtime)
install(FILES README.md DESTINATION ${INSTALL_ROOT} COMPONENT runtime)
# # Install runtime data
# install(DIRECTORY ${PROJECT_SOURCE_DIR}/data DESTINATION ${INSTALL_DATA} COMPONENT runtime)
#
# Install githooks directory
#
if(OPTION_GIT_HOOKS)
message(STATUS "Installing git hooks at ${CMAKE_CURRENT_SOURCE_DIR}")
execute_process(
COMMAND git config --local core.hooksPath githooks/${PROJECT_OS_FAMILY}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endif()