0

I'm running into a very strange issue with CMake list(APPEND ...). My script is the following (just an example):

cmake_minimum_required(VERSION 3.20)
include(CMakePrintHelpers)

project(exe)
file(WRITE ${PROJECT_NAME}.cpp "#include <iostream>
#include \"test1.h\"
#include \"test2.h\"
void main() {
    std::cout << \"Hello world!\" << std::endl;
}")
add_executable(${PROJECT_NAME} "exe.cpp")

set(COMMANDS "echo /* empty file */ > test1.h"
             "echo /* empty file */ > test2.h")

foreach(CMD IN LISTS COMMANDS)
    list(APPEND CMDS COMMAND "cmd.exe /c \"${CMD}\"")
endforeach()

cmake_print_variables(PROJECT_NAME CMDS)
add_custom_target(${PROJECT_NAME}_headers1
                    ${CMDS}
                    BYPRODUCTS test1.h test2.h
                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                    VERBATIM)
add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}_headers1)

set(CMDS COMMAND cmd.exe /c "echo /* empty file */ > test1.h"
         COMMAND cmd.exe /c "echo /* empty file */ > test2.h")
cmake_print_variables(PROJECT_NAME CMDS)
add_custom_target(${PROJECT_NAME}_headers2
                    ${CMDS}
                    BYPRODUCTS test1.h test2.h
                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                    VERBATIM)
add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}_headers2)

Generating produce the following output:

d:\play\cmake>cmake.exe -G"Ninja" -S. -B"build/ninja"
-- PROJECT_NAME="exe" ; CMDS="COMMAND;cmd.exe /c "echo /* empty file */ > test1.h";COMMAND;cmd.exe /c "echo /* empty file */ > test2.h""
-- PROJECT_NAME="exe" ; CMDS="COMMAND;cmd.exe;/c;echo /* empty file */ > test1.h;COMMAND;cmd.exe;/c;echo /* empty file */ > test2.h"
-- Configuring done
-- Generating done
-- Build files have been written to: D:/play/cmake/build/ninja

I expected that both custom targets will run the same commands. However, build.ninja file process each custom target differently:

#############################################
# Custom command for CMakeFiles\exe_headers1

build CMakeFiles\exe_headers1 test1.h test2.h: CUSTOM_COMMAND
  COMMAND = cmd.exe /C "cd /D D:\play\cmake && PRE_BUILD && "cmd.exe \c \"echo \* empty file *\ > test1.h\"" && "cmd.exe \c \"echo \* empty file *\ > test2.h\"""
  restat = 1


#############################################
# Custom command for CMakeFiles\exe_headers2

build CMakeFiles\exe_headers2 test1.h test2.h: CUSTOM_COMMAND
  COMMAND = cmd.exe /C "cd /D D:\play\cmake && PRE_BUILD && cmd.exe /c "echo /* empty file */ > test1.h" && cmd.exe /c "echo /* empty file */ > test2.h""
  restat = 1

Does anyone knows how to prevent list(APPEND ...) from "flipping" slash ("/") to backslash ("\")? What's even stranger is that cmake_print_variables(...) output is not affected.

Thanks in advance. -Uri

Uri
  • 479
  • 1
  • 4
  • 10
  • 1
    "I expected that both custom targets will run the same commands." - Eh? Why do you expect targets to run the **same** commands when even output of `cmake_print_variables` **differs**? As you can see, in the second output `;` correctly separates executable and all its arguments. But in the first output the whole string `cmd.exe /c "echo /* empty file */ > test1.h` is interpreted as a **single parameter**. It is wrong to pass such string as `COMMAND`. – Tsyvarev Jun 17 '21 at 23:20
  • This is not what I'm referring to. Please notice that exe_headers1 is *"cmd.exe \c \"echo \* empty file *\ > test1.h\""..." and exe_headers2 is *cmd.exe /c "echo /* empty file */ > test1.h"...*. The slashes are flipped. – Uri Jun 18 '21 at 00:16
  • Yes, I noticed that in the first case you got unexpected backslashes. But fixing these slashes alone has a little sense: you need to fix a **wrong** parameter which you have passed as COMMAND in that case. This is what my previous comment talked about. – Tsyvarev Jun 18 '21 at 00:39
  • Not sure if this has anything to do with the issue, but you've got the `COMMAND` part of [`add_custom_target`](https://cmake.org/cmake/help/latest/command/add_custom_target.html#command:add_custom_target) wrong; the program and the parameters are separate entries of the list so inside the `foreach()` you should use `list(APPEND CMDS COMMAND cmd.exe /c ${CMD})`; Btw: I prefer calling cmake scripts to do something like this header generation, since it's works independent of platform `COMMAND ${CMAKE_COMMAND} -P myScript.cmake` – fabian Jun 18 '21 at 16:04

0 Answers0