First, it is not a good practice to specify link options through target_link_libraries()
command. It is preferable to use target_link_options()
.
And also, it is strongly recommended using the up-to-date syntax regarding target_link_libraries()
by specifying one of following keyword PUBLIC
, PRIVATE
or INTERFACE
.
And to finish, to avoid unexpected de-duplicated link options, it is required to use the SHELL:
prefix.
Here is possible approach:
add_library(jni_component_helper STATIC ${jni_src})
target_link_libraries(jni_component_helper PRIVATE dep1 dep2)
add_library(jni_component STATIC "")
# add dependency of jni_component over jni_component_helper
target_link_libraries (jni_component PRIVATE jni_component_helper)
# ensure the required link option is specified for the helper library only
target_link_options(jni_component INTERFACE "SHELL:-Wl,-whole-archive $<TARGET_FILE:jni_component_helper> -Wl,-no-whole-archive")
And for the final step:
add_subdirectory(jni_component_dir)
add_library(final_lib SHARED ${final_lib_src})
target_link_libraries(final_lib PRIVATE jni_component ... other libs...)