%define debug_package %{nil}
%global oh_version OpenHarmony-v3.2-Release
%global kv_store_dir %{_builddir}/foundation/distributeddatamgr/
%global build_opt /opt/distributed-middleware-build
%global bundle_dir %{build_opt}/openeuler/compiler_gn/foundation/distributeddatamgr/kv_store
%global frameworks_dir %{bundle_dir}/frameworks/libs/distributeddb
%global interface_dir %{bundle_dir}/interfaces/innerkits/distributeddata

Name:       distributeddatamgr_kv_store
Version:    1.0.0
Release:    3
Summary:    Supports distributed key-value and document-based data management, and supports the use of schemas to describe data formats.
License:    Apache-2.0
Url:        https://gitee.com/openharmony/distributeddatamgr_kv_store
Source0:    https://gitee.com/openharmony/distributeddatamgr_kv_store/repository/archive/OpenHarmony-v3.2-Release.tar.gz#/distributeddatamgr_kv_store-OpenHarmony-v3.2-Release.tar.gz
Source1:    frameworks.BUILD.gn
Source2:    interface.BUILD.gn
Source3:    bundle.json
Patch0:    0001-remove-useless-dependency-datashare.patch
Patch1:    0002-remove-useless-dependency-hitrace-hisysevent.patch
Patch2:    0003-remove-useless-dependency-compile.patch
Patch3:    0004-add-huks-component.patch
Patch4:    0005-add-third-part-component.patch
Patch5:    0006-establish-kvstore-dependence-on-boundscheck.patch

BuildRequires: libatomic libicu-devel libxml2-devel openssl-devel
BuildRequires: distributed-build distributed-build_lite hilog distributed-utils
BuildRequires: jsoncpp jsoncpp-devel

Requires:      jsoncpp jsoncpp-devel hilog distributed-utils

%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}/*

# 在rpmbuild/BUILD目录下构建变成目录结构
cd %{_builddir}
cp -rp %{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

mv build/openeuler/vendor %{_builddir}/
cp -rf build/openeuler/compiler_gn/* %{_builddir}/
%setup -q -T -a 0 -c -n %{kv_store_dir}
mv %{kv_store_dir}%{name}-%{oh_version} %{kv_store_dir}kv_store
%patch0 -p1 -d %{kv_store_dir}kv_store
%patch1 -p1 -d %{kv_store_dir}kv_store
%patch2 -p1 -d %{kv_store_dir}kv_store
%patch3 -p1 -d %{_builddir}/base/security
%patch4 -p1 -d %{_builddir}/third_party
%patch5 -p1 -d %{_builddir}/foundation

%build
cd %{_builddir}
rm -rf %{_builddir}/out

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

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

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

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

%ifarch aarch64
module_out_path="out/openeuler/linux_clang_arm64/distributeddatamgr/kv_store"
header_out_path="out/openeuler/innerkits/linux-arm64/kv_store"
%endif
%ifarch x86_64
module_out_path="out/openeuler/linux_clang_x86_64/distributeddatamgr/kv_store"
header_out_path="out/openeuler/innerkits/linux-x86_64/kv_store"
%endif

install -m 0755 %{_builddir}/${module_out_path}/*.so %{buildroot}%{_libdir}
install -m 0755 %{_builddir}/${module_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/
cp -vf %{_builddir}/foundation/distributeddatamgr/kv_store/frameworks/innerkitsimpl/rdb/include/*.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
* 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
