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] Handle multiple flags in ADDITIONAL_COMPILE_FLAGS properly #2

Conversation

stephanosio
Copy link
Member

@stephanosio stephanosio commented Oct 17, 2024

When multiple space-separated compile flags are specified in an ADDITIONAL_COMPILE_FLAGS cache string, the resulting flags are enclosed by double quotes because ADDITIONAL_COMPILE_FLAGS is a string (i.e. not a list) and CMake target_compile_options treats the multiple space-separated arguments as single argument containing spaces.

For example, when libcxx is configured with the following multiple space-separated additional compile flags:

cmake ... "-DLIBCXX_ADDITIONAL_COMPILE_FLAGS=--flag1 --flag2" ...

The resulting compiler command line is as follows:

cc ... "--flag1 --flag2" ...

The above can be problematic for some compilers -- for instance, GCC treats it as a file path and prints out an error.

This patch, by calling separate_arguments on
ADDITIONAL_COMPILE_FLAGS to convert it into the standard semicolon-separated list form, which is properly handled by target_compile_options, ensures that multiple compile flags are handled as such.

With this change, the resulting compiler command line is as follows:

cc ... --flag1 --flag2 ...

When multiple space-separated compile flags are specified in an
`ADDITIONAL_COMPILE_FLAGS` cache string, the resulting flags are
enclosed by double quotes because `ADDITIONAL_COMPILE_FLAGS` is a
string (i.e. not a list) and CMake `target_compile_options` treats the
multiple space-separated arguments as single argument containing spaces.

For example, when libcxx is configured with the following multiple
space-separated additional compile flags:

    cmake ... "-DLIBCXX_ADDITIONAL_COMPILE_FLAGS=--flag1 --flag2" ...

The resulting compiler command line is as follows:

    cc ... "--flag1 --flag2" ...

The above can be problematic for some compilers -- for instance, GCC
treats it as a file path and prints out an error.

This patch, by calling `separate_arguments` on
`ADDITIONAL_COMPILE_FLAGS` to convert it into the standard
semicolon-separated list form, which is properly handled by
`target_compile_options`, ensures that multiple compile flags are
handled as such.

With this change, the resulting compiler command line is as follows:

    cc ... --flag1 --flag2 ...

Signed-off-by: Stephanos Ioannidis <[email protected]>
@stephanosio stephanosio force-pushed the additional_cflags_separate_args branch from 96a47c7 to cbec1a7 Compare October 17, 2024 12:38
@stephanosio stephanosio changed the title [CMake] Handle multiple flags in ADDITIONAL_COMPILER_FLAGS properly [CMake] Handle multiple flags in ADDITIONAL_COMPILE_FLAGS properly Oct 17, 2024
@stephanosio
Copy link
Member Author

Upstreamed in llvm#112703

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.

1 participant