You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.7KB

  1. project(ZLToolKit)
  2. cmake_minimum_required(VERSION 3.1.3)
  3. include(CheckStructHasMember)
  4. include(CheckSymbolExists)
  5. list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
  6. check_struct_has_member("struct mmsghdr" msg_hdr sys/socket.h HAVE_MMSG_HDR)
  7. check_symbol_exists(sendmmsg sys/socket.h HAVE_SENDMMSG_API)
  8. check_symbol_exists(recvmmsg sys/socket.h HAVE_RECVMMSG_API)
  9. if(HAVE_MMSG_HDR)
  10. add_definitions(-DHAVE_MMSG_HDR)
  11. endif()
  12. if(HAVE_SENDMMSG_API)
  13. add_definitions(-DHAVE_SENDMMSG_API)
  14. endif()
  15. if(HAVE_RECVMMSG_API)
  16. add_definitions(-DHAVE_RECVMMSG_API)
  17. endif()
  18. #加载自定义模块
  19. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
  20. #设置库文件路径
  21. set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
  22. #设置可执行程序路径
  23. set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
  24. #设置子目录
  25. set(SUB_DIR_LIST "Network" "Poller" "Thread" "Util")
  26. if(WIN32)
  27. list(APPEND SUB_DIR_LIST "win32")
  28. #防止Windows.h包含Winsock.h
  29. add_definitions(-DWIN32_LEAN_AND_MEAN)
  30. set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  31. endif()
  32. #安装目录
  33. if(WIN32)
  34. set(INSTALL_PATH_LIB $ENV{HOME}/${PROJECT_NAME}/lib)
  35. set(INSTALL_PATH_INCLUDE $ENV{HOME}/${PROJECT_NAME}/include)
  36. else()
  37. set(INSTALL_PATH_LIB lib)
  38. set(INSTALL_PATH_INCLUDE include)
  39. endif()
  40. foreach(SUB_DIR ${SUB_DIR_LIST})
  41. #遍历源文件
  42. aux_source_directory(src/${SUB_DIR} SRC_LIST)
  43. #安装头文件至系统目录
  44. install(DIRECTORY src/${SUB_DIR} DESTINATION ${INSTALL_PATH_INCLUDE} FILES_MATCHING PATTERN "*.h")
  45. endforeach()
  46. #非苹果平台移除.mm类型的文件
  47. if(NOT APPLE)
  48. list(REMOVE_ITEM SRC_LIST "src/Network/Socket_ios.mm")
  49. endif()
  50. if(WIN32)
  51. set(LINK_LIB_LIST WS2_32 Iphlpapi shlwapi)
  52. else()
  53. set(LINK_LIB_LIST)
  54. endif()
  55. set(ENABLE_OPENSSL ON CACHE BOOL "enable openssl")
  56. set(ENABLE_MYSQL ON CACHE BOOL "enable mysql")
  57. #查找openssl是否安装
  58. find_package(OpenSSL QUIET)
  59. if(OPENSSL_FOUND AND ENABLE_OPENSSL)
  60. message(STATUS "找到openssl库:\"${OPENSSL_INCLUDE_DIR}\",ENABLE_OPENSSL宏已打开")
  61. include_directories(${OPENSSL_INCLUDE_DIR})
  62. add_definitions(-DENABLE_OPENSSL)
  63. list(APPEND LINK_LIB_LIST ${OPENSSL_LIBRARIES})
  64. endif()
  65. #查找mysql是否安装
  66. find_package(MYSQL QUIET)
  67. if(MYSQL_FOUND AND ENABLE_MYSQL)
  68. message(STATUS "找到mysqlclient库:\"${MYSQL_INCLUDE_DIR}\",ENABLE_MYSQL宏已打开")
  69. include_directories(${MYSQL_INCLUDE_DIR})
  70. include_directories(${MYSQL_INCLUDE_DIR}/mysql)
  71. add_definitions(-DENABLE_MYSQL)
  72. list(APPEND LINK_LIB_LIST ${MYSQL_LIBRARIES})
  73. endif()
  74. #打印库文件
  75. message(STATUS "将链接依赖库:${LINK_LIB_LIST}")
  76. #使能c++11
  77. set(CMAKE_CXX_STANDARD 11)
  78. if(NOT WIN32)
  79. add_compile_options(-Wno-deprecated-declarations)
  80. add_compile_options(-Wno-predefined-identifier-outside-function)
  81. endif()
  82. #编译动态库
  83. if(NOT IOS AND NOT ANDROID AND NOT WIN32)
  84. add_library(${PROJECT_NAME}_shared SHARED ${SRC_LIST})
  85. target_include_directories(${PROJECT_NAME}_shared PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
  86. target_link_libraries(${PROJECT_NAME}_shared ${LINK_LIB_LIST})
  87. set_target_properties(${PROJECT_NAME}_shared PROPERTIES OUTPUT_NAME "${PROJECT_NAME}")
  88. install(TARGETS ${PROJECT_NAME}_shared ARCHIVE DESTINATION ${INSTALL_PATH_LIB} LIBRARY DESTINATION ${INSTALL_PATH_LIB})
  89. endif()
  90. #编译静态库
  91. add_library(${PROJECT_NAME}_static STATIC ${SRC_LIST})
  92. #引用头文件路径
  93. target_include_directories(${PROJECT_NAME}_static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
  94. set_target_properties(${PROJECT_NAME}_static PROPERTIES OUTPUT_NAME "${PROJECT_NAME}")
  95. #安装静态库至系统目录
  96. install(TARGETS ${PROJECT_NAME}_static ARCHIVE DESTINATION ${INSTALL_PATH_LIB})
  97. #测试程序
  98. if(NOT IOS)
  99. add_subdirectory(tests)
  100. endif()