Pass parameter with spaces to CMake ExternalProjec

2019-06-21 20:00发布

问题:

I'm trying to cross compile bzip2 in Linux utilizing CMake's ExternalProject_Add(...) command.

However, in order to cross-compile libbzip, you must modify some of the Makefile macros to execute the build. Here is my current cross compile variable:

set(bzlib_CROSS CC=${CROSS_PREFIX}gcc AR=${CROSS_PREFIX}ar RANLIB=${CROSS_PREFIX}ranlib LDFLAGS=-L${bzlib_MINGW_SRC}/lib CFLAGS=-Wall -Winline -O2 -g -I${bzlib_MINGW_SRC}/include)

This variable is then used for the build command

set(bzlib_BUILD make ${bzlib_CROSS} PREFIX=${DEPENDENCY_DIR})

CMake errors because it is trying to pass -O2, -g, etc. to make, which of course doesn't work, but when I try to add quotes around the CFLAGS variable it says

cc1: error: unrecognized command line option '-Wall -Winline -O2 -g -I/long/path/here/include'

Is it possible to set a Makefile macro with spaces in it using CMake's BUILD_COMMAND? Thanks!

回答1:

Figured it out...I needed to split the quoted string into its own variable like this

set(bzlib_CFLAGS "-Wall -Winline -O2 -g -I${bzlib_MINGW_SRC}/include")
set(bzlib_CROSS CC=${CROSS_PREFIX}gcc AR=${CROSS_PREFIX}ar RANLIB=${CROSS_PREFIX}ranlib LDFLAGS=-L${bzlib_MINGW_SRC}/lib CFLAGS=${bzlib_CFLAGS})

This worked!