Skip to content

Commit b26bcdb

Browse files
authored
Update Makefile
1 parent e59a0a2 commit b26bcdb

1 file changed

Lines changed: 45 additions & 62 deletions

File tree

Makefile

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,57 @@
1-
CMAKE_COMMON_FLAGS ?= -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
2-
CMAKE_DEBUG_FLAGS ?= -DUSERVER_SANITIZE=''
3-
CMAKE_RELEASE_FLAGS ?=
1+
PROJECT_NAME = service_template
42
NPROCS ?= $(shell nproc)
53
CLANG_FORMAT ?= clang-format
6-
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)
4+
DOCKER_IMAGE ?= ghcr.io/userver-framework/ubuntu-24.04-userver:latest
5+
CMAKE_OPTS ?=
6+
# If we're under TTY, pass "-it" to "docker run"
7+
DOCKER_ARGS = $(shell /bin/test -t 0 && /bin/echo -it || echo)
8+
PRESETS ?= debug release debug-custom release-custom
139

1410
.PHONY: all
1511
all: test-debug test-release
1612

1713
# Run cmake
18-
.PHONY: cmake-debug
19-
cmake-debug:
20-
cmake -B build_debug $(CMAKE_DEBUG_FLAGS)
14+
.PHONY: $(addprefix cmake-, $(PRESETS))
15+
$(addprefix cmake-, $(PRESETS)): cmake-%:
16+
cmake --preset $* $(CMAKE_OPTS)
2117

22-
.PHONY: cmake-release
23-
cmake-release:
24-
cmake -B build_release $(CMAKE_RELEASE_FLAGS)
25-
26-
build_debug/CMakeCache.txt: cmake-debug
27-
build_release/CMakeCache.txt: cmake-release
18+
$(addsuffix /CMakeCache.txt, $(addprefix build-, $(PRESETS))): build-%/CMakeCache.txt:
19+
$(MAKE) cmake-$*
2820

2921
# 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 upastebin
22+
.PHONY: $(addprefix build-, $(PRESETS))
23+
$(addprefix build-, $(PRESETS)): build-%: build-%/CMakeCache.txt
24+
cmake --build build-$* -j $(NPROCS) --target $(PROJECT_NAME)
3325

3426
# Test
35-
.PHONY: test-debug test-release
36-
test-debug test-release: test-%: build-%
37-
cmake --build build_$* -j $(NPROCS) --target runtests-upastebin
38-
cd build_$* && GTEST_COLOR=1 PYTEST_ADDOPTS="--color=yes" ctest -V
39-
pycodestyle tests
27+
.PHONY: $(addprefix test-, $(PRESETS))
28+
$(addprefix test-, $(PRESETS)): test-%: build-%/CMakeCache.txt
29+
cmake --build build-$* -j $(NPROCS)
30+
cd build-$* && ((test -t 1 && GTEST_COLOR=1 PYTEST_ADDOPTS="--color=yes" ctest -V) || ctest -V)
4031

4132
# Start the service (via testsuite service runner)
42-
.PHONY: service-start-debug service-start-release
43-
service-start-debug service-start-release: service-start-%: build-%
44-
cmake --build build_$* -v --target start-upastebin
33+
.PHONY: $(addprefix start-, $(PRESETS))
34+
$(addprefix start-, $(PRESETS)): start-%:
35+
cmake --build build-$* -v --target start-$(PROJECT_NAME)
4536

4637
# Cleanup data
47-
.PHONY: clean-debug clean-release
48-
clean-debug clean-release: clean-%:
49-
cmake --build build_$* --target clean
38+
.PHONY: $(addprefix clean-, $(PRESETS))
39+
$(addprefix clean-, $(PRESETS)): clean-%:
40+
cmake --build build-$* --target clean
5041

5142
.PHONY: dist-clean
5243
dist-clean:
53-
rm -rf build_*
44+
rm -rf build*
5445
rm -rf tests/__pycache__/
5546
rm -rf tests/.pytest_cache/
47+
rm -rf .ccache
48+
rm -rf .vscode/.cache
49+
rm -rf .vscode/compile_commands.json
5650

5751
# Install
58-
.PHONY: install-debug install-release
59-
install-debug install-release: install-%: build-%
60-
cmake --install build_$* -v --component upastebin
52+
.PHONY: $(addprefix install-, $(PRESETS))
53+
$(addprefix install-, $(PRESETS)): install-%: build-%
54+
cmake --install build-$* -v --component $(PROJECT_NAME)
6155

6256
.PHONY: install
6357
install: install-release
@@ -68,28 +62,17 @@ format:
6862
find src -name '*pp' -type f | xargs $(CLANG_FORMAT) -i
6963
find tests -name '*.py' -type f | xargs autopep8 -i
7064

71-
# Set environment for --in-docker-start
72-
export DB_CONNECTION := postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@service-postgres:5432/${POSTGRES_DB}
73-
74-
# Internal hidden targets that are used only in docker environment
75-
--in-docker-start-debug --in-docker-start-release: --in-docker-start-%: install-%
76-
psql ${DB_CONNECTION} -f ./postgresql/data/initial_data.sql
77-
/home/user/.local/bin/upastebin \
78-
--config /home/user/.local/etc/upastebin/static_config.yaml \
79-
--config_vars /home/user/.local/etc/upastebin/config_vars.docker.yaml
80-
81-
# Build and run service in docker environment
82-
.PHONY: docker-start-service-debug docker-start-service-release
83-
docker-start-service-debug docker-start-service-release: docker-start-service-%:
84-
$(DOCKER_COMPOSE) run -p 8080:8080 --rm upastebin-container make -- --in-docker-start-$*
85-
86-
# Start targets makefile in docker environment
87-
.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
88-
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-%:
89-
$(DOCKER_COMPOSE) run --rm upastebin-container make $*
90-
91-
# Stop docker container and remove PG data
92-
.PHONY: docker-clean-data
93-
docker-clean-data:
94-
$(DOCKER_COMPOSE) down -v
95-
rm -rf ./.pgdata
65+
# Start targets makefile in docker wrapper.
66+
# The docker mounts the whole service's source directory,
67+
# so you can do some stuff as you wish, switch back to host (non-docker) system
68+
# and still able to access the results.
69+
.PHONY: $(addprefix docker-cmake-, $(PRESETS)) $(addprefix docker-build-, $(PRESETS)) $(addprefix docker-test-, $(PRESETS)) $(addprefix docker-clean-, $(PRESETS))
70+
$(addprefix docker-cmake-, $(PRESETS)) $(addprefix docker-build-, $(PRESETS)) $(addprefix docker-test-, $(PRESETS)) $(addprefix docker-clean-, $(PRESETS)): docker-%:
71+
docker run $(DOCKER_ARGS) \
72+
--network=host \
73+
-v $$PWD:$$PWD \
74+
-w $$PWD \
75+
$(DOCKER_IMAGE) \
76+
env CCACHE_DIR=$$PWD/.ccache \
77+
HOME=$$HOME \
78+
$$PWD/run_as_user.sh $(shell /bin/id -u) $(shell /bin/id -g) make $*

0 commit comments

Comments
 (0)