Skip to content

Commit f289637

Browse files
Anton3segoon
andauthored
feat cmake: add download_userver() and cmake presets (#84)
Co-authored-by: segoon <[email protected]>
1 parent 3f4df63 commit f289637

File tree

8 files changed

+118
-30
lines changed

8 files changed

+118
-30
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ jobs:
4444
4545
- name: Install packages
4646
run: |
47-
(cd third_party && git clone -b develop --single-branch --depth 1 https://github.com/userver-framework/userver.git)
47+
DEPS_FILE="https://raw.githubusercontent.com/userver-framework/userver/refs/heads/develop/scripts/docs/en/deps/${{matrix.os}}.md"
4848
sudo apt update
49-
sudo apt install --allow-downgrades -y $(cat third_party/userver/scripts/docs/en/deps/${{matrix.os}}.md | tr '\n' ' ')
49+
sudo apt install --allow-downgrades -y $(wget -q -O - ${DEPS_FILE})
5050
python3 -m pip install -r requirements.txt
5151
5252
- name: Setup ccache

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ cmake-build-*
1313
Testing/
1414
.DS_Store
1515
Makefile.local
16+
CmakeUserPresets.json

CMakeLists.txt

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
cmake_minimum_required(VERSION 3.12)
22
project(service_template CXX)
33

4+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
5+
include(DownloadUserver)
46

5-
# Adding userver dependency
6-
find_package(userver COMPONENTS core postgresql QUIET)
7-
if(NOT userver_FOUND) # Fallback to subdirectory usage
8-
# Compatibility mode: some systems don't support these features
9-
set(USERVER_FEATURE_CRYPTOPP_BLAKE2 OFF CACHE BOOL "" FORCE)
10-
set(USERVER_FEATURE_GRPC_CHANNELZ OFF CACHE BOOL "" FORCE)
11-
set(USERVER_FEATURE_REDIS_HI_MALLOC ON CACHE BOOL "" FORCE)
12-
13-
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/userver)
14-
message(STATUS "Using userver framework from third_party/userver")
15-
add_subdirectory(third_party/userver)
16-
else()
17-
message(FATAL_ERROR "Either install the userver or provide a path to it")
18-
endif()
7+
find_package(userver COMPONENTS core QUIET)
8+
if(NOT userver_FOUND)
9+
# Tries TRY_DIR first, falls back to downloading userver from GitHub using CPM.
10+
download_userver(TRY_DIR third_party/userver)
1911
endif()
2012

2113
userver_setup_environment()

CMakePresets.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"version": 2,
3+
"cmakeMinimumRequired": {
4+
"major": 3,
5+
"minor": 20,
6+
"patch": 0
7+
},
8+
"configurePresets": [
9+
{
10+
"name": "debug",
11+
"displayName": "Debug",
12+
"description": "Fully featured Debug build",
13+
"inherits": [
14+
"common-flags"
15+
],
16+
"binaryDir": "${sourceDir}/build_debug",
17+
"cacheVariables": {
18+
"CMAKE_BUILD_TYPE": "Debug",
19+
"USERVER_SANITIZE": "addr;ub"
20+
}
21+
},
22+
{
23+
"name": "release",
24+
"displayName": "Release",
25+
"description": "Fully featured Release build",
26+
"inherits": [
27+
"common-flags"
28+
],
29+
"binaryDir": "${sourceDir}/build_release",
30+
"cacheVariables": {
31+
"CMAKE_BUILD_TYPE": "Release"
32+
}
33+
},
34+
{
35+
"name": "common-flags",
36+
"hidden": true,
37+
"generator": "Ninja",
38+
"cacheVariables": {
39+
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
40+
"CMAKE_C_COMPILER": "cc",
41+
"CMAKE_CXX_COMPILER": "c++"
42+
}
43+
}
44+
]
45+
}

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CMAKE_COMMON_FLAGS ?= -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
2-
CMAKE_DEBUG_FLAGS ?= -DUSERVER_SANITIZE='addr ub'
3-
CMAKE_RELEASE_FLAGS ?=
2+
CMAKE_DEBUG_FLAGS ?= --preset debug
3+
CMAKE_RELEASE_FLAGS ?= --preset release
44
NPROCS ?= $(shell nproc)
55
CLANG_FORMAT ?= clang-format
66
DOCKER_COMPOSE ?= docker-compose
@@ -42,7 +42,7 @@ test-debug test-release: test-%: build-%
4242
# Start the service (via testsuite service runner)
4343
.PHONY: start-debug start-release
4444
start-debug start-release: start-%:
45-
cmake --build build_$* -v --target=start-service_template
45+
cmake --build build_$* -v --target start-service_template
4646

4747
.PHONY: service-start-debug service-start-release
4848
service-start-debug service-start-release: service-start-%: start-%

cmake/DownloadUserver.cmake

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
include_guard(GLOBAL)
2+
3+
function(download_userver)
4+
set(OPTIONS)
5+
set(ONE_VALUE_ARGS TRY_DIR VERSION GIT_TAG)
6+
set(MULTI_VALUE_ARGS)
7+
cmake_parse_arguments(
8+
ARG "${OPTIONS}" "${ONE_VALUE_ARGS}" "${MULTI_VALUE_ARGS}" ${ARGN}
9+
)
10+
11+
if(ARG_TRY_DIR)
12+
get_filename_component(ARG_TRY_DIR "${ARG_TRY_DIR}" REALPATH)
13+
if(EXISTS "${ARG_TRY_DIR}")
14+
message(STATUS "Using userver from ${ARG_TRY_DIR}")
15+
add_subdirectory("${ARG_TRY_DIR}" third_party/userver)
16+
return()
17+
endif()
18+
endif()
19+
20+
include(get_cpm)
21+
22+
if(NOT DEFINED ARG_VERSION AND NOT DEFINED ARG_GIT_TAG)
23+
set(ARG_GIT_TAG develop)
24+
endif()
25+
26+
if(NOT DEFINED CPM_USE_NAMED_CACHE_DIRECTORIES)
27+
set(CPM_USE_NAMED_CACHE_DIRECTORIES ON)
28+
endif()
29+
30+
CPMAddPackage(
31+
NAME userver
32+
GITHUB_REPOSITORY userver-framework/userver
33+
VERSION ${ARG_VERSION}
34+
GIT_TAG ${ARG_GIT_TAG}
35+
${ARG_UNPARSED_ARGUMENTS}
36+
)
37+
endfunction()

cmake/get_cpm.cmake

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SPDX-License-Identifier: MIT
2+
#
3+
# SPDX-FileCopyrightText: Copyright (c) 2019-2023 Lars Melchior and contributors
4+
5+
set(CPM_DOWNLOAD_VERSION 0.40.2)
6+
set(CPM_HASH_SUM "c8cdc32c03816538ce22781ed72964dc864b2a34a310d3b7104812a5ca2d835d")
7+
8+
if(CPM_SOURCE_CACHE)
9+
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
10+
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
11+
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
12+
else()
13+
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
14+
endif()
15+
16+
# Expand relative path. This is important if the provided path contains a tilde (~)
17+
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)
18+
19+
file(DOWNLOAD
20+
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
21+
${CPM_DOWNLOAD_LOCATION} EXPECTED_HASH SHA256=${CPM_HASH_SUM}
22+
)
23+
24+
include(${CPM_DOWNLOAD_LOCATION})

third_party/Readme.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)