summaryrefslogtreecommitdiff
path: root/include/env/get.sh
blob: 0ed9d6ccdcad07f9e6ecb63ab4d376b899bba132 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# SPDX-License-Identifier: GPL-3.0-or-later

# Copyright (c) 2020-2021,2023-2026 Leah Rowe <leah@libreboot.org>
# Copyright (c) 2022 Caleb La Grange <thonkpeasant@protonmail.com>

eval "`newvar url bkup_url fetch_depend loc subcurl subcurl_bkup subgit \
    subgit_bkup subhash badhash badtghash`"

tmpgit="$xbtmp/gitclone"
tmpgitcache="$xbtmp/tmpgit"

# delete a source tree and corresponding builds if metadata changed
# inside config/ - this is done by hashing a concatenation of hashes
# from all metadata, checked against a previously calculated hash

delete_old_project_files()
{
	# delete an entire source tree along with its builds:
	same_pjhash mhash "$tree" badhash "$datadir" \
	    "$configdir/$tree" "$mdir" || \
		x_ rm -Rf "src/$project/$tree" "elf/$project/$tree"

	x_ cp "$xbtmp/new.hash" "$XBMK_CACHE/mhash/$project$tree"

	if [ -n "$target" ] && [ "$target" != "$tree" ]; then
		# delete only target builds, if only the target changed
		same_pjhash mtghash "$target" badtghash "$configdir/$target" \
		    || x_ rm -Rf "elf/$project/$tree/$target"

		x_ cp "$xbtmp/new.hash" "$XBMK_CACHE/mtghash/$project$target"
	fi
}

same_pjhash()
{
	eval "`newvar hash old_hash`"

	hashdir="$1"
	hashname="$2"
	badhashvar="$3"

	shift 3

	x_ mkdir -p "$XBMK_CACHE/$hashdir"

	if [ -f "$XBMK_CACHE/$hashdir/$project$hashname" ]; then
		read -r old_hash < "$XBMK_CACHE/$hashdir/$project$hashname" \
		    || err \
		    "$hashdir: err '$XBMK_CACHE/$hashdir/$project$hashname'" \
		    same_pjhash "$hashdir" "$hashname" "$badhashvar" "$@"
	fi

	eval "$build_sbase"
	fx_ "x_ util/sbase/sha512sum" find "$@" -type f -not -path \
	    "*/.git*/*" | awk '{print $1}' > "$xbtmp/tmp.hash" || \
	    err "!h $project $hashdir" \
	    "same_pjhash" "$hashdir" "$hashname" "$badhashvar" "$@"

	hash="$(x_ "$sha512sum" "$xbtmp/tmp.hash" | awk '{print $1}' || \
	    err)" || err "$hashname: Can't read sha512 of '$xbtmp/tmp.hash'" \
	    "same_pjhash" "$hashdir" "$hashname" "$badhashvar" "$@"

	if [ "$hash" != "$old_hash" ] || \
	    [ ! -f "$XBMK_CACHE/$hashdir/$project$hashname" ]; then
		eval "$badhashvar=\"y\""
	fi

	printf "%s\n" "$hash" > "$xbtmp/new.hash" || \
	    err "!mkhash $xbtmp/new.hash ($hashdir $hashname $badhashvar)" \
	    "same_pjhash" "$hashdir" "$hashname" "$badhashvar" "$@"

	eval "[ \"\$$badhashvar\" = \"y\" ] && return 1"; :
}

fetch_trees()
{
	[ -d "src/$project/$tree" ] && return 0

	mk_dependencies -f $fetch_depend

	git_prep "$url" "$bkup_url" "$xbmkpwd/$configdir/$tree/patches" \
	    "src/$project/$tree"
}

git_prep()
{
	printf "Creating code directory, src/%s/%s\n" "$project" "$tree"

	_patchdir="$3"
	_loc="$4" # $1 and $2 are gitrepo and gitrepo_backup

	[ -z "$rev" ] && \
		err "$project/$tree: rev unset" "git_prep" "$@"

	xbget git "$1" "$2" "$tmpgit" "$rev" "$_patchdir"
	dx_ fetch_submodule "$mdir/module.list"

	[ "$_loc" != "${_loc%/*}" ] && \
		x_ mkdir -p "${_loc%/*}"

	x_ mv "$tmpgit" "$_loc"
}

fetch_submodule()
{
	mcfgdir="$mdir/${1##*/}"
	eval "`newvar st subcurl subcurl_bkup subgit subgit_bkup subhash`"

	e "$mcfgdir/module.cfg" f missing && return 0
	. "$mcfgdir/module.cfg" || \
	    err "Can't read '$mcfgdir/module.cfg'" "fetch_submodules" "$@"

	[ -n "$subgit" ] && [ -n "$subgit_bkup" ] && \
		st="git"
	[ -n "$subcurl" ] && [ -n "$subcurl_bkup" ] && \
		st="curl$st"
	[ "$st" = "curlgit" ] && \
		err "$mdir: git+curl defined" "fetch_submodule" "$@"
	[ -z "$st" ] && \
		return 0
	[ -z "$subhash" ] && \
		err "subhash not set" "fetch_submodule" "$@"

	[ "$st" = "git" ] && \
		x_ rm -Rf "$tmpgit/$1"

	eval "xbget \"\$st\" \"\$sub$st\" \"\$sub${st}_bkup\" \"\$tmpgit/\$1\" \
	    \"\$subhash\" \"\$mdir/\${1##*/}/patches\""
}

# TODO: in the following functions, argument numbers are used
#       which is hard to understand. the code should be modified
#       so that variable names are used instead, for easy reading

xbget()
{
	[ "$1" != "curl" ] && [ "$1" != "copy" ] && [ "$1" != "git" ] && \
		err "Bad dlop (arg 1)" "xbget" "$@"

	for url in "$2" "$3"; do
		[ -z "$url" ] && \
			err "empty URL given in" "xbget" "$@"
		try_fetch_$1 "$url" "$@" || \
			continue
		[ -e "$4" ] && return 0; : # successful download/copy
	done

	err "failed to download file/repository" "xbget" "$@"; :
}

try_fetch_git()
{
	# 1st argument $1 is the current git remote being tried,
	# let's say it was https://foo.example.com/repo, then cached
	# directories becomes cache/mirror/foo.example.com/repo

	cached="clone"
	[ "$XBMK_CACHE_MIRROR" = "y" ] && \
		cached="mirror"
	cached="$cached/${1#*://}"
	cached="$XBMK_CACHE/$cached"

	x_ mkdir -p "${5%/*}" "${cached%/*}"

	try_$2 "$cached" "$@" || \
		return 1
	[ ! -d "$cached" ] && \
		return 1

	if [ ! -d "$5" ]; then
		tmpclone "$cached" "$5" "$6" "$7" || \
		    err "Can't clone final repo" "try_fetch" "$@"; :
	fi

	[ ! -d "$5" ] && \
		return 1; :
}

try_fetch_copy()
{
	try_fetch_curl "$@" || return 1; :
}

try_fetch_curl()
{
	cached="file/$6"
	cached="$XBMK_CACHE/$cached"

	x_ mkdir -p "${5%/*}" "${cached%/*}"

	if bad_checksum "$6" "$cached" 2>/dev/null; then
		x_ rm -f "$cached"
	fi

	[ -f "$cached" ] || \
		try_$2 "$cached" "$@" || \
			return 1

	[ -f "$5" ] && bad_checksum "$6" "$5" 2>/dev/null && \
		x_ cp "$cached" "$5"

	if [ ! -f "$cached" ]; then
		return 1
	elif bad_checksum "$6" "$cached"; then
		x_ rm -f "$cached"
		return 1
	fi

	[ "$cached" != "$5" ] && \
		x_ cp "$cached" "$5"

	if bad_checksum "$6" "$5"; then
		x_ rm -f "$5"
		return 1
	elif [ ! -f "$5" ]; then
		return 1
	fi
}

try_curl()
{
	ua="Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0"

	( x_ curl --location --retry 3 -A "$ua" "$2" -o "$1" ) \
	    || ( x_ wget --tries 3 -U "$ua" "$2" -O "$1" ) || return 1; :
}

try_copy()
{
	( x_ cp "$2" "$1" ) || return 1; :
}

try_git()
{
	gitdest="`findpath "$1" || err "Can't get findpath for '$1'"`" || \
	    err "failed findpath for '$1'" try_get "$@"

	x_ rm -Rf "$tmpgitcache"

	if [ ! -d "$gitdest" ]; then
		if [ "$XBMK_CACHE_MIRROR" = "y" ]; then
			( x_ git clone --mirror "$2" "$tmpgitcache" ) || \
			    return 1
		else
			( x_ git clone "$2" "$tmpgitcache" ) || return 1
		fi

		x_ mkdir -p "${gitdest%/*}"
		x_ mv "$tmpgitcache" "$gitdest"
	fi

	git -C "$gitdest" show "$7" 1>/dev/null 2>/dev/null && \
	    [ "$forcepull" != "y" ] && \
		return 0 # don't pull changes if target rev exists locally

	if [ "$XBMK_CACHE_MIRROR" = "y" ]; then
		( x_ git -C "$gitdest" fetch ) || :; :
		( x_ git -C "$gitdest" update-server-info ) || :; :
	else
		( x_ git -C "$gitdest" pull --all ) || :; :
	fi
}

bad_checksum()
{
	e "$2" f missing && \
		return 0

	eval "$build_sbase"
	csum="$(x_ "$sha512sum" "$2" | awk '{print $1}')" || \
	    err "!sha512 '$2' $1" bad_checksum "$@"

	[ "$csum" = "$1" ] && \
		return 1

	x_ rm -f "$2"
	printf "BAD SHA512 %s, '%s'; need %s\n" "$csum" "$2" "$1" 1>&2
}

tmpclone()
{
	( x_ git clone "$1" "$2" ) || return 1
	( x_ git -C "$2" reset --hard "$3" ) || return 1

	[ ! -d "$4" ] && \
		return 0

	tmpclone_patchlist="`mktemp || err "Can't create tmp patch list"`" || \
	    err "Can't create tmp patch list" "tmpclone" "$@"

	x_ find "$4" -type f | sort > "$tmpclone_patchlist" || \
	    err "Can't write patch names to '$tmpclone_patchlist'" \
	    "tmpclone" "$@"

	while read -r tmpclone_patch; do

		( x_ git -C "$2" am -3 --keep-cr "$tmpclone_patch" ) || \
		    err "Can't apply '$tmpclone_patch'" "tmpclone" "$@"; :

	done < "$tmpclone_patchlist" || \
	    err "Can't read '$tmpclone_patchlist'" "tmpclone" "$@"

	x_ rm -f "$tmpclone_patchlist"
}