diff options
author | Dudemanguy <random342@airmail.cc> | 2022-01-10 19:02:12 -0600 |
---|---|---|
committer | Eli Schwartz <eschwartz93@gmail.com> | 2022-05-03 23:03:56 -0400 |
commit | 557680f7d6b2a2e7c3d0f04fb0d6529ac7a6d475 (patch) | |
tree | dedf300cdb4b39a83e7bfa9eb7720f7fb43f0352 | |
parent | 8b3a54e5085933b78a8509103a76bed7ca8cdde4 (diff) | |
download | meson-557680f7d6b2a2e7c3d0f04fb0d6529ac7a6d475.zip meson-557680f7d6b2a2e7c3d0f04fb0d6529ac7a6d475.tar.gz meson-557680f7d6b2a2e7c3d0f04fb0d6529ac7a6d475.tar.bz2 |
add prefer_static built-in option
By default, meson will try to look for shared libraries first before
static ones. In the meson.build itself, one can use the static keyword
to control if a static library will be tried first but there's no simple
way for an end user performing a build to switch back and forth at will.
Let's cover this usecase by adding an option that allows a user to
specify if they want dependency lookups to try static or shared
libraries first. The writer of the meson.build can manually specify the
static keyword where appropriate which will override the value of this
option.
-rw-r--r-- | docs/markdown/Builtin-options.md | 1 | ||||
-rw-r--r-- | docs/markdown/Configuring-a-build-directory.md | 1 | ||||
-rw-r--r-- | docs/markdown/snippets/prefer_static.md | 9 | ||||
-rw-r--r-- | mesonbuild/coredata.py | 1 | ||||
-rw-r--r-- | mesonbuild/dependencies/__init__.py | 8 | ||||
-rw-r--r-- | mesonbuild/dependencies/base.py | 4 | ||||
-rw-r--r-- | mesonbuild/dependencies/misc.py | 2 | ||||
-rw-r--r-- | mesonbuild/dependencies/scalapack.py | 4 | ||||
-rw-r--r-- | mesonbuild/interpreter/compiler.py | 4 | ||||
-rw-r--r-- | mesonbuild/mesonlib/universal.py | 1 |
10 files changed, 28 insertions, 7 deletions
diff --git a/docs/markdown/Builtin-options.md b/docs/markdown/Builtin-options.md index c524fe4..1e24144 100644 --- a/docs/markdown/Builtin-options.md +++ b/docs/markdown/Builtin-options.md @@ -81,6 +81,7 @@ machine](#specifying-options-per-machine) section for details. | layout {mirror,flat} | mirror | Build directory layout | no | no | | optimization {0, g, 1, 2, 3, s} | 0 | Optimization level | no | no | | pkg_config_path {OS separated path} | '' | Additional paths for pkg-config to search before builtin paths | yes | no | +| prefer_static | false | Whether to try static linking before shared linking | no | no | | cmake_prefix_path | [] | Additional prefixes for cmake to search before builtin paths | yes | no | | stdsplit | true | Split stdout and stderr in test logs | no | no | | strip | false | Strip targets on install | no | no | diff --git a/docs/markdown/Configuring-a-build-directory.md b/docs/markdown/Configuring-a-build-directory.md index 1eb8478..34bde4d 100644 --- a/docs/markdown/Configuring-a-build-directory.md +++ b/docs/markdown/Configuring-a-build-directory.md @@ -31,6 +31,7 @@ a sample output for a simple project. install_umask 0022 [preserve, 0000-0777] Default umask to apply on permissions of installed files layout mirror [mirror, flat] Build directory layout optimization 3 [0, g, 1, 2, 3, s] Optimization level + prefer_static false [true, false] Whether to try static linking before shared linking strip false [true, false] Strip targets on install unity off [on, off, subprojects] Unity build warning_level 1 [0, 1, 2, 3] Compiler warning level to use diff --git a/docs/markdown/snippets/prefer_static.md b/docs/markdown/snippets/prefer_static.md new file mode 100644 index 0000000..c63323b --- /dev/null +++ b/docs/markdown/snippets/prefer_static.md @@ -0,0 +1,9 @@ +## New prefer_static built-in option + +Users can now set a boolean, `prefer_static`, that controls whether or not +static linking should be tried before shared linking. This option acts as +strictly a preference. If the preferred linking method is not successful, +then Meson will fallback and try the other linking method. Specifically +setting the `static` kwarg in the meson.build will take precedence over +the value of `prefer_static` for that specific `dependency` or +`find_library` call. diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py index 162a01b..48881f0 100644 --- a/mesonbuild/coredata.py +++ b/mesonbuild/coredata.py @@ -1229,6 +1229,7 @@ BUILTIN_CORE_OPTIONS: 'MutableKeyedOptionDictType' = OrderedDict([ (OptionKey('install_umask'), BuiltinOption(UserUmaskOption, 'Default umask to apply on permissions of installed files', '022')), (OptionKey('layout'), BuiltinOption(UserComboOption, 'Build directory layout', 'mirror', choices=['mirror', 'flat'])), (OptionKey('optimization'), BuiltinOption(UserComboOption, 'Optimization level', '0', choices=['0', 'g', '1', '2', '3', 's'])), + (OptionKey('prefer_static'), BuiltinOption(UserBooleanOption, 'Whether to try static linking before shared linking', False)), (OptionKey('stdsplit'), BuiltinOption(UserBooleanOption, 'Split stdout and stderr in test logs', True)), (OptionKey('strip'), BuiltinOption(UserBooleanOption, 'Strip targets on install', False)), (OptionKey('unity'), BuiltinOption(UserComboOption, 'Unity build', 'off', choices=['on', 'off', 'subprojects'])), diff --git a/mesonbuild/dependencies/__init__.py b/mesonbuild/dependencies/__init__.py index cdadd56..bbf31ad 100644 --- a/mesonbuild/dependencies/__init__.py +++ b/mesonbuild/dependencies/__init__.py @@ -142,8 +142,8 @@ There are a couple of things about this that still aren't ideal. For one, we don't want to be reading random environment variables at this point. Those should actually be added to `envconfig.Properties` and read in `environment.Environment._set_default_properties_from_env` (see how -`BOOST_ROOT` is handled). We can also handle the `static` keyword. So -now that becomes: +`BOOST_ROOT` is handled). We can also handle the `static` keyword and the +`prefer_static` built-in option. So now that becomes: ```python class FooSystemDependency(ExternalDependency): @@ -156,7 +156,9 @@ class FooSystemDependency(ExternalDependency): self.is_found = False return - static = Mesonlib.LibType.STATIC if kwargs.get('static', False) else Mesonlib.LibType.SHARED + get_option = environment.coredata.get_option + static_opt = kwargs.get('static', get_option(Mesonlib.OptionKey('prefer_static')) + static = Mesonlib.LibType.STATIC if static_opt else Mesonlib.LibType.SHARED lib = self.clib_compiler.find_library( 'foo', environment, [os.path.join(root, 'lib')], libtype=static) if lib is None: diff --git a/mesonbuild/dependencies/base.py b/mesonbuild/dependencies/base.py index 97a19be..8e4d556 100644 --- a/mesonbuild/dependencies/base.py +++ b/mesonbuild/dependencies/base.py @@ -25,7 +25,7 @@ from enum import Enum from .. import mlog, mesonlib from ..compilers import clib_langs -from ..mesonlib import LibType, MachineChoice, MesonException, HoldableObject +from ..mesonlib import LibType, MachineChoice, MesonException, HoldableObject, OptionKey from ..mesonlib import version_compare_many #from ..interpreterbase import FeatureDeprecated, FeatureNew @@ -338,7 +338,7 @@ class ExternalDependency(Dependency, HasNativeKwarg): self.version_reqs = [self.version_reqs] self.required = kwargs.get('required', True) self.silent = kwargs.get('silent', False) - self.static = kwargs.get('static', False) + self.static = kwargs.get('static', self.env.coredata.get_option(OptionKey('prefer_static'))) self.libtype = LibType.STATIC if self.static else LibType.PREFER_SHARED if not isinstance(self.static, bool): raise DependencyException('Static keyword must be boolean') diff --git a/mesonbuild/dependencies/misc.py b/mesonbuild/dependencies/misc.py index 5292d02..2edb0c7 100644 --- a/mesonbuild/dependencies/misc.py +++ b/mesonbuild/dependencies/misc.py @@ -612,7 +612,7 @@ def shaderc_factory(env: 'Environment', shared_libs = ['shaderc'] static_libs = ['shaderc_combined', 'shaderc_static'] - if kwargs.get('static', False): + if kwargs.get('static', env.coredata.get_option(mesonlib.OptionKey('prefer_static'))): c = [functools.partial(PkgConfigDependency, name, env, kwargs) for name in static_libs + shared_libs] else: diff --git a/mesonbuild/dependencies/scalapack.py b/mesonbuild/dependencies/scalapack.py index 707e698..656978d 100644 --- a/mesonbuild/dependencies/scalapack.py +++ b/mesonbuild/dependencies/scalapack.py @@ -17,6 +17,7 @@ import functools import os import typing as T +from ..mesonlib import OptionKey from .base import DependencyMethods from .base import DependencyException from .cmake import CMakeDependency @@ -35,7 +36,8 @@ def scalapack_factory(env: 'Environment', for_machine: 'MachineChoice', candidates: T.List['DependencyGenerator'] = [] if DependencyMethods.PKGCONFIG in methods: - mkl = 'mkl-static-lp64-iomp' if kwargs.get('static', False) else 'mkl-dynamic-lp64-iomp' + static_opt = kwargs.get('static', env.coredata.get_option(OptionKey('prefer_static'))) + mkl = 'mkl-static-lp64-iomp' if static_opt else 'mkl-dynamic-lp64-iomp' candidates.append(functools.partial( MKLPkgConfigDependency, mkl, env, kwargs)) diff --git a/mesonbuild/interpreter/compiler.py b/mesonbuild/interpreter/compiler.py index 185d151..c5db6aa 100644 --- a/mesonbuild/interpreter/compiler.py +++ b/mesonbuild/interpreter/compiler.py @@ -17,6 +17,7 @@ from ..interpreterbase import (ObjectHolder, noPosargs, noKwargs, FeatureNew, disablerIfNotFound, InterpreterException) from ..interpreterbase.decorators import ContainerTypeInfo, typed_kwargs, KwargInfo, typed_pos_args +from ..mesonlib import OptionKey from .interpreterobjects import (extract_required_kwarg, extract_search_dirs) from .type_checking import REQUIRED_KW, in_set_validator, NoneType @@ -592,10 +593,13 @@ class CompilerHolder(ObjectHolder['Compiler']): search_dirs = extract_search_dirs(kwargs) + prefer_static = self.environment.coredata.get_option(OptionKey('prefer_static')) if kwargs['static'] is True: libtype = mesonlib.LibType.STATIC elif kwargs['static'] is False: libtype = mesonlib.LibType.SHARED + elif prefer_static: + libtype = mesonlib.LibType.PREFER_STATIC else: libtype = mesonlib.LibType.PREFER_SHARED linkargs = self.compiler.find_library(libname, self.environment, search_dirs, libtype) diff --git a/mesonbuild/mesonlib/universal.py b/mesonbuild/mesonlib/universal.py index 055b8f0..1213be6 100644 --- a/mesonbuild/mesonlib/universal.py +++ b/mesonbuild/mesonlib/universal.py @@ -2022,6 +2022,7 @@ _BUILTIN_NAMES = { 'install_umask', 'layout', 'optimization', + 'prefer_static', 'stdsplit', 'strip', 'unity', |