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/ark-third-party 19ARK_THIRD_PARTY_FORCE_CLONE=no 20ARK_THIRD_PARTY_MANIFEST="$SCRIPT_DIR/third-party-lists/public" 21 22function print_help 23{ 24 HELP_MESSAGE=" 25 This script installs build-time third party dependencies for ARK Runtime. 26 27 SYNOPSIS 28 29 $0 [OPTIONS] 30 31 OPTIONS 32 33 --help, -h Show this message and exit. 34 35 --manifest=FILE Path to the manifest. Default: $ARK_THIRD_PARTY_MANIFEST 36 37 --force-clone Remove local repositories and re-clone everything anew 38 39 CAVEAT 40 * Manifest file format is documented in the default manifest. 41 " 42 43 echo "$HELP_MESSAGE" 44} 45 46for opt in "$@" 47do 48 case $opt in 49 -h|--help) 50 print_help 51 exit 0 52 ;; 53 --manifest=*) 54 ARK_THIRD_PARTY_MANIFEST=${opt//[-a-zA-Z0-9]*=/} 55 ;; 56 --force-clone) 57 ARK_THIRD_PARTY_FORCE_CLONE=yes 58 ;; 59 *) 60 echo "Error: Unsupported flag $opt" >&2 61 exit 1 62 ;; 63 esac 64done 65 66function apply_patches 67{ 68 local lib_name="$1" 69 local lib_dir="$2" 70 71 local patch_dir="$ARK_ROOT/patches/$lib_name" 72 if [[ ! -d "$patch_dir" ]] ; then 73 echo "$MSG_PREFIX No patch directory $patch_dir for $lib_name" 74 exit 1 75 fi 76 77 pushd "$lib_dir" 78 readarray -t patches <<< "$(find "$patch_dir" -name '*.patch' | sort)" 79 for patch in "${patches[@]}" 80 do 81 git apply --ignore-space-change --check "$patch" 82 git am --ignore-space-change "$patch" 83 done 84 popd 85} 86 87function process_manifest 88{ 89 local manifest="$1" 90 91 while read -r component 92 do 93 component=$(echo "$component" | perl -lane 'chomp; s/^\s+//; s/\s+$//; print $_') 94 95 if [[ "$component" == "" || "$component" =~ ^# ]] ; then 96 continue 97 fi 98 99 IFS=',' read -r -a info <<< "$component" 100 local lib_name="${info[0]}" 101 local lib_repo="${info[1]}" 102 local commit_type="${info[2]}" 103 local commit_id="${info[3]}" 104 local patch_mode="${info[4]}" 105 local submodule_mode="${info[5]}" 106 107 local lib_dir="$ARK_THIRD_PARTY_DIR/$lib_name" 108 109 if [[ "$commit_type" == "branch" || "$commit_type" == "tag" ]] ; then 110 GIT_SSL_NO_VERIFY=true git clone --verbose --depth=1 --branch "$commit_id" "$lib_repo" "$lib_dir" 111 elif [[ "$commit_type" == "commit" ]] ; then 112 GIT_SSL_NO_VERIFY=true git clone --verbose "$lib_repo" "$lib_dir" 113 pushd "$lib_dir" 114 git checkout "$commit_id" 115 popd 116 else 117 echo "$MSG_PREFIX Invalid commit type for $lib_name: $commit_type" 118 exit 1 119 fi 120 121 if [[ "$patch_mode" == "with_patches" ]] ; then 122 apply_patches "$lib_name" "$lib_dir" 123 elif [[ "$patch_mode" != "no_patches" ]] ; then 124 echo "$MSG_PREFIX Invalid patch mode for $lib_name: $patch_mode" 125 exit 1 126 fi 127 128 if [[ "$submodule_mode" =~ ^with_submodules: ]] ; then 129 # Split by delimiter and remove "with_submodules" keyword: 130 IFS=':' read -r -a submodules <<< "$submodule_mode" 131 submodules=("${submodules[@]:1}") 132 pushd "$lib_dir" 133 for submodule in "${submodules[@]}" 134 do 135 git submodule update --init "$submodule" 136 done 137 popd 138 elif [[ "$submodule_mode" != "no_submodules" ]] ; then 139 echo "$MSG_PREFIX Invalid submodule mode for $lib_name: $submodule_mode" 140 exit 1 141 fi 142 143 done < "$manifest" 144} 145 146if [[ "$ARK_THIRD_PARTY_FORCE_CLONE" == "yes" ]] ; then 147 rm -rf "$ARK_THIRD_PARTY_DIR" 148fi 149 150if [[ -d "$ARK_THIRD_PARTY_DIR" ]] ; then 151 echo "$MSG_PREFIX Third-party dependencies are found in $ARK_THIRD_PARTY_DIR, exiting" 152 echo "$MSG_PREFIX If you need to update, restart this script with --force-clone" 153 exit 0 154fi 155 156echo "$MSG_PREFIX Third-party dependencies are not found in $ARK_THIRD_PARTY_DIR" 157 158if [[ ! -f "$ARK_THIRD_PARTY_MANIFEST" ]] ; then 159 echo "$MSG_PREFIX Invalid manifest file '$ARK_THIRD_PARTY_MANIFEST'" 160 exit 1 161fi 162 163echo "$MSG_PREFIX Using manifest $ARK_THIRD_PARTY_MANIFEST" 164process_manifest "$ARK_THIRD_PARTY_MANIFEST" 165 166echo "$MSG_PREFIX Third-party dependencies installed" 167exit 0 168 169