darkcrusade 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #! /bin/sh -
  2. #
  3. # Deploy static cross-compilers for easy distribution
  4. #
  5. # Copyright (c) 2016-2021 Matias Fonzo, <selk@dragora.org>.
  6. # Copyright (c) 2022 DustDFG, <dfgdust@gmail.com>.
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License");
  9. # you may not use this file except in compliance with the License.
  10. # You may obtain a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. # Exit immediately on any error
  20. set -e
  21. PROGRAM="${0##*/}"
  22. # Override locale settings
  23. LC_ALL=C
  24. LANGUAGE=C
  25. export LC_ALL LANGUAGE
  26. umask 022
  27. # Get physical working directory (absolute path)
  28. CWD="$(CDPATH='' cd -P -- "$(dirname -- "$0")" && pwd -P)" || exit $?
  29. ### Functions
  30. usage()
  31. {
  32. printf '%s' \
  33. "Usage: $PROGRAM [OPTIONS] [TARGET]...
  34. Deploy static cross-compilers for easy distribution.
  35. Where TARGET is any target found at the targets/ directory.
  36. If no target name is passed, all available targets will be built.
  37. Options:
  38. -j Parallel jobs for the compiler [${jobs}]
  39. -h Display this help and exit
  40. Some influential environment variables:
  41. TMPDIR Temporary directory for sources [${TMPDIR}]
  42. Available targets from ${CWD}/targets ...
  43. "
  44. for name in "${CWD}/targets"/*
  45. do
  46. sed -e '2q;d' "$name"
  47. done
  48. unset -v name
  49. echo ""
  50. }
  51. warn()
  52. {
  53. printf '%s\n' "$@" 1>&2
  54. }
  55. ### Default values
  56. jobs=1
  57. output="${CWD}/OUTPUT.${PROGRAM}"
  58. TMPDIR="${TMPDIR:=${output}/sources}"
  59. export TMPDIR
  60. ### Parse options
  61. while getopts :hj: name
  62. do
  63. case $name in
  64. h)
  65. usage
  66. exit 0
  67. ;;
  68. j)
  69. jobs="$OPTARG"
  70. ;;
  71. :)
  72. warn "Option '-${OPTARG}' requires an argument"
  73. usage
  74. exit 1
  75. ;;
  76. \?)
  77. warn "Illegal option -- '-${OPTARG}'"
  78. usage
  79. exit 1
  80. ;;
  81. esac
  82. done
  83. shift $(( OPTIND - 1 ))
  84. unset -f usage
  85. # Compose tag for tarball names using yearNumber+monthName+dayNumber
  86. tarname="darkcrusade_$(date +%Y%b%d)"
  87. # Prepare to start the cross compiler process in the phase 1
  88. phase_one="${output}/phase1-native-$(uname -m)-$(date +%Y-%m-%d.%H%M%S)-$$"
  89. echo "${PROGRAM}: Creating native cross compiler on $phase_one ..."
  90. "${CWD}"/bootstrap -j${jobs} -s0 -o "$phase_one"
  91. # Now will start the preparatives to use the recent native cross compiler
  92. echo "${PROGRAM}: Using (existing) compiler from $phase_one ..."
  93. # Set flags according to the generated compiler
  94. for BTCC in "${phase_one}"/cross/*/bin/*-gcc
  95. do
  96. if test -x "$BTCC"
  97. then
  98. BTCC="$BTCC"
  99. break
  100. fi
  101. done
  102. for BTCXX in "${phase_one}"/cross/*/bin/*-g++
  103. do
  104. if test -x "$BTCXX"
  105. then
  106. BTCXX="$BTCXX"
  107. break
  108. fi
  109. done
  110. # Set and compose flags to be used as CC/CXX
  111. BTCC="$BTCC -static -Wl,-Bstatic -static-libgcc"
  112. BTCXX="$BTCXX -static -Wl,-Bstatic -static-libgcc"
  113. IS_DARKCRUSADE=IS_DARKCRUSADE
  114. export BTCC BTCXX IS_DARKCRUSADE
  115. # Loop for create the targets, phase 2
  116. for target in "${CWD}"/targets/${@:-*}
  117. do
  118. target="${target##*/}"
  119. phase_two="${output}/phase2-${target}-$(date +%Y-%m-%d.%H%M%S)-$$"
  120. logfile="${output}/${tarname}-${target}.log"
  121. # Build target against native compiler, logging its output
  122. echo "${PROGRAM}: Making target: $target ..."
  123. rm -f "${output}/bootstrapfailed"
  124. {
  125. "${CWD}"/bootstrap -j${jobs} -s0 -a "$target" -o "$phase_two" || touch "${output}/bootstrapfailed"
  126. } 2>&1 | tee "$logfile"
  127. if test -f "${output}/bootstrapfailed"
  128. then
  129. echo "${PROGRAM}: An error occurred while processing the target: $target" 1>&2
  130. echo " Please check the log file $logfile" 1>&2
  131. exit 99;
  132. fi
  133. # Provide log file, tarball and checkum
  134. lzip -9 "$logfile" &
  135. (
  136. cd -- "$phase_two" && \
  137. tarlz --solid -9 -cv cross/ \
  138. -f "${output}/${tarname}-${target}.tar.lz"
  139. )
  140. (
  141. cd -- "$output" && \
  142. sha256sum "${tarname}-${target}.tar.lz" \
  143. > "${tarname}-${target}.tar.lz.sha256"
  144. )
  145. done