diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3cf36f0..6b29dee 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: Setup ccache diff --git a/.gitignore b/.gitignore index c631f25..9e986f8 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ cmake-build-* Testing/ .DS_Store Makefile.local +CmakeUserPresets.json diff --git a/CMakeLists.txt b/CMakeLists.txt index c4513d2..3166b13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..dff550d --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,45 @@ +{ + "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++" + } + } + ] +} diff --git a/Makefile b/Makefile index 234880f..e9725c5 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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-% diff --git a/cmake/DownloadUserver.cmake b/cmake/DownloadUserver.cmake new file mode 100644 index 0000000..390533a --- /dev/null +++ b/cmake/DownloadUserver.cmake @@ -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() diff --git a/cmake/get_cpm.cmake b/cmake/get_cpm.cmake new file mode 100644 index 0000000..f80013e --- /dev/null +++ b/cmake/get_cpm.cmake @@ -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}) diff --git a/third_party/Readme.md b/third_party/Readme.md deleted file mode 100644 index 2e623aa..0000000 --- a/third_party/Readme.md +++ /dev/null @@ -1,11 +0,0 @@ -### Directory for third party libraries - -`userver` placed into this directory would be used if there's no installed -userver framework. For example: - -``` -cd /data/code -git clone --depth 1 https://github.com/userver-framework/userver.git -git clone --depth 1 https://github.com/userver-framework/service_template.git -ln -s /data/code/userver /data/code/service_template/third_party/userver -```