1b1994897Sopenharmony_ci#!/bin/bash -e
2b1994897Sopenharmony_ci# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3b1994897Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
4b1994897Sopenharmony_ci# you may not use this file except in compliance with the License.
5b1994897Sopenharmony_ci# You may obtain a copy of the License at
6b1994897Sopenharmony_ci#
7b1994897Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
8b1994897Sopenharmony_ci#
9b1994897Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
10b1994897Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
11b1994897Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1994897Sopenharmony_ci# See the License for the specific language governing permissions and
13b1994897Sopenharmony_ci# limitations under the License.
14b1994897Sopenharmony_ci
15b1994897Sopenharmony_ciMSG_PREFIX="[ARK THIRD PARTY]"
16b1994897Sopenharmony_ciSCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
17b1994897Sopenharmony_ciARK_ROOT=$SCRIPT_DIR/..
18b1994897Sopenharmony_ciARK_THIRD_PARTY_DIR=$ARK_ROOT/ark-third-party
19b1994897Sopenharmony_ciARK_THIRD_PARTY_FORCE_CLONE=no
20b1994897Sopenharmony_ciARK_THIRD_PARTY_MANIFEST="$SCRIPT_DIR/third-party-lists/public"
21b1994897Sopenharmony_ci
22b1994897Sopenharmony_cifunction print_help
23b1994897Sopenharmony_ci{
24b1994897Sopenharmony_ci    HELP_MESSAGE="
25b1994897Sopenharmony_ci    This script installs build-time third party dependencies for ARK Runtime.
26b1994897Sopenharmony_ci
27b1994897Sopenharmony_ci    SYNOPSIS
28b1994897Sopenharmony_ci
29b1994897Sopenharmony_ci    $0 [OPTIONS]
30b1994897Sopenharmony_ci
31b1994897Sopenharmony_ci    OPTIONS
32b1994897Sopenharmony_ci
33b1994897Sopenharmony_ci    --help, -h         Show this message and exit.
34b1994897Sopenharmony_ci
35b1994897Sopenharmony_ci    --manifest=FILE    Path to the manifest. Default: $ARK_THIRD_PARTY_MANIFEST
36b1994897Sopenharmony_ci
37b1994897Sopenharmony_ci    --force-clone      Remove local repositories and re-clone everything anew
38b1994897Sopenharmony_ci
39b1994897Sopenharmony_ci    CAVEAT
40b1994897Sopenharmony_ci    * Manifest file format is documented in the default manifest.
41b1994897Sopenharmony_ci    "
42b1994897Sopenharmony_ci
43b1994897Sopenharmony_ci    echo "$HELP_MESSAGE"
44b1994897Sopenharmony_ci}
45b1994897Sopenharmony_ci
46b1994897Sopenharmony_cifor opt in "$@"
47b1994897Sopenharmony_cido
48b1994897Sopenharmony_ci    case $opt in
49b1994897Sopenharmony_ci    -h|--help)
50b1994897Sopenharmony_ci        print_help
51b1994897Sopenharmony_ci        exit 0
52b1994897Sopenharmony_ci        ;;
53b1994897Sopenharmony_ci    --manifest=*)
54b1994897Sopenharmony_ci        ARK_THIRD_PARTY_MANIFEST=${opt//[-a-zA-Z0-9]*=/}
55b1994897Sopenharmony_ci        ;;
56b1994897Sopenharmony_ci    --force-clone)
57b1994897Sopenharmony_ci        ARK_THIRD_PARTY_FORCE_CLONE=yes
58b1994897Sopenharmony_ci        ;;
59b1994897Sopenharmony_ci    *)
60b1994897Sopenharmony_ci      echo "Error: Unsupported flag $opt" >&2
61b1994897Sopenharmony_ci      exit 1
62b1994897Sopenharmony_ci      ;;
63b1994897Sopenharmony_ci  esac
64b1994897Sopenharmony_cidone
65b1994897Sopenharmony_ci
66b1994897Sopenharmony_cifunction apply_patches
67b1994897Sopenharmony_ci{
68b1994897Sopenharmony_ci    local lib_name="$1"
69b1994897Sopenharmony_ci    local lib_dir="$2"
70b1994897Sopenharmony_ci
71b1994897Sopenharmony_ci    local patch_dir="$ARK_ROOT/patches/$lib_name"
72b1994897Sopenharmony_ci    if [[ ! -d "$patch_dir" ]] ; then
73b1994897Sopenharmony_ci        echo "$MSG_PREFIX No patch directory $patch_dir for $lib_name"
74b1994897Sopenharmony_ci        exit 1
75b1994897Sopenharmony_ci    fi
76b1994897Sopenharmony_ci
77b1994897Sopenharmony_ci    pushd "$lib_dir"
78b1994897Sopenharmony_ci        readarray -t patches <<< "$(find "$patch_dir" -name '*.patch' | sort)"
79b1994897Sopenharmony_ci        for patch in "${patches[@]}"
80b1994897Sopenharmony_ci        do
81b1994897Sopenharmony_ci            git apply --ignore-space-change --check "$patch"
82b1994897Sopenharmony_ci            git am --ignore-space-change "$patch"
83b1994897Sopenharmony_ci        done
84b1994897Sopenharmony_ci    popd
85b1994897Sopenharmony_ci}
86b1994897Sopenharmony_ci
87b1994897Sopenharmony_cifunction process_manifest
88b1994897Sopenharmony_ci{
89b1994897Sopenharmony_ci    local manifest="$1"
90b1994897Sopenharmony_ci
91b1994897Sopenharmony_ci    while read -r component
92b1994897Sopenharmony_ci    do
93b1994897Sopenharmony_ci        component=$(echo "$component" | perl -lane 'chomp; s/^\s+//; s/\s+$//; print $_')
94b1994897Sopenharmony_ci
95b1994897Sopenharmony_ci        if [[ "$component" == "" || "$component" =~ ^# ]] ; then
96b1994897Sopenharmony_ci            continue
97b1994897Sopenharmony_ci        fi
98b1994897Sopenharmony_ci
99b1994897Sopenharmony_ci        IFS=',' read -r -a info <<< "$component"
100b1994897Sopenharmony_ci        local lib_name="${info[0]}"
101b1994897Sopenharmony_ci        local lib_repo="${info[1]}"
102b1994897Sopenharmony_ci        local commit_type="${info[2]}"
103b1994897Sopenharmony_ci        local commit_id="${info[3]}"
104b1994897Sopenharmony_ci        local patch_mode="${info[4]}"
105b1994897Sopenharmony_ci        local submodule_mode="${info[5]}"
106b1994897Sopenharmony_ci
107b1994897Sopenharmony_ci        local lib_dir="$ARK_THIRD_PARTY_DIR/$lib_name"
108b1994897Sopenharmony_ci
109b1994897Sopenharmony_ci        if [[ "$commit_type" == "branch" || "$commit_type" == "tag" ]] ; then
110b1994897Sopenharmony_ci            GIT_SSL_NO_VERIFY=true git clone --verbose --depth=1 --branch "$commit_id" "$lib_repo" "$lib_dir"
111b1994897Sopenharmony_ci        elif [[ "$commit_type" == "commit" ]] ; then
112b1994897Sopenharmony_ci            GIT_SSL_NO_VERIFY=true git clone --verbose "$lib_repo" "$lib_dir"
113b1994897Sopenharmony_ci            pushd "$lib_dir"
114b1994897Sopenharmony_ci                git checkout "$commit_id"
115b1994897Sopenharmony_ci            popd
116b1994897Sopenharmony_ci        else
117b1994897Sopenharmony_ci            echo "$MSG_PREFIX Invalid commit type for $lib_name: $commit_type"
118b1994897Sopenharmony_ci            exit 1
119b1994897Sopenharmony_ci        fi
120b1994897Sopenharmony_ci
121b1994897Sopenharmony_ci        if [[ "$patch_mode" == "with_patches" ]] ; then
122b1994897Sopenharmony_ci            apply_patches "$lib_name" "$lib_dir"
123b1994897Sopenharmony_ci        elif [[ "$patch_mode" != "no_patches" ]] ; then
124b1994897Sopenharmony_ci            echo "$MSG_PREFIX Invalid patch mode for $lib_name: $patch_mode"
125b1994897Sopenharmony_ci            exit 1
126b1994897Sopenharmony_ci        fi
127b1994897Sopenharmony_ci
128b1994897Sopenharmony_ci        if [[ "$submodule_mode" =~ ^with_submodules: ]] ; then
129b1994897Sopenharmony_ci            # Split by delimiter and remove "with_submodules" keyword:
130b1994897Sopenharmony_ci            IFS=':' read -r -a submodules <<< "$submodule_mode"
131b1994897Sopenharmony_ci            submodules=("${submodules[@]:1}")
132b1994897Sopenharmony_ci            pushd "$lib_dir"
133b1994897Sopenharmony_ci                for submodule in "${submodules[@]}"
134b1994897Sopenharmony_ci                do
135b1994897Sopenharmony_ci                    git submodule update --init "$submodule"
136b1994897Sopenharmony_ci                done
137b1994897Sopenharmony_ci            popd
138b1994897Sopenharmony_ci        elif [[ "$submodule_mode" != "no_submodules" ]] ; then
139b1994897Sopenharmony_ci            echo "$MSG_PREFIX Invalid submodule mode for $lib_name: $submodule_mode"
140b1994897Sopenharmony_ci            exit 1
141b1994897Sopenharmony_ci        fi
142b1994897Sopenharmony_ci
143b1994897Sopenharmony_ci    done < "$manifest"
144b1994897Sopenharmony_ci}
145b1994897Sopenharmony_ci
146b1994897Sopenharmony_ciif [[ "$ARK_THIRD_PARTY_FORCE_CLONE" == "yes" ]] ; then
147b1994897Sopenharmony_ci    rm -rf "$ARK_THIRD_PARTY_DIR"
148b1994897Sopenharmony_cifi
149b1994897Sopenharmony_ci
150b1994897Sopenharmony_ciif [[ -d "$ARK_THIRD_PARTY_DIR" ]] ; then
151b1994897Sopenharmony_ci    echo "$MSG_PREFIX Third-party dependencies are found in $ARK_THIRD_PARTY_DIR, exiting"
152b1994897Sopenharmony_ci    echo "$MSG_PREFIX If you need to update, restart this script with --force-clone"
153b1994897Sopenharmony_ci    exit 0
154b1994897Sopenharmony_cifi
155b1994897Sopenharmony_ci
156b1994897Sopenharmony_ciecho "$MSG_PREFIX Third-party dependencies are not found in $ARK_THIRD_PARTY_DIR"
157b1994897Sopenharmony_ci
158b1994897Sopenharmony_ciif [[ ! -f "$ARK_THIRD_PARTY_MANIFEST" ]] ; then
159b1994897Sopenharmony_ci    echo "$MSG_PREFIX Invalid manifest file '$ARK_THIRD_PARTY_MANIFEST'"
160b1994897Sopenharmony_ci    exit 1
161b1994897Sopenharmony_cifi
162b1994897Sopenharmony_ci
163b1994897Sopenharmony_ciecho "$MSG_PREFIX Using manifest $ARK_THIRD_PARTY_MANIFEST"
164b1994897Sopenharmony_ciprocess_manifest "$ARK_THIRD_PARTY_MANIFEST"
165b1994897Sopenharmony_ci
166b1994897Sopenharmony_ciecho "$MSG_PREFIX Third-party dependencies installed"
167b1994897Sopenharmony_ciexit 0
168b1994897Sopenharmony_ci
169