From 6bf4a28541f4a785db59fdd7bc62c3b2ede4017f Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Tue, 9 Apr 2024 19:36:22 +0200 Subject: [PATCH 1/6] Fix issue with fetching packages when doing cross-compilation --- xmake/core/package/package.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index c777d3ec83..292f774c57 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -1948,7 +1948,7 @@ function _instance:fetch(opt) end -- fetch it from the system and external package sources (disabled for cross-compilation) - if not fetchinfo and system ~= false and not self:is_cross() then + if not fetchinfo and system ~= false then fetchinfo = self:_fetch_library({system = true, require_version = require_ver, external = external, force = opt.force}) if fetchinfo then is_system = true From dec2cf143065520452917393ecf28d7479f026c4 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 11 Apr 2024 00:53:07 +0800 Subject: [PATCH 2/6] improve to fetch package for cross-compilation #4935 --- xmake/core/package/package.lua | 5 ++-- .../import/private/core/base/is_cross.lua | 23 ++++++++++++++++ .../modules/package/manager/find_package.lua | 27 +++++-------------- 3 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 xmake/core/sandbox/modules/import/private/core/base/is_cross.lua diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 292f774c57..5d80c5e380 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -1729,8 +1729,9 @@ end function _instance:_fetch_library(opt) opt = opt or {} local fetchinfo + local is_cross = self:is_cross() local on_fetch = self:script("fetch") - if on_fetch then + if on_fetch and not is_cross() then fetchinfo = on_fetch(self, {force = opt.force, system = opt.system, external = opt.external, @@ -1947,7 +1948,7 @@ function _instance:fetch(opt) end end - -- fetch it from the system and external package sources (disabled for cross-compilation) + -- fetch it from the system and external package sources if not fetchinfo and system ~= false then fetchinfo = self:_fetch_library({system = true, require_version = require_ver, external = external, force = opt.force}) if fetchinfo then diff --git a/xmake/core/sandbox/modules/import/private/core/base/is_cross.lua b/xmake/core/sandbox/modules/import/private/core/base/is_cross.lua new file mode 100644 index 0000000000..08ed715c99 --- /dev/null +++ b/xmake/core/sandbox/modules/import/private/core/base/is_cross.lua @@ -0,0 +1,23 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed 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-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file is_cross.lua +-- + +return require("base/private/is_cross") + + diff --git a/xmake/modules/package/manager/find_package.lua b/xmake/modules/package/manager/find_package.lua index 82f029c344..022e92511b 100644 --- a/xmake/modules/package/manager/find_package.lua +++ b/xmake/modules/package/manager/find_package.lua @@ -23,6 +23,7 @@ import("core.base.semver") import("core.base.option") import("core.project.config") import("lib.detect.find_tool") +import("private.core.base.is_cross") -- Is the current version matched? function _is_match_version(current_version, require_version) @@ -53,37 +54,23 @@ function _find_package_with_builtin_rule(package_name, opt) -- find system package if be not disabled if opt.system ~= false then - - -- find it from homebrew - if not is_host("windows") then + local plat = opt.plat + local arch = opt.arch + local find_from_host = not is_cross(plat, arch) + if find_from_host and not is_host("windows") then table.insert(managers, "brew") end - - -- find it from vcpkg (support multi-platforms/architectures) + -- vcpkg/conan support multi-platforms/architectures table.insert(managers, "vcpkg") - - -- find it from conan (support multi-platforms/architectures) table.insert(managers, "conan") - - -- only support the current sub-host platform and sub-architecture, e.g. linux, macosx, or msys (subsystem) - local plat = opt.plat - local arch = opt.arch - if plat == os.subhost() and arch == os.subarch() then - - -- find it from pkg-config + if find_from_host then table.insert(managers, "pkgconfig") - - -- find it from pacman if is_subhost("linux", "msys") and plat ~= "windows" and find_tool("pacman") then table.insert(managers, "pacman") end - - -- find it from portage if is_subhost("linux", "msys") and plat ~= "windows" and find_tool("emerge") then table.insert(managers, "portage") end - - -- find it from system table.insert(managers, "system") end end From 5e6d3bf36aa4eba8a069e69c2718ad95860d9c47 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 11 Apr 2024 00:53:34 +0800 Subject: [PATCH 3/6] fix error --- xmake/core/package/package.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 5d80c5e380..271aaad978 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -1729,9 +1729,8 @@ end function _instance:_fetch_library(opt) opt = opt or {} local fetchinfo - local is_cross = self:is_cross() local on_fetch = self:script("fetch") - if on_fetch and not is_cross() then + if on_fetch and not self:is_cross() then fetchinfo = on_fetch(self, {force = opt.force, system = opt.system, external = opt.external, From d6bcf3fb73418c99def14a8e2d0cb08bb75fecee Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 11 Apr 2024 00:54:14 +0800 Subject: [PATCH 4/6] fix on_fetch --- xmake/core/package/package.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 271aaad978..6dcd0f40ff 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -1730,7 +1730,7 @@ function _instance:_fetch_library(opt) opt = opt or {} local fetchinfo local on_fetch = self:script("fetch") - if on_fetch and not self:is_cross() then + if on_fetch and (not opt.system or (opt.system and not self:is_cross())) then fetchinfo = on_fetch(self, {force = opt.force, system = opt.system, external = opt.external, From 56b62725e6e35fa9265f9f6251dae84dd6933df5 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 11 Apr 2024 00:54:39 +0800 Subject: [PATCH 5/6] improve on_fetch --- xmake/core/package/package.lua | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 6dcd0f40ff..abad53326c 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -1730,11 +1730,14 @@ function _instance:_fetch_library(opt) opt = opt or {} local fetchinfo local on_fetch = self:script("fetch") - if on_fetch and (not opt.system or (opt.system and not self:is_cross())) then - fetchinfo = on_fetch(self, {force = opt.force, - system = opt.system, - external = opt.external, - require_version = opt.require_version}) + if on_fetch then + -- we cannot fetch it from system if it's cross-compilation package + if not opt.system or (opt.system and not self:is_cross()) then + fetchinfo = on_fetch(self, {force = opt.force, + system = opt.system, + external = opt.external, + require_version = opt.require_version}) + end if fetchinfo and opt.require_version and opt.require_version:find(".", 1, true) then local version = fetchinfo.version if not (version and (version == opt.require_version or semver.satisfies(version, opt.require_version))) then From 3871415320ddf8ceeaa3a2851a6b27d8a5fa7e71 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 11 Apr 2024 00:56:19 +0800 Subject: [PATCH 6/6] disable some fetch packages for cross --- xmake/core/base/private/is_cross.lua | 2 ++ xmake/modules/package/manager/apt/find_package.lua | 8 ++------ xmake/modules/package/manager/brew/find_package.lua | 6 +++++- .../modules/package/manager/pacman/find_package.lua | 13 ++++++++----- .../package/manager/portage/find_package.lua | 6 ++++-- .../modules/package/manager/system/find_package.lua | 6 +++++- .../modules/package/manager/zypper/find_package.lua | 3 ++- 7 files changed, 28 insertions(+), 16 deletions(-) diff --git a/xmake/core/base/private/is_cross.lua b/xmake/core/base/private/is_cross.lua index 7c02aa18c4..32fb7aae60 100644 --- a/xmake/core/base/private/is_cross.lua +++ b/xmake/core/base/private/is_cross.lua @@ -23,6 +23,8 @@ local os = require("base/os") -- is cross-compilation? function is_cross(plat, arch) + plat = plat or os.subhost() + arch = arch or os.subarch() if os.host() == "windows" then local host_arch = os.arch() if plat == "windows" then diff --git a/xmake/modules/package/manager/apt/find_package.lua b/xmake/modules/package/manager/apt/find_package.lua index 614b1028f3..74ccc07bbd 100644 --- a/xmake/modules/package/manager/apt/find_package.lua +++ b/xmake/modules/package/manager/apt/find_package.lua @@ -23,6 +23,7 @@ import("core.base.option") import("core.project.config") import("core.project.target") import("lib.detect.find_tool") +import("private.core.base.is_cross") import("package.manager.pkgconfig.find_package", {alias = "find_package_from_pkgconfig"}) -- find package @@ -129,19 +130,14 @@ end -- @param opt the options, e.g. {verbose = true, version = "1.12.0") -- function main(name, opt) - - -- check opt = opt or {} - if not is_host(opt.plat) or os.arch() ~= opt.arch then + if is_cross(opt.plat, opt.arch) then return end - -- find dpkg local dpkg = find_tool("dpkg") if not dpkg then return end - - -- find package return _find_package(dpkg, name, opt) end diff --git a/xmake/modules/package/manager/brew/find_package.lua b/xmake/modules/package/manager/brew/find_package.lua index 953d34f4e9..81a98e1704 100644 --- a/xmake/modules/package/manager/brew/find_package.lua +++ b/xmake/modules/package/manager/brew/find_package.lua @@ -25,6 +25,7 @@ import("lib.detect.find_path") import("lib.detect.pkgconfig") import("core.project.target") import("package.manager.find_package") +import("private.core.base.is_cross") -- get the root directory of the brew packages function _brew_pkg_rootdir() @@ -92,9 +93,12 @@ end -- @param opt the options, e.g. {verbose = true, version = "1.12.x") -- function main(name, opt) + opt = opt or {} + if is_cross(opt.plat, opt.arch) then + return + end -- find the prefix directory of brew - opt = opt or {} local brew_pkg_rootdir = _brew_pkg_rootdir() if not brew_pkg_rootdir then return diff --git a/xmake/modules/package/manager/pacman/find_package.lua b/xmake/modules/package/manager/pacman/find_package.lua index 92e5ccf523..f68b3550bf 100644 --- a/xmake/modules/package/manager/pacman/find_package.lua +++ b/xmake/modules/package/manager/pacman/find_package.lua @@ -22,6 +22,7 @@ import("core.base.option") import("core.project.target") import("lib.detect.find_tool") +import("private.core.base.is_cross") import("package.manager.pkgconfig.find_package", {alias = "find_package_from_pkgconfig"}) -- get result from list of file inside pacman package @@ -51,7 +52,7 @@ function _find_package_from_list(list, name, pacman, opt) if line:find("/include/", 1, true) and (line:endswith(".h") or line:endswith(".hpp")) then if not line:startswith("/usr/include/") then local hpath = line - if is_subhost("msys") and opt.plat == "mingw" then + if is_subhost("msys") and opt.plat == "mingw" then hpath = path.join(pathtomsys, line) local basehpath = path.join(pathtomsys, msystem .. "/include") table.insert(result.includedirs, basehpath) @@ -103,9 +104,12 @@ end -- @param opt the options, e.g. {verbose = true, version = "1.12.x") -- function main(name, opt) + opt = opt or {} + if is_cross(opt.plat, opt.arch) then + return + end -- find pacman - opt = opt or {} local pacman = find_tool("pacman") if not pacman then return @@ -125,13 +129,13 @@ function main(name, opt) name = prefix .. arch .. name end end - + -- get package files list list = name and try { function() return os.iorunv(pacman.program, {"-Q", "-l", name}) end } if not list then return end - + -- parse package files list local linkdirs = {} local pkgconfig_files = {} @@ -179,6 +183,5 @@ function main(name, opt) -- if there is no .pc, we parse the package content to obtain the data we want result = _find_package_from_list(list, name, pacman, opt) end - return result end diff --git a/xmake/modules/package/manager/portage/find_package.lua b/xmake/modules/package/manager/portage/find_package.lua index f08b8e39fe..4f0c7b3490 100644 --- a/xmake/modules/package/manager/portage/find_package.lua +++ b/xmake/modules/package/manager/portage/find_package.lua @@ -22,6 +22,7 @@ import("core.base.option") import("lib.detect.find_file") import("lib.detect.find_tool") +import("private.core.base.is_cross") import("package.manager.pkgconfig.find_package", {alias = "find_package_from_pkgconfig"}) -- find package from the system directories @@ -30,9 +31,10 @@ import("package.manager.pkgconfig.find_package", {alias = "find_package_from_pkg -- @param opt the options, e.g. {verbose = true, version = "1.12.x") -- function main(name, opt) - - -- init options opt = opt or {} + if is_cross(opt.plat, opt.arch) then + return + end -- for msys2/mingw? mingw-w64-[i686|x86_64]-xxx if opt.plat == "mingw" then diff --git a/xmake/modules/package/manager/system/find_package.lua b/xmake/modules/package/manager/system/find_package.lua index dd1fd099aa..9a2a7ea550 100644 --- a/xmake/modules/package/manager/system/find_package.lua +++ b/xmake/modules/package/manager/system/find_package.lua @@ -20,6 +20,7 @@ -- imports import("core.language.language") +import("private.core.base.is_cross") import("lib.detect.check_cxsnippets") -- get package items @@ -57,8 +58,11 @@ end -- function main(name, opt) opt = opt or {} - local configs = opt.configs or {} + if is_cross(opt.plat, opt.arch) then + return + end + local configs = opt.configs or {} local items = _get_package_items() local snippet_configs = {} for _, name in ipairs(items) do diff --git a/xmake/modules/package/manager/zypper/find_package.lua b/xmake/modules/package/manager/zypper/find_package.lua index ea5651004b..490b761612 100644 --- a/xmake/modules/package/manager/zypper/find_package.lua +++ b/xmake/modules/package/manager/zypper/find_package.lua @@ -23,6 +23,7 @@ import("core.base.option") import("core.project.config") import("core.project.target") import("lib.detect.find_tool") +import("private.core.base.is_cross") import("package.manager.pkgconfig.find_package", { alias = "find_package_from_pkgconfig" }) -- find package @@ -137,7 +138,7 @@ end -- function main(name, opt) opt = opt or {} - if not is_host(opt.plat) or os.arch() ~= opt.arch then + if is_cross(opt.plat, opt.arch) then return end local rpm = find_tool("rpm")