diff options
| author | ruki <[email protected]> | 2024-06-17 10:36:12 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-06-17 10:36:12 +0800 |
| commit | e411d9b19a5f1e5fbcb0daf47f0bb47160d23d1e (patch) | |
| tree | e1c2cf3b67a60e1711db910cf882b965430aa7fb | |
| parent | eb8de7ea5792dce9cd9a76f59086678111a94def (diff) | |
| parent | c0762d71c2e98f22414e111fb89da24fc1031d78 (diff) | |
Merge pull request #5215 from SirLynix/patch-17
cmake: set CMAKE_SYSTEM_PROCESSOR when cross-compiling
| -rw-r--r-- | xmake/modules/package/tools/cmake.lua | 31 | ||||
| -rw-r--r-- | xmake/modules/private/action/trybuild/cmake.lua | 24 |
2 files changed, 48 insertions, 7 deletions
diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua index 03cc3f665..debe44bd2 100644 --- a/xmake/modules/package/tools/cmake.lua +++ b/xmake/modules/package/tools/cmake.lua @@ -288,6 +288,28 @@ function _get_cmake_version() return cmake_version end +function _get_cmake_system_processor(package, opt) + opt = opt or {} + local configs_str = opt._configs_str + if configs_str and configs_str:find("CMAKE_SYSTEM_PROCESSOR", 1, true) then + return + end + + -- on Windows, CMAKE_SYSTEM_PROCESSOR comes from PROCESSOR_ARCHITECTURE + -- on other systems it's the output of uname -m + if package:is_plat("windows") then + local archs = { + x86 = "x86", + x64 = "AMD64", + x86_64 = "AMD64", + arm = "ARM", + arm64 = "ARM64" + } + return archs[package:arch()] or package:arch() + end + return package:arch() +end + -- insert configs from envs function _insert_configs_from_envs(configs, envs, opt) opt = opt or {} @@ -435,8 +457,9 @@ function _get_configs_for_appleos(package, configs, opt) if package:is_arch("x86_64", "i386") then envs.CMAKE_OSX_SYSROOT = "iphonesimulator" end - elseif package:is_plat("macosx") then + elseif package:is_cross() then envs.CMAKE_SYSTEM_NAME = "Darwin" + envs.CMAKE_SYSTEM_PROCESSOR = _get_cmake_system_processor(package, opt) end envs.CMAKE_OSX_ARCHITECTURES = package:arch() envs.CMAKE_FIND_ROOT_PATH_MODE_PACKAGE = "BOTH" @@ -468,6 +491,7 @@ function _get_configs_for_mingw(package, configs, opt) envs.CMAKE_EXE_LINKER_FLAGS = _get_ldflags(package, opt) envs.CMAKE_SHARED_LINKER_FLAGS = _get_shflags(package, opt) envs.CMAKE_SYSTEM_NAME = "Windows" + envs.CMAKE_SYSTEM_PROCESSOR = _get_cmake_system_processor(package, opt) -- avoid find and add system include/library path -- @see https://github.com/xmake-io/xmake/issues/2037 envs.CMAKE_FIND_ROOT_PATH = sdkdir @@ -552,7 +576,7 @@ function _get_configs_for_cross(package, configs, opt) envs.CMAKE_SHARED_LINKER_FLAGS = _get_shflags(package, opt) -- we don't need to set it as cross compilation if we just pass toolchain -- https://github.com/xmake-io/xmake/issues/2170 - if not package:is_plat(os.subhost()) then + if package:is_cross() then local system_name = package:targetos() or "Linux" if system_name == "linux" then system_name = "Linux" @@ -628,7 +652,7 @@ function _get_configs_for_host_toolchain(package, configs, opt) envs.CMAKE_SHARED_LINKER_FLAGS = _get_shflags(package, opt) -- we don't need to set it as cross compilation if we just pass toolchain -- https://github.com/xmake-io/xmake/issues/2170 - if not package:is_plat(os.subhost()) then + if package:is_cross() then envs.CMAKE_SYSTEM_NAME = "Linux" else if package:config("pic") ~= false then @@ -1191,4 +1215,3 @@ function install(package, configs, opt) end os.cd(oldir) end - diff --git a/xmake/modules/private/action/trybuild/cmake.lua b/xmake/modules/private/action/trybuild/cmake.lua index 3a3b00788..94df5a450 100644 --- a/xmake/modules/private/action/trybuild/cmake.lua +++ b/xmake/modules/private/action/trybuild/cmake.lua @@ -101,6 +101,22 @@ function _is_cross_compilation() return false end +function _get_cmake_system_processor() + -- on Windows, CMAKE_SYSTEM_PROCESSOR comes from PROCESSOR_ARCHITECTURE + -- on other systems it's the output of uname -m + if is_plat("windows") then + local archs = { + x86 = "x86", + x64 = "AMD64", + x86_64 = "AMD64", + arm = "ARM", + arm64 = "ARM64" + } + return archs[os.subarch()] or os.subarch() + end + return os.subarch() +end + -- get configs for windows function _get_configs_for_windows(configs, opt) opt = opt or {} @@ -166,8 +182,9 @@ function _get_configs_for_appleos(configs) if is_arch("x86_64", "i386") then envs.CMAKE_OSX_SYSROOT = "iphonesimulator" end - elseif is_plat("macosx") then + elseif _is_cross_compilation() then envs.CMAKE_SYSTEM_NAME = "Darwin" + envs.CMAKE_SYSTEM_PROCESSOR = _get_cmake_system_processor() end envs.CMAKE_FIND_ROOT_PATH_MODE_LIBRARY = "BOTH" envs.CMAKE_FIND_ROOT_PATH_MODE_INCLUDE = "BOTH" @@ -199,6 +216,7 @@ function _get_configs_for_mingw(configs) envs.CMAKE_EXE_LINKER_FLAGS = table.concat(table.wrap(_get_buildenv("ldflags")), ' ') envs.CMAKE_SHARED_LINKER_FLAGS = table.concat(table.wrap(_get_buildenv("shflags")), ' ') envs.CMAKE_SYSTEM_NAME = "Windows" + envs.CMAKE_SYSTEM_PROCESSOR = _get_cmake_system_processor() -- avoid find and add system include/library path envs.CMAKE_FIND_ROOT_PATH = sdkdir envs.CMAKE_SYSROOT = sdkdir @@ -321,8 +339,8 @@ function _get_configs_for_host_toolchain(configs) envs.CMAKE_SHARED_LINKER_FLAGS = table.concat(table.wrap(_get_buildenv("shflags")), ' ') -- we don't need to set it as cross compilation if we just pass toolchain -- https://github.com/xmake-io/xmake/issues/2170 - if not is_plat(os.subhost()) then - envs.CMAKE_SYSTEM_NAME = "Linux" + if _is_cross_compilation() then + envs.CMAKE_SYSTEM_NAME = "Linux" end for k, v in pairs(envs) do table.insert(configs, "-D" .. k .. "=" .. v) |
