Skip to content

Commit

Permalink
add prefer_static built-in option
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Dudemanguy committed Jan 11, 2022
1 parent 6ce9269 commit 85e11b5
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/markdown/Builtin-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,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 |
Expand Down
1 change: 1 addition & 0 deletions docs/markdown/Configuring-a-build-directory.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions docs/markdown/snippets/prefer_static.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## 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 has the same
effect as setting the `static` keyword in the `dependency` and `find_library`
functions. Specifically setting `static` will override the project value of
`prefer_static` for that specific `dependency` or `find_library` call.
1 change: 1 addition & 0 deletions mesonbuild/coredata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,7 @@ def add_to_argparse(self, name: str, parser: argparse.ArgumentParser, help_suffi
(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'])),
Expand Down
8 changes: 5 additions & 3 deletions mesonbuild/dependencies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.
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):
Expand All @@ -154,7 +154,9 @@ def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.
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:
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/dependencies/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from .. import mlog
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

Expand Down Expand Up @@ -319,7 +319,7 @@ def __init__(self, type_name: DependencyTypeName, environment: 'Environment', kw
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')
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/dependencies/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,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:
Expand Down
4 changes: 3 additions & 1 deletion mesonbuild/dependencies/scalapack.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
import typing as T

from ..mesonlib import OptionKey
from .base import DependencyMethods
from .base import DependencyException
from .cmake import CMakeDependency
Expand All @@ -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))

Expand Down
1 change: 1 addition & 0 deletions mesonbuild/mesonlib/universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,7 @@ class OptionType(enum.IntEnum):
'install_umask',
'layout',
'optimization',
'prefer_static',
'stdsplit',
'strip',
'unity',
Expand Down

0 comments on commit 85e11b5

Please sign in to comment.