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

cmake: allow cross-CPU builds via LIBRESSL_CPU #935

Closed
wants to merge 1 commit into from

Conversation

vszakats
Copy link
Contributor

@vszakats vszakats commented Nov 6, 2023

If LIBRESSL_CPU is not set, honor existing CMAKE_OSX_ARCHITECTURE for Apple targets. Then fall back to CMAKE_C_COMPILER_TARGET (typically a triplet) if set. Otherwise fall back to the value that was used before this patch: CMAKE_SYSTEM_PROCESSOR.

This allows customizing the target CPU, e.g. on Linux.

I'm not certain of the exact CMake rules for this, but on some platforms (e.g. Linux) it is not possible to override the default CMAKE_SYSTEM_PROCESSOR value by passing it to cmake via -DCMAKE_SYSTEM_PROCESSOR=... or -DCMAKE_TOOLCHAIN_FILE=... Meaning, before this patch it was not possible to build for a CPU other than the host one on Linux for example.

Also:

  • simplify syntax in STREQUAL and MATCH expressions in the lines touched.
  • display target CPU and ABI in the configuration phase.

If `LIBRESSL_CPU` is not set, honor existing `CMAKE_OSX_ARCHITECTURE`
for Apple targets. Then fall back to `CMAKE_C_COMPILER_TARGET` if set.
Otherwise fall back to the value that was used before this patch:
`CMAKE_SYSTEM_PROCESSOR`.

This allows customizing the target CPU, e.g. on Linux.

I'm not certain of the exact CMake rules for this, but on some platforms
(e.g. Linux) it is not possible to override the default
`CMAKE_SYSTEM_PROCESSOR` value by passing it to cmake via
`-DCMAKE_SYSTEM_PROCESSOR=...`. Meaning, before this patch it was not
possible to build for a CPU other than the host one.

Also:
- simplify syntax in `STREQUAL` and `MATCH` expressions in the lines
  touched.
- display the target CPU and ABI in the configuration phase.
@vszakats
Copy link
Contributor Author

vszakats commented Nov 7, 2023

@busterb
Copy link
Contributor

busterb commented Nov 7, 2023

Thanks. Does that mean the cross compile tests in this project, scripts/test, aren't doing what we thought they were with Cmake?

@vszakats
Copy link
Contributor Author

vszakats commented Nov 7, 2023

I haven't tried that script, but after a quick look it seems to use CMAKE_OSX_ARCHITECTURES for macOS, which was fixed recently and works OK. And CMAKE_TOOLCHAIN_FILE with Windows. With Windows, CMAKE_SYSTEM_PROCESSOR override does work. The toolchain files setting it most likely also work. As for Android, I have no idea.

FWIW when the CPU was mismatched in a cross-build with ASM enabled, it manifested in clear build failures in my Linux tests.

@vszakats
Copy link
Contributor Author

vszakats commented Nov 7, 2023

After inspecting the CMake source code 1, it seems that if CMAKE_SYSTEM_NAME is set, CMake would allow overriding CMAKE_SYSTEM_PROCESSOR. I still have to test this and investigate what this means for all the build combinations.

Footnotes

  1. https://github.com/Kitware/CMake/blob/4c52349b9e08e56bc594d10b6462f59b05d7c8c8/Modules/CMakeDetermineSystem.cmake#L184

vszakats added a commit to curl/curl-for-win that referenced this pull request Nov 7, 2023
Turns out `CMAKE_SYSTEM_PROCESSOR` is not overridable on Linux
for example.

Patch submitted upstream to fix cross-builds:
libressl/portable#935
@vszakats
Copy link
Contributor Author

vszakats commented Nov 7, 2023

It was useful to take a second look, because the solution is to set CMAKE_SYSTEM_NAME first. Then CMake allows to override CMAKE_SYSTEM_PROCESSOR to a custom value:
https://github.com/curl/curl-for-win/actions/runs/6783897318

I run into a separate issue where arm64 made the build use arm (v4) ASM, but that's for another PR (#936). This could be fixed locally by setting aarch64 instead of arm64.

vszakats added a commit to vszakats/libressl-portable that referenced this pull request Nov 7, 2023
@vszakats vszakats closed this Nov 7, 2023
@vszakats vszakats deleted the cmake-cross-cpu branch November 7, 2023 13:09
@busterb
Copy link
Contributor

busterb commented Nov 7, 2023

Thanks for digging into the logic here. This is potentially more convenient than what CMake provides out of the box. Just wanted to make sure this was solving the right problem.

@vszakats
Copy link
Contributor Author

vszakats commented Nov 7, 2023

No worries, I was also happy to get to the root cause. I'm still not quite sure what is the CMake best-practice here though. No objection to merge this anyway, I agree it could improve the build UX.

@vszakats
Copy link
Contributor Author

vszakats commented Nov 7, 2023

Looking into this more, a triplet doesn't work universally, because some conditions use MATCHES, and some others STREQUAL (seen with Windows) when looking for a CPU value. If you're interested in merging, let me know and I'll continue working on this (it also needs a rebase).

I think maybe the best would be to move the CPU-selection logic to the C preprocessor. This would allow to drop all this (also in autotools), while automatically adding support for building universal (multi-CPU) Apple targets.

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