summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-05-11 23:03:59 +0800
committerGitHub <[email protected]>2025-05-11 23:03:59 +0800
commit2465ded6010de4083d574775476828923bf25c03 (patch)
treebb0ae9678c6b521fc136686cf94faeb5f9cabe6d
parent22fcfc64078f4ad04a9ecf5af30adaf7dae199e5 (diff)
parent362e731a904975eefbc337ef0a8cc3562d1ae6e8 (diff)
Merge pull request #6414 from Arthapz/add-windows-rules
(rules) add win.subsystem rules
-rw-r--r--xmake/rules/platform/windows/subsystem/xmake.lua51
-rw-r--r--xmake/rules/platform/xmake.lua9
-rw-r--r--xmake/rules/qt/load.lua26
-rw-r--r--xmake/rules/wdk/load.lua12
-rw-r--r--xmake/rules/winsdk/mfc/mfc.lua4
-rw-r--r--xmake/rules/winsdk/xmake.lua21
6 files changed, 79 insertions, 44 deletions
diff --git a/xmake/rules/platform/windows/subsystem/xmake.lua b/xmake/rules/platform/windows/subsystem/xmake.lua
new file mode 100644
index 000000000..a1cc9138c
--- /dev/null
+++ b/xmake/rules/platform/windows/subsystem/xmake.lua
@@ -0,0 +1,51 @@
+--!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, Arthapz
+-- @file xmake.lua
+--
+
+-- define rule: subsystem
+-- set any of "boot_application" "console" "efi_application" "efi_boot_service_driver" "efi_rom" "efi_runtime_driver" "native" "posix" "windows"
+-- with target:set_values("windows.subsystem", <your value>) and the rule will pass the proper flag to the linker
+rule("platform.windows.subsystem")
+ on_config("mingw", "windows", function(target)
+ local subsystems = {
+ "BOOT_APPLICATION", "CONSOLE", "EFI_APPLICATION", "EFI_BOOT_SERVICE_DRIVER", "EFI_ROM", "EFI_RUNTIME_DRIVER", "NATIVE", "POSIX", "WINDOWS"
+ }
+
+ local subsystem = target:values("windows.subsystem")
+ if subsystem then
+ local valid = false
+ for _, s in ipairs(subsystems) do
+ if subsystem:upper():startwiths(s) then
+ valid = true
+ break
+ end
+ end
+ assert(valid, "Invalid subsystem " .. subsystem)
+
+ if target:has_tool("ld", "clang") then
+ target:add("ldflags", "-Wl,-subsystem:" .. subsystem, { force = true })
+ elseif target:has_tool("ld", "link", "lld-link") then
+ target:add("ldflags", "/SUBSYSTEM:" .. upper(subsystem), { force = true })
+ elseif target:has_tool("ld", "gcc", "g++") then
+ target:add("ldflags", "-Wl,-m" .. subsystem, { force = true })
+ elseif target:has_tool("ld", "ld") then
+ target:add("ldflags", "-m" .. subsystem, { force = true })
+ end
+ end
+ end)
diff --git a/xmake/rules/platform/xmake.lua b/xmake/rules/platform/xmake.lua
index 0cf18af90..ee2afaaa2 100644
--- a/xmake/rules/platform/xmake.lua
+++ b/xmake/rules/platform/xmake.lua
@@ -19,12 +19,13 @@
--
rule("platform.wasm")
- add_deps("platform.wasm.preloadfiles")
- add_deps("platform.wasm.installfiles")
+ add_deps("platform.wasm.preloadfiles",
+ "platform.wasm.installfiles")
rule("platform.windows")
- add_deps("platform.windows.def")
- add_deps("platform.windows.idl")
+ add_deps("platform.windows.def",
+ "platform.windows.idl",
+ "platform.windows.subsystem")
if is_host("windows") then
add_deps("platform.windows.manifest")
end
diff --git a/xmake/rules/qt/load.lua b/xmake/rules/qt/load.lua
index 0c7b68d71..7ec3c02aa 100644
--- a/xmake/rules/qt/load.lua
+++ b/xmake/rules/qt/load.lua
@@ -518,25 +518,15 @@ function main(target, opt)
-- is gui application?
if opt.gui then
- -- add -subsystem:windows for windows platform
- if target:is_plat("windows") then
- target:add("defines", "_WINDOWS")
- local subsystem = false
- for _, ldflag in ipairs(target:get("ldflags")) do
- if type(ldflag) == "string" then
- ldflag = ldflag:lower()
- if ldflag:find("[/%-]subsystem:") then
- subsystem = true
- break
- end
- end
- end
- -- maybe user will set subsystem to console
- if not subsystem then
- target:add("ldflags", "-subsystem:windows", "-entry:mainCRTStartup", {force = true})
+ 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
- elseif target:is_plat("mingw") then
- target:add("ldflags", "-mwindows", {force = true})
+ end
+ else
+ if not target:values("windows.subsystem") then
+ target:values_set("windows.subsystem", "console")
end
end
diff --git a/xmake/rules/wdk/load.lua b/xmake/rules/wdk/load.lua
index 62cf99467..5700a5627 100644
--- a/xmake/rules/wdk/load.lua
+++ b/xmake/rules/wdk/load.lua
@@ -34,7 +34,9 @@ function driver_umdf(target)
-- add subsystem
local winver = target:values("wdk.env.winver") or config.get("wdk_winver")
- target:add("shflags", "-subsystem:windows," .. os_winver.subsystem(winver), {force = true})
+ if not target:values("windows.subsystem") then
+ target:values_set("windows.subsystem", "windows," .. os_winver.subsystem(winver))
+ end
-- set default driver entry if does not exist
local entry = false
@@ -74,7 +76,9 @@ function driver_kmdf(target)
target:add("ldflags", "-nodefaultlib", {force = true})
-- add subsystem
- target:add("ldflags", "-subsystem:native," .. os_winver.subsystem(winver), {force = true})
+ if not target:values("windows.subsystem") then
+ target:values_set("windows.subsystem", "native," .. os_winver.subsystem(winver))
+ end
-- set default driver entry if does not exist
local entry = false
@@ -117,7 +121,9 @@ function driver_wdm(target)
target:add("ldflags", "-nodefaultlib", {force = true})
-- add subsystem
- target:add("ldflags", "-subsystem:native," .. os_winver.subsystem(winver), {force = true})
+ if not target:values("windows.subsystem") then
+ target:values_set("windows.subsystem", "native," .. os_winver.subsystem(winver))
+ end
-- set default driver entry if does not exist
local entry = false
diff --git a/xmake/rules/winsdk/mfc/mfc.lua b/xmake/rules/winsdk/mfc/mfc.lua
index 69b4f9eff..a5d57d713 100644
--- a/xmake/rules/winsdk/mfc/mfc.lua
+++ b/xmake/rules/winsdk/mfc/mfc.lua
@@ -87,7 +87,9 @@ function application(target, mfc_kind)
_remove_flags(target)
-- set windows subsystem
- target:add("ldflags", "-subsystem:windows", {force = true})
+ if not target:values("windows.subsystem") then
+ target:values_set("windows.subsystem", "windows")
+ end
-- forces a link to complete even with unresolved symbols
if mfc_kind == "static" then
diff --git a/xmake/rules/winsdk/xmake.lua b/xmake/rules/winsdk/xmake.lua
index 6461c4eca..04814653c 100644
--- a/xmake/rules/winsdk/xmake.lua
+++ b/xmake/rules/winsdk/xmake.lua
@@ -30,24 +30,9 @@ rule("win.sdk.application")
end)
after_load(function (target)
-
- -- set subsystem: windows
- if target:is_plat("mingw") then
- target:add("ldflags", "-mwindows", {force = true})
- else
- local subsystem = false
- for _, ldflag in ipairs(target:get("ldflags")) do
- if type(ldflag) == "string" then
- ldflag = ldflag:lower()
- if ldflag:find("[/%-]subsystem:") then
- subsystem = true
- break
- end
- end
- end
- if not subsystem then
- target:add("ldflags", "-subsystem:windows", {force = true, tools = {"link"}})
- end
+ -- set windows subsystem
+ if not target:values("windows.subsystem") then
+ target:values_set("windows.subsystem", "windows")
end
-- add links