forked from top-sekret/psptoolchain
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.sh
130 lines (113 loc) · 2.95 KB
/
common.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/sh
# Run the tar command.
# Usage: run_tar <directory> <tar options...>
run_tar ()
{
DIRECTORY="$1"
shift
tar --directory="$DIRECTORY" --no-same-owner --strip-components=1 "$@"
}
# Extracts a file based on its extension
# Usage: extract <archive> <directory>
auto_extract ()
{
FILE="$1"
NAME="$(echo "$FILE" | sed -e 's|^.*/||')"
EXTENSION="$(echo "$NAME" | sed -e 's|.*\.||')"
DIRECTORY="$2"
if [ -d "$DIRECTORY" ]
then
echo "Deleting existing $DIRECTORY"
rm -rf "$DIRECTORY"
fi
mkdir -p "$DIRECTORY"
echo "Extracting $NAME"
case "$EXTENSION" in
tar)
run_tar "$DIRECTORY" -xf "$FILE"
;;
gz | tgz)
run_tar "$DIRECTORY" -xzf "$FILE"
;;
bz2 | tbz2)
run_tar "$DIRECTORY" -xjf "$FILE"
;;
xz | txz)
run_tar "$DIRECTORY" -xJf "$FILE"
;;
*)
cat >&2 <<_EOF_
Unsupported archive type: $EXTENSION
_EOF_
return 1
;;
esac
return $?
}
# Downloads and extracts a file, with some extra checks.
# Usage: download_and_extract <url> <output directory>
download_and_extract ()
{
URL="$1"
NAME="$(echo "$URL" | sed -e 's|^.*/||;s|\?.*$||')"
OUT_DIR="$2"
# First, if the archive already exists, attempt to extract it. Failing
# that, attempt to continue an interrupted download. If that also fails,
# remove the presumably corrupted file.
if [ -f "$NAME" ]
then
if auto_extract "$NAME" "$OUT_DIR"
then
return 0
else
wget --continue --no-check-certificate "$URL" -O "$NAME" || rm -f "$NAME"
fi
fi
# If the file does not exist at this point, it means it was either never
# downloaded, or it was deleted for being corrupted. Just go ahead and
# download it.
# Using wget --continue here would make buggy servers flip out for nothing.
if ! [ -f "$NAME" ]
then
wget --no-check-certificate "$URL" -O "$NAME" || return 1
fi
auto_extract "$NAME" "$OUT_DIR"
}
# Runs Git in a way that won't lock waiting for the user or anything.
# Usage: git_noninteractive <normal git args...>
git_noninteractive ()
{
SSH_ASKPASS=false git "$@" </dev/null
}
# Clones or updates a Git repository.
# Usage: clone_git_repo <url> <dir> [branch]
clone_git_repo ()
{
URL="$1"
LOCAL_DIR="$2"
BRANCH="${3:-master}"
# It is possible that this is an actual copy of the repository we are
# interested in.
if [ -d "$LOCAL_DIR/.git" ] \
&& git_noninteractive -C "$LOCAL_DIR" status >/dev/null 2>&1 \
&& [ "x$(git_noninteractive -C "$LOCAL_DIR" remote get-url origin 2>/dev/null)" = "x$URL" ]
then
echo "Updating existing repository in $LOCAL_DIR"
cd "$LOCAL_DIR" || return 1
git_noninteractive pull --rebase origin "$BRANCH" || return 1
git_noninteractive reset --hard || return 1
git_noninteractive clean -dfx || return 1
cd - >/dev/null 2>&1 || return 1
return 0
else
echo "Deleting existing $LOCAL_DIR"
rm -rf "$LOCAL_DIR"
fi
git_noninteractive clone --recursive -b "$BRANCH" "$URL" "$LOCAL_DIR" || return 1
}
# Runs make with our options.
# Usage: run_make <normal make args...>
run_make ()
{
make -j"$JOBS" "$@"
}