Skip to content

Commit 58732ea

Browse files
committed
Improve CMakeLists
1 parent e3d5b3c commit 58732ea

2 files changed

Lines changed: 112 additions & 85 deletions

File tree

CMakeLists.txt

Lines changed: 76 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,72 @@
22
# Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
33

44
project(sqlrestore)
5-
cmake_minimum_required(VERSION 3.0)
6-
if(CMAKE_VERSION VERSION_GREATER 3.0)
7-
cmake_policy(SET CMP0054 NEW)
8-
endif()
5+
cmake_minimum_required(VERSION 3.1)
6+
cmake_policy(SET CMP0054 NEW)
97

108
include(CheckCXXCompilerFlag)
119
include(CheckIncludeFiles)
1210
include(FindPkgConfig)
1311
include(FindPackageHandleStandardArgs)
1412
include(cmake/Version.cmake)
15-
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
13+
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
1614
include(cmake/Rpm.cmake)
1715
include(cmake/Deb.cmake)
1816
endif()
1917

2018
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
19+
20+
option(BUILD_WERROR "Build with -Werror" OFF)
21+
22+
if(WIN32)
23+
option(ENABLE_WIN32_CONSOLE "Show the windows console even outside Debug mode" OFF)
24+
endif()
25+
26+
if(MSVC)
27+
set(CMAKE_C_STANDARD 99)
28+
else()
29+
set(CMAKE_C_STANDARD 11)
30+
endif()
31+
2132
set(CMAKE_CXX_STANDARD 17)
2233
set(CMAKE_CXX_STANDARD_REQUIRED ON)
2334

24-
list(APPEND COMPILE_OPTIONS
25-
$<$<COMPILE_LANGUAGE:C>:-std=c99>
26-
$<$<COMPILE_LANGUAGE:CXX>:-std=c++17>
27-
-Wall
28-
-Wextra
29-
-Wpedantic
30-
-Wunused
31-
-Wuninitialized
32-
-Wredundant-decls
33-
-Wcast-align
34-
-Winit-self
35-
-Wmissing-include-dirs
36-
-Wmissing-declarations
37-
-Wstrict-overflow=2
38-
-Wunused-parameter
39-
-Wformat=2
40-
-Wdisabled-optimization
41-
-Wno-sign-conversion
42-
$<$<COMPILE_LANGUAGE:CXX>:-Woverloaded-virtual>
43-
$<$<COMPILE_LANGUAGE:CXX>:-Wno-old-style-cast>
44-
$<$<COMPILE_LANGUAGE:CXX>:-fpermissive>
45-
)
46-
47-
if(APPLE)
48-
list(APPEND COMPILE_OPTIONS -Wno-unused-parameter)
49-
endif()
50-
51-
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
52-
list(APPEND COMPILE_OPTIONS -Wno-language-extension-token)
35+
if(MSVC)
36+
list(APPEND COMPILE_OPTIONS /std:c++17)
37+
else()
38+
list(APPEND COMPILE_OPTIONS
39+
$<$<COMPILE_LANGUAGE:C>:-std=c11>
40+
$<$<COMPILE_LANGUAGE:CXX>:-std=c++17>
41+
-Wall
42+
-Wextra
43+
-Wpedantic
44+
-Wunused
45+
-Wuninitialized
46+
-Wredundant-decls
47+
-Wcast-align
48+
-Winit-self
49+
-Wmissing-include-dirs
50+
-Wmissing-declarations
51+
-Wstrict-overflow=2
52+
-Wunused-parameter
53+
-Wformat=2
54+
-Wdisabled-optimization
55+
-Wno-sign-conversion
56+
$<$<COMPILE_LANGUAGE:CXX>:-Woverloaded-virtual>
57+
$<$<COMPILE_LANGUAGE:CXX>:-Wold-style-cast>
58+
)
5359
endif()
5460

55-
option(BUILD_WERROR "Build with -Werror" ON)
56-
if(BUILD_WERROR)
57-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
58-
endif(BUILD_WERROR)
61+
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
62+
list(APPEND COMPILE_OPTIONS -Wno-language-extension-token)
63+
endif()
5964

60-
add_compile_options(${COMPILE_OPTIONS})
65+
if(NOT MSVC AND BUILD_WERROR)
66+
list(APPEND COMPILE_OPTIONS -Werror)
67+
endif()
6168

62-
if(${CMAKE_BUILD_TYPE} MATCHES "Release")
63-
add_definitions(-DNDEBUG)
64-
add_definitions(-DQT_NO_DEBUG_OUTPUT)
65-
#add_definitions(-DQT_NO_WARNING_OUTPUT)
69+
if(COMPILE_OPTIONS)
70+
add_compile_options(${COMPILE_OPTIONS})
6671
endif()
6772

6873
find_program(CCACHE_EXECUTABLE NAMES ccache)
@@ -75,7 +80,7 @@ endif()
7580
find_package(PkgConfig REQUIRED)
7681
find_package(Threads REQUIRED)
7782
find_package(Boost REQUIRED)
78-
find_package(Backtrace QUIET)
83+
find_package(Backtrace)
7984
if(Backtrace_FOUND)
8085
set(HAVE_BACKTRACE ON)
8186
endif()
@@ -89,11 +94,7 @@ if(GLIB_FOUND)
8994
endif()
9095

9196
# QT
92-
93-
if(NOT QT_DEFAULT_MAJOR_VERSION)
94-
set(QT_DEFAULT_MAJOR_VERSION 5)
95-
endif()
96-
set(QT_MAJOR_VERSION ${QT_DEFAULT_MAJOR_VERSION} CACHE STRING "Qt version to use (5 or 6), defaults to ${QT_DEFAULT_MAJOR_VERSION}")
97+
set(QT_MAJOR_VERSION 6 CACHE STRING "Qt version to use (5 or 6), defaults to 6")
9798

9899
option(BUILD_WITH_QT5 "Use Qt 5" OFF)
99100
option(BUILD_WITH_QT6 "Use Qt 6" OFF)
@@ -108,15 +109,17 @@ else()
108109
elseif(QT_MAJOR_VERSION EQUAL 6)
109110
set(BUILD_WITH_QT6 ON)
110111
else()
111-
set(BUILD_WITH_QT5 ON)
112-
set(QT_MAJOR_VERSION 5)
112+
set(BUILD_WITH_QT6 ON)
113+
set(QT_MAJOR_VERSION 6)
113114
endif()
114115
endif()
115116

117+
set(QT_DEFAULT_MAJOR_VERSION ${QT_MAJOR_VERSION})
118+
116119
set(QT_COMPONENTS Core Concurrent Network Sql Gui Widgets)
117120

118121
if(QT_MAJOR_VERSION EQUAL 5)
119-
set(QT_MIN_VERSION 5.9)
122+
set(QT_MIN_VERSION 5.12)
120123
endif()
121124

122125
find_package(Qt${QT_MAJOR_VERSION} ${QT_MIN_VERSION} REQUIRED COMPONENTS ${QT_COMPONENTS})
@@ -127,30 +130,44 @@ set(QtGui_LIBRARIES Qt${QT_MAJOR_VERSION}::Gui)
127130
set(QtWidgets_LIBRARIES Qt${QT_MAJOR_VERSION}::Widgets)
128131
set(QtNetwork_LIBRARIES Qt${QT_MAJOR_VERSION}::Network)
129132
set(QtSql_LIBRARIES Qt${QT_MAJOR_VERSION}::Sql)
130-
set(QT_LIBRARIES Qt${QT_MAJOR_VERSION}::Core Qt${QT_MAJOR_VERSION}::Concurrent Qt${QT_MAJOR_VERSION}::Gui Qt${QT_MAJOR_VERSION}::Widgets Qt${QT_MAJOR_VERSION}::Network Qt${QT_MAJOR_VERSION}::Sql)
131133

132134
# QUAZIP
133135
find_package(QuaZip-Qt6 REQUIRED)
134136

137+
if(BUILD_WITH_QT5 AND Qt5Core_VERSION VERSION_LESS 5.15.0)
138+
macro(qt_add_resources)
139+
qt5_add_resources(${ARGN})
140+
endmacro()
141+
macro(qt_wrap_cpp)
142+
qt5_wrap_cpp(${ARGN})
143+
endmacro()
144+
macro(qt_wrap_ui)
145+
qt5_wrap_ui(${ARGN})
146+
endmacro()
147+
endif()
148+
135149
# QODBC - Build a modified QODBC driver that works with FreeTDS on UNIX renamed QODBCX
136150
if (UNIX AND _Qt5Core_PRIVATE_DIRS_EXIST AND _Qt5Sql_PRIVATE_DIRS_EXIST AND Qt5Sql_VERSION VERSION_GREATER_EQUAL 5.14.1 AND NOT APPLE)
137151
set(HAVE_QSQLODBCX ON)
138152
add_subdirectory(3rdparty/qsqlodbc)
139153
include_directories(3rdparty/qsqlodbc)
140154
endif ()
141155

142-
if (WIN32)
156+
if(WIN32 AND NOT MSVC)
143157
# RC compiler
144158
string(REPLACE "gcc" "windres" CMAKE_RC_COMPILER_INIT ${CMAKE_C_COMPILER})
145159
enable_language(RC)
146160
SET(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -O coff -o <OBJECT> <SOURCE> -I ${CMAKE_SOURCE_DIR}/dist/windows")
147-
endif(WIN32)
161+
endif()
148162

149-
# Optional bits
150-
if(WIN32)
151-
option(ENABLE_WIN32_CONSOLE "Show the windows console even outside Debug mode" OFF)
152-
endif(WIN32)
163+
if(CMAKE_BUILD_TYPE MATCHES "Release")
164+
add_definitions(-DNDEBUG)
165+
add_definitions(-DQT_NO_DEBUG_OUTPUT)
166+
endif()
153167

168+
if(WIN32)
169+
add_definitions(-DUNICODE)
170+
endif()
154171

155172
# Subdirectories
156173
add_subdirectory(src)

src/CMakeLists.txt

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ set(SOURCES
2727
set(HEADERS
2828
mainwindow.h
2929
application.h
30+
logging.h
3031
scopedresult.h
3132
aboutdialog.h
3233
qsearchfield.h
@@ -53,20 +54,12 @@ set(UI
5354
set(RESOURCES ../data/data.qrc ../data/icons.qrc)
5455
set(OTHER_SOURCES)
5556

56-
option(USE_INSTALL_PREFIX "Look for data in CMAKE_INSTALL_PREFIX" ON)
57-
5857
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
5958
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/version.h)
6059

61-
if(Qt6Core_FOUND)
62-
qt6_wrap_cpp(MOC ${HEADERS})
63-
qt6_wrap_ui(UIC ${UI})
64-
qt6_add_resources(QRC ${RESOURCES})
65-
else()
66-
qt5_wrap_cpp(MOC ${HEADERS})
67-
qt5_wrap_ui(UIC ${UI})
68-
qt5_add_resources(QRC ${RESOURCES})
69-
endif()
60+
qt_wrap_cpp(MOC ${HEADERS})
61+
qt_wrap_ui(UIC ${UI})
62+
qt_add_resources(QRC ${RESOURCES})
7063

7164
add_library(sqlrestore_lib STATIC
7265
${SOURCES}
@@ -78,53 +71,70 @@ add_library(sqlrestore_lib STATIC
7871
${OTHER_UIC_SOURCES}
7972
)
8073

74+
target_include_directories(sqlrestore_lib SYSTEM PUBLIC
75+
${GLIB_INCLUDE_DIRS}
76+
${Boost_INCLUDE_DIRS}
77+
${ZLIB_INCLUDE_DIRS}
78+
${MAGIC_INCLUDE_DIRS}
79+
${QUAZIP_INCLUDE_DIRS}
80+
${SINGLEAPPLICATION_INCLUDE_DIRS}
81+
${SINGLECOREAPPLICATION_INCLUDE_DIRS}
82+
)
83+
8184
target_include_directories(sqlrestore_lib PUBLIC
8285
${CMAKE_SOURCE_DIR}
8386
${CMAKE_BINARY_DIR}
8487
${CMAKE_CURRENT_BINARY_DIR}
8588
${CMAKE_CURRENT_SOURCE_DIR}
86-
${GLIB_INCLUDE_DIRS}
87-
${GLIBCONFIG_INCLUDE_DIRS}
88-
${Boost_INCLUDE_DIRS}
89-
${ZLIB_INCLUDE_DIRS}
90-
${QUAZIP_INCLUDE_DIRS}
9189
)
9290

93-
link_directories(
91+
target_link_directories(sqlrestore_lib PUBLIC
92+
${ZLIB_LIBRARY_DIRS}
93+
${MAGIC_LIBRARY_DIRS}
9494
${GLIB_LIBRARY_DIRS}
9595
${QUAZIP_LIBRARY_DIRS}
9696
)
9797

9898
target_link_libraries(sqlrestore_lib PUBLIC
9999
${CMAKE_THREAD_LIBS_INIT}
100-
${GLIB_LIBRARIES}
101100
${ZLIB_LIBRARIES}
102101
${MAGIC_LIBRARIES}
103-
${QT_LIBRARIES}
102+
${GLIB_LIBRARIES}
103+
${QtCore_LIBRARIES}
104+
${QtConcurrent_LIBRARIES}
105+
${QtGui_LIBRARIES}
106+
${QtWidgets_LIBRARIES}
107+
${QtNetwork_LIBRARIES}
108+
${QtSql_LIBRARIES}
104109
QuaZip::QuaZip
105110
)
106111

107-
if (UNIX AND _Qt5Core_PRIVATE_DIRS_EXIST AND _Qt5Sql_PRIVATE_DIRS_EXIST AND Qt5Sql_VERSION VERSION_GREATER_EQUAL 5.14.1 AND NOT APPLE)
112+
if(UNIX AND _Qt5Core_PRIVATE_DIRS_EXIST AND _Qt5Sql_PRIVATE_DIRS_EXIST AND Qt5Sql_VERSION VERSION_GREATER_EQUAL 5.14.1 AND NOT APPLE)
108113
target_link_libraries(sqlrestore_lib PRIVATE qsqlodbc)
109114
endif()
110115

111-
if (WIN32)
112-
target_link_libraries(sqlrestore_lib PRIVATE odbccp32 regex shlwapi bz2 harfbuzz freetype)
116+
if(WIN32)
117+
target_link_libraries(sqlrestore_lib PRIVATE odbccp32 regex shlwapi)
118+
if(CMAKE_CROSSCOMPILING)
119+
target_link_libraries(sqlrestore_lib PUBLIC bz2 harfbuzz freetype)
120+
endif()
121+
if(MSVC)
122+
target_link_libraries(sqlrestore_lib PUBLIC legacy_stdio_definitions)
123+
endif()
113124
endif()
114125

115-
116126
set(EXECUTABLE_OUTPUT_PATH ..)
117127

118128
# Show the console window in debug mode on Windows
119-
if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT ENABLE_WIN32_CONSOLE)
129+
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT ENABLE_WIN32_CONSOLE)
120130
set(SQLRESTORE-WIN32-FLAG WIN32)
121-
endif (NOT CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT ENABLE_WIN32_CONSOLE)
131+
endif()
122132

123133
# Resource file for windows
124134
if(WIN32)
125135
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../dist/windows/windres.rc.in ${CMAKE_CURRENT_BINARY_DIR}/windres.rc)
126136
set(SQLRESTORE-WIN32-RESOURCES windres.rc)
127-
endif(WIN32)
137+
endif()
128138

129139
add_executable(sqlrestore ${SQLRESTORE-WIN32-FLAG} ${SQLRESTORE-WIN32-RESOURCES} main.cpp)
130140
target_link_libraries(sqlrestore PRIVATE sqlrestore_lib)

0 commit comments

Comments
 (0)