Ninja: Specifying depencies specifically for object compilation
My particular situation is that as part of my project I build a compiler plugin that performs some code instrumentation. I then use this plugin to compile a static library. In this scenario the following doesn't work as expected:
add_library(plugin SHARED ...)
add_library(static-lib STATIC ...)
target_compile_options(static-lib PUBLIC "correct flags to use the compiler plugin")
add_dependencies(static-lib plugin)
This results in the build system interpreting the dependency on plugin as just a link-time dependency. That is the build system thinks it just needs the symbols defined by plugin
when linking together static-lib
. What I want is to tell the build system that plugin
is needed to build the object files inside static-lib
.
The way I have gotten around that is to introduce an intermediate custom target to block the dependency tracker from assuming anything about the nature of the dependency as follows:
add_library(plugin SHARED ...)
add_custom_target(plugin-so DEPENDS plugin)
add_library(static-lib STATIC ...)
target_compile_options(static-lib PUBLIC "correct flags to use the compiler plugin")
add_dependencies(static-lib plugin-so)
This seems to work, but as far as I am concerned, is really not obvious. I think it would be more intuitive to make add_dependencies
introduce a hard ordering constraint between all the steps needed to build the dependent and the dependency, as the documentation suggests. Alternatively, there could be a way of specifying when a dependency is needed, i.e., at compile-time (when building the object files), or at link-time (when linking together the library).