From 2082b3f8fb347e5519d306f2ac2b28f30bae0fdd Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 21 Jul 2017 13:32:58 -0400 Subject: [PATCH] lib: Add version macros and version checking function The version checking function in particular is really useful for people doing `from gi.repository import RpmOstree`, which we'd like at least some things like Anaconda and Pungi to do. Closes: #891 Approved by: jlebon --- Makefile-lib-defines.am | 1 + configure.ac | 10 +++- src/lib/rpmostree-version.h.in | 91 ++++++++++++++++++++++++++++++++++ src/lib/rpmostree.c | 17 +++++++ src/lib/rpmostree.h | 4 ++ tests/check/test-basic.sh | 21 ++++++-- 6 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 src/lib/rpmostree-version.h.in diff --git a/Makefile-lib-defines.am b/Makefile-lib-defines.am index 3eef48cc..76f6c2bb 100644 --- a/Makefile-lib-defines.am +++ b/Makefile-lib-defines.am @@ -20,6 +20,7 @@ librpmostree_public_headers = \ src/lib/rpmostree.h \ + src/lib/rpmostree-version.h \ src/lib/rpmostree-db.h \ src/lib/rpmostree-package.h \ $(NULL) diff --git a/configure.ac b/configure.ac index 2b2939ed..9503cc63 100644 --- a/configure.ac +++ b/configure.ac @@ -3,10 +3,17 @@ dnl To do a release, increment this number, commit, then submit it as a PR dnl to merge via homu. After that, do `git pull origin && git reset --hard origin/master`. dnl Then use https://github.com/cgwalters/git-evtag to sign. dnl Then, git push origin v201X.XX -AC_INIT([rpm-ostree], [2017.7], [walters@verbum.org]) +m4_define([year_version], [2017]) +m4_define([release_version], [7]) +m4_define([package_version], [year_version.release_version]) +AC_INIT([rpm-ostree], [package_version], [atomic-devel@projectatomic.io]) AC_CONFIG_HEADER([config.h]) AC_CONFIG_MACRO_DIR([buildutil]) AC_CONFIG_AUX_DIR([build-aux]) +dnl Versioning information +AC_SUBST([YEAR_VERSION], [year_version]) +AC_SUBST([RELEASE_VERSION], [release_version]) +AC_SUBST([PACKAGE_VERSION], [package_version]) AM_INIT_AUTOMAKE([1.11 -Wno-portability foreign no-define tar-ustar no-dist-gzip dist-xz subdir-objects]) AM_MAINTAINER_MODE([enable]) @@ -195,6 +202,7 @@ AC_CONFIG_FILES([ Makefile api-doc/Makefile src/lib/rpm-ostree-1.pc +src/lib/rpmostree-version.h ]) AC_OUTPUT diff --git a/src/lib/rpmostree-version.h.in b/src/lib/rpmostree-version.h.in new file mode 100644 index 00000000..74ea05bf --- /dev/null +++ b/src/lib/rpmostree-version.h.in @@ -0,0 +1,91 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2017 Georges Basile Stavracas Neto + * Copyright (C) 2017 Red Hat, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; either version 2 of the licence or (at + * your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#pragma once + +/** + * SECTION:rpm-ostree-version + * @short_description: Version checking + * + * Macros to check the version of the library at compile-time. + */ + +/** + * RPM_OSTREE_YEAR_VERSION: + * + * Year version component (e.g. 2017 if %RPM_OSTREE_VERSION is 2017.9) + * + * Since: 2017.8 + */ +#define RPM_OSTREE_YEAR_VERSION (@YEAR_VERSION@) + +/** + * RPM_OSTREE_RELEASE_VERSION: + * + * Release version component (e.g. 9 if %RPM_OSTREE_VERSION is 2017.9) + * + * Since: 2017.8 + */ +#define RPM_OSTREE_RELEASE_VERSION (@RELEASE_VERSION@) + +/** + * RPM_OSTREE_VERSION + * + * Since: 2017.8 + */ +#define RPM_OSTREE_VERSION (@VERSION@) + +/** + * RPM_OSTREE_VERSION_S: + * + * Version encoded as a string, useful for printing and concatenation. + * + * Since: 2017.8 + */ +#define RPM_OSTREE_VERSION_S "@VERSION@" + +#define RPM_OSTREE_ENCODE_VERSION(year,release) \ + ((year) << 16 | (release)) + +/** + * RPM_OSTREE_VERSION_HEX: + * + * ostree version, encoded as an hexadecimal number, useful for + * integer comparisons. + * + * Since: 2017.8 + */ +#define RPM_OSTREE_VERSION_HEX \ + (RPM_OSTREE_ENCODE_VERSION (RPM_OSTREE_YEAR_VERSION, RPM_OSTREE_RELEASE_VERSION)) + +/** + * RPM_OSTREE_CHECK_VERSION: + * @year: required year version + * @release: required release version + * + * Compile-time version checking. Evaluates to %TRUE if the version + * of ostree is equal or greater than the required one. + * + * Since: 2017.8 + */ +#define RPM_OSTREE_CHECK_VERSION(year,release) \ + (RPM_OSTREE_YEAR_VERSION > (year) || \ + (RPM_OSTREE_YEAR_VERSION == (year) && RPM_OSTREE_RELEASE_VERSION >= (release))) diff --git a/src/lib/rpmostree.c b/src/lib/rpmostree.c index 6e7d1e98..5f9aad2e 100644 --- a/src/lib/rpmostree.c +++ b/src/lib/rpmostree.c @@ -62,3 +62,20 @@ rpm_ostree_varsubst_basearch (const char *src, GError **error) g_autoptr(GHashTable) varsubsts = rpmostree_dnfcontext_get_varsubsts (ctx); return _rpmostree_varsubst_string (src, varsubsts, error); } + +/** + * rpm_ostree_check_version: + * @required_year: Major/year required + * @required_release: Release version required + * + * The `RPM_OSTREE_CHECK_VERSION` macro operates at compile time, whereas + * this function operates at runtime. The distinction is most useful for + * things that are dynamic, such as scripting language callers. + * + * Returns: %TRUE if current library has at least the requested version, %FALSE otherwise + */ +gboolean +rpm_ostree_check_version (guint required_year, guint required_release) +{ + return RPM_OSTREE_CHECK_VERSION(required_year, required_release); +} diff --git a/src/lib/rpmostree.h b/src/lib/rpmostree.h index fb533d4a..e63a66c7 100644 --- a/src/lib/rpmostree.h +++ b/src/lib/rpmostree.h @@ -26,6 +26,7 @@ #include #include +#include G_BEGIN_DECLS @@ -35,4 +36,7 @@ char *rpm_ostree_get_basearch (void); _RPMOSTREE_EXTERN char *rpm_ostree_varsubst_basearch (const char *src, GError **error); +_RPMOSTREE_EXTERN +gboolean rpm_ostree_check_version (guint required_year, guint required_release); + G_END_DECLS diff --git a/tests/check/test-basic.sh b/tests/check/test-basic.sh index 091d241f..cfa7af01 100755 --- a/tests/check/test-basic.sh +++ b/tests/check/test-basic.sh @@ -24,7 +24,7 @@ export RPMOSTREE_SUPPRESS_REQUIRES_ROOT_CHECK=yes ensure_dbus -echo "1..22" +echo "1..23" setup_os_repository "archive-z2" "syslinux" @@ -182,7 +182,7 @@ fi assert_file_has_content err.txt 'Unknown.*command' echo "ok error on unknown command" -cat >test-rpmostree-gi <test-rpmostree-gi-arch <test-rpmostree-gi <