Using Custom Makefile as add_custom_command and propogate compilation/linking properties


I have an external library (a single .so to be generated) that i am building as part of my cmake project.
This library is to be built with a custom makefile. This library is meant to depend on some other libraries that are part of my cmake project and those are driven by cmake (add_library, target_link_libraries, etc…).

One solution here is of course to take this external library outside of cmake project, build cmake project and expose stuff with something (e.g. autotools) for consumption with make. however I would like to drive the build entirely with cmake and also on libs that are brought via find_package (those are generated by conan)

I am able to use add_custom_command overall, however for this use case I need to extract all the
compilation/linking properties to feed as input to the makefile in this custom command. Problems i have

  1. One can not extract target properties properly until after the generation step (properly), cause of presence of generator expressions
  2. generator expressions do not allow one to walk over them. I can use file(GENERATE) to get 1 property, but those in turn require walking the result of the first (e.g. searching for all dependencies)
  3. I found cmake-file-api, which does generate information about the build. However, not for results of find_package. Typically find_package would introduce IMPORTED targets and those are not exposed as part of cmake-file-api (for instance for static libs).

Would there be a suggestion on what i can try? Or may be I misunderstand something.


You can pass flags for each usage requirement manually. You can see how it works for compile definitions in this code. This would need to be repeated for each usage requirement that matters (compile options, linker options, link libraries, etc.). You can use file(GENERATE) to write to a file that the Makefile project includes to get the flags that are needed. You might need to make an EXCLUDE_FROM_ALL target that links to what the Makefile does to query though. At that point, might it not be easier to write CMake code for this Makefile project directly?

Thanks a lot for the provided example!

What i’m missing for this option is how to iterate over the dependencies (target’s link libraries) if that contains generate expressions as well as you can not iterate anyhow for those. Also i see for imported targets you assume that this generation is done elsewhere - is that correct?

Just to note, what i did so far and seemed work is to generate a dummy executable with required compile/link options/flags and query those using file-api (adding those via add_custom_command).

You’d want to create a shared library because $<TARGET_TYPE> genexes might change things based on the type. If you also set the EXCLUDE_FROM_ALL property, it won’t be built (normally). But you should be able to use the patterns I linked to to extract all of the info you need as well. That said, I don’t see a genex for transitive library calculations.