Skip to content

Commit 2465ded

Browse files
authored
Merge pull request #6414 from Arthapz/add-windows-rules
(rules) add win.subsystem rules
2 parents 22fcfc6 + 362e731 commit 2465ded

File tree

6 files changed

+79
-44
lines changed

6 files changed

+79
-44
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--!A cross-platform build utility based on Lua
2+
--
3+
-- Licensed under the Apache License, Version 2.0 (the "License");
4+
-- you may not use this file except in compliance with the License.
5+
-- You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
-- Unless required by applicable law or agreed to in writing, software
10+
-- distributed under the License is distributed on an "AS IS" BASIS,
11+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
-- See the License for the specific language governing permissions and
13+
-- limitations under the License.
14+
--
15+
-- Copyright (C) 2015-present, TBOOX Open Source Group.
16+
--
17+
-- @author ruki, Arthapz
18+
-- @file xmake.lua
19+
--
20+
21+
-- define rule: subsystem
22+
-- set any of "boot_application" "console" "efi_application" "efi_boot_service_driver" "efi_rom" "efi_runtime_driver" "native" "posix" "windows"
23+
-- with target:set_values("windows.subsystem", <your value>) and the rule will pass the proper flag to the linker
24+
rule("platform.windows.subsystem")
25+
on_config("mingw", "windows", function(target)
26+
local subsystems = {
27+
"BOOT_APPLICATION", "CONSOLE", "EFI_APPLICATION", "EFI_BOOT_SERVICE_DRIVER", "EFI_ROM", "EFI_RUNTIME_DRIVER", "NATIVE", "POSIX", "WINDOWS"
28+
}
29+
30+
local subsystem = target:values("windows.subsystem")
31+
if subsystem then
32+
local valid = false
33+
for _, s in ipairs(subsystems) do
34+
if subsystem:upper():startwiths(s) then
35+
valid = true
36+
break
37+
end
38+
end
39+
assert(valid, "Invalid subsystem " .. subsystem)
40+
41+
if target:has_tool("ld", "clang") then
42+
target:add("ldflags", "-Wl,-subsystem:" .. subsystem, { force = true })
43+
elseif target:has_tool("ld", "link", "lld-link") then
44+
target:add("ldflags", "/SUBSYSTEM:" .. upper(subsystem), { force = true })
45+
elseif target:has_tool("ld", "gcc", "g++") then
46+
target:add("ldflags", "-Wl,-m" .. subsystem, { force = true })
47+
elseif target:has_tool("ld", "ld") then
48+
target:add("ldflags", "-m" .. subsystem, { force = true })
49+
end
50+
end
51+
end)

xmake/rules/platform/xmake.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
--
2020

2121
rule("platform.wasm")
22-
add_deps("platform.wasm.preloadfiles")
23-
add_deps("platform.wasm.installfiles")
22+
add_deps("platform.wasm.preloadfiles",
23+
"platform.wasm.installfiles")
2424

2525
rule("platform.windows")
26-
add_deps("platform.windows.def")
27-
add_deps("platform.windows.idl")
26+
add_deps("platform.windows.def",
27+
"platform.windows.idl",
28+
"platform.windows.subsystem")
2829
if is_host("windows") then
2930
add_deps("platform.windows.manifest")
3031
end

xmake/rules/qt/load.lua

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -518,25 +518,15 @@ function main(target, opt)
518518

519519
-- is gui application?
520520
if opt.gui then
521-
-- add -subsystem:windows for windows platform
522-
if target:is_plat("windows") then
523-
target:add("defines", "_WINDOWS")
524-
local subsystem = false
525-
for _, ldflag in ipairs(target:get("ldflags")) do
526-
if type(ldflag) == "string" then
527-
ldflag = ldflag:lower()
528-
if ldflag:find("[/%-]subsystem:") then
529-
subsystem = true
530-
break
531-
end
532-
end
533-
end
534-
-- maybe user will set subsystem to console
535-
if not subsystem then
536-
target:add("ldflags", "-subsystem:windows", "-entry:mainCRTStartup", {force = true})
521+
if not target:values("windows.subsystem") then
522+
target:values_set("windows.subsystem", "windows")
523+
if target:has_tool("ld", "link", "lld-link") then
524+
target:add("ldflags", "-entry:mainCRTStartup", {force = true})
537525
end
538-
elseif target:is_plat("mingw") then
539-
target:add("ldflags", "-mwindows", {force = true})
526+
end
527+
else
528+
if not target:values("windows.subsystem") then
529+
target:values_set("windows.subsystem", "console")
540530
end
541531
end
542532

xmake/rules/wdk/load.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ function driver_umdf(target)
3434

3535
-- add subsystem
3636
local winver = target:values("wdk.env.winver") or config.get("wdk_winver")
37-
target:add("shflags", "-subsystem:windows," .. os_winver.subsystem(winver), {force = true})
37+
if not target:values("windows.subsystem") then
38+
target:values_set("windows.subsystem", "windows," .. os_winver.subsystem(winver))
39+
end
3840

3941
-- set default driver entry if does not exist
4042
local entry = false
@@ -74,7 +76,9 @@ function driver_kmdf(target)
7476
target:add("ldflags", "-nodefaultlib", {force = true})
7577

7678
-- add subsystem
77-
target:add("ldflags", "-subsystem:native," .. os_winver.subsystem(winver), {force = true})
79+
if not target:values("windows.subsystem") then
80+
target:values_set("windows.subsystem", "native," .. os_winver.subsystem(winver))
81+
end
7882

7983
-- set default driver entry if does not exist
8084
local entry = false
@@ -117,7 +121,9 @@ function driver_wdm(target)
117121
target:add("ldflags", "-nodefaultlib", {force = true})
118122

119123
-- add subsystem
120-
target:add("ldflags", "-subsystem:native," .. os_winver.subsystem(winver), {force = true})
124+
if not target:values("windows.subsystem") then
125+
target:values_set("windows.subsystem", "native," .. os_winver.subsystem(winver))
126+
end
121127

122128
-- set default driver entry if does not exist
123129
local entry = false

xmake/rules/winsdk/mfc/mfc.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ function application(target, mfc_kind)
8787
_remove_flags(target)
8888

8989
-- set windows subsystem
90-
target:add("ldflags", "-subsystem:windows", {force = true})
90+
if not target:values("windows.subsystem") then
91+
target:values_set("windows.subsystem", "windows")
92+
end
9193

9294
-- forces a link to complete even with unresolved symbols
9395
if mfc_kind == "static" then

xmake/rules/winsdk/xmake.lua

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,9 @@ rule("win.sdk.application")
3030
end)
3131

3232
after_load(function (target)
33-
34-
-- set subsystem: windows
35-
if target:is_plat("mingw") then
36-
target:add("ldflags", "-mwindows", {force = true})
37-
else
38-
local subsystem = false
39-
for _, ldflag in ipairs(target:get("ldflags")) do
40-
if type(ldflag) == "string" then
41-
ldflag = ldflag:lower()
42-
if ldflag:find("[/%-]subsystem:") then
43-
subsystem = true
44-
break
45-
end
46-
end
47-
end
48-
if not subsystem then
49-
target:add("ldflags", "-subsystem:windows", {force = true, tools = {"link"}})
50-
end
33+
-- set windows subsystem
34+
if not target:values("windows.subsystem") then
35+
target:values_set("windows.subsystem", "windows")
5136
end
5237

5338
-- add links

0 commit comments

Comments
 (0)