distro FEATURE apkg-powered upstream rpm packaging
Using this .spec file originally provided by Martin Winter with some
modifications and apkg magic, I'm able to build both source package
libyang2-2.0.0.9.gf60c0dbf-1.fc34.src.rpm
and native RPM packages
libyang2-2.0.0.9.gf60c0dbf-1.fc34.x86_64.rpm
libyang2-devel-2.0.0.9.gf60c0dbf-1.fc34.x86_64.rpm
libyang2-tools-2.0.0.9.gf60c0dbf-1.fc34.x86_64.rpm
libyang2-debuginfo-2.0.0.9.gf60c0dbf-1.fc34.x86_64.rpm
libyang2-debugsource-2.0.0.9.gf60c0dbf-1.fc34.x86_64.rpm
libyang2-tools-debuginfo-2.0.0.9.gf60c0dbf-1.fc34.x86_64.rpm
by running
apkg build -i
on this commit. It should work on any RPM-based distro including SUSE.
I was able to install resulting packages on each testing system.
I've tested this on:
* Fedora 33 VM
* Fedora 34 docker container (fedora:34)
* openSUSE 15 docker container (opensuse/leap)
distro/README.md contains (3 lines of) instructions on howto test this
using apkg in a VM/container/machine of your choice.
diff --git a/distro/README.md b/distro/README.md
new file mode 100644
index 0000000..995cb53
--- /dev/null
+++ b/distro/README.md
@@ -0,0 +1,9 @@
+# upstream packaging
+
+## RPM-based system (Fedora, CentOS, SUSE, ...) quickstart
+
+sudo dnf install -y git rpm-build python3-pip
+pip3 install apkg
+apkg build -i
+
+See apkg docs: https://pkg.labs.nic.cz/pages/apkg/
diff --git a/distro/config/apkg.toml b/distro/config/apkg.toml
new file mode 100644
index 0000000..95f92ed
--- /dev/null
+++ b/distro/config/apkg.toml
@@ -0,0 +1,10 @@
+[project]
+name = "libyang2"
+make_archive_script = "tools/make-dev-archive.sh"
+
+[upstream]
+archive_url = "https://github.com/CESNET/libyang/archive/refs/tags/v{{ version }}.tar.gz"
+version_script = "tools/upstream-version.sh"
+
+[apkg]
+compat = 1
diff --git a/distro/pkg/rpm/libyang2.spec b/distro/pkg/rpm/libyang2.spec
new file mode 100644
index 0000000..7fc3cd0
--- /dev/null
+++ b/distro/pkg/rpm/libyang2.spec
@@ -0,0 +1,87 @@
+Name: libyang2
+Version: {{ version }}
+Release: {{ release }}%{?dist}
+Summary: YANG data modeling language library
+Url: https://github.com/CESNET/libyang
+Source: libyang-%{version}.tar.gz
+License: BSD-3-Clause
+
+BuildRequires: cmake
+BuildRequires: doxygen
+BuildRequires: gcc
+BuildRequires: libcmocka-devel
+BuildRequires: make
+BuildRequires: pcre2-devel
+
+%if "x%{?suse_version}" == "x"
+Requires: pcre2
+%else
+Requires: libpcre2-posix2
+%endif
+
+%package -n libyang2-devel
+Summary: Headers of libyang library
+Conflicts: libyang-devel
+Requires: %{name} = %{version}-%{release}
+Requires: pcre2-devel
+
+%package -n libyang2-tools
+Summary: Helper Tools and examples for libyang library
+Conflicts: libyang
+Provides: libyang-tools
+Requires: %{name} = %{version}-%{release}
+
+%description -n libyang2-devel
+Headers of libyang library.
+
+%description -n libyang2-tools
+Helper Tools and examples for libyang library.
+
+%description
+libyang is a YANG data modelling language parser and toolkit
+written (and providing API) in C.
+
+%prep
+%setup -n libyang-%{version}
+mkdir build
+
+%build
+cd build
+cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr \
+ -DCMAKE_BUILD_TYPE:String="Release" \
+ -DCMAKE_C_FLAGS="${RPM_OPT_FLAGS}" \
+ -DCMAKE_CXX_FLAGS="${RPM_OPT_FLAGS}" \
+ ..
+make
+
+%check
+cd build
+ctest --output-on-failure
+
+%install
+cd build
+make DESTDIR=%{buildroot} install
+
+%post -p /sbin/ldconfig
+
+%postun -p /sbin/ldconfig
+
+%files
+%defattr(-,root,root)
+%{_libdir}/libyang.so.2*
+
+%files -n libyang2-devel
+%defattr(-,root,root)
+%{_libdir}/libyang.so
+%{_libdir}/pkgconfig/libyang.pc
+%{_includedir}/libyang/*.h
+%dir %{_includedir}/libyang/
+
+%files -n libyang2-tools
+%{_bindir}/yanglint
+%{_bindir}/yangre
+%{_mandir}/man1/yanglint.1.gz
+
+%changelog
+* Fri Apr 30 2021 Jakub Ružička <jakub.ruzicka@nic.cz> - {{ version }}-{{ release }}
+- upstream package
diff --git a/tools/make-dev-archive.sh b/tools/make-dev-archive.sh
new file mode 100755
index 0000000..ec3f7ae
--- /dev/null
+++ b/tools/make-dev-archive.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+# create archive from current source using git
+
+VERSION=$(git describe --tags --always)
+# skip "v" from start of version number (if it exists) and replace - with .
+VERSION=${VERSION#v}
+VERSION=${VERSION//[-]/.}
+
+NAMEVER=libyang-$VERSION
+ARCHIVE=$NAMEVER.tar.gz
+
+git archive --format tgz --output $ARCHIVE --prefix $NAMEVER/ HEAD
+
+# apkg expects stdout to list archive files
+echo $ARCHIVE
diff --git a/tools/upstream-version.sh b/tools/upstream-version.sh
new file mode 100755
index 0000000..4489c8e
--- /dev/null
+++ b/tools/upstream-version.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+# get latest upstream libyang version from github
+
+RLS_URL=https://api.github.com/repos/CESNET/libyang/releases
+VERSION=$(curl -s $RLS_URL | grep tag_name | cut -d '"' -f 4 | sort --version-sort | tail -n 1)
+VERSION=${VERSION#v}
+echo $VERSION