target_include_directories failed when target (myprojJNI below) is a target that was created by add_jar GENERATE_NATIVE_HEADERS option.
ERROR: CMake Error at …CMakeLists.txt:… (target_include_directories):
target_include_directories may only set INTERFACE properties on INTERFACE
targets"
This target was created with
add_jar(myprojJava
SOURCES ${JNI_SRC}
INCLUDE_JARS ${JNI_CLASSPATH}
GENERATE_NATIVE_HEADERS myprojJNI)
Here myprojJNI is a target and documenation said :
“using option GENERATE_NATIVE_HEADERS, native header files can be generated for methods declared as native. These files provide the connective glue that allow your Java and C code to interact. An INTERFACE target will be created for an easy usage of generated files. Sub-option DESTINATION can be used to specify the output directory for generated header files” So myprojJNI must be INTERFACE target and therefore allow target_include_directories setting like
target_include_directories(myprojJNI
PUBLIC #<-- Wrong, it must match myprojJNI target type which is INTERFACE
$<INSTALL_INTERFACE:${ARCH}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
)
So why is this error occuring?
Note 1, there is also another error "Could NOT find JNI (missing: JNI_DIR)
" at the begging of CMakeLists in the
find_package(JNI PATHS /opt/jdk/jdk8)
but cmake documentation said JNI is recommended for using
GENERATE_NATIVE_HEADERS, but despite JNI package is not found, the JNI native_headers are generated ok under the build tree when target_include_directories and myprojJNI installation code is bypassed (UseJava.cmake : set (_add_jar_GENERATE_NATIVE_HEADERS_DESTINATION "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_TARGET_NAME}.dir/native_headers)
Note 2. The purpose of caling target_include_directories(myprojJNIis to fix an error occurring when install(TARGETWS myprojJNI … ) indicating that myprojJNIis property INTERFACE_INCLUDE_DIRECTORIES contains path to build tree and source tree which is because default value for the native_headers is under ${CMAKE_CURRENT_BINARY_DIR}.
So the bigger question is more general how to install and export target with dependency on GENERATE_NATIVE_HEADERS target like myprojJNI which seems blocked by the default INTERFACE_INCLUDE_DIRECTORIES pointing to CMAKE_CURRENT_BINARY_DIR.
target_link_libraries(myprojlibShared
...myprojJNI ...)
....
install(TARGETS myprojlibShared myprojJNI ..
EXPORT myExportTargets ...
)
# had to add myprojJNI to avoid error indicating myprojlibShared dependency on myprojJNI