From ae2322cd4f1f89a3a09a29cb923682fe18627a76 Mon Sep 17 00:00:00 2001 From: KangLin Date: Thu, 8 Jul 2021 17:04:26 +0800 Subject: [PATCH] Add cmake --- qtservice/CMakeLists.txt | 149 ++++++++++++++++++ qtservice/cmake/QtService.pc.in | 12 ++ qtservice/cmake/QtServiceConfig.cmake.in | 11 ++ qtservice/cmake/cmake_uninstall.cmake.in | 57 +++++++ qtservice/examples/CMakeLists.txt | 3 + qtservice/examples/controller/CMakeLists.txt | 6 + qtservice/examples/interactive/CMakeLists.txt | 26 +++ qtservice/examples/server/CMakeLists.txt | 31 ++++ qtservice/src/CMakeLists.txt | 132 ++++++++++++++++ 9 files changed, 427 insertions(+) create mode 100644 qtservice/CMakeLists.txt create mode 100644 qtservice/cmake/QtService.pc.in create mode 100644 qtservice/cmake/QtServiceConfig.cmake.in create mode 100644 qtservice/cmake/cmake_uninstall.cmake.in create mode 100644 qtservice/examples/CMakeLists.txt create mode 100644 qtservice/examples/controller/CMakeLists.txt create mode 100644 qtservice/examples/interactive/CMakeLists.txt create mode 100644 qtservice/examples/server/CMakeLists.txt create mode 100644 qtservice/src/CMakeLists.txt diff --git a/qtservice/CMakeLists.txt b/qtservice/CMakeLists.txt new file mode 100644 index 00000000..c2cfc256 --- /dev/null +++ b/qtservice/CMakeLists.txt @@ -0,0 +1,149 @@ +# Author: Kang Lin + +cmake_minimum_required(VERSION 3.5) + +project(QtService) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) +#set(CMAKE_CXX_STANDARD 11) +#set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "verbose") + +#打开 qt 编译工具 +SET(CMAKE_AUTOUIC ON) +SET(CMAKE_AUTOMOC ON) +SET(CMAKE_AUTORCC ON) +SET(CMAKE_VERBOSE_MAKEFILE ON) + +#需要的QT组件 +if(CMAKE_VERSION VERSION_LESS "3.7.0") + set(CMAKE_INCLUDE_CURRENT_DIR ON) +endif() +SET(QT_COMPONENTS Widgets) +find_package(QT NAMES Qt6 Qt5 COMPONENTS Core) +find_package(Qt${QT_VERSION_MAJOR} COMPONENTS ${QT_COMPONENTS}) +message("QT_VERSION:${Qt${QT_VERSION_MAJOR}_VERSION}") +if(Qt${QT_VERSION_MAJOR}_VERSION VERSION_LESS 5.10.0 AND ANDROID) + message(FATAL_ERROR "Qt must great 5.10.0") +endif() +if(Qt${QT_VERSION_MAJOR}_FOUND) + FOREACH(_COMPONENT ${QT_COMPONENTS}) + SET(QT_LIBRARIES ${QT_LIBRARIES} Qt${QT_VERSION_MAJOR}::${_COMPONENT}) + ENDFOREACH() +endif() +get_filename_component(QT_INSTALL_DIR "${Qt${QT_VERSION_MAJOR}_DIR}/../../.." ABSOLUTE) +message("QT_INSTALL_DIR:${QT_INSTALL_DIR}") +message("QT_LIBRARIES:${QT_LIBRARIES}") + +if(POLICY CMP0083) + cmake_policy(SET CMP0083 NEW) +endif() + +if(POLICY CMP0020) + cmake_policy(SET CMP0020 NEW) +endif() + +SET(BUILD_VERSION "v0.0.1") +# Find Git Version Patch +IF(EXISTS "${CMAKE_SOURCE_DIR}/.git") + if(NOT GIT) + SET(GIT $ENV{GIT}) + endif() + if(NOT GIT) + FIND_PROGRAM(GIT NAMES git git.exe git.cmd) + endif() + IF(GIT) + EXECUTE_PROCESS( + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMAND ${GIT} describe --tags + OUTPUT_VARIABLE GIT_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if(NOT GIT_VERSION) + EXECUTE_PROCESS( + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMAND ${GIT} rev-parse --short HEAD + OUTPUT_VARIABLE GIT_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE + ) + endif() + SET(BUILD_VERSION ${GIT_VERSION}) + ENDIF() +ENDIF() +message("BUILD_VERSION:${BUILD_VERSION}") +set(VERSION ${BUILD_VERSION}) + +if(NOT DEFINED CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "Release") +endif(NOT DEFINED CMAKE_BUILD_TYPE) +string(TOLOWER "${CMAKE_BUILD_TYPE}" build_type) +if("debug" STREQUAL build_type) + add_definitions(-D_DEBUG) +endif() + +IF(MSVC) + # This option is to enable the /MP switch for Visual Studio 2005 and above compilers + OPTION(WIN32_USE_MP "Set to ON to build with the /MP option (Visual Studio 2005 and above)." ON) + MARK_AS_ADVANCED(WIN32_USE_MP) + IF(WIN32_USE_MP) + #SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP") + add_compile_options(/MP) + ENDIF(WIN32_USE_MP) + add_compile_options("$<$:/utf-8>") + add_compile_options("$<$:/utf-8>") +ENDIF(MSVC) + +SET(BUILD_SHARED_LIBS ON CACHE BOOL "Build shared libs") +if (BUILD_SHARED_LIBS) + add_definitions(-DBUILD_SHARED_LIBS) + if (CMAKE_COMPILER_IS_GNUCXX AND NOT MINGW) + # Just setting CMAKE_POSITION_INDEPENDENT_CODE should be enough to set + # -fPIC for GCC but sometimes it still doesn't get set, so make sure it + # does. + add_definitions("-fPIC") + endif() + set(CMAKE_POSITION_INDEPENDENT_CODE ON) +else(BUILD_SHARED_LIBS) + add_definitions(-DQT_STATICPLUGIN) +endif(BUILD_SHARED_LIBS) + +include(CMakePackageConfigHelpers) +include(GNUInstallDirs) +include(GenerateExportHeader) +include(CheckIncludeFile) +include(CheckIncludeFileCXX) +include(CheckFunctionExists) + +#CHECK_INCLUDE_FILE_CXX("string" HAVE_STRING_H) +#check_include_file("math.h" HAVE_MATH_H) + +#check_function_exists("fabs" HAVE_FABS) + +set(BUILD_PLATFORM "${CMAKE_SYSTEM_NAME}") +# ---------------------------------------------------------------------------- +# Detect compiler and target platform architecture +# ---------------------------------------------------------------------------- +if(NOT ANDROID) + if(X86_64 OR CMAKE_SIZEOF_VOID_P EQUAL 8) + set(BUILD_ARCH x86_64) + elseif(X86 OR CMAKE_SIZEOF_VOID_P EQUAL 4) + set(BUILD_ARCH x86) + endif() +else() + set(BUILD_ARCH ${CMAKE_SYSTEM_PROCESSOR}) +endif() + +add_subdirectory(src) + +option(BUILD_EXAMPLES "Build examples" ON) +if(BUILD_EXAMPLES) + add_subdirectory(examples) +endif(BUILD_EXAMPLES) + +# Create will be delete files +CONFIGURE_FILE( + "${CMAKE_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in" + "${CMAKE_BINARY_DIR}/cmake_uninstall.cmake" + IMMEDIATE @ONLY) +# Create unistall target +ADD_CUSTOM_TARGET(uninstall + "${CMAKE_COMMAND}" -P "${CMAKE_BINARY_DIR}/cmake_uninstall.cmake" + ) diff --git a/qtservice/cmake/QtService.pc.in b/qtservice/cmake/QtService.pc.in new file mode 100644 index 00000000..33245f9b --- /dev/null +++ b/qtservice/cmake/QtService.pc.in @@ -0,0 +1,12 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=@CMAKE_INSTALL_PREFIX@ +libdir=@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@ +includedir=@CMAKE_INSTALL_PREFIX@/include + +Name: @PROJECT_NAME@ +Description: +Version: @BUILD_VERSION@ +Requires: +Requires.private: +Libs: -L${libdir} -l@PROJECT_NAME@ +Cflags: -I${includedir} diff --git a/qtservice/cmake/QtServiceConfig.cmake.in b/qtservice/cmake/QtServiceConfig.cmake.in new file mode 100644 index 00000000..a05906a6 --- /dev/null +++ b/qtservice/cmake/QtServiceConfig.cmake.in @@ -0,0 +1,11 @@ +@PACKAGE_INIT@ + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/") + +include(CMakeFindDependencyMacro) +foreach(dependens_lib @QTSERVICE_DEPENDENS@) + find_dependency(${dependens_lib}) +endforeach() + +include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") + diff --git a/qtservice/cmake/cmake_uninstall.cmake.in b/qtservice/cmake/cmake_uninstall.cmake.in new file mode 100644 index 00000000..a2dee191 --- /dev/null +++ b/qtservice/cmake/cmake_uninstall.cmake.in @@ -0,0 +1,57 @@ +# Author: Kang Lin(kl222@126.com) + +# Use: Please add the follow code to CMakeLists.txt + +# # Install runtime target +# add_custom_target(install-runtime +# COMMAND +# "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=Runtime +# -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_install.cmake" +# ) +# # Uninstall runtime target +# add_custom_target(uninstall-runtime +# COMMAND +# "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=Runtime +# -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" +# ) +# # Create will be delete files +# CONFIGURE_FILE( +# "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in" +# "${CMAKE_BINARY_DIR}/cmake_uninstall.cmake" +# IMMEDIATE @ONLY) +# # Create unistall target +# ADD_CUSTOM_TARGET(uninstall +# "${CMAKE_COMMAND}" -P "${CMAKE_BINARY_DIR}/cmake_uninstall.cmake" +# DEPENDS uninstall-runtime) + + +if(CMAKE_INSTALL_COMPONENT) + set(CMAKE_INSTALL_MANIFEST "install_manifest_${CMAKE_INSTALL_COMPONENT}.txt") +else() + set(CMAKE_INSTALL_MANIFEST "install_manifest.txt") +endif() + +IF(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/${CMAKE_INSTALL_MANIFEST}") + MESSAGE(WARNING "Cannot find install manifest: \"@CMAKE_CURRENT_BINARY_DIR@/${CMAKE_INSTALL_MANIFEST}\"") +ELSE() + + FILE(READ "@CMAKE_CURRENT_BINARY_DIR@/${CMAKE_INSTALL_MANIFEST}" files) + STRING(REGEX REPLACE "\n" ";" files "${files}") + FOREACH(file ${files}) + MESSAGE(STATUS "Uninstalling \"${file}\"") + IF(EXISTS "${file}") + EXEC_PROGRAM( + "@CMAKE_COMMAND@" ARGS "-E remove \"${file}\"" + OUTPUT_VARIABLE rm_out + RETURN_VALUE rm_retval + ) + IF("${rm_retval}" STREQUAL 0) + ELSE("${rm_retval}" STREQUAL 0) + MESSAGE(FATAL_ERROR "Problem when removing \"${file}\"") + ENDIF("${rm_retval}" STREQUAL 0) + ELSE(EXISTS "${file}") + MESSAGE(STATUS "File \"${file}\" does not exist.") + ENDIF(EXISTS "${file}") + ENDFOREACH(file) + +ENDIF() diff --git a/qtservice/examples/CMakeLists.txt b/qtservice/examples/CMakeLists.txt new file mode 100644 index 00000000..aaa1c00a --- /dev/null +++ b/qtservice/examples/CMakeLists.txt @@ -0,0 +1,3 @@ +add_subdirectory(server) +add_subdirectory(controller) +add_subdirectory(interactive) diff --git a/qtservice/examples/controller/CMakeLists.txt b/qtservice/examples/controller/CMakeLists.txt new file mode 100644 index 00000000..f7cf3ec3 --- /dev/null +++ b/qtservice/examples/controller/CMakeLists.txt @@ -0,0 +1,6 @@ +# Author: Kang Lin + +project(controller) + +add_executable(${PROJECT_NAME} main.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE QtService) diff --git a/qtservice/examples/interactive/CMakeLists.txt b/qtservice/examples/interactive/CMakeLists.txt new file mode 100644 index 00000000..6b34fc2f --- /dev/null +++ b/qtservice/examples/interactive/CMakeLists.txt @@ -0,0 +1,26 @@ +# Author: Kang Lin + +project(interactive) + +#需要的QT组件 +if(CMAKE_VERSION VERSION_LESS "3.7.0") + set(CMAKE_INCLUDE_CURRENT_DIR ON) +endif() +SET(QT_COMPONENTS Core Gui Widgets) +find_package(QT NAMES Qt6 Qt5 COMPONENTS Core) +find_package(Qt${QT_VERSION_MAJOR} COMPONENTS ${QT_COMPONENTS}) +message("QT_VERSION:${Qt${QT_VERSION_MAJOR}_VERSION}") +if(Qt${QT_VERSION_MAJOR}_VERSION VERSION_LESS 5.10.0 AND ANDROID) + message(FATAL_ERROR "Qt must great 5.10.0") +endif() +if(Qt${QT_VERSION_MAJOR}_FOUND) + FOREACH(_COMPONENT ${QT_COMPONENTS}) + SET(QT_LIBRARIES ${QT_LIBRARIES} Qt${QT_VERSION_MAJOR}::${_COMPONENT}) + ENDFOREACH() +endif() +get_filename_component(QT_INSTALL_DIR "${Qt${QT_VERSION_MAJOR}_DIR}/../../.." ABSOLUTE) +message("QT_INSTALL_DIR:${QT_INSTALL_DIR}") +message("QT_LIBRARIES:${QT_LIBRARIES}") + +add_executable(${PROJECT_NAME} main.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE QtService ${QT_LIBRARIES}) diff --git a/qtservice/examples/server/CMakeLists.txt b/qtservice/examples/server/CMakeLists.txt new file mode 100644 index 00000000..7f355a9c --- /dev/null +++ b/qtservice/examples/server/CMakeLists.txt @@ -0,0 +1,31 @@ +# Author: Kang Lin + +project(httpservice) + +#需要的QT组件 +if(CMAKE_VERSION VERSION_LESS "3.7.0") + set(CMAKE_INCLUDE_CURRENT_DIR ON) +endif() + +list(APPEND QT_COMPONENTS Core Network) +if(ANDROID) + SET(QT_COMPONENTS ${QT_COMPONENTS} AndroidExtras) +endif() +find_package(QT NAMES Qt6 Qt5 COMPONENTS Core) +find_package(Qt${QT_VERSION_MAJOR} COMPONENTS ${QT_COMPONENTS}) +message("QT_VERSION:${Qt${QT_VERSION_MAJOR}_VERSION}") +if(Qt${QT_VERSION_MAJOR}_VERSION VERSION_LESS 5.10.0 AND ANDROID) + message(FATAL_ERROR "Qt must great 5.10.0") +endif() +if(Qt${QT_VERSION_MAJOR}_FOUND) + FOREACH(_COMPONENT ${QT_COMPONENTS}) + SET(QT_LIBRARIES ${QT_LIBRARIES} Qt${QT_VERSION_MAJOR}::${_COMPONENT}) + ENDFOREACH() +endif() +get_filename_component(QT_INSTALL_DIR "${Qt${QT_VERSION_MAJOR}_DIR}/../../.." ABSOLUTE) +message("QT_INSTALL_DIR:${QT_INSTALL_DIR}") +message("QT_LIBRARIES:${QT_LIBRARIES}") + +add_executable(${PROJECT_NAME} main.cpp) + +target_link_libraries(${PROJECT_NAME} PRIVATE QtService ${QT_LIBRARIES}) diff --git a/qtservice/src/CMakeLists.txt b/qtservice/src/CMakeLists.txt new file mode 100644 index 00000000..16130c27 --- /dev/null +++ b/qtservice/src/CMakeLists.txt @@ -0,0 +1,132 @@ +# Author: Kang Lin + +cmake_minimum_required(VERSION 3.5) + +project(QtService) + +#需要的QT组件 +if(CMAKE_VERSION VERSION_LESS "3.7.0") + set(CMAKE_INCLUDE_CURRENT_DIR ON) +endif() +SET(QT_COMPONENTS Core) +if(UNIX) + list(APPEND QT_COMPONENTS Network) +endif() +if(ANDROID) + SET(QT_COMPONENTS ${QT_COMPONENTS} AndroidExtras) +endif() +find_package(QT NAMES Qt6 Qt5 COMPONENTS Core) +find_package(Qt${QT_VERSION_MAJOR} COMPONENTS ${QT_COMPONENTS}) +message("QT_VERSION:${Qt${QT_VERSION_MAJOR}_VERSION}") +if(Qt${QT_VERSION_MAJOR}_VERSION VERSION_LESS 5.10.0 AND ANDROID) + message(FATAL_ERROR "Qt must great 5.10.0") +endif() +if(Qt${QT_VERSION_MAJOR}_FOUND) + FOREACH(_COMPONENT ${QT_COMPONENTS}) + SET(QT_LIBRARIES ${QT_LIBRARIES} Qt${QT_VERSION_MAJOR}::${_COMPONENT}) + ENDFOREACH() +endif() +get_filename_component(QT_INSTALL_DIR "${Qt${QT_VERSION_MAJOR}_DIR}/../../.." ABSOLUTE) +message("QT_INSTALL_DIR:${QT_INSTALL_DIR}") +message("QT_LIBRARIES:${QT_LIBRARIES}") + +#打开 qt 编译工具 +SET(CMAKE_AUTOUIC ON) +SET(CMAKE_AUTOMOC ON) +SET(CMAKE_AUTORCC ON) +SET(CMAKE_VERBOSE_MAKEFILE ON) + +set(SOURCE_FILES + qtservice.cpp + ) + +set(INSTALL_HEADER_FILES + QtService + QtServiceBase + QtServiceController + qtservice.h + ) + +set(HEADER_FILES ${INSTALL_HEADER_FILES} + qtservice_p.h + ) + +if(UNIX) + list(APPEND SOURCE_FILES + qtservice_unix.cpp + qtunixsocket.cpp + qtunixserversocket.cpp) + list(APPEND HEADER_FILES + qtunixserversocket.h + qtunixsocket.h) +endif() +if(WIN32) + list(APPEND SOURCE_FILES + qtservice_win.cpp) +endif() + +add_library(${PROJECT_NAME} ${SOURCE_FILES} ${HEADER_FILES}) +set_target_properties(${PROJECT_NAME} PROPERTIES + PUBLIC_HEADER "${INSTALL_HEADER_FILES}" # Install head files + ) +target_link_libraries(${PROJECT_NAME} PUBLIC ${QT_LIBRARIES}) +target_include_directories(${PROJECT_NAME} + PUBLIC $ + $) + +if(WIN32) + if(BUILD_SHARED_LIBS) + target_compile_definitions(${PROJECT_NAME} PRIVATE QT_QTSERVICE_EXPORT UNICODE) + endif() + INSTALL(TARGETS ${PROJECT_NAME} + EXPORT ${PROJECT_NAME}Targets + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_BINDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME} + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + ) +else() + INSTALL(TARGETS ${PROJECT_NAME} + EXPORT ${PROJECT_NAME}Targets + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME} + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + ) +endif() +# Install cmake configure files +install(EXPORT ${PROJECT_NAME}Targets + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" + ) + +export(TARGETS ${PROJECT_NAME} + APPEND FILE ${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake + ) + +include(CMakePackageConfigHelpers) +configure_package_config_file( + ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/QtServiceConfig.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/QtServiceConfig.cmake + INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" + ) + +# Install cmake version configure file +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + VERSION ${BUILD_VERSION} + COMPATIBILITY AnyNewerVersion) + +install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/QtServiceConfig.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") + +configure_file(${CMAKE_SOURCE_DIR}/cmake/QtService.pc.in + ${CMAKE_BINARY_DIR}/QtService.pc + @ONLY) +install(FILES + ${CMAKE_BINARY_DIR}/QtService.pc + DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig + )