mold icon indicating copy to clipboard operation
mold copied to clipboard

Use mold with CMAKE_LANG_LINKER_LAUNCHER

Open BenjaminBeichler opened this issue 3 years ago • 8 comments

I played around with the mold configuration options, and discovered, that you can use mold with CMAKE_CXX_LINKER_LAUNCHER and mold -run. Unfortunately, I needed to create a separate launcher script (e.g. named mold-run):

#!/bin/bash
mold -run $@

Afterwards, you can configure cmake with -DCMAKE_CXX_LINKER_LAUNCHER=mold-run and it does the trick. If I put -DCMAKE_CXX_LINKER_LAUNCHER="mold -run" it is badly escaped (maybe a cmake bug or desired).

If you are willing to accept a PR with the launcher script and some additional documentation, I could create it.

BenjaminBeichler avatar Jan 17 '22 22:01 BenjaminBeichler

Is this a common option? CMAKE_CXX_LINKER_LAUNCHER returns only two results when I search for that word in Google.

rui314 avatar Jan 18 '22 07:01 rui314

It is quite new (CMake 2.21) and I think it is a reaction on mold and other linkers. The documented entry is CMAKE_LANG_LINKER_LAUNCHER (https://cmake.org/cmake/help/v3.21/variable/CMAKE_LANG_LINKER_LAUNCHER.html)

BenjaminBeichler avatar Jan 18 '22 09:01 BenjaminBeichler

Ah, nice. But I wonder if this flag is preferred over LDFLAGS, as I believe LDFLAGS=-B/usr/local/libexec/mold cmake works as well.

rui314 avatar Jan 18 '22 10:01 rui314

Mhh good question: I was searching for a Setting, which could be done globally, not project specific. I think the better ones would be:

  • CMAKE_EXE_LINKER_FLAGS
  • CMAKE_SHARED_LINKER_FLAGS
  • CMAKE_STATIC_LINKER_FLAGS

but I do not totally recognize the pros and cons, except that I do not know whether cmake use this commandline argument to append this to the default configuration or the default config is replaced.

I also experienced one "problem" with my LINKER_LAUNCHER method: I do not see mold in the .comment section of statically linked libraries. Is that normal behavior?

BenjaminBeichler avatar Jan 18 '22 14:01 BenjaminBeichler

No, it's not. mold always leaves its identification string in the .comment section. If there's a .comment section in your binary but if you can't find "mold" in it, it is pretty likely that the binary is not built by mold.

rui314 avatar Jan 18 '22 14:01 rui314

Mhh okay, then this seems to be a cmake bug (maybe as the escaping problem). When I found time for this, I may bring this up on cmake side and would keep this issue lingering, okay?

BenjaminBeichler avatar Jan 18 '22 14:01 BenjaminBeichler

Sure, no problem.

rui314 avatar Jan 18 '22 14:01 rui314

No, it's not. mold always leaves its identification string in the .comment section. If there's a .comment section in your binary but if you can't find "mold" in it, it is pretty likely that the binary is not built by mold.

Can I clarify on this: I meant the process of building a libxxx.a file. It seems to be, that this does not involve the linker but only the archiver. I tried the already described methods for using mold and the the readelf call on my libxxx.a only dumps out .comment sections of the several objects files (without mold tag).

BenjaminBeichler avatar Jan 18 '22 15:01 BenjaminBeichler

I just want to note, when g++ was built with the --with-ld, this overrides all flags, and -B/usr/local/libexec/mold doesn't work. That is the case in our environment, and it took me a long time to figure out why it didn't work. The mold -run script and using CMAKE_CXX_LINKER_LAUNCHER works

martinus avatar Aug 30 '23 10:08 martinus

It looks like CMake 3.29 will support a CMAKE_LINKER_TYPE=mold option, as the MR was just merged today. https://gitlab.kitware.com/cmake/cmake/-/merge_requests/8861

ADKaster avatar Oct 16 '23 16:10 ADKaster