Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
434 lines (352 loc) · 15.1 KB
/
Copy pathCMakeLists.txt
File metadata and controls
434 lines (352 loc) · 15.1 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
cmake_minimum_required(VERSION 3.15)
# The version is declared only in pyproject.toml, which also gives it to the wheel
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/pyproject.toml" MONERO_PYTHON_PYPROJECT)
string(REGEX MATCH "(^|\n)version = \"([0-9]+\\.[0-9]+\\.[0-9]+)\"" MONERO_PYTHON_VERSION_MATCH "${MONERO_PYTHON_PYPROJECT}")
if(NOT MONERO_PYTHON_VERSION_MATCH)
message(FATAL_ERROR "Could not read the version from pyproject.toml")
endif()
set(MONERO_PYTHON_VERSION "${CMAKE_MATCH_2}")
# configure again when the version changes
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/pyproject.toml")
project(monero VERSION ${MONERO_PYTHON_VERSION} LANGUAGES CXX)
message(STATUS "monero-python version ${PROJECT_VERSION}")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if (WIN32)
add_definitions( "-D_GLIBCXX_USE_NANOSLEEP=1" ) # "'sleep_for' is not a member of 'std::this_thread'" in gcc 4.7/4.8
add_definitions( "-DWIN32_LEAN_AND_MEAN" )
endif()
option(USE_CCACHE "Use ccache if found" ON)
# Respect a launcher given explicitly (-DCMAKE_CXX_COMPILER_LAUNCHER or env).
if(USE_CCACHE AND NOT CMAKE_CXX_COMPILER_LAUNCHER)
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
message(STATUS "Using ccache: ${CCACHE_PROGRAM}")
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
endif()
endif()
option(ENABLE_COVERAGE "Enable coverage" OFF)
# A build without a type is not optimized (no -O3, and asserts on): `cmake ..` is what the README and bin/build_libmonero_python.sh run,
# while the wheels and the packages are built as Release. Coverage has its own flags below, and a debug build is asked with a type
get_property(MONERO_PYTHON_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(NOT MONERO_PYTHON_MULTI_CONFIG AND NOT CMAKE_BUILD_TYPE AND NOT ENABLE_COVERAGE)
message(STATUS "No CMAKE_BUILD_TYPE given, using Release")
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()
if(ENABLE_COVERAGE)
message(STATUS "Coverage enabled")
add_compile_options(--coverage -O0 -g -fprofile-update=atomic)
add_link_options(--coverage)
endif()
# The flags of the hardening for the sources of the module. The libraries it links get theirs from
# bin/hardening_flags.sh, and bin/check_elf_hardening.sh, bin/check_pe_hardening.sh and
# bin/check_macho_hardening.sh look for the result in the binaries. A flag that the compiler does not take is left out.
# The clang of Apple has the stack protector on, and a python module has no use of the other flags there.
include(CheckCXXCompilerFlag)
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
foreach(flag IN ITEMS -fstack-protector-strong -fstack-clash-protection)
string(MAKE_C_IDENTIFIER "${flag}" flag_id)
check_cxx_compiler_flag("${flag}" "MONERO_PYTHON_CXX_${flag_id}")
if (MONERO_PYTHON_CXX_${flag_id})
add_compile_options("${flag}")
endif()
endforeach()
add_compile_options("-D_GLIBCXX_ASSERTIONS")
add_compile_definitions("$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>,$<CONFIG:MinSizeRel>>:_FORTIFY_SOURCE=2>")
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64|amd64)$")
check_cxx_compiler_flag(-fcf-protection=full MONERO_PYTHON_CXX_FCF_PROTECTION)
if (MONERO_PYTHON_CXX_FCF_PROTECTION)
add_compile_options(-fcf-protection=full)
endif()
endif()
elseif (MINGW)
# the gcc of MSYS2 does not turn the stack protector on, and the runtime of mingw-w64 has __stack_chk_fail and the guard
check_cxx_compiler_flag(-fstack-protector-strong MONERO_PYTHON_CXX_FSTACK_PROTECTOR_STRONG)
if (MONERO_PYTHON_CXX_FSTACK_PROTECTOR_STRONG)
add_compile_options(-fstack-protector-strong)
endif()
endif()
###################
# Monero C++
###################
set(MONERO_CPP "${CMAKE_SOURCE_DIR}/external/monero-cpp" CACHE STRING "Monero c++ source directory")
set(MONERO_CPP_SRC "${MONERO_CPP}/src")
set(MONERO_CPP_BUILD "${MONERO_CPP}/build")
add_library(monero-cpp STATIC IMPORTED)
set_target_properties(monero-cpp PROPERTIES IMPORTED_LOCATION
${MONERO_CPP_BUILD}/libmonero-cpp.a)
###################
# Monero
###################
set(MONERO_PROJECT "${MONERO_CPP}/external/monero-project" CACHE STRING "Monero project source directory")
set(MONERO_PROJECT_SRC "${MONERO_PROJECT}/src")
set(MONERO_PROJECT_BUILD "${MONERO_PROJECT}/build/release" CACHE STRING "Monero project build directory")
list(APPEND CMAKE_MODULE_PATH "${MONERO_PROJECT}/cmake")
add_library(wallet STATIC IMPORTED)
set_target_properties(wallet PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/lib/libwallet.a)
# libwallet-crypto.a provides x86_64 asm for some wallet functions
if (EXISTS ${MONERO_PROJECT_BUILD}/src/crypto/wallet/libwallet-crypto.a)
add_library(wallet_crypto_lib STATIC IMPORTED)
set_target_properties(wallet_crypto_lib PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/crypto/wallet/libwallet-crypto.a)
set(wallet_crypto wallet_crypto_lib)
endif()
add_library(lmdb STATIC IMPORTED)
set_target_properties(lmdb PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/external/db_drivers/liblmdb/liblmdb.a)
add_library(epee STATIC IMPORTED)
set_target_properties(epee PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/contrib/epee/src/libepee.a)
add_library(rpc_base STATIC IMPORTED)
set_target_properties(rpc_base PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/rpc/librpc_base.a)
add_library(net STATIC IMPORTED)
set_target_properties(net PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/net/libnet.a)
add_library(hardforks STATIC IMPORTED)
set_target_properties(hardforks PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/hardforks/libhardforks.a)
add_library(easylogging STATIC IMPORTED)
set_target_properties(easylogging PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/external/easylogging++/libeasylogging.a)
add_library(cryptonote_core STATIC IMPORTED)
set_target_properties(cryptonote_core PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/cryptonote_core/libcryptonote_core.a)
add_library(cryptonote_protocol STATIC IMPORTED)
set_target_properties(cryptonote_protocol PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/cryptonote_protocol/libcryptonote_protocol.a)
add_library(cryptonote_basic STATIC IMPORTED)
set_target_properties(cryptonote_basic PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/cryptonote_basic/libcryptonote_basic.a)
add_library(cryptonote_format_utils_basic STATIC IMPORTED)
set_target_properties(cryptonote_format_utils_basic PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/cryptonote_basic/libcryptonote_format_utils_basic.a)
add_library(mnemonics STATIC IMPORTED)
set_target_properties(mnemonics PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/mnemonics/libmnemonics.a)
add_library(common STATIC IMPORTED)
set_target_properties(common PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/common/libcommon.a)
add_library(cncrypto STATIC IMPORTED)
set_target_properties(cncrypto PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/crypto/libcncrypto.a)
add_library(ringct STATIC IMPORTED)
set_target_properties(ringct PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/ringct/libringct.a)
add_library(ringct_basic STATIC IMPORTED)
set_target_properties(ringct_basic PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/ringct/libringct_basic.a)
add_library(blockchain_db STATIC IMPORTED)
set_target_properties(blockchain_db PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/blockchain_db/libblockchain_db.a)
add_library(blocks STATIC IMPORTED)
set_target_properties(blocks PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/blocks/libblocks.a)
add_library(checkpoints STATIC IMPORTED)
set_target_properties(checkpoints PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/checkpoints/libcheckpoints.a)
add_library(device STATIC IMPORTED)
set_target_properties(device PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/device/libdevice.a)
add_library(device_trezor STATIC IMPORTED)
set_target_properties(device_trezor PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/device_trezor/libdevice_trezor.a)
add_library(multisig STATIC IMPORTED)
set_target_properties(multisig PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/multisig/libmultisig.a)
add_library(version STATIC IMPORTED)
set_target_properties(version PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/libversion.a)
add_library(randomx STATIC IMPORTED)
set_target_properties(randomx PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/external/randomx/librandomx.a)
############
# Threads
############
find_package(Threads REQUIRED)
############
# Python3
############
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
############
# Pybind11
############
set(PYBIND11_FINDPYTHON ON)
find_package(pybind11 REQUIRED)
############
# Boost
############
# The CMake config that Boost installs since 1.70 (BoostConfig.cmake), which gives imported targets that bring their headers. An older
# Boost has none: the FindBoost module of CMake finds it, which newer CMake versions deprecate (CMP0167), and only there.
set(Boost_USE_MULTITHREADED ON)
set(BOOST_COMPONENTS thread chrono atomic serialization filesystem program_options)
# Find Boost headers
set(BOOST_MIN_VER 1.62)
find_package(Boost ${BOOST_MIN_VER} CONFIG QUIET)
if (Boost_FOUND)
set(BOOST_FIND_MODE CONFIG)
else()
if (POLICY CMP0167)
cmake_policy(SET CMP0167 OLD)
endif()
set(BOOST_FIND_MODE MODULE)
set(Boost_NO_BOOST_CMAKE ON)
find_package(Boost ${BOOST_MIN_VER} MODULE QUIET REQUIRED)
endif()
# Boost System is header-only since 1.69
if (Boost_VERSION_STRING VERSION_LESS 1.69.0)
list(APPEND BOOST_COMPONENTS system)
endif()
# Boost Regex is header-only since 1.84. monero-project uses it, and before that it is a library that the module has to link
if (Boost_VERSION_STRING VERSION_LESS 1.84.0)
list(APPEND BOOST_COMPONENTS regex)
endif()
find_package(Boost ${BOOST_MIN_VER} ${BOOST_FIND_MODE} QUIET REQUIRED COMPONENTS ${BOOST_COMPONENTS})
message(STATUS "Boost ${Boost_VERSION_STRING} (${BOOST_FIND_MODE}) components: ${BOOST_COMPONENTS}")
message(STATUS "Using Boost include dirs at ${Boost_INCLUDE_DIRS}")
############
# OpenSSL
############
find_package(OpenSSL REQUIRED)
message(STATUS "Using OpenSSL include dir at ${OPENSSL_INCLUDE_DIR}")
if (WIN32)
list(APPEND OPENSSL_LIBRARIES ws2_32 crypt32 bcrypt)
endif()
############
# Unbound
############
find_package(Unbound REQUIRED)
message(STATUS "Using libunbound library at ${UNBOUND_LIBRARIES}")
if (APPLE)
find_library(EXPAT_LIBRARY expat HINTS /opt/homebrew/opt/expat/lib /usr/local/opt/expat/lib)
if (EXPAT_LIBRARY)
list(APPEND UNBOUND_LIBRARIES ${EXPAT_LIBRARY})
endif()
find_library(EVENT_LIBRARY NAMES event event_core HINTS /opt/homebrew/opt/libevent/lib /usr/local/opt/libevent/lib)
if (EVENT_LIBRARY)
list(APPEND UNBOUND_LIBRARIES ${EVENT_LIBRARY})
endif()
find_library(NGHTTP2_LIBRARY nghttp2 HINTS /opt/homebrew/opt/libnghttp2/lib /usr/local/opt/libnghttp2/lib)
if (NGHTTP2_LIBRARY)
list(APPEND UNBOUND_LIBRARIES ${NGHTTP2_LIBRARY})
endif()
endif()
############
# LibUSB
############
find_library(usb_LIBRARY NAMES usb-1.0 libusb usb)
set(LibUSB_LIBRARIES ${usb_LIBRARY})
############
# HIDAPI
############
find_package(HIDAPI REQUIRED)
message(STATUS "Using HIDAPI include dir at ${HIDAPI_INCLUDE_DIR}")
############
# libsodium
############
find_library(SODIUM_LIBRARY sodium REQUIRED)
message(STATUS "Using libsodium library at ${SODIUM_LIBRARY}")
############
# Protobuf
############
if (NOT APPLE)
include(FindProtobuf)
find_package(Protobuf)
message(STATUS "Protobuf lib: ${Protobuf_LIBRARY}, inc: ${Protobuf_INCLUDE_DIR}, protoc: ${Protobuf_PROTOC_EXECUTABLE}")
endif()
########################
# Build python library
########################
set(LIBRARY_SRC_FILES
src/cpp/common/py_monero_common.cpp
src/cpp/common/py_monero_common_bindings.cpp
src/cpp/daemon/py_monero_daemon_bindings.cpp
src/cpp/wallet/py_monero_wallet_bindings.cpp
src/cpp/utils/py_monero_utils.cpp
src/cpp/utils/py_monero_utils_bindings.cpp
src/cpp/py_monero.cpp
)
# only this file reports the version, so a new version does not rebuild the others
set_source_files_properties(src/cpp/utils/py_monero_utils.cpp PROPERTIES
COMPILE_DEFINITIONS "MONERO_PYTHON_VERSION=\"${PROJECT_VERSION}\"")
pybind11_add_module(monero ${LIBRARY_SRC_FILES})
if (WIN32)
target_compile_options(monero PRIVATE -Wa,-mbig-obj)
endif()
target_include_directories(monero PRIVATE
src/cpp
"${MONERO_CPP_SRC}/"
${pybind11_INCLUDE_DIR}
${OPENSSL_INCLUDE_DIR}
${HIDAPI_INCLUDE_DIR}
${Protobuf_INCLUDE_DIR}
${UNBOUND_INCLUDE_DIR}
)
# the headers of monero-project as the ones of the system: the warnings of its code, deadline_timer that newer boost versions deprecate
# for one, are not the ones of the module. The compiler looks in them after the directories above
target_include_directories(monero SYSTEM PRIVATE
"${MONERO_PROJECT_SRC}/"
"${MONERO_PROJECT}/contrib/epee/include"
"${MONERO_PROJECT}/external"
"${MONERO_PROJECT}/external/easylogging++"
"${MONERO_PROJECT}/external/rapidjson/include"
)
set(MONERO_PYTHON_LINK_LIBS
monero-cpp
wallet
rpc_base
net
lmdb
easylogging
cryptonote_core
cryptonote_protocol
cryptonote_basic
cryptonote_format_utils_basic
mnemonics
ringct
ringct_basic
common
blockchain_db
blocks
checkpoints
device
device_trezor
multisig
version
cncrypto
randomx
epee
hardforks
${wallet_crypto}
Threads::Threads
Boost::headers
${Boost_LIBRARIES}
${OPENSSL_LIBRARIES}
${Protobuf_LIBRARY}
${SODIUM_LIBRARY}
# after hidapi, which uses it: with --as-needed the linker keeps a shared library only for what comes before it
${HIDAPI_LIBRARIES}
${LibUSB_LIBRARIES}
${UNBOUND_LIBRARIES}
)
target_link_libraries(monero PRIVATE ${MONERO_PYTHON_LINK_LIBS})
# A python module has to export PyInit_monero and nothing else. Left to itself the linker also exports every
# symbol of the static libraries it takes in, those of monero-project and monero-cpp: about 27000 on linux, from
# 7000 to 11000 on macOS and 1100 on windows. They make the module bigger and can clash with the symbols that
# another extension of the same process brings, a boost or a protobuf for one.
if(APPLE)
target_link_options(monero PRIVATE -Wl,-dead_strip -Wl,-exported_symbol,_PyInit_monero)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_link_options(monero PRIVATE -Wl,--exclude-libs,ALL -Wl,--gc-sections)
endif()
# full RELRO, and a stack that is not executable: a library of the static ones that has no .note.GNU-stack makes the linker ask for an executable
# stack for the whole module, which the deb of Ubuntu 24.04 had
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_options(monero PRIVATE -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack)
endif()
install(TARGETS monero DESTINATION .)
# the stubs, as the package monero next to the module: python imports the module, a folder with no __init__.py does not come first,
# and the type checkers read the stubs (mypy with py.typed)
install(DIRECTORY src/monero DESTINATION .)