[CMake] Does the echo command use the system shell?
Michael Wild
themiwi at gmail.com
Thu Jul 29 03:46:22 EDT 2010
On 29. Jul, 2010, at 9:06 , Óscar Fuentes wrote:
> Michael Wild <themiwi at gmail.com> writes:
>
>> The problem is, CMake has to go through the system shell. Of course,
>> CMake could write the command to a file and then invoke a custom
>> interpreter from the system shell, but that would probably be very
>> inefficient and would require CMake to implement a full shell
>> language.
>
> Another option for cmake is to appropriately quote the string so the
> system shell does not alter it. Consider
>
> cmake -E echo "#hello"
>
> cmake would put on the Unix makefile something like
>
> cmake -E echo "\#hello"
>
> and on windows MinGW makefile or VS project it would be
>
> cmake -E echo "#hello"
>
> [snip]
>
>> But we still don't know what your general goal is, WHY you need this
>> and why e.g. a simple add_definitions() isn't enough (because so far I
>> haven't seen anything that requires you to do this stuff dynamically
>> at build-time).
>
> This is not the issue. As mentioned on the previous posts, the question
> is how much confidence can I have on the platform independence of the -E
> commands. The stated goal is to provide platform isolation. We have seen
> that this is not completely true.
>
> But if you insist on knowing the context that raised the issue, it
> consisted on the requirement of creating a header file containing a
> #define with a quoted list of the names of the source files of the
> project. configure_file is a solution, but requires having a
> pre-existing .in file and I'll like to avoid polluting the source tree
> as much as possible. Then add_custom_command seemed a good fit.
> Maintenance-wise, it is simpler than configure_file+file.in or
> file(WRITE+configure_file and has the added feature of regenerating the
> file if it is deleted by mistake.
>
> So here we are.
So, now THAT is an answer (I'll ignore the first part for my own health). Now, writing the list of source files to a header really doesn't need to happen at compile time since it is known at CMake-time. Try this:
project(hello C)
cmake_minimum_required(VERSION 2.8)
set(HELLO_SRCS hello.c hello.h main.c)
# generate hello_srcs_list.c
set(HELLO_LIST_FILE ${CMAKE_CURRENT_BINARY_DIR}/hello_srcs_list.c)
string(REPLACE ";" "\", \"" HELLO_SRCS_LIST "${HELLO_SRCS}")
file(WRITE ${HELLO_LIST_FILE}
"/* Automatically generated by CMake. DO NOT EDIT! */\n\n"
"const char* HELLO_SRCS[] = {\"${HELLO_SRCS_LIST}\", 0};\n")
set_source_files_properties(${HELLO_LIST_FILE} PROPERTIES
GENERATED TRUE)
add_executable(hello ${HELLO_SRCS} ${HELLO_LIST_FILE})
Michael
More information about the CMake
mailing list