From 50312edfc8b382106eae3762ba87616319cdb550 Mon Sep 17 00:00:00 2001 From: Enno Boland Date: Tue, 4 Jun 2024 17:05:06 +0200 Subject: [PATCH] utils: add functions to get the current version of the library --- include/meson.build | 1 + include/sqsh.h | 6 ++ include/sqsh_utils.h | 110 +++++++++++++++++++++++++++++++++++ libsqsh/src/meson.build | 1 + libsqsh/src/utils/version.c | 54 +++++++++++++++++ test/libsqsh/check_version.c | 55 ++++++++++++++++++ test/libsqsh/meson.build | 3 +- 7 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 include/sqsh_utils.h create mode 100644 libsqsh/src/utils/version.c create mode 100644 test/libsqsh/check_version.c diff --git a/include/meson.build b/include/meson.build index 28b57fcb..ef04aecc 100644 --- a/include/meson.build +++ b/include/meson.build @@ -11,6 +11,7 @@ headers = files( 'sqsh_posix.h', 'sqsh_table.h', 'sqsh_tree.h', + 'sqsh_utils.h', 'sqsh_xattr.h', ) diff --git a/include/sqsh.h b/include/sqsh.h index 82ffdb38..3e1bdfce 100644 --- a/include/sqsh.h +++ b/include/sqsh.h @@ -31,6 +31,9 @@ * @file sqsh.h */ +#ifndef SQSH_H +#define SQSH_H + #include "sqsh_archive.h" #include "sqsh_common.h" #include "sqsh_directory.h" @@ -41,4 +44,7 @@ #include "sqsh_posix.h" #include "sqsh_table.h" #include "sqsh_tree.h" +#include "sqsh_utils.h" #include "sqsh_xattr.h" + +#endif /* SQSH_H */ diff --git a/include/sqsh_utils.h b/include/sqsh_utils.h new file mode 100644 index 00000000..465e8a7b --- /dev/null +++ b/include/sqsh_utils.h @@ -0,0 +1,110 @@ +/****************************************************************************** + * * + * Copyright (c) 2023-2024, Enno Boland * + * * + * Redistribution and use in source and binary forms, with or without * + * modification, are permitted provided that the following conditions are * + * met: * + * * + * * Redistributions of source code must retain the above copyright notice, * + * this list of conditions and the following disclaimer. * + * * Redistributions in binary form must reproduce the above copyright * + * notice, this list of conditions and the following disclaimer in the * + * documentation and/or other materials provided with the distribution. * + * * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * + * * + ******************************************************************************/ + +/** + * @author Enno Boland (mail@eboland.de) + * @file sqsh_utils.h + */ + +#ifndef SQSH_UTILS_H +#define SQSH_UTILS_H + +#include "sqsh_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/*************************************** + * utils/version.c + */ + +/** + * @brief Get the version of the library that was used to compile the program as + * a string. + * + * @return Version string + */ +#define SQSH_VERSION "1.4.0" + +/** + * @brief Get the major version of the library that was used to compile the + * program. + * + * @return Major version + */ +#define SQSH_VERSION_MAJOR 1 + +/** + * @brief Get the minor version of the library that was used to compile the + * program. + * + * @return Minor version + */ +#define SQSH_VERSION_MINOR 4 + +/** + * @brief Get the patch version of the library that was used to compile the + * program. + * + * @return Patch version + */ +#define SQSH_VERSION_PATCH 0 + +/** + * @brief Get the version of the currently running library as a string. + * + * @return Version string + */ +const char *sqsh_version(void); + +/** + * @brief Get the major version of the currently running library. + * + * @return Major version + */ +uint16_t sqsh_version_major(void); + +/** + * @brief Get the minor version of the currently running library. + * + * @return Minor version + */ +uint16_t sqsh_version_minor(void); + +/** + * @brief Get the patch version of the currently running library. + * + * @return Patch version + */ +uint16_t sqsh_version_patch(void); + +#ifdef __cplusplus +} +#endif +#endif /* SQSH_UTILS_H */ diff --git a/libsqsh/src/meson.build b/libsqsh/src/meson.build index f01a6c1d..5f45f433 100644 --- a/libsqsh/src/meson.build +++ b/libsqsh/src/meson.build @@ -45,6 +45,7 @@ libsqsh_sources = files( 'tree/traversal.c', 'tree/walker.c', 'utils/error.c', + 'utils/version.c', 'xattr/xattr_iterator.c', ) diff --git a/libsqsh/src/utils/version.c b/libsqsh/src/utils/version.c new file mode 100644 index 00000000..a9391089 --- /dev/null +++ b/libsqsh/src/utils/version.c @@ -0,0 +1,54 @@ +/****************************************************************************** + * * + * Copyright (c) 2023-2024, Enno Boland * + * * + * Redistribution and use in source and binary forms, with or without * + * modification, are permitted provided that the following conditions are * + * met: * + * * + * * Redistributions of source code must retain the above copyright notice, * + * this list of conditions and the following disclaimer. * + * * Redistributions in binary form must reproduce the above copyright * + * notice, this list of conditions and the following disclaimer in the * + * documentation and/or other materials provided with the distribution. * + * * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * + * * + ******************************************************************************/ + +/** + * @author Enno Boland (mail@eboland.de) + * @file version.c + */ + +#include + +const char * +sqsh_version(void) { + return SQSH_VERSION; +} + +uint16_t +sqsh_version_major(void) { + return SQSH_VERSION_MAJOR; +} + +uint16_t +sqsh_version_minor(void) { + return SQSH_VERSION_MINOR; +} + +uint16_t +sqsh_version_patch(void) { + return SQSH_VERSION_PATCH; +} diff --git a/test/libsqsh/check_version.c b/test/libsqsh/check_version.c new file mode 100644 index 00000000..2770a784 --- /dev/null +++ b/test/libsqsh/check_version.c @@ -0,0 +1,55 @@ +/****************************************************************************** + * * + * Copyright (c) 2023-2024, Enno Boland * + * * + * Redistribution and use in source and binary forms, with or without * + * modification, are permitted provided that the following conditions are * + * met: * + * * + * * Redistributions of source code must retain the above copyright notice, * + * this list of conditions and the following disclaimer. * + * * Redistributions in binary form must reproduce the above copyright * + * notice, this list of conditions and the following disclaimer in the * + * documentation and/or other materials provided with the distribution. * + * * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * + * * + ******************************************************************************/ + +/** + * @author Enno Boland (mail@eboland.de) + * @file nasty.c + */ + +#include +#include + +UTEST(version, version_defines_are_correct) { + char *version = getenv("VERSION"); + + ASSERT_NE(NULL, version); + ASSERT_STREQ(SQSH_VERSION, version); + ASSERT_STREQ(SQSH_VERSION, sqsh_version()); + + int major, minor, patch; + ASSERT_EQ(3, sscanf(version, "%d.%d.%d", &major, &minor, &patch)); + + ASSERT_EQ(SQSH_VERSION_MAJOR, major); + ASSERT_EQ(SQSH_VERSION_MAJOR, sqsh_version_major()); + ASSERT_EQ(SQSH_VERSION_MINOR, minor); + ASSERT_EQ(SQSH_VERSION_MINOR, sqsh_version_minor()); + ASSERT_EQ(SQSH_VERSION_PATCH, patch); + ASSERT_EQ(SQSH_VERSION_PATCH, sqsh_version_patch()); +} + +UTEST_MAIN() diff --git a/test/libsqsh/meson.build b/test/libsqsh/meson.build index d87923a7..ea9a359c 100644 --- a/test/libsqsh/meson.build +++ b/test/libsqsh/meson.build @@ -1,6 +1,7 @@ add_languages('cpp', native: false) sqsh_test = [ + 'check_version.c', 'cpp-test.cpp', 'archive/compression_options.c', 'archive/inode_map.c', @@ -101,5 +102,5 @@ foreach p : sqsh_test link_with: [libsqsh.get_static_lib(), libmksqsh.get_static_lib()], dependencies: [threads_dep, utest_dep, cextras_dep], ) - test(p, t, env: {}) + test(p, t, env: {'VERSION': meson.project_version()}) endforeach