How to add include directories to a swig cmake target without adding them globally?

Viewed 645

How do you specify include_directories for a swig target in cmake? I see that to add a swig target you do:

swig_add_library(swg_target
        TYPE STATIC
        LANGUAGE python
        SOURCES swg_src_files.i swg_src_files.h swg_src_files.cpp
)

but following this with the usual

target_include_directories(swg_target PUBLIC ".../include")

does not work (i.e. seems to do precisely nothing) and instead I need to put

include_directories(".../include")

before the call to swig_add_library. Is there a way to add include directories to a swig target without adding these directories globally for every target?

1 Answers

Under the hood, swig_add_library must eventually invoke add_library and set some properties, but there is something curious in the documentation:

Target library properties can be set to apply same configuration to all SWIG input files.

SWIG_USE_TARGET_INCLUDE_DIRECTORIES
If set to TRUE, contents of target property INCLUDE_DIRECTORIES will be forwarded to SWIG compiler. If set to FALSE or not defined, target property INCLUDE_DIRECTORIES will be ignored. This behavior can be overridden by specifying source property USE_TARGET_INCLUDE_DIRECTORIES.

Thus, in order to use target_include_directories, but you must also set the above property on your target. It was added in CMake 3.13, if I read the Git history correctly.

Related