-
Notifications
You must be signed in to change notification settings - Fork 9
/
CMakeLists.txt
229 lines (180 loc) · 7.35 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# How can I use this ?
#
# Well, license here is pretty "super-open", not even copy-left or copy-right,
# basically its close to the WTFPL, just use it as you want, as it's the most practical to you :
#
# * If you want to use it as GIT submodule and compile it in your app, do it.
# * If you prefer separate project as a shared library, do it.
# * If you need to modify the project to be able to integrate in you app (opensource or not), do it.
# * If you want to share you work on the library, thanks a lot, but if you don't, no problem.
# * If you think about some weird case I didn't talk about, well, do whatever you want, I don't need to know about it.
# * If you like it so much you want to spread the word, thank you, you're welcome.
#
# Enjoy !
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
# ──── Include guards ────
if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(FATAL_ERROR "In-source builds not allowed. " "Please make a new directory (called a build directory) and run CMake from there.")
endif()
# ──── Main Project behavior ────
set(QOLM_MAIN_PROJECT OFF)
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(QOLM_MAIN_PROJECT ON)
endif()
# ──── Default build to Release ────
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE
"Release"
CACHE STRING "Choose Release or Debug" FORCE
)
endif()
# ───── OPTIONS ─────
# General
set(QOLM_PROJECT "QOlm" CACHE STRING "Project Name")
set(QOLM_TARGET ${QOLM_PROJECT} CACHE STRING "QOlm library name")
set(QOLM_VERBOSE ${QOLM_MAIN_PROJECT} CACHE BOOL "QOlm log configuration")
set(QOLM_BUILD_SHARED ${BUILD_SHARED_LIBS} CACHE BOOL "Build as a shared library (ON) or as static (OFF)")
set(QOLM_FOLDER_PREFIX ${QOLM_PROJECT} CACHE STRING "Prefix folder for all QOlm generated targets in generated project (only decorative)")
# Tests
set(QOLM_ENABLE_TESTS OFF CACHE BOOL "Create or not a target for test (compatible with CTests)")
set(QOLM_TESTS_PREFIX ${QOLM_PROJECT} CACHE STRING "Prefix for all QOlm tests")
# Examples
set(QOLM_ENABLE_EXAMPLES OFF CACHE BOOL "Create or not a target for examples")
set(QOLM_EXAMPLES_PREFIX ${QOLM_PROJECT} CACHE STRING "Prefix for all QOlm tests")
set(QOLM_ENABLE_INSTALL ${QOLM_MAIN_PROJECT} CACHE BOOL "Generate the install target")
# ───── VERSION ─────
set(QOLM_VERSION_MAJOR 3)
set(QOLM_VERSION_MINOR 2)
set(QOLM_VERSION_PATCH 2)
if( EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
EXECUTE_PROCESS(
COMMAND git rev-parse --short HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE QOLM_VERSION_TAG
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()
if(NOT QOLM_VERSION_TAG)
set(QOLM_VERSION_TAG 00000000)
endif()
set(QOLM_VERSION_TAG_HEX 0x${QOLM_VERSION_TAG})
set(QOLM_VERSION ${QOLM_VERSION_MAJOR}.${QOLM_VERSION_MINOR}.${QOLM_VERSION_PATCH} CACHE STRING "" FORCE)
set(QOLM_VERSION ${QOLM_VERSION_MAJOR}.${QOLM_VERSION_MINOR}.${QOLM_VERSION_PATCH} CACHE STRING "QOlm current version, this is only decorative and will not configure any files" FORCE)
# ───── PROJECT ─────
project(${QOLM_PROJECT} VERSION ${QOLM_VERSION} LANGUAGES CXX)
if(QOLM_MAIN_PROJECT)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
endif()
if(QOLM_ENABLE_INSTALL)
include(GNUInstallDirs)
endif()
# ───── CONFIG ─────
if(QOLM_VERBOSE)
message(STATUS "---------------- ${QOLM_PROJECT} ${QOLM_VERSION} OPTIONS. ----------------")
#General
message(STATUS "QOLM_PROJECT : " ${QOLM_PROJECT})
message(STATUS "QOLM_TARGET : " ${QOLM_TARGET})
message(STATUS "QOLM_VERSION : " ${QOLM_VERSION})
message(STATUS "QOLM_VERSION_TAG : " ${QOLM_VERSION_TAG})
message(STATUS "QOLM_FOLDER_PREFIX : " ${QOLM_FOLDER_PREFIX})
message(STATUS "QOLM_BUILD_SHARED : " ${QOLM_BUILD_SHARED})
message(STATUS "QOLM_ENABLE_INSTALL : " ${QOLM_ENABLE_INSTALL})
# Tests
message(STATUS "QOLM_ENABLE_TESTS : " ${QOLM_ENABLE_TESTS})
if(QOLM_ENABLE_TESTS)
message(STATUS "QOLM_TESTS_PREFIX : " ${QOLM_TESTS_PREFIX})
endif()
# Examples
message(STATUS "QOLM_ENABLE_EXAMPLES : " ${QOLM_ENABLE_EXAMPLES})
if(QOLM_ENABLE_EXAMPLES)
message(STATUS "QOLM_EXAMPLES_PREFIX : " ${QOLM_EXAMPLES_PREFIX})
endif()
message(STATUS "---------------- DONE WITH OPTIONS. -----------------")
endif()
# ───── DEPENDENCIES ─────
if(NOT TARGET Qt::Core)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Qml REQUIRED)
endif()
# ───── LIBRARY ─────
set(QOLM_HDRS
include/QOlm/Details/Export.hpp
include/QOlm/Details/QOlmBase.hpp
include/QOlm/QOlm.hpp
)
set(QOLM_SRCS ${QOLM_HDRS}
src/QOlmBase.cpp
)
if(QOLM_BUILD_SHARED)
add_library(${QOLM_TARGET} SHARED ${QOLM_SRCS})
target_compile_definitions(${QOLM_TARGET} PRIVATE "-DQOLM_DLL_EXPORT")
else()
add_library(${QOLM_TARGET} STATIC ${QOLM_SRCS})
target_compile_definitions(${QOLM_TARGET} PUBLIC "-DQOLM_STATIC")
endif()
add_library(${QOLM_TARGET}::${QOLM_TARGET} ALIAS ${QOLM_TARGET})
target_include_directories(${QOLM_TARGET} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_compile_features(${QOLM_TARGET} PUBLIC cxx_std_17)
target_link_libraries(${QOLM_TARGET} PUBLIC Qt::Core Qt::Qml)
set_target_properties(${QOLM_TARGET} PROPERTIES FOLDER ${QOLM_FOLDER_PREFIX})
source_group("QOlm" FILES ${QOLM_SRCS})
set_target_properties(${QOLM_TARGET} PROPERTIES
AUTOMOC TRUE
)
# ───── LIBRARY TESTS ─────
if(QOLM_ENABLE_TESTS)
include(cmake/FetchGTest.cmake)
enable_testing()
add_subdirectory(tests)
endif()
# ───── LIBRARY EXAMPLES ─────
if(QOLM_ENABLE_EXAMPLES)
add_subdirectory(examples)
endif()
# ───── LOG BUILD COMMAND ─────
if(QOLM_MAIN_PROJECT)
include(cmake/PrintConfiguration.cmake)
endif()
# ───── INSTALLATION ─────
if(QOLM_ENABLE_INSTALL)
install(TARGETS ${QOLM_TARGET} EXPORT ${QOLM_TARGET}Targets)
foreach(HDR_FILE ${QOLM_HDRS})
get_filename_component(HDR_DIRECTORY ${HDR_FILE} DIRECTORY)
get_filename_component(ABSOLUTE_HDR_DIRECTORY ${HDR_DIRECTORY} ABSOLUTE)
file(RELATIVE_PATH RELATIVE_HDR_PATH ${CMAKE_CURRENT_SOURCE_DIR}/include ${ABSOLUTE_HDR_DIRECTORY})
install(FILES ${HDR_FILE} DESTINATION include/${RELATIVE_HDR_PATH})
endforeach()
export(EXPORT ${QOLM_TARGET}Targets
FILE "${CMAKE_CURRENT_BINARY_DIR}/${QOLM_TARGET}/${QOLM_TARGET}Targets.cmake"
NAMESPACE ${QOLM_TARGET}::
)
configure_file(cmake/${QOLM_TARGET}Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/${QOLM_TARGET}/${QOLM_TARGET}Config.cmake"
@ONLY
)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${QOLM_TARGET}/${QOLM_TARGET}ConfigVersion.cmake"
VERSION ${QOLM_VERSION}
COMPATIBILITY AnyNewerVersion
)
set(ConfigPackageLocation lib/cmake/${QOLM_TARGET})
install(EXPORT ${QOLM_TARGET}Targets
FILE
${QOLM_TARGET}Targets.cmake
NAMESPACE
${QOLM_TARGET}::
DESTINATION
${ConfigPackageLocation}
)
install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/${QOLM_TARGET}/${QOLM_TARGET}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${QOLM_TARGET}/${QOLM_TARGET}ConfigVersion.cmake"
DESTINATION
${ConfigPackageLocation}
)
endif()