[CMake] Passing options into COMMAND inside of add_custom_target() or add_custom_command()

Martin Mitáš mity at morous.org
Sat Jun 28 06:06:56 EDT 2014


Hello,

I'm trying to pass multiple options from outside to a command in add_custom_target() 
or add_custom_command(). I am new to CMake and I don't know whether the observed
behavior is bug or I do it in a wrong way.

I am doing all this with CMake 3.0 on Windows within MSYS environment if it
plays some role.

A test demonstrating the issue follows (using 'ls' as the command and '-l -h' 
as the options):

# ==== CMakeLists.txt ====
cmake_minimum_required(VERSION 3.0)
project(test_project)

set(MYTOOL "ls")

add_custom_target(
    x ALL
    COMMAND ${MYTOOL} ${MYTOOL_FLAGS}
    COMMENT "test"
    VERBATIM
)
# ==== End of CMakeLists.txt ====


Using the CMakeLists.txt above, I get this:

$ mkdir build  &&  cd build
$ cmake -G "MSYS Makefiles" -DMYTOOL_FLAGS="-l -h" ..
... (unimportant output censored)
$ make VERBOSE=1
... (unimportant output censored)
[100%] test
ls "-l -h"
ls: invalid option --
Try `ls --help' for more information.
make[2]: *** [CMakeFiles/x] Error 2
make[2]: Leaving directory `/prj/pok/build'
make[1]: *** [CMakeFiles/x.dir/all] Error 2
make[1]: Leaving directory `/prj/pok/build'
make: *** [all] Error 2


As can be seen, CMake propagates the contents of ${MYTOOL_FLAGS} as a single 
option into 'ls', i.e. generated Makefile attempts to execute

	ls "-l -h"

instead of

	ls -l -h

Any ideas how to do this correctly?

Thanks,
Martin




More information about the CMake mailing list