Skip to content

Commit b06f40f

Browse files
authored
cmake: emit warning when failing to cross-compile (#1438)
cmake requires setting CMAKE_SYSTEM_NAME to detect a cross-compile of any form (including same-os but different-processor). This means that, if an OS is not listed under _TARGET_OS_PARAMS, cross-compilation can never work on that platform; it would have silently done a native compile. This change makes it emit a warning in this case, and adds macos to the list so xcompiles work on macos.
1 parent a3cef63 commit b06f40f

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

foreign_cc/private/cmake_script.bzl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ _TARGET_OS_PARAMS = {
2929
"linux": {
3030
"CMAKE_SYSTEM_NAME": "Linux",
3131
},
32+
"macos": {
33+
"CMAKE_SYSTEM_NAME": "Darwin",
34+
},
3235
}
3336

3437
_TARGET_ARCH_PARAMS = {
@@ -124,8 +127,19 @@ def create_cmake_script(
124127
# buildifier: disable=print
125128
print("target_arch is unknown, please update foreign_cc/private/framework/platform.bzl and foreign_cc/private/cmake_script.bzl; triggered by", current_label)
126129
elif target_os != host_os or target_arch != host_arch:
127-
toolchain_dict.update(_TARGET_OS_PARAMS.get(target_os, {}))
128-
toolchain_dict.update(_TARGET_ARCH_PARAMS.get(target_arch, {}))
130+
# if we don't have a value here, it will end up attempting a native
131+
# compile, even if that's not right, so emit a warning
132+
if target_os in _TARGET_OS_PARAMS:
133+
toolchain_dict.update(_TARGET_OS_PARAMS[target_os])
134+
else:
135+
# buildifier: disable=print
136+
print("target_os", target_os, "is not in _TARGET_OS_PARAMS, please update foreign_cc/private/cmake_script.bzl; triggered by", current_label)
137+
138+
if target_arch in _TARGET_ARCH_PARAMS:
139+
toolchain_dict.update(_TARGET_ARCH_PARAMS[target_arch])
140+
else:
141+
# buildifier: disable=print
142+
print("target_arch", target_arch, "is not in _TARGET_ARCH_PARAMS, please update foreign_cc/private/cmake_script.bzl; triggered by", current_label)
129143

130144
keys_with_empty_values_in_user_cache = [key for key in user_cache if user_cache.get(key) == ""]
131145

0 commit comments

Comments
 (0)