summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-09-19 08:59:27 +0800
committerGitHub <[email protected]>2025-09-19 08:59:27 +0800
commit7186aec3786cd31f4dea25716ddf57954bef5bf6 (patch)
tree21d35f7099aeb6587b29c651ad6397b821768c1b
parent44812793dd6e8c9cae32bf8517d516709e91b3db (diff)
parente8a0b0b9d8ad08e0ab3efe5962c10017d84aaf9f (diff)
Merge pull request #6824 from xmake-io/opti
Improve has_flags
-rw-r--r--tests/benchmarks/build_targets/test.lua8
-rw-r--r--xmake/modules/core/tools/cl/has_flags.lua13
-rw-r--r--xmake/modules/core/tools/gcc/has_flags.lua13
-rw-r--r--xmake/modules/detect/tools/find_cmake.lua1
-rw-r--r--xmake/modules/detect/tools/find_ninja.lua1
-rw-r--r--xmake/modules/lib/detect/find_tool.lua11
-rw-r--r--xmake/modules/lib/detect/has_flags.lua6
-rw-r--r--xmake/toolchains/icc/check.lua6
-rw-r--r--xmake/toolchains/icx/check.lua4
-rw-r--r--xmake/toolchains/ifort/check.lua4
-rw-r--r--xmake/toolchains/ifx/check.lua4
-rw-r--r--xmake/toolchains/msvc/check.lua16
-rw-r--r--xmake/toolchains/rust/xmake.lua2
13 files changed, 56 insertions, 33 deletions
diff --git a/tests/benchmarks/build_targets/test.lua b/tests/benchmarks/build_targets/test.lua
index ec9f1bcf1..0f5124bfa 100644
--- a/tests/benchmarks/build_targets/test.lua
+++ b/tests/benchmarks/build_targets/test.lua
@@ -27,7 +27,7 @@ function test_build(t)
os.runv(cmake.program, {"--build", ".", "-j" .. jobs}, {curdir = "build"})
cmake_default_dt = os.mclock() - cmake_default_dt
print("build targets/30: cmake/default: %d ms", cmake_default_dt)
- t:require((cmake_default_dt > xmake_dt) or (cmake_default_dt + 2000 > xmake_dt))
+ t:require((cmake_default_dt > xmake_dt) or (cmake_default_dt + 3000 > xmake_dt))
local ninja = find_tool("ninja")
if ninja then
@@ -49,7 +49,7 @@ function test_build(t)
os.runv(cmake.program, {"--build", ".", "-j" .. jobs}, {curdir = "build", envs = envs})
cmake_ninja_dt = os.mclock() - cmake_ninja_dt
print("build targets/30: cmake/ninja: %d ms", cmake_ninja_dt)
- t:require((cmake_ninja_dt > xmake_dt) or (cmake_ninja_dt + 2000 > xmake_dt))
+ t:require((cmake_ninja_dt > xmake_dt) or (cmake_ninja_dt + 3000 > xmake_dt))
end
local make = find_tool("make")
@@ -61,7 +61,7 @@ function test_build(t)
os.runv(cmake.program, {"--build", ".", "-j" .. jobs}, {curdir = "build"})
cmake_makefile_dt = os.mclock() - cmake_makefile_dt
print("build targets/30: cmake/makefile: %d ms", cmake_makefile_dt)
- t:require((cmake_makefile_dt > xmake_dt) or (cmake_makefile_dt + 2000 > xmake_dt))
+ t:require((cmake_makefile_dt > xmake_dt) or (cmake_makefile_dt + 3000 > xmake_dt))
end
end
@@ -81,7 +81,7 @@ function test_build(t)
meson_build_dt = os.mclock() - meson_build_dt
local meson_dt = meson_setup_dt + meson_build_dt
print("build targets/30: meson: %d ms", meson_dt)
- t:require((meson_dt > xmake_dt) or (meson_dt + 2000 > xmake_dt))
+ t:require((meson_dt > xmake_dt) or (meson_dt + 3000 > xmake_dt))
end
end
diff --git a/xmake/modules/core/tools/cl/has_flags.lua b/xmake/modules/core/tools/cl/has_flags.lua
index 487338a0b..48884b340 100644
--- a/xmake/modules/core/tools/cl/has_flags.lua
+++ b/xmake/modules/core/tools/cl/has_flags.lua
@@ -21,10 +21,19 @@
-- 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
@@ -105,10 +114,10 @@ function main(flags, opt)
-- attempt to check it from the argument list
opt = opt or {}
if not opt.tryrun then
- if _check_from_arglist(flags, opt) then
+ if _check_from_knownargs(flags, opt) then
return true
end
- if _check_from_knownargs(flags, opt) then
+ if _check_from_arglist(flags, opt) then
return true
end
end
diff --git a/xmake/modules/core/tools/gcc/has_flags.lua b/xmake/modules/core/tools/gcc/has_flags.lua
index 731e5da2f..4d001ef6e 100644
--- a/xmake/modules/core/tools/gcc/has_flags.lua
+++ b/xmake/modules/core/tools/gcc/has_flags.lua
@@ -21,6 +21,7 @@
-- imports
import("core.cache.detectcache")
import("core.language.language")
+import("core.base.hashset")
-- is linker?
function _islinker(flags, opt)
@@ -45,6 +46,14 @@ end
-- attempt to check it from known flags
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"})
+ _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
@@ -119,10 +128,10 @@ function main(flags, opt)
-- attempt to check it from the argument list
if not opt.tryrun then
- if _check_from_arglist(flags, opt, islinker) then
+ if _check_from_knownargs(flags, opt, islinker) then
return true
end
- if _check_from_knownargs(flags, opt, islinker) then
+ if _check_from_arglist(flags, opt, islinker) then
return true
end
end
diff --git a/xmake/modules/detect/tools/find_cmake.lua b/xmake/modules/detect/tools/find_cmake.lua
index cebb5487d..a9267543d 100644
--- a/xmake/modules/detect/tools/find_cmake.lua
+++ b/xmake/modules/detect/tools/find_cmake.lua
@@ -40,6 +40,7 @@ function main(opt)
-- init options
opt = opt or {}
+ opt.norunfile = true
if is_host("windows") then
opt.paths = "$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Kitware\\CMake;InstallDir)\\bin"
end
diff --git a/xmake/modules/detect/tools/find_ninja.lua b/xmake/modules/detect/tools/find_ninja.lua
index 4a03d9e41..06b95f24c 100644
--- a/xmake/modules/detect/tools/find_ninja.lua
+++ b/xmake/modules/detect/tools/find_ninja.lua
@@ -40,6 +40,7 @@ function main(opt)
-- find program
opt = opt or {}
+ opt.norunfile = true
local program = find_program(opt.program or "ninja", opt)
if not program and is_host("windows") then
local msvc = toolchain.load("msvc", {plat = os.host(), arch = os.arch()})
diff --git a/xmake/modules/lib/detect/find_tool.lua b/xmake/modules/lib/detect/find_tool.lua
index ddf96753e..dc30b314a 100644
--- a/xmake/modules/lib/detect/find_tool.lua
+++ b/xmake/modules/lib/detect/find_tool.lua
@@ -33,16 +33,21 @@ function _find_from_modules(name, opt)
if program then
return {name = toolname or name, program = program, version = version}
end
+ return false
end
+ return nil
end
-- find tool
function _find_tool(name, opt)
local toolname = find_toolname(name or opt.program)
if toolname then
- local tool = _find_from_modules(toolname, opt)
- if tool then
- return tool
+ local result = _find_from_modules(toolname, opt)
+ if result then
+ return result
+ elseif result == false then
+ -- find_xxx.lua exists, but program not found
+ return
end
end
diff --git a/xmake/modules/lib/detect/has_flags.lua b/xmake/modules/lib/detect/has_flags.lua
index b122bcebb..36409ef84 100644
--- a/xmake/modules/lib/detect/has_flags.lua
+++ b/xmake/modules/lib/detect/has_flags.lua
@@ -53,8 +53,10 @@ function main(name, flags, opt)
opt.flagskey = opt.flagskey or table.concat(flags, " ")
opt.sysflags = table.wrap(opt.sysflags)
+ -- @note avoid running additional `program --version` processes
+ --opt.version = true
+
-- find tool program and version first
- opt.version = true
local tool = find_tool(name, opt)
if not tool then
return false
@@ -63,7 +65,7 @@ function main(name, flags, opt)
-- init tool
opt.toolname = tool.name
opt.program = tool.program
- opt.programver = tool.version
+ --opt.programver = tool.version
-- get tool platform
local plat = opt.plat or config.get("plat") or os.host()
diff --git a/xmake/toolchains/icc/check.lua b/xmake/toolchains/icc/check.lua
index 002b9e191..a88e309af 100644
--- a/xmake/toolchains/icc/check.lua
+++ b/xmake/toolchains/icc/check.lua
@@ -75,12 +75,12 @@ function _check_vsenv(toolchain)
if pathenv then
paths = path.splitenv(pathenv)
end
- local tool = find_tool("cl.exe", {version = true, force = true, paths = paths, envs = vcvars})
+ local tool = find_tool("cl.exe", {force = true, paths = paths, envs = vcvars})
if tool then
program = tool.program
end
if program then
- return vsver, tool
+ return vsver
end
end
end
@@ -117,7 +117,7 @@ function _check_intel_on_windows(toolchain)
local iclvarsall = iccenv.iclvars
local iclenv = iclvarsall[toolchain:arch()]
if iclenv and iclenv.PATH and iclenv.INCLUDE and iclenv.LIB then
- local tool = find_tool("icl.exe", {force = true, envs = iclenv, version = true})
+ local tool = find_tool("icl.exe", {force = true, envs = iclenv})
if tool then
cprint("checking for Intel C/C++ Compiler (%s) ... ${color.success}${text.success}", toolchain:arch())
toolchain:config_set("varsall", iclvarsall)
diff --git a/xmake/toolchains/icx/check.lua b/xmake/toolchains/icx/check.lua
index c781f5c8a..29fc56a7c 100644
--- a/xmake/toolchains/icx/check.lua
+++ b/xmake/toolchains/icx/check.lua
@@ -75,12 +75,12 @@ function _check_vsenv(toolchain)
if pathenv then
paths = path.splitenv(pathenv)
end
- local tool = find_tool("cl.exe", {version = true, force = true, paths = paths, envs = vcvars})
+ local tool = find_tool("cl.exe", {force = true, paths = paths, envs = vcvars})
if tool then
program = tool.program
end
if program then
- return vsver, tool
+ return vsver
end
end
end
diff --git a/xmake/toolchains/ifort/check.lua b/xmake/toolchains/ifort/check.lua
index ac8f31c6e..e9dd5c133 100644
--- a/xmake/toolchains/ifort/check.lua
+++ b/xmake/toolchains/ifort/check.lua
@@ -75,12 +75,12 @@ function _check_vsenv(toolchain)
if pathenv then
paths = path.splitenv(pathenv)
end
- local tool = find_tool("cl.exe", {version = true, force = true, paths = paths, envs = vcvars})
+ local tool = find_tool("cl.exe", {force = true, paths = paths, envs = vcvars})
if tool then
program = tool.program
end
if program then
- return vsver, tool
+ return vsver
end
end
end
diff --git a/xmake/toolchains/ifx/check.lua b/xmake/toolchains/ifx/check.lua
index 75264e14e..7a10a5865 100644
--- a/xmake/toolchains/ifx/check.lua
+++ b/xmake/toolchains/ifx/check.lua
@@ -75,12 +75,12 @@ function _check_vsenv(toolchain)
if pathenv then
paths = path.splitenv(pathenv)
end
- local tool = find_tool("cl.exe", {version = true, force = true, paths = paths, envs = vcvars})
+ local tool = find_tool("cl.exe", {force = true, paths = paths, envs = vcvars})
if tool then
program = tool.program
end
if program then
- return vsver, tool
+ return vsver,
end
end
end
diff --git a/xmake/toolchains/msvc/check.lua b/xmake/toolchains/msvc/check.lua
index 01a81ad93..37f38b53c 100644
--- a/xmake/toolchains/msvc/check.lua
+++ b/xmake/toolchains/msvc/check.lua
@@ -69,12 +69,12 @@ function _check_vsenv(toolchain)
-- check compiler
local program = nil
- local tool = find_tool("cl.exe", {version = true, force = true, envs = vcvars})
+ local tool = find_tool("cl.exe", {force = true, envs = vcvars})
if tool then
program = tool.program
end
if program then
- return vsver, tool
+ return vsver
end
end
end
@@ -83,16 +83,13 @@ end
-- check the visual studio
function _check_vstudio(toolchain)
- local vs, msvc = _check_vsenv(toolchain)
+ local vs = _check_vsenv(toolchain)
if vs then
if toolchain:is_global() then
config.set("vs", vs, {force = true, readonly = true})
end
toolchain:config_set("vs", vs)
cprint("checking for Microsoft Visual Studio (%s) version ... ${color.success}%s", toolchain:arch(), vs)
- if msvc and msvc.version then
- cprint("checking for Microsoft C/C++ Compiler (%s) version ... ${color.success}%s", toolchain:arch(), msvc.version)
- end
else
cprint("checking for Microsoft Visual Studio (%s) version ... ${color.nothing}${text.nothing}", toolchain:arch())
end
@@ -112,16 +109,15 @@ function _check_vc_build_tools(toolchain, sdkdir)
local vcvars = vcvarsall[toolchain:arch()]
if vcvars and vcvars.PATH and vcvars.INCLUDE and vcvars.LIB then
- -- save vcvars
toolchain:config_set("vcvars", vcvars)
toolchain:config_set("vcarchs", table.orderkeys(vcvarsall))
toolchain:config_set("vs_toolset", vcvars.VCToolsVersion)
toolchain:config_set("vs_sdkver", vcvars.WindowsSDKVersion)
-- check compiler
- local cl = find_tool("cl.exe", {version = true, force = true, envs = vcvars})
- if cl and cl.version then
- cprint("checking for Microsoft C/C++ Compiler (%s) version ... ${color.success}%s", toolchain:arch(), cl.version)
+ local cl = find_tool("cl.exe", {force = true, envs = vcvars})
+ if cl then
+ cprint("checking for Microsoft C/C++ Compiler (%s) ... ${color.success}", toolchain:arch())
end
return vcvars
end
diff --git a/xmake/toolchains/rust/xmake.lua b/xmake/toolchains/rust/xmake.lua
index 76bc64923..3baa998bb 100644
--- a/xmake/toolchains/rust/xmake.lua
+++ b/xmake/toolchains/rust/xmake.lua
@@ -34,7 +34,7 @@ toolchain("rust")
if toolchain:config("appledev") == "simulator" then
opt.apple_sim = true
end
-
+
local target = target_triple(toolchain:plat(), toolchain:arch(), opt)
if target then
toolchain:add("rcflags", "--target=" .. target)