%bcond_without java
%bcond_with gtest
%global emacs_version %(pkg-config emacs --modversion)
%global emacs_lispdir %(pkg-config emacs --variable sitepkglispdir)
%global emacs_startdir %(pkg-config emacs --variable sitestartdir)
Summary:             Protocol Buffers - Google's data interchange format
Name:                protobuf2
Version:             2.5.0
Release:             4
License:             BSD
Source:              http://protobuf.googlecode.com/files/protobuf-%{version}.tar.bz2
Source1:             ftdetect-proto.vim
Source2:             protobuf-init.el
Patch1:              protobuf-2.5.0-gtest.patch
Patch2:              protobuf-2.5.0-java-fixes.patch
Patch3:              0001-Add-generic-GCC-support-for-atomic-operations.patch
Patch4:              protobuf-2.5.0-makefile.patch
Patch5:              0001-protobuf2-add-support-for-loongarch64.patch
URL:                 http://code.google.com/p/protobuf/
BuildRequires:       automake autoconf libtool pkgconfig zlib-devel emacs emacs-el >= 24.1 maven-plugin-bundle gcc-c++
%if %{with gtest}
BuildRequires:       gtest-devel
%endif
%description
Protocol Buffers are a way of encoding structured data in an efficient
yet extensible format. Google uses Protocol Buffers for almost all of
its internal RPC protocols and file formats.
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 compiler
Summary:             Protocol Buffers compiler
Requires:            %{name} = %{version}-%{release}
%description compiler
This package contains Protocol Buffers compiler for all programming
languages

%package devel
Summary:             Protocol Buffers C++ headers and libraries
Requires:            %{name} = %{version}-%{release} %{name}-compiler = %{version}-%{release}
Requires:            zlib-devel pkgconfig
%description devel
This package contains Protocol Buffers compiler for all languages and
C++ headers and libraries

%package static
Summary:             Static development files for %{name}
Requires:            %{name} = %{version}-%{release}
%description static
Static libraries for Protocol Buffers

%package lite
Summary:             Protocol Buffers LITE_RUNTIME libraries
%description lite
Protocol Buffers built with optimize_for = LITE_RUNTIME.
The "optimize_for = LITE_RUNTIME" option causes the compiler to generate code
which only depends libprotobuf-lite, which is much smaller than libprotobuf but
lacks descriptors, reflection, and some other features.

%package lite-devel
Summary:             Protocol Buffers LITE_RUNTIME development libraries
Requires:            %{name}-devel = %{version}-%{release} %{name}-lite = %{version}-%{release}
%description lite-devel
This package contains development libraries built with
optimize_for = LITE_RUNTIME.
The "optimize_for = LITE_RUNTIME" option causes the compiler to generate code
which only depends libprotobuf-lite, which is much smaller than libprotobuf but
lacks descriptors, reflection, and some other features.

%package lite-static
Summary:             Static development files for %{name}-lite
Requires:            %{name}-devel = %{version}-%{release}
%description lite-static
This package contains static development libraries built with
optimize_for = LITE_RUNTIME.
The "optimize_for = LITE_RUNTIME" option causes the compiler to generate code
which only depends libprotobuf-lite, which is much smaller than libprotobuf but
lacks descriptors, reflection, and some other features.

%package vim
Summary:             Vim syntax highlighting for Google Protocol Buffers descriptions
Requires:            vim-enhanced
%description vim
This package contains syntax highlighting for Google Protocol Buffers
descriptions in Vim editor

%package emacs
Summary:             Emacs mode for Google Protocol Buffers descriptions
Requires:            emacs >= 0%{emacs_version}
%description emacs
This package contains syntax highlighting for Google Protocol Buffers
descriptions in the Emacs editor.

%package emacs-el
Summary:             Elisp source files for Google protobuf Emacs mode
Requires:            %{name}-emacs = %{version}
%description emacs-el
This package contains the elisp source files for %{name}-emacs
under GNU Emacs. You do not need to install this package to use
%{name}-emacs.
%if %{with java}

%package java
Summary:             Java Protocol Buffers runtime library
BuildRequires:       java-devel >= 1.6 jpackage-utils maven-local maven-compiler-plugin
BuildRequires:       maven-install-plugin maven-jar-plugin maven-javadoc-plugin
BuildRequires:       maven-resources-plugin maven-surefire-plugin maven-antrun-plugin
Conflicts:           %{name}-compiler > %{version}
Conflicts:           %{name}-compiler < %{version}
%description java
This package contains Java Protocol Buffers runtime library.

%package javadoc
Summary:             Javadocs for %{name}-java
Requires:            %{name}-java = %{version}-%{release}
%description javadoc
This package contains the API documentation for %{name}-java.
%endif

%prep
%setup -q -n protobuf-2.5.0
%if %{with gtest}
rm -rf gtest
%patch1 -p1 -b .gtest
%endif
chmod 644 examples/*
%if %{with java}
%patch2 -p1 -b .java-fixes
rm -rf java/src/test
%endif
%patch3 -p1 -b .generic-atomics
%patch4 -p1 -b .generic-atomics-makefile
%ifarch loongarch64
%patch5 -p1
%endif

%build
iconv -f iso8859-1 -t utf-8 CONTRIBUTORS.txt > CONTRIBUTORS.txt.utf8
mv CONTRIBUTORS.txt.utf8 CONTRIBUTORS.txt
export PTHREAD_LIBS="-lpthread"
./autogen.sh
%configure
make %{?_smp_mflags}
%if %{with java}
pushd java
%mvn_file : %{name}
%mvn_build
popd
%endif
emacs -batch -f batch-byte-compile editors/protobuf-mode.el

%check

%install
rm -rf %{buildroot}
make %{?_smp_mflags} install DESTDIR=%{buildroot} STRIPBINARIES=no INSTALL="%{__install} -p" CPPROG="cp -p"
find %{buildroot} -type f -name "*.la" -exec rm -f {} \;
install -p -m 644 -D %{SOURCE1} %{buildroot}%{_datadir}/vim/vimfiles/ftdetect/proto.vim
install -p -m 644 -D editors/proto.vim %{buildroot}%{_datadir}/vim/vimfiles/syntax/proto.vim
%if %{with java}
pushd java
%mvn_install
popd
%endif
mkdir -p $RPM_BUILD_ROOT%{emacs_lispdir}
mkdir -p $RPM_BUILD_ROOT%{emacs_startdir}
install -p -m 0644 editors/protobuf-mode.el $RPM_BUILD_ROOT%{emacs_lispdir}
install -p -m 0644 editors/protobuf-mode.elc $RPM_BUILD_ROOT%{emacs_lispdir}
install -p -m 0644 %{SOURCE2} $RPM_BUILD_ROOT%{emacs_startdir}

%post -p /sbin/ldconfig

%postun -p /sbin/ldconfig

%post lite -p /sbin/ldconfig

%postun lite -p /sbin/ldconfig

%post compiler -p /sbin/ldconfig

%postun compiler -p /sbin/ldconfig

%files
%defattr(-, root, root, -)
%{_libdir}/libprotobuf.so.*
%doc CHANGES.txt CONTRIBUTORS.txt COPYING.txt README.txt

%files compiler
%defattr(-, root, root, -)
%{_bindir}/protoc
%{_libdir}/libprotoc.so.*
%doc COPYING.txt README.txt

%files devel
%defattr(-, root, root, -)
%dir %{_includedir}/google
%{_includedir}/google/protobuf/
%{_libdir}/libprotobuf.so
%{_libdir}/libprotoc.so
%{_libdir}/pkgconfig/protobuf.pc
%doc examples/add_person.cc examples/addressbook.proto examples/list_people.cc examples/Makefile examples/README.txt

%files static
%defattr(-, root, root, -)
%{_libdir}/libprotobuf.a
%{_libdir}/libprotoc.a

%files lite
%defattr(-, root, root, -)
%{_libdir}/libprotobuf-lite.so.*

%files lite-devel
%defattr(-, root, root, -)
%{_libdir}/libprotobuf-lite.so
%{_libdir}/pkgconfig/protobuf-lite.pc

%files lite-static
%defattr(-, root, root, -)
%{_libdir}/libprotobuf-lite.a

%files vim
%defattr(-, root, root, -)
%{_datadir}/vim/vimfiles/ftdetect/proto.vim
%{_datadir}/vim/vimfiles/syntax/proto.vim

%files emacs
%defattr(-,root,root,-)
%{emacs_startdir}/protobuf-init.el
%{emacs_lispdir}/protobuf-mode.elc

%files emacs-el
%defattr(-,root,root,-)
%{emacs_lispdir}/protobuf-mode.el
%if %{with java}

%files java -f java/.mfiles
%doc examples/AddPerson.java examples/ListPeople.java

%files javadoc -f java/.mfiles-javadoc
%endif

%changelog
* Tue May 23 2023 huajingyun <huajingyun@loongson.cn> - 2.5.0-4
- Add loongarch64 support

* Wed Aug 18 2021 lingsheng <lingsheng@huawei.com> - 2.5.0-3
- Modify subpackage's install require

* Mon May 31 2021 huanghaitao <huanghaitao8@huawei.com> - 2.5.0-2
- Completing build dependencies

* Fri Feb 26 2021 Ge Wang <wangge20@huawei.com> - 2.5.0-1
- Package init
