bootstrap 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #! /bin/sh -
  2. #
  3. # Builder of custom stages (cross compilers, GNU/Linux distributions)
  4. #
  5. # Copyright (c) 2014-2020 Matias Fonzo, <selk@dragora.org>.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. # EXIT STATUS
  19. # 0 = Successful completion
  20. # 1 = Minor common errors (e.g: help usage, support not available)
  21. # 2 = Command execution error
  22. # 3 = Integrity check error for compressed files
  23. PROGRAM="${0##*/}"
  24. # Override locale settings
  25. LC_ALL=C
  26. LANGUAGE=C
  27. export LC_ALL LANGUAGE
  28. # Get current working directory (absolute path)
  29. CWD="$(CDPATH= cd -- $(dirname -- "$0") && pwd)"
  30. #### Functions
  31. usage() {
  32. printf "%s\n" \
  33. "Builder of custom stages (cross compilers, GNU/Linux distributions)" \
  34. "" \
  35. "Usage: $PROGRAM [OPTION]... [FILE]..." \
  36. "" \
  37. "Defaults for the options are specified in brackets." \
  38. "" \
  39. "Options:" \
  40. " -h display this help and exit" \
  41. " -V print version information and exit" \
  42. " -a target architecture [${arch}]" \
  43. " -j parallel jobs for the compiler [${jobs}]" \
  44. " -k keep (don't delete) source directory" \
  45. " -o output directory [${output}]" \
  46. " -s stage number to build [${stage}]" \
  47. "" \
  48. "Some influential environment variables:" \
  49. " TMPDIR temporary directory for sources [${TMPDIR}]" \
  50. " BTCC C compiler command [${BTCC}]" \
  51. " BTCXX C++ compiler command [${BTCXX}]" \
  52. " BTCFLAGS C compiler flags [${BTCFLAGS}]" \
  53. " BTCXXFLAGS C++ compiler flags [${BTCXXFLAGS}]" \
  54. " BTLDFLAGS linker flags [${BTLDFLAGS}]" \
  55. " VENDOR vendor name to reflect on the target triplet" \
  56. "" \
  57. "Supported architectures (targets):"
  58. for name in "${CWD}/targets"/*
  59. do
  60. printf "%s" "${name##*/} "
  61. done
  62. unset name
  63. echo ""
  64. }
  65. version()
  66. {
  67. printf "%s\n" \
  68. "$PROGRAM 3.21" \
  69. "Copyright (C) 2014-2020 Matias Andres Fonzo." \
  70. "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>" \
  71. "This is free software: you are free to change and redistribute it." \
  72. "There is NO WARRANTY, to the extent permitted by law."
  73. }
  74. warn()
  75. {
  76. echo "$@" 1>&2
  77. }
  78. chkstatus_or_exit()
  79. {
  80. status=$?
  81. if test $status -ne 0
  82. then
  83. echo "Return status = $status" 1>&2
  84. exit ${1-2}; # If not given, defaults to 2
  85. fi
  86. unset status
  87. }
  88. # Function to test and extract compressed files
  89. unpack()
  90. {
  91. for file in "$@"
  92. do
  93. case $file in
  94. *.tar)
  95. tar tf "$file" > /dev/null && \
  96. tar xpf "$file"
  97. chkstatus_or_exit 3
  98. ;;
  99. *.tar.gz | *.tgz | *.tar.Z )
  100. gzip -cd "$file" | tar tf - > /dev/null && \
  101. gzip -cd "$file" | tar xpf -
  102. chkstatus_or_exit 3
  103. ;;
  104. *.tar.bz2 | *.tbz2 | *.tbz )
  105. bzip2 -cd "$file" | tar tf - > /dev/null && \
  106. bzip2 -cd "$file" | tar xpf -
  107. chkstatus_or_exit 3
  108. ;;
  109. *.tar.lz | *.tlz )
  110. lzip -cd "$file" | tar tf - > /dev/null && \
  111. lzip -cd "$file" | tar xpf -
  112. chkstatus_or_exit 3
  113. ;;
  114. *.tar.xz | *.txz )
  115. xz -cd "$file" | tar tf - > /dev/null && \
  116. xz -cd "$file" | tar xpf -
  117. chkstatus_or_exit 3
  118. ;;
  119. *.zip | *.ZIP )
  120. unzip -t "$file" > /dev/null && \
  121. unzip "$file" > /dev/null
  122. chkstatus_or_exit 3
  123. ;;
  124. *.gz)
  125. gzip -t "$file" && \
  126. gzip -cd "$file" > "$(basename -- $file .gz)"
  127. chkstatus_or_exit 3
  128. ;;
  129. *.Z)
  130. gzip -t "$file" && \
  131. gzip -cd "$file" > "$(basename -- $file .Z)"
  132. chkstatus_or_exit 3
  133. ;;
  134. *.bz2)
  135. bzip2 -t "$file" && \
  136. bzip2 -cd "$file" > "$(basename -- $file .bz2)"
  137. chkstatus_or_exit 3
  138. ;;
  139. *.lz)
  140. lzip -t "$file" && \
  141. lzip -cd "$file" > "$(basename -- $file .lz)"
  142. chkstatus_or_exit 3
  143. ;;
  144. *.xz)
  145. xz -t "$file" && \
  146. xz -cd "$file" > "$(basename -- $file .xz)"
  147. chkstatus_or_exit 3
  148. ;;
  149. *)
  150. warn "${PROGRAM}: cannot unpack ${file}: Unsupported extension"
  151. exit 1
  152. esac
  153. done
  154. unset file
  155. }
  156. # Print a warning for good practices.
  157. #
  158. # Recommended practices is to set variables in
  159. # **front** of `configure' or make(1), see:
  160. #
  161. # http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Defining-Variables.html
  162. # http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Setting-Output-Variables.html
  163. # http://www.gnu.org/software/make/manual/make.html#Environment
  164. warn_flags()
  165. {
  166. warn "WARNING: Environment variable '$1' is set." \
  167. "This will be unset to avoid possible risks." \
  168. ""
  169. }
  170. #### Default values
  171. BTCC="${BTCC:=cc}"
  172. BTCXX="${BTCXX:=c++}"
  173. BTCFLAGS="${BTCFLAGS:=-g0 -Os}"
  174. BTCXXFLAGS="${BTCXXFLAGS:=$BTCFLAGS}"
  175. BTLDFLAGS="${BTLDFLAGS:=-s}"
  176. opt_keep=opt_keep.off
  177. stage=0
  178. libSuffix=""
  179. arch="$(uname -m)" || chkstatus_or_exit
  180. jobs=1
  181. worktree="$CWD"
  182. output=${worktree}/OUTPUT.${PROGRAM}
  183. TMPDIR="${TMPDIR:=${output}/sources}"
  184. # Compose vendor name adding "-" as suffix
  185. test -n "$VENDOR" && VENDOR="${VENDOR}-"
  186. #### Parse options
  187. while getopts :ha:j:ko:s:V name
  188. do
  189. case $name in
  190. h)
  191. usage
  192. exit 0
  193. ;;
  194. a)
  195. arch="$OPTARG"
  196. ;;
  197. j)
  198. jobs="$OPTARG"
  199. ;;
  200. k)
  201. opt_keep=opt_keep
  202. ;;
  203. o)
  204. output="$OPTARG"
  205. ;;
  206. s)
  207. stage="$OPTARG"
  208. ;;
  209. V)
  210. version
  211. exit 0
  212. ;;
  213. :)
  214. warn "Option '-${OPTARG}' requires an argument"
  215. usage
  216. exit 1
  217. ;;
  218. \?)
  219. warn "Illegal option -- '-${OPTARG}'"
  220. usage
  221. exit 1
  222. ;;
  223. esac
  224. done
  225. shift $(( OPTIND - 1 ))
  226. # Check for environment variables, print warning, unset in any case
  227. test "${CC:+CC_defined}" = CC_defined && warn_flags CC
  228. test "${CXX:+CXX_defined}" = CXX_defined && warn_flags CXX
  229. test "${CFLAGS:+CFLAGS_defined}" = CFLAGS_defined && warn_flags CFLAGS
  230. test "${CXXFLAGS:+CXXFLAGS_defined}" = CXXFLAGS_defined && warn_flags CXXFLAGS
  231. test "${LDFLAGS:+LDFLAGS_defined}" = LDFLAGS_defined && warn_flags LDFLAGS
  232. unset CC CXX CFLAGS CXXFLAGS LDFLAGS warn_flags
  233. # Load target architecture-file
  234. if test -f "${worktree}/targets/$arch"
  235. then
  236. echo "${PROGRAM}: Loading target $arch ..."
  237. . "${worktree}/targets/$arch"
  238. else
  239. warn \
  240. "${PROGRAM}: Target name not recognized -- '${arch}'" \
  241. "See '$0 -h' for more information"
  242. exit 1
  243. fi
  244. # Determine the host to use.
  245. #
  246. # Set up the host if a C compiler command was exported,
  247. # otherwise, a local `cc' will be used instead
  248. if test -n "$BTCC"
  249. then
  250. host="$(${BTCC} -dumpmachine)" || chkstatus_or_exit
  251. else
  252. host="$(cc -dumpmachine)" || chkstatus_or_exit
  253. fi
  254. # If the host and the target are the same triplet, it won't work.
  255. # We are changing the host if it is really needed
  256. if test "$host" = "$target"
  257. then
  258. # Rename VENDOR to 'cross'. If empty, 'cross-linux' is added
  259. case "${host%-*-*}" in
  260. *-*)
  261. host="$(echo "$host" | sed -e 's/-[^-]*/-cross/')"
  262. ;;
  263. *)
  264. host="$(echo "$host" | sed -e 's/-[^-]*/-cross-linux/')"
  265. ;;
  266. esac
  267. fi
  268. # Compose variables for the physical output,
  269. # printing some useful information
  270. crossdir=${output}/cross/${target}
  271. rootdir=${output}/stage${stage}
  272. printf "%s\n" \
  273. "BTCC: $BTCC" \
  274. "BTCXX: $BTCXX" \
  275. "BTCFLAGS: $BTCFLAGS" \
  276. "BTCXXFLAGS: $BTCXXFLAGS" \
  277. "BTLDFLAGS: $BTLDFLAGS" \
  278. "Host: $host" \
  279. "Target: $target" \
  280. "Output directory: $output" \
  281. "Cross directory: $crossdir" \
  282. "Root directory: $rootdir"
  283. # Remove write permission for group and other
  284. umask 022
  285. # Create required directories
  286. mkdir -p -- "$output" "$TMPDIR"
  287. chkstatus_or_exit
  288. # Set default PATH, propagate variables
  289. PATH="${crossdir}/bin:${PATH}"
  290. export PATH VENDOR TMPDIR
  291. # Main loop
  292. for file in "${CWD}/stages/${stage}"/${@:-??-*}
  293. do
  294. file="${file##*/}"
  295. if test ! -f "${CWD}/stages/${stage}/$file"
  296. then
  297. warn "${PROGRAM}: ${stage}/${file}: No such file or stage number"
  298. exit 1
  299. fi
  300. if test ! -x "${CWD}/stages/${stage}/$file"
  301. then
  302. warn "${PROGRAM}: ${stage}/${file}: File not executable, dodging"
  303. continue;
  304. fi
  305. #### Stage pre-settings
  306. # Create sysroot directory, recreating the symlink for the stage 0
  307. if test "$stage" = 0
  308. then
  309. mkdir -p -- "${crossdir}/$target" || chkstatus_or_exit
  310. if test ! -e "${crossdir}/${target}/usr"
  311. then
  312. ln -sf . "${crossdir}/${target}/usr" || chkstatus_or_exit
  313. fi
  314. # Ensure toolchain sanity for multilib purposes
  315. case $arch in
  316. aarch64* | arm* | i586 | *x32 | x86_64 | riscv* )
  317. if test ! -e "${crossdir}/lib" && test -n "$libSuffix"
  318. then
  319. ln -sf lib${libSuffix} "${crossdir}/lib" || chkstatus_or_exit
  320. fi
  321. ;;
  322. esac
  323. fi
  324. # Create "tools" directory, recreating the symlink for the stage 1
  325. if test "$stage" = 1
  326. then
  327. if test ! -d "${rootdir}/tools"
  328. then
  329. mkdir -p -- "${rootdir}/tools" || chkstatus_or_exit
  330. fi
  331. # Make required symlink
  332. ln -sf "${rootdir}/tools" / || chkstatus_or_exit
  333. fi
  334. echo "${PROGRAM}: Processing $file from stage $stage ..."
  335. # Set trap before to run the script in order to
  336. # catch the return status, exit code 2 if fails
  337. trap 'chkstatus_or_exit 2' EXIT HUP INT QUIT ABRT TERM
  338. # Exit immediately on any error
  339. set -e
  340. . "${CWD}/stages/${stage}/$file"
  341. # Deactivate shell option(s)
  342. set +e
  343. # Reset given signals
  344. trap - EXIT HUP INT QUIT ABRT TERM
  345. # Delete declared directories on the cleanup() function
  346. if test "$opt_keep" != opt_keep
  347. then
  348. if type cleanup 1> /dev/null 2> /dev/null
  349. then
  350. cleanup
  351. chkstatus_or_exit 2
  352. unset cleanup
  353. fi
  354. fi
  355. # Back to the current working directory
  356. cd -- "$CWD" || chkstatus_or_exit
  357. done