[cmake-developers] Generating guards around add_library(... IMPORTED)
Stephen Kelly
steveire at gmail.com
Thu Nov 29 12:29:30 EST 2012
Hi there,
This subject came up before and I would like to revisit it:
http://thread.gmane.org/gmane.comp.programming.tools.cmake.devel/2147/focus=2151
The problem is that authors of Config files are currently required to guard
their include(MyPackageTargets.cmake) line manually.
This is not obvious, nor is the correct way to do it documented. Even now,
Config files for KDE Frameworks (development version) are not using a guard
at all.
Through some experiments with the Qt config files I landed on using if (NOT
TARGET), as I had originally suggested in the thread above, because a
variable didn't work, and a simple directory property didn't work.
http://qt.gitorious.org/qt/qtbase/commit/2b735ac8e6b36c66444986a381cc56cd65a2bf7c
http://qt.gitorious.org/qt/qtbase/commit/f16a77d78342acba9df2195dd20c9e2ce06a61ff
What does work is a INHERITED directory property, as suggested here:
http://thread.gmane.org/gmane.comp.kde.devel.buildsystem/7165/focus=41551
because they apparently have the same scope and visibility as the imported
targets themselves (that is, visible in this dir and subdirs, but not in the
parent).
However, that is inconvenient as the BRIEF_DOCS and FULL_DOCS components of
define_property() must be present and non-empty strings. I don't think it's
practical to expect writers of Config files to do that.
The define_property() trick also doesn't work for IMPORTED GLOBAL targets.
Currently CMake doesn't seem to generate those, but in the future it might
generate targets which have different scope that INHERITED properties.
Really I think the most practical solution is to generate 'if (NOT TARGET)'
when exporting. I hit this problem several times today with KDE Frameworks
Config files, and I think this solution is the most beneficial. The concern
about people having the same name for an IMPORTED and a regular target is
minor in comparison - They will eventually hit an error anyway.
If you want, we could possibly even add 'if(NOT IMPORTED_TARGET)' to
workaround the problem.
Thanks,
Steve.
More information about the cmake-developers
mailing list