lokinet/CMakeLists.txt

300 lines
8.8 KiB
CMake
Raw Normal View History

cmake_minimum_required(VERSION 3.5.1) # xenial's cmake version
2018-05-16 15:56:51 +02:00
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
endif()
2018-08-12 19:22:29 +02:00
set(PROJECT_NAME lokinet)
project(${PROJECT_NAME} C CXX)
2018-06-14 16:04:42 +02:00
# Core options
2019-11-05 12:45:49 +01:00
option(USE_AVX2 "enable avx2 code" OFF)
option(USE_NETNS "enable networking namespace support. Linux only" OFF)
2019-11-05 13:11:05 +01:00
option(NATIVE_BUILD "optimise for host system and FPU" ON)
2019-11-05 12:45:49 +01:00
option(EMBEDDED_CFG "optimise for older hardware or embedded systems" OFF)
2019-04-19 20:36:32 +02:00
if (NOT MSVC)
2019-09-18 22:19:13 +02:00
option(STATIC_LINK "link statically against dependencies" OFF)
2019-11-05 12:45:49 +01:00
option(STATIC_LINK_RUNTIME "link statically against compiler runtime, standard library and pthreads" OFF)
endif()
option(SHADOW "use shadow testing framework. linux only" OFF)
option(XSAN "use sanitiser, if your system has it" OFF)
option(JEMALLOC "use jemalloc. Not required on BSD" OFF)
option(DEBIAN "build for debian" OFF)
option(TESTNET "testnet build" OFF)
option(WITH_SHARED "build shared library" OFF)
option(WITH_COVERAGE "generate coverage data" OFF)
2019-07-29 14:14:35 +02:00
option(USE_SHELLHOOKS "enable shell hooks on compile time (dangerous)" OFF)
2019-03-19 01:22:37 +01:00
option(WARNINGS_AS_ERRORS "treat all warnings as errors. turn off for development, on for release" OFF)
2019-11-05 12:45:49 +01:00
option(TRACY_ROOT "include tracy profiler source" OFF)
option(WITH_TESTS "build unit tests" ON)
option(WITH_SYSTEMD "enable systemd integration for sd_notify" OFF)
2018-06-15 15:42:49 +02:00
include(cmake/target_link_libraries_system.cmake)
include(cmake/add_import_library.cmake)
include(cmake/add_log_tag.cmake)
2019-08-29 15:59:04 +02:00
include(cmake/libatomic.cmake)
2019-09-18 22:19:13 +02:00
if (STATIC_LINK AND STATIC_LINK_RUNTIME)
message(FATAL "Cannot set both STATIC_LINK and STATIC_LINK_RUNTIME")
endif()
if (STATIC_LINK)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
message(STATUS "setting static library suffix search")
endif()
2019-07-13 15:05:00 +02:00
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
# No in-source building
include(MacroEnsureOutOfSourceBuild)
macro_ensure_out_of_source_build("${PROJECT_NAME} requires an out-of-source build. Create a build directory and run 'cmake ${CMAKE_SOURCE_DIR} [options]'.")
2019-09-30 10:59:34 +02:00
include(cmake/basic_definitions.cmake)
if(MSVC_VERSION)
enable_language(ASM_MASM)
list(APPEND CMAKE_ASM_MASM_SOURCE_FILE_EXTENSIONS s)
2019-07-17 20:57:15 +02:00
add_definitions(-D_WIN32_WINNT=0x0600 -DNOMINMAX -DSODIUM_STATIC)
else()
enable_language(ASM)
endif(MSVC_VERSION)
include(cmake/solaris.cmake)
2019-07-22 01:39:56 +02:00
include(cmake/unix.cmake)
include(cmake/win32.cmake)
2019-11-05 12:58:40 +01:00
# try detecting the target arch and set the flags needed here
if(NOT WIN32)
2019-11-05 13:01:14 +01:00
if(IOS OR ANDROID)
2019-11-05 12:58:40 +01:00
set(NON_PC_TARGET ON)
2019-11-05 13:01:14 +01:00
else()
include(TargetArch)
target_architecture(COMPILE_ARCH)
if(COMPILE_ARCH MATCHES i386 OR COMPILE_ARCH MATCHES x86_64)
set(NON_PC_TARGET OFF)
else()
set(NON_PC_TARGET ON)
endif()
2019-11-05 12:58:40 +01:00
endif()
endif()
2019-10-02 17:40:26 +02:00
if(WIN32)
set(CMAKE_CXX_STANDARD 17)
else()
set(CMAKE_CXX_STANDARD 14)
endif(WIN32)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
2019-08-02 11:27:27 +02:00
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
2018-06-15 15:42:49 +02:00
2019-10-17 06:14:03 +02:00
# this is messing with release builds
add_compile_options(-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0)
if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND SHADOW)
2019-01-19 04:20:24 +01:00
message( FATAL_ERROR "shadow-framework is Linux only" )
endif(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND SHADOW)
2018-12-03 15:39:30 +01:00
2019-05-18 17:34:03 +02:00
if(NOT DEBIAN AND NOT MSVC_VERSION)
set(OPTIMIZE_FLAGS -O3)
set(DEBUG_FLAGS -O0 -g3)
endif()
2019-09-22 16:32:03 +02:00
if(XSAN)
set(DEBUG_FLAGS ${DEBUG_FLAGS} "-fsanitize=${XSAN}" -fno-omit-frame-pointer)
2019-05-18 17:34:03 +02:00
set(OPTIMIZE_FLAGS "-O0")
2019-09-22 16:32:03 +02:00
endif(XSAN)
2019-05-18 17:34:03 +02:00
if(CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]")
set(OPTIMIZE_FLAGS "")
add_definitions(-DLOKINET_DEBUG=1)
set(CRYPTO_FLAGS "")
add_compile_options( ${DEBUG_FLAGS} )
link_libraries( ${DEBUG_FLAGS} )
endif(CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]")
# Add non-386 target-specific options here
if(NON_PC_TARGET)
add_definitions(-DRPI)
set(WITH_STATIC ON)
endif(NON_PC_TARGET)
2019-07-12 15:06:59 +02:00
if(WITH_SHELLHOOKS)
add_definitions(-DENABLE_SHELLHOOKS)
endif(WITH_SHELLHOOKS)
2019-08-27 01:10:48 +02:00
# Always build PIC
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(TRACY_ROOT)
include_directories(${TRACY_ROOT})
add_definitions(-DTRACY_ENABLE)
endif()
2019-03-02 03:01:04 +01:00
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wno-unknown-warning-option)
endif()
if (NOT MSVC_VERSION)
add_compile_options(-Wall -Wextra -Wno-unknown-pragmas)
# vla are evil
add_compile_options(-Wvla)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fpermissive>)
add_compile_options(-Wno-unused-function -Wno-deprecated-declarations -Wno-unknown-pragmas)
endif()
2018-09-07 22:54:50 +02:00
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wthread-safety)
endif()
include(cmake/coverage.cmake)
2019-01-12 02:19:24 +01:00
# these vars are set by the cmake toolchain spec
2018-09-25 10:31:29 +02:00
if (WOW64_CROSS_COMPILE OR WIN64_CROSS_COMPILE)
include(cmake/cross_compile.cmake)
endif(WOW64_CROSS_COMPILE OR WIN64_CROSS_COMPILE)
2018-08-09 15:55:51 +02:00
if(DEBIAN)
2019-11-05 12:45:49 +01:00
add_definitions(-DDEBIAN)
elseif(NATIVE_BUILD)
2020-02-07 05:22:27 +01:00
if(CMAKE_SYSTEM_PROCESSOR STREQUAL ppc64le)
set(CRYPTO_FLAGS -mcpu=native -mtune=native)
else()
set(CRYPTO_FLAGS -march=native -mtune=native)
endif()
elseif(NOT NON_PC_TARGET)
2019-11-05 12:45:49 +01:00
if (USE_AVX2)
set(CRYPTO_FLAGS -march=haswell -mtune=haswell -mfpmath=sse)
2019-11-05 12:45:49 +01:00
else()
# Public binary releases
set(CRYPTO_FLAGS -march=nocona -mtune=haswell -mfpmath=sse)
2019-10-30 21:55:12 +01:00
endif()
endif()
if(EMBEDDED_CFG)
message(WARNING "This configuration is optimised for older hardware and/or constrained node operation, may result in poor performance on desktop systems")
message(WARNING "For deployment on such systems, all external code (currently, libuv) must also be compiled for the target!")
set(CRYPTO_FLAGS -march=i486 -mtune=i486)
endif()
2019-11-05 13:11:05 +01:00
add_compile_options(${OPTIMIZE_FLAGS} ${CRYPTO_FLAGS})
2019-11-05 13:26:44 +01:00
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
2018-07-17 01:26:58 +02:00
2019-09-18 22:19:13 +02:00
include(cmake/static_link_runtime.cmake)
2019-08-17 13:33:07 +02:00
include(cmake/static_link.cmake)
2018-06-15 15:42:49 +02:00
if(USE_NETNS)
add_definitions(-DNETNS=1)
else()
add_definitions(-DNETNS=0)
endif(USE_NETNS)
2018-06-06 19:02:57 +02:00
if(TESTNET)
add_definitions(-DTESTNET=1)
# 5 times slower than realtime
add_definitions(-DTESTNET_SPEED=5)
endif(TESTNET)
2018-06-06 14:59:15 +02:00
if(SHADOW)
include(cmake/shadow.cmake)
endif(SHADOW)
2019-12-14 02:20:45 +01:00
unset(GIT_VERSION)
unset(GIT_VERSION_REAL)
if(NOT GIT_VERSION)
exec_program("git" ${CMAKE_CURRENT_SOURCE_DIR} ARGS "rev-parse --short HEAD" OUTPUT_VARIABLE GIT_VERSION_UNSTRIP)
string(STRIP "${GIT_VERSION_UNSTRIP}" GIT_VERSION)
endif(NOT GIT_VERSION)
string(REGEX REPLACE "^fatal.*$" nogit GIT_VERSION_REAL "${GIT_VERSION}")
2019-11-05 13:28:08 +01:00
2018-09-20 21:24:13 +02:00
# HeapAlloc(2) on Windows was significantly revamped in 2009
# but the old algorithm isn't too bad either
# this is _the_ system allocator on BSD UNIX
# openbsd replaced it with a secure/randomised malloc not too
# long ago
2018-09-06 13:46:19 +02:00
if(JEMALLOC)
set(MALLOC_LIB jemalloc)
endif(JEMALLOC)
if(WITH_SYSTEMD)
pkg_check_modules(SD REQUIRED libsystemd)
add_definitions(-DWITH_SYSTEMD)
include_directories(${SD_INCLUDE_DIRS})
set(SD_LIBS ${SD_LDFLAGS})
endif()
option(SUBMODULE_CHECK "Enables checking that vendored library submodules are up to date" ON)
if(SUBMODULE_CHECK)
find_package(Git)
if(GIT_FOUND)
function(check_submodule relative_path)
execute_process(COMMAND git rev-parse "HEAD" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${relative_path} OUTPUT_VARIABLE localHead)
execute_process(COMMAND git rev-parse "HEAD:${relative_path}" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} OUTPUT_VARIABLE checkedHead)
string(COMPARE EQUAL "${localHead}" "${checkedHead}" upToDate)
if (upToDate)
message(STATUS "Submodule '${relative_path}' is up-to-date")
else()
message(FATAL_ERROR "Submodule '${relative_path}' is not up-to-date. Please update with\ngit submodule update --init --recursive\nor run cmake with -DSUBMODULE_CHECK=OFF")
endif()
endfunction ()
message(STATUS "Checking submodules")
check_submodule(external/nlohmann)
2020-02-13 19:41:53 +01:00
check_submodule(external/googletest)
2020-02-13 20:18:17 +01:00
check_submodule(external/cxxopts)
check_submodule(external/ghc-filesystem)
check_submodule(external/optional-lite)
check_submodule(external/date)
endif()
endif()
2019-11-05 13:29:43 +01:00
if(WITH_TESTS)
2020-02-13 19:41:53 +01:00
add_subdirectory(external/googletest EXCLUDE_FROM_ALL)
2019-11-05 13:29:43 +01:00
endif()
set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(external/nlohmann EXCLUDE_FROM_ALL)
2020-02-13 20:18:17 +01:00
add_subdirectory(external/cxxopts)
add_subdirectory(external/ghc-filesystem)
add_subdirectory(external/optional-lite)
add_subdirectory(external/date)
2019-11-05 13:28:08 +01:00
2018-11-06 15:06:09 +01:00
if(ANDROID)
list(APPEND LIBS log)
add_definitions(-DANDROID)
2019-04-08 16:27:55 +02:00
set(ANDROID_PLATFORM_SRC android/ifaddrs.c)
endif(ANDROID)
2019-12-12 20:58:46 +01:00
set(LIBS ${MALLOC_LIB} ${LIBUV_LIBRARY} ${SD_LIBS})
if(TRACY_ROOT)
list(APPEND LIBS -ldl)
endif()
2019-06-24 17:51:58 +02:00
add_subdirectory(crypto)
add_subdirectory(llarp)
add_subdirectory(libabyss)
add_subdirectory(daemon)
2019-11-05 12:45:49 +01:00
if(WITH_TESTS)
enable_testing()
endif()
2019-07-17 11:08:04 +02:00
if (NOT SHADOW)
2019-11-05 12:45:49 +01:00
if(WITH_TESTS)
add_subdirectory(test)
endif()
2018-08-06 01:48:59 +02:00
if(ANDROID)
add_subdirectory(jni)
endif(ANDROID)
endif()