diff options
| author | ruki <[email protected]> | 2017-06-20 12:57:35 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-06-20 12:57:35 +0800 |
| commit | 0ef4acaa5f9d78c7fd8941ccbe8d3f237e91268c (patch) | |
| tree | f6f953421cbff144644ab5e6781ad8b187bf12ff /xmake/modules/detect/tools/gcc | |
| parent | bc5021e091a9e7b962243354c88e0fe11dff563f (diff) | |
improve has_flags for gcc
Diffstat (limited to 'xmake/modules/detect/tools/gcc')
| -rw-r--r-- | xmake/modules/detect/tools/gcc/has_flag.lua | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/xmake/modules/detect/tools/gcc/has_flag.lua b/xmake/modules/detect/tools/gcc/has_flag.lua new file mode 100644 index 000000000..a0624dc4a --- /dev/null +++ b/xmake/modules/detect/tools/gcc/has_flag.lua @@ -0,0 +1,114 @@ +--!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 check_flag.lua +-- + +-- imports +import("lib.detect.cache") + +-- is linker? +function _islinker(flag, opt) + + -- the flag is "-Wl,<arg>" or "-Xlinker <arg>"? + if flag:startswith("-Wl,") or flag: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 + +-- attempt to check it from the argument list of gcc +function _check_from_arglist(flag, opt, islinker) + + -- make cache key + local key = "detect.tools.gcc.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, {"--help"}) + if arglist then + for arg in arglist:gmatch("%s+(%-[%-%a]+)%s+") do + flags[arg] = true + end + end + + -- save cache + cacheinfo[flagskey] = flags + cache.save(key, cacheinfo) + end + + -- ok? + return flags[flag] +end + +-- try running to check flag +function _check_try_running(flag, opt, islinker) + + -- check flag for linker + if islinker then + + -- make an stub source file + local sourcefile = path.join(os.tmpdir(), "detect", "gcc_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 () os.runv(opt.program, {flag, "-o", os.nuldev(), sourcefile}); return true end } + end + + -- check flag for compiler + return try { function () os.runv(opt.program, {flag, "-S", "-o", os.nuldev(), "-xc", os.nuldev()}); 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) + + -- is linker? + local islinker = _islinker(flag, opt) + + -- attempt to check it from the argument list of gcc + if _check_from_arglist(flag, opt, islinker) then + return true + end + + -- try running to check it + return _check_try_running(flag, opt, islinker) +end + |
