Include PRIVATE generated headers dir under build tree in test execution from a source dir

Could you please advise on the proper way to support test execution when test program includes generated file from the build tree without exposing generated file as PUBLIC. When library is built header files are generated under sub directory which is at the same level as the test sub directory as per the following example.
The problem is that to make test execution work I would need to make $<BUILD_INTERFACE:}/…/genned/include > PUBLIC. Test is only for a particular library is not needed by other libraries being built or might cause conflict with other libraries . I’d like to avoid moving test dir under mylib subdir nor adding copy command
|-- mylib/
| |-- CMakeLists.txt
| -- prog1.cpp |-- genned/ | |-- CMakeLists.txt | – include
genned.h {generated by mylib build}
-- test/ |-- CMakeLists.txt – test.cpp { has #include “genned.h”}

CMakeLists.txt of mylib subdir
add_library(my_lib SHARED)

# since we are in src dir and include is at the same level
#these headers include genned.h

This works but include exposed as public
$<BUILD_INTERFACE:}/…/genned/include >
#generated headers are in —not working


test(xtests SCENARIO=)



Make fails with an
In file included from test.cpp
fatal error…: genned.h: No such file or directory compilation terminated.

To make it work I had to

Since you don’t want to attach the include directory information to my_lib you have the following options.

  1. Construct an INTERFACE target that holds the target include directory, and use that in addition:
add_library(my_lib_test_info INTERFACE)
target_include_directories(my_lib_test_info INTERFACE  $<BUILD_INTERFACE:}/…/genned/include >)
target_link_libraries(xtests PRIVATE my_lib_test_info)
  1. Place the include directory information on xtests when you construct it