The Problem

If you are attempting to use any rule from @build_bazel_rules_apple on MacOS with a mediapipe project you may face a build error. The error will contain the following message:

Configurable attribute "srcs" doesn't match this configuration (would a default condition help?).
Conditions checked:
 @cpuinfo//:linux_x86_64
 @cpuinfo//:linux_arm
 @cpuinfo//:linux_armhf
 @cpuinfo//:linux_armv7a
 @cpuinfo//:linux_armeabi
 @cpuinfo//:linux_aarch64
 @cpuinfo//:linux_mips64
 @cpuinfo//:linux_riscv64
 @cpuinfo//:linux_s390x
 @cpuinfo//:macos_x86_64
 @cpuinfo//:macos_arm64
 @cpuinfo//:windows_x86_64
 @cpuinfo//:android_armv7
 @cpuinfo//:android_arm64
 @cpuinfo//:android_x86
 @cpuinfo//:android_x86_64
 @cpuinfo//:ios_x86_64
 @cpuinfo//:ios_x86
 @cpuinfo//:ios_armv7
 @cpuinfo//:ios_arm64
 @cpuinfo//:ios_arm64e
 @cpuinfo//:watchos_x86_64
 @cpuinfo//:watchos_x86
 @cpuinfo//:watchos_armv7k
 @cpuinfo//:watchos_arm64_32
 @cpuinfo//:tvos_x86_64
 @cpuinfo//:tvos_arm64
 @cpuinfo//:emscripten_wasm

This github issue summarizes the problem succinctly. This issue has remained open for nearly 2 years without response from the mediapipe team so I will provide a patch based solution here until the problem is resolved properly.

It is not evident which component is to blame for this problem but it is a combination of cpuinfo, tensorflow, mediapipe, bazel, and the @build_bazel_rules_apple library. As these are all very large and complex Google projects (with the exception of cpuinfo), combining them all presents significant challenges.

As is standard with many Google open source projects, if the use case does not fit how Google intends the project to be used, the maintainers provide little if any support. The following fix requires 2 layers of nested patching and must be updated anytime you modify the tensorflow version inside your bazel WORKSPACE.

If you’re using mediapipe v0.8.11 or higher and have the same version of tensorflow as this example, you can copy the final patch I have generated at the bottom of the article.

The Solution - EASY Way

May 2023 Update!

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.

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 to fix this behavior, it is unlikely to be resolved soon.

I have described this further in this issue and submitted a pull request to fix it.

The Fix

Add the --incompatible_enable_apple_toolchain_resolution flag to your build command and modify@build_bazel_rules_apple with the following patch:

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",

The Solution - HARD Way

After identifying the root cause of the problem, the solution described below is no longer necessary but remains included for completeness.

Begin by locating the tensorflow import inside the WORKSPACE file in mediapipe. It will look like this:

# TensorFlow repo should always go after the other external dependencies.
# TF on 2023-03-08.
_TENSORFLOW_GIT_COMMIT = "24f7ee636d62e1f8d8330357f8bbd65956dfb84d"
_TENSORFLOW_SHA256 = "7f8a96dd99215c0cdc77230d3dbce43e60102b64a89203ad04aa09b0a187a4bd"
http_archive(
    name = "org_tensorflow",
    urls = [
      "https://github.com/tensorflow/tensorflow/archive/%s.tar.gz" % _TENSORFLOW_GIT_COMMIT,
    ],
    patches = [
        "@//third_party:org_tensorflow_compatibility_fixes.diff",
        # Diff is generated with a script, don't update it manually.
        "@//third_party:org_tensorflow_custom_ops.diff",
    ],
    patch_args = [
        "-p1",
    ],
    strip_prefix = "tensorflow-%s" % _TENSORFLOW_GIT_COMMIT,
    sha256 = _TENSORFLOW_SHA256,
)

Now clone the tensorflow github repository to a directory next to your project and checkout the exact commit specified.

In your bazel build command you can point to your locally cloned repository with the --override_repository flag. In this example it would look like --override_repository=org_tensorflow=<PATH_TO_REPO>.

You are now ready for layer one of the patch. Now let’s dive into layer two. Open the tensorflow repository in your favorite code editor. You now need to locate all instances of cpuinfo inside tensorflow. In this case cpuinfo is directly declared and it is also used inside ruy and xnnpack. These two are contained as submodules, so you will need to run:

git submodule init
git submodule update

Within the tensorflow repository navigate to tensorflow/workspace2.bzl. Within this file find the following entries for cpuinfo and xnnpack and add the patch_file line as shown:

    tf_http_archive(
        name = "cpuinfo",
        strip_prefix = "cpuinfo-5e63739504f0f8e18e941bd63b2d6d42536c7d90",
        sha256 = "18eca9bc8d9c4ce5496d0d2be9f456d55cbbb5f0639a551ce9c8bac2e84d85fe",
        patch_file = ["//third_party:cpuinfo_macos_compat.patch"],
        urls = tf_mirror_urls("https://github.com/pytorch/cpuinfo/archive/5e63739504f0f8e18e941bd63b2d6d42536c7d90.tar.gz"),
    )
    tf_http_archive(
        name = "XNNPACK",
        sha256 = "21cf16fb50c32682b8548cde83933bbd90b884983c50b9db7550002a72a75ad5",
        strip_prefix = "XNNPACK-8e3d3359f9bec608e09fac1f7054a2a14b1bd73c",
        patch_file = ["//third_party:xnnpack_macos_compat.patch"],
        urls = tf_mirror_urls("https://github.com/google/XNNPACK/archive/8e3d3359f9bec608e09fac1f7054a2a14b1bd73c.zip"),
    )

Locate ruy in third_party/ruy/workspace.bzl and add the patch_file line as shown:

        name = "ruy",
        # LINT.IfChange
        sha256 = "dd6bf40322303cf8982f340e4139397c8fa350ff691d5254599cb21e0138fc65",
        strip_prefix = "ruy-841ea4172ba904fe3536789497f9565f2ef64129",
        patch_file = ["//third_party:ruy_macos_compat.patch"],
        urls = tf_mirror_urls("https://github.com/google/ruy/archive/841ea4172ba904fe3536789497f9565f2ef64129.zip"),
        # LINT.ThenChange(//tensorflow/lite/tools/cmake/modules/ruy.cmake)
        build_file = "//third_party/ruy:BUILD",

Now the three libraries will use the patches that we will generate shortly for cpuinfo. You now need to clone each of the three git repositories above seperatly and checkout the exact commit specified. Within each you will need to make a variant of the following patch (for cpuinfo):

diff --git a/BUILD.bazel b/BUILD.bazel
index 4ac8f10..6551fec 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -112,6 +112,7 @@ cc_library(
         ":linux_mips64": COMMON_SRCS + LINUX_SRCS,
         ":linux_riscv64": COMMON_SRCS + LINUX_SRCS,
         ":linux_s390x": COMMON_SRCS + LINUX_SRCS,
+        ":macos": COMMON_SRCS + X86_SRCS + MACH_SRCS + MACH_X86_SRCS,
         ":macos_x86_64": COMMON_SRCS + X86_SRCS + MACH_SRCS + MACH_X86_SRCS,
         ":macos_arm64": COMMON_SRCS + MACH_SRCS + MACH_ARM_SRCS,
         ":windows_x86_64": COMMON_SRCS + X86_SRCS + WINDOWS_X86_SRCS,
@@ -242,13 +243,21 @@ config_setting(
 )
 
 config_setting(
-    name = "macos_x86_64",
+    name = "macos",
     values = {
         "apple_platform_type": "macos",
         "cpu": "darwin",
     },
 )
 
+config_setting(
+    name = "macos_x86_64",
+    values = {
+        "apple_platform_type": "macos",
+        "cpu": "darwin_x86_64",
+    },
+)
+
 config_setting(
     name = "windows_x86_64",
     values = {"cpu": "x64_windows"},

Once you have made the changes you need to generate the patch file, commit them locally. This guide provides some excellent tips on patching bazel dependencies. After you have committed you need to generate the patch with the command git format-patch --keep-subject --no-stat --zero-commit <BASE_COMMIT>. This will create a patch file in the current directory your working in. Examine the file and notice it will contain some content in the header and footer you do not need. Copy everything between the header and footer:

From 0000000000000000000000000000000000000000 
From: your_name <[email protected]>
Date: TODAY
Subject: macos config

COPY WHAT IS HERE

-- 
2.38.1

Paste the contents you have just copied into a new file in the tensorflow repository at the following location: third_party/cpuinfo_macos_compat.patch. Now repeat this process with the xnnpack and ruy libraries generating the files: third_party/xnnpack_macos_compat.patch and third_party/ruy_macos_compat.patch respectively.

Once you have added these three patches, commit your changes locally to the tensorflow repository and generate the patch with git format-patch --keep-subject --no-stat --zero-commit <BASE_TENSORFLOW_COMMIT>.

Final Patch

You should now have a tensorflow patch (which contains your 3 sub patches) that looks like the following. If you are using the same version of tensorflow you can simply copy this patch.

NOTE: This is the patch for tensorflow 24f7ee636d62e1f8d8330357f8bbd65956dfb84d used in mediapipe v0.9.2.1 and v0.9.1

diff --git a/tensorflow/workspace2.bzl b/tensorflow/workspace2.bzl
index d46fab64b41..e8d1a24b1b4 100644
--- a/tensorflow/workspace2.bzl
+++ b/tensorflow/workspace2.bzl
@@ -132,6 +132,7 @@ def _tf_repositories():
         name = "XNNPACK",
         sha256 = "21cf16fb50c32682b8548cde83933bbd90b884983c50b9db7550002a72a75ad5",
         strip_prefix = "XNNPACK-8e3d3359f9bec608e09fac1f7054a2a14b1bd73c",
+        patch_file = ["//third_party:xnnpack_macos_compat.patch"],
         urls = tf_mirror_urls("https://github.com/google/XNNPACK/archive/8e3d3359f9bec608e09fac1f7054a2a14b1bd73c.zip"),
     )
     # LINT.ThenChange(//tensorflow/lite/tools/cmake/modules/xnnpack.cmake)
@@ -154,6 +155,7 @@ def _tf_repositories():
         name = "clog",
         strip_prefix = "cpuinfo-5e63739504f0f8e18e941bd63b2d6d42536c7d90",
         sha256 = "18eca9bc8d9c4ce5496d0d2be9f456d55cbbb5f0639a551ce9c8bac2e84d85fe",
+        patch_file = ["//third_party:cpuinfo_macos_compat.patch"],
         urls = tf_mirror_urls("https://github.com/pytorch/cpuinfo/archive/5e63739504f0f8e18e941bd63b2d6d42536c7d90.tar.gz"),
     )
 
@@ -161,6 +163,7 @@ def _tf_repositories():
         name = "cpuinfo",
         strip_prefix = "cpuinfo-5e63739504f0f8e18e941bd63b2d6d42536c7d90",
         sha256 = "18eca9bc8d9c4ce5496d0d2be9f456d55cbbb5f0639a551ce9c8bac2e84d85fe",
+        patch_file = ["//third_party:cpuinfo_macos_compat.patch"],
         urls = tf_mirror_urls("https://github.com/pytorch/cpuinfo/archive/5e63739504f0f8e18e941bd63b2d6d42536c7d90.tar.gz"),
     )
 
diff --git a/third_party/cpuinfo_macos_compat.patch b/third_party/cpuinfo_macos_compat.patch
new file mode 100644
index 00000000000..bb33c4cc68e
--- /dev/null
+++ b/third_party/cpuinfo_macos_compat.patch
@@ -0,0 +1,35 @@
+diff --git a/BUILD.bazel b/BUILD.bazel
+index 4ac8f10..6551fec 100644
+--- a/BUILD.bazel
++++ b/BUILD.bazel
+@@ -112,6 +112,7 @@ cc_library(
+         ":linux_mips64": COMMON_SRCS + LINUX_SRCS,
+         ":linux_riscv64": COMMON_SRCS + LINUX_SRCS,
+         ":linux_s390x": COMMON_SRCS + LINUX_SRCS,
++        ":macos": COMMON_SRCS + X86_SRCS + MACH_SRCS + MACH_X86_SRCS,
+         ":macos_x86_64": COMMON_SRCS + X86_SRCS + MACH_SRCS + MACH_X86_SRCS,
+         ":macos_arm64": COMMON_SRCS + MACH_SRCS + MACH_ARM_SRCS,
+         ":windows_x86_64": COMMON_SRCS + X86_SRCS + WINDOWS_X86_SRCS,
+@@ -242,13 +243,21 @@ config_setting(
+ )
+ 
+ config_setting(
+-    name = "macos_x86_64",
++    name = "macos",
+     values = {
+         "apple_platform_type": "macos",
+         "cpu": "darwin",
+     },
+ )
+ 
++config_setting(
++    name = "macos_x86_64",
++    values = {
++        "apple_platform_type": "macos",
++        "cpu": "darwin_x86_64",
++    },
++)
++
+ config_setting(
+     name = "windows_x86_64",
+     values = {"cpu": "x64_windows"},
diff --git a/third_party/ruy/workspace.bzl b/third_party/ruy/workspace.bzl
index 8b8ca8bc973..41827d5f04e 100644
--- a/third_party/ruy/workspace.bzl
+++ b/third_party/ruy/workspace.bzl
@@ -8,6 +8,7 @@ def repo():
         # LINT.IfChange
         sha256 = "dd6bf40322303cf8982f340e4139397c8fa350ff691d5254599cb21e0138fc65",
         strip_prefix = "ruy-841ea4172ba904fe3536789497f9565f2ef64129",
+        patch_file = ["//third_party:ruy_macos_compat.patch"],
         urls = tf_mirror_urls("https://github.com/google/ruy/archive/841ea4172ba904fe3536789497f9565f2ef64129.zip"),
         # LINT.ThenChange(//tensorflow/lite/tools/cmake/modules/ruy.cmake)
         build_file = "//third_party/ruy:BUILD",
diff --git a/third_party/ruy_macos_compat.patch b/third_party/ruy_macos_compat.patch
new file mode 100644
index 00000000000..2888ec8d80b
--- /dev/null
+++ b/third_party/ruy_macos_compat.patch
@@ -0,0 +1,35 @@
+diff --git a/third_party/cpuinfo.BUILD b/third_party/cpuinfo.BUILD
+index 6d68cd5..1c05e8e 100644
+--- a/third_party/cpuinfo.BUILD
++++ b/third_party/cpuinfo.BUILD
+@@ -131,6 +131,7 @@ cc_library(
+         ":linux_armv7a": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM32_SRCS,
+         ":linux_armeabi": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM32_SRCS,
+         ":linux_aarch64": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM64_SRCS,
++        ":macos": COMMON_SRCS + X86_SRCS + MACH_SRCS + MACH_X86_SRCS,
+         ":macos_x86_64": COMMON_SRCS + X86_SRCS + MACH_SRCS + MACH_X86_SRCS,
+         ":windows_x86_64": COMMON_SRCS + X86_SRCS + WINDOWS_X86_SRCS,
+         ":android_armv7": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM32_SRCS + ANDROID_ARM_SRCS,
+@@ -239,13 +240,21 @@ config_setting(
+ )
+ 
+ config_setting(
+-    name = "macos_x86_64",
++    name = "macos",
+     values = {
+         "apple_platform_type": "macos",
+         "cpu": "darwin",
+     },
+ )
+ 
++config_setting(
++    name = "macos_x86_64",
++    values = {
++        "apple_platform_type": "macos",
++        "cpu": "darwin_x86_64",
++    },
++)
++
+ config_setting(
+     name = "android",
+     values = {"crosstool_top": "//external:android/crosstool"},
diff --git a/third_party/xnnpack_macos_compat.patch b/third_party/xnnpack_macos_compat.patch
new file mode 100644
index 00000000000..7116844ab1a
--- /dev/null
+++ b/third_party/xnnpack_macos_compat.patch
@@ -0,0 +1,82 @@
+diff --git a/BUILD.bazel b/BUILD.bazel
+index b17798cef..52ff06c0a 100644
+--- a/BUILD.bazel
++++ b/BUILD.bazel
+@@ -15788,13 +15788,21 @@ config_setting(
+ )
+ 
+ config_setting(
+-    name = "macos_x86_64",
++    name = "macos",
+     values = {
+         "apple_platform_type": "macos",
+         "cpu": "darwin",
+     },
+ )
+ 
++config_setting(
++    name = "macos_x86_64",
++    values = {
++        "apple_platform_type": "macos",
++        "cpu": "darwin_x86_64",
++    },
++)
++
+ config_setting(
+     name = "macos_arm64",
+     values = {
+@@ -15932,6 +15940,7 @@ selects.config_setting_group(
+         ":android_x86_64",
+         ":ios_x86_64",
+         ":linux_k8",
++        ":macos",
+         ":macos_x86_64",
+         ":tvos_x86_64",
+         ":watchos_x86_64",
+diff --git a/build_defs.bzl b/build_defs.bzl
+index b8217a18d..ecc3e0b33 100644
+--- a/build_defs.bzl
++++ b/build_defs.bzl
+@@ -158,6 +158,7 @@ def xnnpack_cc_library(
+             ":linux_armhf": aarch32_copts,
+             ":linux_armv7a": aarch32_copts,
+             ":linux_arm64": aarch64_copts,
++            ":macos": gcc_x86_copts,
+             ":macos_x86_64": gcc_x86_copts,
+             ":macos_arm64": aarch64_copts,
+             ":windows_x86_64_clang": ["/clang:" + opt for opt in gcc_x86_copts],
+diff --git a/third_party/cpuinfo.BUILD b/third_party/cpuinfo.BUILD
+index 1997f4e3a..833517370 100644
+--- a/third_party/cpuinfo.BUILD
++++ b/third_party/cpuinfo.BUILD
+@@ -102,6 +102,7 @@ cc_library(
+         ":linux_armhf": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM32_SRCS,
+         ":linux_armv7a": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM32_SRCS,
+         ":linux_aarch64": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM64_SRCS,
++        ":macos": COMMON_SRCS + X86_SRCS + MACH_SRCS + MACH_X86_SRCS,
+         ":macos_x86_64": COMMON_SRCS + X86_SRCS + MACH_SRCS + MACH_X86_SRCS,
+         ":macos_arm64": COMMON_SRCS + MACH_SRCS + MACH_ARM_SRCS,
+         ":windows_x86_64": COMMON_SRCS + X86_SRCS + WINDOWS_X86_SRCS,
+@@ -198,13 +199,21 @@ config_setting(
+ )
+ 
+ config_setting(
+-    name = "macos_x86_64",
++    name = "macos",
+     values = {
+         "apple_platform_type": "macos",
+         "cpu": "darwin",
+     },
+ )
+ 
++config_setting(
++    name = "macos_x86_64",
++    values = {
++        "apple_platform_type": "macos",
++        "cpu": "darwin_x86_64",
++    },
++)
++
+ config_setting(
+     name = "macos_arm64",
+     values = {

UPDATE: This is the patch for tensorflow version d712c0c9e24519cc8cd3720279666720d1000eee used in mediapipe v0.9.3.0

diff --git a/tensorflow/workspace2.bzl b/tensorflow/workspace2.bzl
index 70dd1177225..6a197bbcce2 100644
--- a/tensorflow/workspace2.bzl
+++ b/tensorflow/workspace2.bzl
@@ -145,6 +145,7 @@ def _tf_repositories():
         name = "XNNPACK",
         sha256 = "c979b62e8b77af60dfd7567f22ade20d5a9d4d0888f8a2d60d155fc0d31b22ab",
         strip_prefix = "XNNPACK-b9d4073a6913891ce9cbd8965c8d506075d2a45a",
+        patch_file = ["//third_party:xnnpack_macos_compat.patch"],
         urls = tf_mirror_urls("https://github.com/google/XNNPACK/archive/b9d4073a6913891ce9cbd8965c8d506075d2a45a.zip"),
     )
     # LINT.ThenChange(//tensorflow/lite/tools/cmake/modules/xnnpack.cmake)
@@ -167,6 +168,7 @@ def _tf_repositories():
         name = "cpuinfo",
         strip_prefix = "cpuinfo-3dc310302210c1891ffcfb12ae67b11a3ad3a150",
         sha256 = "ba668f9f8ea5b4890309b7db1ed2e152aaaf98af6f9a8a63dbe1b75c04e52cb9",
+        patch_file = ["//third_party:cpuinfo_macos_compat.patch"],
         urls = tf_mirror_urls("https://github.com/pytorch/cpuinfo/archive/3dc310302210c1891ffcfb12ae67b11a3ad3a150.zip"),
     )
 
diff --git a/third_party/cpuinfo_macos_compat.patch b/third_party/cpuinfo_macos_compat.patch
new file mode 100644
index 00000000000..6dbb6aa91ed
--- /dev/null
+++ b/third_party/cpuinfo_macos_compat.patch
@@ -0,0 +1,35 @@
+diff --git a/BUILD.bazel b/BUILD.bazel
+index 0c92064..efabc7b 100644
+--- a/BUILD.bazel
++++ b/BUILD.bazel
+@@ -113,6 +113,7 @@ cc_library(
+         ":linux_mips64": COMMON_SRCS + LINUX_SRCS,
+         ":linux_riscv64": COMMON_SRCS + LINUX_SRCS,
+         ":linux_s390x": COMMON_SRCS + LINUX_SRCS,
++        ":macos": COMMON_SRCS + X86_SRCS + MACH_SRCS + MACH_X86_SRCS,
+         ":macos_x86_64": COMMON_SRCS + X86_SRCS + MACH_SRCS + MACH_X86_SRCS,
+         ":macos_arm64": COMMON_SRCS + MACH_SRCS + MACH_ARM_SRCS,
+         ":windows_x86_64": COMMON_SRCS + X86_SRCS + WINDOWS_X86_SRCS,
+@@ -240,13 +241,21 @@ config_setting(
+ )
+ 
+ config_setting(
+-    name = "macos_x86_64",
++    name = "macos",
+     values = {
+         "apple_platform_type": "macos",
+         "cpu": "darwin",
+     },
+ )
+ 
++config_setting(
++    name = "macos_x86_64",
++    values = {
++        "apple_platform_type": "macos",
++        "cpu": "darwin_x86_64",
++    },
++)
++
+ config_setting(
+     name = "windows_x86_64",
+     values = {"cpu": "x64_windows"},
diff --git a/third_party/ruy/workspace.bzl b/third_party/ruy/workspace.bzl
index 3cee172c52f..553a3ffcc87 100644
--- a/third_party/ruy/workspace.bzl
+++ b/third_party/ruy/workspace.bzl
@@ -8,6 +8,7 @@ def repo():
         # LINT.IfChange
         sha256 = "a22c42e80c7bb450db8492728e4742ee66f46d5458c45fe67ce2c9b61240630c",
         strip_prefix = "ruy-3286a34cc8de6149ac6844107dfdffac91531e72",
+        patch_file = ["//third_party:ruy_macos_compat.patch"],
         urls = tf_mirror_urls("https://github.com/google/ruy/archive/3286a34cc8de6149ac6844107dfdffac91531e72.zip"),
         # LINT.ThenChange(//tensorflow/lite/tools/cmake/modules/ruy.cmake)
         build_file = "//third_party/ruy:BUILD",
diff --git a/third_party/ruy_macos_compat.patch b/third_party/ruy_macos_compat.patch
new file mode 100644
index 00000000000..2888ec8d80b
--- /dev/null
+++ b/third_party/ruy_macos_compat.patch
@@ -0,0 +1,35 @@
+diff --git a/third_party/cpuinfo.BUILD b/third_party/cpuinfo.BUILD
+index 6d68cd5..1c05e8e 100644
+--- a/third_party/cpuinfo.BUILD
++++ b/third_party/cpuinfo.BUILD
+@@ -131,6 +131,7 @@ cc_library(
+         ":linux_armv7a": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM32_SRCS,
+         ":linux_armeabi": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM32_SRCS,
+         ":linux_aarch64": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM64_SRCS,
++        ":macos": COMMON_SRCS + X86_SRCS + MACH_SRCS + MACH_X86_SRCS,
+         ":macos_x86_64": COMMON_SRCS + X86_SRCS + MACH_SRCS + MACH_X86_SRCS,
+         ":windows_x86_64": COMMON_SRCS + X86_SRCS + WINDOWS_X86_SRCS,
+         ":android_armv7": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM32_SRCS + ANDROID_ARM_SRCS,
+@@ -239,13 +240,21 @@ config_setting(
+ )
+ 
+ config_setting(
+-    name = "macos_x86_64",
++    name = "macos",
+     values = {
+         "apple_platform_type": "macos",
+         "cpu": "darwin",
+     },
+ )
+ 
++config_setting(
++    name = "macos_x86_64",
++    values = {
++        "apple_platform_type": "macos",
++        "cpu": "darwin_x86_64",
++    },
++)
++
+ config_setting(
+     name = "android",
+     values = {"crosstool_top": "//external:android/crosstool"},
diff --git a/third_party/xnnpack_macos_compat.patch b/third_party/xnnpack_macos_compat.patch
new file mode 100644
index 00000000000..431090813ab
--- /dev/null
+++ b/third_party/xnnpack_macos_compat.patch
@@ -0,0 +1,47 @@
+diff --git a/BUILD.bazel b/BUILD.bazel
+index 9719e8f87..085773afa 100644
+--- a/BUILD.bazel
++++ b/BUILD.bazel
+@@ -11590,13 +11590,21 @@ config_setting(
+ )
+ 
+ config_setting(
+-    name = "macos_x86_64",
++    name = "macos",
+     values = {
+         "apple_platform_type": "macos",
+         "cpu": "darwin",
+     },
+ )
+ 
++config_setting(
++    name = "macos_x86_64",
++    values = {
++        "apple_platform_type": "macos",
++        "cpu": "darwin_x86_64",
++    },
++)
++
+ config_setting(
+     name = "macos_arm64",
+     values = {
+@@ -11734,6 +11742,7 @@ selects.config_setting_group(
+         ":android_x86_64",
+         ":ios_x86_64",
+         ":linux_k8",
++        ":macos",
+         ":macos_x86_64",
+         ":tvos_x86_64",
+         ":watchos_x86_64",
+diff --git a/build_defs.bzl b/build_defs.bzl
+index 01b436eb7..d148b064c 100644
+--- a/build_defs.bzl
++++ b/build_defs.bzl
+@@ -158,6 +158,7 @@ def xnnpack_cc_library(
+             ":linux_armhf": aarch32_copts,
+             ":linux_armv7a": aarch32_copts,
+             ":linux_arm64": aarch64_copts,
++            ":macos": gcc_x86_copts,
+             ":macos_x86_64": gcc_x86_copts,
+             ":macos_arm64": aarch64_copts,
+             ":windows_x86_64_clang": ["/clang:" + opt for opt in gcc_x86_copts],

If you are using a different version, you will need to go through the entire procedure above to generate the proper patch and corresponding sub patches. Add the contents of this patch to a new file in the proejct repository (mediapipe) at the path: third_party/org_tensorflow_fixes_builds_for_macos.diff.

Finally you can add the following in the WORKSPACE file to apply the patch:

patches = [
        "@//third_party:org_tensorflow_compatibility_fixes.diff",
        # Diff is generated with a script, don't update it manually.
        "@//third_party:org_tensorflow_custom_ops.diff",
        "@//third_party:org_tensorflow_fixes_builds_for_macos.diff",
        
    ],

Conclusion

If you’ve made it this far, congratulations! The double-nested patching process is not for the faint of heart. I hope you now you have a functional MacOS mediapipe build with @build_bazel_rules_apple. As these patches must be regenerated anytime the version of tensorflow changes in the WORKSPACE file, we may meet again here soon.