diff options
| author | ruki <[email protected]> | 2025-11-17 15:27:57 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-11-17 15:27:57 +0800 |
| commit | 1122dba23f4d74a6be13a23ecd96fa458b9b5813 (patch) | |
| tree | 986d97db7a7b924878e100c457773022049a1ea1 | |
| parent | 934b2f410cf91fd54003576e615faa8db65f4aa3 (diff) | |
| parent | 80b4ebb7033748c8cf1b132b1c0a68c3810229ef (diff) | |
Merge pull request #7031 from xmake-io/require
improve to parse require
| -rw-r--r-- | xmake/core/project/project.lua | 9 | ||||
| -rw-r--r-- | xmake/modules/private/action/require/impl/package.lua | 101 | ||||
| -rw-r--r-- | xmake/modules/private/utils/package.lua | 99 |
3 files changed, 107 insertions, 102 deletions
diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 1bdba46ca..f10cdc894 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -1138,14 +1138,17 @@ function project.requireconfs_str() local requires_extra = project._memcache():get("requires_extra") local sync_requires_to_deps = project._memcache():get("package.sync_requires_to_deps") if requires_str and not sync_requires_to_deps then + local package_utils = project._package_utils + if package_utils == nil then + package_utils = sandbox_module.import("private.utils.package", {anonymous = true}) + project._package_utils = package_utils + end requires_extra = requires_extra and table.wrap(requires_extra) or {} requireconfs_str = requireconfs_str and table.wrap(requireconfs_str) or {} requireconfs_extra = requireconfs_extra and table.wrap(requireconfs_extra) or {} for _, require_str in ipairs(table.wrap(requires_str)) do if not require_str:find("::", 1, true) then - local splitinfo = require_str:split("%s") - local packagename = splitinfo[1] - local packageversion = splitinfo[2] + local packagename, packageversion = package_utils.parse_requirestr(require_str) local requireconf_str = "**." .. packagename local requireconf_extra = table.clone(requires_extra[require_str]) if requireconf_extra then diff --git a/xmake/modules/private/action/require/impl/package.lua b/xmake/modules/private/action/require/impl/package.lua index 1e64d8b47..9fb765f21 100644 --- a/xmake/modules/private/action/require/impl/package.lua +++ b/xmake/modules/private/action/require/impl/package.lua @@ -35,116 +35,19 @@ import("private.action.require.impl.check_api") import("private.action.require.impl.repository") import("private.action.require.impl.search_packages") import("private.action.require.impl.utils.requirekey", {alias = "_get_requirekey"}) +import("private.utils.package", {alias = "package_utils"}) -- get memcache function _memcache() return memcache.cache("require.impl.package") end --- --- parse require string --- --- basic --- - add_requires("zlib") --- --- semver --- - add_requires("tbox >=1.5.1", "zlib >=1.2.11") --- - add_requires("tbox", {version = ">=1.5.1"}) --- --- git branch/tag --- - add_requires("zlib master") --- --- with the given repository --- - add_requires("xmake-repo@tbox >=1.5.1") --- --- with the given configs --- - add_requires("aaa_bbb_ccc >=1.5.1 <1.6.0", {optional = true, alias = "mypkg", debug = true}) --- - add_requires("tbox", {config = {coroutine = true, abc = "xxx"}}) --- --- with namespace and the 3rd package manager --- - add_requires("xmake::xmake-repo@tbox >=1.5.1") --- - add_requires("vcpkg::ffmpeg") --- - add_requires("conan::OpenSSL/1.0.2n@conan/stable") --- - add_requires("conan::openssl/1.1.1g") -- new --- - add_requires("brew::pcre2/libpcre2-8 10.x", {alias = "pcre2"}) --- --- clone as a standalone package with the different configs --- we can install and use these three packages at the same time. --- - add_requires("zlib") --- - add_requires("zlib~debug", {debug = true}) --- - add_requires("zlib~shared", {configs = {shared = true}, alias = "zlib_shared"}) --- --- - add_requires("zlib~label1") --- - add_requires("zlib", {label = "label2"}) --- --- private package, only for installation, do not export any links/includes and environments to target --- - add_requires("zlib", {private = true}) --- --- {system = nil/true/false}: --- nil: get remote or system packages --- true: only get system package --- false: only get remote packages --- --- {build = true}: always build packages, we do not use the precompiled artifacts --- --- simply configs as string: --- add_requires("boost[iostreams,system,thread,key=value] >=1.78.0") --- add_requires("boost[iostreams,thread=n] >=1.78.0") --- add_requires("libplist[shared,debug,codecs=[foo,bar,zoo]]") --- -function _parse_require(require_str) - - -- split package and version info - local splitinfo = require_str:split('%s+') - assert(splitinfo and #splitinfo > 0, "require(\"%s\"): invalid!", require_str) - - -- get package info - local packageinfo = splitinfo[1] - - -- get version - -- - -- e.g. - -- - -- latest - -- >=1.5.1 <1.6.0 - -- master || >1.4 - -- ~1.2.3 - -- ^1.1 - -- - local version = "latest" - if #splitinfo > 1 then - version = table.concat(table.slice(splitinfo, 2), " ") - end - assert(version, "require(\"%s\"): unknown version!", require_str) - - -- require third-party packages? e.g. brew::pcre2/libpcre2-8 - local reponame = nil - local packagename = nil - if require_str:find("::", 1, true) then - packagename = packageinfo - else - - -- get repository name, package name and package url - local pos = packageinfo:lastof('@', true) - if pos then - packagename = packageinfo:sub(pos + 1) - reponame = packageinfo:sub(1, pos - 1) - else - packagename = packageinfo - end - end - - -- check package name - assert(packagename, "require(\"%s\"): the package name not found!", require_str) - return packagename, version, reponame -end - -- load require info function _load_require(require_str, requires_extra, opt) opt = opt or {} -- parse require - local packagename, version, reponame = _parse_require(require_str) + local packagename, version, reponame = package_utils.parse_requirestr(require_str) -- get require extra local require_extra = {} diff --git a/xmake/modules/private/utils/package.lua b/xmake/modules/private/utils/package.lua index 66a755ca8..6df309f5f 100644 --- a/xmake/modules/private/utils/package.lua +++ b/xmake/modules/private/utils/package.lua @@ -54,3 +54,102 @@ function fetchinfo_set_concat(fetchinfo) debug.setmetatable(fetchinfo, {__concat = _concat_packages}) end end + +-- +-- parse require string +-- +-- basic +-- - add_requires("zlib") +-- +-- semver +-- - add_requires("tbox >=1.5.1", "zlib >=1.2.11") +-- - add_requires("tbox", {version = ">=1.5.1"}) +-- +-- git branch/tag +-- - add_requires("zlib master") +-- +-- with the given repository +-- - add_requires("xmake-repo@tbox >=1.5.1") +-- +-- with the given configs +-- - add_requires("aaa_bbb_ccc >=1.5.1 <1.6.0", {optional = true, alias = "mypkg", debug = true}) +-- - add_requires("tbox", {config = {coroutine = true, abc = "xxx"}}) +-- +-- with namespace and the 3rd package manager +-- - add_requires("xmake::xmake-repo@tbox >=1.5.1") +-- - add_requires("vcpkg::ffmpeg") +-- - add_requires("conan::OpenSSL/1.0.2n@conan/stable") +-- - add_requires("conan::openssl/1.1.1g") -- new +-- - add_requires("brew::pcre2/libpcre2-8 10.x", {alias = "pcre2"}) +-- +-- clone as a standalone package with the different configs +-- we can install and use these three packages at the same time. +-- - add_requires("zlib") +-- - add_requires("zlib~debug", {debug = true}) +-- - add_requires("zlib~shared", {configs = {shared = true}, alias = "zlib_shared"}) +-- +-- - add_requires("zlib~label1") +-- - add_requires("zlib", {label = "label2"}) +-- +-- private package, only for installation, do not export any links/includes and environments to target +-- - add_requires("zlib", {private = true}) +-- +-- {system = nil/true/false}: +-- nil: get remote or system packages +-- true: only get system package +-- false: only get remote packages +-- +-- {build = true}: always build packages, we do not use the precompiled artifacts +-- +-- simply configs as string: +-- add_requires("boost[iostreams,system,thread,key=value] >=1.78.0") +-- add_requires("boost[iostreams,thread=n] >=1.78.0") +-- add_requires("libplist[shared,debug,codecs=[foo,bar,zoo]]") +-- +function parse_requirestr(require_str) + + -- split package and version info + local splitinfo = require_str:split('%s+') + assert(splitinfo and #splitinfo > 0, "require(\"%s\"): invalid!", require_str) + + -- get package info + local packageinfo = splitinfo[1] + + -- get version + -- + -- e.g. + -- + -- latest + -- >=1.5.1 <1.6.0 + -- master || >1.4 + -- ~1.2.3 + -- ^1.1 + -- + local version = "latest" + if #splitinfo > 1 then + version = table.concat(table.slice(splitinfo, 2), " ") + end + assert(version, "require(\"%s\"): unknown version!", require_str) + + -- require third-party packages? e.g. brew::pcre2/libpcre2-8 + local reponame = nil + local packagename = nil + if require_str:find("::", 1, true) then + packagename = packageinfo + else + + -- get repository name, package name and package url + local pos = packageinfo:lastof('@', true) + if pos then + packagename = packageinfo:sub(pos + 1) + reponame = packageinfo:sub(1, pos - 1) + else + packagename = packageinfo + end + end + + -- check package name + assert(packagename, "require(\"%s\"): the package name not found!", require_str) + return packagename, version, reponame +end + |
