tile/CMakeLists.txt

345 lines
12 KiB
CMake
Raw Normal View History

2024-06-16 21:07:45 +08:00
cmake_minimum_required(VERSION 3.5)
2024-06-11 19:13:30 +08:00
set(tile_VERSION_MAJOR 0)
set(tile_VERSION_MINOR 1)
set(tile_VERSION_PATCH 0)
2024-08-10 02:59:47 +08:00
set(tile_VERSION_BUILD $ENV{CI_STEP_NUMBER})
2024-07-27 12:53:30 +08:00
if (NOT tile_VERSION_BUILD)
set(tile_VERSION_BUILD "0")
endif()
2024-06-11 19:13:30 +08:00
project(
tile
2024-08-13 23:09:49 +08:00
VERSION "${tile_VERSION_MAJOR}.${tile_VERSION_MINOR}.${tile_VERSION_PATCH}"
2024-06-13 15:10:38 +08:00
LANGUAGES C CXX)
2024-06-11 19:13:30 +08:00
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
option(TILE_BUILD_TESTS "Build tests" OFF)
option(TILE_BUILD_BENCHMARKS "Build tests" OFF)
option(TILE_WITH_OPENSSL "Build with openssl" OFF)
option(TILE_BUILD_SHARED "Build shared library" ON)
option(TILE_BUILD_STATIC "Build static library" ON)
2024-06-17 15:28:32 +08:00
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
2024-06-16 21:07:45 +08:00
set(TILE_BUILD_TESTS ON)
set(TILE_BUILD_BENCHMARKS ON)
endif()
2024-06-11 19:13:30 +08:00
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
2024-06-17 14:22:34 +08:00
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gz")
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -gz")
2024-06-11 19:13:30 +08:00
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static") set(CMAKE_C_FLAGS
2024-06-11 23:34:48 +08:00
# "${CMAKE_CXX_FLAGS} -static") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}
# -fsanitize=address ") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address
# ")
2024-06-11 19:13:30 +08:00
set(WHOLE_ARCHIVE_PREFIX "-Wl,-force_load")
# set(NO_WHOLE_ARCHIVE_PREFIX "")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
2024-06-17 14:22:34 +08:00
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gz")
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -gz")
2024-06-11 19:13:30 +08:00
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static") set(CMAKE_C_FLAGS
# "${CMAKE_CXX_FLAGS} -static")
set(WHOLE_ARCHIVE_PREFIX "-Wl,-force_load,")
# set(NO_WHOLE_ARCHIVE_PREFIX "")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
2024-07-03 18:30:06 +08:00
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gz")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -gz")
2024-06-14 16:19:06 +08:00
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
# set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
2024-06-11 19:13:30 +08:00
2024-06-16 00:46:50 +08:00
set(STATIC_BINARY_FLAGS "-static-libgcc -static-libstdc++")
2024-06-11 19:13:30 +08:00
set(WHOLE_ARCHIVE_PREFIX "-Wl,--whole-archive")
set(WHOLE_ARCHIVE_SUFFIX "-Wl,--no-whole-archive")
endif()
# extern int getifaddrs(struct ifaddrs **ifap); extern void freeifaddrs(struct
# ifaddrs *ifa);
include(CheckSymbolExists)
include(cmake/BuildInfo.cmake)
check_symbol_exists("getifaddrs" "ifaddrs.h" TILE_HAVE_GETIFADDRS)
check_symbol_exists("freeifaddrs" "ifaddrs.h" TILE_HAVE_FREEIFADDRS)
2024-06-17 14:40:36 +08:00
find_package(Git REQUIRED)
2024-06-11 19:13:30 +08:00
get_git_commit_hash(GIT_COMMIT_HASH)
get_git_commit_date(GIT_COMMIT_DATE)
get_git_commit_subject(GIT_COMMIT_SUBJECT)
2024-06-16 00:46:50 +08:00
set(THIRD_PARTY_INCLUDE_DIRS
2024-06-20 21:17:12 +08:00
# "third_party/json" "third_party/inja"
"third_party/sigslot"
2024-06-16 00:46:50 +08:00
"${CMAKE_CURRENT_BINARY_DIR}/third_party/gflags/include")
2024-06-14 12:51:04 +08:00
include_directories(${THIRD_PARTY_INCLUDE_DIRS})
2024-06-11 19:13:30 +08:00
add_subdirectory("third_party/zlib")
2024-06-20 21:17:12 +08:00
add_subdirectory("third_party/jsoncpp")
2024-06-11 19:13:30 +08:00
add_subdirectory("third_party/fmt")
add_subdirectory("third_party/googletest")
add_subdirectory("third_party/gflags")
set(GFLAGS_USE_TARGET_NAMESPACE ON)
set(gflags_DIR "${CMAKE_CURRENT_BINARY_DIR}/third_party/gflags")
add_subdirectory("third_party/glog")
2024-08-05 11:32:59 +08:00
# add_subdirectory("third_party/context")
2024-06-11 19:13:30 +08:00
set(CURL_DISABLE_TESTS ON)
2024-06-16 00:46:50 +08:00
set(CURL_CA_PATH
"none"
CACHE STRING "" FORCE)
2024-06-11 19:13:30 +08:00
set(CURL_ENABLE_SSL
OFF
CACHE BOOL "" FORCE)
set(USE_LIBIDN2
OFF
CACHE BOOL "" FORCE)
set(CURL_USE_LIBPSL
OFF
CACHE BOOL "" FORCE)
set(CURL_USE_LIBSSH
OFF
CACHE BOOL "" FORCE)
set(CURL_USE_LIBSSH2
OFF
CACHE BOOL "" FORCE)
set(CURL_USE_GSSAPI
OFF
CACHE BOOL "" FORCE)
set(CURL_USE_RTMP
OFF
CACHE BOOL "" FORCE)
set(USE_OPENSSL_QUIC
OFF
CACHE BOOL "" FORCE)
set(USE_MSH3
OFF
CACHE BOOL "" FORCE)
set(CURL_DISABLE_LDAP
ON
CACHE BOOL "" FORCE)
set(ZLIB_FOUND ON)
set(ZLIB_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/zlib")
set(ZLIB_LIBRARIES zlib)
add_subdirectory("third_party/curl")
# add_subdirectory("third_party/date")
if(TILE_WITH_OPENSSL)
add_subdirectory("third_party/openssl-cmake")
list(APPEND TILE_LINK_LIBS ssl crypto)
endif()
configure_file("tile/base/config.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/tile/base/config.h" @ONLY)
# file(GLOB zlib_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/zlib/zlib/*.c")
set(TILE_SRCS
"tile/base/buffer/builtin_buffer_block.cc"
"tile/base/buffer/compression_output_stream.cc"
"tile/base/buffer/polymorphic_buffer.cc"
"tile/base/buffer.cc"
"tile/base/chrono.cc"
"tile/base/compression.cc"
"tile/base/compression/compression.cc"
"tile/base/compression/gzip.cc"
"tile/base/compression/util.cc"
"tile/base/demangle.cc"
"tile/base/encoding/base64.cc"
"tile/base/encoding/detail/hex_chars.cc"
"tile/base/encoding/hex.cc"
"tile/base/encoding/percent.cc"
2024-07-03 18:30:06 +08:00
"tile/base/exposed_var.cc"
2024-06-11 19:13:30 +08:00
"tile/base/internal/background_task_host.cc"
"tile/base/internal/background_task_host.h"
"tile/base/internal/case_insensitive_hash_map.h"
"tile/base/internal/curl.cc"
"tile/base/internal/curl.h"
"tile/base/internal/early_init.h"
"tile/base/internal/index_alloc.cc"
"tile/base/internal/logging.cc"
"tile/base/internal/logging.h"
"tile/base/internal/thread_pool.cc"
"tile/base/internal/time_keeper.cc"
"tile/base/internal/time_keeper.h"
"tile/base/net/endpoint.cc"
"tile/base/object_pool/disabled.cc"
"tile/base/object_pool/global.cc"
"tile/base/object_pool/thread_local.cc"
"tile/base/option.cc"
"tile/base/option/gflags_provider.cc"
"tile/base/option/json_parser.cc"
"tile/base/option/key.cc"
"tile/base/option/option_provider.cc"
"tile/base/option/option_service.cc"
"tile/base/slice.cc"
"tile/base/status.cc"
"tile/base/string.cc"
"tile/base/thread/cond_var.cc"
"tile/base/thread/latch.cc"
"tile/base/thread/mutex.cc"
"tile/base/thread/rw_mutex.cc"
"tile/base/thread/scoped_lock.cc"
"tile/base/thread/spinlock.cc"
2024-07-12 17:11:19 +08:00
"tile/base/memory_barrier.cc"
2024-08-05 11:32:59 +08:00
# "tile/fiber/detail/fiber.cc"
2024-06-11 19:13:30 +08:00
"tile/io/detail/eintr_safe.cc"
"tile/io/native/acceptor.cc"
"tile/io/descriptor.cc"
2024-08-05 19:21:43 +08:00
"tile/io/util/rate_limiter.cc"
2024-06-11 19:13:30 +08:00
"tile/io/event_loop.cc"
"tile/init.cc"
"tile/init/on_init.cc"
"tile/init/override_flag.cc"
"tile/net/http/http_headers.cc"
"tile/net/http/http_message.cc"
"tile/net/http/http_request.cc"
"tile/net/http/http_response.cc"
"tile/net/http/http_client.cc"
"tile/net/http/types.cc"
"tile/net/internal/http_task.cc"
"tile/net/internal/http_engine.cc"
"tile/testing/internal/random_string.cc"
"tile/rpc/protocol/http/buffer_io.cc"
"tile/rpc/protocol/message.cc"
# "tile/rpc/server.cc"
2024-08-14 21:40:13 +08:00
"tile/base/config/configuration.cc"
"tile/base/config/ini_file_configuration.cc"
2024-08-14 22:42:06 +08:00
"tile/base/config/json_configuration.cc"
2024-08-14 21:40:13 +08:00
"tile/base/config/layered_configuration.cc"
2024-06-11 19:13:30 +08:00
)
if((NOT TILE_HAVE_GETIFADDRS) OR (NOT TILE_HAVE_FREEIFADDRS))
list(APPEND TILE_SRCS "tile/base/net/detail/android/ifaddrs.c")
endif()
2024-06-12 11:11:43 +08:00
add_library(tile OBJECT ${TILE_SRCS})
2024-06-11 19:13:30 +08:00
set_target_properties(tile PROPERTIES VERSION ${PROJECT_VERSION}
2024-06-17 22:46:00 +08:00
target_precompile_headers(tile PUBLIC inja/inja.h)
target_precompile_headers(tile PUBLIC inja/string_view.h)
target_precompile_headers(tile PUBLIC json/nlohann/json.hpp)
2024-06-11 19:13:30 +08:00
SOVERSION "${tile_VERSION_MAJRO}")
# target_sources(tile PRIVATE ${TILE_SRCS})
target_include_directories(
tile
2024-07-27 12:53:30 +08:00
PUBLIC
2024-07-30 15:26:10 +08:00
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/mustache.hpp"
2024-07-27 12:53:30 +08:00
"${CMAKE_CURRENT_BINARY_DIR}"
2024-06-11 19:13:30 +08:00
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/fmt/include"
"${CMAKE_CURRENT_BINARY_DIR}/third_party/glog"
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/glog/src"
"${CMAKE_CURRENT_SOURCE_DIR}"
2024-06-14 12:31:48 +08:00
${THIRD_PARTY_INCLUDE_DIRS}
2024-06-11 19:13:30 +08:00
RPIVATE
"${CMAKE_CURRENT_BINARY_DIR}/third_party/gflags/include")
target_link_libraries(
tile
PUBLIC # -Wl,--start-group
2024-08-05 11:32:59 +08:00
# nova_context
zlib gflags::gflags glog::glog
2024-06-20 21:17:12 +08:00
jsoncpp_static
2024-06-11 19:13:30 +08:00
# -Wl,--end-group
2024-06-16 21:07:45 +08:00
libcurl fmt)
2024-06-11 19:26:15 +08:00
if((CMAKE_SYSTEM_PROCESSOR MATCHES "riscv64") OR (CMAKE_SYSTEM_PROCESSOR MATCHES
"mips*"))
2024-06-11 19:13:30 +08:00
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_link_libraries(tile PUBLIC atomic)
endif()
endif()
# set(LIB_NAMES tile)
#
# if(("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") OR (MINGW) OR (HAIKU) OR
# ("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD") OR ("${CMAKE_SYSTEM_NAME}"
# STREQUAL "GNU") OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "OpenBSD") OR
# ("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia") OR ("${CMAKE_SYSTEM_NAME}"
# STREQUAL "DragonFly") OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")) # FIXME:
# It should be "GNU ld # for elf" set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES}
# -Wl,--no-whole-archive) elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
# set(LIB_NAMES -Wl,-all_load ${LIB_NAMES}) endif()
add_library(tile::tile ALIAS tile)
# add_library(tile SHARED $<TARGET_OBJECTS:tile>) target_include_directories(
# tile PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/third_party/fmt/include"
# "${CMAKE_CURRENT_BINARY_DIR}/third_party/glog"
# "${CMAKE_CURRENT_SOURCE_DIR}/third_party/glog/src"
# "${CMAKE_CURRENT_SOURCE_DIR}" RPIVATE
# "${CMAKE_CURRENT_BINARY_DIR}/third_party/gflags/include")
# target_link_libraries( tile PRIVATE ${WHOLE_ARCHIVE_PREFIX} gflags::gflags
# glog::glog libcurl fmt::fmt ${NO_WHOLE_ARCHIVE_PREFIX})
if(TILE_BUILD_TESTS)
enable_testing()
add_executable(tile_test_all "tile/testing/main.cc")
target_link_libraries(tile_test_all PRIVATE gtest gmock tile::tile)
2024-06-29 16:13:22 +08:00
macro(tile_add_custom_test test_name test_file)
2024-06-11 19:13:30 +08:00
add_executable(${test_name} ${test_file} "tile/testing/main.cc")
target_link_libraries(
${test_name} PUBLIC gtest gmock ${WHOLE_ARCHIVE_PREFIX} tile::tile
${WHOLE_ARCHIVE_SUFFIX})
target_sources(${PROJECT_NAME}_test_all PRIVATE ${test_file})
endmacro()
2024-06-29 16:13:22 +08:00
macro(tile_add_test test_name test_file)
tile_add_custom_test(${test_name} ${test_file})
add_test(NAME ${test_name} COMMAND ${test_name})
endmacro()
2024-08-13 23:09:49 +08:00
function(add_test_group prefix group_name)
file(GLOB_RECURSE TEST_FILES "${prefix}/**/*_test.cc")
foreach(SRC_FILE ${TEST_FILES})
if(IS_DIRECTORY ${SRC_FILE})
continue()
endif()
# convert to relative path message(STATUS "${prefix} -> ${TEST_FILE}")
file(RELATIVE_PATH TEST_NAME "${prefix}" "${SRC_FILE}")
string(REPLACE "/" "_" TEST_NAME "${TEST_NAME}")
# if group_name is not empty, add suffix _
if (NOT group_name STREQUAL "")
set(TEST_NAME "${group_name}_${TEST_NAME}")
endif()
tile_add_test(${TEST_NAME} ${SRC_FILE})
endforeach()
endfunction(add_test_group)
add_test_group(${CMAKE_CURRENT_SOURCE_DIR}/tile/base base)
add_test_group(${CMAKE_CURRENT_SOURCE_DIR}/tile/init init)
add_test_group(${CMAKE_CURRENT_SOURCE_DIR}/tile/io io)
add_test_group(${CMAKE_CURRENT_SOURCE_DIR}/tile/net net)
add_test_group(${CMAKE_CURRENT_SOURCE_DIR}/tile/rpc rpc)
2024-06-29 16:13:22 +08:00
tile_add_custom_test("custom_http_client_test" "tests/http_client_test.cc")
2024-06-11 19:13:30 +08:00
endif(TILE_BUILD_TESTS)
if(TILE_BUILD_BENCHMARKS)
add_subdirectory("third_party/benchmark")
add_executable(tile_bm_all "tile/testing/bm_main.cc")
target_link_libraries(tile_bm_all PRIVATE benchmark::benchmark tile::tile)
macro(tile_add_bm benchmark_name benchmark_file)
add_executable(${benchmark_name} ${benchmark_file}
"tile/testing/bm_main.cc")
target_link_libraries(${benchmark_name} PRIVATE benchmark::benchmark
tile::tile)
target_sources(tile_bm_all PRIVATE ${benchmark_file})
endmacro()
2024-08-05 11:32:59 +08:00
# tile_add_bm(fiber_detail_fiber_benchmark "tile/fiber/detail/fiber_benchmark.cc")
2024-06-11 19:13:30 +08:00
tile_add_bm(base_casting_benchmark "tile/base/casting_benchmark.cc")
tile_add_bm(base_thread_mutex_benchmark "tile/base/thread/mutex_benchmark.cc")
tile_add_bm(base_encoding_benchmark "tile/base/encoding_benchmark.cc")
tile_add_bm(base_internal_time_keeper_benchmark
"tile/base/internal/time_keeper_benchmark.cc")
tile_add_bm(base_chrono_benchmark "tile/base/chrono_benchmark.cc")
endif(TILE_BUILD_BENCHMARKS)