Can I get CMake to generate Makefiles utilizing gcc incremental linking?

613 views Asked by At

I've recently become aware of gcc's incremental linking feature, and I want to use it. The thing is, I don't write my own Makefile's - I use CMake. The inter-file dependencies and the targets are essentially the same, but I want to have CMake try to obtain them using incremental linking rather than linking from scratch?

Moreover, if it's possible to have this even for files-within libraries, i.e. when you recompile one .o within a .a file, instead of that whole file be reconsidered when linking the executable, only the single .o within it is reconsidered/reapplied.

To illustrate, suppose my CMakeLists.txt has:

add_executable(
    foo
    a.cpp
    b.cpp
    c.cpp
)

Right now, when a.cpp changes, we get a compilation a.cpp -> a.o then a regular linkage a.o b.o c.o -> foo. I want it to be a.o foo something_else_maybe -> foo

Note: This question is not about MSVC and its own incremental linking capabilities.

1

There are 1 answers

0
yugr On

I've recently become aware of gcc's incremental linking feature

I think we need to clarify some terms. Incremental links is linker feature which allows you to speed up linking when only small subset of object files has changed. It does so by re-using results of previous link.

GNU ld does not have such a feature. What it can do is relocatable link i.e. combine several objects into one. If you link a.o and b.o to ab.o and then modify a.o, it'll not be able to reuse results of relocatable link so you'll have to re-link ab.o from scratch (as opposed to honest incremental linking).

I want to have CMake try to obtain them using incremental linking rather than linking from scratch

I'm afraid that CMake (or any other build system) does not provide support for this for several reasons.

First of all in this case you'll have to cache results of ld -r for all possible subsets of your object files. This number grows exponentially which makes it impractical.

Secondly, there is more to linking the app besides linking it's object files: library linking, generation of dynamic sections (PLT, relocations, etc.), relaxation, etc. which will have to be done from scratch every time, even if you somehow manage to use -r. It can easily turn up to take much longer time than just linking object files.