xref: /aosp_15_r20/external/cronet/android/tools/import/import_cronet.sh (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1#!/bin/bash
2
3# Copyright 2023 Google Inc. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Script to invoke copybara locally to import Cronet into Android.
18# Inputs:
19#  Environment:
20#   ANDROID_BUILD_TOP: path the root of the current Android directory.
21#  Arguments:
22#   -l rev: The last revision that was imported.
23#  Optional Arguments:
24#   -n rev: The new revision to import.
25#   -f: Force copybara to ignore a failure to find the last imported revision.
26
27set -e -x
28
29OPTSTRING=fl:n:
30
31usage() {
32    cat <<EOF
33Usage: import_cronet.sh -n new-rev [-l last-rev] [-f]
34EOF
35    exit 1
36}
37
38COPYBARA_FOLDER_ORIGIN="/tmp/copybara-origin"
39
40#######################################
41# Create local upstream-import branch in external/cronet.
42# Globals:
43#   ANDROID_BUILD_TOP
44# Arguments:
45#   none
46#######################################
47setup_upstream_import_branch() {
48    local git_dir="${ANDROID_BUILD_TOP}/external/cronet"
49
50    (cd "${git_dir}" && git fetch aosp upstream-import:upstream-import)
51}
52
53#######################################
54# Setup folder.origin for copybara inside /tmp
55# Globals:
56#   COPYBARA_FOLDER_ORIGIN
57# Arguments:
58#   new_rev, string
59#######################################
60setup_folder_origin() (
61    local _new_rev=$1
62    mkdir -p "${COPYBARA_FOLDER_ORIGIN}"
63    cd "${COPYBARA_FOLDER_ORIGIN}"
64
65    if [ -d src ]; then
66        (cd src && git fetch --tags && git checkout "${_new_rev}")
67    else
68        # For this to work _new_rev must be a branch or a tag.
69        git clone --depth=1 --branch "${_new_rev}" https://chromium.googlesource.com/chromium/src.git
70    fi
71
72
73    cat <<EOF >.gclient
74solutions = [
75  {
76    "name": "src",
77    "url": "https://chromium.googlesource.com/chromium/src.git",
78    "managed": False,
79    "custom_deps": {},
80    "custom_vars": {},
81  },
82]
83target_os = ["android"]
84EOF
85    cd src
86    # Set appropriate gclient flags to speed up syncing.
87    gclient sync \
88        --no-history \
89        --shallow \
90        --delete_unversioned_trees
91)
92
93#######################################
94# Runs the copybara import of Chromium
95# Globals:
96#   ANDROID_BUILD_TOP
97#   COPYBARA_FOLDER_ORIGIN
98# Arguments:
99#   last_rev, string or empty
100#   force, string or empty
101#######################################
102do_run_copybara() {
103    local _last_rev=$1
104    local _force=$2
105
106    local -a flags
107    flags+=(--git-destination-url="file://${ANDROID_BUILD_TOP}/external/cronet")
108    flags+=(--repo-timeout 3m)
109
110    # buildtools/third_party/libc++ contains an invalid symlink
111    flags+=(--folder-origin-ignore-invalid-symlinks)
112    flags+=(--git-no-verify)
113
114    if [ ! -z "${_force}" ]; then
115        flags+=(--force)
116    fi
117
118    if [ ! -z "${_last_rev}" ]; then
119        flags+=(--last-rev "${_last_rev}")
120    fi
121
122    /google/bin/releases/copybara/public/copybara/copybara \
123        "${flags[@]}" \
124        "${ANDROID_BUILD_TOP}/external/cronet/android/tools/import/copy.bara.sky" \
125        import_cronet "${COPYBARA_FOLDER_ORIGIN}/src"
126}
127
128while getopts $OPTSTRING opt; do
129    case "${opt}" in
130        f) force=true ;;
131        l) last_rev="${OPTARG}" ;;
132        n) new_rev="${OPTARG}" ;;
133        ?) usage ;;
134        *) echo "'${opt}' '${OPTARG}'"
135    esac
136done
137
138if [ -z "${new_rev}" ]; then
139    echo "-n argument required"
140    usage
141fi
142
143setup_upstream_import_branch
144setup_folder_origin "${new_rev}"
145do_run_copybara "${last_rev}" "${force}"
146
147