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.