Skip to content

Commit 5fa397a

Browse files
authored
feat build: add wider cmake presets and devcontainers support (#69)
1 parent 7221e4d commit 5fa397a

File tree

18 files changed

+127
-119
lines changed

18 files changed

+127
-119
lines changed

.devcontainer/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is the minimal devcontainers configuration e.g. for use in VSCode or CLion.
2+
If you don't use devcontainers, feel free to delete this directory.

.devcontainer/devcontainer.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"image": "ghcr.io/userver-framework/ubuntu-22.04-userver-pg-dev",
3+
"remoteUser": "user",
4+
"workspaceMount": "source=${localWorkspaceFolder},target=/home/user/service_template,type=bind",
5+
"workspaceFolder": "/home/user/service_template",
6+
"runArgs": [
7+
"--cap-add=SYS_PTRACE",
8+
"--security-opt",
9+
"seccomp=unconfined"
10+
],
11+
"forwardPorts": [
12+
8080
13+
],
14+
"containerEnv": {
15+
"SHELL": "/bin/bash",
16+
"PREFIX": "${PREFIX:-~/.local}",
17+
"CCACHE_DIR": "/home/user/service_template/.ccache",
18+
"CORES_DIR": "/cores"
19+
},
20+
"customizations": {
21+
"vscode": {
22+
"extensions": [
23+
"llvm-vs-code-extensions.vscode-clangd",
24+
"ms-vscode.cmake-tools",
25+
"ms-vscode.makefile-tools",
26+
"vadimcn.vscode-lldb",
27+
"ms-azuretools.vscode-docker"
28+
],
29+
"settings": {
30+
"cmake.automaticReconfigure": false,
31+
"cmake.configureOnEdit": false,
32+
"cmake.configureOnOpen": false,
33+
"cmake.copyCompileCommands": "${workspaceFolder}/.vscode/compile_commands.json",
34+
"clangd.path": "/usr/bin/clangd",
35+
"clangd.arguments": [
36+
"--background-index",
37+
"--compile-commands-dir=.vscode",
38+
"--clang-tidy",
39+
"--completion-style=detailed",
40+
"--header-insertion=never"
41+
]
42+
}
43+
}
44+
},
45+
"onCreateCommand": "sudo git config --global --add safe.directory /home/user/service_template",
46+
"mounts": []
47+
}

.github/workflows/ci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ jobs:
7272
- name: Test run after install
7373
if: matrix.make == 'test-release'
7474
run: >-
75-
./local_installation/bin/pg_service_template
76-
--config=./local_installation/etc/pg_service_template/static_config.yaml
77-
--config_vars=./local_installation/etc/pg_service_template/config_vars.yaml
75+
./local_installation/bin/service_template
76+
--config=./local_installation/etc/service_template/static_config.yaml
77+
--config_vars=./local_installation/etc/service_template/config_vars.yaml
7878
&
7979
8080
- name: Check work run service
8181
if: matrix.make == 'test-release'
8282
run: |
83-
ps aux | grep pg_service_template | grep config && curl http://localhost:8080/ping -v
83+
ps aux | grep service_template | grep config && curl http://localhost:8080/ping -v
8484
8585
- name: Stop all
8686
if: matrix.make == 'test-release'
8787
run: |
88-
killall pg_service_template
88+
killall service_template

.github/workflows/docker.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
sudo apt install --allow-downgrades -y docker-compose
3434
3535
- name: Setup ccache
36-
run: docker-compose run --rm pg_service_template-container bash -c 'ccache -M 2.0GB && ccache -s'
36+
run: docker-compose run --rm service_template-container bash -c 'ccache -M 2.0GB && ccache -s'
3737

3838
- name: Cmake
3939
run: make docker-cmake-release

.vscode/cmake-variants.yaml

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

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.12)
2-
project(pg_service_template CXX)
2+
project(service_template CXX)
33

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

CMakePresets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"inherits": [
1414
"common-flags"
1515
],
16-
"binaryDir": "${sourceDir}/build_debug",
16+
"binaryDir": "${sourceDir}/build-debug",
1717
"cacheVariables": {
1818
"CMAKE_BUILD_TYPE": "Debug",
1919
"USERVER_SANITIZE": "addr;ub"
@@ -26,7 +26,7 @@
2626
"inherits": [
2727
"common-flags"
2828
],
29-
"binaryDir": "${sourceDir}/build_release",
29+
"binaryDir": "${sourceDir}/build-release",
3030
"cacheVariables": {
3131
"CMAKE_BUILD_TYPE": "Release"
3232
}

Makefile

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,54 @@
1-
CMAKE_COMMON_FLAGS ?= -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
2-
CMAKE_DEBUG_FLAGS ?= --preset debug
3-
CMAKE_RELEASE_FLAGS ?= --preset release
1+
PROJECT_NAME = service_template
42
NPROCS ?= $(shell nproc)
53
CLANG_FORMAT ?= clang-format
64
DOCKER_COMPOSE ?= docker-compose
7-
8-
# NOTE: use Makefile.local to override the options defined above.
9-
-include Makefile.local
10-
11-
CMAKE_DEBUG_FLAGS += -DCMAKE_BUILD_TYPE=Debug $(CMAKE_COMMON_FLAGS)
12-
CMAKE_RELEASE_FLAGS += -DCMAKE_BUILD_TYPE=Release $(CMAKE_COMMON_FLAGS)
5+
PRESETS ?= debug release debug-custom release-custom
136

147
.PHONY: all
158
all: test-debug test-release
169

1710
# Run cmake
18-
.PHONY: cmake-debug
19-
cmake-debug:
20-
cmake -B build_debug $(CMAKE_DEBUG_FLAGS)
21-
22-
.PHONY: cmake-release
23-
cmake-release:
24-
cmake -B build_release $(CMAKE_RELEASE_FLAGS)
11+
.PHONY: $(addprefix cmake-, $(PRESETS))
12+
$(addprefix cmake-, $(PRESETS)): cmake-%:
13+
cmake --preset $*
2514

26-
build_debug/CMakeCache.txt: cmake-debug
27-
build_release/CMakeCache.txt: cmake-release
15+
$(addsuffix /CMakeCache.txt, $(addprefix build-, $(PRESETS))): build-%/CMakeCache.txt: cmake-%
2816

2917
# Build using cmake
30-
.PHONY: build-debug build-release
31-
build-debug build-release: build-%: build_%/CMakeCache.txt
32-
cmake --build build_$* -j $(NPROCS) --target pg_service_template
18+
.PHONY: $(addprefix build-, $(PRESETS))
19+
$(addprefix build-, $(PRESETS)): build-%: build-%/CMakeCache.txt
20+
cmake --build build-$* -j $(NPROCS) --target $(PROJECT_NAME)
3321

3422
# Test
35-
.PHONY: test-debug test-release
36-
test-debug test-release: test-%: build-%
37-
cmake --build build_$* -j $(NPROCS) --target pg_service_template_unittest
38-
cmake --build build_$* -j $(NPROCS) --target pg_service_template_benchmark
39-
cd build_$* && ((test -t 1 && GTEST_COLOR=1 PYTEST_ADDOPTS="--color=yes" ctest -V) || ctest -V)
23+
.PHONY: $(addprefix test-, $(PRESETS))
24+
$(addprefix test-, $(PRESETS)): test-%: build-%/CMakeCache.txt
25+
cmake --build build-$* -j $(NPROCS)
26+
cd build-$* && ((test -t 1 && GTEST_COLOR=1 PYTEST_ADDOPTS="--color=yes" ctest -V) || ctest -V)
4027
pycodestyle tests
4128

4229
# Start the service (via testsuite service runner)
43-
.PHONY: start-debug start-release
44-
start-debug start-release: start-%: build-%
45-
cmake --build build_$* -v --target start-pg_service_template
46-
47-
.PHONY: service-start-debug service-start-release
48-
service-start-debug service-start-release: service-start-%: start-%
30+
.PHONY: $(addprefix start-, $(PRESETS))
31+
$(addprefix start-, $(PRESETS)): start-%:
32+
cmake --build build-$* -v --target start-$(PROJECT_NAME)
4933

5034
# Cleanup data
51-
.PHONY: clean-debug clean-release
52-
clean-debug clean-release: clean-%:
53-
cmake --build build_$* --target clean
35+
.PHONY: $(addprefix clean-, $(PRESETS))
36+
$(addprefix clean-, $(PRESETS)): clean-%:
37+
cmake --build build-$* --target clean
5438

5539
.PHONY: dist-clean
5640
dist-clean:
57-
rm -rf build_*
41+
rm -rf build*
5842
rm -rf tests/__pycache__/
5943
rm -rf tests/.pytest_cache/
44+
rm -rf .ccache
45+
rm -rf .vscode/.cache
46+
rm -rf .vscode/compile_commands.json
6047

6148
# Install
62-
.PHONY: install-debug install-release
63-
install-debug install-release: install-%: build-%
64-
cmake --install build_$* -v --component pg_service_template
49+
.PHONY: $(addprefix install-, $(PRESETS))
50+
$(addprefix install-, $(PRESETS)): install-%: build-%
51+
cmake --install build-$* -v --component $(PROJECT_NAME)
6552

6653
.PHONY: install
6754
install: install-release
@@ -76,24 +63,22 @@ format:
7663
export DB_CONNECTION := postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@service-postgres:5432/${POSTGRES_DB}
7764

7865
# Internal hidden targets that are used only in docker environment
79-
--in-docker-start-debug --in-docker-start-release: --in-docker-start-%: install-%
66+
.PHONY: $(addprefix --in-docker-start-, $(PRESETS))
67+
$(addprefix --in-docker-start-, $(PRESETS)): --in-docker-start-%: install-%
8068
psql ${DB_CONNECTION} -f ./postgresql/data/initial_data.sql
81-
/home/user/.local/bin/pg_service_template \
82-
--config /home/user/.local/etc/pg_service_template/static_config.yaml \
83-
--config_vars /home/user/.local/etc/pg_service_template/config_vars.docker.yaml
69+
/home/user/.local/bin/$(PROJECT_NAME) \
70+
--config /home/user/.local/etc/$(PROJECT_NAME)/static_config.yaml \
71+
--config_vars /home/user/.local/etc/$(PROJECT_NAME)/config_vars.docker.yaml
8472

8573
# Build and run service in docker environment
86-
.PHONY: docker-start-debug docker-start-release
74+
.PHONY: $(addprefix docker-start-, $(PRESETS))
8775
docker-start-debug docker-start-release: docker-start-%:
88-
$(DOCKER_COMPOSE) run -p 8080:8080 --rm pg_service_template-container make -- --in-docker-start-$*
89-
90-
.PHONY: docker-start-service-debug docker-start-service-release
91-
docker-start-service-debug docker-start-service-release: docker-start-service-%: docker-start-%
76+
$(DOCKER_COMPOSE) run -p 8080:8080 --rm service_template-container make -- --in-docker-start-$*
9277

9378
# Start targets makefile in docker environment
94-
.PHONY: docker-cmake-debug docker-build-debug docker-test-debug docker-clean-debug docker-install-debug docker-cmake-release docker-build-release docker-test-release docker-clean-release docker-install-release
95-
docker-cmake-debug docker-build-debug docker-test-debug docker-clean-debug docker-install-debug docker-cmake-release docker-build-release docker-test-release docker-clean-release docker-install-release: docker-%:
96-
$(DOCKER_COMPOSE) run --rm pg_service_template-container make $*
79+
.PHONY: $(addprefix docker-cmake-, $(PRESETS)) $(addprefix docker-build-, $(PRESETS)) $(addprefix docker-test-, $(PRESETS)) $(addprefix docker-clean-, $(PRESETS)) $(addprefix docker-install-, $(PRESETS))
80+
$(addprefix docker-cmake-, $(PRESETS)) $(addprefix docker-build-, $(PRESETS)) $(addprefix docker-test-, $(PRESETS)) $(addprefix docker-clean-, $(PRESETS)) $(addprefix docker-install-, $(PRESETS)): docker-%:
81+
$(DOCKER_COMPOSE) run --rm $(PROJECT_NAME)-container make $*
9782

9883
# Stop docker container and remove PG data
9984
.PHONY: docker-clean-data

README.md

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,29 @@ Template of a C++ service that uses [userver framework](https://github.com/userv
77

88
To create your own userver-based service follow the following steps:
99

10-
1. Press the green "Use this template button" at the top of this github page
10+
1. Press the "Use this template button" at the top right of this GitHub page
1111
2. Clone the service `git clone your-service-repo && cd your-service-repo`
12-
3. Give a propper name to your service and replace all the occurences of "pg_service_template" string with that name
13-
(could be done via `find . -not -path "./third_party/*" -not -path ".git/*" -not -path './build_*' -type f | xargs sed -i 's/pg_service_template/YOUR_SERVICE_NAME/g'`).
12+
3. Give a proper name to your service and replace all the occurrences of "service_template" string with that name
13+
(could be done via `find . -not -path "./third_party/*" -not -path ".git/*" -not -path './build_*' -type f | xargs sed -i 's/service_template/YOUR_SERVICE_NAME/g'`).
1414
4. Feel free to tweak, adjust or fully rewrite the source code of your service.
1515

1616

1717
## Makefile
1818

19-
Makefile contains typicaly useful targets for development:
20-
21-
* `make build-debug` - debug build of the service with all the assertions and sanitizers enabled
22-
* `make build-release` - release build of the service with LTO
23-
* `make test-debug` - does a `make build-debug` and runs all the tests on the result
24-
* `make test-release` - does a `make build-release` and runs all the tests on the result
25-
* `make start-debug` - builds the service in debug mode and starts it
26-
* `make start-release` - builds the service in release mode and starts it
27-
* `make` or `make all` - builds and runs all the tests in release and debug modes
28-
* `make format` - autoformat all the C++ and Python sources
29-
* `make clean-` - cleans the object files
30-
* `make dist-clean` - clean all, including the CMake cached configurations
31-
* `make install` - does a `make build-release` and runs install in directory set in environment `PREFIX`
32-
* `make install-debug` - does a `make build-debug` and runs install in directory set in environment `PREFIX`
19+
`PRESET` is either `debug`, `release`, or if you've added custom presets in `CMakeUserPresets.json`, it
20+
can also be `debug-custom`, `release-custom`.
21+
22+
* `make cmake-PRESET` - run cmake configure, update cmake options and source file lists
23+
* `make build-PRESET` - build the service
24+
* `make test-PRESET` - build the service and run all tests
25+
* `make start-PRESET` - build the service, start it in testsuite environment and leave it running
26+
* `make install-PRESET` - build the service and install it in directory set in environment `PREFIX`
27+
* `make` or `make all` - build and run all tests in `debug` and `release` modes
28+
* `make format` - reformat all C++ and Python sources
29+
* `make dist-clean` - clean build files and cmake cache
3330
* `make docker-COMMAND` - run `make COMMAND` in docker environment
34-
* `make docker-build-debug` - debug build of the service with all the assertions and sanitizers enabled in docker environment
35-
* `make docker-test-debug` - does a `make build-debug` and runs all the tests on the result in docker environment
36-
* `make docker-start-release` - does a `make install-release` and runs service in docker environment
37-
* `make docker-start-debug` - does a `make install-debug` and runs service in docker environment
3831
* `make docker-clean-data` - stop docker containers and clean database data
3932

40-
Edit `Makefile.local` to change the default configuration and build options.
41-
4233

4334
## License
4435

configs/config_vars.testing.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ is-testing: true
66

77
server-port: 8080
88

9-
# pg_service_template_db_1 is the service name + _ + filename of the db_1.sql
10-
dbconnection: 'postgresql://testsuite@localhost:15433/pg_service_template_db_1'
9+
# service_template_db_1 is the service name + _ + filename of the db_1.sql
10+
dbconnection: 'postgresql://testsuite@localhost:15433/service_template_db_1'

0 commit comments

Comments
 (0)