diff options
| author | ruki <[email protected]> | 2026-01-07 23:56:51 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2026-01-09 17:59:06 +0800 |
| commit | 3b1f621c6b25958eef72dd98df074fcb6bc81d5e (patch) | |
| tree | 625444e9025b15c55a6a6dfff7c84bdd9f133cb7 | |
| parent | 2c77aafe1a0c1b87c9b423541eaff4a04748e5e5 (diff) | |
improve to select package version for schemes
| -rw-r--r-- | tests/projects/package/schemes/xmake.lua | 12 | ||||
| -rw-r--r-- | xmake/core/base/scopeinfo.lua | 4 | ||||
| -rw-r--r-- | xmake/core/package/scheme.lua | 112 | ||||
| -rw-r--r-- | xmake/modules/private/action/require/impl/package.lua | 54 |
4 files changed, 149 insertions, 33 deletions
diff --git a/tests/projects/package/schemes/xmake.lua b/tests/projects/package/schemes/xmake.lua index 7022523fc..6c6ae5bfe 100644 --- a/tests/projects/package/schemes/xmake.lua +++ b/tests/projects/package/schemes/xmake.lua @@ -17,7 +17,7 @@ package("cmake") scheme:add("urls", "https://github.com/Kitware/CMake/releases/download/v$(version).tar.gz", {version = function (version) return version .. "/cmake-" .. version .. (version:ge("3.20") and "-macos-universal" or "-Darwin-x86_64") end}) - scheme:add("versions", "4.2.1", "0bb18f295e52d7e9309980e361e79e76a1d8da67a1587255cbe3696ea998f597") + scheme:add("versions", "4.2.1", "0bb18f295e52d7e9309980e361e79e76a1d8da67a1587255cbe3696ea998f597") return true elseif is_host("linux") then if os.arch():find("arm64.*") then @@ -27,7 +27,7 @@ package("cmake") scheme:add("urls", "https://github.com/Kitware/CMake/releases/download/v$(version)-aarch64.tar.gz", {version = function (version) return version .. "/cmake-" .. version .. (version:ge("3.20") and "-linux" or "-Linux") end}) - scheme:add("versions", "4.2.1", "3e178207a2c42af4cd4883127f8800b6faf99f3f5187dccc68bfb2cc7808f5f7") + scheme:add("versions", "4.2.1", "3e178207a2c42af4cd4883127f8800b6faf99f3f5187dccc68bfb2cc7808f5f7") else scheme:add("urls", "https://cmake.org/files/v$(version)-x86_64.tar.gz", {version = function (version) return table.concat(table.slice((version):split('%.'), 1, 2), '.') .. "/cmake-" .. version .. (version:ge("3.20") and "-linux" or "-Linux") @@ -35,7 +35,7 @@ package("cmake") scheme:add("urls", "https://github.com/Kitware/CMake/releases/download/v$(version)-x86_64.tar.gz", {version = function (version) return version .. "/cmake-" .. version .. (version:ge("3.20") and "-linux" or "-Linux") end}) - scheme:add("versions", "4.2.1", "c059bff1e97a2b6b5b0c0872263627486345ad0ed083298cb21cff2eda883980") + scheme:add("versions", "4.2.1", "c059bff1e97a2b6b5b0c0872263627486345ad0ed083298cb21cff2eda883980") end return true elseif is_host("windows") then @@ -46,7 +46,7 @@ package("cmake") scheme:add("urls", "https://github.com/Kitware/CMake/releases/download/v$(version).zip", {excludes = {"*/doc/*"}, version = function (version) return version .. "/cmake-" .. version .. (version:ge("3.20") and "-windows-x86_64" or "-win64-x64") end}) - scheme:add("versions", "4.2.1", "dfc2b2afac257555e3b9ce375b12b2883964283a366c17fec96cf4d17e4f1677") + scheme:add("versions", "4.2.1", "dfc2b2afac257555e3b9ce375b12b2883964283a366c17fec96cf4d17e4f1677") elseif os.arch() == "x86" then scheme:add("urls", "https://cmake.org/files/v$(version).zip", {excludes = {"*/doc/*"}, version = function (version) return table.concat(table.slice((version):split('%.'), 1, 2), '.') .. "/cmake-" .. version .. (version:ge("3.20") and "-windows-i386" or "-win32-x86") @@ -54,7 +54,7 @@ package("cmake") scheme:add("urls", "https://github.com/Kitware/CMake/releases/download/v$(version).zip", {excludes = {"*/doc/*"}, version = function (version) return version .. "/cmake-" .. version .. (version:ge("3.20") and "-windows-i386" or "-win32-x86") end}) - scheme:add("versions", "4.2.1", "696129556482da90293f9d64c1fa68dbe06b0ede80331d7da9aaa03aada6aecf") + scheme:add("versions", "4.2.1", "696129556482da90293f9d64c1fa68dbe06b0ede80331d7da9aaa03aada6aecf") elseif os.arch() == "arm64" then scheme:add("urls", "https://cmake.org/files/v$(version).zip", {excludes = {"*/doc/*"}, version = function (version) return table.concat(table.slice((version):split('%.'), 1, 2), '.') .. "/cmake-" .. version .. "-windows-arm64" @@ -62,7 +62,7 @@ package("cmake") scheme:add("urls", "https://github.com/Kitware/CMake/releases/download/v$(version).zip", {excludes = {"*/doc/*"}, version = function (version) return version .. "/cmake-" .. version .. "-windows-arm64" end}) - scheme:add("versions", "4.2.1", "96b097ca3a019cd62839d4805958ad0163dd1adedcfbe578730d57c098aaf667") + scheme:add("versions", "4.2.1", "96b097ca3a019cd62839d4805958ad0163dd1adedcfbe578730d57c098aaf667") end return true end diff --git a/xmake/core/base/scopeinfo.lua b/xmake/core/base/scopeinfo.lua index 2fd362475..f088cf8ae 100644 --- a/xmake/core/base/scopeinfo.lua +++ b/xmake/core/base/scopeinfo.lua @@ -359,11 +359,7 @@ end -- add the api dictionary to the scope info function _instance:_api_add_dictionary(name, dict_or_key, value, extra_config) - - -- get the scope info local scope = self._INFO - - -- check scope[name] = scope[name] or {} if type(dict_or_key) == "table" then local dict = {} diff --git a/xmake/core/package/scheme.lua b/xmake/core/package/scheme.lua index 7c8e61f2d..a7166283a 100644 --- a/xmake/core/package/scheme.lua +++ b/xmake/core/package/scheme.lua @@ -90,6 +90,107 @@ function _instance:extraconf_set(name, item, key, value) return self._INFO:extraconf_set(name, item, key, value) end +-- get urls +function _instance:urls() + local urls = self._URLS + if urls == nil then + urls = table.wrap(self:get("urls")) + if #urls == 1 and urls[1] == "" then + urls = {} + end + end + return urls +end + +-- get versions +function _instance:versions() + if self._VERSIONS == nil then + -- we need to sort the build number in semver list + -- https://github.com/xmake-io/xmake/issues/6953 + local versions = {} + for version, _ in table.orderpairs(self:_versions_list()) do + -- remove the url alias prefix if exists + local pos = version:find(':', 1, true) + if pos then + version = version:sub(pos + 1, -1) + end + table.insert(versions, version) + end + self._VERSIONS = table.unique(versions) + end + return self._VERSIONS +end + +-- get versions list +function _instance:_versions_list() + if self._VERSIONS_LIST == nil then + local versions = table.wrap(self:get("versions")) + local versionfiles = self:get("versionfiles") + if versionfiles then + for _, versionfile in ipairs(table.wrap(versionfiles)) do + if not path.is_absolute(versionfile) then + local subpath = versionfile + versionfile = path.join(self:scriptdir(), subpath) + if not os.isfile(versionfile) and self:package() then + versionfile = path.join(self:package():scriptdir(), subpath) + end + end + if os.isfile(versionfile) then + local list = io.readfile(versionfile) + for _, line in ipairs(list:split("\n")) do + local splitinfo = line:split("%s+") + if #splitinfo == 2 then + local version = splitinfo[1] + local shasum = splitinfo[2] + versions[version] = shasum + end + end + end + end + end + self._VERSIONS_LIST = versions + end + return self._VERSIONS_LIST +end + +-- get version string +function _instance:version_str() + return self._VERSION_STR +end + +-- set version +function _instance:version_set(version, source) + self._VERSION_STR = version + self._VERSION_SOURCE = source + if source == "branch" then + self._BRANCH = version + elseif source == "tag" then + self._TAG = version + elseif source == "commit" then + self._COMMIT = version + end +end + +-- get branch version +function _instance:branch() + return self._BRANCH +end + +-- get tag version +function _instance:tag() + return self._TAG +end + +-- get commit version +function _instance:commit() + return self._COMMIT +end + +-- is git ref? +function _instance:gitref() + return self:branch() or self:tag() or self:commit() +end + -- interpreter function scheme._interpreter() local interp = scheme._INTERPRETER @@ -107,19 +208,12 @@ function scheme.apis() values = { -- scheme.set_xxx "scheme.set_urls", - "scheme.set_policy", -- scheme.add_xxx - "scheme.add_urls", - "scheme.add_patches", - "scheme.add_resources", - "scheme.add_versionfiles", - "scheme.add_versions" + "scheme.add_urls" }, keyvalues = { - -- scheme.set_xxx - "scheme.set_policy" -- scheme.add_xxx - , "scheme.add_patches" + "scheme.add_patches" , "scheme.add_resources" }, paths = { diff --git a/xmake/modules/private/action/require/impl/package.lua b/xmake/modules/private/action/require/impl/package.lua index eae010012..2b55d6834 100644 --- a/xmake/modules/private/action/require/impl/package.lua +++ b/xmake/modules/private/action/require/impl/package.lua @@ -342,20 +342,8 @@ function _add_package_configurations(package) package:add("configs", "shflags", {builtin = true, description = "Set the shared library linker flags."}) end --- select package version -function _select_package_version(package, requireinfo, locked_requireinfo) - - -- get it from the locked requireinfo - if locked_requireinfo then - local version = locked_requireinfo.version - local source = "version" - if locked_requireinfo.branch then - source = "branch" - elseif locked_requireinfo.tag then - source = "tag" - end - return version, source - end +-- select version from package or scheme +function _select_version_from(package, requireinfo) -- has git url? local has_giturl = false @@ -397,6 +385,44 @@ function _select_package_version(package, requireinfo, locked_requireinfo) version = "latest" source = "version" end + return version, source +end + +-- select package version +function _select_package_version(package, requireinfo, locked_requireinfo) + + -- get it from the locked requireinfo + if locked_requireinfo then + local version = locked_requireinfo.version + local source = "version" + if locked_requireinfo.branch then + source = "branch" + elseif locked_requireinfo.tag then + source = "tag" + end + return version, source + end + + -- select version from package or schemes + local version, source + local schemes = package:schemes_orderlist() + if schemes then + for _, scheme in ipairs(schemes) do + local scheme_version, scheme_source = _select_version_from(scheme, requireinfo) + if scheme_version then + scheme:version_set(scheme_version, scheme_source) + if version then + assert(scheme_version == version, "package(%s): the version lists of schemes are mismatch.") + else + version = scheme_version + source = scheme_source + end + end + end + else + version, source = _select_version_from(package, requireinfo) + end + if not version and not package:is_thirdparty() and is_system ~= true then raise("package(%s): version(%s) not found!", package:name(), require_version) end |
