summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-06-20 14:09:27 +0800
committerruki <[email protected]>2017-06-20 14:09:27 +0800
commitace13f7594d5dbddd771cfe0a4004bbcb506b11c (patch)
tree4219dddb0e050b3efc898297baa628992cc128a0
parenta4a011d5147d15c424b6a01633b33b9d8d11a4ff (diff)
add cl.has_flag
-rw-r--r--xmake/modules/detect/tools/cl/has_flag.lua98
-rw-r--r--xmake/modules/detect/tools/gcc/has_flag.lua7
2 files changed, 104 insertions, 1 deletions
diff --git a/xmake/modules/detect/tools/cl/has_flag.lua b/xmake/modules/detect/tools/cl/has_flag.lua
new file mode 100644
index 000000000..1f6e370e3
--- /dev/null
+++ b/xmake/modules/detect/tools/cl/has_flag.lua
@@ -0,0 +1,98 @@
+--!The Make-like Build Utility based on Lua
+--
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements. See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership. The ASF licenses this file
+-- to you 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 - 2017, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file has_flag.lua
+--
+
+-- imports
+import("lib.detect.cache")
+
+-- attempt to check it from the argument list
+function _check_from_arglist(flag, opt)
+
+ -- make cache key
+ local key = "detect.tools.cl.has_flag"
+
+ -- make flags key
+ local flagskey = opt.program .. "_" .. (opt.programver or "")
+
+ -- load cache
+ local cacheinfo = cache.load(key)
+
+ -- get all flags from argument list
+ local flags = cacheinfo[flagskey]
+ if not flags then
+
+ -- get argument list
+ flags = {}
+ local arglist = os.iorunv(opt.program, {"-?"})
+ if arglist then
+ for arg in arglist:gmatch("(/[%-%a%d]+)%s+") do
+ flags[arg:gsub("/", "-")] = true
+ end
+ end
+
+ -- save cache
+ cacheinfo[flagskey] = flags
+ cache.save(key, cacheinfo)
+ end
+
+ -- ok?
+ return flags[flag:gsub("/", "-")]
+end
+
+-- try running to check flag
+function _check_try_running(flag, opt)
+
+ -- make an stub source file
+ local sourcefile = path.join(os.tmpdir(), "detect", "cl_has_flag.c")
+ if not os.isfile(sourcefile) then
+ io.writefile(sourcefile, "int main(int argc, char** argv)\n{return 0;}")
+ end
+
+ -- check it
+ return try { function ()
+ local _, errors = os.iorunv(opt.program, {"-c", "-nologo", flag, "-Fo" .. os.nuldev(), sourcefile})
+ if errors and #errors:trim() > 0 then
+ return false
+ end
+ return true
+ end
+ }
+end
+
+-- has_flag(flag)?
+--
+-- @param opt the argument options, .e.g {toolname = "", program = "", programver = "", toolkind = "[cc|cxx|ld|ar|sh|gc|rc|dc|mm|mxx]"}
+--
+-- @return true or false
+--
+function main(flag, opt)
+
+ -- attempt to check it from the argument list
+ if _check_from_arglist(flag, opt) then
+ return true
+ end
+
+ -- try running to check it
+ return _check_try_running(flag, opt)
+end
+
diff --git a/xmake/modules/detect/tools/gcc/has_flag.lua b/xmake/modules/detect/tools/gcc/has_flag.lua
index af00b84d8..8b1f691ef 100644
--- a/xmake/modules/detect/tools/gcc/has_flag.lua
+++ b/xmake/modules/detect/tools/gcc/has_flag.lua
@@ -41,6 +41,11 @@ end
-- attempt to check it from the argument list of gcc
function _check_from_arglist(flag, opt, islinker)
+ -- only for compiler
+ if islinker then
+ return
+ end
+
-- make cache key
local key = "detect.tools.gcc.has_flag"
@@ -58,7 +63,7 @@ function _check_from_arglist(flag, opt, islinker)
flags = {}
local arglist = os.iorunv(opt.program, {"--help"})
if arglist then
- for arg in arglist:gmatch("%s+(%-[%-%a]+)%s+") do
+ for arg in arglist:gmatch("%s+(%-[%-%a%d]+)%s+") do
flags[arg] = true
end
end