CMakeLists.txt 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. # Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
  2. # Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 2 or (at your option)
  7. # version 3 of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. cmake_minimum_required(VERSION 3.10.0)
  17. project(KeePassXC)
  18. set(APP_ID "org.keepassxc.${PROJECT_NAME}")
  19. if(NOT CMAKE_BUILD_TYPE)
  20. set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
  21. "Choose the type of build, options are: Debug Release RelWithDebInfo Profile"
  22. FORCE)
  23. endif()
  24. string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
  25. if(CMAKE_BUILD_TYPE_LOWER STREQUAL "debug" OR CMAKE_BUILD_TYPE_LOWER STREQUAL "relwithdebinfo")
  26. set(IS_DEBUG_BUILD TRUE)
  27. endif()
  28. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
  29. # Support Visual Studio Code
  30. include(CMakeToolsHelpers OPTIONAL)
  31. include(FeatureSummary)
  32. include(KPXCMacDeployHelpers)
  33. include(CheckCCompilerFlag)
  34. include(CheckCXXCompilerFlag)
  35. include(CheckCXXSourceCompiles)
  36. option(WITH_TESTS "Enable building of unit tests" ON)
  37. option(WITH_GUI_TESTS "Enable building of GUI tests" OFF)
  38. option(WITH_DEV_BUILD "Use only for development. Disables/warns about deprecated methods." OFF)
  39. option(WITH_ASAN "Enable address sanitizer checks (Linux / macOS only)" OFF)
  40. option(WITH_COVERAGE "Use to build with coverage tests (GCC only)." OFF)
  41. option(WITH_APP_BUNDLE "Enable Application Bundle for macOS" ON)
  42. option(WITH_CCACHE "Use ccache for build" OFF)
  43. set(WITH_XC_ALL OFF CACHE BOOL "Build in all available plugins")
  44. option(WITH_XC_AUTOTYPE "Include Auto-Type." ON)
  45. option(WITH_XC_NETWORKING "Include networking code (e.g. for downloading website icons)." OFF)
  46. option(WITH_XC_BROWSER "Include browser integration with keepassxc-browser." OFF)
  47. option(WITH_XC_BROWSER_PASSKEYS "Passkeys support for browser integration." OFF)
  48. option(WITH_XC_YUBIKEY "Include YubiKey support." OFF)
  49. option(WITH_XC_SSHAGENT "Include SSH agent support." OFF)
  50. option(WITH_XC_KEESHARE "Sharing integration with KeeShare" OFF)
  51. option(WITH_XC_UPDATECHECK "Include automatic update checks; disable for controlled distributions" ON)
  52. if(UNIX AND NOT APPLE)
  53. option(WITH_XC_FDOSECRETS "Implement freedesktop.org Secret Storage Spec server side API." OFF)
  54. endif()
  55. option(WITH_XC_DOCS "Enable building of documentation" ON)
  56. set(WITH_XC_X11 ON CACHE BOOL "Enable building with X11 deps")
  57. if(APPLE)
  58. # Perform the platform checks before applying the stricter compiler flags.
  59. # Otherwise the kSecAccessControlTouchIDCurrentSet deprecation warning will result in an error.
  60. try_compile(XC_APPLE_COMPILER_SUPPORT_BIOMETRY
  61. ${CMAKE_CURRENT_BINARY_DIR}/tiometry_test/
  62. ${CMAKE_CURRENT_SOURCE_DIR}/cmake/compiler-checks/macos/control_biometry_support.mm)
  63. message(STATUS "Biometry compiler support: ${XC_APPLE_COMPILER_SUPPORT_BIOMETRY}")
  64. try_compile(XC_APPLE_COMPILER_SUPPORT_TOUCH_ID
  65. ${CMAKE_CURRENT_BINARY_DIR}/touch_id_test/
  66. ${CMAKE_CURRENT_SOURCE_DIR}/cmake/compiler-checks/macos/control_touch_id_support.mm)
  67. message(STATUS "Touch ID compiler support: ${XC_APPLE_COMPILER_SUPPORT_TOUCH_ID}")
  68. try_compile(XC_APPLE_COMPILER_SUPPORT_WATCH
  69. ${CMAKE_CURRENT_BINARY_DIR}/tiometry_test/
  70. ${CMAKE_CURRENT_SOURCE_DIR}/cmake/compiler-checks/macos/control_watch_support.mm)
  71. message(STATUS "Apple watch compiler support: ${XC_APPLE_COMPILER_SUPPORT_WATCH}")
  72. endif()
  73. if(WITH_CCACHE)
  74. # Use the Compiler Cache (ccache) program
  75. # (install with: sudo apt get ccache)
  76. find_program(CCACHE_FOUND ccache)
  77. if(NOT CCACHE_FOUND)
  78. message(FATAL_ERROR "ccache requested but cannot be found.")
  79. endif()
  80. set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_FOUND})
  81. endif()
  82. if(WITH_XC_ALL)
  83. # Enable all options (except update check and docs)
  84. set(WITH_XC_AUTOTYPE ON)
  85. set(WITH_XC_NETWORKING ON)
  86. set(WITH_XC_BROWSER ON)
  87. set(WITH_XC_BROWSER_PASSKEYS ON)
  88. set(WITH_XC_YUBIKEY ON)
  89. set(WITH_XC_SSHAGENT ON)
  90. set(WITH_XC_KEESHARE ON)
  91. if(UNIX AND NOT APPLE)
  92. set(WITH_XC_FDOSECRETS ON)
  93. endif()
  94. endif()
  95. # Prefer WITH_XC_NETWORKING setting over WITH_XC_UPDATECHECK
  96. if(NOT WITH_XC_NETWORKING AND WITH_XC_UPDATECHECK)
  97. message(STATUS "Disabling WITH_XC_UPDATECHECK because WITH_XC_NETWORKING is disabled")
  98. set(WITH_XC_UPDATECHECK OFF)
  99. endif()
  100. if(UNIX AND NOT APPLE AND NOT WITH_XC_X11)
  101. message(STATUS "Disabling WITH_XC_AUTOTYPE because WITH_XC_X11 is disabled")
  102. set(WITH_XC_AUTOTYPE OFF)
  103. endif()
  104. set(KEEPASSXC_VERSION_MAJOR "2")
  105. set(KEEPASSXC_VERSION_MINOR "8")
  106. set(KEEPASSXC_VERSION_PATCH "0")
  107. set(KEEPASSXC_VERSION "${KEEPASSXC_VERSION_MAJOR}.${KEEPASSXC_VERSION_MINOR}.${KEEPASSXC_VERSION_PATCH}")
  108. set(OVERRIDE_VERSION "" CACHE STRING "Override the KeePassXC Version for Snapshot builds")
  109. set(KEEPASSXC_BUILD_TYPE "Snapshot" CACHE STRING "Set KeePassXC build type to distinguish between stable releases and snapshots")
  110. set_property(CACHE KEEPASSXC_BUILD_TYPE PROPERTY STRINGS Snapshot Release PreRelease)
  111. # Retrieve git HEAD revision hash
  112. set(GIT_HEAD_OVERRIDE "" CACHE STRING "Manually set the Git HEAD hash when missing (eg, when no .git folder exists)")
  113. execute_process(COMMAND git rev-parse --short=7 HEAD
  114. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  115. OUTPUT_VARIABLE GIT_HEAD
  116. ERROR_QUIET)
  117. string(STRIP "${GIT_HEAD}" GIT_HEAD)
  118. if(GIT_HEAD STREQUAL "" AND NOT GIT_HEAD_OVERRIDE STREQUAL "")
  119. string(SUBSTRING "${GIT_HEAD_OVERRIDE}" 0 7 GIT_HEAD)
  120. elseif(EXISTS ${CMAKE_SOURCE_DIR}/.gitrev)
  121. file(READ ${CMAKE_SOURCE_DIR}/.gitrev GIT_HEAD)
  122. endif()
  123. message(STATUS "Found Git HEAD Revision: ${GIT_HEAD}\n")
  124. # Check if on a tag, if so build as a release
  125. execute_process(COMMAND git tag --points-at HEAD
  126. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  127. OUTPUT_VARIABLE GIT_TAG
  128. ERROR_QUIET)
  129. string(REGEX REPLACE "latest" "" GIT_TAG "${GIT_TAG}")
  130. if(GIT_TAG MATCHES "[0-9]+\.[0-9]+\.[0-9]+")
  131. string(STRIP "${GIT_TAG}" GIT_TAG)
  132. set(OVERRIDE_VERSION ${GIT_TAG})
  133. elseif(EXISTS ${CMAKE_SOURCE_DIR}/.version)
  134. file(READ ${CMAKE_SOURCE_DIR}/.version OVERRIDE_VERSION)
  135. endif()
  136. string(REGEX REPLACE "(\r?\n)+" "" OVERRIDE_VERSION "${OVERRIDE_VERSION}")
  137. if(OVERRIDE_VERSION)
  138. if(OVERRIDE_VERSION MATCHES "^[\\.0-9]+-beta[0-9]*")
  139. set(KEEPASSXC_BUILD_TYPE "PreRelease")
  140. set(KEEPASSXC_VERSION ${OVERRIDE_VERSION})
  141. elseif(OVERRIDE_VERSION MATCHES "^[\\.0-9]+$")
  142. set(KEEPASSXC_BUILD_TYPE "Release")
  143. set(KEEPASSXC_VERSION ${OVERRIDE_VERSION})
  144. else()
  145. set(KEEPASSXC_BUILD_TYPE "Snapshot")
  146. set(KEEPASSXC_VERSION ${OVERRIDE_VERSION})
  147. endif()
  148. else()
  149. if(KEEPASSXC_BUILD_TYPE STREQUAL "PreRelease")
  150. set(KEEPASSXC_VERSION "${KEEPASSXC_VERSION}-preview")
  151. elseif(KEEPASSXC_BUILD_TYPE STREQUAL "Snapshot")
  152. set(KEEPASSXC_VERSION "${KEEPASSXC_VERSION}-snapshot")
  153. endif()
  154. endif()
  155. if(KEEPASSXC_BUILD_TYPE STREQUAL "Release")
  156. set(KEEPASSXC_BUILD_TYPE_RELEASE ON)
  157. elseif(KEEPASSXC_BUILD_TYPE STREQUAL "PreRelease")
  158. set(KEEPASSXC_BUILD_TYPE_PRE_RELEASE ON)
  159. else()
  160. set(KEEPASSXC_BUILD_TYPE_SNAPSHOT ON)
  161. endif()
  162. message(STATUS "Setting up build for KeePassXC v${KEEPASSXC_VERSION}\n")
  163. # Distribution info
  164. set(KEEPASSXC_DIST ON)
  165. set(KEEPASSXC_DIST_TYPE "Other" CACHE STRING "KeePassXC Distribution Type")
  166. set_property(CACHE KEEPASSXC_DIST_TYPE PROPERTY STRINGS Snap AppImage Flatpak Other)
  167. if(KEEPASSXC_DIST_TYPE STREQUAL "Snap")
  168. set(KEEPASSXC_DIST_SNAP ON)
  169. elseif(KEEPASSXC_DIST_TYPE STREQUAL "AppImage")
  170. set(KEEPASSXC_DIST_APPIMAGE ON)
  171. elseif(KEEPASSXC_DIST_TYPE STREQUAL "Flatpak")
  172. set(KEEPASSXC_DIST_FLATPAK ON)
  173. elseif(KEEPASSXC_DIST_TYPE STREQUAL "Other")
  174. unset(KEEPASSXC_DIST)
  175. endif()
  176. if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.14.0")
  177. cmake_policy(SET CMP0083 NEW)
  178. include(CheckPIESupported)
  179. check_pie_supported()
  180. endif()
  181. # Find Botan early since the version affects subsequent compiler options
  182. find_package(Botan REQUIRED)
  183. if(BOTAN_VERSION VERSION_GREATER_EQUAL "3.0.0")
  184. set(WITH_XC_BOTAN3 TRUE)
  185. elseif(BOTAN_VERSION VERSION_LESS "2.11.0")
  186. # Check for minimum Botan version
  187. message(FATAL_ERROR "Botan 2.11.0 or higher is required")
  188. endif()
  189. include_directories(SYSTEM ${BOTAN_INCLUDE_DIR})
  190. # Create position independent code for shared libraries and executables
  191. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  192. if("${CMAKE_SIZEOF_VOID_P}" EQUAL "4")
  193. set(IS_32BIT TRUE)
  194. endif()
  195. set(CLANG_COMPILER_ID_REGEX "^(Apple)?[Cc]lang$")
  196. if("${CMAKE_C_COMPILER}" MATCHES "clang$"
  197. OR "${CMAKE_EXTRA_GENERATOR_C_SYSTEM_DEFINED_MACROS}" MATCHES "__clang__"
  198. OR "${CMAKE_C_COMPILER_ID}" MATCHES ${CLANG_COMPILER_ID_REGEX})
  199. set(CMAKE_COMPILER_IS_CLANG 1)
  200. endif()
  201. if("${CMAKE_CXX_COMPILER}" MATCHES "clang(\\+\\+)?$"
  202. OR "${CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS}" MATCHES "__clang__"
  203. OR "${CMAKE_CXX_COMPILER_ID}" MATCHES ${CLANG_COMPILER_ID_REGEX})
  204. set(CMAKE_COMPILER_IS_CLANGXX 1)
  205. endif()
  206. macro(add_gcc_compiler_cxxflags FLAGS)
  207. if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANGXX)
  208. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAGS}")
  209. endif()
  210. endmacro(add_gcc_compiler_cxxflags)
  211. macro(add_gcc_compiler_cflags FLAGS)
  212. if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
  213. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLAGS}")
  214. endif()
  215. endmacro(add_gcc_compiler_cflags)
  216. macro(add_gcc_compiler_flags FLAGS)
  217. add_gcc_compiler_cxxflags("${FLAGS}")
  218. add_gcc_compiler_cflags("${FLAGS}")
  219. endmacro(add_gcc_compiler_flags)
  220. # Copies of above macros that first ensure the compiler understands a given flag
  221. # Because check_*_compiler_flag() sets -D with name, need to provide "safe" FLAGNAME
  222. macro(check_add_gcc_compiler_cxxflag FLAG FLAGNAME)
  223. check_cxx_compiler_flag("${FLAG}" CXX_HAS${FLAGNAME})
  224. if(CXX_HAS${FLAGNAME})
  225. add_gcc_compiler_cxxflags("${FLAG}")
  226. endif()
  227. endmacro(check_add_gcc_compiler_cxxflag)
  228. macro(check_add_gcc_compiler_cflag FLAG FLAGNAME)
  229. check_c_compiler_flag("${FLAG}" CC_HAS${FLAGNAME})
  230. if(CC_HAS${FLAGNAME})
  231. add_gcc_compiler_cflags("${FLAG}")
  232. endif()
  233. endmacro(check_add_gcc_compiler_cflag)
  234. # This is the "front-end" for the above macros
  235. # Optionally takes additional parameter(s) with language to check (currently "C" or "CXX")
  236. macro(check_add_gcc_compiler_flag FLAG)
  237. string(REGEX REPLACE "[-=]" "_" FLAGNAME "${FLAG}")
  238. set(check_lang_spec ${ARGN})
  239. list(LENGTH check_lang_spec num_extra_args)
  240. set(langs C CXX)
  241. if(num_extra_args GREATER 0)
  242. set(langs "${check_lang_spec}")
  243. endif()
  244. if("C" IN_LIST langs)
  245. check_add_gcc_compiler_cflag("${FLAG}" "${FLAGNAME}")
  246. endif()
  247. if("CXX" IN_LIST langs)
  248. check_add_gcc_compiler_cxxflag("${FLAG}" "${FLAGNAME}")
  249. endif()
  250. endmacro(check_add_gcc_compiler_flag)
  251. add_definitions(-DQT_NO_EXCEPTIONS -DQT_STRICT_ITERATORS -DQT_NO_CAST_TO_ASCII)
  252. if(NOT IS_DEBUG_BUILD)
  253. add_definitions(-DQT_NO_DEBUG_OUTPUT)
  254. endif()
  255. if(WITH_APP_BUNDLE)
  256. add_definitions(-DWITH_APP_BUNDLE)
  257. endif()
  258. add_gcc_compiler_flags("-fno-common")
  259. find_package(OpenMP)
  260. if(OpenMP_FOUND)
  261. add_gcc_compiler_cflags(${OpenMP_C_FLAGS})
  262. add_gcc_compiler_cxxflags(${OpenMP_CXX_FLAGS})
  263. endif()
  264. add_gcc_compiler_flags("-Wall -Wextra -Wundef -Wpointer-arith -Wno-long-long")
  265. add_gcc_compiler_flags("-Wformat=2 -Wmissing-format-attribute")
  266. add_gcc_compiler_flags("-fvisibility=hidden")
  267. add_gcc_compiler_cxxflags("-fvisibility-inlines-hidden")
  268. if(CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
  269. check_add_gcc_compiler_flag("-Wshadow-compatible-local")
  270. check_add_gcc_compiler_flag("-Wshadow-local")
  271. add_gcc_compiler_flags("-Werror")
  272. # This is needed since compiling against Botan3 requires compiling against C++20
  273. if(WITH_XC_BOTAN3)
  274. add_gcc_compiler_cxxflags("-Wno-error=deprecated-enum-enum-conversion -Wno-error=deprecated")
  275. endif()
  276. endif()
  277. if (NOT HAIKU)
  278. if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.8.999) OR CMAKE_COMPILER_IS_CLANGXX)
  279. add_gcc_compiler_flags("-fstack-protector-strong")
  280. else()
  281. add_gcc_compiler_flags("-fstack-protector --param=ssp-buffer-size=4")
  282. endif()
  283. endif()
  284. add_gcc_compiler_cxxflags("-Wnon-virtual-dtor -Wold-style-cast -Woverloaded-virtual")
  285. add_gcc_compiler_cflags("-Wchar-subscripts -Wwrite-strings")
  286. if(WITH_ASAN)
  287. if(NOT (CMAKE_SYSTEM_NAME STREQUAL "Linux" OR APPLE))
  288. message(FATAL_ERROR "WITH_ASAN is only supported on Linux / macOS at the moment.")
  289. endif()
  290. add_gcc_compiler_flags("-fsanitize=address -DWITH_ASAN")
  291. if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  292. if(NOT (CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9))
  293. add_gcc_compiler_flags("-fsanitize=leak -DWITH_LSAN")
  294. endif()
  295. endif()
  296. endif()
  297. if(CMAKE_BUILD_TYPE_LOWER MATCHES "(release|relwithdebinfo|minsizerel)")
  298. add_gcc_compiler_flags("-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2")
  299. endif()
  300. check_add_gcc_compiler_flag("-Werror=format-security")
  301. check_add_gcc_compiler_flag("-Werror=implicit-function-declaration" C)
  302. check_add_gcc_compiler_flag("-Wcast-align")
  303. if(UNIX AND NOT APPLE)
  304. check_add_gcc_compiler_flag("-Qunused-arguments")
  305. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--as-needed -Wl,--no-undefined")
  306. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,relro,-z,now -pie")
  307. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--as-needed")
  308. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-z,relro,-z,now")
  309. endif()
  310. set(CMAKE_C_STANDARD 99)
  311. if(WITH_XC_BOTAN3)
  312. set(CMAKE_CXX_STANDARD 20)
  313. else()
  314. set(CMAKE_CXX_STANDARD 17)
  315. endif()
  316. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  317. check_cxx_compiler_flag("-fsized-deallocation" CXX_HAS_fsized_deallocation)
  318. if(CXX_HAS_fsized_deallocation)
  319. # Do additional check: the deallocation functions must be there too.
  320. set(CMAKE_REQUIRED_FLAGS "-fsized-deallocation")
  321. check_cxx_source_compiles("#include <new>
  322. int main() { void * ptr = nullptr; std::size_t size = 1; ::operator delete(ptr, size); }"
  323. HAVE_DEALLOCATION_FUNCTIONS)
  324. if(HAVE_DEALLOCATION_FUNCTIONS)
  325. check_add_gcc_compiler_flag("-fsized-deallocation" CXX)
  326. endif()
  327. unset(CMAKE_REQUIRED_FLAGS)
  328. endif()
  329. if(APPLE AND CMAKE_COMPILER_IS_CLANGXX)
  330. add_gcc_compiler_cxxflags("-stdlib=libc++")
  331. endif()
  332. if(WITH_DEV_BUILD)
  333. add_definitions(-DQT_DEPRECATED_WARNINGS)
  334. else()
  335. add_definitions(-DQT_NO_DEPRECATED_WARNINGS)
  336. add_gcc_compiler_cxxflags("-Wno-deprecated-declarations")
  337. endif()
  338. # MSVC specific options
  339. if (MSVC)
  340. if(MSVC_TOOLSET_VERSION LESS 141)
  341. message(FATAL_ERROR "Only Microsoft Visual Studio 17 and newer are supported!")
  342. endif()
  343. add_compile_options(/permissive- /utf-8 /MP)
  344. if(IS_DEBUG_BUILD)
  345. add_compile_options(/Zf)
  346. if(MSVC_TOOLSET_VERSION GREATER 141)
  347. add_compile_definitions(/fsanitize=address)
  348. endif()
  349. endif()
  350. endif()
  351. if(WIN32)
  352. set(CMAKE_RC_COMPILER_INIT windres)
  353. enable_language(RC)
  354. if(MINGW)
  355. set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> <FLAGS> -O coff <DEFINES> -i <SOURCE> -o <OBJECT>")
  356. endif()
  357. if(NOT IS_DEBUG_BUILD)
  358. if(MSVC)
  359. # By default MSVC enables NXCOMPAT
  360. add_compile_options(/guard:cf)
  361. add_link_options(/DYNAMICBASE /HIGHENTROPYVA /GUARD:CF)
  362. else(MINGW)
  363. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--nxcompat -Wl,--dynamicbase")
  364. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--nxcompat -Wl,--dynamicbase")
  365. # Enable high entropy ASLR for 64-bit builds
  366. if(NOT IS_32BIT)
  367. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--high-entropy-va")
  368. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--high-entropy-va")
  369. endif()
  370. endif()
  371. endif()
  372. endif()
  373. if(APPLE AND WITH_APP_BUNDLE OR WIN32)
  374. set(PROGNAME KeePassXC)
  375. else()
  376. set(PROGNAME keepassxc)
  377. endif()
  378. if(WIN32)
  379. set(CLI_INSTALL_DIR ".")
  380. set(PROXY_INSTALL_DIR ".")
  381. set(BIN_INSTALL_DIR ".")
  382. set(PLUGIN_INSTALL_DIR ".")
  383. set(DATA_INSTALL_DIR "share")
  384. elseif(APPLE AND WITH_APP_BUNDLE)
  385. set(BUNDLE_INSTALL_DIR "${PROGNAME}.app/Contents")
  386. set(CMAKE_INSTALL_MANDIR "${BUNDLE_INSTALL_DIR}/Resources/man")
  387. set(CLI_INSTALL_DIR "${BUNDLE_INSTALL_DIR}/MacOS")
  388. set(PROXY_INSTALL_DIR "${BUNDLE_INSTALL_DIR}/MacOS")
  389. set(BIN_INSTALL_DIR "${BUNDLE_INSTALL_DIR}/MacOS")
  390. set(PLUGIN_INSTALL_DIR "${BUNDLE_INSTALL_DIR}/PlugIns")
  391. set(DATA_INSTALL_DIR "${BUNDLE_INSTALL_DIR}/Resources")
  392. else()
  393. include(GNUInstallDirs)
  394. set(CLI_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}")
  395. set(PROXY_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}")
  396. set(BIN_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}")
  397. set(PLUGIN_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/keepassxc")
  398. set(DATA_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/keepassxc")
  399. endif()
  400. if(WITH_TESTS)
  401. enable_testing()
  402. endif(WITH_TESTS)
  403. if(WITH_COVERAGE)
  404. # Include code coverage, use with -DCMAKE_BUILD_TYPE=Debug
  405. include(CodeCoverage)
  406. append_coverage_compiler_flags()
  407. set(COVERAGE_EXCLUDES
  408. "'^(.+/)?thirdparty/.*'"
  409. "'^(.+/)?main\\.cpp$$'"
  410. "'^(.+/)?cli/keepassxc-cli\\.cpp$$'"
  411. "'^(.+/)?proxy/keepassxc-proxy\\.cpp$$'")
  412. if(WITH_COVERAGE AND CMAKE_COMPILER_IS_CLANGXX)
  413. set(MAIN_BINARIES
  414. "$<TARGET_FILE:${PROGNAME}>"
  415. "$<TARGET_FILE:keepassxc-cli>"
  416. "$<TARGET_FILE:keepassxc-proxy>")
  417. setup_target_for_coverage_llvm(
  418. NAME coverage
  419. BINARY ${MAIN_BINARIES}
  420. SOURCES_ROOT ${CMAKE_SOURCE_DIR}/src
  421. )
  422. else()
  423. setup_target_for_coverage_gcovr(
  424. NAME coverage
  425. SOURCES_ROOT ${CMAKE_SOURCE_DIR}/src
  426. )
  427. endif()
  428. endif()
  429. include(CLangFormat)
  430. set(QT_COMPONENTS Core Network Concurrent Gui Svg Widgets Test LinguistTools)
  431. if(UNIX AND NOT APPLE)
  432. if(WITH_XC_X11)
  433. list(APPEND QT_COMPONENTS X11Extras)
  434. endif()
  435. find_package(Qt5 COMPONENTS ${QT_COMPONENTS} DBus REQUIRED)
  436. elseif(APPLE)
  437. find_package(Qt5 COMPONENTS ${QT_COMPONENTS} REQUIRED HINTS
  438. /usr/local/opt/qt@5/lib/cmake
  439. /usr/local/Cellar/qt@5/*/lib/cmake
  440. /opt/homebrew/opt/qt@5/lib/cmake
  441. ENV PATH)
  442. find_package(Qt5 COMPONENTS MacExtras HINTS
  443. /usr/local/opt/qt@5/lib/cmake
  444. /usr/local/Cellar/qt@5/*/lib/cmake
  445. /opt/homebrew/opt/qt@5/lib/cmake
  446. ENV PATH)
  447. else()
  448. find_package(Qt5 COMPONENTS ${QT_COMPONENTS} REQUIRED)
  449. endif()
  450. if(Qt5Core_VERSION VERSION_LESS "5.2.0")
  451. message(FATAL_ERROR "Qt version 5.2.0 or higher is required")
  452. endif()
  453. # CBOR for Passkeys requires Qt 5.12
  454. if(Qt5Core_VERSION VERSION_LESS "5.12.0")
  455. message(STATUS "Qt version 5.12.0 or higher is required for Passkeys support")
  456. set(WITH_XC_BROWSER_PASSKEYS OFF)
  457. endif()
  458. get_filename_component(Qt5_PREFIX ${Qt5_DIR}/../../.. REALPATH)
  459. if(APPLE)
  460. # Add includes under Qt5 Prefix in case Qt6 is also installed
  461. include_directories(SYSTEM ${Qt5_PREFIX}/include)
  462. endif()
  463. # Process moc automatically
  464. set(CMAKE_AUTOMOC ON)
  465. # Process .ui files automatically
  466. set(CMAKE_AUTOUIC ON)
  467. # Process .qrc files automatically
  468. set(CMAKE_AUTORCC ON)
  469. if(APPLE)
  470. set(CMAKE_MACOSX_RPATH TRUE)
  471. find_program(MACDEPLOYQT_EXE macdeployqt HINTS ${Qt5_PREFIX}/bin ${Qt5_PREFIX}/tools/qt5/bin ENV PATH)
  472. if(NOT MACDEPLOYQT_EXE)
  473. message(FATAL_ERROR "macdeployqt is required to build on macOS")
  474. endif()
  475. message(STATUS "Using macdeployqt: ${MACDEPLOYQT_EXE}")
  476. set(MACDEPLOYQT_EXTRA_BINARIES "")
  477. elseif(WIN32)
  478. find_program(WINDEPLOYQT_EXE windeployqt HINTS ${Qt5_PREFIX}/bin ${Qt5_PREFIX}/tools/qt5/bin ENV PATH)
  479. if(NOT WINDEPLOYQT_EXE)
  480. message(FATAL_ERROR "windeployqt is required to build on Windows")
  481. endif()
  482. message(STATUS "Using windeployqt: ${WINDEPLOYQT_EXE}")
  483. endif()
  484. # Debian sets the build type to None for package builds.
  485. # Make sure we don't enable asserts there.
  486. set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_NONE QT_NO_DEBUG)
  487. # Find Argon2 -- Botan 2.18 and below does not support threaded Argon2
  488. find_library(ARGON2_LIBRARIES NAMES argon2)
  489. find_path(ARGON2_INCLUDE_DIR NAMES argon2.h PATH_SUFFIXES local/include)
  490. include_directories(SYSTEM ${ARGON2_INCLUDE_DIR})
  491. # Find zlib
  492. find_package(ZLIB REQUIRED)
  493. if(ZLIB_VERSION_STRING VERSION_LESS "1.2.0")
  494. message(FATAL_ERROR "zlib 1.2.0 or higher is required to use the gzip format")
  495. endif()
  496. include_directories(SYSTEM ${ZLIB_INCLUDE_DIR})
  497. # Find Minizip
  498. find_package(Minizip REQUIRED)
  499. if(WITH_XC_YUBIKEY)
  500. find_package(PCSC REQUIRED)
  501. include_directories(SYSTEM ${PCSC_INCLUDE_DIRS})
  502. if(UNIX AND NOT APPLE)
  503. find_library(LIBUSB_LIBRARIES NAMES usb-1.0 REQUIRED)
  504. find_path(LIBUSB_INCLUDE_DIR NAMES libusb.h PATH_SUFFIXES "libusb-1.0" "libusb" REQUIRED)
  505. include_directories(SYSTEM ${LIBUSB_INCLUDE_DIR})
  506. endif()
  507. endif()
  508. if(UNIX)
  509. check_cxx_source_compiles("#include <sys/prctl.h>
  510. int main() { prctl(PR_SET_DUMPABLE, 0); return 0; }"
  511. HAVE_PR_SET_DUMPABLE)
  512. check_cxx_source_compiles("#include <malloc.h>
  513. int main() { return 0; }"
  514. HAVE_MALLOC_H)
  515. check_cxx_source_compiles("#include <malloc.h>
  516. int main() { malloc_usable_size(NULL); return 0; }"
  517. HAVE_MALLOC_USABLE_SIZE)
  518. check_cxx_source_compiles("#include <sys/resource.h>
  519. int main() {
  520. struct rlimit limit;
  521. limit.rlim_cur = 0;
  522. limit.rlim_max = 0;
  523. setrlimit(RLIMIT_CORE, &limit);
  524. return 0;
  525. }" HAVE_RLIMIT_CORE)
  526. if(APPLE)
  527. check_cxx_source_compiles("#include <sys/types.h>
  528. #include <sys/ptrace.h>
  529. int main() { ptrace(PT_DENY_ATTACH, 0, 0, 0); return 0; }"
  530. HAVE_PT_DENY_ATTACH)
  531. endif()
  532. endif()
  533. include_directories(SYSTEM ${ZLIB_INCLUDE_DIR})
  534. find_library(ZXCVBN_LIBRARIES zxcvbn)
  535. if(NOT ZXCVBN_LIBRARIES)
  536. add_subdirectory(src/thirdparty/zxcvbn)
  537. set(ZXCVBN_LIBRARIES zxcvbn)
  538. endif(NOT ZXCVBN_LIBRARIES)
  539. add_subdirectory(src)
  540. add_subdirectory(share)
  541. if(WITH_TESTS)
  542. add_subdirectory(tests)
  543. endif(WITH_TESTS)
  544. if(WITH_XC_DOCS)
  545. add_subdirectory(docs)
  546. endif()
  547. if(PRINT_SUMMARY)
  548. # This will print ENABLED, REQUIRED and DISABLED
  549. feature_summary(WHAT ALL)
  550. else()
  551. # This will only print ENABLED and DISABLED feature
  552. feature_summary(WHAT ENABLED_FEATURES DESCRIPTION "Enabled features:")
  553. feature_summary(WHAT DISABLED_FEATURES DESCRIPTION "Disabled features:")
  554. endif()