0

Consider a simple scenario where my cmake project adds a dependency as a subdirectory:

.
├── CMakeLists.txt
├── src
├── include
│
└── externals
    └── BAR
        ├── CMakeLists.txt
        ├── src
        └── include

The main CMakeLists.txt is something like:

cmake_minimum_required(VERSION 3.0)

project(FOO)

add_subdirectory(externals/BAR)

set(SOURCES ${CMAKE_SOURCE_DIR}/src/foo.cpp
            ${CMAKE_SOURCE_DIR}/include/bar.hpp)

add_library(${PROJECT_NAME} SHARED ${SOURCES})

target_include_directories(${PROJECT_NAME} PUBLIC
    ${CMAKE_SOURCE_DIR}/include                    # this works
    ${BAR_INCLUDE_DIR})                            # this does not

The problem is, the include directories of the added project are not accessable from FOO project.

BAR is a huge dependency with its own sub directories. Its root cmake uses INCLUDE_DIRECTORIES command (instead of target_include_directories). It then does some FILE ( GLOB headers and use them when triggering make install

I am aware that there are many questions regarding subdirectories in cmake, however, in my case cannot modify the CMakeLists.txt in the subdirectory. It's a git submodule that gets updated constantly and it's a pain to modify it constantly.

How can I access BAR's include directories (and later its libraries) without changing its cmakes?

P.S. BAR gets compiled properly and shared library appears in the build folder.

Pouya
  • 1,266
  • 3
  • 18
  • 44
  • 1
    Did you try accessing `BAR_INCLUDE_DIR` from the top-level CMake? Is it empty? Is it not populated completely? When using `add_subdirectory()`, some of these `BAR_*` variables **should** be accessible... – Kevin Nov 21 '19 at 15:55
  • @squareskittles, Thanks for feedback, my bad. I was looking at a standalone version of it **after** `make install`. The cmake itself makes `GLOB` to `headers` and then install them. The question then is, how can I access its headers if it doesn't set `BAR_INCLUDE_DIR` without installation? – Pouya Nov 21 '19 at 16:04
  • You could've used `PARENT_SCOPE` to expose include dir from under subdirectory but since you say you can't modify subproject `CMakeLists.txt` I guess there's no other way – Alexey Andronov Nov 21 '19 at 16:04
  • 1
    "The question then is, how can I access its headers if it doesn't set BAR_INCLUDE_DIR without installation?" - Install `BAR` first and only then configure your project. That way you may use `find_package(BAR)`. Alternative to pre-installation is installing the `BAR` during configuring your project, so futher `find_package(BAR)` will work. – Tsyvarev Nov 21 '19 at 16:17
  • @Tsyvarev, Thanks. I'm more for your second suggestion. Does it mean by some hand made bash magic or there are some already existing mechanism in cmake? Could you elaborate please? – Pouya Nov 21 '19 at 16:24
  • 1
    You may use [execute_process](https://cmake.org/cmake/help/v3.7/command/execute_process.html) for run scripts or tools immediately at configuration stage. Alternatively, the function [ExternalProject_Add](https://cmake.org/cmake/help/v3.7/module/ExternalProject.html) easily drives building an external project. While the function itself builds the external project only at the *build* stage of the main project, there are approaches to change that: https://stackoverflow.com/questions/37845413/how-to-configure-externalproject-during-main-project-configuration. – Tsyvarev Nov 21 '19 at 16:40

0 Answers0