-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQtUtils.cmake
222 lines (169 loc) · 7.56 KB
/
QtUtils.cmake
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
#
# Copyright (C) 2021-2025 QuasarApp.
# Distributed under the MIT software license, see the accompanying
# Everyone is permitted to copy and distribute verbatim copies
# of this license document, but changing it is not allowed.
#
# This module provides The qt utilty functions of QuasarApp group.
#**********************
# Availabel functions:
# *********************
# The prepareQM function - This function prepare translations for you prijects
# The updateGitVars macross - This macross update all GIT variables releative current cmake file.
# The addQML function - This function add QML module into target and prepare qmldir file.
#**********************
# Availabel VARIABLE:
# *********************
# The GIT_COMMIT_COUNT variable - This variable contains commits count
# The GIT_COMMIT_HASH variable - This variable contains short version of the buildet commit hash.
# Note:
# For update actualy value of git variables use the updateGitVars macros
if(DEFINED PROJECT_QT_UTILS_SUPPORT)
return()
else()
set(PROJECT_QT_UTILS_SUPPORT 1)
endif()
# This function add QML module into target and prepare qmldir file and qrc files.
# this function designed to prepare a static or shared library with qml components,
# that can be imported into other projects only after linked parent library.
# Arguments :
# nicecery arguments:
# target - it is name of the target for that will be added QML module.
# QML_DIR - it is directory with QML files.
# optional arguments:
# VERSION - it is version of the QML module. Default value is 1.0.
# MODULE_NAME - it is name of the QML module. Default value is TARGET_NAME.
# For example if the target name is QEQuick3d then the module name will be QEQuick3dQML.
# PUBLIC - it is flag that indicate that the QML module will be public and afailable to using in all child libs. Default value is OFF.
# Example:
# addQML(QEQuick3d QML_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
# addQML(QEQuick3d QML_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src VERSION 1.0 MODULE_NAME QEQuick3d)
#
# To get make qml module for your shared or static library:
# addQML(${CURRENT_PROJECT} QML_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/QEQuick3d" PUBLIC)
# addQML(${CURRENT_PROJECT} QML_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/QEQuick3d" VERSION 1.0 MODULE_NAME QEQuick3d PUBLIC)
function(addQML target)
set(options VERSION MODULE_NAME PUBLIC)
set(oneValueArgs QML_DIR)
cmake_parse_arguments(arg_addQML
"${options}" "${oneValueArgs}" ""
${ARGN}
)
# QML module configuration.
file(GLOB QML_FILES
RELATIVE "${arg_addQML_QML_DIR}"
"${arg_addQML_QML_DIR}/*.qml")
if(NOT EXISTS "${arg_addQML_QML_DIR}")
message(FATAL_ERROR "QML_DIR is not a readable directory")
endif()
file(GLOB_RECURSE ALL_QML_FILES
RELATIVE "${arg_addQML_QML_DIR}"
"${arg_addQML_QML_DIR}/*.qml")
if (NOT arg_addQML_VERSION)
set(arg_addQML_VERSION "1.0")
endif()
if (NOT arg_addQML_MODULE_NAME)
set(arg_addQML_MODULE_NAME "${target}")
endif()
if(${arg_addQML_PUBLIC})
set(QML_MODULE_CONTENT "")
foreach(QML_FILE ${QML_FILES})
get_filename_component(QML_NAME ${QML_FILE} NAME_WE)
set(QML_MODULE_CONTENT "${QML_MODULE_CONTENT}${QML_NAME} ${arg_addQML_VERSION} ${QML_NAME}.qml\n")
endforeach()
write_file("${arg_addQML_QML_DIR}/qmldir" "module ${arg_addQML_MODULE_NAME}\n" )
write_file("${arg_addQML_QML_DIR}/qmldir" "${QML_MODULE_CONTENT}" APPEND)
file(RELATIVE_PATH QMLDIR_RELEATIVE_PATH ${arg_addQML_QML_DIR} "${arg_addQML_QML_DIR}/qmldir")
if (NOT QML_IMPORT_PATH MATCHES "${arg_addQML_QML_DIR}/..")
set(QML_IMPORT_PATH ${QML_IMPORT_PATH} "${arg_addQML_QML_DIR}/.." CACHE STRING "update global variable to access to qml from qt creator." FORCE)
endif()
endif()
# here is creating a qrc file and adding them into target
set(QML_QRC_FILE "${arg_addQML_QML_DIR}/${arg_addQML_MODULE_NAME}.qrc")
write_file(${QML_QRC_FILE} "<RCC>\n")
write_file(${QML_QRC_FILE} " <qresource prefix=\"/${arg_addQML_MODULE_NAME}\">\n" APPEND)
if(${arg_addQML_PUBLIC})
write_file(${QML_QRC_FILE} " <file>${QMLDIR_RELEATIVE_PATH}</file>\n" APPEND)
endif()
foreach(QML_FILE ${ALL_QML_FILES})
write_file(${QML_QRC_FILE} " <file>${QML_FILE}</file>\n" APPEND)
endforeach()
write_file(${QML_QRC_FILE} " </qresource>\n" APPEND)
write_file(${QML_QRC_FILE} "</RCC>\n" APPEND)
# add to taget new qrc file as source
target_sources(${target} PRIVATE ${QML_QRC_FILE})
endfunction()
# This function prepare translations for you prijects
# Arguments :
# name - it is name of the translations target.
# sourceDir - it is root source directory. lupdate will be scan this dir for prepare translations.
# ts_files - it is list of the supported languages.
function(prepareQM name sourceDir ts_files)
find_program(LUPDATE_EXECUTABLE lupdate)
find_program(LRELEASE_EXECUTABLE lrelease)
message(supported translations = ${ts_files})
add_custom_target(${name}Translations ALL
SOURCES ${ts_files}
)
foreach(_ts_file ${ts_files})
execute_process(
COMMAND ${LUPDATE_EXECUTABLE} -no-obsolete -locations none -recursive ${sourceDir} -ts ${_ts_file})
execute_process(
COMMAND ${LRELEASE_EXECUTABLE} ${_ts_file})
endforeach()
endfunction()
# This macros create or update next variables:
# GIT_COMMIT_COUNT - This variable contains count of the commits.
# GIT_COMMIT_HASH - This variable contains short hash of the current cummit.
macro(updateGitVars)
execute_process(
COMMAND git rev-list --count HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_COUNT
)
string(STRIP "${GIT_COMMIT_COUNT}" GIT_COMMIT_COUNT)
execute_process(
COMMAND git rev-parse --short HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH
)
string(STRIP "${GIT_COMMIT_HASH}" GIT_COMMIT_HASH)
endmacro()
# This macros create or update next variables:
# GIT_COMMIT_COUNT - This variable contains count of the commits.
# GIT_COMMIT_COUNT_FROM - This variable contains count of the commits from the hash.
# GIT_COMMIT_HASH - This variable contains short hash of the current cummit.
macro(updateGitVarsWithHash hash)
execute_process(
COMMAND git rev-list --count HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_COUNT
)
string(STRIP "${GIT_COMMIT_COUNT}" GIT_COMMIT_COUNT)
execute_process(
COMMAND git rev-list ${hash}..HEAD --count
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_COUNT_FROM
)
string(STRIP "${GIT_COMMIT_COUNT_FROM}" GIT_COMMIT_COUNT_FROM)
execute_process(
COMMAND git rev-parse --short HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH
)
string(STRIP "${GIT_COMMIT_HASH}" GIT_COMMIT_HASH)
endmacro()
# This function do some as cmake function configure_file but add files into target for convenient access from editor
# Arguments :
# name - it is name of the target for that will be configuret selected file.
# file - it is file that will be configured
function(configure_file_in name file)
if (TARGET ${name}Templates)
target_sources(${name}Templates PRIVATE "${file}.in")
else()
add_custom_target(${name}Templates ALL
SOURCES "${file}.in"
)
endif()
configure_file("${file}.in" ${file} @ONLY)
endfunction()