How do I speed up Meson build when many targets use the same C++ sources
Asked Answered
W

1

7

I have a new meson project with 58 targets. Many of them use the same *.cc files, and meson builds each *.cc file once for each target, which is much slower than autotools. Also, the compile flags should be the same between targets, so in theory meson should be able to re-use the objects.

Is building many targets well-supported in meson? Is there a standard solution for situations like this? I could try to organize all of the sources in my project into shared libraries, but then I would have to decide how to group them, and with autotools I don't have to decide that. I haven't managed to find documentation on this.

-BenRI

P.S. Here is a minimal example:

-------- file: app1.cc -------

int f(int x ,int y) {return x;}
int main() { return f(0,1);}

------ file: meson.build -----

project('app12','cpp')
executable('app1',['app1.cc'])
executable('app2',['app1.cc'])

---------- command -----------

% meson . meson
% cd meson
% meson configure -Dwarning_level=3
% ninja

You should see a warning for unused parameter 'y' occur twice. The file compile_commands.json also has two entries for app1.cc .

Winner answered 8/12, 2017 at 12:18 Comment(4)
Have you read the documentation discussing reuse of object files between targets? It's impossible to be more specific since you haven't created an MCVE to showHaggi
Yeah, I read that. It "strongly [recommends] that you only use this feature for generating unit test executables in the manner described above." Also, manually creating an extracted object for each source file shared between build targets doesn't seem like the meson way, since it requires the user to specify things that the system already knows and the user could get wrong.Winner
I can add an MCVE if that would really help, but since rebuilding each source file for each target is the acknowledged behavior (I think) I'm not sure what it would show.Winner
Personally I'd say restructure the code to use libraries.Football
W
5

Having the same source compiled with the same compile flags again and again is just waste of CPU power because it will generate the same binary file unless you have unreproducible build tricks like date and time.

If what you meant was to build common code for all targets, you can just use static_library to build your code and reuse it from all of your target binary.

Move your f() in lib.cc and use the following meson.build.

project('app12','cpp')
mylib = static_library('mylib', 'lib.cc')
executable('app1', 'app1.cc', link_with : mylib)
executable('app2', 'app1.cc', link_with : mylib)
Waterfowl answered 9/12, 2017 at 5:45 Comment(1)
I think moving shared code into a static library is indeed the approach recommended by meson developers. I did this, and it works fine. Interestingly, I was worried that linking a static library containing so many objects to every target would increase the size of the executables, but in fact size decreased substantially.Winner

© 2022 - 2024 — McMap. All rights reserved.