2018-11-06 15:27:25 +01:00
# Lowest version - android ndk 3.6.0
cmake_minimum_required ( VERSION 3.6.0 )
2018-05-16 15:56:51 +02:00
2019-06-26 23:39:29 +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 )
2019-04-19 20:24:33 +02:00
project ( ${ PROJECT_NAME } C CXX )
2018-06-14 16:04:42 +02:00
2019-02-09 17:16:00 +01:00
# Core options
2018-11-06 23:48:17 +01:00
option ( USE_AVX2 "enable avx2 code" )
2019-01-19 04:08:32 +01:00
option ( USE_NETNS "enable networking namespace support. Linux only" )
2019-03-05 23:05:00 +01:00
option ( AMD_RYZEN_HACK "hack for AMD Ryzen FPU bug (support FMA3 and FMA4 in FPU, but does not show in CPUID)" )
2019-08-27 14:05:37 +02:00
option ( NATIVE_BUILD "optimise for host system and FPU, may not be portable" )
2019-08-27 15:47:44 +02:00
option ( EMBEDDED_CFG "optimise for older hardware or embedded systems" )
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-02-06 04:11:09 +01:00
option ( STATIC_LINK_RUNTIME "link statically against compiler runtime, standard library and pthreads" )
2019-04-19 20:36:32 +02:00
endif ( )
2018-12-14 00:16:44 +01:00
option ( NON_PC_TARGET "non-pc target build: iphone, andriod, embedded non-i386 SBC, etc" )
2018-12-12 05:51:22 +01:00
option ( SHADOW "use shadow testing framework. linux only" )
option ( ASAN "use address sanitiser, if your system has it" )
option ( JEMALLOC "use jemalloc. Not required on BSD" )
option ( DEBIAN "build for debian" )
option ( TESTNET "testnet build" )
2019-01-08 23:30:21 +01:00
option ( WITH_SHARED "build shared library" )
2019-01-12 02:19:24 +01:00
option ( WITH_COVERAGE "generate coverage data" )
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 )
2018-06-15 15:42:49 +02:00
2019-04-25 01:27:31 +02:00
include ( cmake/target_link_libraries_system.cmake )
2019-04-30 01:48:43 +02:00
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-04-25 01:27:31 +02:00
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 ( )
2019-07-13 15:05:00 +02:00
list ( APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake" )
2019-02-09 17:16:00 +01:00
# Basic definitions
2019-04-30 01:48:43 +02:00
set ( LIB lokinet )
set ( SHARED_LIB ${ LIB } -shared )
set ( STATIC_LIB ${ LIB } -static )
set ( CRYPTOGRAPHY_LIB ${ LIB } -cryptography )
set ( UTIL_LIB ${ LIB } -util )
set ( PLATFORM_LIB ${ LIB } -platform )
set ( ANDROID_LIB ${ LIB } android )
set ( ABYSS libabyss )
set ( ABYSS_LIB abyss )
set ( ABYSS_EXE ${ ABYSS_LIB } -main )
get_filename_component ( TT_ROOT "vendor/libtuntap-master" ABSOLUTE )
add_definitions ( -D ${ CMAKE_SYSTEM_NAME } )
get_filename_component ( CORE_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/include" ABSOLUTE )
get_filename_component ( ABYSS_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/${ABYSS}/include" ABSOLUTE )
2019-02-09 17:16:00 +01:00
2019-04-30 01:48:43 +02:00
if ( MSVC_VERSION )
2019-04-19 20:24:33 +02:00
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 )
2019-04-19 20:24:33 +02:00
else ( )
enable_language ( ASM )
endif ( MSVC_VERSION )
2019-04-28 17:57:41 +02:00
include ( cmake/solaris.cmake )
2019-07-22 01:39:56 +02:00
include ( cmake/unix.cmake )
include ( cmake/win32.cmake )
2019-03-24 01:40:19 +01:00
2018-12-03 15:39:30 +01:00
if ( WIN32 )
2019-04-11 17:17:58 +02:00
set ( CMAKE_CXX_STANDARD 17 )
2018-12-03 15:39:30 +01:00
else ( )
2019-01-13 15:10:38 +01:00
set ( CMAKE_CXX_STANDARD 14 )
2018-12-12 05:51:22 +01:00
endif ( WIN32 )
2018-10-28 19:57:04 +01:00
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
2018-12-12 05:51:22 +01:00
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" )
2018-12-12 05:51:22 +01:00
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 ( )
if ( ASAN )
set ( DEBUG_FLAGS ${ DEBUG_FLAGS } -fsanitize=address -fno-omit-frame-pointer )
set ( OPTIMIZE_FLAGS "-O0" )
endif ( ASAN )
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 )
2019-04-30 01:48:43 +02:00
set ( ABSEIL_DIR vendor/abseil-cpp )
include_directories ( SYSTEM ${ ABSEIL_DIR } )
2019-04-19 20:24:33 +02:00
add_subdirectory ( vendor/cxxopts )
2019-03-02 03:01:04 +01:00
add_subdirectory ( vendor/nlohmann )
2019-04-25 01:27:31 +02:00
include_directories ( SYSTEM vendor/cxxopts/include )
2019-03-02 03:01:04 +01:00
2019-03-24 01:54:25 +01:00
if ( CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
add_compile_options ( -Wno-unknown-warning-option )
endif ( )
2019-04-19 20:24:33 +02:00
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
2019-03-24 01:54:25 +01:00
if ( CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
2019-03-12 11:13:33 +01:00
add_compile_options ( -Wthread-safety )
2019-03-03 21:51:47 +01:00
endif ( )
2019-04-28 17:57:41 +02:00
include ( cmake/coverage.cmake )
2019-01-12 02:19:24 +01:00
2018-12-12 05:51:22 +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 )
2019-04-30 01:48:43 +02:00
include ( cmake/cross_compile.cmake )
2018-12-12 05:51:22 +01:00
endif ( WOW64_CROSS_COMPILE OR WIN64_CROSS_COMPILE )
2018-11-19 14:39:35 +01:00
2018-08-09 15:55:51 +02:00
if ( DEBIAN )
add_definitions ( -DDEBIAN )
2019-02-09 17:16:00 +01:00
elseif ( NOT ANDROID AND NOT NON_PC_TARGET )
if ( NOT USE_AVX2 )
2019-03-21 03:13:14 +01:00
# Public binary releases
set ( CRYPTO_FLAGS -march=nocona -mtune=core2 -mfpmath=sse )
2019-02-09 17:16:00 +01:00
else ( )
set ( CRYPTO_FLAGS -march=haswell -mtune=native -mfpmath=sse )
endif ( )
endif ( )
2018-12-12 05:51:22 +01:00
2019-03-05 23:05:00 +01:00
# only needed if using AVX2
if ( AMD_RYZEN_HACK AND USE_AVX2 )
set ( CRYPTO_FLAGS -march=native -mfpmath=sse -mavx -mavx2 -mfma )
message ( WARNING "This option may be removed in a future release. Contact your computer manufacturer for updated ROMs or microcode patches." )
endif ( AMD_RYZEN_HACK AND USE_AVX2 )
2019-08-27 14:05:37 +02:00
if ( NATIVE_BUILD )
message ( WARNING "May fail at runtime if the floating-point unit on the target system does not implement required features, some platforms will check this for you" )
set ( CRYPTO_FLAGS -march=native -mfpmath=sse -mtune=native )
endif ( )
2019-08-27 15:47:44 +02:00
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 ( )
if ( NOT MSVC OR NOT MSVC_VERSION )
add_compile_options ( ${ OPTIMIZE_FLAGS } ${ CRYPTO_FLAGS } )
endif ( )
add_subdirectory ( ${ ABSEIL_DIR } )
add_subdirectory ( vendor/gtest )
2019-08-27 03:40:37 +02:00
2019-08-29 17:22:58 +02:00
if ( FS_LIB STREQUAL "cppbackport" )
add_subdirectory ( vendor )
endif ( )
2018-10-28 19:57:04 +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
2018-11-19 14:39:35 +01:00
if ( USE_NETNS )
add_definitions ( -DNETNS=1 )
else ( )
add_definitions ( -DNETNS=0 )
2018-12-12 05:51:22 +01:00
endif ( USE_NETNS )
2018-06-06 19:02:57 +02:00
2018-10-21 16:19:49 +02:00
if ( TESTNET )
add_definitions ( -DTESTNET=1 )
2018-12-12 05:51:22 +01:00
endif ( TESTNET )
2018-10-21 16:19:49 +02:00
2018-06-06 14:59:15 +02:00
if ( SHADOW )
2019-04-28 17:57:41 +02:00
include ( cmake/shadow.cmake )
2018-12-12 05:51:22 +01:00
endif ( SHADOW )
2018-06-01 19:47:37 +02:00
2018-05-28 15:49:44 +02:00
if ( NOT GIT_VERSION )
2019-04-19 20:24:33 +02:00
exec_program ( "git" ${ CMAKE_CURRENT_SOURCE_DIR } ARGS "rev-parse --short HEAD" OUTPUT_VARIABLE GIT_VERSION_UNSTRIP )
string ( STRIP "${GIT_VERSION_UNSTRIP}" GIT_VERSION )
2018-12-12 05:51:22 +01:00
endif ( NOT GIT_VERSION )
2018-05-28 15:49:44 +02:00
2019-05-03 16:00:12 +02:00
string ( REGEX REPLACE ^fatal.* $ nogit GIT_VERSION_REAL "${GIT_VERSION}" )
add_definitions ( "-DGIT_REV=\" ${ GIT_VERSION_REAL } \"")
2018-08-12 19:22:29 +02:00
set ( EXE lokinet )
2018-07-09 06:09:54 +02:00
set ( EXE_SRC daemon/main.cpp )
2018-05-16 15:56:51 +02: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 )
2018-12-12 05:51:22 +01:00
endif ( JEMALLOC )
2018-09-19 14:22:34 +02:00
2018-11-06 15:06:09 +01:00
if ( ANDROID )
2019-02-09 17:16:00 +01:00
list ( APPEND LIBS log )
2018-12-12 05:51:22 +01:00
add_definitions ( -DANDROID )
2019-04-08 16:27:55 +02:00
set ( ANDROID_PLATFORM_SRC android/ifaddrs.c )
2018-12-12 05:51:22 +01:00
endif ( ANDROID )
2018-11-19 14:39:35 +01:00
2018-08-15 17:36:34 +02:00
set ( LIBTUNTAP_SRC_BASE
2018-08-20 21:12:12 +02:00
$ { T T _ R O O T } / t u n t a p . c p p
2018-08-08 21:37:33 +02:00
$ { T T _ R O O T } / t u n t a p _ l o g . c p p
$ { L I B T U N T A P _ I M P L } )
2018-09-24 12:25:29 +02:00
2019-02-09 17:16:00 +01:00
if ( UNIX )
set ( LIBTUNTAP_SRC ${ TT_ROOT } /tuntap-unix.c ${ LIBTUNTAP_SRC_BASE } )
2018-09-29 10:16:54 +02:00
else ( )
2018-12-12 05:51:22 +01:00
set ( LIBTUNTAP_SRC ${ LIBTUNTAP_SRC_BASE } )
2019-04-30 01:48:43 +02:00
endif ( )
2018-07-13 15:14:42 +02:00
2019-07-13 15:05:00 +02:00
set ( LIBS ${ MALLOC_LIB } ${ FS_LIB } ${ LIBUV_LIBRARY } )
2019-06-24 17:51:58 +02:00
2019-02-09 17:16:00 +01:00
add_subdirectory ( crypto )
add_subdirectory ( llarp )
2019-04-30 01:48:43 +02:00
add_subdirectory ( libabyss )
2019-02-15 23:19:19 +01:00
2019-02-19 10:43:17 +01:00
if ( NOT WIN32 )
add_executable ( ${ ABYSS_EXE } ${ ABYSS } /main.cpp )
2019-06-24 17:51:58 +02:00
target_link_libraries ( ${ ABYSS_EXE } PUBLIC ${ ABYSS_LIB } Threads::Threads ${ LIBS } )
2019-04-19 20:24:33 +02:00
elseif ( NOT MSVC_VERSION )
2019-02-19 10:43:17 +01:00
add_executable ( ${ ABYSS_EXE } ${ ABYSS } /main.cpp llarp/win32/abyss.rc )
target_link_libraries ( ${ ABYSS_EXE } PUBLIC ${ ABYSS_LIB } ${ STATIC_LIB } ws2_32 )
2019-04-19 20:24:33 +02:00
else ( )
add_executable ( ${ ABYSS_EXE } ${ ABYSS } /main.cpp )
target_link_libraries ( ${ ABYSS_EXE } PUBLIC ${ ABYSS_LIB } ${ STATIC_LIB } ws2_32 )
2019-02-19 10:43:17 +01:00
endif ( NOT WIN32 )
2019-04-19 20:24:33 +02:00
2019-06-18 06:03:43 +02:00
# Why does abyss not inherit the existing include folders?
target_include_directories ( ${ ABYSS_LIB } PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${ABYSS}/include" llarp vendor/nlohmann/include include crypto/include )
target_include_directories ( ${ ABYSS_EXE } PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${ABYSS}/include" llarp vendor/nlohmann/include include crypto/include )
2019-04-19 20:24:33 +02:00
2019-02-19 10:43:17 +01:00
# for freebsd
if ( ${ CMAKE_SYSTEM_NAME } MATCHES "FreeBSD" )
2019-06-10 18:38:24 +02:00
target_link_directories ( ${ ABYSS_EXE } PRIVATE /usr/local/lib )
2019-04-25 01:27:31 +02:00
target_include_directories ( ${ ABYSS_LIB } SYSTEM PUBLIC /usr/local/include )
2019-02-19 10:43:17 +01:00
endif ( ${ CMAKE_SYSTEM_NAME } MATCHES "FreeBSD" )
add_log_tag ( ${ ABYSS_EXE } )
add_log_tag ( ${ ABYSS_LIB } )
2019-02-15 23:19:19 +01:00
2019-03-11 14:01:43 +01:00
2018-06-06 14:59:15 +02:00
if ( SHADOW )
2019-03-11 14:01:43 +01:00
set ( LOKINET_SHADOW shadow-plugin- ${ SHARED_LIB } )
set ( LOKINET_SHADOW_LIBS ${ SHARED_LIB } )
add_shadow_plugin ( ${ LOKINET_SHADOW } ${ EXE_SRC } )
target_link_libraries ( ${ LOKINET_SHADOW } ${ LOKINET_SHADOW_LIBS } )
2019-07-01 11:33:03 +02:00
target_include_directories ( ${ LOKINET_SHADOW } PUBLIC ${ PROJECT_SOURCE_DIR } /include ${ PROJECT_SOURCE_DIR } /llarp ${ PROJECT_SOURCE_DIR } /crypto/include )
2018-06-06 19:02:57 +02:00
else ( )
2019-01-19 04:08:32 +01:00
if ( NOT WIN32 )
add_executable ( ${ EXE } ${ EXE_SRC } )
2019-08-12 23:47:30 +02:00
add_executable ( lokinet-rcutil daemon/rcutil.cpp )
2019-04-19 20:24:33 +02:00
elseif ( NOT MSVC_VERSION )
2019-02-02 09:51:33 +01:00
add_executable ( ${ EXE } ${ EXE_SRC } llarp/win32/version.rc )
2019-08-12 23:47:30 +02:00
add_executable ( lokinet-rcutil daemon/rcutil.cpp llarp/win32/version.rc )
2019-04-25 01:27:31 +02:00
else ( )
2019-04-19 20:24:33 +02:00
add_executable ( ${ EXE } ${ EXE_SRC } )
2019-08-12 23:47:30 +02:00
add_executable ( lokinet-rcutil daemon/rcutil.cpp )
2019-01-19 04:08:32 +01:00
endif ( NOT WIN32 )
2019-01-15 01:42:50 +01:00
2019-02-15 21:49:10 +01:00
add_log_tag ( ${ EXE } )
2019-08-12 23:47:30 +02:00
add_log_tag ( lokinet-rcutil )
2019-02-15 21:49:10 +01:00
2018-12-12 05:51:22 +01:00
install ( TARGETS ${ EXE } RUNTIME DESTINATION bin )
2019-08-12 23:47:30 +02:00
install ( TARGETS lokinet-rcutil RUNTIME DESTINATION bin )
2018-12-16 23:31:32 +01:00
if ( WIN32 )
2018-12-17 05:07:00 +01:00
install ( PROGRAMS ${ CMAKE_SOURCE_DIR } /lokinet-bootstrap.exe DESTINATION bin )
2018-12-16 23:31:32 +01:00
else ( )
2018-12-17 05:07:00 +01:00
install ( PROGRAMS ${ CMAKE_SOURCE_DIR } /lokinet-bootstrap DESTINATION bin )
2018-12-16 23:31:32 +01:00
endif ( )
2019-01-19 04:08:32 +01:00
2018-12-16 23:28:09 +01:00
if ( ${ CMAKE_SYSTEM_NAME } MATCHES "Linux" )
install ( CODE "execute_process(COMMAND setcap cap_net_admin,cap_net_bind_service=+eip ${CMAKE_INSTALL_PREFIX}/bin/lokinet)" )
endif ( ${ CMAKE_SYSTEM_NAME } MATCHES "Linux" )
2019-06-10 18:36:00 +02:00
if ( ${ CMAKE_SYSTEM_NAME } MATCHES "FreeBSD" )
target_link_directories ( ${ EXE } PRIVATE /usr/local/lib )
2019-08-12 23:47:30 +02:00
target_link_directories ( lokinet-rcutil PRIVATE /usr/local/lib )
2019-06-10 18:36:00 +02:00
endif ( )
2019-06-24 17:51:58 +02:00
target_link_libraries ( ${ EXE } PUBLIC ${ EXE_LIBS } ${ LIBS } )
2019-08-12 23:47:30 +02:00
target_link_libraries ( lokinet-rcutil PUBLIC ${ EXE_LIBS } ${ LIBS } )
2019-01-08 23:30:21 +01:00
2018-08-06 01:48:59 +02:00
if ( ANDROID )
2018-09-03 13:41:04 +02:00
add_library ( ${ ANDROID_LIB } SHARED jni/lokinet_android.cpp )
2019-04-08 17:54:19 +02:00
set_property ( TARGET ${ ANDROID_LIB } PROPERTY CXX_STANDARD 14 )
2019-02-15 21:49:10 +01:00
add_log_tag ( ${ ANDROID_LIB } )
2018-11-06 15:06:09 +01:00
target_link_libraries ( ${ ANDROID_LIB } ${ STATIC_LIB } ${ LIBS } )
2018-12-12 05:51:22 +01:00
endif ( ANDROID )
2019-01-23 00:58:24 +01:00
endif ( SHADOW )
2019-02-09 17:16:00 +01:00
enable_testing ( )
2019-07-17 11:08:04 +02:00
if ( NOT SHADOW )
2019-02-09 17:16:00 +01:00
add_subdirectory ( test )
endif ( )