FairRoot icon indicating copy to clipboard operation
FairRoot copied to clipboard

CMake: Auto-Generation of FooLinkDef.h files

Open klenze opened this issue 2 years ago • 2 comments

The Linkdef file must at the moment be manually maintained for GENERATE_LIBRARY() to work.

I think this is error-prone and mostly redundant. For our (r3b) use case, in 99% of the cases, if you are writing a class inheriting from TObject, e.g. with the ClassDef/ClassImp macro calls, you also want the accessible from CINT/PyROOT, so you want the class in LinkDef.h.

A good heuristic would be to assume that anything in SRCS passed to ClassImp() should also end up in the linkdef file. A simple approach would be to grep for ClassImp() in SRCS and just put them into the file.

For more sophistication (e.g. respecting #if 0 blocks), perhaps #define ClassImpLink(X) ClassImp(X); void LinkMe##X(){},(Or even wrap ClassDef instead) and then nm -C OBJECTS | grep LinkMe | sed ... > LinkDef.h

Or cat SRC | sed s/ClassDef/LinkMe/ | $CC $FLAGS -E - | grep LinkMe | sed ... > LinkDef.h

I am aware that these are all hacks, but I would argue that so is the dictionary creation mechanism (from a standard C++ perspective).

In GENERATE_LIBRARY(), all of this could be done conditionally on e.g. LINKDEF being set to "auto" or something to minimise disruption of existing code.

If this feature is generally wanted, I could try to implement it.

klenze avatar Jul 07 '21 22:07 klenze

The problem with auto-generation of LinkDef.h files is, that those actually contain meaningful user input. It tells rootcling for what types to create dictionaries. From your description it seems, that in your use case you always create dictionaries for everything. Have you considered using selection by filename (such a LinkDef.h should indeed be straightforward to generate)?

Regarding your question about implementing such a feature, I would say, if it is useful, it is definitely appreciated. For the implementation I recommend to start developing a concise and well-documented CMake function that just does the job of generating the desired LinkDef.h file. Once this is achieved, it should be easy to integrate it into GENERATE_LIBRARY(). Also have a look at cmake/modules/FairRootTargetRootDictionary.cmake which already modernizes a significant part of the older GENERATE_LIBRARY() macro to modern target-based CMake.

dennisklein avatar Jul 08 '21 06:07 dennisklein

In most cases the generation of LinkDef.h files isn't that straight forward as discussed so far. If the use case is to generate the glue code for ROOT for all sources of your target I completely agree that this can be done automatically. In most cases the generation of LinkDef.h files isn't that straight forward as discussed so far. One of such non trivial cases is when you have to provide for some reason a manually maintained streamer or if you don't want to create a streamer at all.

If this feature is included to our CMake it should only be optional and the current GENERATE_LIBRARY() function should work as before.

fuhlig1 avatar Jul 08 '21 08:07 fuhlig1