From 0d2f106b0d7304e352dc830f5d325fb6619c32be Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 18 Mar 2023 00:33:31 +0800 Subject: add has_flags --- xmake/core/package/package.lua | 54 ++++++++++++++++- xmake/core/project/target.lua | 128 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+), 1 deletion(-) diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index a7171c1d1..910be52b6 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -36,6 +36,7 @@ local scopeinfo = require("base/scopeinfo") local interpreter = require("base/interpreter") local memcache = require("cache/memcache") local toolchain = require("tool/toolchain") +local compiler = require("tool/compiler") local sandbox = require("sandbox/sandbox") local config = require("project/config") local policy = require("project/policy") @@ -59,6 +60,16 @@ function _instance.new(name, info, opt) return instance end +-- get memcache +function _instance:_memcache() + local cache = self._MEMCACHE + if not cache then + cache = memcache.cache("core.package.package." .. tostring(self)) + self._MEMCACHE = cache + end + return cache +end + -- get the package name function _instance:name() return self._NAME @@ -1110,6 +1121,23 @@ function _instance:toolconfig(name) end end +-- get the target compiler +function _instance:compiler(sourcekind) + local compilerinst = self:_memcache():get("compiler") + if not compilerinst then + if not sourcekind then + os.raise("please pass sourcekind to the first argument of package:compiler(), e.g. cc, cxx, as") + end + local instance, errors = compiler.load(sourcekind, self) + if not instance then + os.raise(errors) + end + compilerinst = instance + self:_memcache():set("compiler", compilerinst) + end + return compilerinst +end + -- has the given tool for the current package? -- -- e.g. @@ -2087,7 +2115,7 @@ end -- has the given c types? -- --- @param types the types +-- @param types the types -- @param opt the argument options, e.g. { defines = ""} -- -- @return true or false, errors @@ -2141,6 +2169,30 @@ function _instance:has_cxxincludes(includes, opt) return sandbox_module.import("lib.detect.has_cxxincludes", {anonymous = true})(includes, opt) end +-- has the given c flags? +-- +-- @param flags the flags +-- @param opt the argument options, e.g. { flagskey = "xxx" } +-- +-- @return true or false, errors +-- +function _instance:has_cflags(flags, opt) + local compinst = self:compiler("cc") + return compinst:has_flags(flags, "cflags", opt) +end + +-- has the given c++ flags? +-- +-- @param flags the flags +-- @param opt the argument options, e.g. { flagskey = "xxx" } +-- +-- @return true or false, errors +-- +function _instance:has_cxxflags(flags, opt) + local compinst = self:compiler("cxx") + return compinst:has_flags(flags, "cxxflags", opt) +end + -- check the given c snippets? -- -- @param snippets the snippets diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 79c5f0546..dfcf58a56 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -2252,6 +2252,134 @@ function _instance:has_tool(toolkind, ...) end end +-- has the given c funcs? +-- +-- @param funcs the funcs +-- @param opt the argument options, e.g. { includes = ""} +-- +-- @return true or false, errors +-- +function _instance:has_cfuncs(funcs, opt) + opt = opt or {} + opt.target = self + return sandbox_module.import("lib.detect.has_cfuncs", {anonymous = true})(funcs, opt) +end + +-- has the given c++ funcs? +-- +-- @param funcs the funcs +-- @param opt the argument options, e.g. { includes = ""} +-- +-- @return true or false, errors +-- +function _instance:has_cxxfuncs(funcs, opt) + opt = opt or {} + opt.target = self + return sandbox_module.import("lib.detect.has_cxxfuncs", {anonymous = true})(funcs, opt) +end + +-- has the given c types? +-- +-- @param types the types +-- @param opt the argument options, e.g. { defines = ""} +-- +-- @return true or false, errors +-- +function _instance:has_ctypes(types, opt) + opt = opt or {} + opt.target = self + return sandbox_module.import("lib.detect.has_ctypes", {anonymous = true})(types, opt) +end + +-- has the given c++ types? +-- +-- @param types the types +-- @param opt the argument options, e.g. { defines = ""} +-- +-- @return true or false, errors +-- +function _instance:has_cxxtypes(types, opt) + opt = opt or {} + opt.target = self + return sandbox_module.import("lib.detect.has_cxxtypes", {anonymous = true})(types, opt) +end + +-- has the given c includes? +-- +-- @param includes the includes +-- @param opt the argument options, e.g. { defines = ""} +-- +-- @return true or false, errors +-- +function _instance:has_cincludes(includes, opt) + opt = opt or {} + opt.target = self + return sandbox_module.import("lib.detect.has_cincludes", {anonymous = true})(includes, opt) +end + +-- has the given c++ includes? +-- +-- @param includes the includes +-- @param opt the argument options, e.g. { defines = ""} +-- +-- @return true or false, errors +-- +function _instance:has_cxxincludes(includes, opt) + opt = opt or {} + opt.target = self + return sandbox_module.import("lib.detect.has_cxxincludes", {anonymous = true})(includes, opt) +end + +-- has the given c flags? +-- +-- @param flags the flags +-- @param opt the argument options, e.g. { flagskey = "xxx" } +-- +-- @return true or false, errors +-- +function _instance:has_cflags(flags, opt) + local compinst = self:compiler("cc") + return compinst:has_flags(flags, "cflags", opt) +end + +-- has the given c++ flags? +-- +-- @param flags the flags +-- @param opt the argument options, e.g. { flagskey = "xxx" } +-- +-- @return true or false, errors +-- +function _instance:has_cxxflags(flags, opt) + local compinst = self:compiler("cxx") + return compinst:has_flags(flags, "cxxflags", opt) +end + +-- check the given c snippets? +-- +-- @param snippets the snippets +-- @param opt the argument options, e.g. { includes = ""} +-- +-- @return true or false, errors +-- +function _instance:check_csnippets(snippets, opt) + opt = opt or {} + opt.target = self + return sandbox_module.import("lib.detect.check_csnippets", {anonymous = true})(snippets, opt) +end + +-- check the given c++ snippets? +-- +-- @param snippets the snippets +-- @param opt the argument options, e.g. { includes = ""} +-- +-- @return true or false, errors +-- +function _instance:check_cxxsnippets(snippets, opt) + opt = opt or {} + opt.target = self + return sandbox_module.import("lib.detect.check_cxxsnippets", {anonymous = true})(snippets, opt) +end + -- get project function target._project() return target._PROJECT -- cgit v1.3.1 From 21f980d98c09362721ccd48f42a6fa5508cf4375 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 18 Mar 2023 00:37:10 +0800 Subject: add has_features --- xmake/core/package/package.lua | 13 +++++++++++ xmake/core/project/option.lua | 3 +-- xmake/core/project/target.lua | 13 +++++++++++ .../sandbox/modules/import/core/tool/compiler.lua | 27 +++++----------------- xmake/modules/lib/detect/has_features.lua | 20 ++++++---------- 5 files changed, 40 insertions(+), 36 deletions(-) diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 910be52b6..48cf97b04 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -2193,6 +2193,19 @@ function _instance:has_cxxflags(flags, opt) return compinst:has_flags(flags, "cxxflags", opt) end +-- has the given features? +-- +-- @param features the features, e.g. {"c_static_assert", "cxx_constexpr"} +-- @param opt the argument options, e.g. {cxflags = "", defines = "", includedirs = "", ...} +-- +-- @return true or false, errors +-- +function _instance:has_features(features, opt) + opt = opt or {} + opt.target = self + return sandbox_module.import("core.tool.compiler", {anonymous = true}).has_features(features, opt) +end + -- check the given c snippets? -- -- @param snippets the snippets diff --git a/xmake/core/project/option.lua b/xmake/core/project/option.lua index dcb94aa55..73e3a8f1f 100644 --- a/xmake/core/project/option.lua +++ b/xmake/core/project/option.lua @@ -209,8 +209,7 @@ function _instance:_do_check_features() -- all features are supported? features = table.wrap(features) - local features_supported = self._core_tool_compiler.has_features(features, {target = self}) - if features_supported and #features_supported == #features then + if self._core_tool_compiler.has_features(features, {target = self}) then passed = 1 end diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index dfcf58a56..549f18301 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -2354,6 +2354,19 @@ function _instance:has_cxxflags(flags, opt) return compinst:has_flags(flags, "cxxflags", opt) end +-- has the given features? +-- +-- @param features the features, e.g. {"c_static_assert", "cxx_constexpr"} +-- @param opt the argument options, e.g. {cxflags = "", defines = "", includedirs = "", ...} +-- +-- @return true or false, errors +-- +function _instance:has_features(features, opt) + opt = opt or {} + opt.target = self + return sandbox_module.import("core.tool.compiler", {anonymous = true}).has_features(features, opt) +end + -- check the given c snippets? -- -- @param snippets the snippets diff --git a/xmake/core/sandbox/modules/import/core/tool/compiler.lua b/xmake/core/sandbox/modules/import/core/tool/compiler.lua index 4545a0a14..aae2201ce 100644 --- a/xmake/core/sandbox/modules/import/core/tool/compiler.lua +++ b/xmake/core/sandbox/modules/import/core/tool/compiler.lua @@ -226,7 +226,7 @@ end -- @param opt the argument options (contain all the compiler attributes of target), -- e.g. {target = ..., targetkind = "static", cxflags = "", defines = "", includedirs = "", ...} -- --- @return the supported features or nil +-- @return the true or false -- function sandbox_core_tool_compiler.has_features(features, opt) @@ -236,14 +236,10 @@ function sandbox_core_tool_compiler.has_features(features, opt) return end - -- init options - opt = opt or {} - - -- get the language kinds - local langkinds = language.langkinds() - -- classify features by the source kind + opt = opt or {} local features_by_kind = {} + local langkinds = language.langkinds() for _, feature in ipairs(table.wrap(features)) do -- get language kind @@ -260,29 +256,18 @@ function sandbox_core_tool_compiler.has_features(features, opt) end -- has features for each compiler? - local results = nil for sourcekind, features in pairs(features_by_kind) do - - -- get the compiler instance local instance = sandbox_core_tool_compiler.load(sourcekind, opt) - - -- get flags local flags = instance:compflags(opt) - - -- has features? local ok, results_or_errors = sandbox.load(sandbox_core_tool_compiler._has_features, instance:name(), features, {flags = flags, program = instance:program(), envs = instance:runenvs()}) if not ok then raise(results_or_errors) end - - -- save results - if results_or_errors then - results = table.join(results or {}, results_or_errors) + if not results_or_errors then + return false end end - - -- ok? - return results + return true end -- has the given flags? diff --git a/xmake/modules/lib/detect/has_features.lua b/xmake/modules/lib/detect/has_features.lua index 80dbc2ec7..2b879dd1e 100644 --- a/xmake/modules/lib/detect/has_features.lua +++ b/xmake/modules/lib/detect/has_features.lua @@ -28,7 +28,7 @@ import("lib.detect.features", {alias = "get_features"}) -- @param features the features -- @param opt the argument options, e.g. {verbose = false, flags = {}, program = ""}} -- --- @return the supported features or nil +-- @return true or false -- -- @code -- local features = has_features("clang", "cxx_constexpr") @@ -37,20 +37,14 @@ import("lib.detect.features", {alias = "get_features"}) -- @endcode -- function main(name, features, opt) - - -- init options opt = opt or {} - - -- get all features - local all = get_features(name, opt) or {} - - -- get results - local results = nil + local allfeatures = get_features(name, opt) or {} + local passed = 0 + features = table.wrap(features) for _, feature in ipairs(features) do - if all[feature] then - results = results or {} - table.insert(results, feature) + if allfeatures[feature] then + passed = passed + 1 end end - return results + return passed == #features end -- cgit v1.3.1 From 2330f270a9fad2dfd8ec96227cd4d71c3dcbcfd8 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 18 Mar 2023 00:40:10 +0800 Subject: fix comments --- xmake/core/package/package.lua | 18 +++++++++--------- xmake/core/project/target.lua | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 48cf97b04..13b3d1660 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -2088,7 +2088,7 @@ end -- has the given c funcs? -- -- @param funcs the funcs --- @param opt the argument options, e.g. { includes = ""} +-- @param opt the argument options, e.g. {includes = "xxx.h", configs = {defines = ""}} -- -- @return true or false, errors -- @@ -2102,7 +2102,7 @@ end -- has the given c++ funcs? -- -- @param funcs the funcs --- @param opt the argument options, e.g. {includes = ""} +-- @param opt the argument options, e.g. {includes = "xxx.h", configs = {defines = ""}} -- -- @return true or false, errors -- @@ -2116,7 +2116,7 @@ end -- has the given c types? -- -- @param types the types --- @param opt the argument options, e.g. { defines = ""} +-- @param opt the argument options, e.g. {configs = {defines = ""}} -- -- @return true or false, errors -- @@ -2129,8 +2129,8 @@ end -- has the given c++ types? -- --- @param types the types --- @param opt the argument options, e.g. { defines = ""} +-- @param types the types +-- @param opt the argument options, e.g. {configs = {defines = ""}} -- -- @return true or false, errors -- @@ -2144,7 +2144,7 @@ end -- has the given c includes? -- -- @param includes the includes --- @param opt the argument options, e.g. { defines = ""} +-- @param opt the argument options, e.g. {configs = {defines = ""}} -- -- @return true or false, errors -- @@ -2158,7 +2158,7 @@ end -- has the given c++ includes? -- -- @param includes the includes --- @param opt the argument options, e.g. { defines = ""} +-- @param opt the argument options, e.g. {configs = {defines = ""}} -- -- @return true or false, errors -- @@ -2209,7 +2209,7 @@ end -- check the given c snippets? -- -- @param snippets the snippets --- @param opt the argument options, e.g. { includes = ""} +-- @param opt the argument options, e.g. {includes = "xxx.h", configs = {defines = ""}} -- -- @return true or false, errors -- @@ -2223,7 +2223,7 @@ end -- check the given c++ snippets? -- -- @param snippets the snippets --- @param opt the argument options, e.g. { includes = ""} +-- @param opt the argument options, e.g. {includes = "xxx.h", configs = {defines = ""}} -- -- @return true or false, errors -- diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 549f18301..c1764ff0f 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -2255,7 +2255,7 @@ end -- has the given c funcs? -- -- @param funcs the funcs --- @param opt the argument options, e.g. { includes = ""} +-- @param opt the argument options, e.g. {includes = "xxx.h", configs = {defines = ""}} -- -- @return true or false, errors -- @@ -2268,7 +2268,7 @@ end -- has the given c++ funcs? -- -- @param funcs the funcs --- @param opt the argument options, e.g. { includes = ""} +-- @param opt the argument options, e.g. {includes = "xxx.h", configs = {defines = ""}} -- -- @return true or false, errors -- @@ -2281,7 +2281,7 @@ end -- has the given c types? -- -- @param types the types --- @param opt the argument options, e.g. { defines = ""} +-- @param opt the argument options, e.g. {configs = {defines = ""}} -- -- @return true or false, errors -- @@ -2293,8 +2293,8 @@ end -- has the given c++ types? -- --- @param types the types --- @param opt the argument options, e.g. { defines = ""} +-- @param types the types +-- @param opt the argument options, e.g. {configs = {defines = ""}} -- -- @return true or false, errors -- @@ -2307,7 +2307,7 @@ end -- has the given c includes? -- -- @param includes the includes --- @param opt the argument options, e.g. { defines = ""} +-- @param opt the argument options, e.g. {configs = {defines = ""}} -- -- @return true or false, errors -- @@ -2320,7 +2320,7 @@ end -- has the given c++ includes? -- -- @param includes the includes --- @param opt the argument options, e.g. { defines = ""} +-- @param opt the argument options, e.g. {configs = {defines = ""}} -- -- @return true or false, errors -- @@ -2370,7 +2370,7 @@ end -- check the given c snippets? -- -- @param snippets the snippets --- @param opt the argument options, e.g. { includes = ""} +-- @param opt the argument options, e.g. {includes = "xxx.h", configs = {defines = ""}} -- -- @return true or false, errors -- @@ -2383,7 +2383,7 @@ end -- check the given c++ snippets? -- -- @param snippets the snippets --- @param opt the argument options, e.g. { includes = ""} +-- @param opt the argument options, e.g. {includes = "xxx.h", configs = {defines = ""}} -- -- @return true or false, errors -- -- cgit v1.3.1 From f7bc68f09a8d20fcae1304c9ab70d7f9b7472de5 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 18 Mar 2023 00:41:25 +0800 Subject: fix comments --- xmake/core/package/package.lua | 2 +- xmake/core/project/target.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 13b3d1660..cfc60f696 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -2196,7 +2196,7 @@ end -- has the given features? -- -- @param features the features, e.g. {"c_static_assert", "cxx_constexpr"} --- @param opt the argument options, e.g. {cxflags = "", defines = "", includedirs = "", ...} +-- @param opt the argument options, e.g. {flags = ""} -- -- @return true or false, errors -- diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index c1764ff0f..dae799f9e 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -2357,7 +2357,7 @@ end -- has the given features? -- -- @param features the features, e.g. {"c_static_assert", "cxx_constexpr"} --- @param opt the argument options, e.g. {cxflags = "", defines = "", includedirs = "", ...} +-- @param opt the argument options, e.g. {flags = ""} -- -- @return true or false, errors -- -- cgit v1.3.1