keepassxc/CMakeLists.txt

281 lines
9.1 KiB
CMake
Raw Normal View History

2010-08-07 09:10:44 -04:00
# Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 or (at your option)
# version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
2016-12-01 22:55:09 -05:00
"Choose the type of build, options are: None Debug Release RelWithDebInfo Debug DebugFull Profile MinSizeRel."
FORCE)
endif()
project(KeePassXC)
2010-08-07 09:10:44 -04:00
2015-07-22 10:59:38 -04:00
cmake_minimum_required(VERSION 2.8.12)
2010-08-07 09:10:44 -04:00
2012-05-21 17:37:29 -04:00
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
2017-05-05 20:20:27 -04:00
# Support Visual Studio Code
include(CMakeToolsHelpers OPTIONAL)
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
include(CheckCXXSourceCompiles)
2010-08-13 12:08:06 -04:00
option(WITH_TESTS "Enable building of unit tests" ON)
2011-12-26 13:18:21 -05:00
option(WITH_GUI_TESTS "Enable building of GUI tests" OFF)
option(WITH_DEV_BUILD "Use only for development. Disables/warns about deprecated methods." OFF)
2017-03-15 10:26:40 -04:00
option(WITH_ASAN "Enable address sanitizer checks (Linux only)" OFF)
option(WITH_COVERAGE "Use to build with coverage tests (GCC only)." OFF)
2010-08-07 09:10:44 -04:00
option(WITH_XC_AUTOTYPE "Include Auto-Type." ON)
option(WITH_XC_HTTP "Include KeePassHTTP and Custom Icon Downloads." OFF)
2017-02-25 11:11:02 -05:00
option(WITH_XC_YUBIKEY "Include YubiKey support." OFF)
2017-05-06 11:50:05 -04:00
# Process ui files automatically from source files
set(CMAKE_AUTOUIC ON)
set(KEEPASSXC_VERSION_MAJOR "2")
set(KEEPASSXC_VERSION_MINOR "1")
set(KEEPASSXC_VERSION_PATCH "4")
set(KEEPASSXC_VERSION "${KEEPASSXC_VERSION_MAJOR}.${KEEPASSXC_VERSION_MINOR}.${KEEPASSXC_VERSION_PATCH}")
2010-08-13 12:08:06 -04:00
if("${CMAKE_C_COMPILER}" MATCHES "clang$" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
2012-04-18 10:10:40 -04:00
set(CMAKE_COMPILER_IS_CLANG 1)
2012-05-21 17:37:29 -04:00
endif()
if("${CMAKE_CXX_COMPILER}" MATCHES "clang(\\+\\+)?$" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(CMAKE_COMPILER_IS_CLANGXX 1)
2012-05-21 17:37:29 -04:00
endif()
2012-04-18 10:10:40 -04:00
macro(add_gcc_compiler_cxxflags FLAGS)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANGXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAGS}")
2012-05-21 17:37:29 -04:00
endif()
endmacro(add_gcc_compiler_cxxflags)
macro(add_gcc_compiler_cflags FLAGS)
if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLAGS}")
2012-05-21 17:37:29 -04:00
endif()
endmacro(add_gcc_compiler_cflags)
macro(add_gcc_compiler_flags FLAGS)
add_gcc_compiler_cxxflags("${FLAGS}")
add_gcc_compiler_cflags("${FLAGS}")
endmacro(add_gcc_compiler_flags)
2012-04-18 10:10:40 -04:00
add_definitions(-DQT_NO_EXCEPTIONS -DQT_STRICT_ITERATORS -DQT_NO_CAST_TO_ASCII)
add_gcc_compiler_flags("-fno-common")
add_gcc_compiler_flags("-Wall -Wextra -Wundef -Wpointer-arith -Wno-long-long")
add_gcc_compiler_flags("-Wformat=2 -Wmissing-format-attribute")
add_gcc_compiler_flags("-fvisibility=hidden")
add_gcc_compiler_cxxflags("-fvisibility-inlines-hidden")
2012-04-18 10:10:40 -04:00
if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.8.999) OR CMAKE_COMPILER_IS_CLANGXX)
add_gcc_compiler_flags("-fstack-protector-strong")
else()
add_gcc_compiler_flags("-fstack-protector --param=ssp-buffer-size=4")
endif()
add_gcc_compiler_cxxflags("-fno-exceptions -fno-rtti")
add_gcc_compiler_cxxflags("-Wnon-virtual-dtor -Wold-style-cast -Woverloaded-virtual")
add_gcc_compiler_cflags("-Wchar-subscripts -Wwrite-strings")
2017-03-14 09:29:09 -04:00
if(WITH_ASAN)
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
message(FATAL_ERROR "WITH_ASAN is only supported on Linux at the moment.")
endif()
add_gcc_compiler_flags("-fsanitize=address -DWITH_ASAN")
2017-03-14 09:53:29 -04:00
if(NOT (CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9))
add_gcc_compiler_flags("-fsanitize=leak -DWITH_LSAN")
endif()
2017-03-14 09:29:09 -04:00
endif()
string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
2017-03-10 07:23:46 -05:00
if (CMAKE_BUILD_TYPE_LOWER MATCHES "(release|relwithdebinfo|minsizerel)")
add_gcc_compiler_flags("-D_FORTIFY_SOURCE=2")
endif()
check_c_compiler_flag("-Werror=format-security -Werror=implicit-function-declaration" WERROR_C_AVAILABLE)
check_cxx_compiler_flag("-Werror=format-security" WERROR_CXX_AVAILABLE)
if(WERROR_C_AVAILABLE AND WERROR_CXX_AVAILABLE)
add_gcc_compiler_flags("-Werror=format-security")
add_gcc_compiler_cflags("-Werror=implicit-function-declaration")
endif()
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wcast-align")
if(WITH_COVERAGE)
# Include code coverage, use with -DCMAKE_BUILD_TYPE=Coverage
include(CodeCoverage)
setup_target_for_coverage(kp_coverage "make test" coverage)
endif()
2012-05-21 17:37:29 -04:00
endif()
if(CMAKE_COMPILER_IS_GNUCC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wcast-align")
2012-05-21 17:37:29 -04:00
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
2017-03-10 07:23:46 -05:00
if (CMAKE_COMPILER_IS_CLANGXX)
add_gcc_compiler_flags("-Qunused-arguments")
endif()
add_gcc_compiler_flags("-pie -fPIE")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--no-add-needed -Wl,--as-needed -Wl,--no-undefined")
2017-03-10 07:23:46 -05:00
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,relro,-z,now")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--no-add-needed -Wl,--as-needed")
2017-03-10 07:23:46 -05:00
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-z,relro,-z,now")
2012-05-21 17:37:29 -04:00
endif()
2010-08-07 09:10:44 -04:00
add_gcc_compiler_cxxflags("-std=c++11")
2012-04-18 14:18:48 -04:00
2015-07-24 12:28:12 -04:00
if(APPLE)
add_gcc_compiler_cxxflags("-stdlib=libc++")
2012-05-21 17:37:29 -04:00
endif()
2012-04-18 14:18:48 -04:00
2015-07-24 12:28:12 -04:00
add_gcc_compiler_cflags("-ansi")
if(WITH_DEV_BUILD)
add_definitions(-DQT_DEPRECATED_WARNINGS -DGCRYPT_NO_DEPRECATED)
2012-06-29 08:14:58 -04:00
endif()
if(MINGW)
set(CMAKE_RC_COMPILER_INIT windres)
enable_language(RC)
set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> <FLAGS> -O coff <DEFINES> -i <SOURCE> -o <OBJECT>")
if(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo"))
# Enable DEP and ASLR
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--nxcompat -Wl,--dynamicbase")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--nxcompat -Wl,--dynamicbase")
endif()
endif()
2012-05-21 17:37:29 -04:00
if(APPLE OR MINGW)
set(PROGNAME KeePassXC)
2012-05-21 17:37:29 -04:00
else()
set(PROGNAME keepassxc)
2012-05-21 17:37:29 -04:00
endif()
2010-08-07 09:10:44 -04:00
if(APPLE AND "${CMAKE_INSTALL_PREFIX}" STREQUAL "/usr/local")
set(CMAKE_INSTALL_PREFIX "/Applications")
endif()
if(MINGW)
2017-02-04 14:52:43 -05:00
set(CLI_INSTALL_DIR ".")
2012-07-18 14:58:49 -04:00
set(BIN_INSTALL_DIR ".")
set(PLUGIN_INSTALL_DIR ".")
set(DATA_INSTALL_DIR "share")
elseif(APPLE)
2017-02-04 14:52:43 -05:00
set(CLI_INSTALL_DIR "/usr/local/bin")
2012-07-18 14:58:49 -04:00
set(BIN_INSTALL_DIR ".")
set(PLUGIN_INSTALL_DIR "${PROGNAME}.app/Contents/PlugIns")
2012-07-18 14:58:49 -04:00
set(DATA_INSTALL_DIR "${PROGNAME}.app/Contents/Resources")
2012-05-21 17:37:29 -04:00
else()
include(GNUInstallDirs)
2017-02-04 14:52:43 -05:00
set(CLI_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}")
set(BIN_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}")
set(PLUGIN_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/keepassxc")
set(DATA_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/keepassxc")
2012-05-21 17:37:29 -04:00
endif()
2012-05-21 17:37:29 -04:00
if(WITH_TESTS)
enable_testing()
endif(WITH_TESTS)
2010-08-13 12:08:06 -04:00
2015-07-22 14:53:49 -04:00
find_package(Qt5Core 5.2 REQUIRED)
find_package(Qt5Concurrent 5.2 REQUIRED)
find_package(Qt5Widgets 5.2 REQUIRED)
find_package(Qt5Test 5.2 REQUIRED)
find_package(Qt5LinguistTools 5.2 REQUIRED)
find_package(Qt5Network 5.2 REQUIRED)
if (UNIX AND NOT APPLE)
find_package(Qt5DBus 5.2 REQUIRED)
endif()
2015-07-22 10:59:38 -04:00
set(CMAKE_AUTOMOC ON)
# Debian sets the the build type to None for package builds.
# Make sure we don't enable asserts there.
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_NONE QT_NO_DEBUG)
2010-08-07 09:10:44 -04:00
2017-01-25 20:58:46 -05:00
find_package(LibGPGError REQUIRED)
find_package(Gcrypt 1.6.0 REQUIRED)
find_package(ZLIB REQUIRED)
check_cxx_source_compiles("
2012-05-21 17:37:29 -04:00
#include <zlib.h>
2012-05-21 17:37:29 -04:00
#if !defined(ZLIB_VERNUM) || (ZLIB_VERNUM < 0x1200)
#error zlib 1.2.x or higher is required to use the gzip format
#endif
2012-05-21 17:37:29 -04:00
int main() { return 0; }" ZLIB_SUPPORTS_GZIP)
if(NOT ZLIB_SUPPORTS_GZIP)
2012-05-21 17:37:29 -04:00
message(FATAL_ERROR "zlib 1.2.x or higher is required to use the gzip format")
endif()
# Optional
if(WITH_XC_YUBIKEY)
find_package(YubiKey REQUIRED)
include_directories(SYSTEM ${YUBIKEY_INCLUDE_DIRS})
endif()
if(UNIX)
check_cxx_source_compiles("#include <sys/prctl.h>
int main() { prctl(PR_SET_DUMPABLE, 0); return 0; }"
HAVE_PR_SET_DUMPABLE)
check_cxx_source_compiles("#include <sys/resource.h>
int main() {
struct rlimit limit;
limit.rlim_cur = 0;
limit.rlim_max = 0;
setrlimit(RLIMIT_CORE, &limit);
return 0;
}" HAVE_RLIMIT_CORE)
if(APPLE)
check_cxx_source_compiles("#include <sys/types.h>
#include <sys/ptrace.h>
int main() { ptrace(PT_DENY_ATTACH, 0, 0, 0); return 0; }"
HAVE_PT_DENY_ATTACH)
endif()
endif()
2017-02-15 18:44:20 -05:00
include_directories(SYSTEM ${GCRYPT_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR})
2015-07-22 10:59:38 -04:00
include(FeatureSummary)
2010-08-07 09:10:44 -04:00
add_subdirectory(src)
add_subdirectory(share)
2012-05-21 17:37:29 -04:00
if(WITH_TESTS)
add_subdirectory(tests)
endif(WITH_TESTS)
if(PRINT_SUMMARY)
# This will print ENABLED, REQUIRED and DISABLED
feature_summary(WHAT ALL)
else()
# This will only print ENABLED and DISABLED feature
feature_summary(WHAT ENABLED_FEATURES DESCRIPTION "Enabled features:")
feature_summary(WHAT DISABLED_FEATURES DESCRIPTION "Disabled features:")
endif()