/
update.sh
executable file
·107 lines (87 loc) · 2.85 KB
/
update.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env bash
# Copyright 2021 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# This script updates the list of external dependencies in imports.go, which
# allows `go mod` to operate in that directory to update and vendor those
# dependencies.
set -euo pipefail
cd "$(dirname "$0")"
source ../../tools/devshell/lib/vars.sh
UPDATE=0
while getopts u: flag
do
case "${flag}" in
u)
UPDATE=1
;;
*)
echo "Invalid option: $flag"
exit 1
;;
esac
done
# Create various symlinks needed for the `go list` call below.
fx-command-run setup-go
function cleanup() {
rm "$COBALT_DST"
find "$TMP" -maxdepth 1 -mindepth 1 -exec mv {} . \;
}
trap cleanup EXIT
# Escape third_party/go.mod.
readonly COBALT_DST=$FUCHSIA_DIR/cobalt
ln -s "$FUCHSIA_DIR"/third_party/cobalt "$COBALT_DST"
# Move jiri-managed repositories out of the module.
TMP=$(mktemp -d)
if ignored=$(git check-ignore ./*); then
echo "$ignored" | xargs --no-run-if-empty -I % mv % "$TMP"/%
fi
GOROOTBIN=$(fx-command-run go env GOROOT)/bin
GO=$GOROOTBIN/go
GOFMT=$GOROOTBIN/gofmt
IMPORTS=()
for dir in $FUCHSIA_DIR $FUCHSIA_DIR/cobalt $FUCHSIA_DIR/third_party/syzkaller/sys/syz-sysgen; do
while IFS='' read -r line; do IMPORTS+=("$line"); done < <(cd "$dir" && git ls-files -- \
'*.go' ':!third_party/golibs/vendor' |
xargs dirname |
sort | uniq |
sed 's|^|./|' |
xargs "$GO" list -tags deadlock_detection -mod=readonly -e -f \
'{{join .Imports "\n"}}{{"\n"}}{{join .TestImports "\n"}}{{"\n"}}{{join .XTestImports "\n"}}' |
grep -vF go.fuchsia.dev/fuchsia/ |
# Apparently we generate these normally checked-in files?
grep -vF 'go.chromium.org/luci' |
grep -F . |
sort | uniq |
xargs "$GO" list -mod=readonly -e -f \
'{{if not .Goroot}}_ "{{.ImportPath}}"{{end}}' |
grep -vF github.com/google/syzkaller/ |
sort | uniq)
done
IMPORTS_STR=$(
IFS=$'\n'
echo "${IMPORTS[*]}"
)
THIS_SCRIPT=$(realpath --relative-to="$FUCHSIA_DIR" "$(basename "$0")")
printf '// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Code generated by %s; DO NOT EDIT.
package imports
import (\n%s\n)' "//$THIS_SCRIPT" "$IMPORTS_STR" | $GOFMT -s >imports.go
if [ $UPDATE -eq 1 ]; then
# The following two lines used to be in the opposite order, which incorrectly
# caused `go get -u` to fetch gvisor from the default branch instead of the
# go branch.
$GO get -u gvisor.dev/gvisor@go
$GO get -u
else
$GO get
fi
$GO mod tidy
$GO mod vendor
"${PREBUILT_PYTHON3_DIR}/bin/python3.8" update_sources.py \
--build-file='BUILD.gn' \
--golibs-dir='.' > "${TMP}/BUILD.gn"
mv "${TMP}/BUILD.gn" 'BUILD.gn'
"${PREBUILT_GN}" format 'BUILD.gn'