Skip to content

Commit 5643477

Browse files
authored
Updated CMakeLists.txt, CI and userver submodule (#3)
1 parent c886d7b commit 5643477

File tree

14 files changed

+183
-145
lines changed

14 files changed

+183
-145
lines changed

.github/workflows/ci.yml

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@ jobs:
1515
fail-fast: false
1616
matrix:
1717
include:
18-
- os: ubuntu-20.04
18+
- os: ubuntu-22.04
1919
make: test-debug
20-
info: g++-9 + test-debug
20+
info: g++-11 + test-debug
2121

22-
- os: ubuntu-20.04
22+
- os: ubuntu-22.04
2323
make: test-release
24-
info: g++-9 + test-release
24+
info: g++-11 + test-release
2525

2626
name: '${{matrix.os}}: ${{matrix.info}}'
2727
runs-on: ${{matrix.os}}
2828

2929
steps:
30-
- uses: actions/checkout@v2
30+
- uses: actions/checkout@v4
3131
with:
3232
submodules: true
3333

3434
- name: Reuse ccache directory
35-
uses: actions/cache@v2
35+
uses: actions/cache@v4
3636
with:
3737
path: ~/.ccache
3838
key: '${{matrix.os}} ${{matrix.info}} ccache-dir ${{github.ref}} run-${{github.run_number}}'
@@ -43,13 +43,32 @@ jobs:
4343
- name: Install packages
4444
run: |
4545
sudo apt update
46+
sudo apt install --allow-downgrades -y postgresql $(cat third_party/userver/scripts/docs/en/deps/${{matrix.os}}.md | tr '\n' ' ')
4647
sudo apt install --allow-downgrades -y pep8 $(cat third_party/userver/scripts/docs/en/deps/${{matrix.os}}.md | tr '\n' ' ')
48+
49+
- name: Reinstall postgres 14
50+
run: |
51+
sudo apt purge libpq5 libpq-dev postgresql-*
52+
sudo apt install -y postgresql-14 postgresql-client-14 postgresql-server-dev-14
53+
4754
4855
- name: Setup ccache
4956
run: |
5057
ccache -M 2.0GB
5158
ccache -s
5259
60+
- name: Run postgresql
61+
if: matrix.make == 'test-release'
62+
run: |
63+
pg_lsclusters
64+
sudo -u postgres psql --command="CREATE USER realmedium_sample_user PASSWORD 'password'" --command="\du"
65+
sudo -u postgres createdb --owner=realmedium_sample_user realmedium_db-1
66+
67+
- name: Run migrations
68+
if: matrix.make == 'test-release'
69+
run: |
70+
PGPASSWORD=password psql 'postgresql://realmedium_sample_user@localhost:5432/realmedium_db-1' -f ./postgresql/schemas/db-1.sql
71+
5372
- name: Run ${{matrix.make}}
5473
run: |
5574
make ${{matrix.make}}
@@ -62,8 +81,11 @@ jobs:
6281
6382
- name: Test run after install
6483
if: matrix.make == 'test-release'
65-
run: |
66-
./local_installation/bin/realmedium_sample --config=./local_installation/etc/realmedium_sample/static_config.yaml &
84+
run: >-
85+
./local_installation/bin/realmedium_sample
86+
--config=./local_installation/etc/realmedium_sample/static_config.yaml
87+
--config_vars=./local_installation/etc/realmedium_sample/config_vars.yaml
88+
&
6789
6890
- name: Check work run service
6991
if: matrix.make == 'test-release'

.github/workflows/docker.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ jobs:
2828
ccache-dir-${{github.ref}}_run-
2929
ccache-
3030
31+
- name: Install docker-compose
32+
run: |
33+
sudo apt update
34+
sudo apt install --allow-downgrades -y docker-compose
35+
3136
- name: Setup ccache
3237
run: docker-compose run --rm realmedium-sample bash -c 'ccache -M 4.0GB && ccache -s'
3338

CMakeLists.txt

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
cmake_minimum_required(VERSION 3.12)
22
project(realmedium_sample CXX)
33

4-
include(third_party/userver/cmake/SetupEnvironment.cmake)
5-
include(GNUInstallDirs)
64

7-
add_subdirectory(third_party/userver)
5+
# Adding userver dependency
6+
find_package(userver COMPONENTS core postgresql QUIET)
7+
if(NOT userver_FOUND) # Fallback to subdirectory usage
8+
# Enable userver libraries that are needed in this project
9+
set(USERVER_FEATURE_POSTGRESQL ON CACHE BOOL "" FORCE)
10+
11+
# Compatibility mode: some systems don't support these features
12+
set(USERVER_FEATURE_CRYPTOPP_BLAKE2 OFF CACHE BOOL "" FORCE)
13+
set(USERVER_FEATURE_GRPC_CHANNELZ OFF CACHE BOOL "" FORCE)
14+
set(USERVER_FEATURE_REDIS_HI_MALLOC ON CACHE BOOL "" FORCE)
15+
16+
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/userver)
17+
message(STATUS "Using userver framework from third_party/userver")
18+
add_subdirectory(third_party/userver)
19+
else()
20+
message(FATAL_ERROR "Either install the userver or provide a path to it")
21+
endif()
22+
endif()
23+
24+
userver_setup_environment()
25+
826

927
set(CPP_JWT_BUILD_TESTS OFF)
1028
set(CPP_JWT_BUILD_EXAMPLES OFF)
@@ -98,7 +116,7 @@ add_library(${PROJECT_NAME}_objs OBJECT
98116
)
99117

100118
include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
101-
target_link_libraries(${PROJECT_NAME}_objs PUBLIC userver-core userver-postgresql)
119+
target_link_libraries(${PROJECT_NAME}_objs PUBLIC userver::core userver::postgresql)
102120

103121
target_include_directories(${PROJECT_NAME}_objs PUBLIC cpp-jwt)
104122
target_link_libraries(${PROJECT_NAME}_objs PUBLIC cpp-jwt)
@@ -118,12 +136,16 @@ add_executable(${PROJECT_NAME}_unittest
118136
src/validators/validator_test.cpp
119137
)
120138

121-
target_link_libraries(${PROJECT_NAME}_unittest PRIVATE ${PROJECT_NAME}_objs userver-utest)
139+
target_link_libraries(${PROJECT_NAME}_unittest PRIVATE ${PROJECT_NAME}_objs userver::utest)
122140
add_google_tests(${PROJECT_NAME}_unittest)
123141

124142
# Functional Tests
125143
add_subdirectory(tests)
126144

145+
146+
# Install
147+
include(GNUInstallDirs)
148+
127149
if(DEFINED ENV{PREFIX})
128150
message(STATUS "Set install prefix: $ENV{PREFIX}")
129151
file(TO_CMAKE_PATH "$ENV{PREFIX}" PREFIX_PATH)
@@ -136,7 +158,7 @@ set(CONFIG_JWT ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_SYSCONFDIR}/${PROJECT_NAM
136158

137159
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configs/static_config.yaml.in ${CMAKE_CURRENT_SOURCE_DIR}/configs/static_config.yaml)
138160

139-
FILE(GLOB CONFIGS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.yaml ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.json)
161+
file(GLOB CONFIGS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.yaml ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.json)
140162

141163
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${PROJECT_NAME})
142164
install(FILES ${CONFIGS_FILES} DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/${PROJECT_NAME} COMPONENT ${PROJECT_NAME})

Makefile

Lines changed: 73 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
CMAKE_COMMON_FLAGS ?= -DUSERVER_OPEN_SOURCE_BUILD=1 -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
2-
32
CMAKE_DEBUG_FLAGS ?= -DUSERVER_SANITIZE='addr ub'
43
CMAKE_RELEASE_FLAGS ?=
54
CMAKE_OS_FLAGS ?= -DUSERVER_FEATURE_CRYPTOPP_BLAKE2=0 -DUSERVER_FEATURE_REDIS_HI_MALLOC=1
65
NPROCS ?= $(shell nproc)
76
CLANG_FORMAT ?= clang-format
7+
DOCKER_COMPOSE ?= docker-compose
88

99
ifeq ($(KERNEL),Darwin)
1010
CMAKE_COMMON_FLAGS += -DUSERVER_NO_WERROR=1 -DUSERVER_CHECK_PACKAGE_VERSIONS=0 \
@@ -17,124 +17,91 @@ CMAKE_COMMON_FLAGS += -DUSERVER_NO_WERROR=1 -DUSERVER_CHECK_PACKAGE_VERSIONS=0 \
1717
endif
1818

1919

20-
# NOTE: use Makefile.local for customization
20+
# NOTE: use Makefile.local to override the options defined above.
2121
-include Makefile.local
2222

23+
CMAKE_DEBUG_FLAGS += -DCMAKE_BUILD_TYPE=Debug $(CMAKE_COMMON_FLAGS)
24+
CMAKE_RELEASE_FLAGS += -DCMAKE_BUILD_TYPE=Release $(CMAKE_COMMON_FLAGS)
25+
26+
.PHONY: all
2327
all: test-debug test-release
2428

25-
# Debug cmake configuration
26-
build_debug/Makefile:
27-
@git submodule update --init
28-
@mkdir -p build_debug
29-
@cd build_debug && \
30-
cmake -DCMAKE_BUILD_TYPE=Debug $(CMAKE_COMMON_FLAGS) $(CMAKE_DEBUG_FLAGS) $(CMAKE_OS_FLAGS) $(CMAKE_OPTIONS) ..
31-
32-
# Release cmake configuration
33-
build_release/Makefile:
34-
@git submodule update --init
35-
@mkdir -p build_release
36-
@cd build_release && \
37-
cmake -DCMAKE_BUILD_TYPE=Release $(CMAKE_COMMON_FLAGS) $(CMAKE_RELEASE_FLAGS) $(CMAKE_OS_FLAGS) $(CMAKE_OPTIONS) ..
38-
39-
# build using cmake
40-
build-impl-%: build_%/Makefile
41-
@cmake --build build_$* -j $(NPROCS) --target realmedium_sample
42-
43-
# test
44-
test-impl-%: build-impl-%
45-
@cmake --build build_$* -j $(NPROCS) --target realmedium_sample_unittest
46-
@cd build_$* && ((test -t 1 && GTEST_COLOR=1 PYTEST_ADDOPTS="--color=yes" ctest -V) || ctest -V)
47-
@pep8 tests
48-
49-
# testsuite service runner
50-
service-impl-start-%: build-impl-%
51-
@cd ./build_$* && $(MAKE) start-realmedium-sample
52-
53-
# clean
54-
clean-impl-%:
55-
cd build_$* && $(MAKE) clean
56-
57-
# dist-clean
58-
.PHONY: dist-clean
59-
dist-clean:
60-
@rm -rf build_*
61-
@rm -f ./configs/static_config.yaml
29+
# Run cmake
30+
.PHONY: cmake-debug
31+
cmake-debug:
32+
cmake -B build_debug $(CMAKE_DEBUG_FLAGS)
6233

63-
# format
64-
.PHONY: format
65-
format:
66-
@find src -name '*pp' -type f | xargs $(CLANG_FORMAT) -i
67-
@find tests -name '*.py' -type f | xargs autopep8 -i
68-
69-
.PHONY: cmake-debug build-debug test-debug clean-debug cmake-release build-release test-release clean-release install install-debug docker-cmake-debug docker-build-debug docker-test-debug docker-clean-debug docker-cmake-release docker-build-release docker-test-release docker-clean-release docker-install docker-install-debug docker-start-service-debug docker-start-service docker-clean-data
70-
71-
install-debug: build-debug
72-
@cd build_debug && \
73-
cmake --install . -v --component realmedium_sample
74-
75-
install: build-release
76-
@cd build_release && \
77-
cmake --install . -v --component realmedium_sample
78-
79-
# Hide target, use only in docker environment
80-
--debug-start-in-docker: install
81-
@ulimit -n 100000
82-
@sed -i 's/config_vars.yaml/config_vars.docker.yaml/g' /home/user/.local/etc/realmedium_sample/static_config.yaml
83-
@psql 'postgresql://user:password@service-postgres:5432/realmedium_db-1' -f ./postgresql/schemas/db-1.sql
84-
@/home/user/.local/bin/realmedium_sample \
85-
--config /home/user/.local/etc/realmedium_sample/static_config.yaml
86-
87-
# Hide target, use only in docker environment
88-
--debug-start-in-docker-debug: install-debug
89-
@sed -i 's/config_vars.yaml/config_vars.docker.yaml/g' /home/user/.local/etc/realmedium_sample/static_config.yaml
90-
@psql 'postgresql://user:password@service-postgres:5432/realmedium_db-1' -f ./postgresql/schemas/db-1.sql
91-
@/home/user/.local/bin/realmedium_sample \
92-
--config /home/user/.local/etc/realmedium_sample/static_config.yaml
93-
94-
# Start targets makefile in docker enviroment
95-
docker-impl-%:
96-
docker-compose run --rm realmedium-sample make $*
97-
98-
# Build and runs service in docker environment
99-
docker-start-service-debug:
100-
@docker-compose run -p 8080:8080 --rm realmedium-sample make -- --debug-start-in-docker-debug
101-
102-
# Build and runs service in docker environment
103-
docker-start-service:
104-
@docker-compose run -p 8080:8080 --rm realmedium-sample make -- --debug-start-in-docker
34+
.PHONY: cmake-release
35+
cmake-release:
36+
cmake -B build_release $(CMAKE_RELEASE_FLAGS)
10537

106-
# Stop docker container and remove PG data
107-
docker-clean-data:
108-
@docker-compose down -v
109-
@rm -rf ./.pgdata
38+
build_debug/CMakeCache.txt: cmake-debug
39+
build_release/CMakeCache.txt: cmake-release
11040

111-
# Explicitly specifying the targets to help shell with completions
112-
cmake-debug: build_debug/Makefile
113-
cmake-release: build_release/Makefile
41+
# Build using cmake
42+
.PHONY: build-debug build-release
43+
build-debug build-release: build-%: build_%/CMakeCache.txt
44+
cmake --build build_$* -j $(NPROCS) --target realmedium_sample
11445

115-
build-debug: build-impl-debug
116-
build-release: build-impl-release
46+
# Test
47+
.PHONY: test-debug test-release
48+
test-debug test-release: test-%: build-%
49+
cmake --build build_$* -j $(NPROCS) --target realmedium_sample_unittest
50+
cd build_$* && ((test -t 1 && GTEST_COLOR=1 PYTEST_ADDOPTS="--color=yes" ctest -V) || ctest -V)
51+
pycodestyle tests
11752

118-
test-debug: test-impl-debug
119-
test-release: test-impl-release
53+
# Start the service (via testsuite service runner)
54+
.PHONY: service-start-debug service-start-release
55+
service-start-debug service-start-release: service-start-%: build-%
56+
cmake --build build_$* -v --target start-realmedium_sample
12057

121-
service-start-debug: service-impl-start-debug
122-
service-start-release: service-impl-start-release
58+
# Cleanup data
59+
.PHONY: clean-debug clean-release
60+
clean-debug clean-release: clean-%:
61+
cmake --build build_$* --target clean
12362

124-
clean-debug: clean-impl-debug
125-
clean-release: clean-impl-release
63+
.PHONY: dist-clean
64+
dist-clean:
65+
rm -rf build_*
66+
rm -rf tests/__pycache__/
67+
rm -rf tests/.pytest_cache/
68+
69+
# Install
70+
.PHONY: install-debug install-release
71+
install-debug install-release: install-%: build-%
72+
cmake --install build_$* -v --component realmedium_sample
73+
74+
.PHONY: install
75+
install: install-release
76+
77+
# Format the sources
78+
.PHONY: format
79+
format:
80+
find src -name '*pp' -type f | xargs $(CLANG_FORMAT) -i
81+
find tests -name '*.py' -type f | xargs autopep8 -i
12682

127-
docker-cmake-debug: docker-impl-cmake-debug
128-
docker-cmake-release: docker-impl-cmake-release
83+
# Set environment for --in-docker-start
84+
export DB_CONNECTION := postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@service-postgres:5432/${POSTGRES_DB}
12985

130-
docker-build-debug: docker-impl-build-debug
131-
docker-build-release: docker-impl-build-release
86+
# Internal hidden targets that are used only in docker environment
87+
--in-docker-start-debug --in-docker-start-release: --in-docker-start-%: install-%
88+
psql ${DB_CONNECTION} -f ./postgresql/data/initial_data.sql
89+
/home/user/.local/bin/realmedium_sample \
90+
--config /home/user/.local/etc/realmedium_sample/static_config.yaml \
91+
--config_vars /home/user/.local/etc/realmedium_sample/config_vars.docker.yaml
13292

133-
docker-test-debug: docker-impl-test-debug
134-
docker-test-release: docker-impl-test-release
93+
# Build and run service in docker environment
94+
.PHONY: docker-start-service-debug docker-start-service-release
95+
docker-start-service-debug docker-start-service-release: docker-start-service-%:
96+
$(DOCKER_COMPOSE) run -p 8080:8080 --rm realmedium-sample make -- --in-docker-start-$*
13597

136-
docker-clean-debug: docker-impl-clean-debug
137-
docker-clean-release: docker-impl-clean-release
98+
# Start targets makefile in docker environment
99+
.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
100+
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-%:
101+
$(DOCKER_COMPOSE) run --rm realmedium-sample make $*
138102

139-
docker-install: docker-impl-install
140-
docker-install-debug: docker-impl-install-debug
103+
# Stop docker container and remove PG data
104+
.PHONY: docker-clean-data
105+
docker-clean-data:
106+
$(DOCKER_COMPOSE) down -v
107+
rm -rf ./.pgdata

configs/config_vars.docker.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
worker-threads: 4
22
worker-fs-threads: 2
3-
logger-level: critical
3+
logger-level: debug
44

55
is_testing: false
66

77
server-port: 8080
88

9-
dbconnection: 'postgresql://user:password@service-postgres:5432/realmedium_db-1'
10-

configs/config_vars.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
worker-threads: 4
22
worker-fs-threads: 2
3-
logger-level: error
3+
logger-level: info
44

55
is_testing: false
66

77
server-port: 8080
88

9-
dbconnection: 'postgresql://user:password@service-postgres:5432/realmedium_db-1'
10-
9+
dbconnection: 'postgresql://realmedium_sample_user:password@localhost:5432/realmedium_db-1'

configs/config_vars_testing.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ is_testing: true
66

77
server-port: 8080
88

9-
dbconnection: 'postgresql://testsuite@localhost:15433/realmedium_db_1'
9+
dbconnection: 'postgresql://testsuite@localhost:15433/realmedium_db-1'

0 commit comments

Comments
 (0)