aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/compilers
diff options
context:
space:
mode:
Diffstat (limited to 'mesonbuild/compilers')
-rw-r--r--mesonbuild/compilers/compilers.py20
-rw-r--r--mesonbuild/compilers/mixins/clang.py10
-rw-r--r--mesonbuild/compilers/mixins/gnu.py17
3 files changed, 42 insertions, 5 deletions
diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py
index 0f83f4c..07569a7 100644
--- a/mesonbuild/compilers/compilers.py
+++ b/mesonbuild/compilers/compilers.py
@@ -268,6 +268,8 @@ clike_debug_args = {False: [],
base_options: 'KeyedOptionDictType' = {
OptionKey('b_pch'): coredata.UserBooleanOption('Use precompiled headers', True),
OptionKey('b_lto'): coredata.UserBooleanOption('Use link time optimization', False),
+ OptionKey('b_lto'): coredata.UserBooleanOption('Use link time optimization', False),
+ OptionKey('b_lto_threads'): coredata.UserIntegerOption('Use multiple threads for Link Time Optimization', (None, None,0)),
OptionKey('b_sanitize'): coredata.UserComboOption('Code sanitizer to use',
['none', 'address', 'thread', 'undefined', 'memory', 'address,undefined'],
'none'),
@@ -300,11 +302,25 @@ def option_enabled(boptions: T.Set[OptionKey], options: 'KeyedOptionDictType',
except KeyError:
return False
+
+def get_option_value(options: 'KeyedOptionDictType', opt: OptionKey, fallback: '_T') -> '_T':
+ """Get the value of an option, or the fallback value."""
+ try:
+ v: '_T' = options[opt].value
+ except KeyError:
+ return fallback
+
+ assert isinstance(v, type(fallback)), f'Should have {type(fallback)!r} but was {type(v)!r}'
+ # Mypy doesn't understand that the above assert ensures that v is type _T
+ return v
+
+
def get_base_compile_args(options: 'KeyedOptionDictType', compiler: 'Compiler') -> T.List[str]:
args = [] # type T.List[str]
try:
if options[OptionKey('b_lto')].value:
- args.extend(compiler.get_lto_compile_args())
+ args.extend(compiler.get_lto_compile_args(
+ threads=get_option_value(options, OptionKey('b_lto_threads'), 0)))
except KeyError:
pass
try:
@@ -926,7 +942,7 @@ class Compiler(metaclass=abc.ABCMeta):
ret.append(arg)
return ret
- def get_lto_compile_args(self) -> T.List[str]:
+ def get_lto_compile_args(self, *, threads: int = 0) -> T.List[str]:
return []
def get_lto_link_args(self) -> T.List[str]:
diff --git a/mesonbuild/compilers/mixins/clang.py b/mesonbuild/compilers/mixins/clang.py
index fcb2225..9c17a55 100644
--- a/mesonbuild/compilers/mixins/clang.py
+++ b/mesonbuild/compilers/mixins/clang.py
@@ -49,7 +49,8 @@ class ClangCompiler(GnuLikeCompiler):
super().__init__()
self.id = 'clang'
self.defines = defines or {}
- self.base_options.add(OptionKey('b_colorout'))
+ self.base_options.update({OptionKey('b_colorout'), OptionKey('b_lto_threads')})
+
# TODO: this really should be part of the linker base_options, but
# linkers don't have base_options.
if isinstance(self.linker, AppleDynamicLinker):
@@ -135,3 +136,10 @@ class ClangCompiler(GnuLikeCompiler):
def get_coverage_link_args(self) -> T.List[str]:
return ['--coverage']
+
+ def get_lto_compile_args(self, *, threads: int = 0) -> T.List[str]:
+ args = super().get_lto_compile_args(threads=threads)
+ # In clang -flto=0 means auto
+ if threads >= 0:
+ args.append(f'-flto-jobs={threads}')
+ return args
diff --git a/mesonbuild/compilers/mixins/gnu.py b/mesonbuild/compilers/mixins/gnu.py
index 95bcd7c..5afbb83 100644
--- a/mesonbuild/compilers/mixins/gnu.py
+++ b/mesonbuild/compilers/mixins/gnu.py
@@ -17,6 +17,7 @@
import abc
import functools
import os
+import multiprocessing
import pathlib
import re
import subprocess
@@ -281,7 +282,9 @@ class GnuLikeCompiler(Compiler, metaclass=abc.ABCMeta):
return self._split_fetch_real_dirs(line.split('=', 1)[1])
return []
- def get_lto_compile_args(self) -> T.List[str]:
+ def get_lto_compile_args(self, *, threads: int = 0) -> T.List[str]:
+ # This provides a base for many compilers, GCC and Clang override this
+ # for their specific arguments
return ['-flto']
def sanitizer_compile_args(self, value: str) -> T.List[str]:
@@ -330,7 +333,7 @@ class GnuCompiler(GnuLikeCompiler):
super().__init__()
self.id = 'gcc'
self.defines = defines or {}
- self.base_options.add(OptionKey('b_colorout'))
+ self.base_options.update({OptionKey('b_colorout'), OptionKey('b_lto_threads')})
def get_colorout_args(self, colortype: str) -> T.List[str]:
if mesonlib.version_compare(self.version, '>=4.9.0'):
@@ -383,3 +386,13 @@ class GnuCompiler(GnuLikeCompiler):
def get_prelink_args(self, prelink_name: str, obj_list: T.List[str]) -> T.List[str]:
return ['-r', '-o', prelink_name] + obj_list
+
+ def get_lto_compile_args(self, *, threads: int = 0) -> T.List[str]:
+ if threads == 0:
+ if mesonlib.version_compare(self.version, '>= 10.0'):
+ return ['-flto=auto']
+ # This matches clang's behavior of using the number of cpus
+ return [f'-flto={multiprocessing.cpu_count()}']
+ elif threads > 0:
+ return [f'-flto={threads}']
+ return super().get_lto_compile_args(threads=threads)