Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing support for iPhoneSimulator platform on arm64 #980

Closed
mrosales opened this issue Nov 6, 2020 · 15 comments
Closed

Missing support for iPhoneSimulator platform on arm64 #980

mrosales opened this issue Nov 6, 2020 · 15 comments

Comments

@mrosales
Copy link

mrosales commented Nov 6, 2020

I've been stuck on this for awhile now. Since the introduction of Xcode 12, the default behavior when building a binary for all architecture is to include the iPhoneSimulator target for arm64, which would be to support Apple Silicon computers.

I'm trying to build an apple_static_library for the simulator with something like a --ios_multi_cpus=arm64,x86_64 option to build a simulator fat archive with both simulator slices.

Ultimately my goal is to create a simulator fat archive and a device fat archive and then merge them into an XCFramework that is importable by an Xcode based project, but I am stuck on figuring out how to configure bazel to build something that doesn't always assume that arm64 is iphoneos and iphonesimulator is x86_64 or i386.

I've been looking through the rules and configuration in this repo and don't think that this is possible. Am I missing something, or is this a missing feature?

@keith
Copy link
Member

keith commented Nov 6, 2020

I haven't heard of anyone successfully doing this yet (although I doubt many have tried). I also haven't audited the rules or bazel core for where these assumptions are (i imagine many are in bazel itself)

@mrosales
Copy link
Author

mrosales commented Nov 6, 2020

I did a little bit more digging and found the following assumption in the bazel project:

https://github.com/bazelbuild/bazel/blob/450c7ad65bfcffebf753159ee2b617e557ffb4fd/src/main/java/com/google/devtools/build/lib/rules/apple/ApplePlatform.java#L44

More critically, though, it looks like the method for selecting the build platform to use assumes a 1:1 mapping between (PlatformType, CPU) and the ApplePlatform, which is an assumption that is broken with the introduction of new arm-based Apple Silicon machines.

https://github.com/bazelbuild/bazel/blob/450c7ad65bfcffebf753159ee2b617e557ffb4fd/src/main/java/com/google/devtools/build/lib/rules/apple/ApplePlatform.java#L108

@keith
Copy link
Member

keith commented Nov 6, 2020

Related: bazelbuild/bazel#11628

I def agree there's some UX question here because of the archs overlap

@ihomway
Copy link

ihomway commented Nov 9, 2020

Facing the same problem here.
After trying and searching for many days, I found the problem is the assumption too. I hope there will be a workaround to fix this issue.

@keith
Copy link
Member

keith commented Nov 9, 2020

I wouldn't expect there to be much investment in Apple Silicon in the near term so if this is blocking you I would suggest submitting a PR to bazel core with the fix

@indragiek
Copy link
Contributor

indragiek commented Dec 4, 2020

For anyone blocked on being able to build an arm64 iOS simulator slice, I came up with a small patch to bazel core that adds support -- check out bazel at the 3.7.1 tag and apply this: https://gist.github.com/indragiek/e14162c0098d97ee976bceae9441f04d

Then generate an arm64 iOS Simulator slice by running:

$ bazel build ... --cpu=ios_sim_arm64 --apple_platform_type=ios

I don't have a plan to contribute this as-is to bazel as it seems like this may not be the right approach to fixing the archs overlap issue, but it's good enough to solve the immediate problem.

@ihomway
Copy link

ihomway commented Dec 7, 2020

indragiek's solution works for me, but if you have Swift files in your project, you may need a small change in rules_swift library, you can find this small change here 👇:

ihomway/rules_swift@a3699bf

@acecilia
Copy link

acecilia commented May 26, 2021

@indragiek Trying to use your solution to get the arm64 simulator slice from an intel mac in Catalina 10.15.7.

This is the script I am using to build bazel with your patch:

#!/bin/zsh

set -euo pipefail

readonly WORK_DIR="$(pwd)"
readonly BUILD_DIR="${WORK_DIR}/build-frameworks"
readonly OUTPUT_DIR="${BUILD_DIR}/output"
readonly BAZEL_DIR="${BUILD_DIR}/bazel"
readonly BAZEL_BIN="${BAZEL_DIR}/bin/bazel"

download_patched_version_of_bazel() {
    mkdir -p "${BAZEL_DIR}/4.1.0"
    cd "${BAZEL_DIR}/4.1.0"
    curl -O -L "https://github.com/bazelbuild/bazel/releases/download/4.1.0/bazel-4.1.0-darwin-x86_64"
    mv bazel-4.1.0-darwin-x86_64 bazel
    chmod +x bazel

    cd "${BAZEL_DIR}"
    git clone --branch "3.7.2" --depth 1 https://github.com/bazelbuild/bazel.git "3.7.2"
    cd "3.7.2"
    curl -O -L https://gist.githubusercontent.com/indragiek/e14162c0098d97ee976bceae9441f04d/raw/dba3a66a90d0a96d0dc6e236ab3fa1d41a2ce359/ios_sim_arm64.patch
    git apply ios_sim_arm64.patch

    # To install java11 on macos: brew install java11 https://mkyong.com/java/how-to-install-java-on-mac-osx/
    # https://github.com/bazelbuild/bazel/issues/11399#issuecomment-628945756
    # https://github.com/bazelbuild/rules_nodejs/issues/1301
    export JAVA_HOME="/usr/local/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home" && "${BAZEL_DIR}"/4.1.0/bazel build --incompatible_restrict_string_escapes=false -c opt //src:bazel
    mkdir -p "$(dirname ${BAZEL_BIN})"
    cp bazel-bin/src/bazel "${BAZEL_BIN}"
    chmod +x "${BAZEL_BIN}"

    cd "${WORK_DIR}"
}

Which then I am using as:

${BAZEL_BIN} build --config=ios --cpu=ios_sim_arm64 -c opt tensorflow/lite/ios:TensorFlowLiteC_framework

Bazel builds correctly with your patch, but then when using it I am getting the following error:

ERROR: /private/var/tmp/_bazel_andres/5dd7712722979875795c5b78f5afb6d1/external/cpuinfo/BUILD.bazel:100:11: 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

Any help would be much appreciated 🙌

EDIT: never mind, seems an issue specific to the project I am building and not bazel 🙏

kyle-foreflight pushed a commit to kyle-foreflight/bazel that referenced this issue Jun 4, 2021
@peterkorodi
Copy link

peterkorodi commented Jun 22, 2021

Hi @acecilia,

I got the same error. It seems that cpuinfo/BUILD.bazeldosen't contain ios_sim_arm64.
Could you solve this issue, and compile tensorflowlite to sim_arm64?

@acecilia
Copy link

@peterkorodi yes. I managed to compile an xcframework for tensorflowlite. Find the binary here: https://github.com/revolut-mobile/TensorFlowLiteC-binary/releases/tag/2.5.0

@keith
Copy link
Member

keith commented Aug 19, 2021

Note that this stuff is starting to land upstream, once bazelbuild/bazel#13870 lands building for the arm64 simulator should work

@bvirlet
Copy link

bvirlet commented Oct 21, 2021

bazelbuild/bazel#13870 seems to have been merged for release in Bazel 5.0, targeted release date November 16.

@keith
Copy link
Member

keith commented Oct 26, 2021

Closing this now that ios_sim_arm64 has landed upstream, it should be available in the 5.x RC coming this week. For xcframeworks in general please subscribe to the tracking issue #1249

@keith keith closed this as completed Oct 26, 2021
@bvirlet
Copy link

bvirlet commented Nov 22, 2021

@acecilia are you still using your patches to build TensorFlowLiteC for Apple Silicon or is there an easier path now?

@acecilia
Copy link

@bvirlet I did not need to rebuild TensorFlowLiteC yet, so no idea about current status

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants