• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright (c) 2021 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})
18
19if [[ "${@}" =~ "--tool-repo" && -f "${code_dir}/prebuilts.sh" ]]; then
20    # prebuilts.sh should be a symbolic link to a prebuilts_download.sh created by oneself.
21    bash ${code_dir}/prebuilts.sh $@
22else
23while [ $# -gt 0 ]; do
24  case "$1" in
25    -skip-ssl|--skip-ssl) # wget、npm skip ssl check, which will allow
26                          # hacker to get and modify data stream between server and client!
27    SKIP_SSL=YES
28    ;;
29    -h|--help)
30    HELP=YES
31    ;;
32    --disable-rich)       # disable the rich module of python
33    DISABLE_RICH=YES
34    ;;
35    --enable-symlink)     # enable symlink while copying node_modules
36    ENABLE_SYMLINK=YES
37    ;;
38    --build-arkuix)
39    BUILD_ARKUIX=YES
40    ;;
41    --tool-repo)
42    TOOL_REPO="$2"
43    shift
44    ;;
45    --tool-repo=*)
46    TOOL_REPO="${1#--tool-repo=}"
47    ;;
48    --npm-registry)
49    NPM_REGISTRY="$2"
50    shift
51    ;;
52    --npm-registry=*)
53    NPM_REGISTRY="${1#--npm-registry=}"
54    ;;
55    --trusted-host)
56    TRUSTED_HOST="$2"
57    shift
58    ;;
59    --trusted-host=*)
60    TRUSTED_HOST="${1#--trusted-host=}"
61    ;;
62    --pypi-url)           # python package index url
63    PYPI_URL="$2"
64    shift
65    ;;
66    --pypi-url=*)
67    PYPI_URL="${1#--pypi-url=}"
68    ;;
69    *)
70    echo "$0: Warning: unsupported parameter: $1" >&2
71    ;;
72  esac
73  shift
74done
75
76case $(uname -s) in
77    Linux)
78
79        host_platform=linux
80        ;;
81    Darwin)
82        host_platform=darwin
83        ;;
84    *)
85        echo "Unsupported host platform: $(uname -s)"
86        exit 1
87esac
88
89case $(uname -m) in
90    arm64)
91
92        host_cpu=arm64
93        ;;
94    *)
95        host_cpu=x86_64
96esac
97
98if [ "X${SKIP_SSL}" == "XYES" ];then
99    wget_ssl_check="--skip-ssl"
100else
101    wget_ssl_check=''
102fi
103
104if [ "X${HELP}" == "XYES" ];then
105    help="-h"
106else
107    help=''
108fi
109
110if [ "X${ENABLE_SYMLINK}" == "XYES" ];then
111    enable_symlink="--enable-symlink"
112else
113    enable_symlink=''
114fi
115
116if [ ! -z "$TOOL_REPO" ];then
117    tool_repo="--tool-repo $TOOL_REPO"
118else
119    tool_repo=''
120fi
121
122if [ ! -z "$NPM_REGISTRY" ];then
123    npm_registry="--npm-registry $NPM_REGISTRY"
124else
125    npm_registry=''
126fi
127
128if [ ! -z "$TRUSTED_HOST" ];then
129    trusted_host=$TRUSTED_HOST
130elif [ ! -z "$PYPI_URL" ];then
131    trusted_host=${PYPI_URL/#*:\/\//}       # remove prefix part such as http:// https:// etc.
132    trusted_host=${trusted_host/%[:\/]*/}   # remove suffix part including the port number
133else
134    trusted_host='repo.huaweicloud.com'
135fi
136
137if [ ! -z "$PYPI_URL" ];then
138    pypi_url=$PYPI_URL
139else
140    pypi_url='http://repo.huaweicloud.com/repository/pypi/simple'
141fi
142
143if [ $UID -ne 0 ]; then
144    npm_para=''
145else
146    npm_para='--unsafe-perm'
147fi
148
149if [ "X${BUILD_ARKUIX}" == "XYES" ];then
150    build_arkuix="--build-arkuix"
151else
152    build_arkuix=''
153fi
154
155if [ "X${DISABLE_RICH}" == "XYES" ];then
156  disable_rich='--disable-rich'
157else
158  set +e
159  pip3 install --trusted-host $trusted_host -i $pypi_url rich;
160  if [ $? -eq 0 ];then
161      echo "rich installed successfully"
162  else
163      disable_rich='--disable-rich'
164  fi
165  set -e
166fi
167
168cpu="--host-cpu $host_cpu"
169platform="--host-platform $host_platform"
170echo "prebuilts_download start"
171if [ -d "${code_dir}/prebuilts/build-tools/common/nodejs" ];then
172    rm -rf "${code_dir}/prebuilts/build-tools/common/nodejs"
173    echo "remove nodejs"
174fi
175python3 "${code_dir}/build/prebuilts_download.py" $wget_ssl_check $tool_repo $npm_registry $help $cpu $platform $npm_para $disable_rich $enable_symlink $build_arkuix
176echo "prebuilts_download end"
177
178if [[ "${host_platform}" == "linux" ]]; then
179    sed -i "1s%.*%#!/usr/bin/env python3%" ${code_dir}/prebuilts/python/${host_platform}-x86/3.10.2/bin/pip3.10
180elif [[ "${host_platform}" == "darwin" ]]; then
181    sed -i "" "1s%.*%#!/use/bin/env python3%" ${code_dir}/prebuilts/python/${host_platform}-x86/3.10.2/bin/pip3.10
182fi
183prebuild_python3_path="$code_dir/prebuilts/python/${host_platform}-x86/3.10.2/bin/python3.10"
184prebuild_pip3_path="${code_dir}/prebuilts/python/${host_platform}-x86/3.10.2/bin/pip3.10"
185$prebuild_python3_path $prebuild_pip3_path install --trusted-host $trusted_host -i $pypi_url pyyaml requests prompt_toolkit\=\=1.0.14 kconfiglib\>\=14.1.0 asn1crypto cryptography json5\=\=0.9.6
186
187# llvm_ndk is merged form llvm and libcxx-ndk for compiling the native of hap
188llvm_dir="${code_dir}/prebuilts/clang/ohos/linux-x86_64"
189llvm_dir_win="${code_dir}/prebuilts/clang/ohos/windows-x86_64"
190llvm_dir_mac_x86="${code_dir}/prebuilts/clang/ohos/darwin-x86_64"
191llvm_dir_mac_arm64="${code_dir}/prebuilts/clang/ohos/darwin-arm64"
192llvm_dir_list=($llvm_dir $llvm_dir_win $llvm_dir_mac_x86 $llvm_dir_mac_arm64)
193
194function create_executable() {
195    exe_dir=$1
196    exe_name=$2
197    exe_path=$exe_dir/$exe_name
198
199    if [[ ! -d "$exe_dir" ]]; then
200        echo "Error: directory '$exe_dir' does not exist while creating $exe_name"
201        return 1
202    fi
203
204    if [[ ! -e "$exe_path" ]]; then
205        touch $exe_path
206        chmod 755 $exe_path
207        echo "Created $exe_path"
208    else
209        echo "Warning: '$exe_path' already exists, will not create it"
210    fi
211}
212
213function create_lldb_mi() {
214    if [[ "$host_platform" == "linux" ]]; then
215        create_executable $llvm_dir/llvm/bin "lldb-mi"
216        create_executable $llvm_dir_win/llvm/bin "lldb-mi.exe"
217    elif [[ "$host_platform" == "darwin" ]]; then
218        if [[ "$host_cpu" == "arm64" ]]; then
219            create_executable $llvm_dir_mac_arm64/llvm/bin "lldb-mi"
220        elif [[ "$host_cpu" == "x86_64" ]]; then
221            create_executable $llvm_dir_mac_x86/llvm/bin "lldb-mi"
222        else
223            echo "Error: unrecognized CPU '$host_cpu' for Darwin"
224            return 1
225        fi
226    else
227        echo "Error: unsupported host platform '$host_platform'"
228    fi
229}
230
231# copy libcxx-ndk library outside c++
232function copy_inside_cxx(){
233for i in ${llvm_dir_list[@]}
234do
235    libcxx_dir="${i}/libcxx-ndk/lib"
236    if [[ -d "${i}/libcxx-ndk" ]]; then
237        for file in `ls ${libcxx_dir}`
238        do
239            if [ ! -d "${libcxx_dir}/${file}/c++" ];then
240                `mkdir -p ${libcxx_dir}/c++`
241                `cp -r ${libcxx_dir}/${file}/* ${libcxx_dir}/c++`
242                `mv ${libcxx_dir}/c++ ${libcxx_dir}/${file}/c++`
243            fi
244        done
245    fi
246done
247}
248
249function update_llvm_ndk(){
250if [[ -e "${llvm_dir}/llvm_ndk" ]];then
251  rm -rf "${llvm_dir}/llvm_ndk"
252fi
253mkdir -p "${llvm_dir}/llvm_ndk"
254cp -af "${llvm_dir}/llvm/include" "${llvm_dir}/llvm_ndk"
255cp -rfp "${llvm_dir}/libcxx-ndk/include" "${llvm_dir}/llvm_ndk"
256}
257
258function change_rustlib_name(){
259rust_dir="${code_dir}/prebuilts/rustc/linux-x86_64/current/lib/rustlib/"
260for file in `find $rust_dir -path $rust_dir/x86_64-unknown-linux-gnu -prune -o -name "lib*.*"`
261do
262    dir_name=${file%/*}
263    file_name=${file##*/}
264    file_prefix=`echo $file_name | awk '{split($1, arr, "."); print arr[1]}'`
265    file_prefix=`echo $file_prefix | awk '{split($1, arr, "-"); print arr[1]}'`
266    file_suffix=`echo $file_name | awk '{split($1, arr, "."); print arr[2]}'`
267    if [[ $file_suffix != "rlib" && $file_suffix != "so" || $file_prefix == "librustc_demangle" || $file_prefix == "libcfg_if" || $file_prefix == "libunwind" ]]
268    then
269        continue
270    fi
271    if [[ $file_suffix == "rlib" ]]
272    then
273        if [[ $file_prefix == "libstd" || $file_prefix == "libtest" ]]
274        then
275            newfile_name="$file_prefix.dylib.rlib"
276        else
277            newfile_name="$file_prefix.rlib"
278        fi
279    fi
280
281    if [[ $file_suffix == "so" ]]
282    then
283        newfile_name="$file_prefix.dylib.so"
284    fi
285    if [[ "$file_name" == "$newfile_name" ]]
286    then
287        continue
288    fi
289    mv $file "$dir_name/$newfile_name"
290done
291}
292
293if [[ "${BUILD_ARKUIX}" != "YES" ]]; then
294    copy_inside_cxx
295    echo "======copy inside cxx finished!======"
296    if [[ "${host_platform}" == "linux" ]]; then
297        update_llvm_ndk
298        echo "======update llvm ndk finished!======"
299        change_rustlib_name
300        echo "======change rustlib name finished!======"
301    fi
302    create_lldb_mi
303fi
304echo -e "\n"
305fi
306