summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-08-20 23:05:20 +0800
committerruki <[email protected]>2022-08-20 23:05:28 +0800
commitca8cef5bfdde703586e90760f80fb8e1095d3ebc (patch)
tree994cdf4dbaff2e8facf81fed707c27e14635a521
parent32fa15ce715e20617c01a1cfb02babd50a92e293 (diff)
improve armclang/has_flags
-rw-r--r--xmake/modules/detect/tools/armclang/has_flags.lua107
-rw-r--r--xmake/modules/detect/tools/gcc/has_flags.lua21
2 files changed, 107 insertions, 21 deletions
diff --git a/xmake/modules/detect/tools/armclang/has_flags.lua b/xmake/modules/detect/tools/armclang/has_flags.lua
index 6464e10b6..0c126a26b 100644
--- a/xmake/modules/detect/tools/armclang/has_flags.lua
+++ b/xmake/modules/detect/tools/armclang/has_flags.lua
@@ -19,5 +19,110 @@
--
-- imports
-inherit("detect.tools.gcc.has_flags")
+import("core.cache.detectcache")
+import("core.language.language")
+
+-- is linker?
+function _islinker(flags, opt)
+
+ -- the flags is "-Wl,<arg>" or "-Xlinker <arg>"?
+ local flags_str = table.concat(flags, " ")
+ if flags_str:startswith("-Wl,") or flags_str:startswith("-Xlinker ") then
+ return true
+ end
+
+ -- the tool kind is ld or sh?
+ local toolkind = opt.toolkind or ""
+ return toolkind == "ld" or toolkind == "sh" or toolkind:endswith("ld") or toolkind:endswith("sh")
+end
+
+-- try running
+function _try_running(program, argv, opt)
+ local errors = nil
+ local has_target
+ for _, v in ipairs(argv) do
+ if v:startswith("-target=") then
+ has_target = true
+ break
+ end
+ end
+ if not has_target then
+ table.insert(argv, 1, "-mcpu=cortex-m3")
+ table.insert(argv, 1, "-target=arm-arm-none-eabi")
+ end
+ return try { function () os.runv(program, argv, opt); return true end, catch { function (errs) errors = (errs or ""):trim() end }}, errors
+end
+
+-- attempt to check it from the argument list
+function _check_from_arglist(flags, opt, islinker)
+ local key = "detect.tools.armclang." .. (islinker and "has_ldflags" or "has_cflags")
+ local flagskey = opt.program .. "_" .. (opt.programver or "")
+ local allflags = detectcache:get2(key, flagskey)
+ if not allflags then
+ allflags = {}
+ local arglist = try {function () return os.iorunv(opt.program, {islinker and "-Wl,--help" or "--help"}, {envs = opt.envs}) end}
+ if arglist then
+ for arg in arglist:gmatch("%s+(%-[%-%a%d]+)%s+") do
+ allflags[arg] = true
+ end
+ end
+ detectcache:set2(key, flagskey, allflags)
+ detectcache:save()
+ end
+ local flag = flags[1]
+ if islinker and flag then
+ if flag:startswith("-Wl,") then
+ flag = flag:match("-Wl,(.-),") or flag:sub(5)
+ end
+ end
+ return allflags[flag]
+end
+
+-- get extension
+function _get_extension(opt)
+ -- @note we need detect extension for ndk/clang++.exe: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]
+ return (opt.program:endswith("++") or opt.flagkind == "cxxflags") and ".cpp" or (table.wrap(language.sourcekinds()[opt.toolkind or "cc"])[1] or ".c")
+end
+
+-- try running to check flags
+function _check_try_running(flags, opt, islinker)
+
+ -- make an stub source file
+ local sourcefile = path.join(os.tmpdir(), "detect", "armclang_has_flags" .. _get_extension(opt))
+ if not os.isfile(sourcefile) then
+ io.writefile(sourcefile, "int main(int argc, char** argv)\n{return 0;}")
+ end
+
+ -- check flags for linker
+ local tmpfile = os.tmpfile()
+ if islinker then
+ return _try_running(opt.program, table.join(flags, "-o", tmpfile, sourcefile), opt)
+ end
+
+ -- check flags for compiler
+ -- @note we cannot use os.nuldev() as the output file, maybe run failed for some flags, e.g. --coverage
+ return _try_running(opt.program, table.join(flags, "-c", "-o", tmpfile, sourcefile), opt)
+end
+
+-- has_flags(flags)?
+--
+-- @param opt the argument options, e.g. {toolname = "", program = "", programver = "", toolkind = "[cc|cxx|ld|ar|sh|gc|mm|mxx]"}
+--
+-- @return true or false
+--
+function main(flags, opt)
+
+ -- is linker?
+ opt = opt or {}
+ local islinker = _islinker(flags, opt)
+
+ -- attempt to check it from the argument list
+ if not opt.tryrun and _check_from_arglist(flags, opt, islinker) then
+ return true
+ end
+
+ -- try running to check it
+ return _check_try_running(flags, opt, islinker)
+end
+
diff --git a/xmake/modules/detect/tools/gcc/has_flags.lua b/xmake/modules/detect/tools/gcc/has_flags.lua
index 8f019a36a..64fa70df8 100644
--- a/xmake/modules/detect/tools/gcc/has_flags.lua
+++ b/xmake/modules/detect/tools/gcc/has_flags.lua
@@ -73,21 +73,6 @@ function _get_extension(opt)
return (opt.program:endswith("++") or opt.flagkind == "cxxflags") and ".cpp" or (table.wrap(language.sourcekinds()[opt.toolkind or "cc"])[1] or ".c")
end
--- has `-S` flag?
-function _has_flag_S(opt)
- local has_flag_S = _g.has_flag_S
- if has_flag_S == nil then
- if opt.program:find("armclang", 1, true) then
- -- it is disallowed in ARM compiler
- has_flag_S = false
- else
- has_flag_S = true
- end
- _g.has_flag_S = has_flag_S
- end
- return has_flag_S
-end
-
-- try running to check flags
function _check_try_running(flags, opt, islinker)
@@ -105,11 +90,7 @@ function _check_try_running(flags, opt, islinker)
-- check flags for compiler
-- @note we cannot use os.nuldev() as the output file, maybe run failed for some flags, e.g. --coverage
- local extraflags = {}
- if _has_flag_S(opt) then
- table.insert(extraflags, "-S")
- end
- return _try_running(opt.program, table.join(flags, extraflags, "-o", tmpfile, sourcefile), opt)
+ return _try_running(opt.program, table.join(flags, "-S", "-o", tmpfile, sourcefile), opt)
end
-- has_flags(flags)?