summaryrefslogtreecommitdiff
path: root/xmake/rules/qt/load.lua
blob: 29dad499def05014232590b2c9cfb90f808bf440 (plain)
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
--!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, Xmake Open Source Community.
--
-- @author      ruki
-- @file        load.lua
--

-- imports
import("core.base.semver")
import("core.project.config")
import("core.project.target", {alias = "core_target"})
import("core.base.hashset")
import("lib.detect.find_library")

-- make link for framework
function _link(target, linkdirs, framework, qt_sdkver, infix)
    if framework:startswith("Qt") then
        local debug_suffix = "_debug"
        if target:is_plat("windows") then
            debug_suffix = "d"
        elseif target:is_plat("mingw") then
            if qt_sdkver:ge("5.15.2") then
                debug_suffix = ""
            else
                debug_suffix = "d"
            end
        elseif target:is_plat("android") or target:is_plat("linux") or target:is_plat("cross") then
            debug_suffix = ""
        end
        if qt_sdkver:ge("5.0") then
            framework = "Qt" .. qt_sdkver:major() .. framework:sub(3) .. infix .. (is_mode("debug") and debug_suffix or "")
        else -- for qt4.x, e.g. QtGui4.lib
            if target:is_plat("windows", "mingw") then
                framework = "Qt" .. framework:sub(3) .. infix .. (is_mode("debug") and debug_suffix or "") .. qt_sdkver:major()
            else
                framework = "Qt" .. framework:sub(3) .. infix .. (is_mode("debug") and debug_suffix or "")
            end
        end
        if target: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(target, linkdirs, qt_sdkver, libpattern)
    local links = {}
    local debug_suffix = "_debug"
    if target:is_plat("windows") then
        debug_suffix = "d"
    elseif target:is_plat("mingw") then
        debug_suffix = "d"
    elseif target:is_plat("android") or target:is_plat("linux") or target:is_plat("cross")  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 to ignore qt framework libraries, e.g. libQt5xxx.a, Qt5Core.lib ..
            -- but bundled library names like libQt5Bundledxxx.a on Qt6.x
            -- @see https://github.com/xmake-io/xmake/issues/3572
            if basename:startswith("libQt" .. qt_sdkver:major() .. "Bundled") or (
                (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, core_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", table.unpack(table.wrap(plugin.links)))
        end
        if plugin.linkdirs then
            target:values_add("qt.linkdirs", table.unpack(table.wrap(plugin.linkdirs)))
        end
        if plugin.resources then
            target:values_add("qt.plugin_resources", table.unpack(table.wrap(plugin.resources)))
        end
        -- TODO: add prebuilt object files in qt sdk.
        -- these file is located at plugins/xxx/objects-Release/xxxPlugin_init/xxxPlugin_init.cpp.o
    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

-- get target c++ version
function _get_target_cppversion(target)
    local languages = target:get("languages")
    for _, language in ipairs(languages) do
        if language:startswith("c++") or language:startswith("cxx") then
            local v = language:match("%d+") or language:match("latest")
            if v then return v end
        end
    end
end

-- get frameworks from target
-- @see https://github.com/xmake-io/xmake/issues/4135
function _get_frameworks_from_target(target)
    local values = table.wrap(target:get("frameworks"))
    for _, value in ipairs((target:get_from("frameworks", "option::*"))) do
        table.join2(values, value)
    end
    for _, value in ipairs((target:get_from("frameworks", "package::*"))) do
        table.join2(values, value)
    end
    for _, value in ipairs((target:get_from("__qt_frameworks", "dep::*", {interface = true}))) do
        table.join2(values, value)
    end
    return table.unique(values)
end

-- generate static plugin import file
function _generate_plugin_import(target)
    local plugins = target:values("qt.plugins")
    if not plugins then
        return
    end
    local importfile = path.join(config.builddir(), ".qt", "plugin", target:name(), "static_import.cpp")
    local content = "#include <QtPlugin>\n"
    for _, plugin in ipairs(plugins) do
        content = content .. string.format("Q_IMPORT_PLUGIN(%s)\n", plugin)
    end
    local plugin_resources = target:values("qt.plugin_resources")
    if plugin_resources then
        content = content .. "int init_qt_plugin_resources() {\n"
        for _, res in ipairs(table.unique(plugin_resources)) do
            content = content .. string.format("    Q_INIT_RESOURCE(%s);\n", res)
        end
        content = content .. "    return 0;\n}\n"
        content = content .. "static int s_init_qt_plugin_resources = init_qt_plugin_resources();\n"
    end
    io.writefile(importfile, content)
    target:add("files", importfile)
end

function _add_qmakeprllibs(target, prlfile, qt)
    if os.isfile(prlfile) then
        local contents = io.readfile(prlfile)
        local envs = {}
        if contents then
            for _, prlenv in ipairs(contents:split('\n', {plain = true})) do
                local kv = prlenv:split('=', {plain = true})
                if #kv == 2 then
                    envs[kv[1]:trim()] = kv[2]:trim()
                end
            end
        end
        if envs.QMAKE_PRL_LIBS_FOR_CMAKE then
            for _, lib in ipairs(envs.QMAKE_PRL_LIBS_FOR_CMAKE:split(';', {plain = true})) do
                local lib = lib
                if lib:startswith("-L") then
                    local libdir = lib:sub(3)
                    target:add("linkdirs", libdir)
                else
                    if qt.qmldir then
                        lib = string.gsub(lib, "%$%$%[QT_INSTALL_QML%]", qt.qmldir)
                    end
                    if qt.sdkdir then
                        lib = string.gsub(lib, "%$%$%[QT_INSTALL_PREFIX%]", qt.sdkdir)
                    end
                    if qt.pluginsdir then
                        lib = string.gsub(lib, "%$%$%[QT_INSTALL_PLUGINS%]", qt.pluginsdir)
                    end
                    if qt.libdir then
                        lib = string.gsub(lib, "%$%$%[QT_INSTALL_LIBS%]", qt.libdir)
                    end
                    if lib:startswith("-l") then
                        lib = lib:sub(3)
                    end
                    target:add("syslinks", lib)
                end
            end
        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

    -- get qt sdk infix
    local infix = ""
    if qt.mkspecsdir then
        if os.isfile(path.join(qt.mkspecsdir, "qconfig.pri")) then
            local qconfig = io.readfile(path.join(qt.mkspecsdir, "qconfig.pri"))
            if qconfig then
                qconfig = qconfig:trim():split("\n")
                for _, line in ipairs(qconfig) do
                    if line:startswith("QT_LIBINFIX") then
                        local kv = line:split("=", {plain = true, limit = 2})
                        if #kv == 2 then
                            infix = kv[2]:trim()
                        end
                    end
                end
            end
        end
    end

    -- add -fPIC
    if not target:is_plat("windows", "mingw") then
        target:add("cxflags", "-fPIC")
        target:add("mxflags", "-fPIC")
        target:add("asflags", "-fPIC")
    end

    if qt_sdkver:ge("6.0") then
        -- @see https://github.com/xmake-io/xmake/issues/2071
        if target:is_plat("windows") and target:has_tool("cxx", "clang_cl", "cl") then
            target:add("cxxflags", "/Zc:__cplusplus")
            target:add("cxxflags", "/permissive-")
        end
    end
    -- need c++11 at least
    local languages = target:get("languages")
    local cxxlang = false
    for _, lang in ipairs(languages) do
        -- c++* or gnuc++*
        if lang:find("cxx", 1, true) or lang:find("c++", 1, true) 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
        local cppversion = _get_target_cppversion(target)
        if qt_sdkver:ge("6.0") then
            -- add conditionnaly c++17 to avoid for example "cl : Command line warning D9025 : overriding '/std:c++latest' with '/std:c++17'" warning
            if (not cppversion) or (tonumber(cppversion) and tonumber(cppversion) < 17) then
                target:add("languages", "c++17")
            end
        else
            -- add conditionnaly c++11 to avoid for example "cl : Command line warning D9025 : overriding '/std:c++latest' with '/std:c++11'" warning
            if (not cppversion) or (tonumber(cppversion) and tonumber(cppversion) < 11) then
                target:add("languages", "c++11")
            end
        end
    end

    -- add definitions 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
    _generate_plugin_import(target)

    -- backup the user syslinks, we need to add them behind the qt syslinks
    local syslinks_user = target:get("syslinks")
    target:set("syslinks", nil)

    -- add qt links and directories
    target:add("syslinks", target:values("qt.links"))
    local qtprldirs = {}
    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)
            table.insert(qtprldirs, linkdir)
        end
    end
    for _, qt_link in ipairs(target:values("qt.links")) do
        for _, qt_libdir in ipairs(qtprldirs) do
            local prl_file = path.join(qt_libdir, qt_link .. ".prl")
            _add_qmakeprllibs(target, prl_file, qt)
        end
    end

    -- backup qt frameworks
    local qt_frameworks = target:get("frameworks")
    if qt_frameworks then
        target:set("__qt_frameworks", qt_frameworks)
    end
    local qt_frameworks_extra = target:extraconf("frameworks")
    if qt_frameworks_extra then
        target:extraconf_set("__qt_frameworks", qt_frameworks_extra)
    end

    -- add frameworks
    if opt.frameworks then
        target:add("frameworks", opt.frameworks)
    end

    -- do frameworks for qt
    local frameworksset = hashset.new()
    local qt_frameworks = _get_frameworks_from_target(target)
    for _, framework in ipairs(qt_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 target: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 definitions
                target:add("defines", "QT_" .. framework:sub(3):upper() .. "_LIB")

                -- add includedirs
                if target: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
                        local link = _link(target, qt.libdir, framework, qt_sdkver, infix)
                        target:add("syslinks", link)
                        _add_qmakeprllibs(target, path.join(qt.libdir, link .. ".prl"), qt)
                        _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
                    local link = _link(target, qt.libdir, framework, qt_sdkver, infix)
                    target:add("syslinks", link)
                    _add_qmakeprllibs(target, path.join(qt.libdir, link .. ".prl"), qt)
                    _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
        elseif target:is_plat("macosx") then
            --@see https://github.com/xmake-io/xmake/issues/5336
            frameworksset:insert(framework)
        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
    -- and exclude qt framework libraries, e.g. libQt5xxx.a, Qt5xxx.lib
    local libpattern
    if qt_sdkver:ge("6.0") then
        -- e.g. libQt6BundledFreetype.a on Qt6.x
        -- @see https://github.com/xmake-io/xmake/issues/3572
        libpattern = target:is_plat("windows") and "Qt*.lib" or "libQt*.a"
    else
        -- e.g. libqtmain.a, libqtfreetype.q, libqtlibpng.a on Qt5.x
        libpattern = target:is_plat("windows") and "qt*.lib" or "libqt*.a"
    end
    target:add("syslinks", _find_static_links_3rd(target, qt.libdir, qt_sdkver, libpattern))

    -- add user syslinks
    if syslinks_user then
        target:add("syslinks", syslinks_user)
    end

    -- add includedirs, linkdirs
    local fallbackmkspec = ""
    if target: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)

            -- 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, qt.includedir)
        fallbackmkspec = "macx-clang"
        target:add("linkdirs", qt.libdir)
    elseif target:is_plat("linux") then
        target:set("frameworks", nil)
        _add_includedirs(target, qt.includedir)
        fallbackmkspec = "linux-g++"
        target:add("rpathdirs", qt.libdir)
        target:add("linkdirs", qt.libdir)
    elseif target:is_plat("cross") then
        _add_includedirs(target, qt.includedir)
    elseif target:is_plat("windows") then
        target:set("frameworks", nil)
        _add_includedirs(target, qt.includedir)
        fallbackmkspec = "win32-msvc"
        target:add("linkdirs", qt.libdir)
        target:add("syslinks", "ws2_32", "gdi32", "ole32", "advapi32", "shell32", "user32", "opengl32", "imm32", "winmm", "iphlpapi")
        -- for debugger, https://github.com/xmake-io/xmake-vscode/issues/225
        if qt.bindir_host then
            target:add("runenvs", "PATH", qt.bindir_host)
        end
        if qt.bindir then
            target:add("runenvs", "PATH", qt.bindir)
        end
    elseif target:is_plat("mingw") then
        target:set("frameworks", nil)
        -- we need to fix it, because gcc maybe does not work on latest mingw when `-isystem D:\a\_temp\msys64\mingw64\include` is passed.
        -- and qt.includedir will be this path value when Qt sdk directory just is `D:\a\_temp\msys64\mingw64`
        -- @see https://github.com/msys2/MINGW-packages/issues/10761#issuecomment-1044302523
        if is_subhost("msys") then
            local mingw_prefix = os.getenv("MINGW_PREFIX")
            local mingw_includedir = path.normalize(path.join(mingw_prefix or "/", "include"))
            if qt.includedir and qt.includedir and path.normalize(qt.includedir) ~= mingw_includedir then
                _add_includedirs(target, qt.includedir)
            end
        else
            _add_includedirs(target, qt.includedir)
        end
        fallbackmkspec = "win32-g++"
        target:add("linkdirs", qt.libdir)
        target:add("syslinks", "mingw32", "ws2_32", "gdi32", "ole32", "advapi32", "shell32", "user32", "iphlpapi")
    elseif target:is_plat("android") then
        target:set("frameworks", nil)
        _add_includedirs(target, qt.includedir)
        fallbackmkspec = "android-clang"
        target:add("rpathdirs", qt.libdir)
        target:add("linkdirs", qt.libdir)
    elseif target:is_plat("wasm") then
        target:set("frameworks", nil)
        _add_includedirs(target, qt.includedir)
        fallbackmkspec = "wasm-emscripten"
        target:add("rpathdirs", qt.libdir)
        target:add("linkdirs", qt.libdir)
        -- add prebuilt object files in qt sdk.
        -- these files are located at lib/objects-Release/xxxmodule_resources_x/.rcc/xxxmodule.cpp.o
        for _, framework in ipairs(qt_frameworks) do
            local prefix = framework
            if framework:startswith("Qt") then
                prefix = framework:sub(3)
            end
            for _, filepath in ipairs(os.files(path.join(qt.libdir, "objects-*", prefix .. "_resources_*", ".rcc", "*.o"))) do
                table.insert(target:objectfiles(), filepath)
            end
        end
        target:add("ldflags", "-s FETCH=1", "-s ERROR_ON_UNDEFINED_SYMBOLS=1", "-s ALLOW_MEMORY_GROWTH=1", "--bind")
        target:add("shflags", "-s FETCH=1", "-s ERROR_ON_UNDEFINED_SYMBOLS=1", "-s ALLOW_MEMORY_GROWTH=1", "--bind")
        if qt_sdkver:ge("6.0") then
            -- @see https://github.com/xmake-io/xmake/issues/4137
            -- @see QtWasmHelpers.cmake: qt_internal_setup_wasm_target_properties
            target:add("ldflags", "-s MAX_WEBGL_VERSION=2", "-s WASM_BIGINT=1", "-s STACK_SIZE=5MB")
            target:add("ldflags", "-sASYNCIFY_IMPORTS=qt_asyncify_suspend_js,qt_asyncify_resume_js")
            -- @see Qt6WasmMacros.cmake: _qt_internal_add_wasm_extra_exported_methods
            target:add("ldflags", "-s EXPORTED_RUNTIME_METHODS=UTF16ToString,stringToUTF16,JSEvents,specialHTMLTargets,FS,callMain")
            -- @see https://github.com/emscripten-core/emscripten/issues/21844
            target:add("ldflags", "-s EXPORTED_FUNCTIONS=_main,__embind_initialize_bindings", {force = true})
            target:add("ldflags", "-s MODULARIZE=1", "-s EXPORT_NAME=createQtAppInstance")
            target:add("shflags", "-s MAX_WEBGL_VERSION=2", "-s WASM_BIGINT=1", "-s STACK_SIZE=5MB")
            target:add("shflags", "-sASYNCIFY_IMPORTS=qt_asyncify_suspend_js,qt_asyncify_resume_js")
            target:add("shflags", "-s EXPORTED_RUNTIME_METHODS=UTF16ToString,stringToUTF16,JSEvents,specialHTMLTargets,FS,callMain")
            target:add("shflags", "-s EXPORTED_FUNCTIONS=_main,__embind_initialize_bindings", {force = true})
            target:add("shflags", "-s MODULARIZE=1", "-s EXPORT_NAME=createQtAppInstance")
            target:set("extension", ".js")
        else
            target:add("ldflags", "-s WASM=1", "-s FULL_ES2=1", "-s FULL_ES3=1", "-s USE_WEBGL2=1")
            target:add("ldflags", "-s EXPORTED_RUNTIME_METHODS=[\"UTF16ToString\",\"stringToUTF16\"]")
            target:add("shflags", "-s WASM=1", "-s FULL_ES2=1", "-s FULL_ES3=1", "-s USE_WEBGL2=1")
            target:add("shflags", "-s EXPORTED_RUNTIME_METHODS=[\"UTF16ToString\",\"stringToUTF16\"]")
        end
    end
    _add_includedirs(target, path.join(qt.mkspecsdir, qt.mkspec or fallbackmkspec))

    -- is gui application?
    if opt.gui then
        if not target:values("windows.subsystem") then
            target:values_set("windows.subsystem", "windows")
            if target:has_tool("ld", "link", "lld-link") then
                target:add("ldflags", "-entry:mainCRTStartup", {force = true})
            end
        end
    else
        if not target:values("windows.subsystem") then
            target:values_set("windows.subsystem", "console")
        end
    end

    -- set default runtime
    -- @see https://github.com/xmake-io/xmake/issues/4161
    if not target:get("runtimes") then
        target:set("runtimes", is_mode("debug") and "MDd" or "MD")
    end
end