Skip to content

Commit

Permalink
feat build: prefer static libs when possible
Browse files Browse the repository at this point in the history
Tests: протестировано CI

Pull Request resolved: #821
commit_hash:1762ad5ac6e6a712c45b4964ddf2f57ac73be0c1
  • Loading branch information
fdr400 committed Jan 10, 2025
1 parent f9dd97a commit 11766b2
Show file tree
Hide file tree
Showing 67 changed files with 367 additions and 196 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/alpine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
path: |
${{env.CCACHE_DIR}}
${{env.CPM_SOURCE_CACHE}}
key: 'alpine-cache-dir ${{github.ref}}'
key: 'alpine-cache-dir ${{github.ref}} run-${{github.run_number}}'
restore-keys: |
alpine-cache-dir ${{github.ref}}
alpine-cache-dir
Expand All @@ -59,7 +59,7 @@ jobs:
run: |
pwd
cat /etc/alpine-release
apk add lld ccache git
apk add lld ccache git gdb
apk add $(cat scripts/docs/en/deps/alpine.md)
- name: Install test dependencies
Expand Down Expand Up @@ -91,6 +91,7 @@ jobs:
CMAKE_PROGRAM_PATH=/usr/lib/llvm17/bin/ \
cmake -S . -B build_debug \
-DCMAKE_BUILD_TYPE=Debug \
-DUSERVER_USE_STATIC_LIBS=1 \
-DUSERVER_USE_LD=lld \
-DUSERVER_NO_WERROR=OFF \
-DUSERVER_BUILD_ALL_COMPONENTS=1 \
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/ci-conan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ jobs:
run: |
brew update
brew install postgresql redis clang-format
brew install libiconv # https://stackoverflow.com/questions/57734434/libiconv-or-iconv-undefined-symbol-on-mac-osx
brew install [email protected]
- name: Install common packages
Expand Down
12 changes: 10 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
-DCMAKE_CXX_COMPILER=clang++-18
-DCMAKE_C_COMPILER=clang-18
-DCMAKE_BUILD_TYPE=Debug
-DUSERVER_USE_STATIC_LIBS=1
-DUSERVER_SANITIZE="ub addr"
-DUSERVER_NO_WERROR=0
-DUSERVER_BUILD_ALL_COMPONENTS=1
Expand All @@ -45,6 +46,7 @@ jobs:
-DCMAKE_CXX_COMPILER=clang++-14
-DCMAKE_C_COMPILER=clang-14
-DCMAKE_BUILD_TYPE=Debug
-DUSERVER_USE_STATIC_LIBS=1
-DUSERVER_SANITIZE="ub addr"
-DUSERVER_NO_WERROR=0
-DUSERVER_BUILD_ALL_COMPONENTS=1
Expand All @@ -71,6 +73,7 @@ jobs:
-DUSERVER_FEATURE_REDIS_HI_MALLOC=1
-DUSERVER_FORCE_DOWNLOAD_ABSEIL=0
-DUSERVER_DOWNLOAD_PACKAGE_ABSEIL=0
-DUSERVER_MONGODB_USE_CMAKE_CONFIG=0
os: ubuntu-20.04
info: g++-9 + release
id: ubuntu20_gcc9_release
Expand All @@ -91,6 +94,7 @@ jobs:
-DUSERVER_FEATURE_REDIS_HI_MALLOC=1
-DUSERVER_FORCE_DOWNLOAD_ABSEIL=0
-DUSERVER_DOWNLOAD_PACKAGE_ABSEIL=0
-DUSERVER_MONGODB_USE_CMAKE_CONFIG=0
os: ubuntu-20.04
info: g++-8 + debug
id: ubuntu20_gcc8_debug
Expand All @@ -109,7 +113,7 @@ jobs:
path: |
${{env.CCACHE_DIR}}
${{env.CPM_SOURCE_CACHE}}
key: 'ubuntu-cache-dir ${{matrix.id}} ${{github.ref}}'
key: 'ubuntu-cache-dir ${{matrix.id}} ${{github.ref}} run-${{github.run_number}}'
restore-keys: |
ubuntu-cache-dir ${{matrix.id}} ${{github.ref}}
ubuntu-cache-dir ${{matrix.id}}
Expand Down Expand Up @@ -217,7 +221,11 @@ jobs:
git clone --recursive --depth 1 -b v1.45.0 https://github.com/grpc/grpc new_grpc
GRPC_BUILD_ENABLE_CCACHE=true \
cmake -GNinja -S./new_grpc -B./new_grpc/build \
-DABSL_PROPAGATE_CXX_STD=ON -DABSL_ENABLE_INSTALL=ON
-DABSL_PROPAGATE_CXX_STD=ON -DABSL_ENABLE_INSTALL=ON \
-DgRPC_SSL_PROVIDER=package \
-DgRPC_CARES_PROVIDER=package \
-DgRPC_ZLIB_PROVIDER=package \
-DgRPC_INSTALL=ON
cmake --build new_grpc/build -j$(nproc)
sudo cmake --install new_grpc/build
rm -rf new_grpc
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
- cmake-flags: >-
-DCMAKE_CXX_STANDARD=17
-DCPM_SOURCE_CACHE=~/.cache/CPM
-DUSERVER_USE_LD=gold
-DUSERVER_NO_WERROR=0
-DUSERVER_BUILD_SAMPLES=1
-DUSERVER_BUILD_TESTS=1
Expand All @@ -32,6 +31,7 @@ jobs:
-DCMAKE_CXX_COMPILER=clang++-16
-DCMAKE_CXX_STANDARD=17
-DCPM_SOURCE_CACHE=~/.cache/CPM
-DUSERVER_USE_STATIC_LIBS=1
-DUSERVER_NO_WERROR=0
-DUSERVER_BUILD_ALL_COMPONENTS=1
-DUSERVER_BUILD_SAMPLES=1
Expand Down Expand Up @@ -59,6 +59,7 @@ jobs:
-DCMAKE_CXX_COMPILER=clang++-16
-DCMAKE_CXX_STANDARD=20
-DCPM_SOURCE_CACHE=~/.cache/CPM
-DUSERVER_USE_STATIC_LIBS=1
-DUSERVER_BUILD_ALL_COMPONENTS=1
-DUSERVER_BUILD_SAMPLES=1
-DUSERVER_BUILD_TESTS=1
Expand Down Expand Up @@ -97,7 +98,7 @@ jobs:
path: |
.ccache
.cache/CPM
key: 'docker-cache-dir ${{matrix.id}} ${{github.ref}}'
key: 'docker-cache-dir ${{matrix.id}} ${{github.ref}} run-${{github.run_number}}'
restore-keys: |
docker-cache-dir ${{matrix.id}} ${{github.ref}}
docker-cache-dir ${{matrix.id}}
Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
CMAKE_FLAGS: >-
-GNinja
-DCMAKE_BUILD_TYPE=Debug
-DUSERVER_USE_STATIC_LIBS=1
-DUSERVER_NO_WERROR=1
-DUSERVER_BUILD_ALL_COMPONENTS=1
-DUSERVER_BUILD_SAMPLES=1
Expand All @@ -48,7 +49,7 @@ jobs:
path: |
${{env.CCACHE_DIR}}
${{env.CPM_SOURCE_CACHE}}
key: 'macos-cache-dir ${{github.ref}}'
key: 'macos-cache-dir ${{github.ref}} run-${{github.run_number}}'
restore-keys: |
macos-cache-dir ${{github.ref}}
macos-cache-dir
Expand All @@ -63,6 +64,12 @@ jobs:
brew install lld
brew link postgresql@16 # postgresql is keg-only
brew link --force zlib # keg-only + need for static linkage
brew link --force icu4c@76 # keg-only + need for static boost linkage
brew link --force curl # keg-only
brew link --force cyrus-sasl # keg-only
brew link --force openldap # keg-only
brew remove --ignore-dependencies abseil
- name: Setup ccache
run: |
Expand Down
8 changes: 4 additions & 4 deletions .mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,10 @@
"cmake/SetupGTest.cmake":"taxi/uservices/userver/cmake/SetupGTest.cmake",
"cmake/SetupGoogleProtoApis.cmake":"taxi/uservices/userver/cmake/SetupGoogleProtoApis.cmake",
"cmake/SetupGrpc.cmake":"taxi/uservices/userver/cmake/SetupGrpc.cmake",
"cmake/SetupHomebrew.cmake":"taxi/uservices/userver/cmake/SetupHomebrew.cmake",
"cmake/SetupLTO.cmake":"taxi/uservices/userver/cmake/SetupLTO.cmake",
"cmake/SetupLinker.cmake":"taxi/uservices/userver/cmake/SetupLinker.cmake",
"cmake/SetupMongoDeps.cmake":"taxi/uservices/userver/cmake/SetupMongoDeps.cmake",
"cmake/SetupOpentelemetryProto.cmake":"taxi/uservices/userver/cmake/SetupOpentelemetryProto.cmake",
"cmake/SetupPGO.cmake":"taxi/uservices/userver/cmake/SetupPGO.cmake",
"cmake/SetupPostgresqlDeps.cmake":"taxi/uservices/userver/cmake/SetupPostgresqlDeps.cmake",
Expand All @@ -420,6 +422,7 @@
"cmake/UserverGrpcTargets.cmake":"taxi/uservices/userver/cmake/UserverGrpcTargets.cmake",
"cmake/UserverModule.cmake":"taxi/uservices/userver/cmake/UserverModule.cmake",
"cmake/UserverPack.cmake":"taxi/uservices/userver/cmake/UserverPack.cmake",
"cmake/UserverPreferStaticLibs.cmake":"taxi/uservices/userver/cmake/UserverPreferStaticLibs.cmake",
"cmake/UserverRequireDWCAS.cmake":"taxi/uservices/userver/cmake/UserverRequireDWCAS.cmake",
"cmake/UserverRequireDWCAS.cpp":"taxi/uservices/userver/cmake/UserverRequireDWCAS.cpp",
"cmake/UserverSetupEnvironment.cmake":"taxi/uservices/userver/cmake/UserverSetupEnvironment.cmake",
Expand Down Expand Up @@ -448,7 +451,6 @@
"cmake/install/userver-universal-config.cmake":"taxi/uservices/userver/cmake/install/userver-universal-config.cmake",
"cmake/install/userver-ydb-config.cmake":"taxi/uservices/userver/cmake/install/userver-ydb-config.cmake",
"cmake/modules/FindBrotli.cmake":"taxi/uservices/userver/cmake/modules/FindBrotli.cmake",
"cmake/modules/FindCrypto.cmake":"taxi/uservices/userver/cmake/modules/FindCrypto.cmake",
"cmake/modules/FindCryptoPP.cmake":"taxi/uservices/userver/cmake/modules/FindCryptoPP.cmake",
"cmake/modules/FindGrpcChannelz.cmake":"taxi/uservices/userver/cmake/modules/FindGrpcChannelz.cmake",
"cmake/modules/FindGssApi.cmake":"taxi/uservices/userver/cmake/modules/FindGssApi.cmake",
Expand All @@ -461,7 +463,7 @@
"cmake/modules/FindPythonDev.cmake":"taxi/uservices/userver/cmake/modules/FindPythonDev.cmake",
"cmake/modules/FindRdKafka.cmake":"taxi/uservices/userver/cmake/modules/FindRdKafka.cmake",
"cmake/modules/FindRocksDB.cmake":"taxi/uservices/userver/cmake/modules/FindRocksDB.cmake",
"cmake/modules/FindSSL.cmake":"taxi/uservices/userver/cmake/modules/FindSSL.cmake",
"cmake/modules/FindSASL2.cmake":"taxi/uservices/userver/cmake/modules/FindSASL2.cmake",
"cmake/modules/FindUserverGBench.cmake":"taxi/uservices/userver/cmake/modules/FindUserverGBench.cmake",
"cmake/modules/FindUserverGrpc.cmake":"taxi/uservices/userver/cmake/modules/FindUserverGrpc.cmake",
"cmake/modules/Findbson.cmake":"taxi/uservices/userver/cmake/modules/Findbson.cmake",
Expand All @@ -470,11 +472,9 @@
"cmake/modules/Findclickhouse-cpp.cmake":"taxi/uservices/userver/cmake/modules/Findclickhouse-cpp.cmake",
"cmake/modules/Findfmt.cmake":"taxi/uservices/userver/cmake/modules/Findfmt.cmake",
"cmake/modules/Findlibgflags.cmake":"taxi/uservices/userver/cmake/modules/Findlibgflags.cmake",
"cmake/modules/Findlibintl.cmake":"taxi/uservices/userver/cmake/modules/Findlibintl.cmake",
"cmake/modules/Findlibmariadb.cmake":"taxi/uservices/userver/cmake/modules/Findlibmariadb.cmake",
"cmake/modules/Findlibsnappy.cmake":"taxi/uservices/userver/cmake/modules/Findlibsnappy.cmake",
"cmake/modules/Findlibyamlcpp.cmake":"taxi/uservices/userver/cmake/modules/Findlibyamlcpp.cmake",
"cmake/modules/Findlibzip.cmake":"taxi/uservices/userver/cmake/modules/Findlibzip.cmake",
"cmake/modules/Findlibzstd.cmake":"taxi/uservices/userver/cmake/modules/Findlibzstd.cmake",
"cmake/modules/Findlz4.cmake":"taxi/uservices/userver/cmake/modules/Findlz4.cmake",
"cmake/modules/Findmongoc.cmake":"taxi/uservices/userver/cmake/modules/Findmongoc.cmake",
Expand Down
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ option(
"Prepare build of userver to install in system"
OFF
)
if(CMAKE_VERSION GREATER_EQUAL 3.31)
cmake_policy(SET CMP0177 NEW)
endif()

set(USERVER_AVAILABLE_COMPONENTS universal)

Expand Down Expand Up @@ -144,6 +147,16 @@ option(USERVER_FEATURE_OTLP "Provide asynchronous OTLP exporters" "${USERVER_LIB
set(CMAKE_DEBUG_POSTFIX d)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

option(USERVER_USE_STATIC_LIBS "Tries to find all dependencies as static libraries" OFF)

if(USERVER_USE_STATIC_LIBS)
message(
WARNING
"USERVER_USE_STATIC_LIBS turned ON, prioritized to find static libraries. Not all dependencies may be taken into account and linkage may be broken")
include(UserverPreferStaticLibs)
endif()

include(UserverSetupEnvironment)
userver_setup_environment()

Expand Down
3 changes: 3 additions & 0 deletions clickhouse/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ userver_module(clickhouse
DBTEST_DATABASES clickhouse
)

target_compile_options(${PROJECT_NAME} PUBLIC "-Wno-error=pedantic")


_userver_directory_install(COMPONENT clickhouse FILES
"${USERVER_ROOT_DIR}/cmake/modules/Findclickhouse-cpp.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/userver/modules
Expand Down
27 changes: 4 additions & 23 deletions cmake/ModuleHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ macro(_userver_module_begin)
set(oneValueArgs
# Target name, also used for package name by default
NAME
# Custom package name; NAME is used by default
PACKAGE_NAME
# For multi-target packages
COMMON_NAME
VERSION
)
set(multiValueArgs
Expand Down Expand Up @@ -130,6 +126,7 @@ macro(_userver_module_find_part)
PATH_SUFFIXES ${ARG_PATH_SUFFIXES}
PATHS ${ARG_PATHS}
)
message(DEBUG "STATUS FIND(${ARG_NAMES}) ARGS: ${find_command_args}")
if("${ARG_PART_TYPE}" STREQUAL "library")
find_library(${find_command_args})
elseif("${ARG_PART_TYPE}" STREQUAL "path")
Expand Down Expand Up @@ -226,11 +223,7 @@ macro(_userver_module_end)
include("${USERVER_CMAKE_DIR}/DetectVersion.cmake")

set(name "${ARG_NAME}")
if(ARG_PACKAGE_NAME)
set(current_package_name "${ARG_PACKAGE_NAME}")
else()
set(current_package_name "${ARG_NAME}")
endif()
set(current_package_name "${ARG_NAME}")
set(libraries_variable "${ARG_NAME}_LIBRARIES")
set(includes_variable "${ARG_NAME}_INCLUDE_DIRS")
set(programs_variable "${ARG_NAME}_EXECUTABLE")
Expand Down Expand Up @@ -295,18 +288,11 @@ macro(_userver_module_end)
mark_as_advanced(${required_vars})
else()
# Forward to another CMake module, add nice error messages if missing.
if(ARG_COMMON_NAME)
set(wrapped_package_name "${ARG_COMMON_NAME}")
else()
set(wrapped_package_name "${current_package_name}")
endif()
set(wrapped_package_name "${current_package_name}")
set(find_command_args "${wrapped_package_name}")
if(ARG_VERSION)
list(APPEND find_command_args "${ARG_VERSION}")
endif()
if(ARG_COMMON_NAME)
list(APPEND find_command_args COMPONENTS "${name}")
endif()
find_package(${find_command_args})
set("${name}_FOUND" "${${wrapped_package_name}_FOUND}")
endif()
Expand All @@ -332,16 +318,11 @@ macro(_userver_module_end)
endif()
endif()

if(ARG_COMMON_NAME
OR (NOT "${${libraries_variable}}" STREQUAL "")
if((NOT "${${libraries_variable}}" STREQUAL "")
OR (NOT "${${includes_variable}}" STREQUAL ""))
if(NOT TARGET "${name}")
add_library("${name}" INTERFACE IMPORTED GLOBAL)

if(ARG_COMMON_NAME AND TARGET "${ARG_COMMON_NAME}::${name}")
target_link_libraries("${name}" INTERFACE "${ARG_COMMON_NAME}::${name}")
endif()

if(NOT "${${includes_variable}}" STREQUAL "")
target_include_directories("${name}" INTERFACE ${${includes_variable}})
message(STATUS "${name} include directories: ${${includes_variable}}")
Expand Down
17 changes: 15 additions & 2 deletions cmake/SetupCURL.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
option(USERVER_DOWNLOAD_PACKAGE_CURL "Download and setup libcurl if no libcurl of matching version was found" ${USERVER_DOWNLOAD_PACKAGES})

if(NOT USERVER_FORCE_DOWNLOAD_PACKAGES)
# Curl has too many dependencies to reliably
# link with all of them statically without CMake Config
if (USERVER_USE_STATIC_LIBS)
list(REVERSE CMAKE_FIND_LIBRARY_SUFFIXES)
endif()

if(USERVER_DOWNLOAD_PACKAGE_CURL)
find_package(CURL "7.68" QUIET)
find_package(CURL "7.68")
else()
find_package_required_version(CURL "libcurl4-openssl-dev" "7.68")
find_package(CURL "7.68" REQUIRED)
endif()

if (USERVER_USE_STATIC_LIBS)
list(REVERSE CMAKE_FIND_LIBRARY_SUFFIXES)
endif()

if(CURL_FOUND)
Expand Down Expand Up @@ -45,3 +55,6 @@ CPMAddPackage(
"CURL_DISABLE_TESTS ON"
${CURL_LTO_OPTION}
)

mark_targets_as_system("${CURL_SOURCE_DIR}")
write_package_stub(CURL)
9 changes: 8 additions & 1 deletion cmake/SetupClickhouseCPP.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
option(USERVER_DOWNLOAD_PACKAGE_CLICKHOUSECPP "Download and setup clickhouse-cpp" ${USERVER_DOWNLOAD_PACKAGES})

if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
find_package(lz4 REQUIRED)
include(SetupAbseil)
endif()

if (NOT USERVER_FORCE_DOWNLOAD_PACKAGES)
if (USERVER_DOWNLOAD_PACKAGE_CLICKHOUSECPP)
find_package(clickhouse-cpp QUIET)
Expand All @@ -8,6 +13,9 @@ if (NOT USERVER_FORCE_DOWNLOAD_PACKAGES)
endif()

if (clickhouse-cpp_FOUND)
if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
target_link_libraries(clickhouse-cpp INTERFACE lz4::lz4 absl::int128)
endif()
return()
endif()
endif()
Expand All @@ -27,4 +35,3 @@ CPMAddPackage(
)

add_library(clickhouse-cpp ALIAS clickhouse-cpp-lib)
target_compile_options(clickhouse-cpp-lib PUBLIC -Wno-pedantic)
2 changes: 1 addition & 1 deletion cmake/SetupGrpc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ CPMAddPackage(
"gRPC_SSL_PROVIDER package"
"gRPC_PROTOBUF_PROVIDER package"
"gRPC_BENCHMARK_PROVIDER none"
"gRPC_ABSL_PROVIDER none"
"gRPC_ABSL_PROVIDER package"
"gRPC_CARES_LIBRARIES c-ares::cares"
"gRPC_INSTALL OFF"
)
Expand Down
Loading

0 comments on commit 11766b2

Please sign in to comment.