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

Add native support for Apple Silicon #12900

Closed

Conversation

meteorcloudy
Copy link
Member

@meteorcloudy meteorcloudy commented Jan 26, 2021

After this PR, we can use a x86 version of Bazel to build an ARM64 Bazel binary for Apple Silicon by

bazel build --cpu=darwin_arm64 //src:bazel

The Bazel client, embedded tools and embedded JDK are all native ARM64 binaries.

Changes in this PR:

  1. Add OpenJDK definitions for ARM64 macOS
  2. Manually created the embedded JDK binaries on an Apple Silicon machine and uploaded them to the bazel mirror site.
  3. AutoCpuConverter.java: identify the cpu value correctly
  4. cc_toolchain_config.bzl: Explicitly specify the target platform also in the x86 cc toolchain, so that we can also do cross-platform compile from ARM64 to x86.
  5. lib_cc_configure.bzl: return darwin_x86_64 or darwin_arm64 instead of just darwin for cpu value on macOS.

With the native arm64 Bazel binary, building Bazel itself takes only half of the time compared with the x86 Bazel binary on an Apple DTK machine:

$ bazel build --cpu=darwin_arm64 --disk_cache= --repository_cache= //src:bazel
Starting local Bazel server and connecting to it...
...
Target //src:bazel up-to-date:
  bazel-bin/src/bazel
INFO: Elapsed time: 1044.462s, Critical Path: 180.08s
INFO: 2984 processes: 179 internal, 2092 darwin-sandbox, 1 local, 712 worker.
INFO: Build completed successfully, 2984 total actions

cp  ./bazel-bin/src/bazel ~/bin/bazel-arm64

$ bazel-arm64 clean --expunge

$ bazel-arm64 build --disk_cache= --repository_cache= //src:bazel
Starting local Bazel server and connecting to it...
...
Target //src:bazel up-to-date:
  bazel-bin/src/bazel
INFO: Elapsed time: 464.470s, Critical Path: 114.88s
INFO: 2984 processes: 179 internal, 2092 darwin-sandbox, 1 local, 712 worker.
INFO: Build completed successfully, 2984 total actions

Closes: #11628

Copy link
Member

@meisterT meisterT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @comius

@meteorcloudy
Copy link
Member Author

FYI @aiuto

Copy link
Contributor

@comius comius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do android tests pass (with a bootstrapped version of bazel on arm architecture)? See #12880

Consider using jdk11.0.9 otherwise, if it's not too much effort.

return "darwin";
switch (CPU.getCurrent()) {
case X86_64:
return "darwin_x86_64";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing "darwin" to "darwin_x86_64" might cause some issues. @katre is probably more familiar with this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It indeed broke the test_disable_cc_toolchain_detection test on macOS, check the latest commit for the fix. I'm not sure if there are more issues I missed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long term this will be great because there's existing ambiguity between darwin and darwin_x86_64

@meteorcloudy
Copy link
Member Author

Do android tests pass (with a bootstrapped version of bazel on arm architecture)? See #12880

Consider using jdk11.0.9 otherwise, if it's not too much effort.

I haven't tried to install the Android SDK yet, looks like they don't have official Apple Silicon support for now. And there are many other tests still broken, so it will probably take a while to enable tests for Apple Silicon. Hopefully we can get that issue resolved by then, otherwise I can revert the JDK version to 11.0.9.

@meteorcloudy
Copy link
Member Author

meteorcloudy commented Jan 26, 2021

One problem I noticed from the failing test is that netty doesn't work on Apple Silicon because the jar file actually contains some native shared libraries that're not built for Apple Silicon. This means remote caching/remote execution won't work on Apple Silicon, and I'm not sure what to do..

Copy link
Member

@keith keith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@aiuto
Copy link
Contributor

aiuto commented Jan 27, 2021

cc: @susinmotion

@bazel-io bazel-io closed this in 8c7e11a Jan 27, 2021
@mahenzon
Copy link

mahenzon commented Jan 29, 2021

Hi!
I'm trying to build bazel for darwin_arm64, I cloned repo (latest commit is db69c9f, so of course it includes "Add native support for Apple Silicon" (8c7e11a)
Also, I applied this patch bazelbuild/rules_apple#980 (comment)

So, I use this command:
bazel build --cpu=darwin_arm64 //src:bazel

But I'm getting this:

<...>
Use --sandbox_debug to see verbose messages from the sandbox
In file included from src/main/java/net/starlark/java/eval/cpu_profiler_posix.cc:20:
bazel-out/darwin_arm64-fastbuild/bin/external/bazel_tools/tools/jdk/include/jni.h:45:10: fatal error: 'jni_md.h' file not found
#include "jni_md.h"
         ^~~~~~~~~~
1 error generated.
Target //src:bazel failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.730s, Critical Path: 1.19s
INFO: 10 processes: 6 internal, 4 darwin-sandbox.
FAILED: Build did NOT complete successfully

I also tried to checkout 8c7e11a in case it can help, but it doesn’t . Getting the same error

Is there something else I should patch?

@meteorcloudy
Copy link
Member Author

@mahenzon Which Bazel version were you using? Can you try with the latest 4.0.0?

@meteorcloudy
Copy link
Member Author

@mahenzon And can you check if jni_md.h exist in your JDK installation? Somewhere like /Library/Java/JavaVirtualMachines/zulu-13.jdk/Contents/Home/include/darwin/jni_md.h

@mahenzon
Copy link

mahenzon commented Jan 29, 2021

yeah, there's file /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/include/darwin/jni_md.h

I used 3.7.0.
This time I tried with 4.0.0 and it worked! Thanks!

So, full list of steps:

  • Download 4.0.0 curl -O -L https://github.com/bazelbuild/bazel/releases/download/4.0.0/bazel-4.0.0-darwin-x86_64
  • mv bazel-4.0.0-darwin-x86_64 bazel
  • chmod +x bazel
  • download source and do any patches you need
  • build source using bazel 4.0.0 ~/bazel-dist-4.0.0/bazel build --cpu=darwin_arm64 //src:bazel

Your build will be in bazel-bin/src/bazel. Copy it where you need it and run chmod +x bazel
Note that this build's version will be no_version:

➜ ./bazel --version
bazel no_version

UPD:
it's recommended to build Bazel in release mode by adding -c opt:
~/bazel-dist-4.0.0/bazel build --cpu=darwin_arm64 -c opt //src:bazel
© TelegramMessenger/Telegram-iOS#467 (comment)

@philwo
Copy link
Member

philwo commented Feb 27, 2021

It also works on my Mac Mini M1 and gives a nice ~2x speed increase! 🚀

$ cd ~/bin
$ curl -LO https://github.com/bazelbuild/bazel/releases/download/4.0.0/bazel-4.0.0-darwin-x86_64
$ chmod +x ~/bin/bazel-4.0.0-darwin-x86_64

$ cd ~/src && git clone https://github.com/bazelbuild/bazel.git && cd bazel
$ bazel-4.0.0-darwin-x86_64 build --cpu=darwin_arm64 -c opt //src:bazel
INFO: Elapsed time: 495.900s, Critical Path: 148.84s
INFO: Build completed successfully, 2884 total actions

$ mv bazel-bin/src/bazel ~/bin/bazel
$ bazel clean --expunge
$ bazel build --cpu=darwin_arm64 -c opt //src:bazel
INFO: Elapsed time: 255.364s, Critical Path: 68.15s
INFO: Build completed successfully, 2884 total actions

@qthequartermasterman
Copy link

It also works on my Mac Mini M1 and gives a nice ~2x speed increase! 🚀

$ cd ~/bin
$ curl -LO https://github.com/bazelbuild/bazel/releases/download/4.0.0/bazel-4.0.0-darwin-x86_64
$ chmod +x ~/bin/bazel-4.0.0-darwin-x86_64

$ cd ~/src && git clone https://github.com/bazelbuild/bazel.git && cd bazel
$ bazel-4.0.0-darwin-x86_64 build --cpu=darwin_arm64 -c opt //src:bazel
INFO: Elapsed time: 495.900s, Critical Path: 148.84s
INFO: Build completed successfully, 2884 total actions

$ mv bazel-bin/src/bazel ~/bin/bazel
$ bazel clean --expunge
$ bazel build --cpu=darwin_arm64 -c opt //src:bazel
INFO: Elapsed time: 255.364s, Critical Path: 68.15s
INFO: Build completed successfully, 2884 total actions

I have successfully used these steps to build bazel 4.0.0 on Apple Silicon. However, another application I am trying to build is throwing an error because this bazel build is unversioned, as @mahenzon mentions (#12900 (comment)). Is there any way to build bazel in a way so that it returns a correct version number?

@meteorcloudy
Copy link
Member Author

meteorcloudy commented Apr 13, 2021

@qthequartermasterman
--embed_label <version> is the build flag you are looking for.

@qthequartermasterman
Copy link

qthequartermasterman commented Apr 18, 2021 via email

meteorcloudy added a commit that referenced this pull request Apr 20, 2021
After this PR, we can use a x86 version of Bazel to build an ARM64 Bazel binary for Apple Silicon by
```
bazel build --cpu=darwin_arm64 //src:bazel
```
The Bazel client, embedded tools and embedded JDK are all native ARM64 binaries.

Changes in this PR:
1. Add OpenJDK definitions for ARM64 macOS
2. Manually created the embedded JDK binaries on an Apple Silicon machine and uploaded them to the bazel mirror site.
3. AutoCpuConverter.java: identify the cpu value correctly
4. cc_toolchain_config.bzl: Explicitly specify the target platform also in the x86 cc toolchain, so that we can also do cross-platform compile from ARM64 to x86.

With the native arm64 Bazel binary, building Bazel itself takes only half of the time compared with the x86 Bazel binary on an Apple DTK machine:
```
$ bazel build --cpu=darwin_arm64 --disk_cache= --repository_cache= //src:bazel
Starting local Bazel server and connecting to it...
...
Target //src:bazel up-to-date:
  bazel-bin/src/bazel
INFO: Elapsed time: 1044.462s, Critical Path: 180.08s
INFO: 2984 processes: 179 internal, 2092 darwin-sandbox, 1 local, 712 worker.
INFO: Build completed successfully, 2984 total actions

cp  ./bazel-bin/src/bazel ~/bin/bazel-arm64

$ bazel-arm64 clean --expunge

$ bazel-arm64 build --disk_cache= --repository_cache= //src:bazel
Starting local Bazel server and connecting to it...
...
Target //src:bazel up-to-date:
  bazel-bin/src/bazel
INFO: Elapsed time: 464.470s, Critical Path: 114.88s
INFO: 2984 processes: 179 internal, 2092 darwin-sandbox, 1 local, 712 worker.
INFO: Build completed successfully, 2984 total actions

```

Closes: #11628

Closes #12900.

PiperOrigin-RevId: 354058336
meteorcloudy added a commit that referenced this pull request Apr 20, 2021
After this PR, we can use a x86 version of Bazel to build an ARM64 Bazel binary for Apple Silicon by
```
bazel build --cpu=darwin_arm64 //src:bazel
```
The Bazel client, embedded tools and embedded JDK are all native ARM64 binaries.

Changes in this PR:
1. Add OpenJDK definitions for ARM64 macOS
2. Manually created the embedded JDK binaries on an Apple Silicon machine and uploaded them to the bazel mirror site.
3. AutoCpuConverter.java: identify the cpu value correctly
4. cc_toolchain_config.bzl: Explicitly specify the target platform also in the x86 cc toolchain, so that we can also do cross-platform compile from ARM64 to x86.

With the native arm64 Bazel binary, building Bazel itself takes only half of the time compared with the x86 Bazel binary on an Apple DTK machine:
```
$ bazel build --cpu=darwin_arm64 --disk_cache= --repository_cache= //src:bazel
Starting local Bazel server and connecting to it...
...
Target //src:bazel up-to-date:
  bazel-bin/src/bazel
INFO: Elapsed time: 1044.462s, Critical Path: 180.08s
INFO: 2984 processes: 179 internal, 2092 darwin-sandbox, 1 local, 712 worker.
INFO: Build completed successfully, 2984 total actions

cp  ./bazel-bin/src/bazel ~/bin/bazel-arm64

$ bazel-arm64 clean --expunge

$ bazel-arm64 build --disk_cache= --repository_cache= //src:bazel
Starting local Bazel server and connecting to it...
...
Target //src:bazel up-to-date:
  bazel-bin/src/bazel
INFO: Elapsed time: 464.470s, Critical Path: 114.88s
INFO: 2984 processes: 179 internal, 2092 darwin-sandbox, 1 local, 712 worker.
INFO: Build completed successfully, 2984 total actions

```

Closes: #11628

Closes #12900.

PiperOrigin-RevId: 354058336

Revert default cpu value on x86 macOS to "darwin"

Many downstream projects are still depending on the default cpu value
on x86 macOS being "darwin" instead of "darwin_x86_64"

https://buildkite.com/bazel/bazel-auto-sheriff-face-with-cowboy-hat/builds/412

Related: #11628

Closes #12918.

PiperOrigin-RevId: 354279409
meteorcloudy added a commit that referenced this pull request Apr 20, 2021
After this PR, we can use a x86 version of Bazel to build an ARM64 Bazel binary for Apple Silicon by
```
bazel build --cpu=darwin_arm64 //src:bazel
```
The Bazel client, embedded tools and embedded JDK are all native ARM64 binaries.

Changes in this PR:
1. Add OpenJDK definitions for ARM64 macOS
2. Manually created the embedded JDK binaries on an Apple Silicon machine and uploaded them to the bazel mirror site.
3. AutoCpuConverter.java: identify the cpu value correctly
4. cc_toolchain_config.bzl: Explicitly specify the target platform also in the x86 cc toolchain, so that we can also do cross-platform compile from ARM64 to x86.

With the native arm64 Bazel binary, building Bazel itself takes only half of the time compared with the x86 Bazel binary on an Apple DTK machine:
```
$ bazel build --cpu=darwin_arm64 --disk_cache= --repository_cache= //src:bazel
Starting local Bazel server and connecting to it...
...
Target //src:bazel up-to-date:
  bazel-bin/src/bazel
INFO: Elapsed time: 1044.462s, Critical Path: 180.08s
INFO: 2984 processes: 179 internal, 2092 darwin-sandbox, 1 local, 712 worker.
INFO: Build completed successfully, 2984 total actions

cp  ./bazel-bin/src/bazel ~/bin/bazel-arm64

$ bazel-arm64 clean --expunge

$ bazel-arm64 build --disk_cache= --repository_cache= //src:bazel
Starting local Bazel server and connecting to it...
...
Target //src:bazel up-to-date:
  bazel-bin/src/bazel
INFO: Elapsed time: 464.470s, Critical Path: 114.88s
INFO: 2984 processes: 179 internal, 2092 darwin-sandbox, 1 local, 712 worker.
INFO: Build completed successfully, 2984 total actions

```

Closes: #11628

Closes #12900.

PiperOrigin-RevId: 354058336

Revert default cpu value on x86 macOS to "darwin"

Many downstream projects are still depending on the default cpu value
on x86 macOS being "darwin" instead of "darwin_x86_64"

https://buildkite.com/bazel/bazel-auto-sheriff-face-with-cowboy-hat/builds/412

Related: #11628

Closes #12918.

PiperOrigin-RevId: 354279409

Update platform version to 0.0.4

Use configuration flags for //src/conditions:darwin_arm64

Partially reverts 6d637f4, to allow
cross-compiling Bazel from darwin_x86_64 to darwin_arm64 with
the `--cpu=darwin_arm64` flag.

The only source that is different between darwin_x86_64 and darwin_arm64
now is the embedded JDK.

Closes #12653.

PiperOrigin-RevId: 346298051

Make sure Bazel selects correct remote JDK on Apple Silicon
philwo pushed a commit that referenced this pull request Apr 20, 2021
After this PR, we can use a x86 version of Bazel to build an ARM64 Bazel binary for Apple Silicon by
```
bazel build --cpu=darwin_arm64 //src:bazel
```
The Bazel client, embedded tools and embedded JDK are all native ARM64 binaries.

Changes in this PR:
1. Add OpenJDK definitions for ARM64 macOS
2. Manually created the embedded JDK binaries on an Apple Silicon machine and uploaded them to the bazel mirror site.
3. AutoCpuConverter.java: identify the cpu value correctly
4. cc_toolchain_config.bzl: Explicitly specify the target platform also in the x86 cc toolchain, so that we can also do cross-platform compile from ARM64 to x86.

With the native arm64 Bazel binary, building Bazel itself takes only half of the time compared with the x86 Bazel binary on an Apple DTK machine:
```
$ bazel build --cpu=darwin_arm64 --disk_cache= --repository_cache= //src:bazel
Starting local Bazel server and connecting to it...
...
Target //src:bazel up-to-date:
  bazel-bin/src/bazel
INFO: Elapsed time: 1044.462s, Critical Path: 180.08s
INFO: 2984 processes: 179 internal, 2092 darwin-sandbox, 1 local, 712 worker.
INFO: Build completed successfully, 2984 total actions

cp  ./bazel-bin/src/bazel ~/bin/bazel-arm64

$ bazel-arm64 clean --expunge

$ bazel-arm64 build --disk_cache= --repository_cache= //src:bazel
Starting local Bazel server and connecting to it...
...
Target //src:bazel up-to-date:
  bazel-bin/src/bazel
INFO: Elapsed time: 464.470s, Critical Path: 114.88s
INFO: 2984 processes: 179 internal, 2092 darwin-sandbox, 1 local, 712 worker.
INFO: Build completed successfully, 2984 total actions

```

Closes: #11628

Closes #12900.

PiperOrigin-RevId: 354058336

Revert default cpu value on x86 macOS to "darwin"

Many downstream projects are still depending on the default cpu value
on x86 macOS being "darwin" instead of "darwin_x86_64"

https://buildkite.com/bazel/bazel-auto-sheriff-face-with-cowboy-hat/builds/412

Related: #11628

Closes #12918.

PiperOrigin-RevId: 354279409

Update platform version to 0.0.4

Use configuration flags for //src/conditions:darwin_arm64

Partially reverts 6d637f4, to allow
cross-compiling Bazel from darwin_x86_64 to darwin_arm64 with
the `--cpu=darwin_arm64` flag.

The only source that is different between darwin_x86_64 and darwin_arm64
now is the embedded JDK.

Closes #12653.

PiperOrigin-RevId: 346298051

Make sure Bazel selects correct remote JDK on Apple Silicon
@lexuanquynh
Copy link

I got error:

Required bazel version is "4.0.0", but "no_version"" is reported by /Users/xuanquynhle/bin/bazel

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

Successfully merging this pull request may close these issues.

Support macOS Big Sur and Apple Silicon hardware platform
9 participants