%define debug_package %{nil}
%global oh_version OpenHarmony-v3.2-Release
%global distributeddata_dir %{_builddir}/foundation/distributeddatamgr/
%global build_opt /opt/distributed-middleware-build
%global bundle_dir %{build_opt}/openeuler/compiler_gn/foundation/distributeddatamgr/kv_store

Name:       distributeddatamgr_kv_store
Version:    1.0.0
Release:    4
Summary:    Supports distributed key-value data management
License:    Apache-2.0
Url:        https://gitee.com/openharmony/distributeddatamgr_kv_store
Source1:    https://gitee.com/openharmony/distributeddatamgr_kv_store/repository/archive/%{oh_version}.tar.gz#/distributeddatamgr_kv_store-%{oh_version}.tar.gz
Source2:    frameworks.BUILD.gn
Source3:    interface.BUILD.gn
Source4:    bundle.json
Patch1:     0001-remove-useless-dependency-datashare.patch
Patch2:     0002-remove-useless-dependency-hitrace-hisysevent.patch
Patch3:     0003-remove-useless-dependency-compile.patch
Patch4:     0004-establish-kvstore-dependence-on-boundscheck.patch

BuildRequires: libboundscheck, zlib, zlib-devel, jsoncpp, jsoncpp-devel, compat-openssl11-devel
BuildRequires: distributed-build, distributed-beget, hilog, commonlibrary_c_utils
BuildRequires: communication_ipc, communication_dsoftbus
BuildRequires: systemabilitymgr_safwk, systemabilitymgr_samgr
BuildRequires: security_dataclassification, security_device_auth, security_huks
BuildRequires: distributedhardware_device_manager

Requires: libboundscheck, zlib, zlib-devel, jsoncpp, jsoncpp-devel, compat-openssl11-devel
Requires: distributed-beget, hilog, commonlibrary_c_utils
Requires: communication_ipc, communication_dsoftbus
Requires: systemabilitymgr_safwk, systemabilitymgr_samgr
Requires: security_dataclassification, security_device_auth, security_huks
Requires: distributedhardware_device_manager

%description
KV database (KV store) is developed based on the KV storage capability provided by the current public basic library,and provides key-value pair data management capabilities for device applications. On a platform with processes, the parameter management provided by KV storage is accessed by a single process and cannot be used by other processes. On such platforms, KV storage is loaded in the application process as a basic library to ensure that it is not accessed by other processes.

%prep
rm -rf %{_builddir}/*
cp -rf %{build_opt} %{_builddir}/build
ln -s %{_builddir}/build/build_scripts/build.sh %{_builddir}/build.sh
ln -s %{_builddir}/build/core/gn/dotfile.gn %{_builddir}/.gn
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 %{distributeddata_dir}
if [ -d "%{distributeddata_dir}/kv_store" ]; then
  rm -rf %{distributeddata_dir}/kv_store
fi
mv %{distributeddata_dir}/%{name}-%{oh_version} %{distributeddata_dir}/kv_store

%patch -P1 -p1 -d %{distributeddata_dir}/kv_store
%patch -P2 -p1 -d %{distributeddata_dir}/kv_store
%patch -P3 -p1 -d %{distributeddata_dir}/kv_store
%patch -P4 -p1 -d %{_builddir}/foundation

%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

%install
%define frameworks_dir %{bundle_dir}/frameworks/libs/distributeddb
%define interface_dir %{bundle_dir}/interfaces/innerkits/distributeddata

install -d -m 0755 %{buildroot}/%{_includedir}/kv_store
install -d -m 0755 %{buildroot}/%{_libdir}
install -d -m 0755 %{buildroot}/%{frameworks_dir}
install -d -m 0755 %{buildroot}/%{interface_dir}
install -d -m 0755 %{buildroot}/system/lib64

cp %{SOURCE2} %{buildroot}/%{frameworks_dir}/BUILD.gn
cp %{SOURCE3} %{buildroot}/%{interface_dir}/BUILD.gn
cp %{SOURCE4} %{buildroot}/%{bundle_dir}

%ifarch aarch64
%define lib_out_path out/openeuler/linux_clang_arm64/distributeddatamgr/kv_store
%define header_out_path out/openeuler/innerkits/linux-arm64/kv_store
%endif
%ifarch x86_64
%define lib_out_path out/openeuler/linux_clang_x86_64/distributeddatamgr/kv_store
%define header_out_path out/openeuler/innerkits/linux-x86_64/kv_store
%endif

install -m 0755 %{_builddir}/%{lib_out_path}/*.so %{buildroot}%{_libdir}
install -m 0755 %{_builddir}/%{lib_out_path}/*.so %{buildroot}/system/lib64/

# 对外头文件，使用kv_store功能的头文件
find %{_builddir}/%{header_out_path} -name *.h -print0 | xargs -0 -i cp -rf {} %{buildroot}%{_includedir}/kv_store/
%define rdb_header_path foundation/distributeddatamgr/kv_store/frameworks/innerkitsimpl/rdb/include
cp -rf %{_builddir}/%{rdb_header_path}/*.h %{buildroot}%{_includedir}/kv_store/

# 导出所有的头文件到编译目录，给其他分布式数据组件编译使用
pushd %{_builddir}/foundation/distributeddatamgr/kv_store
for include_file in `find . \( -name kvstoremock -o -name test -o -name jskitsimpl \) -prune -o -name "*.h" -type f`
do
  if [[ ${include_file} != *.h ]]; then
    continue
  fi
  cp --parents ${include_file} %{buildroot}%{bundle_dir}
done
popd

%files
%{_libdir}/*.so
%{_includedir}/kv_store/*
%{bundle_dir}/*
/system/*

%changelog
* Tue Nov 21 2023 Peng He <hepeng68@huawei.com> - 1.0.0-4
- Update build requires and requires.

* Fri Aug 04 2023 Yuying Mu <muyuying1@huawei.com> - 1.0.0-3
- Disassemble the original patch according to its function.

* Thu Aug 03 2023 Peng He <hepeng68@huawei.com> - 1.0.0-2
- Add requires and rectify the contents of the RPM package.

* Tue Jul 18 2023 Ge Wang <wang__ge@126.com> - 1.0.0-1
- init package
