Debian Codegraph @

DOS/mkdostools

raw
1 #!/bin/bash -p
2 ###############################################################################
3 # BRLTTY - A background process providing access to the console screen (when in
4 # text mode) for a blind person using a refreshable braille display.
5 #
6 # Copyright (C) 1995-2026 by The BRLTTY Developers.
7 #
8 # BRLTTY comes with ABSOLUTELY NO WARRANTY.
9 #
10 # This is free software, placed under the terms of the
11 # GNU Lesser General Public License, as published by the Free Software
12 # Foundation; either version 2.1 of the License, or (at your option) any
13 # later version. Please see the file LICENSE-LGPL for details.
14 #
15 # Web Page: http://brltty.app/
16 #
17 # This software is maintained by Dave Mielke <[email protected]>.
18 ###############################################################################
19
20 set -e
21 umask 022
22 shopt -s nullglob
23
24 readonly autoconfOldVersion=2.13
25 readonly autoconfNewVersion=2.59
26 readonly automakeVersion=1.9.6
27 readonly crxVersion=2.04
28
29 readonly defaultArchivesSubdirectory="Archives"
30 readonly defaultBuildSubdirectory="Build"
31 readonly defaultInstallSubdirectory="Tools"
32 readonly defaultTargetSystem="i586-pc-msdosdjgpp"
33
34 function setVariable {
35 local variable="${1}"
36 local value="${2}"
37
38 eval "${variable}"'="${value}"'
39 }
40
41 function setArrayElement {
42 local array="${1}"
43 local index="${2}"
44 local value="${3}"
45
46 setVariable "${array}[${index}]" "${value}"
47 }
48
49 function defineEnumeration {
50 local array="${1}"
51 shift 1
52
53 declare -g -i -A "${array}"
54 local value=0
55
56 while [ "${#}" -gt 0 ]
57 do
58 setArrayElement "${array}" "${1}" $((value++))
59 shift 1
60 done
61
62 readonly "${array}"
63 }
64
65 readonly programName="${0##*/}"
66 readonly programDirectory="$(realpath "$(dirname "${0}")")"
67
68 function programMessage {
69 local message="${1}"
70
71 echo >&2 "${programName}: ${message}"
72 }
73
74 defineEnumeration logLevels error warning task step detail
75 declare -i logLevel="${logLevels[task]}"
76
77 function logMore {
78 ((logLevel += 1)) || :
79 }
80
81 function logLess {
82 ((logLevel -= 1)) || :
83 }
84
85 function logMessage {
86 local level="${1}"
87 local message="${2}"
88
89 local value="${logLevels[$level]}"
90
91 [ -n "${value}" ] || {
92 logWarning "undefined log level: ${level}"
93 value=0
94 }
95
96 [ "${value}" -gt "${logLevel}" ] || programMessage "${message}"
97 }
98
99 function syntaxError {
100 local message="${1}"
101
102 logError "${message}"
103 exit 2
104 }
105
106 function semanticError {
107 local message="${1}"
108
109 logError "${message}"
110 exit 3
111 }
112
113 function externalError {
114 local message="${1}"
115
116 logError "${message}"
117 exit 4
118 }
119
120 function showUsage {
121 cat <<-END-OF-USAGE
122 usage: ${programName} [-option ...]
123 -h display a usage summary (this text), and then exit
124 -q decrease output verbosity (may be specified more than once)
125 -v increase output verbosity (may be specified more than once)
126 -d directory specify the ROOT directory (default is the current directory)
127 -a directory specify the archives directory (default is ROOT/${defaultArchivesSubdirectory})
128 -b directory specify the build directory (default is ROOT/${defaultBuildSubdirectory})
129 -i directory specify the install directory (default is ROOT/${defaultInstallSubdirectory})
130 -t system specify the target system (default is ${defaultTargetSystem})
131 -g version the gcc version to build (defaults if only one is archived)
132 END-OF-USAGE
133
134 exit 0
135 }
136
137 function handleDirectoryOption {
138 local variable="${1}"
139 local default="${2}"
140 local name="${3}"
141
142 [ -n "${!variable}" ] || setVariable "${variable}" "${default}"
143 setVariable "${variable}" "$(realpath "${!variable}")"
144 readonly "${variable}"
145
146 [ -z "${name}" ] || logDetail "${name} directory: ${!variable}"
147 }
148
149 function logArrayElements {
150 local array="${1}"
151 local name="${2}"
152
153 message="${name} properties:"
154
155 eval local 'indices="${!'"${array}"'[*]}"'
156 local index
157
158 for index in ${indices}
159 do
160 local variable="${array}[${index}]"
161 message+=" ${index}=${!variable}"
162 done
163
164 logDetail "${message}"
165 }
166
167 function findHostCommand {
168 local variable="${1}"
169 shift 1
170
171 local command
172 for command
173 do
174 local path="$(type -p "${command}")"
175
176 [ -z "${path}" ] || {
177 setVariable "${variable}" "${path}"
178 export "${variable}"
179 logDetail "host command location: ${variable} -> ${!variable}"
180 return 0
181 }
182 done
183
184 semanticError "host command not found: ${variable} (${*})"
185 }
186
187 function pathChange {
188 local newPath="${1}"
189
190 export PATH="${newPath}"
191 logDetail "host command search path: ${PATH}"
192 }
193
194 function pathPrepend {
195 local directory="${1}"
196
197 pathChange "${directory}:${PATH}"
198 }
199
200 function makeDirectory {
201 local path="${1}"
202
203 mkdir -p "${path}"
204 }
205
206 function emptyDirectory {
207 local path="${1}"
208
209 rm -f -r "${path}/"*
210 }
211
212 function initializeDirectory {
213 local path="${1}"
214
215 makeDirectory "${path}"
216 emptyDirectory "${path}"
217 }
218
219 function verifyArchive {
220 local array="${1}"
221 local type="${2}"
222 local prefix="${3}"
223 local suffix="${4}"
224 local version="${5}"
225
226 declare -g -A "${array}"
227 setArrayElement "${array}" type "${type}"
228 setArrayElement "${array}" prefix "${prefix}"
229 setArrayElement "${array}" suffix "${suffix}"
230
231 local name="${prefix%-}"
232 setArrayElement "${array}" name "${name}"
233
234 local originalDirectory="${PWD}"
235 cd "${archivesDirectory}"
236 if [ -n "${version}" ]
237 then
238 local file="${prefix}${version}${suffix}"
239 [ -f "${file}" ] || semanticError "${type} archive not found: ${file}"
240 else
241 local files=("${prefix}"*"${suffix}")
242 local count="${#files[*]}"
243 ((count > 0)) || semanticError "${type} archive not found: ${name}"
244 ((count == 1)) || semanticError "${type} package with multiple archives: ${files[*]}"
245
246 local file="${files[0]}"
247 version="${file%${suffix}}"
248 version="${version:${#prefix}}"
249 fi
250 cd "${originalDirectory}"
251
252 [[ "${version}" =~ ^[0-9]{3}$ ]] && version="${version:0:1}.${version:1}"
253 setArrayElement "${array}" version "${version}"
254
255 setArrayElement "${array}" file "${file}"
256 setArrayElement "${array}" path "${archivesDirectory}/${file}"
257
258 local source="${name}-${version}"
259 setArrayElement "${array}" source "${source}"
260
261 readonly "${array}"
262 logArrayElements "${array}" "archive"
263 }
264
265 function gnuVerifyArchive {
266 local array="${1}"
267 local name="${2}"
268 local version="${3}"
269
270 verifyArchive "${array}" Gnu "${name}-" ".tar.gz" "${version}"
271 }
272
273 function djgppVerifyArchive {
274 local array="${1}"
275 local name="${2}"
276 local type="${3}"
277 local version="${4}"
278
279 verifyArchive "${array}" DJGPP "${name}" "${type}.zip" "${version//./}"
280 }
281
282 function logPackageTask {
283 local array="${1}"
284 local task="${2}"
285
286 local nameVariable="${array}[name]"
287 local versionVariable="${array}[version]"
288 logTask "${task}: ${!nameVariable}-${!versionVariable}"
289 }
290
291 function unpackArchive {
292 local array="${1}"
293
294 local typeVariable="${array}[type]"
295 local pathVariable="${array}[path]"
296
297 logPackageTask "${array}" "unpacking ${!typeVariable} archive"
298 "unpackArchive_${!typeVariable}" "${!pathVariable}"
299 }
300
301 function unpackArchive_Gnu {
302 local path="${1}"
303
304 tar xfz "${path}"
305 }
306
307 function unpackArchive_DJGPP {
308 local path="${1}"
309
310 unzip -q -a "${path}"
311 }
312
313 function changeScriptVariable {
314 local script="${1}"
315 local variable="${2}"
316 local value="${3}"
317
318 sed -e "/^ *${variable} *=/s%=.*%='${value}'%" -i "${script}"
319 }
320
321 function logBuildDirectory {
322 logNote "build directory: ${PWD}"
323 }
324
325 function runBuildCommand {
326 local logFile="${1}"
327 shift 1
328
329 logNote "build command: ${*}"
330 "${@}" >&"${logFile}" || externalError "build error: for details, see ${PWD}/${logFile}"
331 }
332
333 function configurePackage {
334 local source="${1}"
335 shift 1
336
337 runBuildCommand configure.log "${source}/configure" "${@}"
338 }
339
340 function makePackage {
341 runBuildCommand make.log make "${@}"
342 }
343
344 function installPackage {
345 runBuildCommand install.log make install "${@}"
346 }
347
348 function buildHostPackage {
349 local array="${1}"
350 shift 1
351
352 logPackageTask "${array}" "building host package"
353
354 local sourceVariable="${array}[source]"
355 local build="${!sourceVariable}-host"
356 local prefix="$(realpath "${!sourceVariable}-install")"
357
358 makeDirectory "${build}"
359 cd "${build}"
360 logBuildDirectory
361 configurePackage "../${!sourceVariable}" --prefix="${prefix}"
362 makePackage
363 installPackage
364 cd ..
365
366 local bin="${prefix}/bin"
367 pathPrepend "${bin}"
368
369 while [ "${#}" -gt 0 ]
370 do
371 local command="${1}"
372 local variable="${2}"
373 shift 2
374
375 changeScriptVariable "${gccUnpackScript}" "${variable}" "${bin}/${command}"
376 done
377 }
378
379 function buildHostArchive {
380 local array="${1}"
381 shift 1
382
383 unpackArchive "${array}"
384 buildHostPackage "${array}" "${@}"
385 }
386
387 function buildHostAutoconf {
388 local array="${1}"
389 local autoconfVariable="${2}"
390 local autoheaderVariable="${3}"
391
392 buildHostArchive "${array}" autoconf "${autoconfVariable}" autoheader "${autoheaderVariable}"
393 }
394
395 function configureTargetPackage {
396 local array="${1}"
397 shift 1
398
399 local sourceVariable="${array}[source]"
400 configurePackage "../${!sourceVariable}" \
401 "--prefix=${installDirectory}" \
402 "--target=${targetSystem}" \
403 "${@}"
404 }
405
406 function buildTargetPackage {
407 local array="${1}"
408 shift 1
409
410 logPackageTask "${array}" "building target package"
411
412 cd gnu
413 local sourceVariable="${array}[source]"
414 local build="${!sourceVariable}-target"
415 makeDirectory "${build}"
416 cd "${build}"
417 logBuildDirectory
418 configureTargetPackage "${array}" "${@}"
419 makePackage
420 installPackage
421 cd ../..
422 }
423
424 rootDirectory=""
425 archivesDirectory=""
426 buildDirectory=""
427 installDirectory=""
428
429 targetSystem=""
430 gccVersion=""
431
432 while getopts ":hqvd:a:b:i:t:g:" option
433 do
434 case "${option}"
435 in
436 h) showUsage;;
437
438 q) logLess;;
439 v) logMore;;
440
441 d) rootDirectory="${OPTARG}";;
442 a) archivesDirectory="${OPTARG}";;
443 b) buildDirectory="${OPTARG}";;
444 i) installDirectory="${OPTARG}";;
445
446 t) targetSystem="${OPTARG}";;
447 g) gccVersion="${OPTARG}";;
448
449 :) syntaxError "missing ooperand: -${OPTARG}";;
450 \?) syntaxError "unknown option: -${OPTARG}";;
451 *) syntaxError "unimplemented option: -${option}";;
452 esac
453 done
454
455 shift $((OPTIND - 1))
456 [ "${#}" -eq 0 ] || syntaxError "too many parameters"
457
458 handleDirectoryOption rootDirectory "${PWD}" "root"
459 handleDirectoryOption archivesDirectory "${rootDirectory}/${defaultArchivesSubdirectory}" "archives"
460 handleDirectoryOption buildDirectory "${rootDirectory}/${defaultBuildSubdirectory}" "build"
461 handleDirectoryOption installDirectory "${rootDirectory}/${defaultInstallSubdirectory}" "install"
462
463 [ -n "${targetSystem}" ] || targetSystem="${defaultTargetSystem}"
464 readonly targetSystem
465
466 pathChange "$(getconf PATH)"
467 unset MAKEFLAGS
468
469 logTask "finding host commands"
470 findHostCommand CC cc gcc
471 findHostCommand CXX c++ g++ cxx gxx
472 findHostCommand LIBTOOL libtool
473
474 logTask "verifying archives"
475 gnuVerifyArchive gnuAutoconfOld autoconf "${autoconfOldVersion}"
476 gnuVerifyArchive gnuAutoconfNew autoconf "${autoconfNewVersion}"
477 gnuVerifyArchive gnuAutomake automake "${automakeVersion}"
478 gnuVerifyArchive gnuBinutils binutils
479 gnuVerifyArchive gnuGcc gcc "${gccVersion}"
480 djgppVerifyArchive djgppGcc gcc s2 "${gnuGcc[version]}"
481 djgppVerifyArchive djgppCrx djcrx "" "${crxVersion}"
482
483 logTask "preparing build directory"
484 initializeDirectory "${buildDirectory}"
485 cd "${buildDirectory}"
486
487 unpackArchive djgppCrx
488 unpackArchive djgppGcc
489
490 gccUnpackScript="unpack-gcc.sh"
491 buildHostAutoconf gnuAutoconfOld AUTOCONF_OLD AUTOHEADER_OLD
492 buildHostAutoconf gnuAutoconfNew AUTOCONF AUTOHEADER
493 buildHostArchive gnuAutomake
494
495 logTask "patching gcc source"
496 logBuildDirectory
497 chmod u=rwx,go=r "${gccUnpackScript}"
498 runBuildCommand unpack-gcc.log "./${gccUnpackScript}" "$(realpath --relative-to=. "${gnuGcc["path"]}")"
499
500 cd gnu
501 unpackArchive gnuBinutils
502 cd ..
503
504 logTask "preparing install directory"
505 initializeDirectory "${installDirectory}"
506 pathPrepend "$${installDirectory}/bin"
507 readonly targetDirectory="${installDirectory}/${targetSystem}"
508 makeDirectory "${targetDirectory}"
509 makeDirectory "${targetDirectory}/bin"
510 cp -r lib "${targetDirectory}"
511 cp -r include "${targetDirectory}"
512
513 logTask "building stubify"
514 cd src/stub
515 logBuildDirectory
516 runBuildCommand compile.log "${CC}" -O2 stubify.c -o "${targetDirectory}/bin/stubify"
517 cd ../..
518
519 buildTargetPackage gnuBinutils
520 buildTargetPackage djgppGcc --with-headers="${targetDirectory}/include"
521
522 logTask "creating symbolic links"
523 cd "${targetDirectory}/lib"
524 ln -s libstdc++.a libstdcxx.a
525 ln -s libsupc++.a libsupcxx.a
526
527 logTask "cleaning up"
528 cd /
529 emptyDirectory "${buildDirectory}"
530
531 logTask "done"
532 exit 0