I am playing around with clang C++ Modules, trying to figure ou how it could be leveraged to get rid of build systems on simple projects.
I want to use the link attribute in the modulemap to indicate which library should be linked when a module is imported, however AFAIK the capability to automatically add these flags to the link flags depends on the underlying linker and the GNU linker doesn’t support it.
At the moment I use LLVM bitcode as my object file format, because Clang keeps the module link flags in a “Linker Options” attribute, and I can parse the object files afterwards to extract these flags and add them to my link command-line. Clang does then the lowering to native objects and the actual native linker call.
I would like to simplify this, and I was thinking that Clang could parse the object files itself if they are LLVM BC, and then pass the flags to the linker if the link step is done in the same execution.
I have two way of doing it in mind, first way would be to have the driver parse the objects and add the required flags to the link action, but maybe it’s not the right place to do that. Another way would be to add the linker flags in some private ELF section during the compilation phase, and have the link phase load them afterwards, which could work even without LLVM bitcode intermediate.
Does that makes sense? Any thoughts, advices?
Sounds plausible - as for which direction to go, I don’t know (it’s hardly my area of expertise) though the bitcode/module-agnostic version does seem preferable. Is there any prior art in the area? One or more linkers that already support the sort of feature you’ve described (so the linker looks for the special object file section with the info, rather than the compile driver?)
We already implement autolinking on Mac and Windows, but not Linux. So far
as I know there is no way for an ELF object file to tell the linker to pull
in more libraries, but MachO and COFF can do this.
Wouldn't it be possible to create a custom section in the ELF object for
the linker flags, and have Clang parse them when it is used as a linker
invocation then pass them down to the linker? Obvisouly this won't work
when invoking the linker alone, but it could be a first step.
I believe Swift compiler does this kind of thing:
(remembering to cc: cfe-dev this time)
On PS4 we put those kinds of smarts into the linker. We invented a custom ELF section for passing things like extra library names (a-la pragma comment lib) plus the linker will auto-detect BC files and run them through LTO.
I’ve also seen a platform (not sure which) that invented really elaborate mechanisms that they then stuck into the .comment section.