Debian Codegraph @

CMakeLists.txt

raw ◀ ▶
1 Debian patch touch this file
1 # Copyright (c) 2014-2022, The Monero Project
2 #
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without modification, are
6 # permitted provided that the following conditions are met:
7 #
8 # 1. Redistributions of source code must retain the above copyright notice, this list of
9 # conditions and the following disclaimer.
10 #
11 # 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 # of conditions and the following disclaimer in the documentation and/or other
13 # materials provided with the distribution.
14 #
15 # 3. Neither the name of the copyright holder nor the names of its contributors may be
16 # used to endorse or promote products derived from this software without specific
17 # prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 # THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #
29 # Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
32
33 list(INSERT CMAKE_MODULE_PATH 0
34 "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
35 include(CheckCCompilerFlag)
36 include(CheckCXXCompilerFlag)
37 include(CheckLinkerFlag)
38 include(CheckLibraryExists)
39 include(CheckFunctionExists)
40 include(FindPythonInterp)
41
42 if (IOS)
43 INCLUDE(CmakeLists_IOS.txt)
44 endif()
45
46 cmake_minimum_required(VERSION 3.5)
47 message(STATUS "CMake version ${CMAKE_VERSION}")
48
49 project(monero)
50
51 option (USE_CCACHE "Use ccache if a usable instance is found" ON)
52 if (USE_CCACHE)
53 include(FindCcache) # Has to be included after the project() macro, to be able to read the CXX variable.
54 else()
55 message(STATUS "ccache deselected")
56 endif()
57 option (USE_COMPILATION_TIME_PROFILER "Use compilation time profiler (for CLang >= 9 only)" OFF)
58 if (USE_COMPILATION_TIME_PROFILER)
59 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
60 message(FATAL_ERROR "The flag USE_COMPILATION_TIME_PROFILER is meant to be set only for CLang compiler!")
61 endif()
62 add_compile_options("-ftime-trace")
63 endif()
64
65 if (${CMAKE_VERSION} VERSION_GREATER "3.0.0" AND CMAKE_MAKE_PROGRAM MATCHES "ninja")
66 set(MONERO_PARALLEL_COMPILE_JOBS "" CACHE STRING "The maximum number of concurrent compilation jobs.")
67 if (MONERO_PARALLEL_COMPILE_JOBS)
68 set_property(GLOBAL APPEND PROPERTY JOB_POOLS compile_job_pool=${MONERO_PARALLEL_COMPILE_JOBS})
69 set(CMAKE_JOB_POOL_COMPILE compile_job_pool)
70 endif ()
71
72 set(MONERO_PARALLEL_LINK_JOBS "" CACHE STRING "The maximum number of concurrent link jobs.")
73 if (MONERO_PARALLEL_LINK_JOBS)
74 set_property(GLOBAL APPEND PROPERTY JOB_POOLS link_job_pool=${MONERO_PARALLEL_LINK_JOBS})
75 set(CMAKE_JOB_POOL_LINK link_job_pool)
76 endif ()
77 endif ()
78
79 option (USE_CLANG_TIDY_C "Lint the code with clang-tidy - variant C" OFF)
80 option (USE_CLANG_TIDY_CXX "Lint the code with clang-tidy - variant C++" OFF)
81 if (USE_CLANG_TIDY_C AND USE_CLANG_TIDY_CXX)
82 message(FATAL_ERROR "Enabling both USE_CLANG_TIDY_C and USE_CLANG_TIDY_CXX simultaneously crashes clang-tidy.")
83 endif()
84 if (USE_CLANG_TIDY_C OR USE_CLANG_TIDY_CXX)
85 include(SetClangTidy)
86 endif()
87 if (USE_CLANG_TIDY_C)
88 monero_clang_tidy("C")
89 elseif (USE_CLANG_TIDY_CXX)
90 monero_clang_tidy("CXX")
91 endif()
92
93 enable_language(C ASM)
94
95 # Require C11/C++11 and disable extensions for all targets
96 set(CMAKE_C_STANDARD 11)
97 set(CMAKE_C_STANDARD_REQUIRED ON)
98 set(CMAKE_C_EXTENSIONS OFF)
99 set(CMAKE_CXX_STANDARD 14)
100 set(CMAKE_CXX_STANDARD_REQUIRED ON)
101 set(CMAKE_CXX_EXTENSIONS OFF)
102
103 function (die msg)
104 if (NOT WIN32)
105 string(ASCII 27 Esc)
106 set(ColourReset "${Esc}[m")
107 set(BoldRed "${Esc}[1;31m")
108 else ()
109 set(ColourReset "")
110 set(BoldRed "")
111 endif ()
112
113 message(FATAL_ERROR "${BoldRed}${msg}${ColourReset}")
114 endfunction ()
115
116 function (add_c_flag_if_supported flag var)
117 # Prepending the flag with -Werror will only add the flag,
118 # if it doesn't result in generation of a warning of using a flag unknown to the compiler.
119 set(TMP "-Werror ${flag}")
120 string(REGEX REPLACE "[- ]" "_" supported ${TMP}_c)
121 check_c_compiler_flag(${TMP} ${supported})
122 if(${${supported}})
123 set(${var} "${${var}} ${flag}" PARENT_SCOPE)
124 endif()
125 endfunction()
126
127 function (add_cxx_flag_if_supported flag var)
128 set(TMP "-Werror ${flag}")
129 string(REGEX REPLACE "[- ]" "_" supported ${TMP}_cxx)
130 check_cxx_compiler_flag(${TMP} ${supported})
131 if(${${supported}})
132 set(${var} "${${var}} ${flag}" PARENT_SCOPE)
133 endif()
134 endfunction()
135
136 function (add_linker_flag_if_supported flag var)
137 string(REPLACE "-" "_" supported ${flag}_ld)
138 string(REPLACE "," "_" supported ${flag}_ld)
139 check_linker_flag(${flag} ${supported})
140 if(${${supported}})
141 set(${var} "${${var}} ${flag}" PARENT_SCOPE)
142 endif()
143 endfunction()
144
145 function (add_definition_if_function_found function var)
146 string(REPLACE "-" "_" supported ${function}_function)
147 check_function_exists(${function} ${supported})
148 if(${${supported}})
149 add_definitions("-D${var}")
150 endif()
151 endfunction()
152
153 function (add_definition_if_library_exists library function header var)
154 string(REPLACE "-" "_" supported ${function}_library)
155 check_library_exists(${library} ${function} ${header} ${supported})
156 if(${${supported}})
157 add_definitions("-D${var}")
158 endif()
159 endfunction()
160
161 option(RELINK_TARGETS "Relink targets, when just a dependant .so changed, but not its header?" OFF)
162 function (monero_set_target_no_relink target)
163 if (RELINK_TARGETS MATCHES OFF)
164 # Will not relink the target, when just its dependant .so has changed, but not it's interface
165 set_target_properties("${target}" PROPERTIES LINK_DEPENDS_NO_SHARED true)
166 endif()
167 endfunction()
168
169 option(STRIP_TARGETS "Strip symbols from targets?" OFF)
170 function (monero_set_target_strip target)
171 if (STRIP_TARGETS)
172 set_target_properties("${target}" PROPERTIES LINK_FLAGS_RELEASE -s)
173 set_target_properties("${target}" PROPERTIES LINK_FLAGS_DEBUG -s)
174 # Stripping from Debug might make sense if you're low on disk space, but want to test if debug version builds properly.
175 endif()
176 endfunction()
177
178 function (monero_add_minimal_executable name)
179 source_group("${name}"
180 FILES
181 ${ARGN})
182
183 add_executable("${name}"
184 ${ARGN})
185 monero_set_target_no_relink("${name}")
186 monero_set_target_strip ("${name}")
187 endfunction()
188
189 # Finds all headers in a directory and its subdirs, to be able to search for them and autosave in IDEs.
190 #
191 # Parameters:
192 # - headers_found: Output variable, which will hold the found headers
193 # - module_root_dir: The search path for the headers. Typically it will be the module's root dir, so "${CMAKE_CURRENT_SOURCE_DIR}" or a derivative of it.
194 macro (monero_find_all_headers headers_found module_root_dir)
195 file(GLOB ${headers_found}
196 "${module_root_dir}/*.h*" # h* will include hpps as well.
197 "${module_root_dir}/**/*.h*" # Any number of subdirs will be included.
198 "${module_root_dir}/*.inl" # .inl is typically template code and is being treated as headers (it's being included).
199 "${module_root_dir}/**/*.inl"
200 )
201 endmacro()
202
203 # Function to forbid undefined symbols and also verify
204 # 1) Test project with all types of libraries and without undefined symbols can compile successfully
205 # 2) Test project with all types of libraries and undefined symbols can not compile successfully
206 function(forbid_undefined_symbols)
207 unset(TMP)
208 # https://www.unix.com/man-page/linux/1/ld, --no-undefined, Report unresolved symbol references from regular object files.
209 add_linker_flag_if_supported(-Wl,--no-undefined TMP)
210 # https://www.unix.com/man-page/osx/1/ld/, -undefined, Specifies how undefined symbols are to be treated.
211 add_linker_flag_if_supported(-Wl,-undefined,error TMP)
212 string(APPEND CMAKE_SHARED_LINKER_FLAGS ${TMP})
213 string(APPEND CMAKE_MODULE_LINKER_FLAGS ${TMP})
214 set(CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS} PARENT_SCOPE)
215 set(CMAKE_MODULE_LINKER_FLAGS ${CMAKE_MODULE_LINKER_FLAGS} PARENT_SCOPE)
216 set(TEST_PROJECT "${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/CMakeTmp/test_project")
217 foreach(EXPECT IN ITEMS TRUE FALSE)
218 file(REMOVE_RECURSE "${TEST_PROJECT}")
219 file(MAKE_DIRECTORY "${TEST_PROJECT}")
220 file(WRITE "${TEST_PROJECT}/CMakeLists.txt"
221 [=[
222 cmake_minimum_required(VERSION 3.5)
223 project(test)
224 option(EXPECT_SUCCESS "" ON)
225 file(WRITE "${CMAKE_SOURCE_DIR}/incorrect_source.cpp" "void undefined_symbol(); void symbol() { undefined_symbol(); }")
226 if (EXPECT_SUCCESS)
227 file(APPEND "${CMAKE_SOURCE_DIR}/incorrect_source.cpp" " void undefined_symbol() {}; ")
228 endif()
229 add_library(l0 SHARED incorrect_source.cpp)
230 add_library(l1 MODULE incorrect_source.cpp)
231 add_library(l2 STATIC incorrect_source.cpp)
232 add_library(l3 OBJECT incorrect_source.cpp)
233 ]=]
234 )
235 try_compile(SUCCESS "${TEST_PROJECT}/build" "${TEST_PROJECT}" test
236 CMAKE_FLAGS
237 "-DCMAKE_SHARED_LINKER_FLAGS=${CMAKE_SHARED_LINKER_FLAGS}"
238 "-DCMAKE_MODULE_LINKER_FLAGS=${CMAKE_MODULE_LINKER_FLAGS}"
239 "-DEXPECT_SUCCESS=${EXPECT}"
240 )
241 if (NOT ${SUCCESS} STREQUAL ${EXPECT})
242 message(FATAL_ERROR "Undefined symbols test failure: expect(${EXPECT}), success(${SUCCESS})")
243 endif()
244 file(REMOVE_RECURSE "${TEST_PROJECT}")
245 endforeach()
246 endfunction()
247 if (NOT (CMAKE_SYSTEM_NAME MATCHES "kOpenBSD.*|OpenBSD.*") AND NOT OSSFUZZ)
248 forbid_undefined_symbols()
249 endif()
250
251 if (MINGW)
252 function(export_all_symbols)
253 unset(TMP)
254 add_linker_flag_if_supported(-Wl,--export-all-symbols TMP)
255 string(APPEND CMAKE_SHARED_LINKER_FLAGS ${TMP})
256 string(APPEND CMAKE_MODULE_LINKER_FLAGS ${TMP})
257 set(CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS} PARENT_SCOPE)
258 set(CMAKE_MODULE_LINKER_FLAGS ${CMAKE_MODULE_LINKER_FLAGS} PARENT_SCOPE)
259 endfunction()
260 export_all_symbols()
261 endif()
262
263 if(NOT CMAKE_BUILD_TYPE)
264 set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
265 message(STATUS "Setting default build type: ${CMAKE_BUILD_TYPE}")
266 endif()
267 string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)
268
269 # ARCH defines the target architecture, either by an explicit identifier or
270 # one of the following two keywords. By default, ARCH a value of 'native':
271 # target arch = host arch, binary is not portable. When ARCH is set to the
272 # string 'default', no -march arg is passed, which creates a binary that is
273 # portable across processors in the same family as host processor. In cases
274 # when ARCH is not set to an explicit identifier, cmake's builtin is used
275 # to identify the target architecture, to direct logic in this cmake script.
276 # Since ARCH is a cached variable, it will not be set on first cmake invocation.
277 if (NOT ARCH_ID)
278 if (NOT ARCH OR ARCH STREQUAL "" OR ARCH STREQUAL "native" OR ARCH STREQUAL "default")
279 if(CMAKE_SYSTEM_PROCESSOR STREQUAL "")
280 set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR})
281 endif()
282 set(ARCH_ID "${CMAKE_SYSTEM_PROCESSOR}")
283 else()
284 set(ARCH_ID "${ARCH}")
285 endif()
286 endif()
287 string(TOLOWER "${ARCH_ID}" ARM_ID)
288 string(SUBSTRING "${ARM_ID}" 0 3 ARM_TEST)
289 if (ARM_TEST STREQUAL "arm")
290 set(ARM 1)
291 string(SUBSTRING "${ARM_ID}" 0 5 ARM_TEST)
292 if (ARM_TEST STREQUAL "armv6")
293 set(ARM6 1)
294 endif()
295 if (ARM_TEST STREQUAL "armv7")
296 set(ARM7 1)
297 endif()
298 endif()
299
300 if (ARM_ID STREQUAL "aarch64" OR ARM_ID STREQUAL "arm64" OR ARM_ID STREQUAL "armv8-a")
301 set(ARM 1)
302 set(ARM8 1)
303 set(ARCH "armv8-a")
304 endif()
305
306 if(ARCH_ID STREQUAL "ppc64le")
307 set(PPC64LE 1)
308 set(PPC64 0)
309 set(PPC 0)
310 endif()
311
312 if(ARCH_ID STREQUAL "powerpc64" OR ARCH_ID STREQUAL "ppc64")
313 set(PPC64LE 0)
314 set(PPC64 1)
315 set(PPC 0)
316 endif()
317
318 if(ARCH_ID STREQUAL "powerpc" OR ARCH_ID STREQUAL "ppc")
319 set(PPC64LE 0)
320 set(PPC64 0)
321 set(PPC 1)
322 endif()
323
324 if(ARCH_ID STREQUAL "s390x")
325 set(S390X 1)
326 endif()
327
328 if(ARCH_ID STREQUAL "riscv64")
329 set(RISCV 1)
330 set(RISCV64 1)
331 endif()
332
333 if(ARCH_ID STREQUAL "riscv32")
334 set(RISCV 1)
335 set(RISCV32 1)
336 endif()
337
338 if(ARCH_ID STREQUAL "loongarch64")
339 set(LOONGARCH 1)
340 set(LOONGARCH64 1)
341 endif()
342
343 if(WIN32 OR ARM OR PPC64LE OR PPC64 OR PPC)
344 set(OPT_FLAGS_RELEASE "-O2")
345 else()
346 set(OPT_FLAGS_RELEASE "-Ofast")
347 endif()
348
349 # BUILD_TAG is used to select the build type to check for a new version
350 if(BUILD_TAG)
351 message(STATUS "Building build tag ${BUILD_TAG}")
352 add_definitions("-DBUILD_TAG=${BUILD_TAG}")
353 else()
354 message(STATUS "Building without build tag")
355 endif()
356
357 if(NOT MANUAL_SUBMODULES)
358 find_package(Git)
359 if(GIT_FOUND)
360 function (check_submodule relative_path)
361 execute_process(COMMAND git rev-parse "HEAD" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${relative_path} OUTPUT_VARIABLE localHead)
362 execute_process(COMMAND git rev-parse "HEAD:${relative_path}" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} OUTPUT_VARIABLE checkedHead)
363 string(COMPARE EQUAL "${localHead}" "${checkedHead}" upToDate)
364 if (upToDate)
365 message(STATUS "Submodule '${relative_path}' is up-to-date")
366 else()
367 message(FATAL_ERROR "Submodule '${relative_path}' is not up-to-date. Please update all submodules with\ngit submodule update --init --force\nor run cmake with -DMANUAL_SUBMODULES=1\n")
368 endif()
369 endfunction ()
370
371 message(STATUS "Checking submodules")
372 check_submodule(external/rapidjson)
373 check_submodule(external/trezor-common)
374 check_submodule(external/randomx)
375 check_submodule(external/supercop)
376 endif()
377 endif()
378
379 set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG ${OPT_FLAGS_RELEASE}")
380 set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG ${OPT_FLAGS_RELEASE}")
381
382 # set this to 0 if per-block checkpoint needs to be disabled
383 set(PER_BLOCK_CHECKPOINT 1)
384
•385 if(PER_BLOCK_CHECKPOINT)
386 add_definitions("-DPER_BLOCK_CHECKPOINT")
387 set(Blocks "blocks")
388 else()
389 set(Blocks "")
390 endif()
391
392 list(INSERT CMAKE_MODULE_PATH 0
393 "${CMAKE_SOURCE_DIR}/cmake")
394
395 if (NOT DEFINED ENV{DEVELOPER_LOCAL_TOOLS})
396 message(STATUS "Could not find DEVELOPER_LOCAL_TOOLS in env (not required)")
397 set(BOOST_IGNORE_SYSTEM_PATHS_DEFAULT OFF)
398 elseif ("$ENV{DEVELOPER_LOCAL_TOOLS}" EQUAL 1)
399 message(STATUS "Found: env DEVELOPER_LOCAL_TOOLS = 1")
400 set(BOOST_IGNORE_SYSTEM_PATHS_DEFAULT ON)
401 else()
402 message(STATUS "Found: env DEVELOPER_LOCAL_TOOLS = 0")
403 set(BOOST_IGNORE_SYSTEM_PATHS_DEFAULT OFF)
404 endif()
405
406 message(STATUS "BOOST_IGNORE_SYSTEM_PATHS defaults to ${BOOST_IGNORE_SYSTEM_PATHS_DEFAULT}")
407 option(BOOST_IGNORE_SYSTEM_PATHS "Ignore boost system paths for local boost installation" ${BOOST_IGNORE_SYSTEM_PATHS_DEFAULT})
408
409 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
410 enable_testing()
411
412 option(BUILD_DOCUMENTATION "Build the Doxygen documentation." ON)
413 option(BUILD_TESTS "Build tests." OFF)
414 if (CMAKE_BUILD_TYPE STREQUAL "Debug")
415 set(DEFAULT_BUILD_DEBUG_UTILITIES ON)
416 else()
417 set(DEFAULT_BUILD_DEBUG_UTILITIES OFF)
418 endif()
419 option(BUILD_DEBUG_UTILITIES "Build debug utilities." DEFAULT_BUILD_DEBUG_UTILITIES)
420
421 if(OSSFUZZ)
422 message(STATUS "Using OSS-Fuzz fuzzing system")
423 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOSSFUZZ")
424 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DOSSFUZZ")
425 endif()
426
427 # Check whether we're on a 32-bit or 64-bit system
428 if(CMAKE_SIZEOF_VOID_P EQUAL "8")
429 set(DEFAULT_BUILD_64 ON)
430 else()
431 set(DEFAULT_BUILD_64 OFF)
432 endif()
433 option(BUILD_64 "Build for 64-bit? 'OFF' builds for 32-bit." ${DEFAULT_BUILD_64})
434
435 if(BUILD_64)
436 set(ARCH_WIDTH "64")
437 else()
438 set(ARCH_WIDTH "32")
439 endif()
440 message(STATUS "Building for a ${ARCH_WIDTH}-bit system")
441
442 # CMAKE_SYSTEM_NAME checks are commonly known, but specifically taken from libsdl's CMakeLists
443 if(CMAKE_SYSTEM_NAME MATCHES "kFreeBSD.*|FreeBSD")
444 set(FREEBSD TRUE)
445 endif()
446
447 # Check if we're on DragonFly BSD. See the README.md for build instructions.
448 if(CMAKE_SYSTEM_NAME MATCHES "DragonFly.*")
449 set(DRAGONFLY TRUE)
450 endif()
451
452 # Check if we're on OpenBSD. See the README.md for build instructions.
453 if(CMAKE_SYSTEM_NAME MATCHES "kOpenBSD.*|OpenBSD.*")
454 set(OPENBSD TRUE)
455 endif()
456
457 # TODO: check bsdi, NetBSD, to see if they need the same FreeBSD changes
458 #
459 # elseif(CMAKE_SYSTEM_NAME MATCHES "kNetBSD.*|NetBSD.*")
460 # set(NETBSD TRUE)
461 # elseif(CMAKE_SYSTEM_NAME MATCHES ".*BSDI.*")
462 # set(BSDI TRUE)
463
464 include_directories(/usr/include)
465
466 include_directories(external/rapidjson/include external/easylogging++ src contrib/epee/include external external/supercop/include)
467
468 if(APPLE)
469 cmake_policy(SET CMP0042 NEW)
470 endif()
471
472 if(MSVC OR MINGW)
473 set(DEFAULT_STATIC true)
474 else()
475 set(DEFAULT_STATIC false)
476 endif()
477 option(STATIC "Link libraries statically" ${DEFAULT_STATIC})
478
479 # This is a CMake built-in switch that concerns internal libraries
480 set(BUILD_SHARED_LIBS_DEFAULT OFF)
481 if (NOT STATIC AND CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
482 set(BUILD_SHARED_LIBS_DEFAULT ON)
483 endif()
484 option(BUILD_SHARED_LIBS "Build internal libraries as shared" ${BUILD_SHARED_LIBS_DEFAULT})
485
486 if (BUILD_SHARED_LIBS)
487 message(STATUS "Building internal libraries with position independent code")
488 add_definitions("-DBUILD_SHARED_LIBS")
489 else()
490 message(STATUS "Building internal libraries as static")
491 endif()
492 set(PIC_FLAG "-fPIC")
493
494 if(MINGW)
495 string(REGEX MATCH "^[^/]:/[^/]*" msys2_install_path "${CMAKE_C_COMPILER}")
496 message(STATUS "MSYS location: ${msys2_install_path}")
497 set(CMAKE_INCLUDE_PATH "${msys2_install_path}/mingw${ARCH_WIDTH}/include")
498 # This is necessary because otherwise CMake will make Boost libraries -lfoo
499 # rather than a full path. Unfortunately, this makes the shared libraries get
500 # linked due to a bug in CMake which misses putting -static flags around the
501 # -lfoo arguments.
502 set(DEFLIB ${msys2_install_path}/mingw${ARCH_WIDTH}/lib)
503 list(REMOVE_ITEM CMAKE_C_IMPLICIT_LINK_DIRECTORIES ${DEFLIB})
504 list(REMOVE_ITEM CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES ${DEFLIB})
505 endif()
506
507 if(STATIC)
508 if(MSVC)
509 set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .dll.a .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
510 else()
511 set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
512 endif()
513 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DZMQ_STATIC")
514 endif()
515
516 option(SANITIZE "Use ASAN memory sanitizer" OFF)
517 if(SANITIZE)
518 if (MSVC)
519 message(FATAL_ERROR "Cannot sanitize with MSVC")
520 else()
521 message(STATUS "Using ASAN")
522 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
523 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
524 endif()
525 endif()
526
527 # Set default blockchain storage location:
528 # memory was the default in Cryptonote before Monero implemented LMDB, it still works but is unnecessary.
529 # set(DATABASE memory)
530 set(DATABASE lmdb)
531 message(STATUS "Using LMDB as default DB type")
532 set(BLOCKCHAIN_DB DB_LMDB)
533 add_definitions("-DDEFAULT_DB_TYPE=\"lmdb\"")
534 add_definitions("-DBLOCKCHAIN_DB=${BLOCKCHAIN_DB}")
535
536 # Can't install hook in static build on OSX, because OSX linker does not support --wrap
537 # On ARM, having libunwind package (with .so's only) installed breaks static link.
538 # When possible, avoid stack tracing using libunwind in favor of using easylogging++.
539 if (APPLE)
540 set(DEFAULT_STACK_TRACE OFF)
541 set(LIBUNWIND_LIBRARIES "")
542 elseif (DEPENDS AND NOT LINUX)
543 set(DEFAULT_STACK_TRACE OFF)
544 set(LIBUNWIND_LIBRARIES "")
545 elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND NOT MINGW)
546 set(DEFAULT_STACK_TRACE ON)
547 set(STACK_TRACE_LIB "easylogging++") # for diag output only
548 set(LIBUNWIND_LIBRARIES "")
549 elseif (ARM)
550 set(DEFAULT_STACK_TRACE OFF)
551 set(LIBUNWIND_LIBRARIES "")
552 else()
553 find_package(Libunwind)
554 if(LIBUNWIND_FOUND)
555 set(DEFAULT_STACK_TRACE ON)
556 set(STACK_TRACE_LIB "libunwind") # for diag output only
557 else()
558 set(DEFAULT_STACK_TRACE OFF)
559 set(LIBUNWIND_LIBRARIES "")
560 endif()
561 endif()
562
563 option(STACK_TRACE "Install a hook that dumps stack on exception" ${DEFAULT_STACK_TRACE})
564
565 if(STACK_TRACE)
566 message(STATUS "Stack trace on exception enabled (using ${STACK_TRACE_LIB})")
567 else()
568 message(STATUS "Stack trace on exception disabled")
569 endif()
570
571 if (UNIX AND NOT APPLE)
572 # Note that at the time of this writing the -Wstrict-prototypes flag added below will make this fail
573 set(THREADS_PREFER_PTHREAD_FLAG ON)
574 find_package(Threads)
575 add_c_flag_if_supported(-pthread CMAKE_C_FLAGS)
576 add_cxx_flag_if_supported(-pthread CMAKE_CXX_FLAGS)
577 endif()
578
579 # Handle OpenSSL, used for sha256sum on binary updates and light wallet ssl http
580 if (CMAKE_SYSTEM_NAME MATCHES "(SunOS|Solaris)")
581 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthreads")
582 endif ()
583
584 if (APPLE AND NOT IOS)
585 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=default")
586 if (NOT OPENSSL_ROOT_DIR)
587 EXECUTE_PROCESS(COMMAND brew --prefix openssl
588 OUTPUT_VARIABLE OPENSSL_ROOT_DIR
589 OUTPUT_STRIP_TRAILING_WHITESPACE)
590 message(STATUS "Using OpenSSL found at ${OPENSSL_ROOT_DIR}")
591 endif()
592 endif()
593
594 find_package(OpenSSL REQUIRED)
595 message(STATUS "Using OpenSSL include dir at ${OPENSSL_INCLUDE_DIR}")
596 include_directories(${OPENSSL_INCLUDE_DIR})
597 if(STATIC AND NOT IOS)
598 if(UNIX)
599 set(OPENSSL_LIBRARIES "${OPENSSL_LIBRARIES};${CMAKE_DL_LIBS};${CMAKE_THREAD_LIBS_INIT}")
600 endif()
601 endif()
602
603 if (WIN32)
604 list(APPEND OPENSSL_LIBRARIES ws2_32 crypt32)
605 endif()
606
607 find_package(HIDAPI)
608
609 add_definition_if_library_exists(c memset_s "string.h" HAVE_MEMSET_S)
610 add_definition_if_library_exists(c explicit_bzero "strings.h" HAVE_EXPLICIT_BZERO)
611 add_definition_if_function_found(strptime HAVE_STRPTIME)
612
613 add_definitions(-DAUTO_INITIALIZE_EASYLOGGINGPP)
614
615 set(MONERO_GENERATED_HEADERS_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated_include")
616 include_directories(${MONERO_GENERATED_HEADERS_DIR})
617
618 # As of OpenBSD 6.8, -march=<anything> breaks the build
619 function(set_default_arch)
620 if (OPENBSD)
621 set(ARCH default)
622 else()
623 set(ARCH native)
624 endif()
625
626 set(ARCH ${ARCH} CACHE STRING "CPU to build for: -march value or 'default' to not pass -march at all")
627 endfunction()
628
629 if (NOT (MSVC OR ARCH))
630 set_default_arch()
631 endif()
632
633 option(COVERAGE "Enable profiling for test coverage report" OFF)
634 if(COVERAGE)
635 message(STATUS "Building with profiling for test coverage report")
636 endif()
637 macro (monero_enable_coverage)
638 if(COVERAGE)
639 foreach(COV_FLAG -fprofile-arcs -ftest-coverage --coverage)
640 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COV_FLAG}")
641 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COV_FLAG}")
642 endforeach()
643 endif()
644 endmacro()
645
646 function (monero_add_library name)
647 monero_add_library_with_deps(NAME "${name}" SOURCES ${ARGN})
648 endfunction()
649
650 function (monero_add_library_with_deps)
651 cmake_parse_arguments(MONERO_ADD_LIBRARY "" "NAME" "DEPENDS;SOURCES" ${ARGN})
652 source_group("${MONERO_ADD_LIBRARY_NAME}" FILES ${MONERO_ADD_LIBRARY_SOURCES})
653
654 # Define a ("virtual") object library and an actual library that links those
655 # objects together. The virtual libraries can be arbitrarily combined to link
656 # any subset of objects into one library archive. This is used for releasing
657 # libwallet, which combines multiple components.
658 set(objlib obj_${MONERO_ADD_LIBRARY_NAME})
659 add_library(${objlib} OBJECT ${MONERO_ADD_LIBRARY_SOURCES})
660 add_library("${MONERO_ADD_LIBRARY_NAME}" $<TARGET_OBJECTS:${objlib}>)
661 monero_set_target_no_relink("${MONERO_ADD_LIBRARY_NAME}")
662 monero_set_target_strip ("${MONERO_ADD_LIBRARY_NAME}")
663 if (MONERO_ADD_LIBRARY_DEPENDS)
664 add_dependencies(${objlib} ${MONERO_ADD_LIBRARY_DEPENDS})
665 endif()
666 set_property(TARGET "${MONERO_ADD_LIBRARY_NAME}" PROPERTY FOLDER "libs")
667 target_compile_definitions(${objlib}
668 PRIVATE $<TARGET_PROPERTY:${MONERO_ADD_LIBRARY_NAME},INTERFACE_COMPILE_DEFINITIONS>)
669 endfunction ()
670
671 # Generate header for embedded translations
672 # Generate header for embedded translations, use target toolchain if depends, otherwise use the
673 # lrelease and lupdate binaries from the host
674 include(ExternalProject)
675 ExternalProject_Add(generate_translations_header
676 SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/translations"
677 BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/translations"
678 STAMP_DIR ${LRELEASE_PATH}
679 CMAKE_ARGS -DLRELEASE_PATH=${LRELEASE_PATH}
680 INSTALL_COMMAND ${CMAKE_COMMAND} -E echo "")
681 include_directories("${CMAKE_CURRENT_BINARY_DIR}/translations")
682 add_subdirectory(external)
683
684 # Final setup for libunbound
685 include_directories(${UNBOUND_INCLUDE_DIR})
686
687 # Final setup for easylogging++
688 include_directories(${EASYLOGGING_INCLUDE})
689 link_directories(${EASYLOGGING_LIBRARY_DIRS})
690
691 # Final setup for liblmdb
692 include_directories(${LMDB_INCLUDE})
693
694 # Final setup for libunwind
695 include_directories(${LIBUNWIND_INCLUDE})
696 link_directories(${LIBUNWIND_LIBRARY_DIRS})
697
698 # Final setup for hid
699 if (HIDAPI_FOUND)
700 message(STATUS "Using HIDAPI include dir at ${HIDAPI_INCLUDE_DIR}")
701 add_definitions(-DHAVE_HIDAPI)
702 include_directories(${HIDAPI_INCLUDE_DIR})
703 link_directories(${LIBHIDAPI_LIBRARY_DIRS})
704 else()
705 message(STATUS "Could not find HIDAPI")
706 endif()
707
708 # Trezor support check
709 include(CheckTrezor)
710
711 if(MSVC)
712 add_definitions("/bigobj /MP /W3 /GS- /D_CRT_SECURE_NO_WARNINGS /wd4996 /wd4345 /D_WIN32_WINNT=0x0600 /DWIN32_LEAN_AND_MEAN /DGTEST_HAS_TR1_TUPLE=0 /FIinline_c.h /D__SSE4_1__")
713 # set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Dinline=__inline")
714 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10485760")
715 if(STATIC)
716 foreach(VAR CMAKE_C_FLAGS_DEBUG CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_RELEASE)
717 string(REPLACE "/MD" "/MT" ${VAR} "${${VAR}}")
718 endforeach()
719 endif()
720 include_directories(SYSTEM src/platform/msc)
721 else()
722 include(TestCXXAcceptsFlag)
723 message(STATUS "Building on ${CMAKE_SYSTEM_PROCESSOR} for ${ARCH}")
724 if(ARCH STREQUAL "default")
725 set(ARCH_FLAG "")
726 elseif(PPC64LE)
727 set(ARCH_FLAG "-mcpu=power8")
728 elseif(PPC64)
729 set(ARCH_FLAG "-mcpu=970")
730 elseif(PPC)
731 set(ARCH_FLAG "-mcpu=7400")
732 elseif(IOS AND ARCH STREQUAL "arm64")
733 message(STATUS "IOS: Changing arch from arm64 to armv8")
734 set(ARCH_FLAG "-march=armv8")
735 else()
736 set(ARCH_FLAG "-march=${ARCH}")
737 if(ARCH STREQUAL "native")
738 check_c_compiler_flag(-march=native CC_SUPPORTS_MARCH_NATIVE)
739 if (NOT CC_SUPPORTS_MARCH_NATIVE)
740 check_c_compiler_flag(-mtune=native CC_SUPPORTS_MTUNE_NATIVE)
741 if (CC_SUPPORTS_MTUNE_NATIVE)
742 set(ARCH_FLAG "-mtune=${ARCH}")
743 else()
744 set(ARCH_FLAG "")
745 endif()
746 endif()
747 endif()
748 endif()
749
750 option(NO_AES "Explicitly disable AES support" ${NO_AES})
751
752 if(NO_AES)
753 message(STATUS "AES support explicitly disabled")
754 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNO_AES")
755 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNO_AES")
756 elseif(NOT ARM AND NOT PPC64LE AND NOT PPC64 AND NOT PPC AND NOT S390X AND NOT RISCV AND NOT LOONGARCH)
757 message(STATUS "AES support enabled")
758 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes")
759 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes")
760 elseif(PPC64LE OR PPC64 OR PPC)
761 message(STATUS "AES support not available on POWER")
762 elseif(S390X)
763 message(STATUS "AES support not available on s390x")
764 elseif(RISCV)
765 message(STATUS "AES support not available on RISC-V")
766 elseif(LOONGARCH)
767 message(STATUS "AES support not available on LOONGARCH")
768 elseif(ARM6)
769 message(STATUS "AES support not available on ARMv6")
770 elseif(ARM7)
771 message(STATUS "AES support not available on ARMv7")
772 elseif(ARM8)
773 CHECK_CXX_ACCEPTS_FLAG("-march=${ARCH}+crypto" ARCH_PLUS_CRYPTO)
774 if(ARCH_PLUS_CRYPTO)
775 message(STATUS "Crypto extensions enabled for ARMv8")
776 set(ARCH_FLAG "-march=${ARCH}+crypto")
777 else()
778 message(STATUS "Crypto extensions unavailable on your ARMv8 device")
779 endif()
780 else()
781 message(STATUS "AES support disabled")
782 endif()
783
784 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${ARCH_FLAG}")
785 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ARCH_FLAG}")
786
787 set(WARNINGS "-Wall -Wextra -Wpointer-arith -Wundef -Wvla -Wwrite-strings -Wno-error=extra -Wno-error=deprecated-declarations -Wno-unused-parameter -Wno-error=unused-variable -Wno-error=undef -Wno-error=uninitialized")
788 if(CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
789 if(ARM)
790 set(WARNINGS "${WARNINGS} -Wno-error=inline-asm")
791 endif()
792 else()
793 set(WARNINGS "${WARNINGS} -Wlogical-op -Wno-error=maybe-uninitialized -Wno-error=cpp")
794 endif()
795 if(MINGW)
796 set(WARNINGS "${WARNINGS} -Wno-error=unused-value -Wno-error=unused-but-set-variable")
797 set(MINGW_FLAG "${MINGW_FLAG} -DWIN32_LEAN_AND_MEAN")
798 set(Boost_THREADAPI win32)
799 include_directories(SYSTEM src/platform/mingw)
800 # mingw doesn't support LTO (multiple definition errors at link time)
801 set(USE_LTO_DEFAULT false)
802 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,10485760")
803 if(NOT BUILD_64)
804 add_definitions(-DWINVER=0x0600 -D_WIN32_WINNT=0x0600)
805 endif()
806 endif()
807 set(C_WARNINGS "-Waggregate-return -Wnested-externs -Wold-style-definition -Wstrict-prototypes")
808 set(CXX_WARNINGS "-Wno-reorder -Wno-missing-field-initializers")
809 try_compile(STATIC_ASSERT_RES "${CMAKE_CURRENT_BINARY_DIR}/static-assert" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/test-static-assert.c" CMAKE_FLAGS -DCMAKE_C_STANDARD=11)
810 if(STATIC_ASSERT_RES)
811 set(STATIC_ASSERT_FLAG "")
812 else()
813 set(STATIC_ASSERT_FLAG "-Dstatic_assert=_Static_assert")
814 endif()
815
816 try_compile(STATIC_ASSERT_CPP_RES "${CMAKE_CURRENT_BINARY_DIR}/static-assert" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/test-static-assert.cpp" CMAKE_FLAGS -DCMAKE_CXX_STANDARD=11)
817 if(STATIC_ASSERT_CPP_RES)
818 set(STATIC_ASSERT_CPP_FLAG "")
819 else()
820 set(STATIC_ASSERT_CPP_FLAG "-Dstatic_assert=_Static_assert")
821 endif()
822
823 monero_enable_coverage()
824 # With GCC 6.1.1 the compiled binary malfunctions due to aliasing. Until that
825 # is fixed in the code (Issue #847), force compiler to be conservative.
826 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-strict-aliasing")
827 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
828
829 # if those don't work for your compiler, single it out where appropriate
830 if(CMAKE_BUILD_TYPE STREQUAL "Release" AND NOT OPENBSD)
831 set(C_SECURITY_FLAGS "${C_SECURITY_FLAGS} -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1")
832 set(CXX_SECURITY_FLAGS "${CXX_SECURITY_FLAGS} -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1")
833 endif()
834
835 # warnings
836 add_c_flag_if_supported(-Wformat C_SECURITY_FLAGS)
837 add_cxx_flag_if_supported(-Wformat CXX_SECURITY_FLAGS)
838 add_c_flag_if_supported(-Wformat-security C_SECURITY_FLAGS)
839 add_cxx_flag_if_supported(-Wformat-security CXX_SECURITY_FLAGS)
840
841 # -fstack-protector
842 if (NOT OPENBSD AND NOT (WIN32 AND (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_LESS 9.1)))
843 add_c_flag_if_supported(-fstack-protector C_SECURITY_FLAGS)
844 add_cxx_flag_if_supported(-fstack-protector CXX_SECURITY_FLAGS)
845 add_c_flag_if_supported(-fstack-protector-strong C_SECURITY_FLAGS)
846 add_cxx_flag_if_supported(-fstack-protector-strong CXX_SECURITY_FLAGS)
847 endif()
848
849 # New in GCC 8.2
850 if (NOT OPENBSD AND NOT (WIN32 AND (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_LESS 9.1)))
851 add_c_flag_if_supported(-fcf-protection=full C_SECURITY_FLAGS)
852 add_cxx_flag_if_supported(-fcf-protection=full CXX_SECURITY_FLAGS)
853 endif()
854 if (NOT WIN32 AND NOT OPENBSD)
855 add_c_flag_if_supported(-fstack-clash-protection C_SECURITY_FLAGS)
856 add_cxx_flag_if_supported(-fstack-clash-protection CXX_SECURITY_FLAGS)
857 endif()
858
859 # Removed in GCC 9.1 (or before ?), but still accepted, so spams the output
860 if (NOT (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 9.1))
861 add_c_flag_if_supported(-mmitigate-rop C_SECURITY_FLAGS)
862 add_cxx_flag_if_supported(-mmitigate-rop CXX_SECURITY_FLAGS)
863 endif()
864
865 # linker
866 if (NOT SANITIZE AND NOT OSSFUZZ AND NOT (WIN32 AND (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND (CMAKE_C_COMPILER_VERSION VERSION_LESS 9.1 OR NOT STATIC))))
867 # PIE executables randomly crash at startup with ASAN
868 # Windows binaries die on startup with PIE when compiled with GCC <9.x
869 # Windows dynamically-linked binaries die on startup with PIE regardless of GCC version
870 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
871 # Clang does not support -pie flag
872 add_linker_flag_if_supported("-Wl,-pie" LD_SECURITY_FLAGS)
873 else()
874 add_linker_flag_if_supported("-pie" LD_SECURITY_FLAGS)
875 endif()
876 endif()
877 add_linker_flag_if_supported(-Wl,-z,relro LD_SECURITY_FLAGS)
878 add_linker_flag_if_supported(-Wl,-z,now LD_SECURITY_FLAGS)
879 add_linker_flag_if_supported(-Wl,-z,noexecstack noexecstack_SUPPORTED)
880 if (noexecstack_SUPPORTED)
881 set(LD_SECURITY_FLAGS "${LD_SECURITY_FLAGS} -Wl,-z,noexecstack")
882 endif()
883 add_linker_flag_if_supported(-Wl,-z,noexecheap noexecheap_SUPPORTED)
884 if (noexecheap_SUPPORTED)
885 set(LD_SECURITY_FLAGS "${LD_SECURITY_FLAGS} -Wl,-z,noexecheap")
886 endif()
887
888 if(BACKCOMPAT)
889 add_linker_flag_if_supported(-Wl,--wrap=__divmoddi4 LD_BACKCOMPAT_FLAGS)
890 add_linker_flag_if_supported(-Wl,--wrap=glob LD_BACKCOMPAT_FLAGS)
891 message(STATUS "Using Lib C back compat flags: ${LD_BACKCOMPAT_FLAGS}")
892 endif()
893
894 # some windows linker bits
895 if (WIN32)
896 add_linker_flag_if_supported(-Wl,--dynamicbase LD_SECURITY_FLAGS)
897 add_linker_flag_if_supported(-Wl,--nxcompat LD_SECURITY_FLAGS)
898 add_linker_flag_if_supported(-Wl,--high-entropy-va LD_SECURITY_FLAGS)
899 endif()
900
901 # Warnings, that when ignored are so severe, that they can segfault or even UB any application.
902 # Treat them as errors.
903 add_c_flag_if_supported( -Werror=switch C_SECURITY_FLAGS)
904 add_cxx_flag_if_supported(-Werror=switch CXX_SECURITY_FLAGS)
905 add_c_flag_if_supported( -Werror=return-type C_SECURITY_FLAGS)
906 add_cxx_flag_if_supported(-Werror=return-type CXX_SECURITY_FLAGS)
907
908 message(STATUS "Using C security hardening flags: ${C_SECURITY_FLAGS}")
909 message(STATUS "Using C++ security hardening flags: ${CXX_SECURITY_FLAGS}")
910 message(STATUS "Using linker security hardening flags: ${LD_SECURITY_FLAGS}")
911
912 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE ${MINGW_FLAG} ${STATIC_ASSERT_FLAG} ${WARNINGS} ${C_WARNINGS} ${PIC_FLAG} ${C_SECURITY_FLAGS}")
913 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GNU_SOURCE ${MINGW_FLAG} ${STATIC_ASSERT_CPP_FLAG} ${WARNINGS} ${CXX_WARNINGS} ${PIC_FLAG} ${CXX_SECURITY_FLAGS}")
914 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LD_SECURITY_FLAGS} ${LD_BACKCOMPAT_FLAGS}")
915
916 # With GCC 6.1.1 the compiled binary malfunctions due to aliasing. Until that
917 # is fixed in the code (Issue #847), force compiler to be conservative.
918 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-strict-aliasing")
919 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
920
921 if(ARM)
922 message(STATUS "Setting FPU Flags for ARM Processors")
923
924 #NB NEON hardware does not fully implement the IEEE 754 standard for floating-point arithmetic
925 #Need custom assembly code to take full advantage of NEON SIMD
926
927 #Cortex-A5/9 -mfpu=neon-fp16
928 #Cortex-A7/15 -mfpu=neon-vfpv4
929 #Cortex-A8 -mfpu=neon
930 #ARMv8 -FP and SIMD on by default for all ARM8v-A series, NO -mfpu setting needed
931
932 #For custom -mtune, processor IDs for ARMv8-A series:
933 #0xd04 - Cortex-A35
934 #0xd07 - Cortex-A57
935 #0xd08 - Cortex-A72
936 #0xd03 - Cortex-A73
937
938 if(NOT ARM8)
939 CHECK_CXX_ACCEPTS_FLAG(-mfpu=vfp3-d16 CXX_ACCEPTS_VFP3_D16)
940 CHECK_CXX_ACCEPTS_FLAG(-mfpu=vfp4 CXX_ACCEPTS_VFP4)
941 CHECK_CXX_ACCEPTS_FLAG(-mfloat-abi=hard CXX_ACCEPTS_MFLOAT_HARD)
942 CHECK_CXX_ACCEPTS_FLAG(-mfloat-abi=softfp CXX_ACCEPTS_MFLOAT_SOFTFP)
943 endif()
944
945 if(ARM8)
946 CHECK_CXX_ACCEPTS_FLAG(-mfix-cortex-a53-835769 CXX_ACCEPTS_MFIX_CORTEX_A53_835769)
947 CHECK_CXX_ACCEPTS_FLAG(-mfix-cortex-a53-843419 CXX_ACCEPTS_MFIX_CORTEX_A53_843419)
948 endif()
949
950 if(ARM6)
951 message(STATUS "Selecting VFP for ARMv6")
952 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfpu=vfp")
953 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=vfp")
954 if(DEPENDS)
955 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -marm")
956 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -marm")
957 endif()
958 endif(ARM6)
959
960 if(ARM7)
961 if(CXX_ACCEPTS_VFP3_D16 AND NOT CXX_ACCEPTS_VFP4)
962 message(STATUS "Selecting VFP3 for ARMv7")
963 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfpu=vfp3-d16")
964 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=vfp3-d16")
965 endif()
966
967 if(CXX_ACCEPTS_VFP4)
968 message(STATUS "Selecting VFP4 for ARMv7")
969 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfpu=vfp4")
970 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=vfp4")
971 endif()
972
973 if(CXX_ACCEPTS_MFLOAT_HARD)
974 message(STATUS "Setting Hardware ABI for Floating Point")
975 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfloat-abi=hard")
976 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfloat-abi=hard")
977 endif()
978
979 if(CXX_ACCEPTS_MFLOAT_SOFTFP AND NOT CXX_ACCEPTS_MFLOAT_HARD)
980 message(STATUS "Setting Software ABI for Floating Point")
981 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfloat-abi=softfp")
982 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfloat-abi=softfp")
983 endif()
984 endif(ARM7)
985
986 if(ARM8)
987 if(CXX_ACCEPTS_MFIX_CORTEX_A53_835769)
988 message(STATUS "Enabling Cortex-A53 workaround 835769")
989 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfix-cortex-a53-835769")
990 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfix-cortex-a53-835769")
991 endif()
992
993 if(CXX_ACCEPTS_MFIX_CORTEX_A53_843419)
994 message(STATUS "Enabling Cortex-A53 workaround 843419")
995 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfix-cortex-a53-843419")
996 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfix-cortex-a53-843419")
997 endif()
998 endif(ARM8)
999
1000 endif(ARM)
1001
1002 # random crash on startup when asan is on if pie is enabled
1003 if(NOT SANITIZE AND ANDROID AND NOT BUILD_GUI_DEPS STREQUAL "ON" OR IOS)
1004 #From Android 5: "only position independent executables (PIE) are supported"
1005 message(STATUS "Enabling PIE executable")
1006 set(PIC_FLAG "")
1007 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE")
1008 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE")
1009 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_CXX_FLAGS} -fPIE -pie")
1010 endif()
1011
1012 if(APPLE)
1013 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=default -DGTEST_HAS_TR1_TUPLE=0")
1014 endif()
1015
1016 set(DEBUG_FLAGS "-g3")
1017
1018 # At least some CLANGs default to not enough for monero
1019 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftemplate-depth=900")
1020
1021 if(NOT DEFINED USE_LTO_DEFAULT)
1022 set(USE_LTO_DEFAULT false)
1023 endif()
1024 set(USE_LTO ${USE_LTO_DEFAULT} CACHE BOOL "Use Link-Time Optimization (Release mode only)")
1025
1026 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
1027 # There is a clang bug that does not allow to compile code that uses AES-NI intrinsics if -flto is enabled, so explicitly disable
1028 set(USE_LTO false)
1029 endif()
1030
1031
1032 if(USE_LTO)
1033 set(RELEASE_FLAGS "${RELEASE_FLAGS} -flto")
1034 if(STATIC)
1035 set(RELEASE_FLAGS "${RELEASE_FLAGS} -ffat-lto-objects")
1036 endif()
1037 # Since gcc 4.9 the LTO format is non-standard (slim), so we need the gcc-specific ar and ranlib binaries
1038 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9.0) AND NOT OPENBSD AND NOT DRAGONFLY)
1039 # When invoking cmake on distributions on which gcc's binaries are prefixed
1040 # with an arch-specific triplet, the user must specify -DCHOST=<prefix>
1041 if (DEFINED CHOST)
1042 set(CMAKE_AR "${CHOST}-gcc-ar")
1043 set(CMAKE_RANLIB "${CHOST}-gcc-ranlib")
1044 else()
1045 set(CMAKE_AR "gcc-ar")
1046 set(CMAKE_RANLIB "gcc-ranlib")
1047 endif()
1048 endif()
1049 endif()
1050
1051 set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${DEBUG_FLAGS}")
1052 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${DEBUG_FLAGS}")
1053 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${RELEASE_FLAGS}")
1054 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${RELEASE_FLAGS}")
1055
1056 if(STATIC)
1057 # STATIC already configures most deps to be linked in statically,
1058 # here we make more deps static if the platform permits it
1059 if (MINGW)
1060 # On Windows, this is as close to fully-static as we get:
1061 # this leaves only deps on /c/Windows/system32/*.dll
1062 set(STATIC_FLAGS "-static")
1063 elseif (NOT (APPLE OR FREEBSD OR OPENBSD OR DRAGONFLY))
1064 # On Linux, we don't support fully static build, but these can be static
1065 set(STATIC_FLAGS "-static-libgcc -static-libstdc++")
1066 endif()
1067 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${STATIC_FLAGS} ")
1068 endif()
1069 endif()
1070
1071 if (${BOOST_IGNORE_SYSTEM_PATHS} STREQUAL "ON")
1072 set(Boost_NO_SYSTEM_PATHS TRUE)
1073 endif()
1074
1075 set(OLD_LIB_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
1076 set(Boost_NO_BOOST_CMAKE ON)
1077 if(STATIC)
1078 if(MINGW)
1079 set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
1080 endif()
1081
1082 set(Boost_USE_STATIC_LIBS ON)
1083 set(Boost_USE_STATIC_RUNTIME ON)
1084 endif()
1085
1086 # Find Boost headers
1087 set(BOOST_MIN_VER 1.62)
1088 find_package(Boost ${BOOST_MIN_VER} QUIET REQUIRED)
1089
1090 if(NOT Boost_FOUND)
1091 die("Could not find Boost libraries, please make sure you have installed Boost or libboost-all-dev (>=${BOOST_MIN_VER}) or the equivalent")
1092 elseif(Boost_FOUND)
1093 message(STATUS "Found Boost Version: ${Boost_VERSION_STRING}")
1094
1095 set(BOOST_COMPONENTS filesystem thread date_time chrono serialization program_options locale)
1096
1097 # Boost System is header-only since 1.69
1098 if (Boost_VERSION_STRING VERSION_LESS 1.69.0)
1099 list(APPEND BOOST_COMPONENTS system)
1100 endif()
1101
1102 # Boost Regex is header-only since 1.77
1103 if (Boost_VERSION_STRING VERSION_LESS 1.77.0)
1104 list(APPEND BOOST_COMPONENTS regex)
1105 endif()
1106
1107 message(STATUS "Boost components: ${BOOST_COMPONENTS}")
1108
1109 # Find required Boost libraries
1110 find_package(Boost ${BOOST_MIN_VER} QUIET REQUIRED COMPONENTS ${BOOST_COMPONENTS})
1111 set(CMAKE_FIND_LIBRARY_SUFFIXES ${OLD_LIB_SUFFIXES})
1112 endif()
1113
1114 add_definitions(-DBOOST_ASIO_ENABLE_SEQUENTIAL_STRAND_ALLOCATION)
1115 add_definitions(-DBOOST_NO_AUTO_PTR)
1116 add_definitions(-DBOOST_UUID_DISABLE_ALIGNMENT) # This restores UUID's std::has_unique_object_representations property
1117
1118 include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
1119 if(MINGW)
1120 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wa,-mbig-obj")
1121 set(EXTRA_LIBRARIES mswsock;ws2_32;iphlpapi;crypt32;bcrypt)
1122 if(DEPENDS)
1123 set(ICU_LIBRARIES icuio icui18n icuuc icudata icutu iconv)
1124 else()
1125 # This is an extremely ugly hack to get around Boost not being built with static ICU.
1126 # We reported the issue, we are waiting for upstream to fix this issue: https://github.com/boostorg/boost/issues/1079#issue-3384962885
1127 # This hack links shared ICU libs to avoid linker errors we get in MSYS2 compilation (undefined symbols to ICU).
1128 set(OLD_LIB_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
1129 set(CMAKE_FIND_LIBRARY_SUFFIXES ".dll.a")
1130 find_library(ICUIO_LIBRARIES NAMES icuio REQUIRED)
1131 find_library(ICUIN_LIBRARIES NAMES icuin REQUIRED)
1132 find_library(ICUUC_LIBRARIES NAMES icuuc REQUIRED)
1133 find_library(ICUDT_LIBRARIES NAMES icudt REQUIRED)
1134 find_library(ICUTU_LIBRARIES NAMES icutu REQUIRED)
1135 find_library(ICONV_LIBRARIES NAMES iconv REQUIRED)
1136 set(ICU_LIBRARIES ${ICUIO_LIBRARIES} ${ICUIN_LIBRARIES} ${ICUUC_LIBRARIES} ${ICUDT_LIBRARIES} ${ICUTU_LIBRARIES} ${ICONV_LIBRARIES})
1137 set(CMAKE_FIND_LIBRARY_SUFFIXES ${OLD_LIB_SUFFIXES})
1138 endif()
1139 elseif(APPLE OR OPENBSD OR ANDROID)
1140 set(EXTRA_LIBRARIES "")
1141 elseif(FREEBSD)
1142 set(EXTRA_LIBRARIES execinfo)
1143 elseif(DRAGONFLY)
1144 find_library(COMPAT compat)
1145 set(EXTRA_LIBRARIES execinfo ${COMPAT})
1146 elseif(CMAKE_SYSTEM_NAME MATCHES "(SunOS|Solaris)")
1147 set(EXTRA_LIBRARIES socket nsl resolv)
1148 elseif(NOT MSVC AND NOT DEPENDS)
1149 find_library(RT rt)
1150 set(EXTRA_LIBRARIES ${RT})
1151 endif()
1152
1153 list(APPEND EXTRA_LIBRARIES ${CMAKE_DL_LIBS})
1154
1155 if (HIDAPI_FOUND OR LibUSB_COMPILE_TEST_PASSED)
1156 if (APPLE)
1157 if(DEPENDS)
1158 list(APPEND EXTRA_LIBRARIES "-framework Foundation -framework AppKit -framework IOKit")
1159 else()
1160 find_library(COREFOUNDATION CoreFoundation)
1161 find_library(APPKIT AppKit)
1162 find_library(IOKIT IOKit)
1163 list(APPEND EXTRA_LIBRARIES ${APPKIT})
1164 list(APPEND EXTRA_LIBRARIES ${IOKIT})
1165 list(APPEND EXTRA_LIBRARIES ${COREFOUNDATION})
1166 endif()
1167 endif()
1168 if (WIN32)
1169 list(APPEND EXTRA_LIBRARIES setupapi)
1170 endif()
1171 endif()
1172
1173 option(USE_READLINE "Build with GNU readline support." ON)
1174 if(USE_READLINE AND NOT DEPENDS)
1175 find_package(Readline)
1176 if(READLINE_FOUND AND GNU_READLINE_FOUND)
1177 add_definitions(-DHAVE_READLINE)
1178 include_directories(${Readline_INCLUDE_DIR})
1179 message(STATUS "Found readline library at: ${Readline_ROOT_DIR}")
1180 set(EPEE_READLINE epee_readline)
1181 else()
1182 message(STATUS "Could not find GNU readline library so building without readline support")
1183 endif()
1184 elseif(USE_READLINE AND DEPENDS AND NOT MINGW)
1185 find_path(Readline_INCLUDE_PATH readline/readline.h)
1186 find_library(Readline_LIBRARY readline)
1187 find_library(Terminfo_LIBRARY tinfo)
1188 set(Readline_LIBRARY "${Readline_LIBRARY};${Terminfo_LIBRARY}")
1189 set(GNU_READLINE_LIBRARY ${Readline_LIBRARY})
1190 add_definitions(-DHAVE_READLINE)
1191 set(EPEE_READLINE epee_readline)
1192 endif()
1193
1194 if(ANDROID)
1195 set(ATOMIC libatomic.a)
1196 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=user-defined-warnings")
1197 endif()
1198 if(CMAKE_C_COMPILER_ID MATCHES "Clang" AND ARCH_WIDTH EQUAL "32" AND NOT IOS AND NOT FREEBSD)
1199 find_library(ATOMIC atomic)
1200 if (ATOMIC_FOUND)
1201 list(APPEND EXTRA_LIBRARIES ${ATOMIC})
1202 endif()
1203 endif()
1204
1205 find_path(ZMQ_INCLUDE_PATH zmq.h)
1206 find_library(ZMQ_LIB zmq)
1207 find_library(PGM_LIBRARY pgm)
1208 find_library(NORM_LIBRARY norm)
1209 find_library(GSSAPI_LIBRARY gssapi_krb5)
1210 find_library(PROTOLIB_LIBRARY protolib)
1211 find_library(SODIUM_LIBRARY sodium)
1212 find_library(BSD_LIBRARY bsd)
1213 find_library(MD_LIBRARY md)
1214 find_library(PROTOKIT_LIBRARY protokit)
1215
1216 if(NOT ZMQ_INCLUDE_PATH)
1217 message(FATAL_ERROR "Could not find required header zmq.h")
1218 endif()
1219 if(NOT ZMQ_LIB)
1220 message(FATAL_ERROR "Could not find required libzmq")
1221 endif()
1222 include_directories(${ZMQ_INCLUDE_PATH})
1223 if(PGM_LIBRARY)
1224 set(ZMQ_LIB "${ZMQ_LIB};${PGM_LIBRARY}")
1225 endif()
1226 if(NORM_LIBRARY)
1227 set(ZMQ_LIB "${ZMQ_LIB};${NORM_LIBRARY}")
1228 endif()
1229 if(GSSAPI_LIBRARY)
1230 set(ZMQ_LIB "${ZMQ_LIB};${GSSAPI_LIBRARY}")
1231 endif()
1232 if(PROTOLIB_LIBRARY)
1233 set(ZMQ_LIB "${ZMQ_LIB};${PROTOLIB_LIBRARY}")
1234 endif()
1235 if(SODIUM_LIBRARY)
1236 message(STATUS "ZMQ_LIB: ${ZMQ_LIB};${SODIUM_LIBRARY}")
1237 set(ZMQ_LIB "${ZMQ_LIB};${SODIUM_LIBRARY}")
1238 find_path(SODIUM_INCLUDE_PATH sodium/crypto_verify_32.h)
1239 if (SODIUM_INCLUDE_PATH)
1240 message(STATUS "SODIUM_INCLUDE_PATH: ${SODIUM_INCLUDE_PATH}")
1241 include_directories(${SODIUM_INCLUDE_PATH})
1242 else()
1243 message(FATAL_ERROR "Could not find required sodium/crypto_verify_32.h")
1244 endif()
1245 endif()
1246 if(BSD_LIBRARY)
1247 set(ZMQ_LIB "${ZMQ_LIB};${BSD_LIBRARY}")
1248 endif()
1249 if(MD_LIBRARY)
1250 set(ZMQ_LIB "${ZMQ_LIB};${MD_LIBRARY}")
1251 endif()
1252 if(PROTOKIT_LIBRARY)
1253 set(ZMQ_LIB "${ZMQ_LIB};${PROTOKIT_LIBRARY}")
1254 endif()
1255
1256 include(external/supercop/functions.cmake) # place after setting flags and before src directory inclusion
1257 add_subdirectory(contrib)
1258 add_subdirectory(src)
1259
1260 find_package(PythonInterp)
1261 if(BUILD_TESTS)
1262 message(STATUS "Building tests")
1263 add_subdirectory(tests)
1264 else()
1265 message(STATUS "Not building tests")
1266 endif()
1267
1268 if(BUILD_DEBUG_UTILITIES)
1269 message(STATUS "Building debug utilities")
1270 else()
1271 message(STATUS "Not building debug utilities")
1272 endif()
1273
1274 if(BUILD_DOCUMENTATION)
1275 set(DOC_GRAPHS "YES" CACHE STRING "Create dependency graphs (needs graphviz)")
1276 set(DOC_FULLGRAPHS "NO" CACHE STRING "Create call/callee graphs (large)")
1277
1278 find_program(DOT_PATH dot)
1279
1280 if (DOT_PATH STREQUAL "DOT_PATH-NOTFOUND")
1281 message("Doxygen: graphviz not found - graphs disabled")
1282 set(DOC_GRAPHS "NO")
1283 endif()
1284
1285 find_package(Doxygen)
1286 if(DOXYGEN_FOUND)
1287 configure_file("cmake/Doxyfile.in" "Doxyfile" @ONLY)
1288 configure_file("cmake/Doxygen.extra.css.in" "Doxygen.extra.css" @ONLY)
1289 add_custom_target(doc
1290 ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
1291 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1292 COMMENT "Generating API documentation with Doxygen.." VERBATIM)
1293 endif()
1294 endif()
1295
1296 # when ON - will install libwallet_merged into "lib"
1297 option(BUILD_GUI_DEPS "Build GUI dependencies." OFF)
1298
1299 find_package(PythonInterp)
1300 find_program(iwyu_tool_path NAMES iwyu_tool.py iwyu_tool)
1301 if (iwyu_tool_path AND PYTHONINTERP_FOUND)
1302 add_custom_target(iwyu
1303 COMMAND "${PYTHON_EXECUTABLE}" "${iwyu_tool_path}" -p "${CMAKE_BINARY_DIR}" -- --no_fwd_decls
1304 COMMENT "Running include-what-you-use tool"
1305 VERBATIM
1306 )
1307 endif()