Search code examples
cmakeexternal-project

How to set command argument in externalproject_add


I want to create a cmake script to build the zlib automatically in my project.

I added

externalproject_add(zlib
  PREFIX .
  # download step
  GIT_REPOSITORY git@github.com:madler/zlib.git
  GIT_TAG v1.2.8
  # configure step
  SOURCE_DIR zlib
  CONFIGURE_COMMAND ${CMAKE_SOURCE_DIR}/build/zlib/zlib/configure --archs="-arch ${ARCH}" --static
  # build step
  BUILD_COMMAND make
  BUILD_IN_SOURCE 1
  # install step
  INSTALL_DIR zlib-${ARCH}
  INSTALL_COMMAND make install
  # logging
  LOG_CONFIGURE 1
  LOG_BUILD 1
  LOG_INSTALL 1
  )

But it generate configuration commands like this:

'/Users/david/Documents/cmake_projects/build/zlib/zlib/configure' '--archs=' '-arch' 'x86_64"' '--static"'

Which I think is not properly quoted.

I tried many tricks, but I cannot get the command work. Any idea?


Solution

  • Double quotes prevent string from being split. I'd also separate at least ${ARCH_PARAMS} parameter into separate variable, so, You'd have something like this:

    set(ARCH_PARAMS "--archs='-arch ${ARCH}'")
    ...
    ...
    CONFIGURE_COMMAND ${CMAKE_SOURCE_DIR}/build/zlib/zlib/configure ${ARCH_PARAMS} --static
    

    So, it's gonna be passed as this:

    '/tmp/so_test/build/zlib/zlib/configure' '--archs='-arch x64_86'' '--static'

    On the side note, zlib config seams a bit fishy, because it keeps complaining about compiler error reporting:

    $ ./configure --static --archs="-arch x86_64"

    Checking for gcc...

    Compiler error reporting is too harsh for ./configure (perhaps remove -Werror).

    ** ./configure aborting.

    Relevant questions:

    cmake: How to include literal double-quote in custom command?