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

Respect CMAKE_<LANG>_COMPILE_OPTIONS_TARGET #542

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mvf
Copy link

@mvf mvf commented Jul 23, 2024

In cross builds with CMAKE_<C|CXX>_COMPILER_TARGET, a hard-coded --target= was passed to the compiler during linking. Some compilers, such as QNX's QCC wrapper, use a different parameter and the build would fail.

To fix this, take the parameter name from CMake's CMAKE_<LANG>_COMPILE_OPTIONS_TARGET, with a fallback to --target=.

The variable was added to CMake back in 2013 with the very change that added CMAKE_<LANG>_COMPILER_TARGET: Kitware/CMake@76552d5. Nowadays, many cross compilers declare this variable:

$ grep -r '_COMPILE_OPTIONS_TARGET ' cmake
cmake/Modules/CMakeSwiftInformation.cmake:44:set(CMAKE_Swift_COMPILE_OPTIONS_TARGET "-target ")
cmake/Modules/Compiler/Clang.cmake:39:      set(CMAKE_${lang}_COMPILE_OPTIONS_TARGET "-target ")
cmake/Modules/Compiler/Clang.cmake:42:      set(CMAKE_${lang}_COMPILE_OPTIONS_TARGET "--target=")
cmake/Modules/Compiler/IBMClang.cmake:33:  set(CMAKE_${lang}_COMPILE_OPTIONS_TARGET "--target=")
cmake/Modules/Compiler/IntelLLVM.cmake:83:    set(CMAKE_${lang}_COMPILE_OPTIONS_TARGET "--target=")
cmake/Modules/Compiler/LLVMFlang-Fortran.cmake:16:set(CMAKE_Fortran_COMPILE_OPTIONS_TARGET "--target=")
cmake/Modules/Compiler/QCC.cmake:11:  set(CMAKE_${lang}_COMPILE_OPTIONS_TARGET "-V")
cmake/Modules/Platform/Windows-Clang.cmake:266:    set(CMAKE_${lang}_COMPILE_OPTIONS_TARGET "-target ")
cmake/Modules/Platform/Windows-Clang.cmake:268:    set(CMAKE_${lang}_COMPILE_OPTIONS_TARGET "--target="

Required on QCC, which uses "-V" instead of "--target=".
@@ -738,8 +738,11 @@ function(_add_cargo_build out_cargo_build_out_dir)
set(cargo_target_linker $<$<BOOL:${linker}>:${cargo_target_linker_var}=${linker}>)

if(Rust_CROSSCOMPILING AND (CMAKE_C_COMPILER_TARGET OR CMAKE_CXX_COMPILER_TARGET))
string(REPLACE "," "\$<COMMA>" c_opts_target "${CMAKE_C_COMPILE_OPTIONS_TARGET}")
Copy link
Collaborator

Choose a reason for hiding this comment

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

are there any cases where CMAKE_<LANG>_COMPILE_OPTIONS_TARGET contains a comma?

Copy link
Author

Choose a reason for hiding this comment

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

No, but if there ever was one, the CMake error would be pretty cryptic. 😅

Learned this the hard way in another change I got lined up, which gives the same treatment to --sysroot. There, the QCC parameter is -Wc,-isysroot,.

It's probably fine to skip the escaping here though, if you prefer.

Copy link
Collaborator

Choose a reason for hiding this comment

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

another change I got lined up, which gives the same treatment to --sysroot

In that case, could you perhaps follow-up in the upstream CMake issue https://gitlab.kitware.com/cmake/cmake/-/issues/21382#note_852685 and clarify if CMake can officially document these two variables? Brad king seems to have indicated this is fine in the linked comment, but it would be good to have some confirmation, and even better if we could merge a PR to CMake first which documents these variables (to make them part of the official API)

It's probably fine to skip the escaping here though, if you prefer.

I would prefer skipping it unless necessary.

Copy link
Author

Choose a reason for hiding this comment

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

Nice find, I'll approach upstream to get these documented.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@mvf Did you receive any feedback from the CMake Maintainers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants