0

sorry for my bad english in advance.

I try to run the Bash command from here: https://stackoverflow.com/a/45786694/11698553 in a cmake add_custom_command:

add_custom_command(TARGET ${x}
                    PRE_BUILD
                    COMMAND bash -c "$ cpphs --nowarn --nomacro -I./ ${source} | sed -E 's|#line 1 \"missing file: (.*)\"|#include <\\1>|' > ${CMAKE_SOURCE_DIR}/out.txt"
                    COMMAND bash -c "$ cpphs --nowarn --nomacro -I./ ${source}  > ${CMAKE_SOURCE_DIR}/out.txt"
                    COMMAND bash -c "$ cpphs --nowarn --nomacro -I./ \"${source}\" | sed -E 's|#line 1 \"missing file: (.*)\"|#include <\\1>|'"
                    DEPENDENCY ${x}
                    #cpphs --nowarn --nomacro -I./ choclate.cpp | sed -E 's|#line 1 "missing file: (.*)"|#include<\1>|'| sed  -E '/#line [0-9]+ ".*"/d' > out.txt
                    COMMENT "create sourcefiles"
                    ) 

I use Windows with WSL and when I try the commands in the Ubuntu shell they work and vice versa the bash commands which only worked under unix worked normally in cmake.

When I try those commands I got this error Messages with the first Bashcommand: /bin/sh: 1: sed -E s|#line\ 1\ "missing\ file:\ (.*)"|#include\ <\1>| : not found the second Bash command: /bin/sh: 1: cannot create /mnt/c/path/to/output/out.txt: Directory nonexistent(but the directory is existent) the third command: /bin/sh: 1: sed -E s|#line\ 1\ "missing\ file:\ (.*)"|#include\ <\1>|: not found (I commented the other two lines always out to get the error messages)

the command sed --help worked in cmake, so cmake knows sed.

What am I doing wrong?

When you need the complete CMakelists.txt file for more context:



#add_executable(challenge_0-demo example.cpp)
file(GLOB_RECURSE APP_SOURCES *.cpp )
message("Start creating Targets for: ${APP_SOURCES}")
foreach( testsourcefile ${APP_SOURCES} )
    if (NOT TARGET mylib)
        get_filename_component(lib_we ${testsourcefile} NAME_WE)
        message("Created new Target ${lib_we} for ${testsourcefile}")
        add_executable(${lib_we} ${testsourcefile})
        file(SIZE ${testsourcefile} fsize)
        if(fsize EQUAL 0)
            file(READ ../.template.cpp content)
            file(WRITE ${testsourcefile} "${content}")
            message("Template have been copied to ${lib_we}")
        endif()
    endif()
endforeach()
get_property(tars DIRECTORY ${dir} PROPERTY BUILDSYSTEM_TARGETS)
foreach(x ${tars})
    get_target_property(source ${x} SOURCES)
    list(LENGTH source len)
    if (${len} EQUAL 1)
        if( NOT ${source} STREQUAL MY_PROJECT_SOURCES-NOTFOUND)
            message("${x}")
            add_custom_command(TARGET ${x}
                    PRE_BUILD
                    #COMMAND bash -c "cpp  ${source} out.txt
                    #COMMAND bash -c "$ cpphs --nowarn --nomacro -I./ \"${source}\" | sed -E 's|#line 1 \"missing file: (.*)\"|#include <\\1>|' > ${CMAKE_SOURCE_DIR}/out.txt"
                    #COMMAND bash -c "$ cpphs --nowarn --nomacro -I./ \"${source}\"  > ${CMAKE_SOURCE_DIR}/out.txt"
                    COMMAND bash -c "$ cpphs --nowarn --nomacro -I./ \"${source}\" | sed -E 's|#line 1 \"missing file: (.*)\"|#include <\\1>|'"# > ${CMAKE_SOURCE_DIR}/out.txt"
                    COMMAND bash -c "$ cpphs --nowarn --nomacro -I./ \"${source}\" | sed -E 's|#line 1 \"missing file: (.*)\"|#include <\\1>|'"# > ${CMAKE_SOURCE_DIR}/out.txt"
                    DEPENDENCY ${x}
                    #cpphs --nowarn --nomacro -I./ choclate.cpp | sed -E 's|#line 1 "missing file: (.*)"|#include<\1>|'| sed  -E '/#line [0-9]+ ".*"/d' > out.txt
                    COMMENT "create sourcefiles"
                    )
        endif ()
    endif ()
endforeach()

Thank you in advance for any answer.

wert wert
  • 1
  • 1
  • The part generating the error does not use `bash` but `sh`. – Cyrus Oct 30 '21 at 22:56
  • @Cyrus Thank you, but I don't think that this is making a difference. – wert wert Oct 30 '21 at 22:59
  • As you can see from the `not found` error message, bash treats the whole `sed` invocation with parameters as command name. This is because of double quotes. Note, that double quotes has a special meaning in CMake, so escaping ``\"`` escapes the quotes only for CMake, not for the bash. You could use additional level of escaping (like `\\\"`) but it is better to reduce level of nesting commands. E.g. all shells supports redirection and piping, so there is no need to use `bash -c` for select specifically bash. – Tsyvarev Oct 31 '21 at 12:29
  • @Tsyvarev Thank you for your help, but i got another Proplem, i have wrote this command now: ```COMMAND cpphs --nowarn --nomacro -I./ ${source} > ${CMAKE_CURRENT_SOURCE_DIR}/out.txt``` it makes a strange and large file, but without the ```> ${CMAKE_CURRENT_SOURCE_DIR}/out.txt``` it prints out everything correctly. – wert wert Oct 31 '21 at 12:44
  • "it makes a strange and large file" - This is very vague description of the problem. If you suspect that redirection works incorrectly in the default shell, then instead of complex program try "toy" examples. E.g. `COMMAND echo 123 > ${CMAKE_CURRENT_SOURCE_DIR}/out.txt`. – Tsyvarev Oct 31 '21 at 12:53
  • Sorry for the vague description, I now have the new error, it thinks the next line, is also a shell commad and thinks: ```DEPENDENCY ${x}``` is a parameter for the shell, how can i indicate the end of the shell command? because when i run this ```COMMAND echo 123 > ${CMAKE_CURRENT_SOURCE_DIR}/out.txt``` the file contains: ```123 DEPENDENCY choclate ``` – wert wert Oct 31 '21 at 13:06

0 Answers0