Skip to content

Conversation

@lb90
Copy link
Contributor

@lb90 lb90 commented Jul 22, 2025

Fixes #14749

It's not necessary as we have "common/44 pkgconfig-gen"
already.
@lb90 lb90 requested review from jpakkane and xclaesse as code owners July 22, 2025 07:39
Port commit fde514c
to test cases/common/44 pkgconfig-gen.
@lb90 lb90 force-pushed the pkg-config-cflags-private branch from 986c21b to 8f62ce3 Compare July 22, 2025 07:55
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 mesonbuild#14749

[1] https://gitlab.freedesktop.org/pkg-config/pkg-config/-/issues/38
@xclaesse
Copy link
Member

How does it work to get those private cflags from pkgconf? Should we also make Meson use them somewhere? I would like to see a unit test for that. Sorry if it's there and I missed it.

@xclaesse
Copy link
Member

Note that usually projects do not build both shared and static libraries on windows, because it requires compiling everything twice and that capability has only been added recently in Meson. That means that in the case you build only the static library, it is fine to add e.g. MY_LIB_STATIC_COMPILE into Cflags field, or include that into some generated header file. Most projects do that already.

@lb90
Copy link
Contributor Author

lb90 commented Sep 12, 2025

Hi @xclaesse!

How does it work to get those private cflags from pkgconf? Should we also make Meson use them somewhere?

Well, Meson doesn't read .pc files by itself, it invokes a pkg-config binary. All it takes is using pkg-conf with Meson.

Note that usually projects do not build both shared and static libraries on windows

MSYS2 distributes both the shared library and the static library in a single package (and it usually builds the library twice for that). However the package ships one .pc file named after the library. So one can simply do:

dependency('foo') # follow default_library
dependency('foo', static: false) # shared library
dependency('foo', static: true) # static library

See for example the file list in https://packages.msys2.org/packages/mingw-w64-ucrt-x86_64-libepoxy. Fedora mingw packages do the same:

@xclaesse
Copy link
Member

xclaesse commented Sep 12, 2025

All it takes is using pkg-conf with Meson.

You still need to tell pkgconf if you want static or not. We usually pass --static arg for libs but I'm not sure we do for cflags. In either case, we should have a unit test for it

@xclaesse
Copy link
Member

Fedora mingw packages do the same

I'm pretty sure they do that by configuring meson twice and keeping only one of the .pc file generated. For glib I would be surprised if that even works because the macro is not in the .pc file but in generated header file.

@moi15moi
Copy link

moi15moi commented Nov 13, 2025

We usually pass --static arg for libs but I'm not sure we do for cflags.

You're right, meson doesn't pass --static for cflags like it does for libs.
See:

@lru_cache(maxsize=None)
def cflags(self, name: str, allow_system: bool = False,
define_variable: PkgConfigDefineType = None) -> ImmutableListProtocol[str]:
env = None
if allow_system:
env = os.environ.copy()
env['PKG_CONFIG_ALLOW_SYSTEM_CFLAGS'] = '1'
args: T.List[str] = []
args += self._define_variable_args(define_variable)
args += ['--cflags', name]
ret, out, err = self._call_pkgbin(args, env=env)
if ret != 0:
raise DependencyException(f'Could not generate cflags for {name}:\n{err}\n')
return self._split_args(out)
@lru_cache(maxsize=None)
def libs(self, name: str, static: bool = False, allow_system: bool = False,
define_variable: PkgConfigDefineType = None) -> ImmutableListProtocol[str]:
env = None
if allow_system:
env = os.environ.copy()
env['PKG_CONFIG_ALLOW_SYSTEM_LIBS'] = '1'
args: T.List[str] = []
args += self._define_variable_args(define_variable)
if static:
args.append('--static')
args += ['--libs', name]
ret, out, err = self._call_pkgbin(args, env=env)
if ret != 0:
raise DependencyException(f'Could not generate libs for {name}:\n{err}\n')
return self._split_args(out)

@dcbaker
Copy link
Member

dcbaker commented Nov 14, 2025

AFAICT there's no reason we don't other than no one's written the code. pkg-config and pkgconf both accept that (and those are the only two pkg-config implementations we really claim to support)

@bonzini bonzini modified the milestones: 1.10, 1.11 Nov 25, 2025
requires : 'glib-2.0', # Not really, but only here to test that this works.
requires_private : ['gio-2.0', 'gobject-2.0'],
libraries_private : [lib, '-lz'],
cflags_private: '-DSIMPLE_STATIC',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point of adding the flag? It's a shared library, so it doesnt have any purpose, no?

Copy link
Contributor Author

@lb90 lb90 Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's just a check that setting cflags_private doesn't crash Meson. Not much, I know 🙂 The test could be extended but we have to detect pkconf vs pkg-config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for Cflags.private to pkg-config module

5 participants