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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file load.lua
--
-- imports
import("core.base.semver")
import("core.project.config")
import("core.project.target")
import("core.base.hashset")
import("lib.detect.find_library")
-- make link for framework
function _link(linkdirs, framework, qt_sdkver)
if framework:startswith("Qt") then
local debug_suffix = "_debug"
if is_plat("windows") then
debug_suffix = "d"
elseif is_plat("mingw") then
debug_suffix = "d"
elseif is_plat("android") or is_plat("linux") then
debug_suffix = ""
end
framework = "Qt" .. qt_sdkver:major() .. framework:sub(3) .. (is_mode("debug") and debug_suffix or "")
if is_plat("android") then --> -lQt5Core_armeabi/-lQt5CoreDebug_armeabi for 5.14.x
local libinfo = find_library(framework .. "_" .. config.arch(), linkdirs)
if libinfo and libinfo.link then
framework = libinfo.link
end
end
end
return framework
end
-- find the static links from the given qt link directories, e.g. libqt*.a
function _find_static_links_3rd(linkdirs, qt_sdkver, libpattern)
local links = {}
local debug_suffix = "_debug"
if is_plat("windows") then
debug_suffix = "d"
elseif is_plat("mingw") then
debug_suffix = "d"
elseif is_plat("android") or is_plat("linux") then
debug_suffix = ""
end
for _, linkdir in ipairs(linkdirs) do
for _, libpath in ipairs(os.files(path.join(linkdir, libpattern))) do
local basename = path.basename(libpath)
-- we need ignore qt framework libraries, e.g. libQt5xxx.a, Qt5Core.lib ..
if not basename:startswith("libQt" .. qt_sdkver:major()) and not basename:startswith("Qt" .. qt_sdkver:major()) then
if (is_mode("debug") and basename:endswith(debug_suffix)) or (not is_mode("debug") and not basename:endswith(debug_suffix)) then
table.insert(links, target.linkname(path.filename(libpath)))
end
end
end
end
return links
end
-- add plugins
function _add_plugins(target, plugins)
for name, plugin in pairs(plugins) do
target:values_add("qt.plugins", name)
if plugin.links then
target:values_add("qt.links", unpack(table.wrap(plugin.links)))
end
if plugin.linkdirs then
target:values_add("qt.linkdirs", unpack(table.wrap(plugin.linkdirs)))
end
end
end
-- add includedirs if exists
function _add_includedirs(target, includedirs)
for _, includedir in ipairs(includedirs) do
if os.isdir(includedir) then
target:add("sysincludedirs", includedir)
end
end
end
-- the main entry
function main(target, opt)
-- init options
opt = opt or {}
-- get qt sdk
local qt = target:data("qt")
-- get qt sdk version
local qt_sdkver = nil
if qt.sdkver then
qt_sdkver = semver.new(qt.sdkver)
else
raise("Qt SDK version not found, please run `xmake f --qt_sdkver=xxx` to set it.")
end
-- add -fPIC
if not target:is_plat("windows", "mingw") then
target:add("cxflags", "-fPIC")
target:add("mxflags", "-fPIC")
target:add("asflags", "-fPIC")
end
-- need c++11 at least
local languages = target:get("languages")
local cxxlang = false
for _, lang in ipairs(languages) do
if lang:startswith("cxx") or lang:startswith("c++") then
cxxlang = true
break
end
end
if not cxxlang then
-- Qt6 require at least '/std:c++17'
-- @see https://github.com/xmake-io/xmake/issues/1183
if qt_sdkver:ge("6.0") then
target:add("languages", "c++17")
else
target:add("languages", "c++11")
end
end
-- add defines for the compile mode
if is_mode("debug") then
target:add("defines", "QT_QML_DEBUG")
elseif is_mode("release") then
target:add("defines", "QT_NO_DEBUG")
elseif is_mode("profile") then
target:add("defines", "QT_QML_DEBUG", "QT_NO_DEBUG")
end
-- The following define makes your compiler emit warnings if you use
-- any feature of Qt which as been marked deprecated (the exact warnings
-- depend on your compiler). Please consult the documentation of the
-- deprecated API in order to know how to port your code away from it.
target:add("defines", "QT_DEPRECATED_WARNINGS")
-- add plugins
if opt.plugins then
_add_plugins(target, opt.plugins)
end
local plugins = target:values("qt.plugins")
if plugins then
local importfile = path.join(config.buildir(), ".qt", "plugin", target:name(), "static_import.cpp")
local file = io.open(importfile, "w")
if file then
file:print("#include <QtPlugin>")
for _, plugin in ipairs(plugins) do
file:print("Q_IMPORT_PLUGIN(%s)", plugin)
end
file:close()
target:add("files", importfile)
end
end
-- backup the user syslinks, we need add them behind the qt syslinks
local syslinks_user = target:get("syslinks")
target:set("syslinks", nil)
-- add qt links and directories
for _, qt_linkdir in ipairs(target:values("qt.linkdirs")) do
local linkdir = path.join(qt.sdkdir, qt_linkdir)
if os.isdir(linkdir) then
target:add("linkdirs", linkdir)
end
end
target:add("syslinks", target:values("qt.links"))
-- add frameworks
if opt.frameworks then
target:add("frameworks", opt.frameworks)
end
-- do frameworks for qt
local frameworksset = hashset.new()
for _, framework in ipairs(target:get("frameworks")) do
-- translate qt frameworks
if framework:startswith("Qt") then
-- add private includedirs
if framework:lower():endswith("private") then
local private_dir = framework:sub(1, -#("private") - 1);
if is_plat("macosx") then
local frameworkdir = path.join(qt.libdir, framework .. ".framework")
if os.isdir(frameworkdir) then
_add_includedirs(target, path.join(frameworkdir, "Headers", qt.sdkver))
_add_includedirs(target, path.join(frameworkdir, "Headers", qt.sdkver, private_dir))
else
_add_includedirs(target, path.join(qt.includedir, private_dir, qt.sdkver, private_dir))
_add_includedirs(target, path.join(qt.includedir, private_dir, qt.sdkver))
end
else
_add_includedirs(target, path.join(qt.includedir, private_dir, qt.sdkver, private_dir))
_add_includedirs(target, path.join(qt.includedir, private_dir, qt.sdkver))
end
else
-- add defines
target:add("defines", "QT_" .. framework:sub(3):upper() .. "_LIB")
-- add includedirs
if is_plat("macosx") then
local frameworkdir = path.join(qt.libdir, framework .. ".framework")
if os.isdir(frameworkdir) and os.isdir(path.join(frameworkdir, "Headers")) then
_add_includedirs(target, path.join(frameworkdir, "Headers"))
-- e.g. QtGui.framework/Headers/5.15.0/QtGui/qpa/qplatformopenglcontext.h
-- https://github.com/xmake-io/xmake/issues/1226
_add_includedirs(target, path.join(frameworkdir, "Headers", qt.sdkver))
_add_includedirs(target, path.join(frameworkdir, "Headers", qt.sdkver, framework))
frameworksset:insert(framework)
else
target:add("syslinks", _link(qt.libdir, framework, qt_sdkver))
_add_includedirs(target, path.join(qt.includedir, framework))
-- e.g. QtGui/5.15.0/QtGui/qpa/qplatformopenglcontext.h
_add_includedirs(target, path.join(qt.includedir, framework, qt.sdkver))
_add_includedirs(target, path.join(qt.includedir, framework, qt.sdkver, framework))
end
else
target:add("syslinks", _link(qt.libdir, framework, qt_sdkver))
_add_includedirs(target, path.join(qt.includedir, framework))
_add_includedirs(target, path.join(qt.includedir, framework, qt.sdkver))
_add_includedirs(target, path.join(qt.includedir, framework, qt.sdkver, framework))
end
end
end
end
-- remove private frameworks
local local_frameworks = {}
for _, framework in ipairs(target:get("frameworks")) do
if frameworksset:has(framework) then
table.insert(local_frameworks, framework)
end
end
target:set("frameworks", local_frameworks)
-- add some static third-party links if exists, e.g. libqtmain.a, libqtfreetype.q, libqtlibpng.a
-- and exclude qt framework libraries, e.g. libQt5xxx.a, Qt5xxx.lib
target:add("syslinks", _find_static_links_3rd(qt.libdir, qt_sdkver, is_plat("windows") and "qt*.lib" or "libqt*.a"))
-- add user syslinks
if syslinks_user then
target:add("syslinks", syslinks_user)
end
-- add includedirs, linkdirs
if is_plat("macosx") then
target:add("frameworks", "DiskArbitration", "IOKit", "CoreFoundation", "CoreGraphics", "OpenGL")
target:add("frameworks", "Carbon", "Foundation", "AppKit", "Security", "SystemConfiguration")
if not frameworksset:empty() then
target:add("frameworkdirs", qt.libdir)
target:add("rpathdirs", "@executable_path/Frameworks", qt.libdir)
else
target:add("rpathdirs", qt.libdir)
_add_includedirs(target, qt.includedir)
-- remove qt frameworks
local frameworks = table.wrap(target:get("frameworks"))
for i = #frameworks, 1, -1 do
local framework = frameworks[i]
if framework:startswith("Qt") then
table.remove(frameworks, i)
end
end
target:set("frameworks", frameworks)
end
_add_includedirs(target, path.join(qt.mkspecsdir, "macx-clang"))
target:add("linkdirs", qt.libdir)
elseif is_plat("linux") then
target:set("frameworks", nil)
_add_includedirs(target, qt.includedir)
_add_includedirs(target, path.join(qt.mkspecsdir, "linux-g++"))
target:add("rpathdirs", qt.libdir)
target:add("linkdirs", qt.libdir)
elseif is_plat("windows") then
target:set("frameworks", nil)
_add_includedirs(target, qt.includedir)
_add_includedirs(target, path.join(qt.mkspecsdir, "win32-msvc"))
target:add("linkdirs", qt.libdir)
target:add("syslinks", "ws2_32", "gdi32", "ole32", "advapi32", "shell32", "user32", "OpenGL32", "imm32", "winmm", "iphlpapi")
elseif is_plat("mingw") then
target:set("frameworks", nil)
_add_includedirs(target, qt.includedir)
_add_includedirs(target, path.join(qt.mkspecsdir, "win32-g++"))
target:add("linkdirs", qt.libdir)
target:add("syslinks", "mingw32")
elseif is_plat("android") then
target:set("frameworks", nil)
_add_includedirs(target, qt.includedir)
_add_includedirs(target, path.join(qt.mkspecsdir, "android-clang"))
target:add("rpathdirs", qt.libdir)
target:add("linkdirs", qt.libdir)
elseif is_plat("wasm") then
target:set("frameworks", nil)
_add_includedirs(target, qt.includedir)
_add_includedirs(target, path.join(qt.mkspecsdir, "wasm-emscripten"))
target:add("rpathdirs", qt.libdir)
target:add("linkdirs", qt.libdir)
target:add("ldflags", "-s WASM=1", "-s FETCH=1", "-s FULL_ES2=1", "-s FULL_ES3=1", "-s USE_WEBGL2=1", "--bind")
target:add("ldflags", "-s ERROR_ON_UNDEFINED_SYMBOLS=1", "-s EXTRA_EXPORTED_RUNTIME_METHODS=[\"UTF16ToString\",\"stringToUTF16\"]", "-s ALLOW_MEMORY_GROWTH=1")
target:add("shflags", "-s WASM=1", "-s FETCH=1", "-s FULL_ES2=1", "-s FULL_ES3=1", "-s USE_WEBGL2=1", "--bind")
target:add("shflags", "-s ERROR_ON_UNDEFINED_SYMBOLS=1", "-s EXTRA_EXPORTED_RUNTIME_METHODS=[\"UTF16ToString\",\"stringToUTF16\"]", "-s ALLOW_MEMORY_GROWTH=1")
end
-- is gui application?
if opt.gui then
-- add -subsystem:windows for windows platform
if is_plat("windows") then
target:add("defines", "_WINDOWS")
local subsystem = false
for _, ldflag in ipairs(target:get("ldflags")) do
ldflag = ldflag:lower()
if ldflag:find("[/%-]subsystem:") then
subsystem = true
break
end
end
-- maybe user will set subsystem to console
if not subsystem then
target:add("ldflags", "-subsystem:windows", "-entry:mainCRTStartup", {force = true})
end
elseif is_plat("mingw") then
target:add("ldflags", "-mwindows", {force = true})
end
end
end
|