Generator expression with whitespace works in target_compile_options but not in add_compile_options
I tried to convert https://cmake.org/cmake/help/latest/command/add_compile_options.html#example to use a generator expression instead and found some unexpected behavior. See comments in the following file.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.21)
project(Project C)
# Uncomment one of the following statements:
# 1. Build fails as expected due to warnings that are errors:
# add_compile_options(-Wall -Wextra -pedantic -Werror)
# 2. Build fails as expected due to warnings that are errors but multiple generator expressions need to be used:
add_compile_options($<$<C_COMPILER_ID:GNU>:-Wall> $<$<C_COMPILER_ID:GNU>:-Wextra> $<$<C_COMPILER_ID:GNU>:-pedantic> $<$<C_COMPILER_ID:GNU>:-Werror>)
# 3.1. Build fails but due to an invalid command with `"$<1:-Wall" -Wextra -pedantic "-Werror>" -Wall -Werror` even though the generator expression is the same as in point 4:
# add_compile_options($<$<C_COMPILER_ID:GNU>:-Wall -Wextra -pedantic -Werror>)
# 3.2. Build fails but the command includes `"\"-Wall -Wextra -pedantic -Werror\"" -Wall -Wextra -pedantic -Werror`:
# add_compile_options($<$<C_COMPILER_ID:GNU>:"-Wall -Wextra -pedantic -Werror">)
# 3.3. Build fails but due to an invalid command with "-Wall -Wextra -pedantic -Werror" -Wall -Wextra -pedantic -Werror:
# add_compile_options("$<$<C_COMPILER_ID:GNU>:-Wall -Wextra -pedantic -Werror>")
add_executable(Executable main.c)
# 4. Build fails as expected due to warnings that are errors even though the generator expression is the same as in point 3.1:
# target_compile_options(Executable PRIVATE $<$<C_COMPILER_ID:GNU>:-Wall -Wextra -pedantic -Werror>)
main.c:
int main(int argc, const char* argv[]) {
// Expect unused parameter warnings or errors if warnings are errors.
return 0;
}
Command: cmake -S . -B build -G Ninja -D CMAKE_C_COMPILER=gcc && cmake --build build --verbose
OS: Windows 10
CMake version: 3.23.3