%global php_extdir  %(php-config --extension-dir 2>/dev/null || echo "undefined")
%{?perl_default_filter}
%global __provides_exclude_from ^(%{python3_sitearch}/.*\\.so|%{php_extdir}/.*\\.so)$
%global have_mongrel 0
%global have_jsx 0
%global want_d 0
%global want_java 1
%if 0%{?want_java} == 0
%global java_configure --without-java
%else
%global java_configure --with-java
%endif
%if 0%{?have_mongrel} == 0
%global ruby_configure --without-ruby
%global with_ruby 0
%else
%global ruby_configure --with-ruby
%global want_ruby 1
%endif
%if 0%{?have_jsx} == 0
%global erlang_configure --without-erlang
%global want_erlang 0
%else
%global erlang_configure --with-erlang
%global want_erlang 1
%endif
%global want_php 0
%if 0%{?want_php} == 0
%global php_langname %{nil}
%global php_configure --without-php
%else
%global php_langname PHP,\
%global php_configure --with-php
%endif
%global want_golang 0
%global golang_configure --without-go
%global want_lua 0
%global lua_configure --without-lua
Name:                thrift
Version:             0.14.0
Release:             7
Summary:             Software framework for cross-language services development
License:             ASL 2.0 and BSD and zlib
URL:                 https://thrift.apache.org/
Source0:             https://archive.apache.org/dist/thrift/%{version}/thrift-%{version}.tar.gz
# wget https://repo1.maven.org/maven2/org/apache/thrift/libthrift/%{version}/libthrift-%{version}.pom
# add maven-compiler-plugin in build tag
Source1:             libthrift-%{version}.pom
Source2:             https://raw.github.com/apache/thrift/%{version}/bootstrap.sh
Source3:             https://repo1.maven.org/maven2/org/apache/thrift/libthrift/%{version}/libthrift-%{version}-sources.jar
Patch0:              thrift-char.patch
Patch2:              configure-java-prefix.patch
Patch3:              backport-THRIFT-5348-Update-debian-copyright.patch
BuildRequires:       make autoconf automake bison boost-devel boost-static flex gcc-c++ glib2-devel
BuildRequires:       libevent-devel libstdc++-devel libtool maven openssl-devel qt5-qtbase-devel texlive
BuildRequires:       zlib-devel
Obsoletes:           libthrift-java < %{version}-%{release}
Obsoletes:           libthrift-javadoc < %{version}-%{release}
Obsoletes:           fb303 < %{version}-%{release}
Obsoletes:           python3-fb303 < %{version}-%{release}
Obsoletes:           fb303-devel < %{version}-%{release}
Obsoletes:           fb303-java < %{version}-%{release}

%if 0%{?want_golang} > 0
BuildRequires:       golang
Requires:            golang
%endif
%description
The Apache Thrift software framework for cross-language services
development combines a software stack with a code generation engine to
build services that work efficiently and seamlessly between C++, Java,
Python, %{?php_langname}and other languages.

%package devel
Summary:             Development files for thrift
Requires:            thrift%{?_isa} = %{version}-%{release} pkgconfig boost-devel
%description devel
The thrift-devel package contains libraries and header files for
developing applications that use thrift.

%package        qt
Summary:             Qt support for thrift
Requires:            thrift%{?_isa} = %{version}-%{release}
%description    qt
The thrift-qt package contains Qt bindings for thrift.

%package        glib
Summary:             GLib support for thrift
Requires:            thrift%{?_isa} = %{version}-%{release}
%description    glib
The thrift-qt package contains GLib bindings for thrift.

%package -n python3-thrift
Summary:             Python 3 support for thrift
BuildRequires:       python3-devel python3-setuptools
Requires:            thrift%{?_isa} = %{version}-%{release} python3
Obsoletes:           python-thrift < 0.10.0-1
Obsoletes:           python2-thrift < 0.10.0-14
%description -n python3-thrift
The python3-thrift package contains Python bindings for thrift.

%package -n perl-thrift
Summary:             Perl support for thrift
Provides:            perl(Thrift) = %{version}-%{release}
BuildRequires:       perl(Bit::Vector) perl(Class::Accessor) perl(ExtUtils::MakeMaker)
BuildRequires:       perl-generators
Requires:            perl(:MODULE_COMPAT_%(eval "`%{__perl} -V:version`"; echo $version))
Requires:            perl(Bit::Vector) perl(Encode) perl(HTTP::Request) perl(IO::Select)
Requires:            perl(IO::Socket::INET) perl(IO::String) perl(LWP::UserAgent) perl(POSIX)
Requires:            perl(base) perl(constant) perl(strict) perl(utf8) perl(warnings)
Provides:            perl(Thrift::Exception)
Provides:            perl(Thrift::MessageType)
Provides:            perl(Thrift::Type)
BuildArch:           noarch
%description -n perl-thrift
The perl-thrift package contains Perl bindings for thrift.
%if %{?want_d}

%package -n d-thrift
Summary:             D support for thrift
BuildRequires:       ldc
%description -n d-thrift
The d-thrift package contains D bindings for thrift.
%endif
%if 0%{?want_php} != 0

%package -n php-thrift
Summary:             PHP support for thrift
Requires:            thrift%{?_isa} = %{version}-%{release} php(zend-abi) = %{php_zend_api}
Requires:            php(api) = %{php_core_api} php(language) >= 5.3.0 php-date php-json
BuildRequires:       php-devel
%description -n php-thrift
The php-thrift package contains PHP bindings for thrift.
%endif

%package -n libthrift-java
Summary:             Java support for thrift
BuildRequires:       apache-commons-codec apache-commons-lang apache-commons-logging
BuildRequires:       httpcomponents-client httpcomponents-core java-devel javapackages-tools
BuildRequires:       javapackages-local junit log4j slf4j
BuildRequires:       mvn(javax.servlet:javax.servlet-api) = 3.1.0
Requires:            java-headless >= 1:1.6.0 javapackages-tools mvn(org.slf4j:slf4j-api)
Requires:            mvn(commons-lang:commons-lang) mvn(org.apache.httpcomponents:httpclient)
Requires:            mvn(org.apache.httpcomponents:httpcore)
BuildArch:           noarch
%description -n libthrift-java
The libthrift-java package contains Java bindings for thrift.

%if 0%{?want_ruby} > 0

%package -n ruby-thrift
Summary:             Ruby support for thrift
Requires:            thrift%{?_isa} = %{version}-%{release} ruby(release)
BuildRequires:       ruby-devel
%description -n ruby-thrift
The ruby-thrift package contains Ruby bindings for thrift.
%endif
%if 0%{?want_erlang} > 0

%package -n erlang-thrift
Summary:             Erlang support for thrift
Requires:            thrift%{?_isa} = %{version}-%{release} erlang erlang-jsx
BuildRequires:       erlang erlang-rebar
%description -n erlang-thrift
The erlang-thrift package contains Erlang bindings for thrift.
%endif

%prep
%autosetup -p1
%{?!el5:sed -i -e 's/^AC_PROG_LIBTOOL/LT_INIT/g' configure.ac}
find . -name \*.cpp -or -name \*.cc -or -name \*.h | xargs -r chmod 644
cp -p %{SOURCE2} bootstrap.sh
echo 'libthrift_c_glib_la_LIBADD = $(GLIB_LIBS) $(GOBJECT_LIBS) -L../cpp/.libs ' >> lib/c_glib/Makefile.am
echo 'libthriftqt5_la_LIBADD = $(QT_LIBS) -lthrift -L.libs' >> lib/cpp/Makefile.am
echo 'libthriftz_la_LIBADD = $(ZLIB_LIBS) -lthrift -L.libs' >> lib/cpp/Makefile.am
echo 'EXTRA_libthriftqt5_la_DEPENDENCIES = libthrift.la' >> lib/cpp/Makefile.am
echo 'EXTRA_libthriftz_la_DEPENDENCIES = libthrift.la' >> lib/cpp/Makefile.am
sed -i 's|ANT_VALID=.*|ANT_VALID=1|' aclocal/ax_javac_and_java.m4
shopt -s globstar
sed -i -E 's@^(#!.*/env) *python *$@\1 python3@' **/*.py

#prepare libthrift's compiler environment
mkdir libthrift_dir
cp %{SOURCE1} ./libthrift_dir/pom.xml
cp %{SOURCE3} ./libthrift_dir
pushd libthrift_dir
  jar -xvf libthrift-%{version}-sources.jar
popd

%build
export PY_PREFIX=%{_prefix}
export PERL_PREFIX=%{_prefix}
export PHP_PREFIX=%{php_extdir}
export JAVA_PREFIX=%{_javadir}
export RUBY_PREFIX=%{_prefix}
export GLIB_LIBS=$(pkg-config --libs glib-2.0)
export GLIB_CFLAGS=$(pkg-config --cflags glib-2.0)
export GOBJECT_LIBS=$(pkg-config --libs gobject-2.0)
export GOBJECT_CFLAGS=$(pkg-config --cflags gobject-2.0)
find %{_builddir} -name rebar -exec rm -f '{}' \;
find . -name Makefile\* -exec sed -i -e 's/[.][/]rebar/rebar/g' {} \;
sed -i 's|-Dinstall.javadoc.path=$(DESTDIR)$(docdir)/java|-Dinstall.javadoc.path=$(DESTDIR)%{_javadocdir}/thrift|' lib/java/Makefile.*
sed -i 's|$(INSTALL) $$p|$(INSTALL) --mode 644 $$p|g' lib/erl/Makefile.am
sh ./bootstrap.sh
export PYTHON=%{_bindir}/python3
%ifarch loongarch64 sw_64
%configure --disable-dependency-tracking --disable-static --with-boost=/usr --with-boost-libdir=/usr/lib64 \
%else
%configure --disable-dependency-tracking --disable-static --with-boost=/usr \
%endif
  --docdir=%{?_pkgdocdir}%{!?_pkgdocdir:%{_docdir}/thrift-%{version}} \
  %{java_configure} %{ruby_configure} %{erlang_configure} %{golang_configure} %{php_configure} %{lua_configure}
sed -i -e 's/ -shared / -Wl,--as-needed\0/g' libtool
%make_build

pushd libthrift_dir
  mvn package -DskipTests -Dtar
popd

%install
%make_install
find %{buildroot} -name '*.la' -exec rm -f {} ';'
find %{buildroot} -name fastbinary.so | xargs -r chmod 755
find %{buildroot} -name \*.erl -or -name \*.hrl -or -name \*.app | xargs -r chmod 644
find %{buildroot} -name \*.jar -a \! -name \*thrift\* -exec rm -f '{}' \;
find %{buildroot} -name \*.pod -exec rm -f '{}' \;
find %{buildroot} -name .packlist -exec rm -f '{}' \;
find %{buildroot}/usr/lib/perl5 -type d -empty -delete
mkdir -p %{buildroot}/%{perl_vendorlib}/
mv %{buildroot}/usr/lib/perl5/* %{buildroot}/%{perl_vendorlib}
%if 0%{?want_php} != 0
mkdir -p %{buildroot}/%{_datadir}/php/
mv %{buildroot}/%{php_extdir}/Thrift %{buildroot}/%{_datadir}/php/
%endif # want_php
find %{buildroot} -name Thread.h -exec chmod a-x '{}' \;
find %{buildroot} -name \*.py -exec grep -q /usr/bin/env {} \; -print | xargs -r chmod 755
%ldconfig_scriptlets

install -d -m 755 %{buildroot}%{_datadir}/java/libthrift
install -d -m 755 %{buildroot}%{_datadir}/maven-poms/libthrift
cp libthrift_dir/target/libthrift-%{version}.jar %{buildroot}%{_datadir}/java/libthrift/libthrift.jar
cp libthrift_dir/pom.xml %{buildroot}%{_datadir}/maven-poms/libthrift/libthrift.pom
%add_maven_depmap libthrift/libthrift.pom libthrift/libthrift.jar -f "libthrift"

%files
%doc LICENSE NOTICE
%{_bindir}/thrift
%{_libdir}/libthrift-%{version}.so
%{_libdir}/libthriftz-%{version}.so
%{_libdir}/libthriftnb-%{version}.so

%files glib
%{_libdir}/libthrift_c_glib.so
%{_libdir}/libthrift_c_glib.so.*

%files qt
%{_libdir}/libthriftqt5.so
%{_libdir}/libthriftqt5-%{version}.so

%files devel
%{_includedir}/thrift
%{_libdir}/*.so
%{_libdir}/*.so.0
%{_libdir}/*.so.0.0.0
%exclude %{_libdir}/lib*-%{version}.so
%{_libdir}/pkgconfig/thrift-z.pc
%{_libdir}/pkgconfig/thrift-qt5.pc
%{_libdir}/pkgconfig/thrift-nb.pc
%{_libdir}/pkgconfig/thrift.pc
%{_libdir}/pkgconfig/thrift_c_glib.pc
%doc LICENSE NOTICE

%files -n perl-thrift
%{perl_vendorlib}/Thrift
%{perl_vendorlib}/Thrift.pm
%doc LICENSE NOTICE
%if 0%{?want_php} != 0

%files -n php-thrift
%config(noreplace) /etc/php.d/thrift_protocol.ini
%{_datadir}/php/Thrift/
%{php_extdir}/thrift_protocol.so
%doc LICENSE NOTICE
%endif
%if %{?want_erlang} > 0

%files -n erlang-thrift
%{_libdir}/erlang/lib/thrift-%{version}/
%doc LICENSE NOTICE
%endif

%files -n python3-thrift
%{python3_sitearch}/thrift
%{python3_sitearch}/thrift-%{version}-py%{python3_version}.egg-info
%doc LICENSE NOTICE
%if 0%{?want_java} > 0

%files -n libthrift-java 
%{_datadir}/java/libthrift/libthrift.jar
%{_datadir}/maven-poms/libthrift/libthrift.pom
%{_datadir}/maven-metadata/thrift-libthrift.xml
%doc LICENSE NOTICE
%endif

%changelog
* Mon Mar 03 2025 maqi <maqi@uniontech.com> - 0.14.0-7
- specify boost-libdir for sw_64

* Thu May 18 2023 Wenlong Zhang <zhangwenlong@loongson.cn> - 0.14.0-6
- specify boost-libdir for loongarch64

* Fri Dec 30 2022 Cao Jingbo <caojb@chinatelecom.cn> - 0.14.0-5
- Use version macro relpace version number text.

* Wed Dec 28 2022 Cao Jingbo <caojb@chinatelecom.cn> - 0.14.0-4
- THRIFT-5348: Update debian/copyright [skip ci] Patch: Jihoon Seo

* Mon Jun 13 2022 fushanqing <fushanqing@kylinos.cn> - 0.14.0-3
- Remove the dist tag

* Wed Jul 28 2021 Ge Wang <wangge20@huawei.com> - 0.14.0-2
- Add libthrift-java subpackage

* Tue Feb 23 2021 wangyue <wangyue92@huawei.com> - 0.14.0-1
- Update to 0.14.0 to fix CVE-2020-13949 and Drop fb303 package and switch to qt5

* Thu Nov 12 2020 wangxiao <wangxiao65@huawei.com> - 0.10.0-3
- Fix CVE-2019-0205 and CVE-2019-0210

* Thu Nov 05 2020 wangyue <wangyue92@huawei.com> - 0.10.0-2
- Fix CVE-2018-11798 and CVE-2018-1320.

* Tue Aug 18 2020 yaokai <yaokai13@huawei.com> - 0.10.0-1
- package init
