%define debug_package %{nil}
%global build_opt /opt/distributed-middleware-build
%global oh_version OpenHarmony-v3.2-Release
%global oe_version openEuler-22.03-LTS-SP2
%global communication_path %{_builddir}/foundation/communication
%global third_party_path %{_builddir}/third_party
%global binder_dest_path /usr/lib/modules/%(uname -r)/binder
%global ashmem_dest_path /usr/lib/modules/%(uname -r)/ashmem

Name:           communication_ipc
Version:        1.0.0
Release:        1%{?dist}
Summary:        Inter-process communication (IPC) and Remote Procedure Call (RPC)
License:        Apache-2.0 and GPL-2.0
URL:            https://gitee.com/openharmony/communication_ipc
Source1:        https://gitee.com/openharmony/communication_ipc/repository/archive/OpenHarmony-v3.2-Release.tar.gz #/communication_ipc-OpenHarmony-v3.2-Release.tar.gz
Source2:        https://gitee.com/openharmony/communication_dsoftbus/repository/archive/OpenHarmony-v3.2-Release.tar.gz #/communication_dsoftbus-OpenHarmony-v3.2-Release.tar.gz
Source3:        https://gitee.com/openharmony/third_party_sqlite/repository/archive/OpenHarmony-v3.2-Release.tar.gz #/third_party_sqlite-OpenHarmony-v3.2-Release.tar.gz
Source4:        https://gitee.com/openharmony/third_party_libcoap/repository/archive/OpenHarmony-v3.2-Release.tar.gz #/third_party_libcoap-OpenHarmony-v3.2-Release.tar.gz
Source5:        https://gitee.com/openharmony/third_party_mbedtls/repository/archive/OpenHarmony-v3.2-Release.tar.gz #/third_party_mbedtls-OpenHarmony-v3.2-Release.tar.gz
Source6:        https://gitee.com/openeuler/kernel/binder-%{oe_version}.tar.gz
Source7:        ipc.bundle.json
Source8:        ipc.BUILD.gn
Source9:        ipc_core.BUILD.gn
Source10:       ipc_single.BUILD.gn
Source11:       binder.BUILD.gn
Source12:       mbedtls.BUILD.gn
Source13:       dsoftbus.bundle.json
Source14:       dsoftbus.BUILD.gn
Source15:       sdk.BUILD.gn
Source16:       https://gitee.com/openeuler/kernel/ashmem-%{oe_version}.tar.gz
Source17:       binder.pp

Patch1:         0001-remove-dependence-on-access-token-and-hitrace.patch
Patch2:         0002-fix-build-error-from-header-include.patch
Patch3:         0003-adapt-binder-as-a-kernel-module.patch
Patch4:         0004-adapt-compilation-for-softbus_client.patch

BuildRequires:  gcc, make, hilog, kernel-devel, uname-build-checks
BuildRequires:  commonlibrary_c_utils
BuildRequires:  distributed-build, distributed-beget
BuildRequires:  notification_eventhandler
BuildRequires:  cjson-devel, openssl-devel

Requires:  commonlibrary_c_utils
Requires:  distributed-beget
Requires:  notification_eventhandler
Requires:  cjson-devel, openssl-devel

%description
The inter-process communication (IPC) and remote procedure call (RPC) mechanisms are used to implement cross-process communication.

# Decompress source code package, make patches to the source code.
%prep
rm -rf %{_builddir}/*

cp -rf %{build_opt} %{_builddir}/build
[ ! -L "%{_builddir}/build.sh" ] && ln -s %{_builddir}/build/build_scripts/build.sh %{_builddir}/build.sh
[ ! -L "%{_builddir}/.gn" ] && ln -s %{_builddir}/build/core/gn/dotfile.gn %{_builddir}/.gn
[ ! -L "%{_builddir}/build.py" ] && ln -s %{_builddir}/build/lite/build.py %{_builddir}/build.py
cp -rf %{_builddir}/build/openeuler/vendor %{_builddir}/
cp -rf %{_builddir}/build/openeuler/compiler_gn/* %{_builddir}

%setup -q -D -T -a 1 -c -n %{communication_path}
mv %{communication_path}/%{name} %{communication_path}/ipc
%patch -P1 -p1 -d %{communication_path}/ipc
%patch -P2 -p1 -d %{communication_path}/ipc

%setup -q -D -T -a 2 -c -n %{communication_path}
mv %{communication_path}/communication_dsoftbus-%{oh_version} %{communication_path}/dsoftbus
%patch -P4 -p1 -d %{communication_path}/dsoftbus

%setup -q -D -T -a 3 -c -n %{third_party_path}
mv %{third_party_path}/third_party_sqlite-%{oh_version} %{third_party_path}/sqlite

%setup -q -D -T -a 4 -c -n %{third_party_path}
mv %{third_party_path}/third_party_libcoap-%{oh_version} %{third_party_path}/libcoap

%setup -q -D -T -a 5 -c -n %{third_party_path}
mv %{third_party_path}/third_party_mbedtls-%{oh_version} %{third_party_path}/mbedtls

%setup -q -D -T -a 6 -c -n %{_builddir}
%patch -P3 -p1 -d %{_builddir}/binder

%build

%ifarch x86_64
%{_builddir}/build.sh --product-name openeuler --target-cpu x86_64
%endif

%ifarch aarch64
%{_builddir}/build.sh --product-name openeuler --target-cpu arm64
%endif

# build binder
cd %{_builddir}/binder
make %{?_smp_mflags}

cd %{_builddir}/ashmem/src
make %{?_smp_mflags}

%install
install -d -m 0755 %{buildroot}/%{_includedir}/ipc
install -d -m 0755 %{buildroot}/%{_includedir}/mbedtls
install -d -m 0755 %{buildroot}/%{_includedir}/dsoftbus
install -d -m 0755 %{buildroot}/%{_libdir}
install -d -m 0755 %{buildroot}/system/lib64
install -d -m 0755 %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/ipc/interfaces/innerkits/ipc_core
install -d -m 0755 %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/ipc/interfaces/innerkits/ipc_single
install -d -m 0755 %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/ipc/interfaces/innerkits/libdbinder
install -d -m 0755 %{buildroot}%{build_opt}/openeuler/compiler_gn/third_party/json
install -d -m 0755 %{buildroot}%{build_opt}/openeuler/compiler_gn/third_party/mbedtls/
install -d -m 0755 %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/dsoftbus/sdk/
install -d -m 0755 %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/dsoftbus/interfaces/inner_kits
install -d -m 0755 %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/dsoftbus/interfaces/kits/bus_center
install -d -m 0755 %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/dsoftbus/interfaces/kits/common
install -d -m 0755 %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/dsoftbus/interfaces/kits/discovery
install -d -m 0755 %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/dsoftbus/interfaces/kits/transport

%ifarch aarch64
%define header_out_path out/openeuler/innerkits/linux-arm64/ipc
%define module_out_path out/openeuler/linux_clang_arm64
%define dsoftbus_header_out_path out/openeuler/innerkits/linux-arm64/dsoftbus
%endif
%ifarch x86_64
%define header_out_path out/openeuler/innerkits/linux-x86_64/ipc
%define module_out_path out/openeuler/linux_clang_x86_64
%define dsoftbus_header_out_path out/openeuler/innerkits/linux-x86_64/dsoftbus
%endif

# install libs and headers from ipc
install -m 0755 %{_builddir}/%{module_out_path}/communication/ipc/*.so %{buildroot}/%{_libdir}
install -m 0755 %{_builddir}/%{module_out_path}/communication/ipc/*.so %{buildroot}/system/lib64
find %{_builddir}/%{header_out_path} -name *.h -print0 | xargs -0 -i cp -rf {} %{buildroot}%{_includedir}/ipc/

# install libs and headers from dsoftbus
%define dsoftbus_module_path %{module_out_path}/communication/dsoftbus
rm -f %{_builddir}/%{dsoftbus_module_path}/libsoftbus_server.z.so
install -m 0755 %{_builddir}/%{dsoftbus_module_path}/*.so %{buildroot}/%{_libdir}
install -m 0755 %{_builddir}/%{dsoftbus_module_path}/*.so %{buildroot}/system/lib64

# install libs and headers from third party components
%define third_party_module_path %{module_out_path}/common/common
install -m 0755 %{_builddir}/%{third_party_module_path}/*.so %{buildroot}/%{_libdir}
install -m 0755 %{_builddir}/%{third_party_module_path}/*.so %{buildroot}/system/lib64
install -m 0755 %{_builddir}/%{module_out_path}/common/dsoftbus/*.so %{buildroot}/%{_libdir}
install -m 0755 %{_builddir}/%{module_out_path}/common/dsoftbus/*.so %{buildroot}/system/lib64
install -m 0755 %{_builddir}/third_party/mbedtls/include/mbedtls/*.h %{buildroot}/%{_includedir}/mbedtls/
install -m 0755 %{_builddir}/foundation/communication/ipc/interfaces/innerkits/libdbinder/include/rpc_system_ability_callback.h %{buildroot}/%{_includedir}/ipc/
install -m 0755 %{_builddir}/foundation/communication/dsoftbus/interfaces/kits/common/softbus_error_code.h %{buildroot}/%{_includedir}/dsoftbus/
install -m 0755 %{_builddir}/foundation/communication/dsoftbus/interfaces/inner_kits/transport/inner_session.h %{buildroot}/%{_includedir}/dsoftbus/
install -m 0755 %{communication_path}/ipc/interfaces/innerkits/libdbinder/include/* %{buildroot}/%{_includedir}/ipc/
install -m 0755 %{communication_path}/dsoftbus/sdk/transmission/session/cpp/include/* %{buildroot}/%{_includedir}/ipc/


find %{_builddir}/%{header_out_path} -name *.h -print0 | xargs -0 -i cp -rf {} %{buildroot}/%{_includedir}/ipc/
find %{_builddir}/%{dsoftbus_header_out_path} -name *.h -print0 | xargs -0 -i cp -rvf {} %{buildroot}/%{_includedir}/dsoftbus/

#copy bundle.json
install -m 0755 %{SOURCE13} %{buildroot}/%{build_opt}/openeuler/compiler_gn/foundation/communication/dsoftbus/bundle.json

#copy BUILD.gn
install -m 0755 %{SOURCE7}  %{buildroot}/%{build_opt}/openeuler/compiler_gn/foundation/communication/ipc/bundle.json
install -m 0755 %{SOURCE8}  %{buildroot}/%{build_opt}/openeuler/compiler_gn/foundation/communication/ipc/BUILD.gn
install -m 0755 %{SOURCE9}  %{buildroot}/%{build_opt}/openeuler/compiler_gn/foundation/communication/ipc/interfaces/innerkits/ipc_core/BUILD.gn
install -m 0755 %{SOURCE10} %{buildroot}/%{build_opt}/openeuler/compiler_gn/foundation/communication/ipc/interfaces/innerkits/ipc_single/BUILD.gn
install -m 0755 %{SOURCE11} %{buildroot}/%{build_opt}/openeuler/compiler_gn/foundation/communication/ipc/interfaces/innerkits/libdbinder/BUILD.gn
install -m 0755 %{SOURCE12} %{buildroot}/%{build_opt}/openeuler/compiler_gn/third_party/mbedtls/BUILD.gn
install -m 0755 %{SOURCE14} %{buildroot}/%{build_opt}/openeuler/compiler_gn/foundation/communication/dsoftbus/BUILD.gn
install -m 0755 %{SOURCE15} %{buildroot}/%{build_opt}/openeuler/compiler_gn/foundation/communication/dsoftbus/sdk/BUILD.gn

# install ko file of binder
install -d %{buildroot}/%{binder_dest_path}/
install -d %{buildroot}/%{ashmem_dest_path}/
install -m 0755 %{_builddir}/binder/binder_linux.ko %{buildroot}/%{binder_dest_path}/
install -m 0755 %{_builddir}/ashmem/src/ashmem_linux.ko %{buildroot}/%{ashmem_dest_path}/
mkdir -p %{buildroot}/usr/share/pp
cp -pdf %{_builddir}/binder.pp %{buildroot}/usr/share/pp

#create soft link
ln -s /usr/mbedtls/include %{buildroot}%{build_opt}/openeuler/compiler_gn/third_party/mbedtls/include
ln -s /usr/include/ipc %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/ipc/interfaces/innerkits/ipc_core/include
ln -s /usr/include/ipc %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/ipc/interfaces/innerkits/libdbinder/include
ln -s /usr/include/nlohmann_json %{buildroot}%{build_opt}/openeuler/compiler_gn/third_party/json/include
ln -s /usr/include/nlohmann_json/single_include %{buildroot}%{build_opt}/openeuler/compiler_gn/third_party/json/single_include
ln -s /usr/include/dsoftbus %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/dsoftbus/interfaces/inner_kits/transport
ln -s /usr/include/dsoftbus %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/dsoftbus/interfaces/kits/bus_center/include
ln -s /usr/include/dsoftbus %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/dsoftbus/interfaces/kits/common/include
ln -s /usr/include/dsoftbus %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/dsoftbus/interfaces/kits/discovery/include
ln -s /usr/include/dsoftbus %{buildroot}%{build_opt}/openeuler/compiler_gn/foundation/communication/dsoftbus/interfaces/kits/transport/include


%files
%{_libdir}/*.so
%{_includedir}/ipc/*
%{_includedir}/mbedtls/*
%{_includedir}/dsoftbus/*
/system/*
/%{binder_dest_path}/*.ko
/%{ashmem_dest_path}/*.ko
%{build_opt}/*
/usr/share/pp

%post
semodule -X 300 -i /usr/share/pp/binder.pp

%changelog
* Sun Oct 08 2023 Peng He <hepeng68@huawei.com> - 1.0.0-1
* Thu Nov 22 2023 JiaQi Zhao <zhaojiaqi18@huawei.com> - 1.0.0-1
- Init package

