• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash -e
2# Copyright (c) 2021-2022 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.
14
15MSG_PREFIX="[ARK THIRD PARTY]"
16SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
17ARK_ROOT=$SCRIPT_DIR/..
18ARK_THIRD_PARTY_DIR=$ARK_ROOT/third_party
19ARK_THIRD_PARTY_FORCE_CLONE=no
20ARK_INSTALL_NODE=no
21NUM_CLONE_ATTEMPTS=5
22
23MIRROR_ARK_THIRD_PARTY_MANIFEST="$SCRIPT_DIR/extras/third-party-lists/mirror"
24PUBLIC_ARK_THIRD_PARTY_MANIFEST="$SCRIPT_DIR/third-party-lists/public"
25if [ -f "${MIRROR_ARK_THIRD_PARTY_MANIFEST}" ]; then
26    ARK_THIRD_PARTY_MANIFEST="${MIRROR_ARK_THIRD_PARTY_MANIFEST}"
27else
28    ARK_THIRD_PARTY_MANIFEST="${PUBLIC_ARK_THIRD_PARTY_MANIFEST}"
29fi
30
31function print_help
32{
33    HELP_MESSAGE="
34    This script installs build-time third party dependencies for ARK Runtime.
35
36    SYNOPSIS
37
38    $0 [OPTIONS]
39
40    OPTIONS
41
42    --help, -h         Show this message and exit.
43
44    --manifest=FILE    Path to the manifest. Default: $ARK_THIRD_PARTY_MANIFEST
45
46    --force-clone      Remove local repositories and re-clone everything anew
47
48    --clone-attempts=N Try to clone N times. Default: $NUM_CLONE_ATTEMPTS
49
50    --node             Install node js to third-party
51
52    CAVEAT
53    * Manifest file format is documented in the default manifest.
54    "
55
56    echo "$HELP_MESSAGE"
57}
58
59for opt in "$@"
60do
61    case $opt in
62    -h|--help)
63        print_help
64        exit 0
65        ;;
66    --manifest=*)
67        ARK_THIRD_PARTY_MANIFEST=${opt//[-a-zA-Z0-9]*=/}
68        ;;
69    --force-clone)
70        ARK_THIRD_PARTY_FORCE_CLONE=yes
71        ;;
72    --clone-attempts=*)
73        NUM_CLONE_ATTEMPTS=${opt//[-a-zA-Z0-9]*=/}
74        ;;
75    --node)
76        ARK_INSTALL_NODE=yes
77        ;;
78    --repo-name=*)
79        REPO_NAME="${opt#*=}"
80        ;;
81    *)
82      echo "Error: Unsupported flag $opt" >&2
83      exit 1
84      ;;
85  esac
86done
87
88function clone_repository_branch
89{
90    local lib_repo="$1"
91    local lib_dir="$2"
92    local commit_id="$3"  # branch or tag name, the syntax is the same
93
94    for i in `seq 1 $NUM_CLONE_ATTEMPTS`
95    do
96        echo "$MSG_PREFIX Cloning $lib_dir, attempt $i / $NUM_CLONE_ATTEMPTS"
97
98        GIT_SSL_NO_VERIFY=true git clone --verbose --depth=1 --branch "$commit_id" \
99            "$lib_repo" "$lib_dir" || continue
100
101        return
102    done
103
104    echo "$MSG_PREFIX Failed to clone $lib_dir"
105    exit 1
106}
107
108function clone_repository
109{
110    local lib_repo="$1"
111    local lib_dir="$2"
112
113    for i in `seq 1 $NUM_CLONE_ATTEMPTS`
114    do
115        echo "$MSG_PREFIX Cloning $lib_dir, attempt $i / $NUM_CLONE_ATTEMPTS"
116        GIT_SSL_NO_VERIFY=true git clone --verbose "$lib_repo" "$lib_dir" || continue
117
118        pushd "$lib_dir"
119            git checkout "$commit_id"
120        popd
121        return
122    done
123
124    echo "$MSG_PREFIX Failed to clone $lib_dir"
125    exit 1
126}
127
128function apply_patches
129{
130    local lib_name="$1"
131    local lib_dir="$2"
132
133    local patch_dir="$ARK_ROOT/patches/$lib_name"
134    if [[ ! -d "$patch_dir" ]] ; then
135        echo "$MSG_PREFIX No patch directory $patch_dir for $lib_name"
136        exit 1
137    fi
138
139    pushd "$lib_dir"
140        readarray -t patches <<< "$(find "$patch_dir" -name '*.patch' | sort)"
141        for patch in "${patches[@]}"
142        do
143            git apply --ignore-space-change --check "$patch"
144            git am --ignore-space-change "$patch"
145        done
146    popd
147}
148
149function process_manifest
150{
151    local manifest="$1"
152
153    while read -r component
154    do
155        component=$(echo "$component" | perl -lane 'chomp; s/^\s+//; s/\s+$//; print $_')
156
157        if [[ "$component" == "" || "$component" =~ ^# ]] ; then
158            continue
159        fi
160
161        IFS=',' read -r -a info <<< "$component"
162        local lib_name="${info[0]}"
163        local lib_repo="${info[1]}"
164        local commit_type="${info[2]}"
165        local commit_id="${info[3]}"
166        local patch_mode="${info[4]}"
167        local submodule_mode="${info[5]}"
168        if [[ -n "$REPO_NAME" && "$REPO_NAME" != "$lib_name" ]] ; then
169            continue
170        fi
171
172        local lib_dir="$ARK_THIRD_PARTY_DIR/$lib_name"
173
174        if [[ -d "$lib_dir" && -z "$REPO_NAME" ]] ; then
175            echo "$MSG_PREFIX $lib_dir already exists. Skip download."
176            continue
177        fi;
178
179        if [[ "$commit_type" == "branch" || "$commit_type" == "tag" ]] ; then
180            clone_repository_branch "$lib_repo" "$lib_dir" "$commit_id"
181        elif [[ "$commit_type" == "commit" ]] ; then
182            clone_repository "$lib_repo" "$lib_dir"
183        else
184            echo "$MSG_PREFIX Invalid commit type for $lib_name: $commit_type"
185            exit 1
186        fi
187
188        if [[ "$patch_mode" == "with_patches" ]] ; then
189            apply_patches "$lib_name" "$lib_dir"
190        elif [[ "$patch_mode" != "no_patches" ]] ; then
191            echo "$MSG_PREFIX Invalid patch mode for $lib_name: $patch_mode"
192            exit 1
193        fi
194
195        if [[ "$submodule_mode" =~ ^with_submodules: ]] ; then
196            # Split by delimiter and remove "with_submodules" keyword:
197            IFS=':' read -r -a submodules <<< "$submodule_mode"
198            submodules=("${submodules[@]:1}")
199            pushd "$lib_dir"
200                for submodule in "${submodules[@]}"
201                do
202                    git submodule update --init "$submodule"
203                done
204            popd
205        elif [[ "$submodule_mode" != "no_submodules" ]] ; then
206            echo "$MSG_PREFIX Invalid submodule mode for $lib_name: $submodule_mode"
207            exit 1
208        fi
209
210    done < "$manifest"
211}
212
213if [[ "$ARK_INSTALL_NODE" == "yes" ]] ; then
214    NODE_VERSION="v18.13.0"
215    DISTRO="linux-x64"
216    if [ -f "$SCRIPT_DIR/extras/install_nodejs.sh" ]; then
217        bash $SCRIPT_DIR/extras/install_nodejs.sh --node-version=${NODE_VERSION} --distro=${DISTRO}
218    else
219        if [ ! -d "${ARK_THIRD_PARTY_DIR}/nodejs" ]
220        then
221            echo "-- Downloading nodejs for interop tests"
222            ARCHIVE_NAME=node-${NODE_VERSION}-${DISTRO}.tar.xz
223            wget -q -P ${ARK_THIRD_PARTY_DIR}/nodejs https://nodejs.org/dist/${NODE_VERSION}/${ARCHIVE_NAME}
224            tar -xJf ${ARK_THIRD_PARTY_DIR}/nodejs/${ARCHIVE_NAME} -C ${ARK_THIRD_PARTY_DIR}/nodejs
225            rm ${ARK_THIRD_PARTY_DIR}/nodejs/${ARCHIVE_NAME}
226        fi
227    fi
228
229    exit 0
230fi
231
232if [[ "$ARK_THIRD_PARTY_FORCE_CLONE" == "yes" ]] ; then
233    rm -rf "$ARK_THIRD_PARTY_DIR"
234fi
235
236if [[ -d "$ARK_THIRD_PARTY_DIR" && -z "$REPO_NAME" ]] ; then
237    echo "$MSG_PREFIX Third-party dependencies are found in $ARK_THIRD_PARTY_DIR"
238    echo "$MSG_PREFIX If you need to update all, restart this script with --force-clone"
239fi
240
241if [[ -n "$REPO_NAME" ]] ; then
242    rm -rf "$ARK_THIRD_PARTY_DIR/$REPO_NAME"
243
244fi
245
246echo "$MSG_PREFIX Third-party dependencies are not found in $ARK_THIRD_PARTY_DIR"
247
248if [[ ! -f "$ARK_THIRD_PARTY_MANIFEST" ]] ; then
249    echo "$MSG_PREFIX Invalid manifest file '$ARK_THIRD_PARTY_MANIFEST'"
250    exit 1
251fi
252
253echo "$MSG_PREFIX Using manifest $ARK_THIRD_PARTY_MANIFEST"
254process_manifest "$ARK_THIRD_PARTY_MANIFEST"
255
256echo "$MSG_PREFIX Third-party dependencies installed"
257exit 0
258
259