1#!/bin/bash
2# Copyright (c) 2024 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14set -e
15
16script_path=$(cd $(dirname $0);pwd)
17code_dir=$(dirname ${script_path})
18home_path=$HOME
19config_file="$script_path/prebuilts_config.json"
20# 获取当前系统类型和CPU类型
21target_os=$(uname -s | tr '[:upper:]' '[:lower:]')
22target_cpu=$(uname -m | tr '[:upper:]' '[:lower:]')
23
24# 运行Python命令
25python3 "${script_path}/prebuilts_config.py" --code_path $code_dir --home_path $home_path --config_file $config_file --repo_https https://repo.huaweicloud.com --target_os $target_os --target_cpu $target_cpu
26
27PYTHON_PATH=$(realpath ${code_dir}/prebuilts/python/${target_os}-*/*/bin | tail -1)
28
29if [[ "${target_os}" == "linux" ]]; then
30    sed -i "1s%.*%#!/usr/bin/env python3%" "${PYTHON_PATH}/pip3"
31elif [[ "${target_os}" == "darwin" ]]; then
32    sed -i "" "1s%.*%#!/usr/bin/env python3%" "${PYTHON_PATH}/pip3"
33fi
34
35# llvm_ndk is merged form llvm and libcxx-ndk for compiling the native of hap
36llvm_dir="${code_dir}/prebuilts/clang/ohos/linux-x86_64"
37llvm_dir_win="${code_dir}/prebuilts/clang/ohos/windows-x86_64"
38llvm_dir_mac_x86="${code_dir}/prebuilts/clang/ohos/darwin-x86_64"
39llvm_dir_mac_arm64="${code_dir}/prebuilts/clang/ohos/darwin-arm64"
40llvm_dir_list=($llvm_dir $llvm_dir_win $llvm_dir_mac_x86 $llvm_dir_mac_arm64)
41
42# copy libcxx-ndk library outside c++
43function copy_inside_cxx(){
44for i in ${llvm_dir_list[@]}
45do
46    libcxx_dir="${i}/libcxx-ndk/lib"
47    if [[ -d "${i}/libcxx-ndk" ]]; then
48        for file in $(ls ${libcxx_dir})
49        do
50            if [ ! -d "${libcxx_dir}/${file}/c++" ];then
51                $(mkdir -p ${libcxx_dir}/c++)
52                $(cp -r ${libcxx_dir}/${file}/* ${libcxx_dir}/c++)
53                $(mv ${libcxx_dir}/c++ ${libcxx_dir}/${file}/c++)
54            fi
55        done
56    fi
57done
58}
59
60function update_llvm_ndk(){
61if [[ -e "${llvm_dir}/llvm_ndk" ]];then
62  rm -rf "${llvm_dir}/llvm_ndk"
63fi
64mkdir -p "${llvm_dir}/llvm_ndk"
65cp -af "${llvm_dir}/llvm/include" "${llvm_dir}/llvm_ndk"
66cp -rfp "${llvm_dir}/libcxx-ndk/include" "${llvm_dir}/llvm_ndk"
67}
68
69function change_rustlib_name(){
70rust_dir="${code_dir}/prebuilts/rustc/linux-x86_64/current/lib/rustlib/"
71for file in $(find "$rust_dir" -path "$rust_dir/x86_64-unknown-linux-gnu" -prune -o -name "lib*.*")
72do
73    dir_name=${file%/*}
74    file_name=${file##*/}
75    file_prefix=$(echo "$file_name" | awk '{split($1, arr, "."); print arr[1]}')
76    file_prefix=$(echo "$file_prefix" | awk '{split($1, arr, "-"); print arr[1]}')
77    file_suffix=$(echo "$file_name" | awk '{split($1, arr, "."); print arr[2]}')
78    if [[ "$file_suffix" != "rlib" && "$file_suffix" != "so" || "$file_prefix" == "librustc_demangle" || "$file_prefix" == "libcfg_if" || "$file_prefix" == "libunwind" ]]
79    then
80        continue
81    fi
82    if [[ "$file_suffix" == "rlib" ]]
83    then
84        if [[ "$file_prefix" == "libstd" || "$file_prefix" == "libtest" ]]
85        then
86            newfile_name="$file_prefix.dylib.rlib"
87        else
88            newfile_name="$file_prefix.rlib"
89        fi
90    fi
91
92    if [[ "$file_suffix" == "so" ]]
93    then
94        newfile_name="$file_prefix.dylib.so"
95    fi
96    if [[ "$file_name" == "$newfile_name" ]]
97    then
98        continue
99    fi
100    mv $file "$dir_name/$newfile_name"
101done
102}
103
104copy_inside_cxx
105echo "======copy inside cxx finished!======"
106