Skip to content

[llvm] Fix cmake string expansion in CrossCompile.cmake #138901

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 1 commit into
base: main
Choose a base branch
from

Conversation

DavidTruby
Copy link
Member

This fixes an issue where builds on Windows with LLVM_OPTIMIZED_TABLEGEN
will fail if CMAKE_MAKE_PROGRAM, CMAKE_C_COMPILER_LAUNCHER or
CMAKE_CXX_COMPILER_LAUNCHER are lists rather than paths to a binary;
this occurs when one of these programs is passed with arguments instead
of just as a path.

On non-Windows this works regardless as the sub-cmake command runs in a
proper shell, but on Windows it runs differently and so these strings
need to be expanded correctly by cmake.

This fixes an issue where builds on Windows with LLVM_OPTIMIZED_TABLEGEN
will fail if CMAKE_MAKE_PROGRAM, CMAKE_C_COMPILER_LAUNCHER or
CMAKE_CXX_COMPILER_LAUNCHER are lists rather than paths to a binary;
this occurs when one of these programs is passed with arguments instead
of just as a path.

On non-Windows this works regardless as the sub-cmake command runs in a
proper shell, but on Windows it runs differently and so these strings
need to be expanded correctly by cmake.
@llvmbot llvmbot added the cmake Build system in general and CMake in particular label May 7, 2025
@DavidTruby
Copy link
Member Author

This is necessary because CMake won't expand a list into string if the quoted string isn't surrounded by whitespace. If it is, it will correctly expand and add quotes around the resulting string, so moving the string to cover the whole "-D..." option will make cmake do the right thing here.

-DCMAKE_MAKE_PROGRAM="${CMAKE_MAKE_PROGRAM}"
-DCMAKE_C_COMPILER_LAUNCHER="${CMAKE_C_COMPILER_LAUNCHER}"
-DCMAKE_CXX_COMPILER_LAUNCHER="${CMAKE_CXX_COMPILER_LAUNCHER}"
"-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}"
Copy link
Member

Choose a reason for hiding this comment

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

Do you have an example where you'd want to pass flags to the native build system?

Copy link
Member Author

@DavidTruby DavidTruby May 7, 2025

Choose a reason for hiding this comment

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

I didn't have one specifically for the CMAKE_MAKE_PROGRAM, I just thought I'd fx it as well while I was here as the same reasoning would apply there.
We want this for CMAKE_C/CXX_COMPILER_LAUNCHER, where we want to add flags to ccache when using LLVM_CCACHE_BUILD to control certain behaviour especially when building flang. When we tried to make that change it worked on non-Windows but didn't work on Windows, this fixes at least that case. So that's the specific motivation for the change.

Copy link
Member Author

Choose a reason for hiding this comment

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

I suppose I can imagine a case where you might want to enable some debug modes in ninja with -d ...

Copy link
Member

@petrhosek petrhosek left a comment

Choose a reason for hiding this comment

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

Thanks, we have other places in LLVM where we set these variables and ideally we would make the same change consistently everywhere.

-DCMAKE_CXX_COMPILER_LAUNCHER="${CMAKE_CXX_COMPILER_LAUNCHER}"
"-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}"
"-DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER}"
"-DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER}"
Copy link
Contributor

@s-barannikov s-barannikov May 7, 2025

Choose a reason for hiding this comment

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

LLVM_TABLEGEN_FLAGS below should also be fixed (added recently, #138086).

Copy link
Member

@Meinersbur Meinersbur left a comment

Choose a reason for hiding this comment

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

I think the difference is that the surrounding quotes are handled by CMake itself, so they are never seen when passing them as a command line string.

CMake indeed does not use always use cmd.exe to launch programs, but usually it normalizes paths to absolute paths and using forward slashes instead of backslashes, at least for CMAKE_*_COMPILER. Might be different for others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmake Build system in general and CMake in particular
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants