Skip to content

Commit

Permalink
feat cmake: add download_userver() and cmake presets (#5)
Browse files Browse the repository at this point in the history
Co-authored-by: segoon <[email protected]>
  • Loading branch information
Anton3 and segoon authored Jan 17, 2025
1 parent 179355e commit 79bfe67
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 32 deletions.
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"
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})
python3 -m pip install -r requirements.txt
- name: Install mongo
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
22 changes: 5 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
cmake_minimum_required(VERSION 3.12)
project(mongo_grpc_service_template CXX)

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

# Adding userver dependency
find_package(userver COMPONENTS core mongo grpc QUIET)
if(NOT userver_FOUND) # Fallback to subdirectory usage
# Enable userver libraries that are needed in this project
set(USERVER_FEATURE_MONGODB ON CACHE BOOL "" FORCE)
set(USERVER_FEATURE_GRPC ON CACHE BOOL "" FORCE)

# 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()
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
47 changes: 47 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"version": 2,
"cmakeMinimumRequired": {
"major": 3,
"minor": 20,
"patch": 0
},
"configurePresets": [
{
"name": "debug",
"displayName": "Debug",
"description": "Fully featured Debug build",
"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",
"inherits": [
"common-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++",
"USERVER_FEATURE_MONGODB": "ON",
"USERVER_FEATURE_GRPC": "ON"
}
}
]
}
4 changes: 2 additions & 2 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
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.

0 comments on commit 79bfe67

Please sign in to comment.