In the interest of clarity and capturing requirements clearly, I’m attempting to use INTERFACE targets as a mechanism to group compiler flags, definitions, and linker options. An example is like so:
add_library(my_specs INTERFACE)
target_compile_definitions(my_specs INTERFACE MY_DEF=FOO)
target_compile_options(my_specs INTERFACE -mfoo=bar)
target_link_options(my_specs INTERFACE -motherfoo=otherbar)
In a project, I’d use it like so:
add_library(my_lib STATIC ... )
target_link_libraries(my_lib PRIVATE my_specs)
add_executable(my_elf ...)
target_link_libraries(my_elf PRIVATE my_lib my_specs)
And this generally works, the compiler inserts the definitions and options for sources in my_lib and my_elf, and the linker applies the linker option -motherfoo=otherbar
, all good.
But if my_lib comes from a subdirectory or elsewhere:
add_subdirectory(path/to/my_lib)
And it contains some dependencies:
# path/to/my_lib/CMakeLists.txt
find_package(ExternalPackage REQUIRED)
add_subdirectory(child_dependency)
add_library(my_lib STATIC ... )
target_link_libraries(my_lib PRIVATE child_dependency lib_provided_by_external_package)
Then child_dependency and lib_provided_by_external_package are NOT built with the compiler flags and options from my_specs, and the linker options are not used when linking them, even though they are used for my_lib itself.
So when the whole thing is assembled at the executable stage, there can be complete binary incompatibility (when building for, e.g. ARM and one library is compiled with the hard float ABI and the other is not) and the whole link fails (not to mention that even if it could succeed you would have some code using the wrong ABI for float).
Without knowing all of the dependencies and injecting the specs into each of them at the top-level CMakeLists.txt file, and assuming the library is built for various specs at various times and thus the specs cannot be hardcoded into it, what is “the CMake way” to solve this problem and ensure the spec requirements are carried down to the lowest dependency in the library?