Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug(cmake): fix cmake install exports (#10)
If you want to access cmake targets of a library, keep the following in mind. Your library could be either consumed by an `add_subdirectory` (that's the way CPM and therefore edm works), or it could be consumed by `find_package` (the standard way for general package managers, yocto, debian ...). If your library is consumed by `add_subdirectory`, then the consumer will see all of your *local* targets, even the ones, that you're not planning to expose/export (that is also one of the major drawbacks of `add_subdirectory`). If your library is consumed by `find_package`, then only the exported targets are visible. Usually, when you're maintaining a library, you want write it in a way that it can be consumed in both ways. Meaning, both ways should be compatible with this usage ``` target_link_library(some_depending_target PRIVATE you_lib_namespace::your_lib_target ) ``` For this to work, you need to understand how the name of your library gets exported. When your library is consumed by `add_subdirectory`, then everything is visible. So if you create your library like this: ``` add_library(din) add_library(cbv2g::din ALIAS din) ``` Your consumer will see both `din` and `cbv2g::din`. Unfortunately, using `din` here is a bit risky, as this *short* target name might collide with other library targets (hence, when you want your library consumable with `add_subdirectory`, you should prefix basically every target - again, one of the drawbacks of using `add_subdirectory`). So lets make this compatible by using ``` add_library(cbv2g_din) add_library(cbv2g::din ALIAS cbv2g_din) ``` You're expecting your consumer to use the alias (`cbv2g::din`). Now when you want to *install export* your targets, you need to create an export first and add your targets to it by ``` install( TARGETS cbv2g_din ... EXPORT cbv2g-targets ) ``` Here, `cbv2g-targets` is the name of the *export*, you want to install. Furthermore, it is not allowed to use `ALIAS` targets in this command. Secondly, you need to *install* this export by using ``` install( EXPORT cbv2g-targets NAMESPACE cbv2g ) ``` This command has a `NAMESPACE` argument and this namespace will get prepended to the name of the target. But now, the name of the exported target will be `cbv2g::cbv2g_din`, because it adds the name of real, *non-aliased* target to the namespace. But we actually want to have `cbv2g::din` as the exported target name. In order to get this, we would either have to use `din` as the library target name, instead of `cbv2g_din` (but yielding the problems with possible collisions) or set the following property: ``` set_property(TARGET cbv2g_din PROPERTY EXPORT_NAME din) ``` This will basically tell cmake to use a different name than the target name when creating the export name of this library. For maximum compatibility, I've chosen to set the `EXPORT_NAME` property. Btw, the helper function `evc_setup_package` internally calls `install(EXPORTS ...)` and forwards the `NAMESPACE` argument. Signed-off-by: aw <aw@pionix.de>
- Loading branch information