Skip to content

Detect NEON support more reliably in environment.cmake #136

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

louwers
Copy link

@louwers louwers commented Apr 10, 2025

We are building for iOS and CMAKE_GENERATOR_PLATFORM is an empty string. So we ran into an issue that SUPPORT_NEON was not set correctly. The CMake documentation also mentions:

In many cases, this will correspond to the target architecture for the build, but this is not guaranteed.

This is the solution DeepSeek came up with to more reliably detect NEON support. It looks legit? Seems to work as well.

@lemire
Copy link
Member

lemire commented Apr 10, 2025

Running tests.

@louwers
Copy link
Author

louwers commented Apr 11, 2025

Will this work when cross-compiling?

@lemire
Copy link
Member

lemire commented Apr 11, 2025

@louwers Reading the documentation you referenced, I see...

When cross-compiling, a CMAKE_TOOLCHAIN_FILE should set the CMAKE_SYSTEM_PROCESSOR variable to match target architecture that it specifies (via CMAKE__COMPILER and perhaps CMAKE__COMPILER_TARGET).

I don't think that folks run cmake builds on iOS typically.

When you write 'We are building for iOS', I take it that you are cross-compiling. Can you share your CMAKE_TOOLCHAIN_FILE?

@TimSylvester
Copy link

@lemire This came up building iOS/arm64 from MacOS/arm64, but I don't think we specify that value. These are the platform-related values I get:

-- CMAKE_TOOLCHAIN_FILE=
-- CMAKE_SYSTEM_NAME=iOS
-- CMAKE_SYSTEM_VERSION=
-- CMAKE_HOST_SYSTEM_NAME=Darwin
-- CMAKE_HOST_SYSTEM_VERSION=
-- CMAKE_HOST_UNIX=1
-- CMAKE_HOST_APPLE=1
-- CMAKE_SYSTEM_PROCESSOR=
-- CMAKE_HOST_SYSTEM_PROCESSOR=
-- CMAKE_APPLE_SILICON_PROCESSOR=
-- CMAKE_OSX_ARCHITECTURES=
-- CMAKE_OSX_SYSROOT=iphoneos
-- CMAKE_EXECUTABLE_FORMAT=MACHO
-- COMPILER_SUPPORTS_MARCH_NATIVE=1

@louwers
Copy link
Author

louwers commented Apr 11, 2025

Building for iOS on modern hardware does not require cross-compiling.

@lemire
Copy link
Member

lemire commented Apr 11, 2025

@louwers

Building for iOS on modern hardware does not require cross-compiling.

So you are building directly under iOS? You are running cmake on the iOS system? Do you have a reference for how this is done? I have never heard of cmake running under iOS although I suppose that's possible.

I have built software for iOS and I did it from macOS. Is this not your case?

If you are building under macOS (e.g., with Xcode) for iOS using cmake, then you are cross-compiling. And you need a toolchain file.

@TimSylvester

I understand that you have not set CMAKE_SYSTEM_PROCESSOR. But I submit to you that it might be a mistake. My belief is that when cross-compiling, one should do something like this...

cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=/path/to/ios.toolchain.cmake

And the toolchain file should contain the relevant information such as...

set(CMAKE_SYSTEM_NAME iOS)
set(CMAKE_SYSTEM_VERSION 15.0)
set(CMAKE_SYSTEM_PROCESSOR arm64)

... among others...

It may work if you omit the CMAKE_SYSTEM_PROCESSOR parameter, but that's still a mistake to omit it.

There are examples...

@louwers
Copy link
Author

louwers commented Apr 11, 2025

Sorry for the confusion, yes we develop on macOS for iOS.

In this case both platforms are ARM64, is that cross-compiling?

We're setting CMAKE_SYSTEM_NAME directly.

@lemire
Copy link
Member

lemire commented Apr 11, 2025

Cross-compiling is the process of compiling code on one platform (the host) to generate executable code for a different platform (the target). The host and the target can different in different ways: processor architecture, operating system, and so forth. A typical cross-compiling scenario is when you are using a PC or a mac to build code for a mobile or embedded device. In this instance, macOS and iOS are different operating systems, even if the processor architecture is the same.

Thus I recommend you build a toolchain file. It should resolve the issue you have encountered and make your build more robust in general. I recommend checking in your toolchain file in your version control system.

@louwers
Copy link
Author

louwers commented Apr 11, 2025

Seems like there is a good toolchain file available for iOS. https://github.com/leetal/ios-cmake/blob/2d171c3585082c9be0c5311fa3930b7fe7c479e2/ios.toolchain.cmake#L846

I will look into it, would be convenient if we could merge this though. I think it's a good solution.

@lemire
Copy link
Member

lemire commented Apr 11, 2025

I am concerned that this PR only masks the real issue.

@louwers
Copy link
Author

louwers commented Apr 11, 2025

OK that is fair enough, @TimSylvester and I can add a proper toolchain file to our project. 👍

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.

3 participants