diff --git a/CMakeLists.txt b/CMakeLists.txt index afc449c..3124775 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,10 @@ cmake_minimum_required(VERSION 3.12) -project(pg_grpc_service_template CXX) + +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) + +project(pg_grpc_service_template VERSION 0.0.1) + +include(Installing) # Adding userver dependency find_package(userver COMPONENTS core grpc postgresql QUIET) @@ -23,21 +28,29 @@ endif() userver_setup_environment() +# Create a proto library with userver extensions +include(GrpcTargets) +userver_add_grpc_library(${PROJECT_NAME}_proto PROTOS handlers/hello.proto) + +# Create client library +template_add_library( + LIB_NAME ${PROJECT_NAME}_client + LIB_NAMESPACE ${PROJECT_NAME} + PUBLIC_HDRS + src/hello_client.hpp + SRCS + src/hello_client.cpp + PROTOS + proto/handlers/hello.proto +) +target_link_libraries(${PROJECT_NAME}_client PUBLIC $) # Common sources add_library(${PROJECT_NAME}_objs OBJECT src/hello.hpp src/hello.cpp - src/hello_client.hpp - src/hello_client.cpp ) -target_link_libraries(${PROJECT_NAME}_objs PUBLIC userver::postgresql userver::grpc) - -# Create a proto library with userver extensions -include(GrpcTargets) -userver_add_grpc_library(${PROJECT_NAME}_proto PROTOS handlers/hello.proto) -target_link_libraries(${PROJECT_NAME}_objs PUBLIC ${PROJECT_NAME}_proto) - +target_link_libraries(${PROJECT_NAME}_objs PUBLIC userver::postgresql userver::grpc ${PROJECT_NAME}_client) # The Service add_executable(${PROJECT_NAME} src/main.cpp) @@ -79,3 +92,5 @@ file(GLOB CONFIGS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.yaml ${CMAKE_CURRE install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${PROJECT_NAME}) install(FILES ${CONFIGS_FILES} DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/${PROJECT_NAME} COMPONENT ${PROJECT_NAME}) + +include(Packaging) \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e69de29 diff --git a/Makefile b/Makefile index 3616dc3..5775826 100644 --- a/Makefile +++ b/Makefile @@ -63,6 +63,14 @@ install-debug install-release: install-%: build-% .PHONY: install install: install-release +# CPack +.PHONY: cpack-debug cpack-release +cpack-debug cpack-release: cpack-%: build-% + cd build_$* && cpack -G DEB + +.PHONY: cpack +cpack: cpack-release + # Format the sources .PHONY: format format: diff --git a/README.md b/README.md index 6c3bdae..722d1ee 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ Makefile contains typicaly useful targets for development: * `make dist-clean` - clean all, including the CMake cached configurations * `make install` - does a `make build-release` and runs install in directory set in environment `PREFIX` * `make install-debug` - does a `make build-debug` and runs install in directory set in environment `PREFIX` +* `make cpack` - does a `make build-release` and runs cpack +* `make cpack-debug` - does a `make build-debug` and runs cpack * `make docker-COMMAND` - run `make COMMAND` in docker environment * `make docker-build-debug` - debug build of the service with all the assertions and sanitizers enabled in docker environment * `make docker-test-debug` - does a `make build-debug` and runs all the tests on the result in docker environment diff --git a/cmake/Config.cmake.in b/cmake/Config.cmake.in new file mode 100644 index 0000000..a48a6f7 --- /dev/null +++ b/cmake/Config.cmake.in @@ -0,0 +1,5 @@ +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/@TEMPLATE_LIB_NAME@Targets.cmake") + +check_required_components(@TEMPLATE_LIB_NAME@) diff --git a/cmake/Installing.cmake b/cmake/Installing.cmake new file mode 100644 index 0000000..6df6dff --- /dev/null +++ b/cmake/Installing.cmake @@ -0,0 +1,58 @@ +include(GNUInstallDirs) +include(CMakePackageConfigHelpers) + +function(template_add_library) + set(options) + set(one_value_args LIB_NAME LIB_NAMESPACE) + set(multi_value_args PUBLIC_HDRS SRCS PROTOS) + cmake_parse_arguments(TEMPLATE "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) + + add_library(${TEMPLATE_LIB_NAME}) + target_sources(${TEMPLATE_LIB_NAME} PRIVATE "${TEMPLATE_SRCS}") + target_include_directories(${TEMPLATE_LIB_NAME} + PRIVATE + # where the library itself will look for its internal headers + ${CMAKE_CURRENT_SOURCE_DIR}/src + PUBLIC + # where top-level project will look for the library's public headers + $ + # where external projects will look for the library's public headers + $ + ) + set_target_properties(${TEMPLATE_LIB_NAME} PROPERTIES DEBUG_POSTFIX "-d") + + set_target_properties(${TEMPLATE_LIB_NAME} PROPERTIES PUBLIC_HEADER "${TEMPLATE_PUBLIC_HDRS}") + + install(TARGETS ${TEMPLATE_LIB_NAME} + COMPONENT ${TEMPLATE_LIB_NAME} + EXPORT ${TEMPLATE_LIB_NAME}Targets + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${TEMPLATE_LIB_NAME} + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + ) + + install(EXPORT ${TEMPLATE_LIB_NAME}Targets + COMPONENT ${TEMPLATE_LIB_NAME} + FILE ${TEMPLATE_LIB_NAME}Targets.cmake + NAMESPACE ${TEMPLATE_LIB_NAMESPACE}:: + DESTINATION cmake + ) + + write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/${TEMPLATE_LIB_NAME}ConfigVersion.cmake + COMPATIBILITY AnyNewerVersion + ) + + configure_package_config_file(${PROJECT_SOURCE_DIR}/cmake/Config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/${TEMPLATE_LIB_NAME}Config.cmake + INSTALL_DESTINATION cmake + ) + + install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/${TEMPLATE_LIB_NAME}Config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/${TEMPLATE_LIB_NAME}ConfigVersion.cmake + DESTINATION cmake + COMPONENT ${TEMPLATE_LIB_NAME} + ) + + install(FILES "${TEMPLATE_PROTOS}" DESTINATION proto/${TEMPLATE_LIB_NAME} COMPONENT ${TEMPLATE_LIB_NAME}) +endfunction() \ No newline at end of file diff --git a/cmake/Packaging.cmake b/cmake/Packaging.cmake new file mode 100644 index 0000000..d673a9b --- /dev/null +++ b/cmake/Packaging.cmake @@ -0,0 +1,26 @@ +set(CPACK_PACKAGE_NAME ${PROJECT_NAME}) +set(CPACK_PACKAGE_DESCRIPTION_SUMMARY) +set(CPACK_PACKAGE_VENDOR "Template Company") + +set(CPACK_VERBATIM_VARIABLES YES) + +set(CPACK_PACKAGE_INSTALL_DIRECTORY ${CPACK_PACKAGE_NAME}) +SET(CPACK_OUTPUT_FILE_PREFIX ${CMAKE_BINARY_DIR}/_packages) +set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}) + +set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) +set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR}) +set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH}) + +set(CPACK_PACKAGE_CONTACT "template@template.com") +set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Template") + +set(CPACK_RESOURCE_FILE_LICENSE ${PROJECT_SOURCE_DIR}/LICENSE) +set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.md) + +set(CPACK_DEBIAN_PACKAGE_DEPENDS "") +set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT) +set(CPACK_COMPONENTS_GROUPING ONE_PER_GROUP) +set(CPACK_DEB_COMPONENT_INSTALL YES) + +include(CPack) \ No newline at end of file