# Defer install() and configure\_file() until libraries targets built

**URL:** <https://discourse.cmake.org/t/defer-install-and-configure-file-until-libraries-targets-built/9282>\
**Category:** Code\
**Tags:** os:windows, gen:ninja\
**Created:** [October 25, 2023, 9:06pm UTC](https://discourse.cmake.org/t/defer-install-and-configure-file-until-libraries-targets-built/9282 "2023-10-25T21:06:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![f.brier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/90db22/32.png) [@f.brier](https://discourse.cmake.org/u/f.brier)\
**Post date:** [October 25, 2023, 9:06pm UTC](https://discourse.cmake.org/t/defer-install-and-configure-file-until-libraries-targets-built/9282/1 "2023-10-25T21:06:18Z")

</div>

My CMakeLists.txt has two library targets. CPack needs to bundle the libraries and some other files that cannot be generated until the two library targets have been built. My understanding is that CMake cannot execute a function or macro as a custom target or command. Instead the generation and install() calls are placed in a separate .cmake file, along with “include(CPack)” at the end, and a ${CMAKE\_COMMAND} is invoked with lots of parameters to copy all the standard variables into the new CMake instance. For some reason, even though the add\_custom\_target() is passed ALL and the DEPENDS passed the list of library targets, its COMMENT is never displayed. The ${CMAKE\_BINARY\_DIR}/CPackConfig.cmake is not generated.

Regardless, is there an easier way to do this? It is feeling like I am fighting the tool which is usually indicative of trying to do things the hard way. Any pointers would be greatly appreciated.

---

<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 29, 2023, 12:23pm UTC](https://discourse.cmake.org/t/defer-install-and-configure-file-until-libraries-targets-built/9282/2 "2023-10-29T12:23:35Z")

</div>

> [@f.brier](#):
>
> My understanding is that CMake cannot execute a function or macro as a custom target or command

You could run a script that runs the macro/command, but you may also need to shuttle an unknown amount of information across the execution boundary for that to work reliably.

I’m afraid that without an example, it’s hard to suggest what might be wrong as what you describe sounds like it should work at least.

As for why the generation must wait, can you give a more concrete reason why they must exist first? It sounds odd that a library must exist first (an executable would make more sense to me) unless they’re something like Python modules you expect to import and actually execute. Any metadata about locations or the like should be known by CMake and available via `file(GENERATE)` with something like `$<TARGET_FILE>` expressions.

---

<div class="post-metadata">

**Author:** ![f.brier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/90db22/32.png) [@f.brier](https://discourse.cmake.org/u/f.brier)\
**Post date:** [November 3, 2023, 10:47pm UTC](https://discourse.cmake.org/t/defer-install-and-configure-file-until-libraries-targets-built/9282/3 "2023-11-03T22:47:02Z")

</div>

I have been wrestling with coming up with a solution and the root of the problem was likely a conceptual gap of understanding CMake. I now have a solution. If you could comment positively or negatively, that would be great.

CMake builds up a metadata representation of how to build the project, target order etc using keywords such as DEPENDS and commands like add\_dependencies. Unfortunately that metadata is not accessible via get\_target\_property(). So I created my own custom target property TARGET\_DEPENDENCIES and wrapped commands like add\_custom\_target and add\_dependencies to also place the list of dependent targets in that new property and then call the wrapped command. I can then traverse the multiway tree of dependencies and ask each target for its OUTPUT\_NAME and include directories for use in building the archive. Unfortunately, CMake does not set the OUTPUT\_NAME when calling add\_library(). It could set it to the name of the target as a default, but it does not. So, after calling add\_library(), you need to call set\_target\_properties(). As an enhancement, I can wrap add\_library() as well. Please let me know whether I finally “got” CMake and an appropriate solution. Thank you.

One side note - this is all to support building NuGet packages for which there is a CPack generator. I believe there is a gap where the NuGet dependency metadata is not being added to the NuGet package. This is from looking at the source code. I forked the CMake repository. If I can fix this, do you accept pull requests? I don’t think it will be a big change. Again, thank you.

---

<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:** [November 19, 2023, 2:54am UTC](https://discourse.cmake.org/t/defer-install-and-configure-file-until-libraries-targets-built/9282/4 "2023-11-19T02:54:57Z")

</div>

> [@f.brier](#):
>
> Unfortunately, CMake does not set the OUTPUT\_NAME when calling add\_library(). It could set it to the name of the target as a default, but it does not.

Yeah, it could be set by default. Alas, something may be using “manually set `OUTPUT_NAME`” logic somehow, so it’d likely have to be a policy to do that 😕 . Making `myproject_add_library` sounds fine to me.

> [@f.brier](#):
>
> This is from looking at the source code. I forked the CMake repository. If I can fix this, do you accept pull requests? I don’t think it will be a big change. Again, thank you.

Yes, this sounds like a reasonable enhancement. I’m not sure of the mechanism, but some way to tell the NuGet CPack generator things like that sounds fine. I’d look at how the DPKG or RPM generators allow users to specify dependencies as guidance.
