%define debug_package %{nil}
%global source0_name dsoftbus_standard
%global source0_release v3.1.2-release
%global source1_name yocto-embedded-tools
%global source1_release v1.0.0-release
%global source2_name embedded-ipc
%global source2_release v1.0.0-release
%global source3_name libboundscheck
%global source3_release v1.1.11
%global source4_name gn-linux-arm64

Name: dsoftbus
Version: 1.0.0
Release: 1
Summary: openEuler embedded softbus capability support
License: Apache License 2.0

Source0: https://gitee.com/openeuler/%{source0_name}/repository/archive/%{source0_release}.tar.gz #/%{source0_name}-%{source0_release}.tar.gz
Source1: https://gitee.com/openeuler/%{source1_name}/repository/archive/%{source1_release}.tar.gz #/%{source1_name}-%{source1_release}.tar.gz
Source2: https://gitee.com/openeuler/%{source2_name}/repository/archive/%{source2_release}.tar.gz #/%{source2_name}-%{source2_release}.tar.gz
Source3: https://gitee.com/src-openeuler/%{source3_name}/raw/openEuler-22.03-LTS-Next/%{source3_name}-%{source3_release}.tar.gz
Source4: https://chrome-infra-packages.appspot.com/p/gn/gn/linux-arm64/+/DC9187N-24PO5pEFOlVDm4kNr7ewdhh-Lp73tDNUY5QC #/%{source4_name}.zip

Patch0000: 0000-change-set-for-obs-build.patch

BuildRequires: gcc, g++, cmake, python, zip, unzip, ninja-build, git

%description
OpenEuler supports distributed softbus capability, which is part of openEuler's embedded capability

%prep
# Create the directory needed for the build
dsoftbus_build_tools="%{_builddir}/dsoftbus_build_tools"
dsoftbus_build_dir="%{_builddir}/dsoftbus_build"
dsoftbus_prebuilts_build_tools="${dsoftbus_build_dir}/prebuilts/build-tools/linux-x86/bin"
dsoftbus_thirdparty="${dsoftbus_build_dir}/third_party"
dsoftbus_utils="${dsoftbus_build_dir}/utils"
dsoftbus_src="${dsoftbus_build_dir}/foundation/communication"
dsoftbus_hichain="${dsoftbus_build_dir}/base/security"

mkdir -p ${dsoftbus_prebuilts_build_tools}
mkdir -p ${dsoftbus_thirdparty}
mkdir -p ${dsoftbus_utils}
mkdir -p ${dsoftbus_src}
mkdir -p ${dsoftbus_hichain}

# Decompress pack
%setup -q -T -D -b 0 -n %{source0_name}-%{source0_release}
%setup -q -T -D -b 1 -n %{source1_name}-%{source1_release}
%setup -q -T -D -b 2 -n %{source2_name}-%{source2_release}
%setup -q -T -D -b 3 -n %{source3_name}-%{source3_release}

cd %{_builddir}
mv %{source0_name}-%{source0_release} %{source0_name}
mv %{source1_name}-%{source1_release} %{source1_name}
mv %{source2_name}-%{source2_release} %{source2_name}
mv %{source3_name}-%{source3_release} %{source3_name}

mkdir %{_builddir}/libboundscheck-v1.1.11

mv %{_builddir}/yocto-embedded-tools/dsoftbus ${dsoftbus_build_tools}
unzip -d %{_builddir}/gn_aarch/ %{SOURCE4}

# Extract the subpackage contained in the Source
build="build-OpenHarmony-v3.0.2-LTS"
gn="gn-linux-x86-1717"
ninja="ninja-linux-x86-1.10.1"
cJSON="third_party_cJSON-OpenHarmony-v3.1.2-Release"
jinja2="third_party_jinja2-OpenHarmony-v3.1.2-Release"
libcoap="third_party_libcoap-OpenHarmony-v3.1.2-Release"
markupsafe="third_party_markupsafe-OpenHarmony-v3.1.2-Release"
mbedtls="third_party_mbedtls-OpenHarmony-v3.1.2-Release"
openssl="third_party_openssl-OpenHarmony-v3.1.2-Release"
utils="commonlibrary_c_utils-OpenHarmony-v3.1.2-Release"
huks="security_huks-OpenHarmony-v3.1.2-Release"
deviceauth="security_device_auth-OpenHarmony-v3.1.2-Release"


#unpack build
unzip -qd ${dsoftbus_build_dir} ${dsoftbus_build_tools}/build/${build}.zip
mv ${dsoftbus_build_dir}/${build} ${dsoftbus_build_dir}/build

#unpack build_tools
%ifarch x86_64
for i in  $gn $ninja
do
    tar -C ${dsoftbus_prebuilts_build_tools} -zxf ${dsoftbus_build_tools}/build_tools/${i}.tar.gz
done
%endif

%ifarch aarch64
    ninja_dir=$(which ninja)
    cp -rf %{_builddir}/gn_aarch/* ${dsoftbus_prebuilts_build_tools}
    cp ${ninja_dir[0]} ${dsoftbus_prebuilts_build_tools}
%endif

#unpack third_party
for i in  cJSON jinja2 libcoap markupsafe mbedtls openssl
do
pkg=`eval echo '$'"$i"`
    unzip -qd ${dsoftbus_thirdparty} ${dsoftbus_build_tools}/third_party/${i}/${pkg}.zip
    mv ${dsoftbus_thirdparty}/${pkg} ${dsoftbus_thirdparty}/${i}
done

#unpack hichain
for i in  huks deviceauth
do
pkg=`eval echo '$'"$i"`
    unzip -qd ${dsoftbus_hichain} ${dsoftbus_build_tools}/hichain/${i}/${pkg}.zip
    mv ${dsoftbus_hichain}/${pkg} ${dsoftbus_hichain}/${i}
done

#unpack boundcheck
mv %{_builddir}/libboundscheck ${dsoftbus_thirdparty}/bounds_checking_function

#unpack utils
unzip -qd ${dsoftbus_utils} ${dsoftbus_build_tools}/utils/${utils}.zip
mv ${dsoftbus_utils}/${utils} ${dsoftbus_utils}/native

#init_dsoftbus_selfcode
toolchain_path="/usr1/openeuler/gcc/openeuler_gcc_arm64le"
build_patch="0001-add-dsoftbus-build-support-for-embedded-env.patch"
hichain_patch="0002-support-hichian-for-openeuler.patch"
ipc_patch="0003-add-deviceauth-ipc-sdk-compile.patch"
utils_patch="0001-Adaptation-for-dsoftbus.patch"
boundscheck_patch="0001-Adaptation-for-dsoftbus.patch"

mbedtls_patch="0001-Adaptation-for-dsoftbus-v3.1.2.patch"
mbedtls_cve_001_patch="0002-fix-CVE-2021-43666.patch"
mbedtls_cve_002_patch="0003-fix-CVE-2021-45451.patch"
openssl_patch="0001-support-openssl-for-dsoftbus.patch"
huks_patch="0001-support-huks-for-openeuler.patch"
deviceauth_patch_001="0001-support-deviceauth-for-openeuler.patch"
deviceauth_patch_002="0002-adapter-deviceauth-ipc-service.patch"

#init gn root
ln -s ${dsoftbus_build_dir}/build/build_scripts/build.sh ${dsoftbus_build_dir}/build.sh
ln -s ${dsoftbus_build_dir}/build/core/gn/dotfile.gn ${dsoftbus_build_dir}/.gn

#link selfcode
ln -s ${dsoftbus_build_tools}/productdefine ${dsoftbus_build_dir}/productdefine
ln -s ${dsoftbus_build_tools}/depend ${dsoftbus_build_dir}/depend
ln -s %{_builddir}/embedded-ipc ${dsoftbus_build_dir}/depend/ipc
ln -s %{_builddir}/dsoftbus_standard ${dsoftbus_src}/dsoftbus

#link toolchain
ln -s ${toolchain_path} ${dsoftbus_build_dir}/toolchain

#do patch
patch -p1 -d ${dsoftbus_build_dir}/build < ${dsoftbus_build_tools}/build/${build_patch}
patch -p1 -d ${dsoftbus_build_dir}/build < ${dsoftbus_build_tools}/build/${hichain_patch}
patch -p1 -d ${dsoftbus_build_dir}/build < ${dsoftbus_build_tools}/build/${ipc_patch}
patch -p1 -d ${dsoftbus_utils}/native < ${dsoftbus_build_tools}/utils/${utils_patch}
patch -p1 -d ${dsoftbus_thirdparty}/bounds_checking_function < ${dsoftbus_build_tools}/bounds_checking_function/${boundscheck_patch}
patch -p1 -d ${dsoftbus_thirdparty}/mbedtls < ${dsoftbus_build_tools}/third_party/mbedtls/${mbedtls_patch}
patch -p1 -d ${dsoftbus_thirdparty}/mbedtls < ${dsoftbus_build_tools}/third_party/mbedtls/${mbedtls_cve_001_patch}
patch -p1 -d ${dsoftbus_thirdparty}/mbedtls < ${dsoftbus_build_tools}/third_party/mbedtls/${mbedtls_cve_002_patch}
patch -p1 -d ${dsoftbus_thirdparty}/openssl < ${dsoftbus_build_tools}/third_party/openssl/${openssl_patch}
patch -p1 -d ${dsoftbus_hichain}/huks < ${dsoftbus_build_tools}/hichain/huks/${huks_patch}
patch -p1 -d ${dsoftbus_hichain}/deviceauth < ${dsoftbus_build_tools}/hichain/deviceauth/${deviceauth_patch_001}
patch -p1 -d ${dsoftbus_hichain}/deviceauth < ${dsoftbus_build_tools}/hichain/deviceauth/${deviceauth_patch_002}

%patch -p1 -d ${dsoftbus_build_dir}/build

%ifarch x86_64
    sed -i 's/"target_cpu": "arm64"/"target_cpu": "x86_64"/' ${dsoftbus_build_tools}/productdefine/common/device/openEuler.json
%endif

%build
rm -rf %{_builddir}/dsoftbus_build/out
cd %{_builddir}/dsoftbus_build
./build.sh --product-name openEuler

%install
dsoftbus_build_dir="%{_builddir}/dsoftbus_build"
%ifarch x86_64
    dsoftbus_release_dir_name="ohos-x86_64-release"
%endif
%ifarch aarch64
    dsoftbus_release_dir_name="ohos-arm64-release"
%endif

install -d %{buildroot}/%{_includedir}/dsoftbus
install -d %{buildroot}/%{_libdir}
install -d %{buildroot}/%{_bindir}

install -d %{buildroot}/data/data/deviceauth/

# prepare so
install -m 0755 ${dsoftbus_build_dir}/out/${dsoftbus_release_dir_name}/common/common/*.so %{buildroot}/%{_libdir}
install -m 0755 ${dsoftbus_build_dir}/out/${dsoftbus_release_dir_name}/communication/dsoftbus_standard/*.so %{buildroot}/%{_libdir}
install -m 0755 ${dsoftbus_build_dir}/out/${dsoftbus_release_dir_name}/security/huks/*.so %{buildroot}/%{_libdir}
install -m 0755 ${dsoftbus_build_dir}/out/${dsoftbus_release_dir_name}/security/deviceauth_standard/*.so %{buildroot}/%{_libdir}

# prepare bin
install -m 0755 ${dsoftbus_build_dir}/out/${dsoftbus_release_dir_name}/communication/dsoftbus_standard/softbus_server_main %{buildroot}/%{_bindir}

# prepare head files
install -m 554 \
${dsoftbus_build_dir}/foundation/communication/dsoftbus/interfaces/kits/discovery/*.h \
${dsoftbus_build_dir}/foundation/communication/dsoftbus/interfaces/kits/common/*.h \
${dsoftbus_build_dir}/foundation/communication/dsoftbus/interfaces/kits/bus_center/*.h \
${dsoftbus_build_dir}/foundation/communication/dsoftbus/interfaces/kits/transport/*.h \
${dsoftbus_build_dir}/foundation/communication/dsoftbus/core/common/include/softbus_errcode.h \
${dsoftbus_build_dir}/base/security/deviceauth/interfaces/innerkits/*.h \
${dsoftbus_build_dir}/third_party/cJSON/*.h \
${dsoftbus_build_dir}/third_party/bounds_checking_function/include/*.h \
    %{buildroot}/%{_includedir}/dsoftbus

%files
%{_includedir}/dsoftbus/*
%{_bindir}/softbus_server_main
%{_libdir}/*.so
/data/data

%changelog
* Tue Nov 22 2022 xuchongyu <xuchongyu@huawei.com> - 1.0.0-1
- build for openeuler