From a10901bfee2284dacd60c0edc0fb4213011865eb Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 26 Mar 2026 00:47:07 +0800 Subject: improve has_flags --- xmake/modules/core/tools/cl/has_flags.lua | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/xmake/modules/core/tools/cl/has_flags.lua b/xmake/modules/core/tools/cl/has_flags.lua index 48884b340..57c5d2d0c 100644 --- a/xmake/modules/core/tools/cl/has_flags.lua +++ b/xmake/modules/core/tools/cl/has_flags.lua @@ -28,15 +28,38 @@ function _check_from_knownargs(flags, opt) local flag = flags[1]:gsub("/", "-") local known_flags = _g.known_flags if known_flags == nil then - known_flags = hashset.from({"-Ox", "-O1", "-O2", "-Od", "-MT", "-MD", "-MTd", "-MDd", "-EHsc"}) + known_flags = hashset.from({ + "-Ox", "-O1", "-O2", "-Od", + "-MT", "-MD", "-MTd", "-MDd", + "-EHsc", "-EHa", "-EHs", + "-W0", "-W1", "-W2", "-W3", "-W4", "-Wall", "-WX", + "-Z7", "-Zi", "-ZI", + "-c", "-nologo", "-FS", "-FC", "-Gm-", + "-GR", "-GR-", "-GS", "-GS-", + "-Gy", "-Gy-", "-GL", + "-Gd", "-Gr", "-Gz", "-Gv", + "-TC", "-TP", + "-utf-8", "-bigobj", "-MP", + "-showIncludes" + }) _g.known_flags = known_flags end if known_flags:has(flag) then return true end + -- check flags with known prefixes that always accept values (all MSVC versions) if flag:startswith("-D") or flag:startswith("-U") or - flag:startswith("-I") then + flag:startswith("-I") or + flag:startswith("-Fo") or + flag:startswith("-Fe") or + flag:startswith("-Fd") or + flag:startswith("-Fi") or + flag:startswith("-Fp") or + flag:startswith("-FI") or -- force include + flag:startswith("-wd") or -- e.g. /wd4251 + flag:startswith("-we") or -- e.g. /we4062 + flag:startswith("-wo") then return true end end -- cgit v1.3.1 From f623b0100b7270dcd4b923c929e8cb4629677b7b Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 26 Mar 2026 00:47:44 +0800 Subject: improve clang-cl has_flags --- xmake/modules/core/tools/clang_cl/has_flags.lua | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/xmake/modules/core/tools/clang_cl/has_flags.lua b/xmake/modules/core/tools/clang_cl/has_flags.lua index 2f92a8a37..2a670a5ee 100644 --- a/xmake/modules/core/tools/clang_cl/has_flags.lua +++ b/xmake/modules/core/tools/clang_cl/has_flags.lua @@ -21,6 +21,46 @@ -- imports import("core.cache.detectcache") import("core.language.language") +import("core.base.hashset") + +-- attempt to check it from known flags +function _check_from_knownargs(flags, opt) + local flag = flags[1]:gsub("/", "-") + local known_flags = _g.known_flags + if known_flags == nil then + known_flags = hashset.from({ + "-Ox", "-O1", "-O2", "-Od", + "-MT", "-MD", "-MTd", "-MDd", + "-EHsc", "-EHa", "-EHs", + "-W0", "-W1", "-W2", "-W3", "-W4", "-Wall", "-WX", + "-Z7", "-Zi", "-ZI", + "-c", "-nologo", "-FS", "-FC", "-Gm-", + "-GR", "-GR-", "-GS", "-GS-", + "-Gy", "-Gy-", "-GL", + "-Gd", "-Gr", "-Gz", "-Gv", + "-TC", "-TP", + "-utf-8", "-bigobj", "-MP", + "-showIncludes" + }) + _g.known_flags = known_flags + end + if known_flags:has(flag) then + return true + end + -- check flags with known prefixes that always accept values (all versions) + if flag:startswith("-D") or + flag:startswith("-U") or + flag:startswith("-I") or + flag:startswith("-Fo") or + flag:startswith("-Fe") or + flag:startswith("-Fd") or + flag:startswith("-FI") or + flag:startswith("-wd") or + flag:startswith("-we") or + flag:startswith("-wo") then + return true + end +end -- attempt to check it from the argument list function _check_from_arglist(flags, opt) @@ -89,6 +129,12 @@ end -- function main(flags, opt) + -- attempt to check it from known flags + opt = opt or {} + if _check_from_knownargs(flags, opt) then + return true + end + -- attempt to check it from the argument list if _check_from_arglist(flags, opt) then return true -- cgit v1.3.1 From 61c20bbce2d4a50cb21095369246c0d13bd2c2e3 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 26 Mar 2026 00:49:50 +0800 Subject: improve check knownargs --- xmake/modules/core/tools/cl/check_knownargs.lua | 62 +++++++++++++++++++++++++ xmake/modules/core/tools/cl/has_flags.lua | 45 +----------------- xmake/modules/core/tools/clang_cl/has_flags.lua | 43 +---------------- 3 files changed, 66 insertions(+), 84 deletions(-) create mode 100644 xmake/modules/core/tools/cl/check_knownargs.lua diff --git a/xmake/modules/core/tools/cl/check_knownargs.lua b/xmake/modules/core/tools/cl/check_knownargs.lua new file mode 100644 index 000000000..b42fe5e22 --- /dev/null +++ b/xmake/modules/core/tools/cl/check_knownargs.lua @@ -0,0 +1,62 @@ +--!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 check_knownargs.lua +-- + +-- imports +import("core.base.hashset") + +function main(flags) + local flag = flags[1]:gsub("/", "-") + local known_flags = _g.known_flags + if known_flags == nil then + known_flags = hashset.from({ + "-Ox", "-O1", "-O2", "-Od", + "-MT", "-MD", "-MTd", "-MDd", + "-EHsc", "-EHa", "-EHs", + "-W0", "-W1", "-W2", "-W3", "-W4", "-Wall", "-WX", + "-Z7", "-Zi", "-ZI", + "-c", "-nologo", "-FS", "-FC", "-Gm-", + "-GR", "-GR-", "-GS", "-GS-", + "-Gy", "-Gy-", "-GL", + "-Gd", "-Gr", "-Gz", "-Gv", + "-TC", "-TP", + "-utf-8", "-bigobj", "-MP", + "-showIncludes" + }) + _g.known_flags = known_flags + end + if known_flags:has(flag) then + return true + end + -- check flags with known prefixes that always accept values (all MSVC versions) + if flag:startswith("-D") or + flag:startswith("-U") or + flag:startswith("-I") or + flag:startswith("-Fo") or + flag:startswith("-Fe") or + flag:startswith("-Fd") or + flag:startswith("-Fi") or + flag:startswith("-Fp") or + flag:startswith("-FI") or + flag:startswith("-wd") or + flag:startswith("-we") or + flag:startswith("-wo") then + return true + end +end diff --git a/xmake/modules/core/tools/cl/has_flags.lua b/xmake/modules/core/tools/cl/has_flags.lua index 57c5d2d0c..5d24818be 100644 --- a/xmake/modules/core/tools/cl/has_flags.lua +++ b/xmake/modules/core/tools/cl/has_flags.lua @@ -21,48 +21,7 @@ -- imports import("core.language.language") import("core.cache.global_detectcache") -import("core.base.hashset") - --- attempt to check it from known flags -function _check_from_knownargs(flags, opt) - local flag = flags[1]:gsub("/", "-") - local known_flags = _g.known_flags - if known_flags == nil then - known_flags = hashset.from({ - "-Ox", "-O1", "-O2", "-Od", - "-MT", "-MD", "-MTd", "-MDd", - "-EHsc", "-EHa", "-EHs", - "-W0", "-W1", "-W2", "-W3", "-W4", "-Wall", "-WX", - "-Z7", "-Zi", "-ZI", - "-c", "-nologo", "-FS", "-FC", "-Gm-", - "-GR", "-GR-", "-GS", "-GS-", - "-Gy", "-Gy-", "-GL", - "-Gd", "-Gr", "-Gz", "-Gv", - "-TC", "-TP", - "-utf-8", "-bigobj", "-MP", - "-showIncludes" - }) - _g.known_flags = known_flags - end - if known_flags:has(flag) then - return true - end - -- check flags with known prefixes that always accept values (all MSVC versions) - if flag:startswith("-D") or - flag:startswith("-U") or - flag:startswith("-I") or - flag:startswith("-Fo") or - flag:startswith("-Fe") or - flag:startswith("-Fd") or - flag:startswith("-Fi") or - flag:startswith("-Fp") or - flag:startswith("-FI") or -- force include - flag:startswith("-wd") or -- e.g. /wd4251 - flag:startswith("-we") or -- e.g. /we4062 - flag:startswith("-wo") then - return true - end -end +import("core.tools.cl.check_knownargs") -- attempt to check it from the argument list function _check_from_arglist(flags, opt) @@ -137,7 +96,7 @@ function main(flags, opt) -- attempt to check it from the argument list opt = opt or {} if not opt.tryrun then - if _check_from_knownargs(flags, opt) then + if check_knownargs(flags) then return true end if _check_from_arglist(flags, opt) then diff --git a/xmake/modules/core/tools/clang_cl/has_flags.lua b/xmake/modules/core/tools/clang_cl/has_flags.lua index 2a670a5ee..7d6e8c742 100644 --- a/xmake/modules/core/tools/clang_cl/has_flags.lua +++ b/xmake/modules/core/tools/clang_cl/has_flags.lua @@ -21,46 +21,7 @@ -- imports import("core.cache.detectcache") import("core.language.language") -import("core.base.hashset") - --- attempt to check it from known flags -function _check_from_knownargs(flags, opt) - local flag = flags[1]:gsub("/", "-") - local known_flags = _g.known_flags - if known_flags == nil then - known_flags = hashset.from({ - "-Ox", "-O1", "-O2", "-Od", - "-MT", "-MD", "-MTd", "-MDd", - "-EHsc", "-EHa", "-EHs", - "-W0", "-W1", "-W2", "-W3", "-W4", "-Wall", "-WX", - "-Z7", "-Zi", "-ZI", - "-c", "-nologo", "-FS", "-FC", "-Gm-", - "-GR", "-GR-", "-GS", "-GS-", - "-Gy", "-Gy-", "-GL", - "-Gd", "-Gr", "-Gz", "-Gv", - "-TC", "-TP", - "-utf-8", "-bigobj", "-MP", - "-showIncludes" - }) - _g.known_flags = known_flags - end - if known_flags:has(flag) then - return true - end - -- check flags with known prefixes that always accept values (all versions) - if flag:startswith("-D") or - flag:startswith("-U") or - flag:startswith("-I") or - flag:startswith("-Fo") or - flag:startswith("-Fe") or - flag:startswith("-Fd") or - flag:startswith("-FI") or - flag:startswith("-wd") or - flag:startswith("-we") or - flag:startswith("-wo") then - return true - end -end +import("core.tools.cl.check_knownargs") -- attempt to check it from the argument list function _check_from_arglist(flags, opt) @@ -131,7 +92,7 @@ function main(flags, opt) -- attempt to check it from known flags opt = opt or {} - if _check_from_knownargs(flags, opt) then + if check_knownargs(flags) then return true end -- cgit v1.3.1 From c6a54f8a734efdb7df32251e97bb24e9129a28a9 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 26 Mar 2026 00:50:28 +0800 Subject: improve clang-cl has_flags --- xmake/modules/core/tools/cl/has_flags.lua | 6 +----- xmake/modules/core/tools/clang_cl/has_flags.lua | 15 ++------------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/xmake/modules/core/tools/cl/has_flags.lua b/xmake/modules/core/tools/cl/has_flags.lua index 5d24818be..dfef0e1c2 100644 --- a/xmake/modules/core/tools/cl/has_flags.lua +++ b/xmake/modules/core/tools/cl/has_flags.lua @@ -39,11 +39,7 @@ function _check_from_arglist(flags, opt) global_detectcache:set2(key, flagskey, allflags) global_detectcache:save() end - local flag = flags[1]:gsub("/", "-") - if flag:startswith("-D") or flag:startswith("-I") then - return true - end - return allflags[flag] + return allflags[flags[1]:gsub("/", "-")] end -- get extension diff --git a/xmake/modules/core/tools/clang_cl/has_flags.lua b/xmake/modules/core/tools/clang_cl/has_flags.lua index 7d6e8c742..924fb0f3e 100644 --- a/xmake/modules/core/tools/clang_cl/has_flags.lua +++ b/xmake/modules/core/tools/clang_cl/has_flags.lua @@ -25,33 +25,22 @@ import("core.tools.cl.check_knownargs") -- attempt to check it from the argument list function _check_from_arglist(flags, opt) - - -- only one flag? if #flags > 1 then return end - - -- make cache key local key = "core.tools.clang_cl.has_flags" - - -- make allflags key local flagskey = opt.program .. "_" .. (opt.programver or "") - - -- get all allflags from argument list local allflags = detectcache:get2(key, flagskey) if not allflags then - - -- get argument list allflags = {} - local arglist = os.iorunv(opt.program, {"-?"}) + local arglist = os.iorunv(opt.program, {"-?"}, {envs = opt.envs}) if arglist then for arg in arglist:gmatch("(/[%-%a%d]+)%s+") do allflags[arg:gsub("/", "-")] = true end end - - -- save cache detectcache:set2(key, flagskey, allflags) + detectcache:save() end return allflags[flags[1]:gsub("/", "-")] end -- cgit v1.3.1 From c24e4bcd8d8fb444d08fb317f839b830a9a633ef Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 26 Mar 2026 00:52:26 +0800 Subject: improve has_flags again --- xmake/modules/core/tools/cl/check_knownargs.lua | 15 +++------------ xmake/modules/core/tools/gcc/has_flags.lua | 23 +++++++++++++++++++---- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/xmake/modules/core/tools/cl/check_knownargs.lua b/xmake/modules/core/tools/cl/check_knownargs.lua index b42fe5e22..32b9ab03e 100644 --- a/xmake/modules/core/tools/cl/check_knownargs.lua +++ b/xmake/modules/core/tools/cl/check_knownargs.lua @@ -44,18 +44,9 @@ function main(flags) if known_flags:has(flag) then return true end - -- check flags with known prefixes that always accept values (all MSVC versions) - if flag:startswith("-D") or - flag:startswith("-U") or - flag:startswith("-I") or - flag:startswith("-Fo") or - flag:startswith("-Fe") or - flag:startswith("-Fd") or - flag:startswith("-Fi") or - flag:startswith("-Fp") or - flag:startswith("-FI") or - flag:startswith("-wd") or - flag:startswith("-we") or + -- check flags with known prefixes (all MSVC versions) + if flag:startswith("-wd") or -- e.g. /wd4251 + flag:startswith("-we") or -- e.g. /we4062 flag:startswith("-wo") then return true end diff --git a/xmake/modules/core/tools/gcc/has_flags.lua b/xmake/modules/core/tools/gcc/has_flags.lua index 5539fc0f4..d4db0461f 100644 --- a/xmake/modules/core/tools/gcc/has_flags.lua +++ b/xmake/modules/core/tools/gcc/has_flags.lua @@ -50,16 +50,31 @@ function _check_from_knownargs(flags, opt, islinker) local flag = flags[1] local known_flags = _g.known_flags if known_flags == nil then - known_flags = hashset.from({"-O", "-O0", "-O1", "-O2", "-O3", "-Os", "-g"}) + known_flags = hashset.from({ + "-O", "-O0", "-O1", "-O2", "-O3", "-Os", + "-g", "-g0", "-g1", "-g2", "-g3", + "-c", "-S", "-E", "-v", + "-pipe", "-pthread", "-shared", "-static", + "-fPIC", "-fPIE", "-fpic", "-fpie", + "-fno-exceptions", "-fexceptions", + "-fno-rtti", "-frtti", + "-fomit-frame-pointer", "-fno-omit-frame-pointer", + "-ffunction-sections", "-fdata-sections", + "-fno-strict-aliasing", "-fstrict-aliasing", + "-fstack-protector", "-fno-stack-protector", + "-fno-common", + "-MMD", "-MD", "-MP", + "-Wall", "-Wextra", "-Werror", "-pedantic", + "-w" + }) _g.known_flags = known_flags end if known_flags:has(flag) then return true end + -- check flags with known prefixes if not islinker then - if flag:startswith("-D") or - flag:startswith("-U") or - flag:startswith("-I") then + if flag:startswith("-Wno-") then -- -Wno-xxx is always silently accepted return true end end -- cgit v1.3.1 From fe60194acb7098fc0824bea9b653fb5a28114a3d Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 26 Mar 2026 22:47:17 +0800 Subject: fix gcc has_flags --- xmake/modules/core/tools/gcc/has_flags.lua | 6 ------ 1 file changed, 6 deletions(-) diff --git a/xmake/modules/core/tools/gcc/has_flags.lua b/xmake/modules/core/tools/gcc/has_flags.lua index d4db0461f..32662e1a0 100644 --- a/xmake/modules/core/tools/gcc/has_flags.lua +++ b/xmake/modules/core/tools/gcc/has_flags.lua @@ -72,12 +72,6 @@ function _check_from_knownargs(flags, opt, islinker) if known_flags:has(flag) then return true end - -- check flags with known prefixes - if not islinker then - if flag:startswith("-Wno-") then -- -Wno-xxx is always silently accepted - return true - end - end end -- attempt to check it from the argument list -- cgit v1.3.1 From 1a4df91ebd7250f30a57a7563f5edd5c850f3059 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 26 Mar 2026 22:48:11 +0800 Subject: revert gcc has_flags --- xmake/modules/core/tools/gcc/has_flags.lua | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/xmake/modules/core/tools/gcc/has_flags.lua b/xmake/modules/core/tools/gcc/has_flags.lua index 32662e1a0..5539fc0f4 100644 --- a/xmake/modules/core/tools/gcc/has_flags.lua +++ b/xmake/modules/core/tools/gcc/has_flags.lua @@ -50,28 +50,19 @@ function _check_from_knownargs(flags, opt, islinker) local flag = flags[1] local known_flags = _g.known_flags if known_flags == nil then - known_flags = hashset.from({ - "-O", "-O0", "-O1", "-O2", "-O3", "-Os", - "-g", "-g0", "-g1", "-g2", "-g3", - "-c", "-S", "-E", "-v", - "-pipe", "-pthread", "-shared", "-static", - "-fPIC", "-fPIE", "-fpic", "-fpie", - "-fno-exceptions", "-fexceptions", - "-fno-rtti", "-frtti", - "-fomit-frame-pointer", "-fno-omit-frame-pointer", - "-ffunction-sections", "-fdata-sections", - "-fno-strict-aliasing", "-fstrict-aliasing", - "-fstack-protector", "-fno-stack-protector", - "-fno-common", - "-MMD", "-MD", "-MP", - "-Wall", "-Wextra", "-Werror", "-pedantic", - "-w" - }) + known_flags = hashset.from({"-O", "-O0", "-O1", "-O2", "-O3", "-Os", "-g"}) _g.known_flags = known_flags end if known_flags:has(flag) then return true end + if not islinker then + if flag:startswith("-D") or + flag:startswith("-U") or + flag:startswith("-I") then + return true + end + end end -- attempt to check it from the argument list -- cgit v1.3.1