Refactor CMakeLists, targetify everything

This commit is contained in:
Long Nguyen
2022-06-11 16:09:59 +07:00
parent 179d8c5280
commit 18de6db149
8 changed files with 182 additions and 105 deletions

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.9)
cmake_minimum_required(VERSION 3.15)
project(sqlitebrowser
VERSION 3.12.99
DESCRIPTION "GUI editor for SQLite databases"
@@ -12,13 +12,11 @@ OPTION(FORCE_INTERNAL_QHEXEDIT "Don't use distribution's QHexEdit even if availa
OPTION(ALL_WARNINGS "Enable some useful warning flags" OFF)
OPTION(sqlcipher "Build with SQLCipher library" OFF)
OPTION(LOCAL_SQLITE "Prefer SQLite in /usr/local on Unix-like systems" OFF)
OPTION(STATIC_SQLITE "Link SQLite statically" OFF)
# BUILD_VERSION is the current date in YYYYMMDD format. It is only
# used by the nightly version to add the date of the build.
string(TIMESTAMP BUILD_VERSION "%Y%m%d")
add_definitions(-std=c++14)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
@@ -33,26 +31,20 @@ else()
add_executable(${PROJECT_NAME})
endif()
# BUILD_VERSION is the current date in YYYYMMDD format. It is only
# used by the nightly version to add the date of the build.
string(TIMESTAMP BUILD_VERSION "%Y%m%d")
if(NOT BUILD_STABLE_VERSION)
target_compile_definitions(${PROJECT_NAME} PRIVATE BUILD_VERSION=${BUILD_VERSION})
endif()
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" "${CMAKE_MODULE_PATH}")
# BUILD_VERSION is the current date in YYYYMMDD format. It is only
# used by the nightly version to add the date of the build.
string(TIMESTAMP BUILD_VERSION "%Y%m%d")
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" "${CMAKE_MODULE_PATH}")
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()
if(NOT BUILD_STABLE_VERSION)
add_definitions(-DBUILD_VERSION=${BUILD_VERSION})
endif()
# Fix behavior of CMAKE_CXX_STANDARD when targeting macOS.
if(POLICY CMP0025)
cmake_policy(SET CMP0025 NEW)
@@ -64,7 +56,7 @@ if(POLICY CMP0071)
cmake_policy(SET CMP0071 NEW)
endif()
if(WIN32 AND MSVC)
if(MSVC)
if(CMAKE_CL_64)
# Paths for 64-bit windows builds
set(OPENSSL_PATH "C:/dev/OpenSSL-Win64" CACHE PATH "OpenSSL Path")
@@ -91,7 +83,7 @@ if(WIN32 AND MSVC)
endif()
endif()
set(CMAKE_PREFIX_PATH "${QT5_PATH};${SQLITE3_PATH}")
list(PREPEND CMAKE_PREFIX_PATH ${QT5_PATH} ${SQLITE3_PATH})
endif()
# See https://github.com/Homebrew/homebrew-core/issues/8392#issuecomment-325226494
@@ -105,32 +97,25 @@ endif()
find_package(Qt5 REQUIRED COMPONENTS Concurrent Gui LinguistTools Network PrintSupport Test Widgets Xml)
if(NOT FORCE_INTERNAL_QSCINTILLA)
find_package(QScintilla 2.8.10 QUIET)
find_package(QScintilla 2.8.10)
endif()
if(FORCE_INTERNAL_QCUSTOMPLOT)
set(QCustomPlot_FOUND FALSE)
else()
if(NOT FORCE_INTERNAL_QCUSTOMPLOT)
find_package(QCustomPlot)
endif()
if(FORCE_INTERNAL_QHEXEDIT)
set(QHexEdit_FOUND FALSE)
else()
if(NOT FORCE_INTERNAL_QHEXEDIT)
find_package(QHexEdit)
endif()
include_directories(${CMAKE_CURRENT_LIST_DIR}/libs/json)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/libs/json)
if(NOT QSCINTILLA_FOUND)
set(QSCINTILLA_DIR libs/qscintilla/Qt4Qt5)
add_subdirectory(${QSCINTILLA_DIR})
add_subdirectory(libs/qscintilla/Qt4Qt5)
endif()
if(NOT QHexEdit_FOUND)
set(QHexEdit_DIR libs/qhexedit)
set(QHexEdit_INCL_DIR ${QHexEdit_DIR}/src)
add_subdirectory(${QHexEdit_DIR})
add_subdirectory(libs/qhexedit)
endif()
if(NOT QCustomPlot_FOUND)
set(QCustomPlot_DIR libs/qcustomplot-source)
add_subdirectory(${QCustomPlot_DIR})
add_subdirectory(libs/qcustomplot-source)
endif()
if(ENABLE_TESTING)
@@ -410,54 +395,33 @@ endif()
# SQLCipher option
if(sqlcipher)
add_definitions(-DENABLE_SQLCIPHER)
set(LIBSQLITE_NAME sqlcipher)
set(LIBSQLITE_NAME SQLCipher)
else()
set(LIBSQLITE_NAME sqlite3)
set(LIBSQLITE_NAME SQLite3)
endif()
# add extra library path for MacOS and FreeBSD
set(EXTRAPATH APPLE OR ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
if(LOCAL_SQLITE OR EXTRAPATH)
find_library(LIBSQLITE ${LIBSQLITE_NAME} HINTS /usr/local/lib /usr/local/opt/sqlite/lib)
set(ADDITIONAL_INCLUDE_PATHS /usr/local/include /usr/local/opt/sqlite/include)
# add extra library path for MacOS and FreeBSD and when LOCAL_SQLITE is true
set(EXTRAPATH APPLE OR ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" OR LOCAL_SQLITE)
if(EXTRAPATH)
list(PREPEND CMAKE_PREFIX_PATH /usr/local/lib /usr/local/opt/sqlite/lib)
endif()
find_package(${LIBSQLITE_NAME})
if (sqlcipher)
target_link_libraries(${PROJECT_NAME} SQLCipher::SQLCipher)
else()
find_library(LIBSQLITE ${LIBSQLITE_NAME})
target_link_libraries(${PROJECT_NAME} SQLite::SQLite3)
endif()
if(STATIC_SQLITE)
string(REPLACE ".so" ".a" LIBSQLITE ${LIBSQLITE})
endif()
if(WIN32 AND MSVC)
find_path(SQLITE3_INCLUDE_DIR sqlite3.h)
if(MSVC)
if(sqlcipher)
find_file(SQLITE3_DLL sqlcipher.dll)
else()
find_file(SQLITE3_DLL sqlite3.dll)
endif()
include_directories(${SQLITE3_INCLUDE_DIR})
endif()
target_include_directories(${PROJECT_NAME} PRIVATE
${ADDITIONAL_INCLUDE_PATHS}
src
)
if(QHexEdit_FOUND)
include_directories(${QHexEdit_INCLUDE_DIR})
else()
include_directories(${QHexEdit_INCL_DIR})
endif()
if(QCustomPlot_FOUND)
include_directories(${QCustomPlot_INCLUDE_DIR})
else()
include_directories(${QCustomPlot_DIR})
endif()
if(QSCINTILLA_FOUND)
include_directories(${QSCINTILLA_INCLUDE_DIR})
else()
include_directories(${QSCINTILLA_DIR})
endif()
target_include_directories(${PROJECT_NAME} PRIVATE src)
target_sources(${PROJECT_NAME}
PRIVATE
@@ -476,52 +440,19 @@ if(ALL_WARNINGS AND CMAKE_COMPILER_IS_GNUCC)
endif()
endif()
if(NOT QHexEdit_FOUND)
add_dependencies(${PROJECT_NAME} qhexedit)
endif()
if(NOT QCustomPlot_FOUND)
add_dependencies(${PROJECT_NAME} qcustomplot)
endif()
if(NOT QSCINTILLA_FOUND)
add_dependencies(${PROJECT_NAME} qscintilla2)
endif()
if(NOT QHexEdit_FOUND)
link_directories("${CMAKE_CURRENT_BINARY_DIR}/${QHexEdit_DIR}")
endif()
if(NOT QCustomPlot_FOUND)
link_directories("${CMAKE_CURRENT_BINARY_DIR}/${QCustomPlot_DIR}")
endif()
if(NOT QSCINTILLA_FOUND)
link_directories("${CMAKE_CURRENT_BINARY_DIR}/${QSCINTILLA_DIR}")
endif()
set(QT_LIBS Qt5::Gui Qt5::Test Qt5::PrintSupport Qt5::Widgets Qt5::Network Qt5::Concurrent Qt5::Xml)
target_link_libraries(${PROJECT_NAME}
${LPTHREAD}
${QT_LIBS}
${WIN32_STATIC_LINK}
${LIBSQLITE}
${ADDITIONAL_LIBS}
)
if(QHexEdit_FOUND)
target_link_libraries(${PROJECT_NAME} ${QHexEdit_LIBRARIES})
else()
target_link_libraries(${PROJECT_NAME} qhexedit)
endif()
if(QCustomPlot_FOUND)
target_link_libraries(${PROJECT_NAME} ${QCustomPlot_LIBRARIES})
else()
target_link_libraries(${PROJECT_NAME} qcustomplot)
endif()
if(QSCINTILLA_FOUND)
target_link_libraries(${PROJECT_NAME} ${QSCINTILLA_LIBRARIES})
else()
target_link_libraries(${PROJECT_NAME} qscintilla2)
endif()
target_link_libraries(${PROJECT_NAME} QHexEdit::QHexEdit)
target_link_libraries(${PROJECT_NAME} QCustomPlot::QCustomPlot)
target_link_libraries(${PROJECT_NAME} QScintilla::QScintilla)
if(WIN32 AND MSVC)
if(MSVC)
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "DB Browser for SQLite")
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE")
set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_DEFINITIONS_DEBUG "_CONSOLE")

View File

@@ -49,3 +49,11 @@ mark_as_advanced(
QCustomPlot_INCLUDE_DIRS
QCustomPlot_LIBRARIES
)
if (QCustomPlot_FOUND AND NOT TARGET QCustomPlot::QCustomPlot)
add_library(QCustomPlot::QCustomPlot UNKNOWN IMPORTED)
set_target_properties(QCustomPlot::QCustomPlot PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${QCustomPlot_INCLUDE_DIRS}
IMPORTED_LOCATION ${QCustomPlot_LIBRARIES}
)
endif()

View File

@@ -49,3 +49,11 @@ mark_as_advanced(
QHexEdit_INCLUDE_DIRS
QHexEdit_LIBRARIES
)
if (QHexEdit_FOUND AND NOT TARGET QHexEdit::QHexEdit)
add_library(QHexEdit::QHexEdit UNKNOWN IMPORTED)
set_target_properties(QHexEdit::QHexEdit PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${QHexEdit_INCLUDE_DIRS}
IMPORTED_LOCATION ${QHexEdit_LIBRARIES}
)
endif()

View File

@@ -125,3 +125,11 @@ mark_as_advanced (
QSCINTILLA_VERSION_STRING
QSCINTILLA_ROOT_DIR
)
if (QScintilla_FOUND AND NOT TARGET QScintilla::QScintilla)
add_library(QScintilla::QScintilla UNKNOWN IMPORTED)
set_target_properties(QScintilla::QScintilla PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${QSCINTILLA_INCLUDE_DIRS}
IMPORTED_LOCATION ${QSCINTILLA_LIBRARIES}
)
endif()

115
cmake/FindSQLCipher.cmake Normal file
View File

@@ -0,0 +1,115 @@
# - Try to find SQLCipher
# Once done this will define
#
# SQLCIPHER_FOUND - system has SQLCipher
# SQLCIPHER_INCLUDE_DIR - the SQLCipher include directory
# SQLCIPHER_LIBRARIES - Link these to use SQLCipher
# SQLCIPHER_DEFINITIONS - Compiler switches required for using SQLCipher
# SQLCIPHER_VERSION - This is set to major.minor.revision (e.g. 3.4.1)
#
# Hints to find SQLCipher
#
# Set SQLCIPHER_ROOT_DIR to the root directory of a SQLCipher installation
#
# The following variables may be set
#
# SQLCIPHER_USE_OPENSSL - Set to ON/OFF to specify whether to search and use OpenSSL.
# Default is OFF.
# SQLCIPHER_OPENSSL_USE_ZLIB - Set to ON/OFF to specify whether to search and use Zlib in OpenSSL
# Default is OFF.
# Redistribution and use is allowed according to the terms of the BSD license.
# Copyright (c) 2008, Gilles Caulier, <caulier.gilles@gmail.com>
# Copyright (c) 2014, Christian Dávid, <christian-david@web.de>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# use pkg-config to get the directories and then use these values
# in the FIND_PATH() and FIND_LIBRARY() calls
if( NOT WIN32 )
find_package(PkgConfig)
pkg_check_modules(PC_SQLCIPHER QUIET sqlcipher)
set(SQLCIPHER_DEFINITIONS ${PC_SQLCIPHER_CFLAGS_OTHER})
endif( NOT WIN32 )
find_path(SQLCIPHER_INCLUDE_DIR NAMES sqlcipher/sqlite3.h
PATHS
${SQLCIPHER_ROOT_DIR}
${PC_SQLCIPHER_INCLUDEDIR}
${PC_SQLCIPHER_INCLUDE_DIRS}
${CMAKE_INCLUDE_PATH}
PATH_SUFFIXES "include"
)
find_library(SQLCIPHER_LIBRARY NAMES sqlcipher
PATHS
${PC_SQLCIPHER_LIBDIR}
${PC_SQLCIPHER_LIBRARY_DIRS}
${SQLCIPHER_ROOT_DIR}
PATH_SUFFIXES "lib"
)
set(SQLCIPHER_LIBRARIES ${SQLCIPHER_LIBRARY})
set(SQLCIPHER_INCLUDE_DIRS ${SQLCIPHER_INCLUDE_DIR})
if (SQLCIPHER_USE_OPENSSL)
find_package(OpenSSL REQUIRED COMPONENTS Crypto)
if (SQLCIPHER_OPENSSL_USE_ZLIB)
find_package(ZLIB REQUIRED)
# Official FindOpenSSL.cmake does not support Zlib
set_target_properties(OpenSSL::Crypto PROPERTIES INTERFACE_LINK_LIBRARIES ZLIB::ZLIB)
endif()
list(APPEND SQLCIPHER_LIBRARIES ${OPENSSL_LIBRARIES})
list(APPEND SQLCIPHER_INCLUDE_DIRS ${OPENSSL_INCLUDE_DIRS})
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(SQLCipher
DEFAULT_MSG SQLCIPHER_INCLUDE_DIR SQLCIPHER_LIBRARY)
# show the SQLCIPHER_INCLUDE_DIR and SQLCIPHER_LIBRARIES variables only in the advanced view
mark_as_advanced(SQLCIPHER_INCLUDE_DIR SQLCIPHER_LIBRARY)
if (NOT TARGET SQLCipher::SQLCipher)
add_library(SQLCipher::SQLCipher UNKNOWN IMPORTED)
set_property(TARGET SQLCipher::SQLCipher PROPERTY INTERFACE_COMPILE_DEFINITIONS SQLITE_HAS_CODEC)
set_property(TARGET SQLCipher::SQLCipher APPEND PROPERTY INTERFACE_COMPILE_DEFINITIONS "SQLITE_TEMPSTORE=2")
set_target_properties(SQLCipher::SQLCipher PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${SQLCIPHER_INCLUDE_DIRS}"
IMPORTED_INTERFACE_LINK_LANGUAGES "C"
IMPORTED_LOCATION "${SQLCIPHER_LIBRARY}")
if (SQLCIPHER_USE_OPENSSL)
set_target_properties(SQLCipher::SQLCipher PROPERTIES
INTERFACE_LINK_LIBRARIES OpenSSL::Crypto)
set_property(TARGET SQLCipher::SQLCipher APPEND PROPERTY INTERFACE_COMPILE_DEFINITIONS "SQLCIPHER_CRYPTO_OPENSSL")
endif()
endif()

View File

@@ -15,4 +15,7 @@ set(QCUSTOMPLOT_MOC_HDR
add_library(qcustomplot ${QCUSTOMPLOT_SRC} ${QCUSTOMPLOT_MOC_HDR} ${QCUSTOMPLOT_MOC})
target_include_directories(qcustomplot INTERFACE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(qcustomplot Qt5::Widgets Qt5::PrintSupport)
add_library(QCustomPlot::QCustomPlot ALIAS qcustomplot)

View File

@@ -22,5 +22,7 @@ set(QHEXEDIT_MOC_HDR
)
add_library(qhexedit ${QHEXEDIT_SRC} ${QHEXEDIT_HDR} ${QHEXEDIT_MOC_HDR} ${QHEXEDIT_MOC})
target_include_directories(qhexedit INTERFACE src)
target_link_libraries(qhexedit Qt5::Widgets)
add_library(QHexEdit::QHexEdit ALIAS qhexedit)

View File

@@ -170,12 +170,14 @@ set(QSCINTILLA_MOC_HDR
QT5_WRAP_CPP(QSCINTILLA_WRAP_MOC_HDR ${QSCINTILLA_MOC_HDR})
include_directories(. ../include ../lexlib ../src)
add_library(qscintilla2 ${QSCINTILLA_SRC} ${QSCINTILLA_HDR} ${QSCINTILLA_MOC_HDR} ${QSCINTILLA_MOC} ${QSCINTILLA_WRAP_MOC_HDR})
target_include_directories(qscintilla2 PRIVATE ../include ../lexlib ../src)
target_include_directories(qscintilla2 INTERFACE .)
target_link_libraries(qscintilla2 Qt5::Widgets Qt5::PrintSupport)
if (APPLE)
target_link_libraries(qscintilla2 Qt5::MacExtras)
endif()
add_library(QScintilla::QScintilla ALIAS qscintilla2)