File protobuf.spec of Package protobuf

#
# spec file for package protobuf
#
# Copyright (c) 2024 SUSE LLC
# Copyright (c) 2024 Alessandro de Oliveira Faria (A.K.A. CABELO) <cabelo@opensuse.org> or <alessandro.faria@owasp.org>
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#

%define realname protobuf
%define realver  3.20.3
%define prever   0
%define srcext   tar.gz

%bcond_with ruby

#define so_ver   %{realver}
#define so_pkg   %(tr . _ <<< %{realver})_%{prever}

%define so_ver   31
%define so_pkg   31

%define alt_java_prio %(awk -F. '{printf "%%d%%02d%%02d", $1, $2, $3}' <<< %{realver})

%if %{with ruby}
%define ruby_lib %(ruby -rrbconfig -e 'puts RbConfig::CONFIG["rubylibprefix"]')
%define ruby_abi %(ruby -rrbconfig -e 'puts RbConfig::CONFIG["ruby_version"]')
%endif

# turn off the generation of debuginfo rpm  (RH9) ??
%global debug_package %{nil}

%if ! 0%{?py3_ver:1}
%define py3_ver %(%python3 -c "import sys; v=sys.version_info[:2]; print ('%%d.%%d'%%v)" 2>/dev/null || echo PYTHON3-NOT-FOUND)
%endif
%define py3_gen %(%python3 -c "import sys; v=sys.version_info[:2]; print ('%%d%%d'%%v)" 2>/dev/null || echo PYTHON3-NOT-FOUND)

%if ! 0%{?python3_sitelib:1}
%define python3_sitelib %(%python3 -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')
%endif
%if ! 0%{?python3_sitearch:1}
%define python3_sitearch %(%python3 -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib(True))')
%endif

%define gh_repo protocolbuffers/protobuf

# Common info
Name:          %{realname}
Version:       %{realver}
Release:       wiz%{?extraver:0.}1%{?dist}
License:       BSD-3-Clause
Group:         Development/Libraries/Other
URL:           https://protobuf.dev
Summary:       Language- and platform-neutral mechanism for serializing structured data

# Build-time parameters
BuildRequires: cmake >= 3.10
BuildRequires: gcc-c++
BuildRequires: ninja
BuildRequires: zlib-devel >= 1.2.0.4
BuildRequires: java-sdk
BuildRequires: python3-devel python3-setuptools
%if %{with ruby}
BuildRequires: ruby ruby-devel
%endif
%if 0%{?suse_version}
BuildRequires: fdupes
%endif
%if 0%{?rhel} >= 8
BuildRequires: annobin
%endif
BuildArch:     %{_build_arch}
BuildRoot:     %{_tmppath}/%{name}-root
Source0:       https://codeload.github.com/%{gh_repo}/%{srcext}/refs/tags/v%{realver}#/%{realname}-%{realver}%{?extraver}.%{srcext}

%description
Protocol buffers are a flexible, efficient, automated mechanism for serializing
structured data – think XML, but smaller, faster, and simpler. You define how
you want your data to be structured once, then you can use special generated
source code to easily write and read your structured data to and from a variety
of data streams and using a variety of languages. You can even update your data
structure without breaking deployed programs that are compiled against
the "old" format.

%package -n libprotobuf%{?so_pkg}
Group:         System/Libraries
Summary:       Shared library Google's data interchange format
Provides:      libprotobuf = %{version}-%{release}
%if 0%{?rhel}
Provides:      protobuf = %{version}-%{release}
Obsoletes:     protobuf < %{version}-%{release}
%endif

%description -n libprotobuf%{?so_pkg}
Protocol buffers are a flexible, efficient, automated mechanism for serializing
structured data – think XML, but smaller, faster, and simpler. You define how
you want your data to be structured once, then you can use special generated
source code to easily write and read your structured data to and from a variety
of data streams and using a variety of languages. You can even update your data
structure without breaking deployed programs that are compiled against
the "old" format.

This package contains the shared library.

%package -n libprotobuf-lite%{?so_pkg}
Group:         System/Libraries
Summary:       Lite shared library Google's data interchange format
Provides:      libprotobuf-lite = %{version}-%{release}
%if 0%{?rhel}
Provides:      protobuf-lite = %{version}-%{release}
Obsoletes:     protobuf-lite < %{version}-%{release}
%endif

%description -n libprotobuf-lite%{?so_pkg}
Protocol buffers are a flexible, efficient, automated mechanism for serializing
structured data – think XML, but smaller, faster, and simpler. You define how
you want your data to be structured once, then you can use special generated
source code to easily write and read your structured data to and from a variety
of data streams and using a variety of languages. You can even update your data
structure without breaking deployed programs that are compiled against
the "old" format.

This package contains the shared library without certain features like
descriptors and reflection.

%package devel
Group:         Development/Languages/C and C++
Summary:       Development files for Protocol Buffers library
Requires:      libprotobuf%{?so_pkg} = %{version}-%{release}
Requires:      libprotobuf-lite%{?so_pkg} = %{version}-%{release}
Provides:      libprotoc%{?so_pkg} = %{version}-%{release}
Provides:      libprotoc = %{version}-%{release}
Provides:      libprotobuf%{?so_pkg}-devel = %{version}-%{release} libprotobuf-devel = %{version}-%{release}
Provides:      libprotobuf-lite%{?so_pkg}-devel = %{version}-%{release} libprotobuf-lite-devel = %{version}-%{release}
Provides:      protobuf-compiler = %{version}-%{release}
Provides:      protoc = %{version}-%{release}
Obsoletes:     protobuf-compiler < %{version}-%{release}
Obsoletes:     protoc < %{version}-%{release}
Obsoletes:     libprotoc%{?so_pkg} < %{version}-%{release}

%description devel
Development files for Google's Protocol Buffers library

%package java
Group:         Development/Languages/Java
Summary:       Java Protocol Buffers runtime library
BuildArch:     noarch

%description java
Protocol buffers are a flexible, efficient, automated mechanism for serializing
structured data – think XML, but smaller, faster, and simpler. You define how
you want your data to be structured once, then you can use special generated
source code to easily write and read your structured data to and from a variety
of data streams and using a variety of languages. You can even update your data
structure without breaking deployed programs that are compiled against
the "old" format.

This package contains the Java Protocol Buffers runtime library.

%package -n python3-protobuf
Group:         Development/Languages/Python
Summary:       Python Protocol Buffers runtime library
Provides:      protobuf-python = %{version}-%{release} protobuf-python3 = %{version}-%{release}
Requires:      python(abi) = %{py3_ver}
Requires:      python3-six >= 1.9

%description -n python3-protobuf
Protocol buffers are a flexible, efficient, automated mechanism for serializing
structured data – think XML, but smaller, faster, and simpler. You define how
you want your data to be structured once, then you can use special generated
source code to easily write and read your structured data to and from a variety
of data streams and using a variety of languages. You can even update your data
structure without breaking deployed programs that are compiled against
the "old" format.

This package contains the Python Protocol Buffers runtime library.

%if %{with ruby}
%package -n rubygem-google-protobuf
Group:         Development/Languages/Ruby
Summary:       Ruby extension implementing Protocol Buffers
Requires:      ruby(abi) = %{ruby_abi}
Provides:      protobuf-ruby = %{version}-%{release}
Provides:      rubygem(google-protobuf) = %{version}-%{release}
Provides:      rubygem(%{ruby_abi}:google-protobuf) = %{version}-%{release}

%description -n rubygem-google-protobuf
Protocol buffers are a flexible, efficient, automated mechanism for serializing
structured data – think XML, but smaller, faster, and simpler. You define how
you want your data to be structured once, then you can use special generated
source code to easily write and read your structured data to and from a variety
of data streams and using a variety of languages. You can even update your data
structure without breaking deployed programs that are compiled against
the "old" format.

This package contains the Ruby extension that implements Protocol Buffers
functionality in Ruby.
%endif

# Preparation step (unpackung and patching if necessary)
%prep
%setup -q -n %{name}-%{version}%{?extraver}

%build
_CFLAGS='%{optflags} %{?gcc_lto}'
_LDFLAGS='-Wl,--as-needed -Wl,--strip-all -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro %{?gcc_lto}'
export PATH=%{_bindir}:/bin:%{_sbindir}:/sbin
cmake -S cmake -B build \
 -G Ninja \
 -DCMAKE_INSTALL_PREFIX=%{_prefix} \
 -DCMAKE_BUILD_TYPE=Release \
 -DCMAKE_CXX_FLAGS_RELEASE="$_CFLAGS" \
 -DCMAKE_C_FLAGS_RELEASE="$_CFLAGS" \
 -DCMAKE_EXE_LINKER_FLAGS_RELEASE="$_LDFLAGS" \
 -DCMAKE_MODULE_LINKER_FLAGS_RELEASE="$_LDFLAGS" \
 -DCMAKE_SHARED_LINKER_FLAGS_RELEASE="$_LDFLAGS" \
 -DCMAKE_VERBOSE_MAKEFILE=YES \
 \
 -Dprotobuf_BUILD_TESTS=NO \
 -Dprotobuf_BUILD_SHARED_LIBS=YES
ninja -v -C build %{?_smp_mflags}
ln -s ../build/protoc-%{version}.%{prever} src/protoc
export LD_LIBRARY_PATH=$(pwd -P)/build
export PROTOC=$(pwd -P)/build/protoc-%{version}.%{prever}
export LDFLAGS+=" -L$(pwd -P)/build"
# Build java jar
pushd java
../src/protoc --java_out=core/src/main/java -I../src ../src/google/protobuf/descriptor.proto
pushd core/src/main/java/com/google/protobuf
javac *.java
popd
%{__mkdir} -p out/com/google/protobuf
cp core/src/main/java/com/google/protobuf/*.class out/com/google/protobuf/
jar cfM protobuf-java-%{version}.jar -C out com
popd
# Build python module
pushd python
python3 setup.py build --cpp_implementation
popd
# Build ruby module
%if %{with ruby}
pushd ruby
gem build google-protobuf.gemspec
popd
%endif

%install
export CFLAGS="%{optflags} %{?gcc_lto} -I$(pwd -P)/third_party/utf8_range"
export CXXFLAGS="$CFLAGS"
export LDFLAGS='-Wl,--as-needed -Wl,--strip-all -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro %{?gcc_lto}'
export LDFLAGS+=" -L$(pwd -P)/build"
export PATH=%{_bindir}:/bin:%{_sbindir}:/sbin
DESTDIR=%{buildroot} ninja -v -C build install
# Install java jar
%{__install} -D -m0644 java/protobuf-java-%{version}.jar %{buildroot}%{_javadir}/protobuf-java-%{version}.jar
# create a dummy target for /etc/alternatives/protobuf-java
%{__mkdir} -p %{buildroot}%{_sysconfdir}/alternatives
%{__ln_s} -f %{_sysconfdir}/alternatives/protobuf-java.jar %{buildroot}%{_javadir}/protobuf-java.jar
# Install python module
pushd python
python3 setup.py install --cpp_implementation --prefix=%{_prefix} --root=%{buildroot} -O2
popd
# Install ruby module
%if %{with ruby}
export LDFLAGS="$LDFLAGS -fno-lto"
pushd ruby
gem install --local --no-update-sources --build-root %{buildroot} ./google-protobuf-*.gem
popd
%endif
# Remove duplicates
%if 0%{?suse_version}
%fdupes %{buildroot}%{_libdir}
%endif
# Remove odd file(s)
if [ "%{buildroot}" != "/" ]; then
  %{__rm} -vf %{buildroot}%{ruby_lib}/gems/%{ruby_abi}/extensions/*/%{ruby_abi}/google-protobuf-*/{gem.build_complete,gem_make.out}
fi

%clean
[ "%{buildroot}" != "/" ] && %{__rm} -rf %{buildroot}

%files -n libprotobuf%{?so_pkg}
%defattr(-,root,root)
%license LICENSE
%doc CONTRIBUTORS.txt README.md
%{_libdir}/libprotobuf.so.%{?so_ver}*
%{_libdir}/libprotobuf.so.%{?version}*

%files -n libprotobuf-lite%{?so_pkg}
%defattr(-,root,root)
%license LICENSE
%doc CONTRIBUTORS.txt README.md
%{_libdir}/libprotobuf-lite.so.%{?so_ver}*
%{_libdir}/libprotobuf-lite.so.%{?version}*

# Development stuff
%files devel
%defattr(-,root,root)
%license LICENSE
%doc CONTRIBUTORS.txt README.md
%{_bindir}/protoc*
%{_libdir}/pkgconfig/protobuf*.pc
%dir %{_libdir}/cmake/protobuf
%dir %{_libdir}/cmake
%{_libdir}/cmake/protobuf/*.cmake
%dir %{_includedir}/google/protobuf/
%dir %{_includedir}/google/
%{_includedir}/google/protobuf/*
%{_libdir}/libprotoc.so.%{?so_ver}*
%{_libdir}/libprotoc.so.%{?version}*
%{_libdir}/*.so
%exclude %{_libdir}/*.la

%files java
%defattr(-,root,root)
%license LICENSE
%doc CONTRIBUTORS.txt java/README.md
%{_javadir}/protobuf-java-%{version}.jar
%{_javadir}/protobuf-java.jar
%ghost %attr(0644,root,root) %{_sysconfdir}/alternatives/protobuf-java.jar

%files -n python3-protobuf
%defattr(-,root,root)
%license LICENSE
%doc CONTRIBUTORS.txt python/README.md
%dir %{python3_sitearch}/google/
%{python3_sitearch}/google/protobuf/
%{python3_sitearch}/protobuf-*-py%{py3_ver}.egg-info/
%{python3_sitearch}/protobuf-*-py%{py3_ver}-nspkg.pth

%if %{with ruby}
%files -n rubygem-google-protobuf
%defattr(-,root,root)
%license LICENSE
%doc CONTRIBUTORS.txt ruby/README.md
%dir %{ruby_lib}/gems/%{ruby_abi}
%dir %{ruby_lib}/gems
%{ruby_lib}/gems/%{ruby_abi}/*
%docdir %{ruby_lib}/gems/%{ruby_abi}/doc
%exclude %{ruby_lib}/gems/%{ruby_abi}/gems/google-protobuf-*/ext/
%endif

%post   -n libprotobuf%{?so_pkg} -p /sbin/ldconfig
%postun -n libprotobuf%{?so_pkg} -p /sbin/ldconfig

%post   -n libprotobuf-lite%{?so_pkg} -p /sbin/ldconfig
%postun -n libprotobuf-lite%{?so_pkg} -p /sbin/ldconfig

%post   devel -p /sbin/ldconfig
%postun devel -p /sbin/ldconfig

%post java
update-alternatives --install \
  %{_javadir}/protobuf-java.jar protobuf-java.jar %{_javadir}/protobuf-java-%{version}.jar %{alt_java_prio}

%postun java
if [ ! -f %{_javadir}/protobuf-java-%{version}.jar ]; then
   update-alternatives --remove protobuf-java.jar %{_javadir}/protobuf-java-%{version}.jar
fi

%changelog
openSUSE Build Service is sponsored by