Secafo
Secafo

Reputation: 107

Get variables and options from external project

I'd like to know how to get variables and options from an external project. For example, I'm using ExternalProject_Add to add a library that my project depends on, the thing is that this dependency has some variables and options - some of them are on by default - I'm wondering, how to get them from my CMakeLists.txt.

Upvotes: 8

Views: 3027

Answers (2)

d3roch4
d3roch4

Reputation: 396

I parse for answer a question similar.

This work for me:

  set(ZIP_DEP ${CMAKE_BINARY_DIR}/httpserver.zip)
  if(NOT EXISTS ${ZIP_DEP})
    file(DOWNLOAD https://github.com/d3roch4/httpserver/archive/master.zip 
  ${ZIP_DEP})
    execute_process(COMMAND unzip -o ${ZIP_DEP}
                  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
                  RESULT_VARIABLE rv
    )
    if(NOT rv EQUAL 0)
      message(FATAL_ERROR "error: extract of '${ZIP_DEP}' failed")
    endif()
  endif()
  add_subdirectory(${CMAKE_BINARY_DIR}/httpserver-master)

Upvotes: 0

Tsyvarev
Tsyvarev

Reputation: 66288

All commands related with ExternalProject_Add are executed at build stage, but your CMakeLists.txt is interpreted at configure stage, when external library hasn't configured yet, so its variables and options are not defined.

More general, cmake variable from the external project can be extracted in two cases:

  1. External project is included into main project via add_subdirectory.
  2. External project provides packaging file, which define needed variable. In that case external project should be installed before or at configuration stage of main project. You can read about packaging in CMake tutorial.

Upvotes: 5

Related Questions