CMakeLists.txt 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. cmake_minimum_required(VERSION 3.3)
  2. set(QTKEYCHAIN_VERSION 0.12.90)
  3. set(QTKEYCHAIN_SOVERSION 1)
  4. project(qtkeychain VERSION ${QTKEYCHAIN_VERSION} LANGUAGES CXX)
  5. # Enable C++11
  6. SET(CMAKE_CXX_STANDARD 11)
  7. include(FindPkgConfig)
  8. ###
  9. set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" "${PROJECT_SOURCE_DIR}/cmake/Modules")
  10. include(GNUInstallDirs)
  11. include(GenerateExportHeader)
  12. include(CMakePackageConfigHelpers)
  13. include(ECMSetupVersion)
  14. include(ECMGeneratePriFile)
  15. option(BUILD_WITH_QT6 "Build qtkeychain with Qt 6" OFF)
  16. option(BUILD_TEST_APPLICATION "Build test application" OFF)
  17. option(BUILD_TRANSLATIONS "Build translations" ON)
  18. option(QTKEYCHAIN_STATIC "Build static library" OFF)
  19. if(CMAKE_SYSTEM_NAME STREQUAL Android)
  20. set(ANDROID 1)
  21. endif()
  22. if(CMAKE_SYSTEM_NAME STREQUAL Haiku)
  23. set(HAIKU 1)
  24. endif()
  25. if (WIN32)
  26. option(USE_CREDENTIAL_STORE "Build with windows CredentialStore support" ON)
  27. if (USE_CREDENTIAL_STORE)
  28. add_definitions(-DUSE_CREDENTIAL_STORE=1)
  29. endif()
  30. endif()
  31. if( NOT BUILD_WITH_QT6 )
  32. find_package(Qt5 COMPONENTS Core REQUIRED)
  33. endif()
  34. if (Qt5Core_FOUND AND NOT BUILD_WITH_QT6)
  35. set(QTKEYCHAIN_VERSION_INFIX 5)
  36. if(ANDROID)
  37. if(Qt5Core_VERSION VERSION_LESS 5.7)
  38. find_package(Qt5 COMPONENTS Core REQUIRED Private)
  39. include_directories(${Qt5Core_PRIVATE_INCLUDE_DIRS})
  40. endif()
  41. find_package(Qt5 COMPONENTS AndroidExtras REQUIRED)
  42. include_directories(${Qt5AndroidExtras_INCLUDE_DIRS})
  43. set(QTANDROIDEXTRAS_LIBRARIES ${Qt5AndroidExtras_LIBRARIES})
  44. endif()
  45. if(UNIX AND NOT APPLE AND NOT ANDROID AND NOT HAIKU)
  46. find_package(Qt5 COMPONENTS DBus REQUIRED)
  47. include_directories(${Qt5DBus_INCLUDE_DIRS})
  48. set(QTDBUS_LIBRARIES ${Qt5DBus_LIBRARIES})
  49. macro(qt_add_dbus_interface)
  50. qt5_add_dbus_interface(${ARGN})
  51. endmacro()
  52. endif()
  53. if(BUILD_TRANSLATIONS)
  54. find_package(Qt5 COMPONENTS LinguistTools REQUIRED)
  55. macro(qt_add_translation)
  56. qt5_add_translation(${ARGN})
  57. endmacro(qt_add_translation)
  58. macro(qt_create_translation)
  59. qt5_create_translation(${ARGN})
  60. endmacro(qt_create_translation)
  61. endif()
  62. macro(qt_wrap_cpp)
  63. qt5_wrap_cpp(${ARGN})
  64. endmacro()
  65. set(QTCORE_LIBRARIES ${Qt5Core_LIBRARIES})
  66. include_directories(${Qt5Core_INCLUDE_DIRS})
  67. else()
  68. find_package(Qt6 COMPONENTS Core REQUIRED)
  69. set(QTKEYCHAIN_VERSION_INFIX 6)
  70. if(ANDROID)
  71. find_package(Qt6 COMPONENTS AndroidExtras REQUIRED)
  72. include_directories(${Qt6AndroidExtras_INCLUDE_DIRS})
  73. set(QTANDROIDEXTRAS_LIBRARIES ${Qt6AndroidExtras_LIBRARIES})
  74. endif()
  75. if(UNIX AND NOT APPLE AND NOT ANDROID AND NOT HAIKU)
  76. find_package(Qt6 COMPONENTS DBus REQUIRED)
  77. include_directories(${Qt6DBus_INCLUDE_DIRS})
  78. set(QTDBUS_LIBRARIES ${Qt6DBus_LIBRARIES})
  79. macro(qt_add_dbus_interface)
  80. qt6_add_dbus_interface(${ARGN})
  81. endmacro()
  82. endif()
  83. if(BUILD_TRANSLATIONS)
  84. find_package(Qt6 COMPONENTS LinguistTools REQUIRED)
  85. macro(qt_add_translation)
  86. qt6_add_translation(${ARGN})
  87. endmacro(qt_add_translation)
  88. macro(qt_create_translation)
  89. qt6_create_translation(${ARGN})
  90. endmacro(qt_create_translation)
  91. endif()
  92. macro(qt_wrap_cpp)
  93. qt6_wrap_cpp(${ARGN})
  94. endmacro()
  95. set(QTCORE_LIBRARIES ${Qt6Core_LIBRARIES})
  96. endif()
  97. include_directories(${CMAKE_CURRENT_BINARY_DIR})
  98. list(APPEND qtkeychain_LIBRARIES ${QTCORE_LIBRARIES})
  99. set(qtkeychain_SOURCES
  100. keychain.cpp
  101. qkeychain_export.h
  102. keychain.h
  103. )
  104. if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  105. # CMake < 3.15 sneaks in /W# flags for us, so we need a replacement,
  106. # or we'll get a warning (cf. CMP0092)
  107. if (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
  108. string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  109. else()
  110. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
  111. endif()
  112. else()
  113. # MSVC's STL / Qt headers are not MSVC -Wall clean, so don't enable it there
  114. add_definitions( -Wall )
  115. endif()
  116. if(WIN32)
  117. list(APPEND qtkeychain_SOURCES keychain_win.cpp)
  118. if (NOT USE_CREDENTIAL_STORE)
  119. list(APPEND qtkeychain_LIBRARIES crypt32)
  120. list(APPEND qtkeychain_SOURCES plaintextstore.cpp)
  121. endif()
  122. #FIXME: mingw bug; otherwise getting undefined refs to RtlSecureZeroMemory there
  123. if(MINGW)
  124. add_definitions( -O2 )
  125. endif()
  126. endif()
  127. if(APPLE)
  128. list(APPEND qtkeychain_SOURCES keychain_apple.mm)
  129. list(APPEND qtkeychain_LIBRARIES "-framework Foundation" "-framework Security")
  130. endif()
  131. if(HAIKU)
  132. list(APPEND qtkeychain_SOURCES keychain_haiku.cpp)
  133. find_library(BE_LIBRARY be REQUIRED)
  134. list(APPEND qtkeychain_LIBRARIES ${BE_LIBRARY})
  135. endif()
  136. if(UNIX AND NOT APPLE AND NOT ANDROID AND NOT HAIKU)
  137. option(LIBSECRET_SUPPORT "Build with libsecret support" ON)
  138. if(LIBSECRET_SUPPORT)
  139. pkg_check_modules(LIBSECRET REQUIRED libsecret-1)
  140. add_definitions(-DHAVE_LIBSECRET=1)
  141. INCLUDE_DIRECTORIES(${LIBSECRET_INCLUDE_DIRS})
  142. LINK_DIRECTORIES(${LIBSECRET_LIBRARY_DIRS})
  143. list(APPEND qtkeychain_LIBRARIES_PRIVATE ${LIBSECRET_LIBRARIES})
  144. endif()
  145. add_definitions(-DKEYCHAIN_DBUS=1)
  146. list(APPEND qtkeychain_SOURCES keychain_unix.cpp gnomekeyring.cpp libsecret.cpp plaintextstore.cpp)
  147. qt_add_dbus_interface(qtkeychain_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/org.kde.KWallet.xml kwallet_interface KWalletInterface)
  148. list(APPEND qtkeychain_LIBRARIES ${QTDBUS_LIBRARIES} )
  149. endif()
  150. if(ANDROID)
  151. list(APPEND qtkeychain_SOURCES keychain_android.cpp androidkeystore.cpp plaintextstore.cpp)
  152. list(APPEND qtkeychain_LIBRARIES_PRIVATE ${QTANDROIDEXTRAS_LIBRARIES} )
  153. endif()
  154. QT_WRAP_CPP(qtkeychain_MOC_OUTFILES keychain.h keychain_p.h gnomekeyring_p.h)
  155. set(qtkeychain_TR_FILES
  156. translations/qtkeychain_de.ts
  157. translations/qtkeychain_fr.ts
  158. translations/qtkeychain_ro.ts
  159. translations/qtkeychain_ru.ts
  160. translations/qtkeychain_zh.ts
  161. )
  162. set(QTKEYCHAIN_TARGET_NAME qt${QTKEYCHAIN_VERSION_INFIX}keychain)
  163. if(NOT QTKEYCHAIN_STATIC)
  164. add_library(${QTKEYCHAIN_TARGET_NAME} SHARED ${qtkeychain_SOURCES} ${qtkeychain_MOC_OUTFILES} ${qtkeychain_QM_FILES})
  165. else()
  166. add_library(${QTKEYCHAIN_TARGET_NAME} STATIC ${qtkeychain_SOURCES} ${qtkeychain_MOC_OUTFILES} ${qtkeychain_QM_FILES})
  167. endif()
  168. if(WIN32)
  169. set_target_properties( ${QTKEYCHAIN_TARGET_NAME} PROPERTIES DEBUG_POSTFIX "d" )
  170. endif()
  171. file(GLOB qtkeychain_TR_SOURCES *.cpp *.h *.ui)
  172. if ( BUILD_TRANSLATIONS )
  173. qt_create_translation(qtkeychain_MESSAGES ${qtkeychain_TR_SOURCES} ${qtkeychain_TR_FILES})
  174. qt_add_translation(qtkeychain_QM_FILES ${qtkeychain_TR_FILES})
  175. add_custom_target(messages DEPENDS ${qtkeychain_MESSAGES})
  176. add_custom_target(translations DEPENDS ${qtkeychain_QM_FILES} messages)
  177. # https://github.com/frankosterfeld/qtkeychain/issues/185
  178. add_dependencies(${QTKEYCHAIN_TARGET_NAME} translations)
  179. if(QTKEYCHAIN_VERSION_INFIX EQUAL 5 AND QT_TRANSLATIONS_DIR AND NOT QTKEYCHAIN_TRANSLATIONS_DIR)
  180. # Back compatibility with pre-0.11 versions
  181. message (WARNING "QT_TRANSLATIONS_DIR is deprecated, use QTKEYCHAIN_TRANSLATIONS_DIR instead")
  182. set(QTKEYCHAIN_TRANSLATIONS_DIR ${QT_TRANSLATIONS_DIR}
  183. CACHE PATH "The location of the QtKeychain translations" FORCE)
  184. else()
  185. set(QTKEYCHAIN_TRANSLATIONS_DIR
  186. ${CMAKE_INSTALL_DATADIR}/qt${QTKEYCHAIN_VERSION_INFIX}keychain/translations
  187. CACHE PATH "The location of the QtKeychain translations" )
  188. endif()
  189. install(FILES ${qtkeychain_QM_FILES} DESTINATION ${QTKEYCHAIN_TRANSLATIONS_DIR})
  190. endif( BUILD_TRANSLATIONS )
  191. target_link_libraries(${QTKEYCHAIN_TARGET_NAME} PUBLIC ${qtkeychain_LIBRARIES} PRIVATE ${qtkeychain_LIBRARIES_PRIVATE})
  192. if(NOT INTERFACE_INCLUDE_SUFFIX)
  193. set(INTERFACE_INCLUDE_SUFFIX include)
  194. endif()
  195. target_include_directories(${QTKEYCHAIN_TARGET_NAME} PUBLIC $<INSTALL_INTERFACE:${INTERFACE_INCLUDE_SUFFIX}/>)
  196. generate_export_header(${QTKEYCHAIN_TARGET_NAME}
  197. EXPORT_FILE_NAME qkeychain_export.h
  198. EXPORT_MACRO_NAME QKEYCHAIN_EXPORT
  199. )
  200. set_target_properties(${QTKEYCHAIN_TARGET_NAME} PROPERTIES
  201. VERSION ${QTKEYCHAIN_VERSION}
  202. SOVERSION ${QTKEYCHAIN_SOVERSION}
  203. MACOSX_RPATH 1
  204. INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}"
  205. INSTALL_RPATH_USE_LINK_PATH TRUE
  206. )
  207. install(FILES keychain.h ${CMAKE_CURRENT_BINARY_DIR}/qkeychain_export.h
  208. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/qt${QTKEYCHAIN_VERSION_INFIX}keychain/
  209. )
  210. install(TARGETS ${QTKEYCHAIN_TARGET_NAME}
  211. EXPORT Qt${QTKEYCHAIN_VERSION_INFIX}KeychainLibraryDepends
  212. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  213. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  214. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  215. )
  216. if(BUILD_TEST_APPLICATION)
  217. add_executable( testclient testclient.cpp )
  218. target_link_libraries( testclient ${QTKEYCHAIN_TARGET_NAME})
  219. endif()
  220. ###
  221. ### CMake config file
  222. ###
  223. configure_package_config_file("${CMAKE_CURRENT_SOURCE_DIR}/QtKeychainConfig.cmake.in"
  224. "${CMAKE_CURRENT_BINARY_DIR}/Qt${QTKEYCHAIN_VERSION_INFIX}KeychainConfig.cmake"
  225. INSTALL_DESTINATION Qt${QTKEYCHAIN_VERSION_INFIX}Keychain)
  226. ecm_setup_version("${QTKEYCHAIN_VERSION}" VARIABLE_PREFIX SNORE
  227. PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/Qt${QTKEYCHAIN_VERSION_INFIX}KeychainConfigVersion.cmake"
  228. SOVERSION ${QTKEYCHAIN_VERSION})
  229. if(UNIX AND NOT APPLE AND NOT ANDROID AND NOT HAIKU)
  230. set(PRI_EXTRA_DEPS "dbus")
  231. endif()
  232. ecm_generate_pri_file(BASE_NAME Qt${QTKEYCHAIN_VERSION_INFIX}Keychain
  233. LIB_NAME ${QTKEYCHAIN_TARGET_NAME}
  234. DEPS "core ${PRI_EXTRA_DEPS}"
  235. INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR}
  236. FILENAME_VAR pri_filename)
  237. install(FILES ${pri_filename} DESTINATION ${ECM_MKSPECS_INSTALL_DIR})
  238. install(EXPORT Qt${QTKEYCHAIN_VERSION_INFIX}KeychainLibraryDepends
  239. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/Qt${QTKEYCHAIN_VERSION_INFIX}Keychain"
  240. )
  241. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/Qt${QTKEYCHAIN_VERSION_INFIX}KeychainConfig.cmake
  242. ${CMAKE_CURRENT_BINARY_DIR}/Qt${QTKEYCHAIN_VERSION_INFIX}KeychainConfigVersion.cmake
  243. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Qt${QTKEYCHAIN_VERSION_INFIX}Keychain
  244. )