lokinet/CMakeLists.txt

311 lines
9.5 KiB
CMake
Raw Normal View History

2018-11-06 14:27:25 +00:00
# Lowest version - android ndk 3.6.0
cmake_minimum_required(VERSION 3.6.0)
2018-05-16 13:56:51 +00:00
2018-08-12 17:22:29 +00:00
set(PROJECT_NAME lokinet)
project(${PROJECT_NAME} C CXX)
2018-06-14 14:04:42 +00:00
# Core options
option(USE_AVX2 "enable avx2 code" )
2019-01-19 03:08:32 +00:00
option(USE_NETNS "enable networking namespace support. Linux only" )
2019-03-05 22:05:00 +00:00
option(AMD_RYZEN_HACK "hack for AMD Ryzen FPU bug (support FMA3 and FMA4 in FPU, but does not show in CPUID)" )
2019-04-19 18:36:32 +00:00
if (NOT MSVC)
2019-02-06 03:11:09 +00:00
option(STATIC_LINK_RUNTIME "link statically against compiler runtime, standard library and pthreads")
2019-04-19 18:36:32 +00:00
endif()
2018-12-13 23:16:44 +00:00
option(NON_PC_TARGET "non-pc target build: iphone, andriod, embedded non-i386 SBC, etc" )
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" )
option(WITH_SHARED "build shared library")
2019-01-12 01:19:24 +00:00
option(WITH_COVERAGE "generate coverage data")
2019-03-19 00:22:37 +00:00
option(WARNINGS_AS_ERRORS "treat all warnings as errors. turn off for development, on for release" OFF)
2018-06-15 13:42:49 +00:00
include(cmake/target_link_libraries_system.cmake)
include(cmake/add_import_library.cmake)
include(cmake/add_log_tag.cmake)
# Basic definitions
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 12:38:49 +00:00
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
2019-02-09 12:38:49 +00:00
endif()
if(MSVC_VERSION)
enable_language(ASM_MASM)
list(APPEND CMAKE_ASM_MASM_SOURCE_FILE_EXTENSIONS s)
add_definitions(/D_WIN32_WINNT=0x0600 /DNOMINMAX /DSODIUM_STATIC)
else()
enable_language(ASM)
endif(MSVC_VERSION)
include(cmake/solaris.cmake)
2018-12-03 14:39:30 +00:00
if(WIN32)
set(CMAKE_CXX_STANDARD 17)
2018-12-03 14:39:30 +00:00
else()
2019-01-13 14:10:38 +00:00
set(CMAKE_CXX_STANDARD 14)
endif(WIN32)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
2018-06-15 13:42:49 +00:00
if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND SHADOW)
2019-01-19 03:20:24 +00:00
message( FATAL_ERROR "shadow-framework is Linux only" )
endif(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND SHADOW)
2018-12-03 14:39:30 +00:00
set(ABSEIL_DIR vendor/abseil-cpp)
add_subdirectory(${ABSEIL_DIR})
include_directories(SYSTEM ${ABSEIL_DIR})
add_subdirectory(vendor/cxxopts)
2019-03-02 02:01:04 +00:00
add_subdirectory(vendor/nlohmann)
include_directories(SYSTEM vendor/cxxopts/include)
2019-03-02 02:01:04 +00:00
2019-02-01 21:26:49 +00:00
if (NOT WIN32)
add_compile_options(-fPIC)
2019-02-01 21:26:49 +00:00
endif(NOT WIN32)
2019-02-05 21:19:20 +00: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 20:54:50 +00:00
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wthread-safety)
endif()
include(cmake/coverage.cmake)
2019-01-12 01:19:24 +00:00
# these vars are set by the cmake toolchain spec
2018-09-25 08:31:29 +00: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 13:55:51 +00:00
if(DEBIAN)
add_definitions(-DDEBIAN)
elseif(NOT ANDROID AND NOT NON_PC_TARGET)
if (NOT USE_AVX2)
# Public binary releases
set(CRYPTO_FLAGS -march=nocona -mtune=core2 -mfpmath=sse)
else()
set(CRYPTO_FLAGS -march=haswell -mtune=native -mfpmath=sse)
endif()
endif()
2019-03-05 22:05:00 +00: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)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
2018-07-16 23:26:58 +00:00
# not supported on Solaris - system libraries are not available as archives
2019-02-06 03:11:09 +00:00
if(STATIC_LINK_RUNTIME)
if (NOT SOLARIS)
add_compile_options(-static -flto)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
link_libraries( -static -static-libstdc++ -pthread -flto )
else()
# this is messing with release builds
add_compile_options(-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0)
set(CMAKE_AR "gcc-ar")
set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> qcs <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_C_ARCHIVE_FINISH "true")
set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> qcs <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_CXX_ARCHIVE_FINISH "true")
link_libraries( -flto -static-libstdc++ -static-libgcc -static -Wl,--whole-archive -lpthread -Wl,--no-whole-archive )
endif()
else()
link_libraries( -static-libstdc++ -static-libgcc )
endif()
2019-02-06 03:11:09 +00:00
endif(STATIC_LINK_RUNTIME)
2018-06-15 13:42:49 +00:00
if(USE_NETNS)
add_definitions(-DNETNS=1)
else()
add_definitions(-DNETNS=0)
endif(USE_NETNS)
2018-06-06 17:02:57 +00:00
if(TESTNET)
add_definitions(-DTESTNET=1)
endif(TESTNET)
if(NOT DEBIAN AND NOT MSVC_VERSION)
set(OPTIMIZE_FLAGS -O3)
set(DEBUG_FLAGS -O0 -g3)
endif()
2018-06-15 13:42:49 +00:00
if(ASAN)
2019-04-02 09:03:53 +00:00
set(DEBUG_FLAGS ${DEBUG_FLAGS} -fsanitize=address -fno-omit-frame-pointer)
set(OPTIMIZE_FLAGS "-O0")
endif(ASAN)
2018-06-06 12:59:15 +00:00
if(SHADOW)
include(cmake/shadow.cmake)
endif(SHADOW)
2018-09-20 19:24:13 +00: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} )
2019-01-03 21:25:09 +00:00
link_libraries( ${DEBUG_FLAGS} )
endif(CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]")
2018-09-20 19:24:13 +00:00
2018-12-13 23:16:44 +00:00
# Add non-386 target-specific options here
if(NON_PC_TARGET)
2018-11-08 12:31:50 +00:00
add_definitions(-DRPI)
set(WITH_STATIC ON)
2018-12-13 23:16:44 +00:00
endif(NON_PC_TARGET)
2018-10-23 11:29:37 +00:00
2018-11-02 07:51:33 +00:00
add_compile_options(${OPTIMIZE_FLAGS} ${CRYPTO_FLAGS})
2018-06-15 13:42:49 +00:00
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)
add_definitions("-DGIT_REV=\"${GIT_VERSION}\"")
endif(NOT GIT_VERSION)
2018-08-12 17:22:29 +00:00
set(EXE lokinet)
2018-07-09 04:09:54 +00:00
set(EXE_SRC daemon/main.cpp)
2018-05-16 13:56:51 +00:00
2018-09-20 19:24:13 +00: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 11:46:19 +00:00
if(JEMALLOC)
set(MALLOC_LIB jemalloc)
endif(JEMALLOC)
2019-01-05 13:45:05 +00:00
# FS_LIB should resolve to nothing on all other platforms
# it is only required on win32 -rick
set(LIBS ${MALLOC_LIB} ${FS_LIB})
2018-11-06 14:06:09 +00:00
if(ANDROID)
list(APPEND LIBS log)
add_definitions(-DANDROID)
2019-04-08 14:27:55 +00:00
set(ANDROID_PLATFORM_SRC android/ifaddrs.c)
endif(ANDROID)
if(UNIX)
include(cmake/unix.cmake)
elseif(WIN32)
include(cmake/win32.cmake)
else()
message(FATAL_ERROR "What operating system _are_ you building on/for?")
endif()
2018-08-15 15:36:34 +00:00
set(LIBTUNTAP_SRC_BASE
2018-08-20 19:12:12 +00:00
${TT_ROOT}/tuntap.cpp
${TT_ROOT}/tuntap_log.cpp
${LIBTUNTAP_IMPL})
2018-09-24 10:25:29 +00:00
if(UNIX)
set(LIBTUNTAP_SRC ${TT_ROOT}/tuntap-unix.c ${LIBTUNTAP_SRC_BASE})
else()
set(LIBTUNTAP_SRC ${LIBTUNTAP_SRC_BASE})
endif()
if(NOT WIN32)
add_subdirectory(vendor)
endif()
add_subdirectory(crypto)
add_subdirectory(libutp)
add_subdirectory(llarp)
add_subdirectory(libabyss)
2019-02-19 09:43:17 +00:00
if (NOT WIN32)
add_executable(${ABYSS_EXE} ${ABYSS}/main.cpp)
2019-04-08 12:01:52 +00:00
target_link_libraries(${ABYSS_EXE} PUBLIC ${ABYSS_LIB} Threads::Threads)
elseif(NOT MSVC_VERSION)
2019-02-19 09:43:17 +00:00
add_executable(${ABYSS_EXE} ${ABYSS}/main.cpp llarp/win32/abyss.rc)
target_link_libraries(${ABYSS_EXE} PUBLIC ${ABYSS_LIB} ${STATIC_LIB} ws2_32)
else()
add_executable(${ABYSS_EXE} ${ABYSS}/main.cpp)
target_link_libraries(${ABYSS_EXE} PUBLIC ${ABYSS_LIB} ${STATIC_LIB} ws2_32)
2019-02-19 09:43:17 +00:00
endif(NOT WIN32)
2019-02-19 09:43:17 +00:00
target_include_directories(${ABYSS_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${ABYSS}/include")
target_include_directories(${ABYSS_EXE} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${ABYSS}/include")
2019-02-19 09:43:17 +00:00
# for freebsd
if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
target_include_directories(${ABYSS_LIB} SYSTEM PUBLIC /usr/local/include)
2019-02-19 09:43:17 +00:00
endif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
add_log_tag(${ABYSS_EXE})
add_log_tag(${ABYSS_LIB})
2019-03-11 13:01:43 +00:00
2018-06-06 12:59:15 +00:00
if(SHADOW)
2019-03-11 13:01:43 +00: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})
target_include_directories(${LOKINET_SHADOW} PUBLIC ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/llarp ${PROJECT_SOURCE_DIR}/crypto/include ${PROJECT_SOURCE_DIR}/vendor/cppbackport-master/lib)
2018-06-06 17:02:57 +00:00
else()
2019-01-19 03:08:32 +00:00
if(NOT WIN32)
add_executable(${EXE} ${EXE_SRC})
elseif(NOT MSVC_VERSION)
add_executable(${EXE} ${EXE_SRC} llarp/win32/version.rc)
else()
add_executable(${EXE} ${EXE_SRC})
2019-01-19 03:08:32 +00:00
endif(NOT WIN32)
2019-01-15 00:42:50 +00:00
2019-02-15 20:49:10 +00:00
add_log_tag(${EXE})
install(TARGETS ${EXE} RUNTIME DESTINATION bin)
2018-12-16 22:31:32 +00:00
if(WIN32)
2018-12-17 04:07:00 +00:00
install(PROGRAMS ${CMAKE_SOURCE_DIR}/lokinet-bootstrap.exe DESTINATION bin)
2018-12-16 22:31:32 +00:00
else()
2018-12-17 04:07:00 +00:00
install(PROGRAMS ${CMAKE_SOURCE_DIR}/lokinet-bootstrap DESTINATION bin)
2018-12-16 22:31:32 +00:00
endif()
2019-01-19 03:08:32 +00:00
2018-12-16 22:28:09 +00: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")
2018-10-19 11:41:36 +00:00
2019-03-11 13:01:43 +00:00
target_link_libraries(${EXE} PUBLIC ${EXE_LIBS})
2018-08-05 23:48:59 +00:00
if(ANDROID)
2018-09-03 11:41:04 +00:00
add_library(${ANDROID_LIB} SHARED jni/lokinet_android.cpp)
2019-04-08 15:54:19 +00:00
set_property(TARGET ${ANDROID_LIB} PROPERTY CXX_STANDARD 14)
2019-02-15 20:49:10 +00:00
add_log_tag(${ANDROID_LIB})
2018-11-06 14:06:09 +00:00
target_link_libraries(${ANDROID_LIB} ${STATIC_LIB} ${LIBS})
endif(ANDROID)
endif(SHADOW)
enable_testing()
if (NOT SHADOW AND NOT MSVC_VERSION)
add_subdirectory(test)
endif()