build: honor default_library for the installed libraries
Subsystems (3)
PATCH v3 1/1 build: honor default_library for the installed libraries
From: Yegor Yefremov <[email protected]>
The installed libraries (libgpiod, libgpiodcxx, libgpiod-glib, libgpiodbus and libgpiosim) are built with both_libraries(), so the shared variant is always built, even with -Ddefault_library=static. When the toolchain or the environment requests a fully static link (e.g. LDFLAGS=-static, as done by Buildroot for static-only builds), linking the shared library then fails:
FAILED: lib/libgpiod.so.3.1.2
[...] crtbeginT.o: relocation R_ARC_32_ME against `__TMC_END__' can not be used when making a shared object; recompile with -fPIC
Use library() instead, which follows default_library, and set default_library=both in the project's default options, so that a default build still installs both the shared and static libraries. As library() returns an object behaving like the shared library when both are built, the users of these objects (link_with, pkgconfig.generate, generate_gir, full_path) keep working as before.
The Rust bindings always link against the static libgpiosim. With default_library=shared, build a static archive from the objects of the shared libgpiosim for them, which is neither installed nor built by default.
With default_library=static, generate_gir() would get the static libgpiod-glib, but meson's gnome module does not pass internal static libraries to g-ir-scanner, so linking libgpiod-glib.a then fails with undefined references to libgpiod. A typelib for a static-only library would also be useless, as it has no shared library to load at runtime. Disable introspection when only static libraries are built, and error out if it was explicitly enabled.
With a default build, the only change in the installed files is that libgpiotools, already declared with library(), is now also installed as a static library, like the other libraries.
---
Changes v2 -> v3:
- also convert libgpiosim, which is installed too and still built the shared library with default_library=static; keep providing a static libgpiosim for the Rust bindings with default_library=shared
Changes v1 -> v2:
- disable introspection with default_library=static, as meson does not pass internal static libraries to g-ir-scanner (Bartosz)
bindings/cxx/meson.build | 3 +-- bindings/glib/meson.build | 3 +-- dbus/lib/meson.build | 3 +-- lib/meson.build | 3 +-- meson.build | 6 ++++++ tests/gpiosim/meson.build | 17 ++++++++++++++--- 6 files changed, 24 insertions(+), 11 deletions(-)
bindings/cxx/meson.build +1 -2
@@ -3,7 +3,7 @@ cxx_inc = include_directories('.', '../../include') -libgpiodcxx_both = both_libraries('gpiodcxx', +libgpiodcxx_lib = library('gpiodcxx', sources: [ 'chip.cpp', 'chip-info.cpp',
@@ -29,7 +29,6 @@ libgpiodcxx_both = both_libraries('gpiodcxx', soversion: libgpiodcxx_soversion, install: true, ) -libgpiodcxx_lib = libgpiodcxx_both.get_shared_lib() install_headers('gpiod.hpp')
bindings/glib/meson.build +1 -2
@@ -61,7 +61,7 @@ glib_public_headers = files( glib_inc = include_directories('.', '../../include') -libgpiod_glib_both = both_libraries('gpiod-glib', +libgpiod_glib_lib = library('gpiod-glib', sources: glib_sources + [gen_enum_c, gen_enum_h], include_directories: glib_inc, c_args: ['-DG_LOG_DOMAIN="gpiod-glib"', '-DGPIODGLIB_COMPILATION'] + profiling_c_args,
@@ -71,7 +71,6 @@ libgpiod_glib_both = both_libraries('gpiod-glib', soversion: libgpiod_glib_soversion, install: true, ) -libgpiod_glib_lib = libgpiod_glib_both.get_shared_lib() install_headers('gpiod-glib.h')
dbus/lib/meson.build +1 -2
@@ -14,7 +14,7 @@ generated_dbus = gnome.gdbus_codegen('generated-gpiodbus', dbus_lib_inc = include_directories('.', '../../include') -libgpiodbus_both = both_libraries('gpiodbus', +libgpiodbus_lib = library('gpiodbus', sources: ['gpiodbus.h', generated_dbus], include_directories: dbus_lib_inc, c_args: ['-DG_LOG_DOMAIN="gpiodbus"'],
@@ -22,7 +22,6 @@ libgpiodbus_both = both_libraries('gpiodbus', soversion: 1, install: true, ) -libgpiodbus_lib = libgpiodbus_both.get_shared_lib() install_data('io.gpiod1.xml', install_dir: get_option('datadir') / 'dbus-1' / 'interfaces',
lib/meson.build +1 -2
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-or-later # SPDX-FileCopyrightText: 2026 Qualcomm Technologies, Inc. and/or its subsidiaries -libgpiod_both = both_libraries('gpiod', +libgpiod_lib = library('gpiod', sources: [ 'chip.c', 'chip-info.c',
@@ -22,7 +22,6 @@ libgpiod_both = both_libraries('gpiod', soversion: libgpiod_soversion, install: true, ) -libgpiod_lib = libgpiod_both.get_shared_lib() pkgconfig = import('pkgconfig') pkgconfig.generate(libgpiod_lib,
meson.build +6 -0
@@ -6,6 +6,7 @@ project('libgpiod', 'c', license: 'LGPL-2.1-or-later', default_options: [ 'c_std=gnu89', + 'default_library=both', 'cpp_std=gnu++17', 'warning_level=2', ],
@@ -79,6 +80,11 @@ if opt_introspection.enabled() 'introspection requires -Dbindings-glib=enabled') endif +# Introspection requires the shared library. +opt_introspection = opt_introspection.require( + get_option('default_library') != 'static', + error_message: 'introspection requires -Ddefault_library=shared or both') + # systemd requires D-Bus. if opt_systemd.enabled() assert(opt_dbus.allowed(),
tests/gpiosim/meson.build +14 -3
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-or-later # SPDX-FileCopyrightText: 2026 Qualcomm Technologies, Inc. and/or its subsidiaries -libgpiosim_both = both_libraries('gpiosim', +libgpiosim_lib = library('gpiosim', sources: ['gpiosim.c'], c_args: ['-fvisibility=hidden'], dependencies: [libkmod_dep, mount_dep, threads_dep],
@@ -9,8 +9,19 @@ libgpiosim_both = both_libraries('gpiosim', soversion: libgpiosim_soversion, install: true, ) -libgpiosim_lib = libgpiosim_both.get_shared_lib() -libgpiosim_static = libgpiosim_both.get_static_lib() + +# The Rust bindings always link against the static libgpiosim. +if get_option('default_library') == 'both' + libgpiosim_static = libgpiosim_lib.get_static_lib() +elif get_option('default_library') == 'static' + libgpiosim_static = libgpiosim_lib +else + libgpiosim_static = static_library('gpiosim', + objects: libgpiosim_lib.extract_all_objects(recursive: false), + build_by_default: false, + ) +endif + libgpiosim_inc = include_directories('.') executable('gpiosim-selftest',
Signature · 1 line
2.55.0
Bartosz Golaszewski
AppliedQuoted 8 lines · Yegor Yefremov
The installed libraries (libgpiod, libgpiodcxx, libgpiod-glib, libgpiodbus and libgpiosim) are built with
both_libraries(), so the shared variant is always built, even with -Ddefault_library=static. When the toolchain or the environment requests a fully static link (e.g. LDFLAGS=-static, as done by Buildroot for static-only builds), linking the shared library then fails:[...]
Applied, thanks!
[1/1] build: honor default_library for the installed libraries
https://git.kernel.org/brgl/c/114d75e95c92fe5aa5721a0c6da2c56f2414ebb4
Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>