From 720cf65b54d50394a5a4110dff364734f6e54f0e Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 4 Dec 2024 22:41:35 +0800 Subject: [PATCH] impl sync_requires_to_deps --- .../sync_requires_to_deps/xmake.lua | 2 +- xmake/core/project/project.lua | 36 +++++++++++++++++++ .../private/action/require/impl/package.lua | 5 +++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/tests/projects/package/compatibility/sync_requires_to_deps/xmake.lua b/tests/projects/package/compatibility/sync_requires_to_deps/xmake.lua index 2d3d0b0294f..439e322e943 100644 --- a/tests/projects/package/compatibility/sync_requires_to_deps/xmake.lua +++ b/tests/projects/package/compatibility/sync_requires_to_deps/xmake.lua @@ -25,7 +25,7 @@ package_end() set_policy("package.sync_requires_to_deps", true) add_requires("test") -add_requires("zlib", {system = false, configs = {shared = true}}) +add_requires("zlib >=1.2.13", {system = false, configs = {shared = true}}) target("test") set_kind("binary") diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 6c896dde582..77793d3e50e 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -1030,6 +1030,42 @@ function project.requireconfs_str() project.requires_str() local requireconfs_str = project._memcache():get("requireconfs_str") local requireconfs_extra = project._memcache():get("requireconfs_extra") + -- synchronize requires configuration to all package dependencies. + -- @see https://github.com/xmake-io/xmake/issues/5745#issuecomment-2513951471 + if project.policy("package.sync_requires_to_deps") then + local requires_str = project._memcache():get("requires_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 + 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 requireconf_str = "**." .. packagename + local requireconf_extra = table.clone(requires_extra[require_str]) + if requireconf_extra then + requireconf_extra.configs = table.clone(requireconf_extra.configs) or {} + end + if packageversion then + requireconf_extra = requireconf_extra or {configs = {}} + requireconf_extra.configs.version = packageversion + end + if requireconf_extra then + requireconf_extra.override = true + table.insert(requireconfs_str, requireconf_str) + requireconfs_extra[requireconf_str] = requireconf_extra + end + end + end + project._memcache():set("requireconfs_str", requireconfs_str) + project._memcache():set("requireconfs_extra", requireconfs_extra) + project._memcache():set("package.sync_requires_to_deps", true) + end + end return requireconfs_str, requireconfs_extra end diff --git a/xmake/modules/private/action/require/impl/package.lua b/xmake/modules/private/action/require/impl/package.lua index e3700af9d46..30c571592e7 100644 --- a/xmake/modules/private/action/require/impl/package.lua +++ b/xmake/modules/private/action/require/impl/package.lua @@ -1211,6 +1211,11 @@ function _get_requirepaths(package) table.insert(requirepaths, requirepath .. "." .. package:name()) end end + -- we also need to resolve requires conflict in toplevel, if `package.sync_requires_to_deps` policy is enabled. + -- @see https://github.com/xmake-io/xmake/issues/5745#issuecomment-2513951471 + if project.policy("package.sync_requires_to_deps") then + table.insert(requirepaths, package:name()) + end else table.insert(requirepaths, package:name()) end