[CMake] Fun with lists and execute_process

Dan Kegel dank at kegel.com
Tue Sep 2 17:06:33 EDT 2014


http://www.cmake.org/Wiki/CMake/Language_Syntax does explain
about semicolons, strings, and lists, but the consequences are
still kind of surprising.  Newbies like me have to write little programs
to feel our way towards an understanding of what works.

For example, I wanted to pass a space-separated list of things
as a single argument to an external program, and I didn't
get what was going on until I wrote a tiny program to explore
the problem in isolation.  Here it is, on the off chance some
other newbie will dredge it up in a search and find it useful.

cmake_minimum_required(VERSION 2.8.11)
project(blah)

# foo.pl is the single line
# print STDERR "args are: ".join(',', at ARGV)."\n";

function(foo)
    execute_process(COMMAND perl foo.pl ${ARGN})
endfunction()

# Works; passes two arguments
foo(
   --libs "foo.a bar.a"
)

set(LIBS foo.a)
list(APPEND LIBS bar.a)
# Fails; passes three arguments, despite the quotes
foo(
   --libs "${LIBS}"
)

STRING(REGEX REPLACE ";" " " XLIBS "${LIBS}" )
# Works; passes two arguments
foo(
   --libs "${XLIBS}"
)


More information about the CMake mailing list