Search code examples
cmakeexternal-project

CMake ExternalProject_Add() that uses a parent folder in its include_directories()


I have an 'int_tester' CMake project that makes use of ExternalProject_Add for its 'drv' dependency. Now, this 'drv' is also built using CMake, and it uses a parent directory in its include_directories(). Trouble is that I do not know how to copy this parent folder inside ExternalProject_Add().

Directory structure is like:

/  
|-common  
|-IP  
| |-int_tester  
| |-drv  

CMakeLists.txt for drv looks like:

add_library(SPT_driver STATIC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c)  
target_include_directories(SPT_driver
    PUBLIC  ${CMAKE_CURRENT_SOURCE_DIR}/api/
    PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../common/include/
)  

CMakeLists.txt for int_tester like:

add_executable(int_tester ...)  
ExternalProject_Add(drv_ext
    PREFIX ${CMAKE_CURRENT_BINARY_DIR}/drv/drv_dir
    URL ${CMAKE_CURRENT_SOURCE_DIR}/../drv
    INSTALL_COMMAND ""
)
add_library(drv_import STATIC IMPORTED) #phony lib
add_dependencies (drv_import drv_ext) #link phony lib to the external proj
ExternalProject_Get_Property(drv_ext install_dir) #find out where the external proj is installed
set_property(TARGET drv_import PROPERTY IMPORTED_LOCATION ${install_dir}/src/drv_ext-build/obj/drv.a) #so that the phony lib can have a target
include_directories(${install_dir}/src/drv_ext/api) #and to make current executable dependent on api .h and .a of the externally built lib
target_link_libraries(int_tester drv_import)

As one can see, drv depends on ${CMAKE_CURRENT_SOURCE_DIR}/../../common/include/.

When 'int_tester' builds 'drv' (via ExternalProject_Add), folder ../../common/include/ cannot be found (it is not brought it by ExternalProject_Add). Even if I add another fake ExternProject for this common folder, its directory structure will not match the one expected by CMakeLists.txt of 'drv'.

What am I missing, please?


Solution

  • If you want to build 'drv' using IP/drv as its source directory, explicitely specify SOURCE_DIR option for ExternalProject_Add:

    ExternalProject_Add(drv_ext
        PREFIX ${CMAKE_CURRENT_BINARY_DIR}/drv/drv_dir
        SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../drv
        INSTALL_COMMAND ""
    )
    

    Without this option, source directory is deduced from PREFX (see documentation for exact algorithm of that deduction).


    Option URL for ExternalProject_Add denotes directory, which should be copied into the source one. If you do not want to copy, do not use that option.