# Using Custom Makefile as add\_custom\_command and propogate compilation/linking properties

**URL:** https://discourse.cmake.org/t/using-custom-makefile-as-add-custom-command-and-propogate-compilation-linking-properties/6737
**Category:** Usage
**Created:** [October 27, 2022, 11:05am UTC](https://discourse.cmake.org/t/using-custom-makefile-as-add-custom-command-and-propogate-compilation-linking-properties/6737 "2022-10-27T11:05:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![vkhristenko](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vkhristenko/32/2866_2.png) [@vkhristenko](https://discourse.cmake.org/u/vkhristenko)
#### Post date: [October 27, 2022, 11:05am UTC](https://discourse.cmake.org/t/using-custom-makefile-as-add-custom-command-and-propogate-compilation-linking-properties/6737/1 "2022-10-27T11:05:54Z")

</div>

Hello,

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.

VK

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [October 28, 2022, 12:32am UTC](https://discourse.cmake.org/t/using-custom-makefile-as-add-custom-command-and-propogate-compilation-linking-properties/6737/2 "2022-10-28T00:32:37Z")

</div>

You can pass flags for each usage requirement manually. You can see how it works for compile definitions in [this code](https://gitlab.kitware.com/vtk/vtk/-/blob/4c826cf18f0870b2e2e7d3c5381e345753fadae0/CMake/vtkModule.cmake#L3270). 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?

---

<div class="post-metadata">

### Author: ![vkhristenko](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vkhristenko/32/2866_2.png) [@vkhristenko](https://discourse.cmake.org/u/vkhristenko)
#### Post date: [October 28, 2022, 3:41am UTC](https://discourse.cmake.org/t/using-custom-makefile-as-add-custom-command-and-propogate-compilation-linking-properties/6737/3 "2022-10-28T03:41:24Z")

</div>

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`).

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [October 28, 2022, 12:34pm UTC](https://discourse.cmake.org/t/using-custom-makefile-as-add-custom-command-and-propogate-compilation-linking-properties/6737/4 "2022-10-28T12:34:36Z")

</div>

> [@vkhristenko](#):
>
> 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.
