bazelbuild / bazelbuild/rules_apple
Rules Apple Fails on Intel x86 Mac when using CpuInfo (Tensorflow)
- Dominant language
- Starlark
- Stars
- 593
- Forks
- 334
- Avg merge
- 16h 48m
- Merged PRs (30d)
- 9
Description
# Problem
If you are attempting to use any rule from `@build_bazel_rules_apple` on an Intel x86_64 Mac with anything that relies on the [cpuinfo](https://github.com/pytorch/cpuinfo) project (like Tensorflow) you will face the build error described by this [mediapipe issue](https://github.com/google/mediapipe/issues/2324) and this [tensorflow issue](https://github.com/tensorflow/tensorflow/issues/50887).
## Root Cause
The root cause of the problem falls with the complex interplay between `cpuinfo`, `tensorflow`, `bazel`, and the `@build_bazel_rules_apple library`. Bazel has reverted the default cpu value on x86 macOS to "darwin" in this [commit](https://github.com/bazelbuild/bazel/commit/e96b8ca0c244e1a6a747b98b8f01e2526c9db862).
This means that bazel expects Intel x86_64 Macs to report their cpu as `darwin`. `@build_bazel_rules_apple` will report the cpu as `darwin_x86_64`. This cpu does not exist in the `cpuinfo` project as it expects `darwin`. Although there is an unmerged [pull request](https://github.com/pytorch/cpuinfo/pull/139) to fix this behavior, it is unlikely to be resolved soon.
# Solution
In order to fix the problem, I have created a patch so that the library can read the `host_cpu` value which will report as `darwin` on an Intel x86_64 Mac. If this value is present, it will return `darwin` instead of `darwin_x86_64`.
## The Fix
It is necessary to add the `--incompatible_enable_apple_toolchain_resolution` flag to your build command and modify`@build_bazel_rules_apple` with the following patch:
```diff
diff --git a/apple/internal/transition_support.bzl b/apple/internal/transition_support.bzl
index 65f51b89..7da7ade3 100644
--- a/apple/internal/transition_support.bzl
+++ b/apple/internal/transition_support.bzl
@@ -102,6 +102,9 @@ def _cpu_string(*, cpu, platform_type, settings = {}):
return "ios_sim_arm64"
return "ios_x86_64"
if platform_type == "macos":
+ host_cpu = settings["//command_line_option:host_cpu"]
+ if host_cpu == "darwin":
+ return "darwin"
if cpu:
return "darwin_{}".format(cpu)
macos_cpus = settings["//command_line_option:macos_cpus"]
@@ -342,6 +345,7 @@ _apple_rule_common_transition_inputs = [
"//command_line_option:apple_crosstool_top",
]
_apple_rule_base_transition_inputs = _apple_rule_common_transition_inputs + [
+ "//command_line_option:host_cpu",
"//command_line_option:cpu",
"//command_line_option:ios_multi_cpus",
"//command_line_option:macos_cpus",
```
Contributor guide
Research direction
Start in apple/internal/transition_support.bzl and review the _cpu_string function and its transition inputs. Reproduce the Intel x86_64 macOS build with the incompatible Apple toolchain resolution flag and a cpuinfo-dependent target such as TensorFlow. Done means the build reports the compatible darwin CPU value instead of failing with darwin_x86_64.
Written by the indexing model from the issue text.
Assessment
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100