Skip to content

Commit 54a8b38

Browse files
committed
modules/pkg-config: Add support for Cflags.private
Cflags.private are compiler flags which are retrieved by pkg-config when static linking is requested. Currently, not all pkg-config implementations support Cflags.private; FreeDesktop.Org's pkg-config doesn't support it [1], but pkgconf does. Fixes #14749 [1] https://gitlab.freedesktop.org/pkg-config/pkg-config/-/issues/38
1 parent 2f1667e commit 54a8b38

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

docs/markdown/Pkgconfig-module.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ keyword arguments.
4646
- `requires` list of strings, pkgconfig-dependencies or libraries that
4747
`pkgconfig.generate()` was used on to put in the `Requires` field
4848
- `requires_private` the same as `requires` but for the `Requires.private` field
49+
- `cflags_private` (*Since 1.9.0*) compiler flags added when linking with a static
50+
library. Note: currently, the FreeDesktop.org pkg-config implementation does
51+
[not support Cflags.private](https://gitlab.freedesktop.org/pkg-config/pkg-config/-/issues/38)
4952
- `url` a string with a url for the library
5053
- `license` (*Since 1.9.0*) a string with a SPDX license to add to the generated file.
5154
- `variables` a list of strings with custom variables to add to the

mesonbuild/modules/pkgconfig.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class GenerateKw(TypedDict):
4949
install_dir: T.Optional[str]
5050
d_module_versions: T.List[T.Union[str, int]]
5151
extra_cflags: T.List[str]
52+
cflags_private: T.List[str]
5253
variables: T.Dict[str, str]
5354
uninstalled_variables: T.Dict[str, str]
5455
unescaped_variables: T.Dict[str, str]
@@ -96,6 +97,7 @@ def __init__(self, state: ModuleState, name: str, metadata: T.Dict[str, MetaData
9697
self.priv_libs: T.List[LIBS] = []
9798
self.priv_reqs: T.List[str] = []
9899
self.cflags: T.List[str] = []
100+
self.cflags_private: T.List[str] = []
99101
self.version_reqs: T.DefaultDict[str, T.Set[str]] = defaultdict(set)
100102
self.link_whole_targets: T.List[T.Union[build.CustomTarget, build.CustomTargetIndex, build.StaticLibrary]] = []
101103
self.uninstalled_incdirs: mesonlib.OrderedSet[str] = mesonlib.OrderedSet()
@@ -165,6 +167,9 @@ def _process_reqs(self, reqs: T.Sequence[T.Union[str, build.StaticLibrary, build
165167
def add_cflags(self, cflags: T.List[str]) -> None:
166168
self.cflags += mesonlib.stringlistify(cflags)
167169

170+
def add_cflags_private(self, cflags_private: T.List[str]) -> None:
171+
self.cflags_private += mesonlib.stringlistify(cflags_private)
172+
168173
def _add_uninstalled_incdirs(self, incdirs: T.List[build.IncludeDirs], subdir: T.Optional[str] = None) -> None:
169174
for i in incdirs:
170175
curdir = i.get_curdir()
@@ -372,6 +377,7 @@ def _fn(xs: T.Union[T.List[str], T.List[LIBS]], libs: bool = False) -> T.Union[T
372377
# Reset exclude list just in case some values can be both cflags and libs.
373378
exclude = set()
374379
self.cflags = _fn(self.cflags)
380+
self.cflags_private = _fn(self.cflags_private)
375381

376382
class PkgConfigModule(NewExtensionModule):
377383

@@ -596,11 +602,16 @@ def generate_libs_flags(libs: T.List[LIBS]) -> T.Iterable[str]:
596602
if cflags and not dataonly:
597603
ofile.write('Cflags: {}\n'.format(' '.join(cflags)))
598604

605+
cflags_private: T.List[str] = [self._escape(f) for f in deps.cflags_private]
606+
if cflags_private and not dataonly:
607+
ofile.write('Cflags.private: {}\n'.format(' '.join(cflags_private)))
608+
599609
@typed_pos_args('pkgconfig.generate', optargs=[(build.SharedLibrary, build.StaticLibrary)])
600610
@typed_kwargs(
601611
'pkgconfig.generate',
602612
D_MODULE_VERSIONS_KW.evolve(since='0.43.0'),
603613
INSTALL_DIR_KW,
614+
KwargInfo('cflags_private', ContainerTypeInfo(list, str), default=[], listify=True, since='1.9.0'),
604615
KwargInfo('conflicts', ContainerTypeInfo(list, str), default=[], listify=True),
605616
KwargInfo('dataonly', bool, default=False, since='0.54.0'),
606617
KwargInfo('description', (str, NoneType)),
@@ -651,7 +662,7 @@ def generate(self, state: ModuleState,
651662
dataonly = kwargs['dataonly']
652663
if dataonly:
653664
default_subdirs = []
654-
blocked_vars = ['libraries', 'libraries_private', 'requires_private', 'extra_cflags', 'subdirs']
665+
blocked_vars = ['libraries', 'libraries_private', 'requires_private', 'extra_cflags', 'cflags_private', 'subdirs']
655666
# Mypy can't figure out that this TypedDict index is correct, without repeating T.Literal for the entire list
656667
if any(kwargs[k] for k in blocked_vars): # type: ignore
657668
raise mesonlib.MesonException(f'Cannot combine dataonly with any of {blocked_vars}')
@@ -680,6 +691,7 @@ def generate(self, state: ModuleState,
680691
deps.add_pub_reqs(kwargs['requires'])
681692
deps.add_priv_reqs(kwargs['requires_private'])
682693
deps.add_cflags(kwargs['extra_cflags'])
694+
deps.add_cflags_private(kwargs['cflags_private'])
683695

684696
dversions = kwargs['d_module_versions']
685697
if dversions:

test cases/common/44 pkgconfig-gen/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pkgg.generate(
3636
requires : 'glib-2.0', # Not really, but only here to test that this works.
3737
requires_private : ['gio-2.0', 'gobject-2.0'],
3838
libraries_private : [lib, '-lz'],
39+
cflags_private: '-DSIMPLE_STATIC',
3940
license: 'Apache-2.0',
4041
)
4142

0 commit comments

Comments
 (0)