|
| 1 | +# Author: Kang Lin <kl222@126.com> |
| 2 | + |
| 3 | +cmake_minimum_required(VERSION 3.5) |
| 4 | + |
| 5 | +project(QtService) |
| 6 | + |
| 7 | +set(CMAKE_INCLUDE_CURRENT_DIR ON) |
| 8 | +#set(CMAKE_CXX_STANDARD 11) |
| 9 | +#set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 10 | +set(CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "verbose") |
| 11 | + |
| 12 | +#打开 qt 编译工具 |
| 13 | +SET(CMAKE_AUTOUIC ON) |
| 14 | +SET(CMAKE_AUTOMOC ON) |
| 15 | +SET(CMAKE_AUTORCC ON) |
| 16 | +SET(CMAKE_VERBOSE_MAKEFILE ON) |
| 17 | + |
| 18 | +#需要的QT组件 |
| 19 | +if(CMAKE_VERSION VERSION_LESS "3.7.0") |
| 20 | + set(CMAKE_INCLUDE_CURRENT_DIR ON) |
| 21 | +endif() |
| 22 | +SET(QT_COMPONENTS Widgets) |
| 23 | +find_package(QT NAMES Qt6 Qt5 COMPONENTS Core) |
| 24 | +find_package(Qt${QT_VERSION_MAJOR} COMPONENTS ${QT_COMPONENTS}) |
| 25 | +message("QT_VERSION:${Qt${QT_VERSION_MAJOR}_VERSION}") |
| 26 | +if(Qt${QT_VERSION_MAJOR}_VERSION VERSION_LESS 5.10.0 AND ANDROID) |
| 27 | + message(FATAL_ERROR "Qt must great 5.10.0") |
| 28 | +endif() |
| 29 | +if(Qt${QT_VERSION_MAJOR}_FOUND) |
| 30 | + FOREACH(_COMPONENT ${QT_COMPONENTS}) |
| 31 | + SET(QT_LIBRARIES ${QT_LIBRARIES} Qt${QT_VERSION_MAJOR}::${_COMPONENT}) |
| 32 | + ENDFOREACH() |
| 33 | +endif() |
| 34 | +get_filename_component(QT_INSTALL_DIR "${Qt${QT_VERSION_MAJOR}_DIR}/../../.." ABSOLUTE) |
| 35 | +message("QT_INSTALL_DIR:${QT_INSTALL_DIR}") |
| 36 | +message("QT_LIBRARIES:${QT_LIBRARIES}") |
| 37 | + |
| 38 | +if(POLICY CMP0083) |
| 39 | + cmake_policy(SET CMP0083 NEW) |
| 40 | +endif() |
| 41 | + |
| 42 | +if(POLICY CMP0020) |
| 43 | + cmake_policy(SET CMP0020 NEW) |
| 44 | +endif() |
| 45 | + |
| 46 | +SET(BUILD_VERSION "v0.0.1") |
| 47 | +# Find Git Version Patch |
| 48 | +IF(EXISTS "${CMAKE_SOURCE_DIR}/.git") |
| 49 | + if(NOT GIT) |
| 50 | + SET(GIT $ENV{GIT}) |
| 51 | + endif() |
| 52 | + if(NOT GIT) |
| 53 | + FIND_PROGRAM(GIT NAMES git git.exe git.cmd) |
| 54 | + endif() |
| 55 | + IF(GIT) |
| 56 | + EXECUTE_PROCESS( |
| 57 | + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 58 | + COMMAND ${GIT} describe --tags |
| 59 | + OUTPUT_VARIABLE GIT_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE |
| 60 | + ) |
| 61 | + if(NOT GIT_VERSION) |
| 62 | + EXECUTE_PROCESS( |
| 63 | + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 64 | + COMMAND ${GIT} rev-parse --short HEAD |
| 65 | + OUTPUT_VARIABLE GIT_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE |
| 66 | + ) |
| 67 | + endif() |
| 68 | + SET(BUILD_VERSION ${GIT_VERSION}) |
| 69 | + ENDIF() |
| 70 | +ENDIF() |
| 71 | +message("BUILD_VERSION:${BUILD_VERSION}") |
| 72 | +set(VERSION ${BUILD_VERSION}) |
| 73 | + |
| 74 | +if(NOT DEFINED CMAKE_BUILD_TYPE) |
| 75 | + set(CMAKE_BUILD_TYPE "Release") |
| 76 | +endif(NOT DEFINED CMAKE_BUILD_TYPE) |
| 77 | +string(TOLOWER "${CMAKE_BUILD_TYPE}" build_type) |
| 78 | +if("debug" STREQUAL build_type) |
| 79 | + add_definitions(-D_DEBUG) |
| 80 | +endif() |
| 81 | + |
| 82 | +IF(MSVC) |
| 83 | + # This option is to enable the /MP switch for Visual Studio 2005 and above compilers |
| 84 | + OPTION(WIN32_USE_MP "Set to ON to build with the /MP option (Visual Studio 2005 and above)." ON) |
| 85 | + MARK_AS_ADVANCED(WIN32_USE_MP) |
| 86 | + IF(WIN32_USE_MP) |
| 87 | + #SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP") |
| 88 | + add_compile_options(/MP) |
| 89 | + ENDIF(WIN32_USE_MP) |
| 90 | + add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>") |
| 91 | + add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>") |
| 92 | +ENDIF(MSVC) |
| 93 | + |
| 94 | +SET(BUILD_SHARED_LIBS ON CACHE BOOL "Build shared libs") |
| 95 | +if (BUILD_SHARED_LIBS) |
| 96 | + add_definitions(-DBUILD_SHARED_LIBS) |
| 97 | + if (CMAKE_COMPILER_IS_GNUCXX AND NOT MINGW) |
| 98 | + # Just setting CMAKE_POSITION_INDEPENDENT_CODE should be enough to set |
| 99 | + # -fPIC for GCC but sometimes it still doesn't get set, so make sure it |
| 100 | + # does. |
| 101 | + add_definitions("-fPIC") |
| 102 | + endif() |
| 103 | + set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
| 104 | +else(BUILD_SHARED_LIBS) |
| 105 | + add_definitions(-DQT_STATICPLUGIN) |
| 106 | +endif(BUILD_SHARED_LIBS) |
| 107 | + |
| 108 | +include(CMakePackageConfigHelpers) |
| 109 | +include(GNUInstallDirs) |
| 110 | +include(GenerateExportHeader) |
| 111 | +include(CheckIncludeFile) |
| 112 | +include(CheckIncludeFileCXX) |
| 113 | +include(CheckFunctionExists) |
| 114 | + |
| 115 | +#CHECK_INCLUDE_FILE_CXX("string" HAVE_STRING_H) |
| 116 | +#check_include_file("math.h" HAVE_MATH_H) |
| 117 | + |
| 118 | +#check_function_exists("fabs" HAVE_FABS) |
| 119 | + |
| 120 | +set(BUILD_PLATFORM "${CMAKE_SYSTEM_NAME}") |
| 121 | +# ---------------------------------------------------------------------------- |
| 122 | +# Detect compiler and target platform architecture |
| 123 | +# ---------------------------------------------------------------------------- |
| 124 | +if(NOT ANDROID) |
| 125 | + if(X86_64 OR CMAKE_SIZEOF_VOID_P EQUAL 8) |
| 126 | + set(BUILD_ARCH x86_64) |
| 127 | + elseif(X86 OR CMAKE_SIZEOF_VOID_P EQUAL 4) |
| 128 | + set(BUILD_ARCH x86) |
| 129 | + endif() |
| 130 | +else() |
| 131 | + set(BUILD_ARCH ${CMAKE_SYSTEM_PROCESSOR}) |
| 132 | +endif() |
| 133 | + |
| 134 | +add_subdirectory(src) |
| 135 | + |
| 136 | +option(BUILD_EXAMPLES "Build examples" ON) |
| 137 | +if(BUILD_EXAMPLES) |
| 138 | + add_subdirectory(examples) |
| 139 | +endif(BUILD_EXAMPLES) |
| 140 | + |
| 141 | +# Create will be delete files |
| 142 | +CONFIGURE_FILE( |
| 143 | + "${CMAKE_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in" |
| 144 | + "${CMAKE_BINARY_DIR}/cmake_uninstall.cmake" |
| 145 | + IMMEDIATE @ONLY) |
| 146 | +# Create unistall target |
| 147 | +ADD_CUSTOM_TARGET(uninstall |
| 148 | + "${CMAKE_COMMAND}" -P "${CMAKE_BINARY_DIR}/cmake_uninstall.cmake" |
| 149 | + ) |
0 commit comments