8

I'm using cmake and external project module via ExternalProject_Add.

I'd like to specify custom header location for external project (just exactly as if I use include_directories in that project, but I am not able to modify its CMakeLists.txt and don't want to apply patch).

Is there any possibility to pass some include path to my external project?

I tried CMAKE_ARGS -DCMAKE_INCLUDE_PATH=<required path> without success.

avtomaton
  • 4,725
  • 1
  • 38
  • 42
  • **What kind of headers** are located in the directory you want to add? If they relates to some 3d-party library, then external project, probably, looks for headers with `find_package()`, so you need to set CMake variables, which affects on this function. Or your case is different? – Tsyvarev Feb 25 '16 at 17:49
  • @Tsyvarev: My case is different. External project just include headers in some custom location. For main project, for example, I can use `include_directories()` to point to this location, but for external project I don't have access to `CMakeLists.txt`, and I'd like to set environment to that project in my top-level cmake. I already found that `CMAKE_INCLUDE_PATH` is definitely not wnat I'm looking for. – avtomaton Feb 25 '16 at 19:48
  • 1
    So you need workaround for badly written external project. `For main project, for example, I can use include_directories() to point to this location` - you may *wrap* external project, *as if* it is compiled in subdirectory, and `include_directories()` is issued in the top-level (handy written) `CMakeLists.txt` . Other possibilities would be passing `CMAKE_CXX_FLAGS` with additional `-I` option, or even to redefine compiler, which already have this option. – Tsyvarev Feb 25 '16 at 20:53
  • I need workaround for badly written package managers in badly written OS :) Surprisingly not every project provides `Find.cmake`, `Config.cmake` or even `CMakeLists.txt`, but provides headers and libraries. All I need just specify **locations** where I can have headers. Just like `include_directories` in my own `CMakeLists.txt`. Yes, I already found solution with `CMAKE_CXX_FLAGS`, but it looks like a trick and is not completely portable. Wrapping seems even more crappy. But thanks for your comment. I'm just curious about absence of so simple thing in so powerful build sys. – avtomaton Feb 25 '16 at 21:05
  • Found variable `CMAKE_PROJECT__INCLUDE` ([documentation for it](https://cmake.org/cmake/help/v3.0/variable/CMAKE_PROJECT_PROJECT-NAME_INCLUDE.html)). According to its description, it allows to inject code into external project builds. Looks like it is what you search. – Tsyvarev Feb 25 '16 at 21:32
  • Thank you for interesting link, but it can be used only to include some *cmake* code into main project. Nothing related to build environment. – avtomaton Feb 25 '16 at 21:46

2 Answers2

10

You may execute additional CMake script for external project by assigning path to this script to variable CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE (documentation).

Let external project uses CMake command

project(e_cool)

and you want to execute

include_directories(/path/to/additional/include)

at that moment.

For doing that, you need to prepare cmake script with corresponded content:

fix_e_cool.cmake:

include_directories(/path/to/additional/include)

And pass this script via CMAKE_ARGS option of ExternalProject_Add in the main project:

CMakeLists.txt:

...
ExternalProject_Add(<name>
    ...
    CMAKE_ARGS -DCMAKE_PROJECT_e_cool_INCLUDE=${CMAKE_SOURCE_DIR}/fix_e_cool.cmake
)
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
-2

Using option will allow you to pass data from the the command line, for example:

cmake -DHEADER_PATH="/usr/my/path/file.h" ..
Joel
  • 1,805
  • 1
  • 22
  • 22
  • You misunderstood me. I'd like to pass options to external project via `ExternalProject_Add` command, and I'd like to determine **which** variable is responsible for **include paths**. I definitely know that I can pass arguments to current project via command line. – avtomaton Feb 25 '16 at 15:34