diff options
| author | ruki <[email protected]> | 2026-03-26 16:54:48 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-03-26 16:54:48 +0800 |
| commit | e7f5e0fc203ebb4adb26365a7e1e4fe9e82e1be7 (patch) | |
| tree | 5a7ae15a6f1308221fd5bbca2d1e18e84efbb344 | |
| parent | 76ecc812e959a271e2ebc476073948e354e643ea (diff) | |
| parent | 1a4df91ebd7250f30a57a7563f5edd5c850f3059 (diff) | |
Merge pull request #7423 from xmake-io/flags
Improve cl/clang-cl has_flags
| -rw-r--r-- | xmake/modules/core/tools/cl/check_knownargs.lua | 53 | ||||
| -rw-r--r-- | xmake/modules/core/tools/cl/has_flags.lua | 28 | ||||
| -rw-r--r-- | xmake/modules/core/tools/clang_cl/has_flags.lua | 22 |
3 files changed, 65 insertions, 38 deletions
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..32b9ab03e --- /dev/null +++ b/xmake/modules/core/tools/cl/check_knownargs.lua @@ -0,0 +1,53 @@ +--!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 (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 +end diff --git a/xmake/modules/core/tools/cl/has_flags.lua b/xmake/modules/core/tools/cl/has_flags.lua index 48884b340..dfef0e1c2 100644 --- a/xmake/modules/core/tools/cl/has_flags.lua +++ b/xmake/modules/core/tools/cl/has_flags.lua @@ -21,25 +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"}) - _g.known_flags = known_flags - end - if known_flags:has(flag) then - return true - end - if flag:startswith("-D") or - flag:startswith("-U") or - flag:startswith("-I") 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) @@ -57,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 @@ -114,7 +92,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 2f92a8a37..924fb0f3e 100644 --- a/xmake/modules/core/tools/clang_cl/has_flags.lua +++ b/xmake/modules/core/tools/clang_cl/has_flags.lua @@ -21,36 +21,26 @@ -- imports import("core.cache.detectcache") import("core.language.language") +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 @@ -89,6 +79,12 @@ end -- function main(flags, opt) + -- attempt to check it from known flags + opt = opt or {} + if check_knownargs(flags) then + return true + end + -- attempt to check it from the argument list if _check_from_arglist(flags, opt) then return true |
