Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat cmake: add download_userver() and cmake presets #84

Merged
merged 12 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ jobs:

- name: Install packages
run: |
(cd third_party && git clone -b develop --single-branch --depth 1 https://github.com/userver-framework/userver.git)
DEPS_FILE="https://raw.githubusercontent.com/userver-framework/userver/refs/heads/develop/scripts/docs/en/deps/${{matrix.os}}.md"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

зачем качать самый новый список? почему не качать то, что есть в репозитории в ветке?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Качаю то, что есть в репозитории (userver) в ветке (develop)

Зачем качать отдельно - потому что чтобы userver скачался через CPM, нужно вначале корректно прогнать cmake для сервиса, а для этого нужно вначале поставить пакеты, а для этого нужно вначале получить список пакетов

Мне не нравится способ, когда мы прогоняем cmake сервиса с ожидаемой ошибкой сборки и смотрим в build-директорию

sudo apt update
sudo apt install --allow-downgrades -y $(cat third_party/userver/scripts/docs/en/deps/${{matrix.os}}.md | tr '\n' ' ')
sudo apt install --allow-downgrades -y $(wget -q -O - ${DEPS_FILE} | tr '\n' ' ')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tr не нужен

python3 -m pip install -r requirements.txt

- name: Setup ccache
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ cmake-build-*
Testing/
.DS_Store
Makefile.local
CmakeUserPresets.json
segoon marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 6 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
cmake_minimum_required(VERSION 3.12)
project(service_template CXX)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(DownloadUserver)

# Adding userver dependency
find_package(userver COMPONENTS core postgresql QUIET)
if(NOT userver_FOUND) # Fallback to subdirectory usage
# Compatibility mode: some systems don't support these features
set(USERVER_FEATURE_CRYPTOPP_BLAKE2 OFF CACHE BOOL "" FORCE)
set(USERVER_FEATURE_GRPC_CHANNELZ OFF CACHE BOOL "" FORCE)
set(USERVER_FEATURE_REDIS_HI_MALLOC ON CACHE BOOL "" FORCE)

if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/userver)
message(STATUS "Using userver framework from third_party/userver")
add_subdirectory(third_party/userver)
else()
message(FATAL_ERROR "Either install the userver or provide a path to it")
endif()
find_package(userver COMPONENTS core QUIET)
if(NOT userver_FOUND)
# Tries TRY_DIR first, falls back to downloading userver from GitHub using CPM.
download_userver(TRY_DIR third_party/userver)
endif()

userver_setup_environment()
Expand Down
81 changes: 81 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"version": 2,
"cmakeMinimumRequired": {
"major": 3,
"minor": 20,
"patch": 0
},
"configurePresets": [
{
"name": "debug",
"displayName": "Debug",
"description": "Fully featured Debug build, some platforms miss the required dependencies",
"inherits": [
"common-flags"
],
"binaryDir": "${sourceDir}/build_debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"USERVER_SANITIZE": "addr;ub"
}
},
{
"name": "release",
"displayName": "Release",
"description": "Fully featured Release build, some platforms miss the required dependencies",
"inherits": [
"common-flags"
],
"binaryDir": "${sourceDir}/build_release",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Убрал compat часть, её пусть кладут в CMakeUserPresets.json, в доку добавлю пример

"name": "compat-debug",
"displayName": "Compatibility-mode Debug",
"description": "Debug build with some features (e.g. sanitizers) disabled for compatibility",
"inherits": [
"compat-flags",
"common-flags"
],
"binaryDir": "${sourceDir}/build_debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "compat-release",
"displayName": "Compatibility-mode Release",
"description": "Release build with some features disabled for compatibility",
"inherits": [
"common-flags",
"compat-flags"
],
"binaryDir": "${sourceDir}/build_release",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "common-flags",
"hidden": true,
"generator": "Ninja",
"cacheVariables": {
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
"CMAKE_C_COMPILER": "cc",
"CMAKE_CXX_COMPILER": "c++"
}
},
{
"name": "compat-flags",
"hidden": true,
"generator": "Unix Makefiles",
"cacheVariables": {
"USERVER_FEATURE_CRYPTOPP_BLAKE2": "OFF",
"USERVER_FEATURE_GRPC_CHANNELZ": "OFF",
"USERVER_FEATURE_REDIS_HI_MALLOC": "ON"
}
}
]
}
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CMAKE_COMMON_FLAGS ?= -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
CMAKE_DEBUG_FLAGS ?= -DUSERVER_SANITIZE='addr ub'
CMAKE_RELEASE_FLAGS ?=
CMAKE_DEBUG_FLAGS ?= --preset debug
CMAKE_RELEASE_FLAGS ?= --preset release
NPROCS ?= $(shell nproc)
CLANG_FORMAT ?= clang-format
DOCKER_COMPOSE ?= docker-compose
Expand Down Expand Up @@ -42,7 +42,7 @@ test-debug test-release: test-%: build-%
# Start the service (via testsuite service runner)
.PHONY: start-debug start-release
start-debug start-release: start-%:
cmake --build build_$* -v --target=start-service_template
cmake --build build_$* -v --target start-service_template

.PHONY: service-start-debug service-start-release
service-start-debug service-start-release: service-start-%: start-%
Expand Down
37 changes: 37 additions & 0 deletions cmake/DownloadUserver.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
include_guard(GLOBAL)

function(download_userver)
set(OPTIONS)
set(ONE_VALUE_ARGS TRY_DIR VERSION GIT_TAG)
set(MULTI_VALUE_ARGS)
cmake_parse_arguments(
ARG "${OPTIONS}" "${ONE_VALUE_ARGS}" "${MULTI_VALUE_ARGS}" ${ARGN}
)

if(ARG_TRY_DIR)
get_filename_component(ARG_TRY_DIR "${ARG_TRY_DIR}" REALPATH)
if(EXISTS "${ARG_TRY_DIR}")
message(STATUS "Using userver from ${ARG_TRY_DIR}")
add_subdirectory("${ARG_TRY_DIR}" third_party/userver)
return()
endif()
endif()

include(get_cpm)

if(NOT DEFINED ARG_VERSION AND NOT DEFINED ARG_GIT_TAG)
set(ARG_GIT_TAG develop)
endif()

if(NOT DEFINED CPM_USE_NAMED_CACHE_DIRECTORIES)
set(CPM_USE_NAMED_CACHE_DIRECTORIES ON)
endif()

CPMAddPackage(
NAME userver
GITHUB_REPOSITORY userver-framework/userver
VERSION ${ARG_VERSION}
GIT_TAG ${ARG_GIT_TAG}
${ARG_UNPARSED_ARGUMENTS}
)
endfunction()
24 changes: 24 additions & 0 deletions cmake/get_cpm.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-License-Identifier: MIT
#
# SPDX-FileCopyrightText: Copyright (c) 2019-2023 Lars Melchior and contributors

set(CPM_DOWNLOAD_VERSION 0.40.2)
set(CPM_HASH_SUM "c8cdc32c03816538ce22781ed72964dc864b2a34a310d3b7104812a5ca2d835d")

if(CPM_SOURCE_CACHE)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()

# Expand relative path. This is important if the provided path contains a tilde (~)
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)

file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION} EXPECTED_HASH SHA256=${CPM_HASH_SUM}
)

include(${CPM_DOWNLOAD_LOCATION})
11 changes: 0 additions & 11 deletions third_party/Readme.md

This file was deleted.

Loading