forked from pspdev/psptoolchain
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoolchain.sh
executable file
·231 lines (194 loc) · 4.13 KB
/
toolchain.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
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
#!/bin/sh
# toolchain.sh by Jakub Kaszycki <[email protected]>
# This function is based on an old function, which enforced the number of
# jobs. Now the only purpose of this function is to provide a way to detect
# CPU number when the user wants to detect it.
num_cpus ()
{
if command -v getconf >/dev/null 2>&1
then
if getconf _NPROCESSORS_ONLN >/dev/null 2>&1
then
getconf _NPROCESSORS_ONLN
return 0
fi
fi
cat >&2 <<_EOF_
Warning: could not detect number of CPUs, assuming 1 job at a time
_EOF_
echo 1
}
# Parse the command line options.
short_usage ()
{
echo "Usage: $0 [options...] [stages...]"
}
error_usage ()
{
short_usage
echo "Try $0 -h for more information."
}
usage ()
{
short_usage
cat <<_EOF_
Builds the PSP toolchain.
Options:
-d<DIRECTORY> Set <DIRECTORY> as the output directory.
This option is required.
-h Print this help text.
-j<JOBS> Run <JOBS> jobs at the same time. If the argument
"auto" is given, chooses a number depending on the
number of available CPUs. If this option is not given,
the script runs only one job at a time (takes
a lot of time with tasks like building GCC, but is
safer, more stable and much more deterministic).
-s<DIRECTORY> Set <DIRECTORY> as the source directory.
The default is current directory.
If no stages are passed, all stages are run. Otherwise, only the selected
stages are run with the order as passed on the command line.
Stages may be passed as names, numbers or script names.
_EOF_
}
PSPDEV=
JOBS=1
SOURCE=.
while getopts '+:d:hj:s:' opt
do
case "$opt" in
d)
PSPDEV="$OPTARG"
;;
h)
usage
exit 0
;;
j)
JOBS="$OPTARG"
;;
s)
SOURCE="$OPTARG"
;;
\?)
echo "Unknown option: -$OPTOPT"
error_usage >&2
exit 2
;;
:)
echo "Missing argument to option -$OPTOPT" >&2
error_usage >&2
exit 2
;;
*)
echo 'Internal error' >&2
exit 99
esac
done
shift $((OPTIND-1))
if [ -z "$PSPDEV" ]
then
echo "Missing required option -d"
exit 2
fi
if [ "x$JOBS" = xauto ]
then
JOBS="$(num_cpus)"
fi
PSPDEV="$(realpath "$PSPDEV")"
SOURCE="$(realpath "$SOURCE")"
PSPDEV_TMPDIR="$(mktemp -dt pspdev-tmp-XXXXXX)"
cleanup ()
{
rm -rf "$PSPDEV_TMPDIR"
}
trap cleanup EXIT
export JOBS
export PSPDEV
export PSPDEV_TMPDIR
PATH="$PSPDEV/bin:$PATH"
export PATH
# Usage: run_script SCRIPT TYPE
run_script ()
{
SCRIPT="$1"
echo "Running $2 script: $(basename "$SCRIPT")"
"$SCRIPT"
X=$?
if ! [ "$X" -eq 0 ]
then
echo "Script $(basename "$SCRIPT") failed with error $X"
exit 1
fi
}
# Usage: run_scripts DIR TYPE
run_scripts ()
{
echo "Running $2 scripts"
IFS_backup="$IFS"
IFS='
'
for SCRIPT in $(find "$1" -name '*.sh' | sort)
do
run_script "$SCRIPT" "$2"
done
IFS="$IFS_backup"
unset IFS_backup
}
## Enter the psptoolchain directory.
cd "$SOURCE" || { echo "ERROR: Could not enter the psptoolchain directory."; exit 1; }
## Create the build directory.
mkdir -p build || { echo "ERROR: Could not create the build directory."; exit 1; }
## Enter the build directory.
cd build || { echo "ERROR: Could not enter the build directory."; exit 1; }
run_scripts ../depends dependency
get_script_number ()
{
NUM="$1"
NUM=$((NUM))
printf '%03d' "$NUM"
unset NUM
}
have_script_number ()
{
# First, check it is a number
if ! [ "$(printf "%s" "$1" | tr -d '0-9' | wc -c)" -eq 0 ]
then
return 1
fi
NUM="$(get_script_number "$1")"
[ "$(find ../scripts -name "$NUM-*.sh" | wc -l)" -eq 1 ]
return $?
}
have_script_name ()
{
[ "$(find ../scripts -name "[0-9][0-9][0-9]-$1.sh" | wc -l)" -eq 1 ]
return $?
}
if [ "$#" -eq 0 ]
then
run_scripts ../scripts build
else
for SCRIPT
do
if echo "$SCRIPT" | grep -F '/' >/dev/null 2>&1
then
# Plain file path.
run_script "$(cd .. && realpath "$SCRIPT")" build
elif [ -e "../scripts/$SCRIPT" ]
then
# Script file name
run_script "../scripts/$SCRIPT" build
elif have_script_number "$SCRIPT"
then
# Script number
run_script "../scripts/$(get_script_number "$SCRIPT")-"*".sh" build
elif have_script_name "$SCRIPT"
then
# Script name
run_script "../scripts/"*"-$SCRIPT.sh" build
else
echo "Unknown script: $SCRIPT" >&2
exit 1
fi
done
fi