Search code examples
cmakebuild-processbuild-automationautotools

How to pass an environment variable to ExternalProject_Add CONFIGURE_COMMAND?


I have a third party library with autotools project. I want to use ExternalProject_Add to build the library.

This can be done the following way:

ExternalProject_Add(project_lib
   SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib
   CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lib/configure --prefix=${LIB_OUTPUT}
   BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/build
)

Now the problem is that I need to pass an environment variable to configure but I cannot find a way to do it.

In the console I would do it the following way:

CPPFLAGS="-fPIC" ./configure --prefix=output

Is there a way to pass CPPFLAGS="-fPIC" env to the configure with ExternalProject_Add/CONFIGURE_COMMAND ?


Solution

  • It can be done by executing configure command through cmake (cmake --help) command mode:

    -E                           = CMake command mode.
    

    So instead of calling configure directly we can execute it through cmake command mode with the environment variables:

    ExternalProject_Add(project_lib
       SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib
       CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env CPPFLAGS=-fPIC ${CMAKE_CURRENT_SOURCE_DIR}/lib/configure --prefix=${LIB_OUTPUT}
       BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/build
    )
    

    [cmake-developers] Setting up environment using ExternalProject_Add