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] Support cross-compiling out of the box #128

Closed
stanhu opened this issue Jul 14, 2023 · 2 comments · Fixed by #130
Closed

[cmake] Support cross-compiling out of the box #128

stanhu opened this issue Jul 14, 2023 · 2 comments · Fixed by #130

Comments

@stanhu
Copy link
Contributor

stanhu commented Jul 14, 2023

While working on mudge/re2#67, I had to add a number of configuration options to make cross-compiling work properly.

For the standard make, mini_portile2 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.

I used 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

I was surprised that CMAKE_SYSTEM_NAME has to be set, or cmake won't try to cross-compile.

I think mini_portile2 should provide these options by default.

@stanhu
Copy link
Contributor Author

stanhu commented Jul 14, 2023

In order to build a C extension against a library, we may also want to add:

  • CMAKE_POSITION_INDEPENDENT_CODE=ON

@flavorjones
Copy link
Owner

Ack. CMake is still "experimental" and you may be the first person to really try to use it for something. Will take a look, but prioritizing it behind some other work at the moment.

stanhu added a commit to stanhu/mini_portile that referenced this issue 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

`CMAKE_SYSTEM_NAME` has to be set, or `cmake` won't try to cross-compile.

Closes flavorjones#128
stanhu added a commit to stanhu/mini_portile that referenced this issue Jul 18, 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 flavorjones#128
stanhu added a commit to stanhu/mini_portile that referenced this issue Jul 18, 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 flavorjones#128
stanhu added a commit to stanhu/mini_portile that referenced this issue Jul 18, 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 flavorjones#128
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants