From b76cf458cea23d8359237248bf97fde693ce9fe2 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 21 Apr 2022 19:43:04 +0100 Subject: meson: Automatically link libglnx-dependent objects to gio-unix If we don't do this, users of libglnx all have to add this dependency themselves, otherwise they'll get errors like: fatal error: gio/gfiledescriptorbased.h: No such file or directory Signed-off-by: Simon McVittie --- meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/meson.build b/meson.build index 6714dc2..1fc438e 100644 --- a/meson.build +++ b/meson.build @@ -86,6 +86,7 @@ libglnx = static_library('glnx', include_directories : libglnx_inc, install : false) libglnx_dep = declare_dependency( + dependencies : libglnx_deps, include_directories : libglnx_inc, link_with : libglnx) -- cgit v1.2.1 From eeea247ade9efc797d7aad88baa9e93742d9a9bf Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 21 Apr 2022 18:36:14 +0100 Subject: meson: Always build with hidden symbol visibility This copylib isn't intended to be part of anyone's ABI. In Autotools, this is typically implemented by doing some ad-hoc compiler checks and adding -fvisibility=hidden to the global CFLAGS of any library that pulls in libglnx, but in Meson it's a build system feature. Signed-off-by: Simon McVittie --- meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/meson.build b/meson.build index 1fc438e..f3dc8a4 100644 --- a/meson.build +++ b/meson.build @@ -83,6 +83,7 @@ libglnx_sources = [ libglnx = static_library('glnx', libglnx_sources, dependencies : libglnx_deps, + gnu_symbol_visibility : 'hidden', include_directories : libglnx_inc, install : false) libglnx_dep = declare_dependency( -- cgit v1.2.1 From c500c362b85ad8119a6f07d91e47166c997753c9 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 21 Apr 2022 18:33:18 +0100 Subject: tests: Build libglnx-testlib as a separate static library One of Flatpak's unit tests uses this, for _GLNX_TEST_SCOPED_TEMP_DIR. Its API is not really stable, but libglnx is a "copylib" anyway, so none of it has (or needs) a strictly stable API. Signed-off-by: Simon McVittie --- tests/meson.build | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/meson.build b/tests/meson.build index 2ad8c44..555009f 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -2,6 +2,25 @@ # Copyright 2019 Collabora Ltd. # SPDX-License-Identifier: LGPL-2.1-or-later +libglnx_testlib = static_library( + 'glnx-testlib', + 'libglnx-testlib.c', + 'libglnx-testlib.h', + dependencies : [ + libglnx_dep, + libglnx_deps, + ], + install : false, +) +libglnx_testlib_dep = declare_dependency( + dependencies : [ + libglnx_dep, + libglnx_deps, + ], + include_directories : include_directories('.'), + link_with : libglnx_testlib, +) + test_names = [ 'errors', 'fdio', @@ -13,13 +32,12 @@ test_names = [ foreach test_name : test_names exe = executable(test_name, [ - 'libglnx-testlib.c', - 'libglnx-testlib.h', 'test-libglnx-' + test_name + '.c', ], dependencies: [ libglnx_dep, libglnx_deps, + libglnx_testlib_dep, ], ) test(test_name, exe) -- cgit v1.2.1 From 96ba9f4d8d747889a10e1a84594922e25fde495c Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 21 Apr 2022 18:51:09 +0100 Subject: tests: Optionally skip building and running the actual tests If we're building libglnx as a subproject in a larger project, we won't necessarily want to run these. Signed-off-by: Simon McVittie --- meson_options.txt | 9 +++++++++ tests/meson.build | 42 ++++++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 meson_options.txt diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 0000000..1028017 --- /dev/null +++ b/meson_options.txt @@ -0,0 +1,9 @@ +# Copyright 2022 Collabora Ltd. +# SPDX-License-Identifier: LGPL-2.0-or-later + +option( + 'tests', + type : 'boolean', + description : 'build and run unit tests', + value : 'true', +) diff --git a/tests/meson.build b/tests/meson.build index 555009f..2c38ab0 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -21,24 +21,26 @@ libglnx_testlib_dep = declare_dependency( link_with : libglnx_testlib, ) -test_names = [ - 'errors', - 'fdio', - 'macros', - 'shutil', - 'xattrs', -] +if get_option('tests') + test_names = [ + 'errors', + 'fdio', + 'macros', + 'shutil', + 'xattrs', + ] -foreach test_name : test_names - exe = executable(test_name, - [ - 'test-libglnx-' + test_name + '.c', - ], - dependencies: [ - libglnx_dep, - libglnx_deps, - libglnx_testlib_dep, - ], - ) - test(test_name, exe) -endforeach + foreach test_name : test_names + exe = executable(test_name, + [ + 'test-libglnx-' + test_name + '.c', + ], + dependencies: [ + libglnx_dep, + libglnx_deps, + libglnx_testlib_dep, + ], + ) + test(test_name, exe) + endforeach +endif -- cgit v1.2.1 From 4d5b1fcf0257f508f4b5e48b099f259b7bb9c908 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 21 Apr 2022 18:33:34 +0100 Subject: tests: Verify libglnx can work as a subproject What isn't tested usually doesn't work, and the only way to use libglnx is as a subproject, so test it like that. Signed-off-by: Simon McVittie --- .gitlab-ci.yml | 17 ++++++++++++----- tests/use-as-subproject/.gitignore | 5 +++++ tests/use-as-subproject/README | 8 ++++++++ tests/use-as-subproject/config.h | 6 ++++++ tests/use-as-subproject/dummy-config.h.in | 6 ++++++ tests/use-as-subproject/meson.build | 25 +++++++++++++++++++++++++ tests/use-as-subproject/use-libglnx.c | 16 ++++++++++++++++ tests/use-as-subproject/use-testlib.c | 17 +++++++++++++++++ 8 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 tests/use-as-subproject/.gitignore create mode 100644 tests/use-as-subproject/README create mode 100644 tests/use-as-subproject/config.h create mode 100644 tests/use-as-subproject/dummy-config.h.in create mode 100644 tests/use-as-subproject/meson.build create mode 100644 tests/use-as-subproject/use-libglnx.c create mode 100644 tests/use-as-subproject/use-testlib.c diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 29de882..d9e465a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,17 +7,24 @@ stages: - build before_script: - - dnf install -y gcc meson ninja-build "pkgconfig(gio-2.0)" "pkgconfig(gio-unix-2.0)" "pkgconfig(glib-2.0)" + - dnf install -y gcc git meson ninja-build "pkgconfig(gio-2.0)" "pkgconfig(gio-unix-2.0)" "pkgconfig(glib-2.0)" xz build: stage: build script: - meson _build . - - cd _build - - ninja - - meson test + - ninja -C _build + - meson test -C _build # Run it again! This previously did not work. - - meson test + - meson test -C _build + # Ensure that we can build as a subproject + - rm -fr _build/meson-dist + - meson dist -C _build + - mkdir -p tests/use-as-subproject/subprojects/libglnx + - tar --strip-components=1 -C tests/use-as-subproject/subprojects/libglnx -xf _build/meson-dist/*.tar.xz + - meson tests/use-as-subproject/_build tests/use-as-subproject + - ninja -C tests/use-as-subproject/_build + - meson test -C tests/use-as-subproject/_build artifacts: when: on_failure name: "libglnx-${CI_COMMIT_REF_NAME}-${CI_JOB_NAME}" diff --git a/tests/use-as-subproject/.gitignore b/tests/use-as-subproject/.gitignore new file mode 100644 index 0000000..ec6149f --- /dev/null +++ b/tests/use-as-subproject/.gitignore @@ -0,0 +1,5 @@ +# Copyright 2022 Collabora Ltd. +# SPDX-License-Identifier: LGPL-2.0-or-later + +/_build/ +/subprojects/ diff --git a/tests/use-as-subproject/README b/tests/use-as-subproject/README new file mode 100644 index 0000000..cc43a09 --- /dev/null +++ b/tests/use-as-subproject/README @@ -0,0 +1,8 @@ +This is a simple example of a project that uses libglnx as a subproject. +The intention is that if this project can successfully build and use libglnx +as a subproject, then so could Flatpak. + + diff --git a/tests/use-as-subproject/config.h b/tests/use-as-subproject/config.h new file mode 100644 index 0000000..dffc647 --- /dev/null +++ b/tests/use-as-subproject/config.h @@ -0,0 +1,6 @@ +/* + * Copyright 2022 Collabora Ltd. + * SPDX-License-Identifier: LGPL-2.0-or-later + */ + +#error Should not use superproject config.h to compile libglnx diff --git a/tests/use-as-subproject/dummy-config.h.in b/tests/use-as-subproject/dummy-config.h.in new file mode 100644 index 0000000..bffb52a --- /dev/null +++ b/tests/use-as-subproject/dummy-config.h.in @@ -0,0 +1,6 @@ +/* + * Copyright 2022 Collabora Ltd. + * SPDX-License-Identifier: LGPL-2.0-or-later + */ + +#error Should not use superproject generated config.h to compile libglnx diff --git a/tests/use-as-subproject/meson.build b/tests/use-as-subproject/meson.build new file mode 100644 index 0000000..65ba37b --- /dev/null +++ b/tests/use-as-subproject/meson.build @@ -0,0 +1,25 @@ +# Copyright 2022 Collabora Ltd. +# SPDX-License-Identifier: LGPL-2.0-or-later + +project( + 'use-libglnx-as-subproject', + 'c', + version : '0', + meson_version : '>=0.49.0', +) +add_project_arguments('-D_GNU_SOURCE', language : 'c') + +configure_file( + copy : true, + input : 'dummy-config.h.in', + output : 'config.h', +) + +glib_dep = dependency('glib-2.0') + +libglnx = subproject('libglnx') +libglnx_dep = libglnx.get_variable('libglnx_dep') +libglnx_testlib_dep = libglnx.get_variable('libglnx_testlib_dep') + +executable('use-libglnx', 'use-libglnx.c', dependencies : [libglnx_dep, glib_dep]) +executable('use-testlib', 'use-testlib.c', dependencies : [libglnx_testlib_dep, glib_dep]) diff --git a/tests/use-as-subproject/use-libglnx.c b/tests/use-as-subproject/use-libglnx.c new file mode 100644 index 0000000..0e14db0 --- /dev/null +++ b/tests/use-as-subproject/use-libglnx.c @@ -0,0 +1,16 @@ +/* + * Copyright 2022 Collabora Ltd. + * SPDX-License-Identifier: LGPL-2.0-or-later + */ + +#include + +int +main (void) +{ + GError *error = NULL; + + glnx_throw (&error, "whatever"); + g_clear_error (&error); + return 0; +} diff --git a/tests/use-as-subproject/use-testlib.c b/tests/use-as-subproject/use-testlib.c new file mode 100644 index 0000000..9a955b4 --- /dev/null +++ b/tests/use-as-subproject/use-testlib.c @@ -0,0 +1,17 @@ +/* + * Copyright 2022 Collabora Ltd. + * SPDX-License-Identifier: LGPL-2.0-or-later + */ + +#include +#include + +int +main (void) +{ + _GLNX_TEST_DECLARE_ERROR (local_error, error); + + glnx_throw (error, "Whatever"); + g_clear_error (&local_error); + return 0; +} -- cgit v1.2.1 From 238f7bfc494e16a0864f88ae891da6e7b3d82e5b Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 21 Apr 2022 19:56:41 +0100 Subject: fdio: Skip glnx_try_fallocate() if _GNU_SOURCE is not defined MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fallocate() is only visible in fcntl.h if _GNU_SOURCE is defined. Most users of libglnx will want to do that anyway, but it seems nicer to avoid "implicit declaration of function ‘fallocate’" warnings from simply including into naive code. Signed-off-by: Simon McVittie --- glnx-fdio.h | 2 ++ tests/use-as-subproject/meson.build | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/glnx-fdio.h b/glnx-fdio.h index b6ba0ea..af53479 100644 --- a/glnx-fdio.h +++ b/glnx-fdio.h @@ -209,6 +209,7 @@ int glnx_renameat2_noreplace (int olddirfd, const char *oldpath, int glnx_renameat2_exchange (int olddirfd, const char *oldpath, int newdirfd, const char *newpath); +#ifdef _GNU_SOURCE /** * glnx_try_fallocate: * @fd: File descriptor @@ -240,6 +241,7 @@ glnx_try_fallocate (int fd, return TRUE; } +#endif /** * glnx_fstat: diff --git a/tests/use-as-subproject/meson.build b/tests/use-as-subproject/meson.build index 65ba37b..2d08160 100644 --- a/tests/use-as-subproject/meson.build +++ b/tests/use-as-subproject/meson.build @@ -7,7 +7,6 @@ project( version : '0', meson_version : '>=0.49.0', ) -add_project_arguments('-D_GNU_SOURCE', language : 'c') configure_file( copy : true, -- cgit v1.2.1