diff options
| author | ruki <[email protected]> | 2023-06-06 17:47:51 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-06-06 17:47:51 +0800 |
| commit | 258c1763be1ee5bb1fe66e339ac63883ab439a7b (patch) | |
| tree | e77af99182d196f59a6dc87d1f23417d19b24216 | |
| parent | 91847c92a4da59fcb1efe843e8c9ee1bc4c987df (diff) | |
| parent | be433ce47536db2b420d9744a19cf02d94ecb9b9 (diff) | |
Merge pull request #3815 from xmake-io/xmake
improve tools.xmake
| -rw-r--r-- | xmake/modules/package/tools/cmake.lua | 26 | ||||
| -rw-r--r-- | xmake/modules/package/tools/xmake.lua | 220 | ||||
| -rw-r--r-- | xmake/modules/private/action/trybuild/cmake.lua | 14 | ||||
| -rw-r--r-- | xmake/modules/private/utils/toolchain.lua | 33 |
4 files changed, 206 insertions, 87 deletions
diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua index ffde2f3d7..b3d669c51 100644 --- a/xmake/modules/package/tools/cmake.lua +++ b/xmake/modules/package/tools/cmake.lua @@ -28,6 +28,7 @@ import("lib.detect.find_file") import("lib.detect.find_tool") import("package.tools.ninja") import("detect.sdks.find_emsdk") +import("private.utils.toolchain", {alias = "toolchain_utils"}) -- get the number of parallel jobs function _get_parallel_njobs(opt) @@ -73,29 +74,10 @@ function _map_linkflags(package, targetkind, sourcekinds, name, values) return linker.map_flags(targetkind, sourcekinds, name, values, {target = package}) end --- is cross compilation? -function _is_cross_compilation(package) - if not package:is_plat(os.subhost()) then - return true - end - if package:is_plat("macosx") and not package:is_arch(os.subarch()) then - return true - end - return false -end - -- is the toolchain compatible with the host? function _is_toolchain_compatible_with_host(package) - local toolchains = package:config("toolchains") - if toolchains then - toolchains = table.wrap(toolchains) - if is_host("linux", "macosx", "bsd") then - for _, name in ipairs(toolchains) do - if name:startswith("clang") or name:startswith("gcc") then - return true - end - end - elseif is_host("windows") and table.contains(toolchains, "msvc") then + for _, name in ipairs(package:config("toolchains")) do + if toolchain_utils.is_compatible_with_host(name) then return true end end @@ -698,7 +680,7 @@ function _get_configs(package, configs, opt) _get_configs_for_mingw(package, configs, opt) elseif package:is_plat("wasm") then _get_configs_for_wasm(package, configs, opt) - elseif _is_cross_compilation(package) then + elseif package:is_cross() then _get_configs_for_cross(package, configs, opt) elseif package:config("toolchains") then -- we still need find system libraries, diff --git a/xmake/modules/package/tools/xmake.lua b/xmake/modules/package/tools/xmake.lua index e4a501b30..e253fee16 100644 --- a/xmake/modules/package/tools/xmake.lua +++ b/xmake/modules/package/tools/xmake.lua @@ -25,6 +25,7 @@ import("core.tool.toolchain") import("core.project.project") import("core.package.repository") import("private.action.require.impl.package", {alias = "require_package"}) +import("private.utils.toolchain", {alias = "toolchain_utils"}) -- get config from toolchains function _get_config_from_toolchains(package, name) @@ -36,6 +37,153 @@ function _get_config_from_toolchains(package, name) end end +-- is the toolchain compatible with the host? +function _is_toolchain_compatible_with_host(package) + for _, name in ipairs(package:config("toolchains")) do + if toolchain_utils.is_compatible_with_host(name) then + return true + end + end +end + +-- get configs for qt +function _get_configs_for_qt(package, configs, opt) + local names = {"qt", "qt_sdkver"} + for _, name in ipairs(names) do + local value = get_config(name) + if value ~= nil then + table.insert(configs, "--" .. name .. "=" .. tostring(value)) + end + end +end + +-- get configs for vcpkg +function _get_configs_for_vcpkg(package, configs, opt) + local names = {"vcpkg"} + for _, name in ipairs(names) do + local value = get_config(name) + if value ~= nil then + table.insert(configs, "--" .. name .. "=" .. tostring(value)) + end + end +end + +-- get configs for windows +function _get_configs_for_windows(package, configs, opt) + local names = {"vs", "vs_toolset", "vs_runtime"} + for _, name in ipairs(names) do + local value = get_config(name) + if value ~= nil then + table.insert(configs, "--" .. name .. "=" .. tostring(value)) + end + end + _get_configs_for_qt(package, configs, opt) + _get_configs_for_vcpkg(package, configs, opt) + + -- we can switch some toolchains, e.g. llvm/clang + if package:config("toolchains") and _is_toolchain_compatible_with_host(package) then + _get_configs_for_host_toolchain(package, configs, opt) + end +end + +-- get configs for appleos +function _get_configs_for_appleos(package, configs, opt) + local appledev = package:config("appledev") + if appledev then + table.insert(configs, "--appledev=" .. appledev) + end + local target_minver = get_config("target_minver") + if target_minver then + table.insert(configs, "--target_minver=" .. target_minver) + end + _get_configs_for_qt(package, configs, opt) + _get_configs_for_vcpkg(package, configs, opt) +end + +-- get configs for android +function _get_configs_for_android(package, configs, opt) + local names = {"ndk", "ndk_sdkver", "ndk_stdcxx", "ndk_cxxstl"} + for _, name in ipairs(names) do + local value = get_config(name) + if value ~= nil then + table.insert(configs, "--" .. name .. "=" .. tostring(value)) + end + end + _get_configs_for_qt(package, configs, opt) + _get_configs_for_vcpkg(package, configs, opt) +end + +-- get configs for mingw +function _get_configs_for_mingw(package, configs, opt) + local names = {"mingw", "sdk", "ld", "sh", "ar", "cc", "cxx", "mm", "mxx"} + for _, name in ipairs(names) do + local value = get_config(name) + if value ~= nil then + table.insert(configs, "--" .. name .. "=" .. tostring(value)) + end + end + _get_configs_for_qt(package, configs, opt) + _get_configs_for_vcpkg(package, configs, opt) +end + +-- get configs for generic, e.g. linux, macosx, bsd host platforms +function _get_configs_for_generic(package, configs, opt) + local names = {"ld", "sh", "ar", "cc", "cxx", "mm", "mxx"} + for _, name in ipairs(names) do + local value = get_config(name) + if value ~= nil then + table.insert(configs, "--" .. name .. "=" .. tostring(value)) + end + end + _get_configs_for_qt(package, configs, opt) + _get_configs_for_vcpkg(package, configs, opt) +end + +-- get configs for host toolchain +function _get_configs_for_host_toolchain(package, configs, opt) + local bindir = _get_config_from_toolchains(package, "bindir") or get_config("bin") + if bindir then + table.insert(configs, "--bin=" .. bindir) + end + local sdkdir = _get_config_from_toolchains(package, "sdkdir") or get_config("sdk") + if sdkdir then + table.insert(configs, "--sdk=" .. sdkdir) + end + local toolchain_name = get_config("toolchain") + if toolchain_name then + table.insert(configs, "--toolchain=" .. toolchain_name) + end + _get_configs_for_qt(package, configs, opt) + _get_configs_for_vcpkg(package, configs, opt) +end + +-- get configs for cross +function _get_configs_for_cross(package, configs, opt) + local cross = _get_config_from_toolchains(package, "cross") or get_config("cross") + if cross then + table.insert(configs, "--cross=" .. cross) + end + local bindir = _get_config_from_toolchains(package, "bindir") or get_config("bin") + if bindir then + table.insert(configs, "--bin=" .. bindir) + end + local sdkdir = _get_config_from_toolchains(package, "sdkdir") or get_config("sdk") + if sdkdir then + table.insert(configs, "--sdk=" .. sdkdir) + end + local toolchain_name = get_config("toolchain") + if toolchain_name then + table.insert(configs, "--toolchain=" .. toolchain_name) + end + local names = {"ld", "sh", "ar", "cc", "cxx", "mm", "mxx"} + for _, name in ipairs(names) do + local value = get_config(name) + if value ~= nil then + table.insert(configs, "--" .. name .. "=" .. tostring(value)) + end + end +end + -- get configs function _get_configs(package, configs, opt) opt = opt or {} @@ -54,61 +202,27 @@ function _get_configs(package, configs, opt) table.insert(configs, "--kind=" .. (package:config("shared") and "shared" or "static")) end if package:is_plat("windows") then - local vs_runtime = package:config("vs_runtime") - if vs_runtime then - table.insert(configs, "--vs_runtime=" .. vs_runtime) - end - elseif package:is_plat("iphoneos", "watchos", "appletvos") then - local appledev = package:config("appledev") - if appledev then - table.insert(configs, "--appledev=" .. appledev) - end - local target_minver = get_config("target_minver") - if target_minver then - table.insert(configs, "--target_minver=" .. target_minver) - end - elseif package:is_plat("cross") then - local cross = _get_config_from_toolchains(package, "cross") or get_config("cross") - if cross then - table.insert(configs, "--cross=" .. cross) - end - local bindir = _get_config_from_toolchains(package, "bindir") or get_config("bin") - if bindir then - table.insert(configs, "--bin=" .. bindir) - end - local sdkdir = _get_config_from_toolchains(package, "sdkdir") or get_config("sdk") - if sdkdir then - table.insert(configs, "--sdk=" .. sdkdir) - end - -- we can only modify toolchain for cross-compilation - -- - -- e.g. xrepo install -p cross --toolchain=muslcc meson, - -- we cannot pass muslcc toolchain to it's deps(zlib, ..), because meson is always host binary and zlib is host library. - local toolchain_name = get_config("toolchain") - if toolchain_name then - table.insert(configs, "--toolchain=" .. toolchain_name) - end - local names = {"ld", "sh", "ar", "cc", "cxx"} - for _, name in ipairs(names) do - local value = get_config(name) - if value ~= nil then - table.insert(configs, "--" .. name .. "=" .. tostring(value)) - end + _get_configs_for_windows(package, configs, opt) + elseif package:is_plat("android") then + _get_configs_for_android(package, configs, opt) + elseif package:is_plat("iphoneos", "watchos", "appletvos") or + -- for cross-compilation on macOS, @see https://github.com/xmake-io/xmake/issues/2804 + (package:is_plat("macosx") and not package:is_arch(os.subarch())) then + _get_configs_for_appleos(package, configs, opt) + elseif package:is_plat("mingw") then + _get_configs_for_mingw(package, configs, opt) + elseif package:is_cross() then + _get_configs_for_cross(package, configs, opt) + elseif package:config("toolchains") then + -- we still need find system libraries, + -- it just pass toolchain environments if the toolchain is compatible with host + if _is_toolchain_compatible_with_host(package) then + _get_configs_for_host_toolchain(package, configs, opt) + else + _get_configs_for_cross(package, configs, opt) end else - local names = {"ndk", "ndk_sdkver", "vs", "vs_toolset", "mingw", "qt", "vcpkg", "ld", "sh", "ar", "cc", "cxx", "mm", "mxx"} - -- We only set the sdk directory when doing cross-compilation, - -- otherwise the host dependency package may also use the cross-compilation toolchain in sdk incorrectly. - -- e.g. build host python -> host zlib -> xmake f --plat=linux --arch=x86_64 --sdk=/tmp/aarch64-linux-musl-cross -> incorrect cross compilation - if package:is_cross() then - table.insert(names, "sdk") - end - for _, name in ipairs(names) do - local value = get_config(name) - if value ~= nil then - table.insert(configs, "--" .. name .. "=" .. tostring(value)) - end - end + _get_configs_for_generic(package, configs, opt) end local policies = get_config("policies") diff --git a/xmake/modules/private/action/trybuild/cmake.lua b/xmake/modules/private/action/trybuild/cmake.lua index 5e4c74799..f4b78fdf7 100644 --- a/xmake/modules/private/action/trybuild/cmake.lua +++ b/xmake/modules/private/action/trybuild/cmake.lua @@ -25,6 +25,7 @@ import("core.tool.toolchain") import("core.platform.platform") import("lib.detect.find_file") import("lib.detect.find_tool") +import("private.utils.toolchain", {alias = "toolchain_utils"}) -- get build directory function _get_buildir() @@ -100,17 +101,6 @@ function _is_cross_compilation() return false end --- is the toolchain compatible with the host? -function _is_toolchain_compatible_with_host(name) - if is_host("linux", "macosx", "bsd") then - if name:startswith("clang") or name:startswith("gcc") then - return true - end - elseif is_host("windows") and name == "msvc" then - return true - end -end - -- get configs for windows function _get_configs_for_windows(configs) table.insert(configs, "-A") @@ -403,7 +393,7 @@ function _get_configs(opt) elseif config.get("toolchain") then -- we still need find system libraries, -- it just pass toolchain environments if the toolchain is compatible with host - if _is_toolchain_compatible_with_host(config.get("toolchain")) then + if toolchain_utils.is_compatible_with_host(config.get("toolchain")) then _get_configs_for_host_toolchain(configs) else _get_configs_for_cross(configs) diff --git a/xmake/modules/private/utils/toolchain.lua b/xmake/modules/private/utils/toolchain.lua new file mode 100644 index 000000000..dd1b6df0c --- /dev/null +++ b/xmake/modules/private/utils/toolchain.lua @@ -0,0 +1,33 @@ +--!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 toolchain.lua +-- + +-- is the compatible with the host? +function is_compatible_with_host(name) + if is_host("linux", "macosx", "bsd") then + if name:startswith("clang") or name:startswith("gcc") or name == "llvm" then + return true + end + elseif is_host("windows") then + if name == "msvc" or name == "llvm" then + return true + end + end +end + |
