summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-06-20 12:57:35 +0800
committerruki <[email protected]>2017-06-20 12:57:35 +0800
commit0ef4acaa5f9d78c7fd8941ccbe8d3f237e91268c (patch)
treef6f953421cbff144644ab5e6781ad8b187bf12ff
parentbc5021e091a9e7b962243354c88e0fe11dff563f (diff)
improve has_flags for gcc
-rw-r--r--xmake/actions/config/main.lua4
-rw-r--r--xmake/core/sandbox/modules/import/lib/detect/cache.lua (renamed from xmake/core/sandbox/modules/import/lib/detect/clear_cache.lua)51
-rw-r--r--xmake/core/sandbox/modules/import/lib/detect/find_package.lua15
-rw-r--r--xmake/core/sandbox/modules/import/lib/detect/find_program.lua12
-rw-r--r--xmake/core/sandbox/modules/import/lib/detect/find_programver.lua10
-rw-r--r--xmake/modules/detect/flags/has_fsanitize_address.lua34
-rw-r--r--xmake/modules/detect/tools/gcc/has_flag.lua114
-rw-r--r--xmake/modules/lib/detect/has_flags.lua57
8 files changed, 184 insertions, 113 deletions
diff --git a/xmake/actions/config/main.lua b/xmake/actions/config/main.lua
index 931f0ced6..d90b9bc3f 100644
--- a/xmake/actions/config/main.lua
+++ b/xmake/actions/config/main.lua
@@ -29,7 +29,7 @@ import("core.project.global")
import("core.project.project")
import("core.platform.platform")
import("core.project.cache")
-import("lib.detect.clear_cache")
+import("lib.detect.cache", {alias = "detectcache"})
import("scanner")
import("configheader")
@@ -234,7 +234,7 @@ function main()
cache.set("rebuild", true)
-- clear detect cache
- clear_cache()
+ detectcache.clear()
end
-- merge the cached configure
diff --git a/xmake/core/sandbox/modules/import/lib/detect/clear_cache.lua b/xmake/core/sandbox/modules/import/lib/detect/cache.lua
index 81cca369e..c3a890de1 100644
--- a/xmake/core/sandbox/modules/import/lib/detect/clear_cache.lua
+++ b/xmake/core/sandbox/modules/import/lib/detect/cache.lua
@@ -19,11 +19,11 @@
-- Copyright (C) 2015 - 2017, TBOOX Open Source Group.
--
-- @author ruki
--- @file clear_cache.lua
+-- @file cache.lua
--
-- define module
-local sandbox_lib_detect_clear_cache = sandbox_lib_detect_clear_cache or {}
+local sandbox_lib_detect_cache = sandbox_lib_detect_cache or {}
-- load modules
local os = require("base/os")
@@ -36,15 +36,54 @@ local project = require("project/project")
local sandbox = require("sandbox/sandbox")
local raise = require("sandbox/modules/raise")
--- clear detect cache
+-- get detect cache instance
+function sandbox_lib_detect_cache._instance()
+
+ -- get it
+ local detectcache = sandbox_lib_detect_cache._INSTANCE or cache(utils.ifelse(os.isfile(project.file()), "local.detect", "memory.detect"))
+ sandbox_lib_detect_cache._INSTANCE = detectcache
+
+ -- ok?
+ return detectcache
+end
+
+-- load detect cache
--
-- @param name the cache name. .e.g find_program, find_programver, ..
--
-function sandbox_lib_detect_clear_cache.main(name)
+function sandbox_lib_detect_cache.load(name)
-- get detect cache
- local detectcache = cache(utils.ifelse(os.isfile(project.file()), "local.detect", "memory.detect"))
+ local detectcache = sandbox_lib_detect_cache._instance()
+ -- attempt to get result from cache first
+ return detectcache:get(name) or {}
+end
+
+-- save detect cache
+--
+-- @param name the cache name. .e.g find_program, find_programver, ..
+-- @param info the cache info
+--
+function sandbox_lib_detect_cache.save(name, info)
+
+ -- get detect cache
+ local detectcache = sandbox_lib_detect_cache._instance()
+
+ -- save cache info
+ detectcache:set(name, info)
+ detectcache:flush()
+end
+
+-- clear detect cache
+--
+-- @param name the cache name. .e.g find_program, find_programver, ..
+--
+function sandbox_lib_detect_cache.clear(name)
+
+ -- get detect cache
+ local detectcache = sandbox_lib_detect_cache._instance()
+
-- clear cache info
if name then
detectcache:set(name, {})
@@ -55,4 +94,4 @@ function sandbox_lib_detect_clear_cache.main(name)
end
-- return module
-return sandbox_lib_detect_clear_cache
+return sandbox_lib_detect_cache
diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_package.lua b/xmake/core/sandbox/modules/import/lib/detect/find_package.lua
index 460f1b195..626f652cd 100644
--- a/xmake/core/sandbox/modules/import/lib/detect/find_package.lua
+++ b/xmake/core/sandbox/modules/import/lib/detect/find_package.lua
@@ -31,13 +31,11 @@ local path = require("base/path")
local utils = require("base/utils")
local table = require("base/table")
local option = require("base/option")
-local cache = require("project/cache")
local config = require("project/config")
local project = require("project/project")
local raise = require("sandbox/modules/raise")
local import = require("sandbox/modules/import")
-local find_file = import("lib.detect.find_file")
-local find_library = import("lib.detect.find_library")
+local cache = require("sandbox/modules/import/lib/detect/cache")
local pkg_config = import("lib.detect.pkg_config")
-- find package from project package directories
@@ -100,6 +98,9 @@ function sandbox_lib_detect_find_package._find_from_systemdirs(name, opt)
end
end
+ -- import find_library
+ local find_library = import("lib.detect.find_library")
+
-- find library
local result = nil
for _, link in ipairs(table.wrap(links)) do
@@ -175,9 +176,6 @@ end
--
function sandbox_lib_detect_find_package.main(name, opt)
- -- get detect cache
- local detectcache = cache(utils.ifelse(os.isfile(project.file()), "local.detect", "memory.detect"))
-
-- init options
opt = opt or {}
opt.plat = opt.plat or config.get("plat") or os.host()
@@ -187,7 +185,7 @@ function sandbox_lib_detect_find_package.main(name, opt)
local key = "find_package_" .. opt.plat .. "_" .. opt.arch
-- attempt to get result from cache first
- local cacheinfo = detectcache:get(key) or {}
+ local cacheinfo = cache.load(key)
local result = cacheinfo[name]
if result ~= nil then
return utils.ifelse(result, result, nil)
@@ -200,8 +198,7 @@ function sandbox_lib_detect_find_package.main(name, opt)
cacheinfo[name] = utils.ifelse(result, result, false)
-- save cache info
- detectcache:set(key, cacheinfo)
- detectcache:flush()
+ cache.save(key, cacheinfo)
-- trace
if option.get("verbose") then
diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_program.lua b/xmake/core/sandbox/modules/import/lib/detect/find_program.lua
index 2b4b6c4fa..57355780b 100644
--- a/xmake/core/sandbox/modules/import/lib/detect/find_program.lua
+++ b/xmake/core/sandbox/modules/import/lib/detect/find_program.lua
@@ -31,11 +31,11 @@ local path = require("base/path")
local table = require("base/table")
local utils = require("base/utils")
local option = require("base/option")
-local cache = require("project/cache")
local project = require("project/project")
local sandbox = require("sandbox/sandbox")
local raise = require("sandbox/modules/raise")
local vformat = require("sandbox/modules/vformat")
+local cache = require("sandbox/modules/import/lib/detect/cache")
-- check program
function sandbox_lib_detect_find_program._check(program, check)
@@ -141,11 +141,8 @@ end
--
function sandbox_lib_detect_find_program.main(name, pathes, check)
- -- get detect cache
- local detectcache = cache(utils.ifelse(os.isfile(project.file()), "local.detect", "memory.detect"))
-
-- attempt to get result from cache first
- local cacheinfo = detectcache:get("find_program") or {}
+ local cacheinfo = cache:load("find_program")
local result = cacheinfo[name]
if result ~= nil then
return utils.ifelse(result, result, nil)
@@ -153,7 +150,7 @@ function sandbox_lib_detect_find_program.main(name, pathes, check)
-- add default search pathes
if os.host() ~= "windows" then
- pathes = table.join(table.wrap(pathes), "/usr/local/lib", "/usr/lib")
+ pathes = table.join(table.wrap(pathes), "/usr/local/bin", "/usr/bin")
end
-- find executable program
@@ -163,8 +160,7 @@ function sandbox_lib_detect_find_program.main(name, pathes, check)
cacheinfo[name] = utils.ifelse(result, result, false)
-- save cache info
- detectcache:set("find_program", cacheinfo)
- detectcache:flush()
+ cache.save("find_program", cacheinfo)
-- trace
if option.get("verbose") then
diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_programver.lua b/xmake/core/sandbox/modules/import/lib/detect/find_programver.lua
index 7daec26c8..b54b03899 100644
--- a/xmake/core/sandbox/modules/import/lib/detect/find_programver.lua
+++ b/xmake/core/sandbox/modules/import/lib/detect/find_programver.lua
@@ -31,10 +31,10 @@ local path = require("base/path")
local table = require("base/table")
local utils = require("base/utils")
local option = require("base/option")
-local cache = require("project/cache")
local project = require("project/project")
local sandbox = require("sandbox/sandbox")
local raise = require("sandbox/modules/raise")
+local cache = require("sandbox/modules/import/lib/detect/cache")
-- find program version
--
@@ -54,11 +54,8 @@ local raise = require("sandbox/modules/raise")
--
function sandbox_lib_detect_find_programver.main(program, command, parse)
- -- get detect cache
- local detectcache = cache(utils.ifelse(os.isfile(project.file()), "local.detect", "memory.detect"))
-
-- attempt to get result from cache first
- local cacheinfo = detectcache:get("find_programver") or {}
+ local cacheinfo = cache.load("find_programver")
local result = cacheinfo[program]
if result ~= nil then
return utils.ifelse(result, result, nil)
@@ -93,8 +90,7 @@ function sandbox_lib_detect_find_programver.main(program, command, parse)
cacheinfo[program] = utils.ifelse(result, result, false)
-- save cache info
- detectcache:set("find_programver", cacheinfo)
- detectcache:flush()
+ cache.save("find_programver", cacheinfo)
-- ok?
return result
diff --git a/xmake/modules/detect/flags/has_fsanitize_address.lua b/xmake/modules/detect/flags/has_fsanitize_address.lua
deleted file mode 100644
index 36ef766d4..000000000
--- a/xmake/modules/detect/flags/has_fsanitize_address.lua
+++ /dev/null
@@ -1,34 +0,0 @@
---!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_fsanitize_address.lua
---
-
--- has flag: '-fsanitize=address'?
---
--- @param flag the flag
--- @param opt the argument options, .e.g {tool = {name = "", program = "", version = ""}}
---
--- @return true or false
---
-function main(flag, opt)
- -- TODO
-end
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
+
diff --git a/xmake/modules/lib/detect/has_flags.lua b/xmake/modules/lib/detect/has_flags.lua
index 43abc5a1e..bbd972c0d 100644
--- a/xmake/modules/lib/detect/has_flags.lua
+++ b/xmake/modules/lib/detect/has_flags.lua
@@ -25,36 +25,6 @@
-- imports
import("lib.detect.find_tool")
--- get flag name
---
--- .e.g
---
--- "-fsanitize=address": fsanitize_address
--- "--coverage": coverage
--- "-nodefaultlib:\"msvcrt.lib\"": nodefaultlib
--- "/TP": tp
--- "fomit-frame-pointer": fomit_frame_pointer
--- "-Wno-error=deprecated-declarations": wno_error_deprecated_declarations
--- "-pagezero_size 10000" pagezero_size
---
-function _get_flagname(flag)
-
- -- get the first name by ' '
- local name = flag:lower():split('%s+')[1]
-
- -- split ':'
- name = name:split(':')[1]
-
- -- translate '-', '/' and '=' to '_'
- name = name:gsub("[%-/=]", "_")
-
- -- remove prefix '_*' .. xxxx
- name = name:gsub("^_+", "")
-
- -- ok?
- return name
-end
-
-- has this flag?
function _has_flag(name, flag, opt)
@@ -66,12 +36,12 @@ function _has_flag(name, flag, opt)
end
-- init tool
- opt.tool = tool
-
- -- TODO tool.check, semver(tool.version)
+ opt.toolname = tool.name
+ opt.program = tool.program
+ opt.programver = tool.version
-- init cache and key
- local key = tool.program .. "_" .. (tool.version or "") .. "_" .. flag
+ local key = tool.program .. "_" .. (tool.version or "") .. "_" .. (opt.toolkind or "") .. "_" .. flag
local results = _g._RESULTS or {}
-- get result from the cache first
@@ -80,21 +50,13 @@ function _has_flag(name, flag, opt)
return result
end
- -- get flag name
- local flagname = _get_flagname(flag)
- print(flagname)
-
- -- "detect.tools.find_xxx" exists?
- local result = nil
- if os.isfile(path.join(os.programdir(), "modules", "detect", "flags", "has_ " .. flagname .. ".lua")) then
- local hasflag = import("detect.flags.has_" .. flagname)
+ -- detect.tools.xxx.has_flag(flag, opt)?
+ if os.isfile(path.join(os.programdir(), "modules", "detect", "tools", tool.name, "has_flag.lua")) then
+ local hasflag = import("detect.tools." .. tool.name .. ".has_flag")
if hasflag then
result = hasflag(flag, opt)
end
- end
-
- -- attempt to check it directly
- if result == nil then
+ else
result = try { function () os.runv(tool.program, {flag}); return true end }
end
@@ -110,13 +72,14 @@ end
--
-- @param name the tool name
-- @param flags the flags
--- @param opt the argument options, .e.g {program = ""}
+-- @param opt the argument options, .e.g {program = "", toolkind = "[cc|cxx|ld|ar|sh|gc|rc|dc|mm|mxx]"}
--
-- @return true or false
--
-- @code
-- local ok = has_flags("clang", "-g")
-- local ok = has_flags("clang", {"-g", "-O0"}, {program = "xcrun -sdk macosx clang"})
+-- local ok = has_flags("clang", "-g", {toolkind = "cxx"})
-- @endcode
--
function main(name, flags, opt)