From f1697c62acae223dd77cb7d8871a9aa1e4ff492d Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 15 Feb 2024 23:53:13 +0800 Subject: [PATCH 1/2] improve is_cross --- xmake/core/base/private/is_cross.lua | 49 ++++++++++++++++++++++++++++ xmake/core/package/package.lua | 23 ++----------- xmake/core/project/target.lua | 6 ++++ 3 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 xmake/core/base/private/is_cross.lua diff --git a/xmake/core/base/private/is_cross.lua b/xmake/core/base/private/is_cross.lua new file mode 100644 index 00000000000..7c02aa18c41 --- /dev/null +++ b/xmake/core/base/private/is_cross.lua @@ -0,0 +1,49 @@ +--!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 +-- + +-- load modules +local os = require("base/os") + +-- is cross-compilation? +function is_cross(plat, arch) + if os.host() == "windows" then + local host_arch = os.arch() + if plat == "windows" then + -- maybe cross-compilation for arm64 on x86/x64 + if (host_arch == "x86" or host_arch == "x64") and arch == "arm64" then + return true + -- maybe cross-compilation for x86/64 on arm64 + elseif host_arch == "arm64" and arch ~= "arm64" then + return true + end + return false + elseif plat == "mingw" then + return false + end + end + if plat ~= os.host() and plat ~= os.subhost() then + return true + end + if arch ~= os.arch() and arch ~= os.subarch() then + return true + end +end + +return is_cross diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index a3985a5e97d..eef05d80e5e 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -35,6 +35,7 @@ local hashset = require("base/hashset") local scopeinfo = require("base/scopeinfo") local interpreter = require("base/interpreter") local select_script = require("base/private/select_script") +local is_cross = require("base/private/is_cross") local memcache = require("cache/memcache") local toolchain = require("tool/toolchain") local compiler = require("tool/compiler") @@ -684,27 +685,7 @@ end -- is cross-compilation? function _instance:is_cross() - if os.host() == "windows" then - local host_arch = os.arch() - if self:is_plat("windows") then - -- maybe cross-compilation for arm64 on x86/x64 - if (host_arch == "x86" or host_arch == "x64") and self:is_arch("arm64") then - return true - -- maybe cross-compilation for x86/64 on arm64 - elseif host_arch == "arm64" and not self:is_arch("arm64") then - return true - end - return false - elseif self:is_plat("mingw") then - return false - end - end - if not self:is_plat(os.host()) and not self:is_plat(os.subhost()) then - return true - end - if not self:is_arch(os.arch()) and not self:is_arch(os.subarch()) then - return true - end + return is_cross(self:plat(), self:arch()) end -- get the filelock of the whole package directory diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 39d43bec911..e6fb98a7b88 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -34,6 +34,7 @@ local hashset = require("base/hashset") local deprecated = require("base/deprecated") local select_script = require("base/private/select_script") local instance_deps = require("base/private/instance_deps") +local is_cross = require("base/private/is_cross") local memcache = require("cache/memcache") local rule = require("project/rule") local option = require("project/option") @@ -1248,6 +1249,11 @@ function _instance:is_rebuilt() return self:data("rebuilt") end +-- is cross-compilation? +function _instance:is_cross() + return is_cross(self:plat(), self:arch()) +end + -- get the enabled option function _instance:opt(name, opt) return self:opts(opt)[name] From da52cf4aa1bbc67b0812c4d4e902e79143391dee Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 15 Feb 2024 23:53:36 +0800 Subject: [PATCH 2/2] add is_cross api --- xmake/core/sandbox/modules/is_cross.lua | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 xmake/core/sandbox/modules/is_cross.lua diff --git a/xmake/core/sandbox/modules/is_cross.lua b/xmake/core/sandbox/modules/is_cross.lua new file mode 100644 index 00000000000..09b4e0ed403 --- /dev/null +++ b/xmake/core/sandbox/modules/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 module +return require("base/private/is_cross") +