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] Automatically add required cross-compilation variables #130

Merged
merged 2 commits into from
Jul 18, 2023

Conversation

stanhu
Copy link
Contributor

@stanhu stanhu commented Jul 17, 2023

For the standard make, mini_portile provides the configure script with the --host parameter, and the script figures out the target compilers to use. With CMake, we don't have that benefit so we need to set the C and C++ compilers ourselves.

This commit uses a simple heuristic to find the target compilers:

  1. For macOS, use clang and clang++. Otherwise use gcc and gcc++.
  2. We search the PATH for <host>-compiler. Use that if we find it. Otherwise default to the base compilers.

To make CMake work with cross-compilation, a number of variables have to be set:

  • CMAKE_SYSTEM_PROCESSOR
  • CMAKE_SYSTEM_NAME
  • CMAKE_C_COMPILER
  • CMAKE_CXX_COMPILER

Closes #128

@stanhu stanhu force-pushed the sh-cmake-cross-compile-vars branch 2 times, most recently from 06092d7 to c76c45d Compare July 18, 2023 05:22
For the standard `make`, mini_portile provides the `configure` script
with the `--host` parameter, and the script figures out the target
compilers to use. With CMake, we don't have that benefit so we need to
set the C and C++ compilers ourselves.

This commit uses a simple heuristic to find the target compilers:

1. For macOS, use `clang` and `clang++`. Otherwise use `gcc` and
   `gcc++`.
2. We search the PATH for `<host>-compiler`. Use that if we find
it. Otherwise default to the base compilers.

To make CMake work with cross-compilation, a number of variables have
to be set:

- CMAKE_SYSTEM_PROCESSOR
- CMAKE_SYSTEM_NAME
- CMAKE_C_COMPILER
- CMAKE_CXX_COMPILER

Closes flavorjones#128
@stanhu stanhu force-pushed the sh-cmake-cross-compile-vars branch from c76c45d to 527a813 Compare July 18, 2023 05:38
Ruby compiled for x64-mingw-ucrt appears to return a processor type of
x64, but CMake only understands x86_64.
@flavorjones
Copy link
Owner

This looks great! Thank you!

@flavorjones flavorjones merged commit f2ad37b into flavorjones:main Jul 18, 2023
@flavorjones
Copy link
Owner

stanhu added a commit to stanhu/re2 that referenced this pull request Jul 18, 2023
This pulls in flavorjones/mini_portile#130.
We no longer need to write custom logic for finding the right
compilers and setting CMake variables for cross-compilation.
@larskanis
Copy link
Collaborator

Great addition! Thank you!

mudge pushed a commit to mudge/re2 that referenced this pull request Jul 25, 2023
This pulls in flavorjones/mini_portile#130.
We no longer need to write custom logic for finding the right
compilers and setting CMake variables for cross-compilation.
mudge pushed a commit to mudge/re2 that referenced this pull request Sep 13, 2023
This pulls in flavorjones/mini_portile#130.
We no longer need to write custom logic for finding the right
compilers and setting CMake variables for cross-compilation.
@flavorjones
Copy link
Owner

@stanhu I'm curious why we're not using RbConfig::CONFIG["CC"] and ["CXX"] here ... would that simplify things?

Asking because OpenBSD and FreeBSD support have been ... interesting.

@flavorjones flavorjones mentioned this pull request May 3, 2024
@stanhu
Copy link
Contributor Author

stanhu commented May 5, 2024

I'm curious why we're not using RbConfig::CONFIG["CC"] and ["CXX"] here ... would that simplify things?

@flavorjones Yeah, that could would work. It looks like mini_portile2 uses RbConfig::CONFIG['CC'] here, so maybe we can simplify this further by using gcc_cmd and introducing something like gxx_cmd:

(ENV["CC"] || @gcc_command || RbConfig::CONFIG["CC"] || "gcc").dup

@stanhu
Copy link
Contributor Author

stanhu commented May 5, 2024

I think I might have been focused on ENV["CC"] because that's what configure uses. I think I defaulted to clang for macOS because the cross-compile images for macOS has compilers with clang-#{host} (e.g. clang-arm64-apple-darwin22). But I think the cross-compile images should have reasonable values for RbConfig::CONFIG["CC"] that match with the image, so that approach should work.

flavorjones added a commit that referenced this pull request May 29, 2024
and use it for configuring CMake.

Related to #141 and #130.
@flavorjones
Copy link
Owner

I've opened a PR to simplify compiler detection at #144

flavorjones added a commit that referenced this pull request May 29, 2024
and use it for configuring CMake.

Related to #141 and #130.
flavorjones added a commit that referenced this pull request May 29, 2024
and use it for configuring CMake.

Related to #141 and #130.
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.

[cmake] Support cross-compiling out of the box
3 participants