summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Brunet <charles.brunet@optelgroup.com>2023-12-12 15:49:59 -0500
committerDylan Baker <dylan@pnwbakers.com>2024-09-06 10:56:44 -0700
commit2d6915a5983a64b58ecafd1b1dc92e9c48579ff2 (patch)
tree8ed2d004c51ef4d36c502b5851ff9f43adf361d4
parent7b3169f464810dfed0daf3075a5a6e5ed91dbc9b (diff)
downloadmeson-2d6915a5983a64b58ecafd1b1dc92e9c48579ff2.tar.gz
add default_both_libraries core option
-rw-r--r--docs/markdown/Builtin-options.md11
-rw-r--r--docs/markdown/snippets/default_both_libraries.md4
-rw-r--r--mesonbuild/build.py4
-rw-r--r--mesonbuild/interpreter/interpreter.py7
-rw-r--r--mesonbuild/options.py2
-rw-r--r--test cases/common/273 both libraries/meson.build44
-rw-r--r--test cases/common/273 both libraries/src/api.h15
-rw-r--r--test cases/common/273 both libraries/src/library.c10
-rw-r--r--test cases/common/273 both libraries/src/library.h5
-rw-r--r--test cases/common/273 both libraries/src/main.c8
-rw-r--r--test cases/common/273 both libraries/test.json16
11 files changed, 123 insertions, 3 deletions
diff --git a/docs/markdown/Builtin-options.md b/docs/markdown/Builtin-options.md
index 6adc4218b..178f793e4 100644
--- a/docs/markdown/Builtin-options.md
+++ b/docs/markdown/Builtin-options.md
@@ -79,6 +79,7 @@ machine](#specifying-options-per-machine) section for details.
| genvslite {vs2022} | vs2022 | Setup multi-builtype ninja build directories and Visual Studio solution | no | no |
| buildtype {plain, debug,<br>debugoptimized, release, minsize, custom} | debug | Build type to use | no | no |
| debug | true | Enable debug symbols and other information | no | no |
+| default_both_libraries {shared, static, auto} | shared | Default library type for both_libraries | no | no |
| default_library {shared, static, both} | shared | Default library type | no | yes |
| errorlogs | true | Whether to print the logs from failing tests. | no | no |
| install_umask {preserve, 0000-0777} | 022 | Default umask to apply on permissions of installed files | no | no |
@@ -177,6 +178,16 @@ fails.
`vsenv` is `true` by default when using the `vs` backend.
+
+#### Details for `default_both_libraries`
+
+Since `1.6.0`, you can select the default type of library selected when using
+a `both_libraries` object. This can be either 'shared' (default value, compatible
+with previous meson versions), 'static', or 'auto'. With auto, the value from
+`default_library` option is used, unless it is 'both', in which case 'shared'
+is used instead.
+
+
## Base options
These are set in the same way as universal options, either by
diff --git a/docs/markdown/snippets/default_both_libraries.md b/docs/markdown/snippets/default_both_libraries.md
new file mode 100644
index 000000000..279c43d22
--- /dev/null
+++ b/docs/markdown/snippets/default_both_libraries.md
@@ -0,0 +1,4 @@
+## New built-in option for default both_libraries
+
+`both_libraries` targets used to be considered as a shared library by default.
+There is now the `default_both_libraries` option to change this default.
diff --git a/mesonbuild/build.py b/mesonbuild/build.py
index 45ff4cc2c..122d94774 100644
--- a/mesonbuild/build.py
+++ b/mesonbuild/build.py
@@ -2486,8 +2486,8 @@ class SharedModule(SharedLibrary):
return self.environment.get_shared_module_dir(), '{moduledir_shared}'
class BothLibraries(SecondLevelHolder):
- def __init__(self, shared: SharedLibrary, static: StaticLibrary) -> None:
- self._preferred_library = 'shared'
+ def __init__(self, shared: SharedLibrary, static: StaticLibrary, preferred_library: Literal['shared', 'static', 'auto']) -> None:
+ self._preferred_library = preferred_library
self.shared = shared
self.static = static
self.subproject = self.shared.subproject
diff --git a/mesonbuild/interpreter/interpreter.py b/mesonbuild/interpreter/interpreter.py
index d69f569c2..e3e1e5b67 100644
--- a/mesonbuild/interpreter/interpreter.py
+++ b/mesonbuild/interpreter/interpreter.py
@@ -3230,6 +3230,11 @@ class Interpreter(InterpreterBase, HoldableObject):
def build_both_libraries(self, node: mparser.BaseNode, args: T.Tuple[str, SourcesVarargsType], kwargs: kwtypes.Library) -> build.BothLibraries:
shared_lib = self.build_target(node, args, kwargs, build.SharedLibrary)
static_lib = self.build_target(node, args, kwargs, build.StaticLibrary)
+ preferred_library = self.coredata.get_option(OptionKey('default_both_libraries'))
+ if preferred_library == 'auto':
+ preferred_library = self.coredata.get_option(OptionKey('default_library'))
+ if preferred_library == 'both':
+ preferred_library = 'shared'
if self.backend.name == 'xcode':
# Xcode is a bit special in that you can't (at least for the moment)
@@ -3261,7 +3266,7 @@ class Interpreter(InterpreterBase, HoldableObject):
# Keep only compilers used for linking
static_lib.compilers = {k: v for k, v in static_lib.compilers.items() if k in compilers.clink_langs}
- return build.BothLibraries(shared_lib, static_lib)
+ return build.BothLibraries(shared_lib, static_lib, preferred_library)
def build_library(self, node: mparser.BaseNode, args: T.Tuple[str, SourcesVarargsType], kwargs: kwtypes.Library):
default_library = self.coredata.get_option(OptionKey('default_library', subproject=self.subproject))
diff --git a/mesonbuild/options.py b/mesonbuild/options.py
index 020c2e623..f1f3554c2 100644
--- a/mesonbuild/options.py
+++ b/mesonbuild/options.py
@@ -63,6 +63,7 @@ _BUILTIN_NAMES = {
'buildtype',
'debug',
'default_library',
+ 'default_both_libraries',
'errorlogs',
'genvslite',
'install_umask',
@@ -624,6 +625,7 @@ BUILTIN_CORE_OPTIONS: T.Dict['OptionKey', 'BuiltinOption'] = OrderedDict([
(OptionKey('debug'), BuiltinOption(UserBooleanOption, 'Enable debug symbols and other information', True)),
(OptionKey('default_library'), BuiltinOption(UserComboOption, 'Default library type', 'shared', choices=['shared', 'static', 'both'],
yielding=False)),
+ (OptionKey('default_both_libraries'), BuiltinOption(UserComboOption, 'Default library type for both_libraries', 'shared', choices=['shared', 'static', 'auto'])),
(OptionKey('errorlogs'), BuiltinOption(UserBooleanOption, "Whether to print the logs from failing tests", True)),
(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'])),
diff --git a/test cases/common/273 both libraries/meson.build b/test cases/common/273 both libraries/meson.build
new file mode 100644
index 000000000..b80a9ce7d
--- /dev/null
+++ b/test cases/common/273 both libraries/meson.build
@@ -0,0 +1,44 @@
+project(
+ 'test both libraries',
+ 'c',
+ meson_version: '>= 1.6.0',
+)
+
+expected = 0
+
+
+with_library = library(
+ 'with_library',
+ files('src/library.c'),
+ c_shared_args: ['-DEXPORT'],
+)
+
+with_library_dep = declare_dependency(
+ link_with: with_library,
+)
+
+if get_option('default_library') == 'shared'
+ expected += 1
+elif get_option('default_library') == 'both'
+ if get_option('default_both_libraries') in ['shared', 'auto']
+ expected += 1
+ endif
+endif
+
+
+
+mainlink = executable(
+ 'mainlink',
+ files('src/main.c'),
+ c_args: [f'-DEXPECTED=@expected@'],
+ link_with: [with_library],
+)
+test('link with', mainlink)
+
+maindep = executable(
+ 'maindep',
+ files('src/main.c'),
+ c_args: [f'-DEXPECTED=@expected@'],
+ dependencies: [with_library_dep],
+)
+test('use dep', maindep)
diff --git a/test cases/common/273 both libraries/src/api.h b/test cases/common/273 both libraries/src/api.h
new file mode 100644
index 000000000..a20ded364
--- /dev/null
+++ b/test cases/common/273 both libraries/src/api.h
@@ -0,0 +1,15 @@
+#pragma once
+
+#if defined EXPORT
+ #if defined _WIN32 || defined __CYGWIN__
+ #define API __declspec(dllexport)
+ #else
+ #if defined __GNUC__
+ #define API __attribute__((visibility("default")))
+ #else
+ #define API
+ #endif
+ #endif
+#else
+ #define API
+#endif
diff --git a/test cases/common/273 both libraries/src/library.c b/test cases/common/273 both libraries/src/library.c
new file mode 100644
index 000000000..decdb6ce9
--- /dev/null
+++ b/test cases/common/273 both libraries/src/library.c
@@ -0,0 +1,10 @@
+#include "library.h"
+
+int library_function(void)
+{
+#if defined EXPORT
+ return 1;
+#else
+ return 0;
+#endif
+}
diff --git a/test cases/common/273 both libraries/src/library.h b/test cases/common/273 both libraries/src/library.h
new file mode 100644
index 000000000..7f57af4f1
--- /dev/null
+++ b/test cases/common/273 both libraries/src/library.h
@@ -0,0 +1,5 @@
+#pragma once
+
+#include "api.h"
+
+int API library_function(void);
diff --git a/test cases/common/273 both libraries/src/main.c b/test cases/common/273 both libraries/src/main.c
new file mode 100644
index 000000000..340322e45
--- /dev/null
+++ b/test cases/common/273 both libraries/src/main.c
@@ -0,0 +1,8 @@
+#include "library.h"
+
+
+int main(void)
+{
+ int sum = library_function();
+ return sum == EXPECTED ? 0 : sum;
+}
diff --git a/test cases/common/273 both libraries/test.json b/test cases/common/273 both libraries/test.json
new file mode 100644
index 000000000..08aa54790
--- /dev/null
+++ b/test cases/common/273 both libraries/test.json
@@ -0,0 +1,16 @@
+{
+ "matrix": {
+ "options": {
+ "default_library": [
+ { "val": "shared" },
+ { "val": "static" },
+ { "val": "both" }
+ ],
+ "default_both_libraries": [
+ { "val": "shared" },
+ { "val": "static" },
+ { "val": "auto" }
+ ]
+ }
+ }
+}