simstr/CMakeLists.txt

184 lines
5.5 KiB
CMake
Raw Normal View History

cmake_minimum_required(VERSION 3.20)
include(cmake/prelude.cmake)
2025-04-05 14:21:11 +00:00
include(CMakeDependentOption)
include(FetchContent)
2025-04-05 14:21:11 +00:00
project(
simstr
VERSION 1.6.7
DESCRIPTION "Yet another modern C++ string library"
2025-11-11 08:52:42 +00:00
HOMEPAGE_URL "https://github.com/orefkov/simstr"
LANGUAGES CXX
)
include(cmake/project-is-top-level.cmake)
include(cmake/variables.cmake)
2025-04-05 14:21:11 +00:00
if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
set(MSVC_COMPILER ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8 /Zc:strictStrings")
elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
set(CLANG_COMPILER ON)
endif()
2025-04-05 14:21:11 +00:00
2025-12-02 08:05:41 +00:00
if (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC OR "${CMAKE_CXX_SIMULATE_ID} " STREQUAL "MSVC ")
set(CAN_LINK_NATVIS On)
endif()
option(SIMSTR_BUILD_TESTS "Build tests" ON)
option(SIMSTR_BENCHMARKS "Build benchmarks" Off)
option(USE_SYSTEM_DEPS "Use system dependencies" Off)
2025-12-02 08:05:41 +00:00
cmake_dependent_option(SIMSTR_LINK_NATVIS "Link natvis file" On CAN_LINK_NATVIS Off)
2025-04-05 14:21:11 +00:00
add_library(simstr_simstr
2025-04-05 14:21:11 +00:00
src/sstring.cpp
src/simple_unicode.cpp
)
add_library(simstr::simstr ALIAS simstr_simstr)
target_include_directories(
simstr_simstr ${warning_guard}
PUBLIC
"\$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
)
target_compile_features(simstr_simstr PUBLIC cxx_std_20)
2025-04-05 14:21:11 +00:00
if(EMSCRIPTEN)
option(SIMSTR_EMSCRIPTEN_MT "Использовать многопоточность в Emscripten" OFF)
target_compile_options(simstr_simstr PUBLIC -Wno-warn-absolute-paths -Wno-unknown-warning-option -msimd128 -msse4.2 -msse3)
if(SIMSTR_EMSCRIPTEN_MT)
message("Build MT emscripten")
target_compile_options(simstr_simstr PUBLIC -pthread)
endif()
endif(EMSCRIPTEN)
message("Configuring simstr for [${CMAKE_BUILD_TYPE}]")
2025-12-02 08:05:41 +00:00
if (SIMSTR_LINK_NATVIS)
# Для MSVC подключаем natvis файл для красивой отладки
if (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC OR "${CMAKE_CXX_SIMULATE_ID} " STREQUAL "MSVC ")
add_custom_command(
TARGET simstr_simstr PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/for_debug/simstr.natvis
${CMAKE_BINARY_DIR}/simstr.natvis
)
target_link_options(simstr_simstr PUBLIC "/natvis:${CMAKE_BINARY_DIR}/simstr.natvis")
endif()
endif()
set_target_properties(
simstr_simstr PROPERTIES
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN YES
VERSION "${PROJECT_VERSION}"
SOVERSION "${PROJECT_VERSION_MAJOR}"
EXPORT_NAME simstr
OUTPUT_NAME simstr
)
function(add_simdutf)
set(SIMDUTF_TOOLS OFF)
set(SIMDUTF_TESTS OFF)
set(SIMDUTF_BENCHMARKS OFF)
FetchContent_Declare(
simdutf
GIT_REPOSITORY https://github.com/simdutf/simdutf.git
GIT_SHALLOW TRUE
GIT_TAG tags/v7.5.0
FIND_PACKAGE_ARGS
)
FetchContent_MakeAvailable(simdutf)
endfunction()
if (USE_SYSTEM_DEPS)
find_package(simdutf REQUIRED)
else ()
add_simdutf()
endif()
target_link_libraries(simstr_simstr PUBLIC simdutf::simdutf)
2025-04-05 14:21:11 +00:00
2025-11-15 10:07:52 +00:00
if(BUILD_SHARED_LIBS)
# Всем объявляем, что мы будем в shared библиотеке
add_compile_definitions(SIMSTR_IN_SHARED)
# Себе объявим, что мы должны экспортировать функции
target_compile_definitions(simstr_simstr PRIVATE SIMSTR_EXPORT)
2025-11-15 10:07:52 +00:00
endif(BUILD_SHARED_LIBS)
2025-04-05 14:21:11 +00:00
if(SIMSTR_BUILD_TESTS)
2025-04-05 14:21:11 +00:00
enable_testing()
set(BUILD_TESTS ON)
2025-04-05 14:21:11 +00:00
# Load and build GTest
FetchContent_Declare(
googletest
# Specify the commit you depend on and update it regularly.
URL https://github.com/google/googletest/archive/refs/tags/v1.17.0.zip
2026-01-25 10:39:22 +00:00
FIND_PACKAGE_ARGS NAMES "GTest 1.17.0"
2025-04-05 14:21:11 +00:00
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt FALSE CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
add_subdirectory(tests)
if(TARGET gtest)
target_compile_features(gtest PUBLIC cxx_std_23)
target_compile_features(gtest_main PUBLIC cxx_std_23)
endif()
2025-04-05 14:21:11 +00:00
endif()
function(GBencmark)
2025-04-05 14:21:11 +00:00
# Load and build Google benchmarks
FetchContent_Declare(
googlebench
# Specify the commit you depend on and update it regularly.
URL https://github.com/google/benchmark/archive/refs/tags/v1.9.4.zip
2026-01-25 10:39:22 +00:00
FIND_PACKAGE_ARGS NAMES "benchmark 1.9.4"
2025-04-05 14:21:11 +00:00
)
set(BENCHMARK_ENABLE_TESTING OFF)
set(BENCHMARK_ENABLE_LTO OFF)
set(BENCHMARK_ENABLE_INSTALL OFF)
set(BENCHMARK_INSTALL_DOCS OFF)
set(BENCHMARK_DOWNLOAD_DEPENDENCIES ON)
set(BENCHMARK_ENABLE_GTEST_TESTS OFF)
2025-04-05 14:21:11 +00:00
add_compile_definitions(BENCHMARK_STATIC_DEFINE)
FetchContent_MakeAvailable(googlebench)
if(TARGET benchmark)
target_compile_features(benchmark PUBLIC cxx_std_23)
endif()
if(EMSCRIPTEN)
# Свежий Clang в Emscripten ругается на __COUNTER__
target_compile_options(benchmark PUBLIC -Wno-c2y-extensions)
if(SIMSTR_EMSCRIPTEN_MT)
target_compile_options(benchmark PUBLIC -pthread)
endif()
endif()
2025-04-05 14:21:11 +00:00
endfunction()
if(SIMSTR_BENCHMARKS) # AND CMAKE_BUILD_TYPE STREQUAL Release)
set(BUILD_BENCHMARKS ON)
2025-04-05 14:21:11 +00:00
GBencmark()
add_subdirectory(bench)
endif()
# ---- Install rules ----
if(NOT CMAKE_SKIP_INSTALL_RULES)
include(cmake/install-rules.cmake)
endif()
# ---- Developer mode ----
if(NOT simstr_DEVELOPER_MODE)
return()
elseif(NOT PROJECT_IS_TOP_LEVEL)
message(
AUTHOR_WARNING
"Developer mode is intended for developers of simstr"
)
endif()
include(cmake/dev-mode.cmake)