Skip to content

Commit

Permalink
Updated CMakeLists.txt, CI and userver submodule (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Liiizak authored Nov 6, 2024
1 parent c886d7b commit 5643477
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 145 deletions.
38 changes: 30 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ jobs:
fail-fast: false
matrix:
include:
- os: ubuntu-20.04
- os: ubuntu-22.04
make: test-debug
info: g++-9 + test-debug
info: g++-11 + test-debug

- os: ubuntu-20.04
- os: ubuntu-22.04
make: test-release
info: g++-9 + test-release
info: g++-11 + test-release

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

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
submodules: true

- name: Reuse ccache directory
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/.ccache
key: '${{matrix.os}} ${{matrix.info}} ccache-dir ${{github.ref}} run-${{github.run_number}}'
Expand All @@ -43,13 +43,32 @@ jobs:
- name: Install packages
run: |
sudo apt update
sudo apt install --allow-downgrades -y postgresql $(cat third_party/userver/scripts/docs/en/deps/${{matrix.os}}.md | tr '\n' ' ')
sudo apt install --allow-downgrades -y pep8 $(cat third_party/userver/scripts/docs/en/deps/${{matrix.os}}.md | tr '\n' ' ')
- name: Reinstall postgres 14
run: |
sudo apt purge libpq5 libpq-dev postgresql-*
sudo apt install -y postgresql-14 postgresql-client-14 postgresql-server-dev-14
- name: Setup ccache
run: |
ccache -M 2.0GB
ccache -s
- name: Run postgresql
if: matrix.make == 'test-release'
run: |
pg_lsclusters
sudo -u postgres psql --command="CREATE USER realmedium_sample_user PASSWORD 'password'" --command="\du"
sudo -u postgres createdb --owner=realmedium_sample_user realmedium_db-1
- name: Run migrations
if: matrix.make == 'test-release'
run: |
PGPASSWORD=password psql 'postgresql://realmedium_sample_user@localhost:5432/realmedium_db-1' -f ./postgresql/schemas/db-1.sql
- name: Run ${{matrix.make}}
run: |
make ${{matrix.make}}
Expand All @@ -62,8 +81,11 @@ jobs:
- name: Test run after install
if: matrix.make == 'test-release'
run: |
./local_installation/bin/realmedium_sample --config=./local_installation/etc/realmedium_sample/static_config.yaml &
run: >-
./local_installation/bin/realmedium_sample
--config=./local_installation/etc/realmedium_sample/static_config.yaml
--config_vars=./local_installation/etc/realmedium_sample/config_vars.yaml
&
- name: Check work run service
if: matrix.make == 'test-release'
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ jobs:
ccache-dir-${{github.ref}}_run-
ccache-
- name: Install docker-compose
run: |
sudo apt update
sudo apt install --allow-downgrades -y docker-compose
- name: Setup ccache
run: docker-compose run --rm realmedium-sample bash -c 'ccache -M 4.0GB && ccache -s'

Expand Down
34 changes: 28 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
cmake_minimum_required(VERSION 3.12)
project(realmedium_sample CXX)

include(third_party/userver/cmake/SetupEnvironment.cmake)
include(GNUInstallDirs)

add_subdirectory(third_party/userver)
# Adding userver dependency
find_package(userver COMPONENTS core postgresql QUIET)
if(NOT userver_FOUND) # Fallback to subdirectory usage
# Enable userver libraries that are needed in this project
set(USERVER_FEATURE_POSTGRESQL 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()
endif()

userver_setup_environment()


set(CPP_JWT_BUILD_TESTS OFF)
set(CPP_JWT_BUILD_EXAMPLES OFF)
Expand Down Expand Up @@ -98,7 +116,7 @@ add_library(${PROJECT_NAME}_objs OBJECT
)

include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(${PROJECT_NAME}_objs PUBLIC userver-core userver-postgresql)
target_link_libraries(${PROJECT_NAME}_objs PUBLIC userver::core userver::postgresql)

target_include_directories(${PROJECT_NAME}_objs PUBLIC cpp-jwt)
target_link_libraries(${PROJECT_NAME}_objs PUBLIC cpp-jwt)
Expand All @@ -118,12 +136,16 @@ add_executable(${PROJECT_NAME}_unittest
src/validators/validator_test.cpp
)

target_link_libraries(${PROJECT_NAME}_unittest PRIVATE ${PROJECT_NAME}_objs userver-utest)
target_link_libraries(${PROJECT_NAME}_unittest PRIVATE ${PROJECT_NAME}_objs userver::utest)
add_google_tests(${PROJECT_NAME}_unittest)

# Functional Tests
add_subdirectory(tests)


# Install
include(GNUInstallDirs)

if(DEFINED ENV{PREFIX})
message(STATUS "Set install prefix: $ENV{PREFIX}")
file(TO_CMAKE_PATH "$ENV{PREFIX}" PREFIX_PATH)
Expand All @@ -136,7 +158,7 @@ set(CONFIG_JWT ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_SYSCONFDIR}/${PROJECT_NAM

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

FILE(GLOB CONFIGS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.yaml ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.json)
file(GLOB CONFIGS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.yaml ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.json)

install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${PROJECT_NAME})
install(FILES ${CONFIGS_FILES} DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/${PROJECT_NAME} COMPONENT ${PROJECT_NAME})
179 changes: 73 additions & 106 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
CMAKE_COMMON_FLAGS ?= -DUSERVER_OPEN_SOURCE_BUILD=1 -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

CMAKE_DEBUG_FLAGS ?= -DUSERVER_SANITIZE='addr ub'
CMAKE_RELEASE_FLAGS ?=
CMAKE_OS_FLAGS ?= -DUSERVER_FEATURE_CRYPTOPP_BLAKE2=0 -DUSERVER_FEATURE_REDIS_HI_MALLOC=1
NPROCS ?= $(shell nproc)
CLANG_FORMAT ?= clang-format
DOCKER_COMPOSE ?= docker-compose

ifeq ($(KERNEL),Darwin)
CMAKE_COMMON_FLAGS += -DUSERVER_NO_WERROR=1 -DUSERVER_CHECK_PACKAGE_VERSIONS=0 \
Expand All @@ -17,124 +17,91 @@ CMAKE_COMMON_FLAGS += -DUSERVER_NO_WERROR=1 -DUSERVER_CHECK_PACKAGE_VERSIONS=0 \
endif


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

CMAKE_DEBUG_FLAGS += -DCMAKE_BUILD_TYPE=Debug $(CMAKE_COMMON_FLAGS)
CMAKE_RELEASE_FLAGS += -DCMAKE_BUILD_TYPE=Release $(CMAKE_COMMON_FLAGS)

.PHONY: all
all: test-debug test-release

# Debug cmake configuration
build_debug/Makefile:
@git submodule update --init
@mkdir -p build_debug
@cd build_debug && \
cmake -DCMAKE_BUILD_TYPE=Debug $(CMAKE_COMMON_FLAGS) $(CMAKE_DEBUG_FLAGS) $(CMAKE_OS_FLAGS) $(CMAKE_OPTIONS) ..

# Release cmake configuration
build_release/Makefile:
@git submodule update --init
@mkdir -p build_release
@cd build_release && \
cmake -DCMAKE_BUILD_TYPE=Release $(CMAKE_COMMON_FLAGS) $(CMAKE_RELEASE_FLAGS) $(CMAKE_OS_FLAGS) $(CMAKE_OPTIONS) ..

# build using cmake
build-impl-%: build_%/Makefile
@cmake --build build_$* -j $(NPROCS) --target realmedium_sample

# test
test-impl-%: build-impl-%
@cmake --build build_$* -j $(NPROCS) --target realmedium_sample_unittest
@cd build_$* && ((test -t 1 && GTEST_COLOR=1 PYTEST_ADDOPTS="--color=yes" ctest -V) || ctest -V)
@pep8 tests

# testsuite service runner
service-impl-start-%: build-impl-%
@cd ./build_$* && $(MAKE) start-realmedium-sample

# clean
clean-impl-%:
cd build_$* && $(MAKE) clean

# dist-clean
.PHONY: dist-clean
dist-clean:
@rm -rf build_*
@rm -f ./configs/static_config.yaml
# Run cmake
.PHONY: cmake-debug
cmake-debug:
cmake -B build_debug $(CMAKE_DEBUG_FLAGS)

# format
.PHONY: format
format:
@find src -name '*pp' -type f | xargs $(CLANG_FORMAT) -i
@find tests -name '*.py' -type f | xargs autopep8 -i

.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

install-debug: build-debug
@cd build_debug && \
cmake --install . -v --component realmedium_sample

install: build-release
@cd build_release && \
cmake --install . -v --component realmedium_sample

# Hide target, use only in docker environment
--debug-start-in-docker: install
@ulimit -n 100000
@sed -i 's/config_vars.yaml/config_vars.docker.yaml/g' /home/user/.local/etc/realmedium_sample/static_config.yaml
@psql 'postgresql://user:password@service-postgres:5432/realmedium_db-1' -f ./postgresql/schemas/db-1.sql
@/home/user/.local/bin/realmedium_sample \
--config /home/user/.local/etc/realmedium_sample/static_config.yaml

# Hide target, use only in docker environment
--debug-start-in-docker-debug: install-debug
@sed -i 's/config_vars.yaml/config_vars.docker.yaml/g' /home/user/.local/etc/realmedium_sample/static_config.yaml
@psql 'postgresql://user:password@service-postgres:5432/realmedium_db-1' -f ./postgresql/schemas/db-1.sql
@/home/user/.local/bin/realmedium_sample \
--config /home/user/.local/etc/realmedium_sample/static_config.yaml

# Start targets makefile in docker enviroment
docker-impl-%:
docker-compose run --rm realmedium-sample make $*

# Build and runs service in docker environment
docker-start-service-debug:
@docker-compose run -p 8080:8080 --rm realmedium-sample make -- --debug-start-in-docker-debug

# Build and runs service in docker environment
docker-start-service:
@docker-compose run -p 8080:8080 --rm realmedium-sample make -- --debug-start-in-docker
.PHONY: cmake-release
cmake-release:
cmake -B build_release $(CMAKE_RELEASE_FLAGS)

# Stop docker container and remove PG data
docker-clean-data:
@docker-compose down -v
@rm -rf ./.pgdata
build_debug/CMakeCache.txt: cmake-debug
build_release/CMakeCache.txt: cmake-release

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

build-debug: build-impl-debug
build-release: build-impl-release
# Test
.PHONY: test-debug test-release
test-debug test-release: test-%: build-%
cmake --build build_$* -j $(NPROCS) --target realmedium_sample_unittest
cd build_$* && ((test -t 1 && GTEST_COLOR=1 PYTEST_ADDOPTS="--color=yes" ctest -V) || ctest -V)
pycodestyle tests

test-debug: test-impl-debug
test-release: test-impl-release
# Start the service (via testsuite service runner)
.PHONY: service-start-debug service-start-release
service-start-debug service-start-release: service-start-%: build-%
cmake --build build_$* -v --target start-realmedium_sample

service-start-debug: service-impl-start-debug
service-start-release: service-impl-start-release
# Cleanup data
.PHONY: clean-debug clean-release
clean-debug clean-release: clean-%:
cmake --build build_$* --target clean

clean-debug: clean-impl-debug
clean-release: clean-impl-release
.PHONY: dist-clean
dist-clean:
rm -rf build_*
rm -rf tests/__pycache__/
rm -rf tests/.pytest_cache/

# Install
.PHONY: install-debug install-release
install-debug install-release: install-%: build-%
cmake --install build_$* -v --component realmedium_sample

.PHONY: install
install: install-release

# Format the sources
.PHONY: format
format:
find src -name '*pp' -type f | xargs $(CLANG_FORMAT) -i
find tests -name '*.py' -type f | xargs autopep8 -i

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

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

docker-test-debug: docker-impl-test-debug
docker-test-release: docker-impl-test-release
# Build and run service in docker environment
.PHONY: docker-start-service-debug docker-start-service-release
docker-start-service-debug docker-start-service-release: docker-start-service-%:
$(DOCKER_COMPOSE) run -p 8080:8080 --rm realmedium-sample make -- --in-docker-start-$*

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

docker-install: docker-impl-install
docker-install-debug: docker-impl-install-debug
# Stop docker container and remove PG data
.PHONY: docker-clean-data
docker-clean-data:
$(DOCKER_COMPOSE) down -v
rm -rf ./.pgdata
4 changes: 1 addition & 3 deletions configs/config_vars.docker.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
worker-threads: 4
worker-fs-threads: 2
logger-level: critical
logger-level: debug

is_testing: false

server-port: 8080

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

5 changes: 2 additions & 3 deletions configs/config_vars.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
worker-threads: 4
worker-fs-threads: 2
logger-level: error
logger-level: info

is_testing: false

server-port: 8080

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

dbconnection: 'postgresql://realmedium_sample_user:password@localhost:5432/realmedium_db-1'
2 changes: 1 addition & 1 deletion configs/config_vars_testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ is_testing: true

server-port: 8080

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

0 comments on commit 5643477

Please sign in to comment.