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

Set CMAKE_OSX_ARCHITECTURES to target architecture #891

Conversation

keith
Copy link
Member

@keith keith commented Apr 15, 2022

This fixes a bug where on M1 macOS machines, when building for the
x86_64 iOS simulator, the cmake dependencies were building for arm64
instead, if you had /usr/sbin in your $PATH used with bazel. The
issue is that cmake calls /usr/sbin/sysctl to determine the host
architecture:

https://gitlab.kitware.com/cmake/cmake/-/blob/6453bd046ef23798c64333042d76851f15eff2fc/Modules/Platform/Darwin-Initialize.cmake#L23-41

And then it uses that computed value here if no other archs are set:

https://gitlab.kitware.com/cmake/cmake/-/blob/6453bd046ef23798c64333042d76851f15eff2fc/Source/cmGeneratorTarget.cxx#L3378-3399

As far as I can tell this is a mismatch in expectation between how we
call cmake vs how you would normally call it for cross compilation since
we do not set CMAKE_SYSTEM_NAME #523

By setting CMAKE_OSX_ARCHITECTURES ourselves to the target arch, we
stop cmake from checking this default value. To get the target arch we
use the Apple fragment, which as far as I can tell is the only way to
get this from a bazel API. Alternatively we could theoretically parse
the command line args and extract the arch from the -target but that
felt more fragile. This option is ignored on non-Apple platforms, and
it's conditional on bazel's Apple logic, so it shouldn't affect any
other cases.

This also updates the Apple related test to catch this case, which
requires linking, which the ios_build_test was not doing. For now it
also uses apple_binary to force the transition, which means rules_apple
isn't required.

This fixes a bug where on M1 macOS machines, when building for the
x86_64 iOS simulator, the cmake dependencies were building for arm64
instead, _if_ you had `/usr/sbin` in your `$PATH` used with bazel. The
issue is that cmake calls `/usr/sbin/sysctl` to determine the host
architecture:

https://gitlab.kitware.com/cmake/cmake/-/blob/6453bd046ef23798c64333042d76851f15eff2fc/Modules/Platform/Darwin-Initialize.cmake#L23-41

And then it uses that computed value here if no other archs are set:

https://gitlab.kitware.com/cmake/cmake/-/blob/6453bd046ef23798c64333042d76851f15eff2fc/Source/cmGeneratorTarget.cxx#L3378-3399

As far as I can tell this is a mismatch in expectation between how we
call cmake vs how you would normally call it for cross compilation since
we do not set `CMAKE_SYSTEM_NAME` bazel-contrib#523

By setting `CMAKE_OSX_ARCHITECTURES` ourselves to the target arch, we
stop cmake from checking this default value. To get the target arch we
use the Apple fragment, which as far as I can tell is the only way to
get this from a bazel API. Alternatively we could theoretically parse
the command line args and extract the arch from the `-target` but that
felt more fragile. This option is ignored on non-Apple platforms, and
it's conditional on bazel's Apple logic, so it shouldn't affect any
other cases.

This also updates the Apple related test to catch this case, which
requires linking, which the ios_build_test was not doing. For now it
also uses apple_binary to force the transition, which means rules_apple
isn't required.
Copy link
Member

@jsharpe jsharpe left a comment

Choose a reason for hiding this comment

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

Thanks @keith LGTM.

@jsharpe jsharpe merged commit e0590b5 into bazel-contrib:main Apr 16, 2022
@keith
Copy link
Member Author

keith commented Apr 16, 2022

Thanks!

@keith keith deleted the ks/set-cmake_osx_architectures-to-target-architecture branch April 20, 2022 17:29
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

Successfully merging this pull request may close these issues.

2 participants