4

I'm building app with flto and I want to parallelise it on all cores that are used in make e.g if I run it with make -j4 then on 4 cores

Example:

cmake_minimum_required(VERSION 2.8)
project(a)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto=jobserver")
add_executable(a a.cpp b.cpp)

where a.cpp and b.cpp are any reasonable cpps (and one of them has main)

Running with make -j2 gives me:

-- Configuring done
-- Generating done
-- Build files have been written to: ...
[ 33%] Building CXX object CMakeFiles/a.dir/b.cpp.o
[ 66%] Building CXX object CMakeFiles/a.dir/a.cpp.o
[100%] Linking CXX executable a
make[3]: warning: jobserver unavailable: using -j1.  Add '+' to parent make rule.
[100%] Built target a

So, only one core is used. Is it possible to pass information about jobserver to gcc here?

RiaD
  • 46,822
  • 11
  • 79
  • 123
  • Would taking an alternative like [`ninja` build system](https://ninja-build.org/) be an option? That has really speed up [my project builds](http://stackoverflow.com/questions/37327526/how-to-speed-up-compile-time-of-my-cmake-enabled-c-project) and the nice thing about CMake is that you can easily switch to generate another build environment (to give it a try). – Florian Dec 23 '16 at 12:37
  • @Florian, I'll look into it, but make solution would be preferable – RiaD Dec 25 '16 at 11:37
  • What version of cmake are you using? Your minimum version of 2.8 is ancient. – James Moore Apr 23 '18 at 16:58

3 Answers3

0

Unfortunately, the only fix to this problem seems to be modifying the makefile(s) generated by cmake. You will have to add the + prefix to all recipe lines of the form

$(CMAKE_COMMAND) -E cmake_link_script path/to/link.txt ...

where the linker command in the file path/to/link.txt contains the -flto=jobserver option.

Leon
  • 31,443
  • 4
  • 72
  • 97
0

As a workaround you may try forcing LTO job count:

set(CMAKE_CXX_COMPILE_OPTIONS_IPO ${CMAKE_CXX_COMPILE_OPTIONS_IPO} -flto=8)
trozen
  • 1,117
  • 13
  • 13
0

Please comment on this bug to bump its priority: https://gitlab.kitware.com/cmake/cmake/-/issues/17781 In the meantime, you have to use fixed -flto=x

TFR
  • 81
  • 3