ext-toolchain.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. #!/usr/bin/env bash
  2. #
  3. # Script for various external toolchain tasks, refer to
  4. # the --help output for more information.
  5. #
  6. # Copyright (C) 2012 Jo-Philipp Wich <jo@mein.io>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. CC=""
  22. CXX=""
  23. CPP=""
  24. CFLAGS=""
  25. TOOLCHAIN="."
  26. LIBC_TYPE=""
  27. # Library specs
  28. LIB_SPECS="
  29. c: ld-* lib{anl,c,cidn,crypt,dl,m,nsl,nss_dns,nss_files,resolv,util}
  30. rt: librt-* librt
  31. pthread: libpthread-* libpthread
  32. stdcpp: libstdc++
  33. thread_db: libthread-db
  34. gcc: libgcc_s
  35. ssp: libssp
  36. gfortran: libgfortran
  37. gomp: libgomp
  38. atomic: libatomic
  39. quadmath: libquadmath
  40. asan: libasan
  41. tasan: libtsan
  42. lasan: liblsan
  43. ubasan: libubsan
  44. "
  45. # Binary specs
  46. BIN_SPECS="
  47. ldd: ldd
  48. ldconfig: ldconfig
  49. gdb: gdb
  50. gdbserver: gdbserver
  51. "
  52. OVERWRITE_CONFIG=""
  53. test_c() {
  54. cat <<-EOT | "${CC:-false}" $CFLAGS -o /dev/null -x c - 2>/dev/null
  55. #include <stdio.h>
  56. int main(int argc, char **argv)
  57. {
  58. printf("Hello, world!\n");
  59. return 0;
  60. }
  61. EOT
  62. }
  63. test_cxx() {
  64. cat <<-EOT | "${CXX:-false}" $CFLAGS -o /dev/null -x c++ - 2>/dev/null
  65. #include <iostream>
  66. using namespace std;
  67. int main()
  68. {
  69. cout << "Hello, world!" << endl;
  70. return 0;
  71. }
  72. EOT
  73. }
  74. test_softfloat() {
  75. cat <<-EOT | "$CC" $CFLAGS -msoft-float -o /dev/null -x c - 2>/dev/null
  76. int main(int argc, char **argv)
  77. {
  78. double a = 0.1;
  79. double b = 0.2;
  80. double c = (a + b) / (a * b);
  81. return 1;
  82. }
  83. EOT
  84. }
  85. test_uclibc() {
  86. local sysroot="$("$CC" $CFLAGS -print-sysroot 2>/dev/null)"
  87. if [ -d "${sysroot:-$TOOLCHAIN}" ]; then
  88. local lib
  89. for lib in "${sysroot:-$TOOLCHAIN}"/{lib,usr/lib,usr/local/lib}/ld*-uClibc*.so*; do
  90. if [ -f "$lib" ] && [ ! -h "$lib" ]; then
  91. return 0
  92. fi
  93. done
  94. fi
  95. return 1
  96. }
  97. test_feature() {
  98. local feature="$1"; shift
  99. # find compilers, libc type
  100. probe_cc
  101. probe_cxx
  102. probe_libc
  103. # common toolchain feature tests
  104. case "$feature" in
  105. c) test_c; return $? ;;
  106. c++) test_cxx; return $? ;;
  107. soft*) test_softfloat; return $? ;;
  108. esac
  109. # assume eglibc/glibc supports all libc features
  110. if [ "$LIBC_TYPE" != "uclibc" ]; then
  111. return 0
  112. fi
  113. # uclibc feature tests
  114. local inc
  115. local sysroot="$("$CC" "$@" -muclibc -print-sysroot 2>/dev/null)"
  116. for inc in "include" "usr/include" "usr/local/include"; do
  117. local conf="${sysroot:-$TOOLCHAIN}/$inc/bits/uClibc_config.h"
  118. if [ -f "$conf" ]; then
  119. case "$feature" in
  120. lfs) grep -q '__UCLIBC_HAS_LFS__ 1' "$conf"; return $?;;
  121. ipv6) grep -q '__UCLIBC_HAS_IPV6__ 1' "$conf"; return $?;;
  122. rpc) grep -q '__UCLIBC_HAS_RPC__ 1' "$conf"; return $?;;
  123. locale) grep -q '__UCLIBC_HAS_LOCALE__ 1' "$conf"; return $?;;
  124. wchar) grep -q '__UCLIBC_HAS_WCHAR__ 1' "$conf"; return $?;;
  125. threads) grep -q '__UCLIBC_HAS_THREADS__ 1' "$conf"; return $?;;
  126. esac
  127. fi
  128. done
  129. return 1
  130. }
  131. find_libs() {
  132. local spec="$(echo "$LIB_SPECS" | sed -ne "s#^[[:space:]]*$1:##ip")"
  133. if [ -n "$spec" ] && probe_cpp; then
  134. local libdir libdirs
  135. for libdir in $(
  136. "$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
  137. sed -ne 's#:# #g; s#^LIBRARY_PATH=##p'
  138. ); do
  139. if [ -d "$libdir" ]; then
  140. libdirs="$libdirs $(cd "$libdir"; pwd)/"
  141. fi
  142. done
  143. local pattern
  144. for pattern in $(eval echo $spec); do
  145. find $libdirs -name "$pattern.so*" | sort -u
  146. done
  147. return 0
  148. fi
  149. return 1
  150. }
  151. find_bins() {
  152. local spec="$(echo "$BIN_SPECS" | sed -ne "s#^[[:space:]]*$1:##ip")"
  153. if [ -n "$spec" ] && probe_cpp; then
  154. local sysroot="$("$CPP" -print-sysroot)"
  155. local bindir bindirs
  156. for bindir in $(
  157. echo "${sysroot:-$TOOLCHAIN}/bin";
  158. echo "${sysroot:-$TOOLCHAIN}/usr/bin";
  159. echo "${sysroot:-$TOOLCHAIN}/usr/local/bin";
  160. "$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
  161. sed -ne 's#:# #g; s#^COMPILER_PATH=##p'
  162. ); do
  163. if [ -d "$bindir" ]; then
  164. bindirs="$bindirs $(cd "$bindir"; pwd)/"
  165. fi
  166. done
  167. local pattern
  168. for pattern in $(eval echo $spec); do
  169. find $bindirs -name "$pattern" | sort -u
  170. done
  171. return 0
  172. fi
  173. return 1
  174. }
  175. wrap_bin_cc() {
  176. local out="$1"
  177. local bin="$2"
  178. echo '#!/bin/sh' > "$out"
  179. echo 'for arg in "$@"; do' >> "$out"
  180. echo ' case "$arg" in -l*|-L*|-shared|-static)' >> "$out"
  181. echo -n ' exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+' >> "$out"
  182. echo -n '-idirafter "$STAGING_DIR/usr/include" ' >> "$out"
  183. echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
  184. echo '-Wl,-rpath-link,"$STAGING_DIR/usr/lib"} "$@" ;;' >> "$out"
  185. echo ' esac' >> "$out"
  186. echo 'done' >> "$out"
  187. echo -n 'exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+' >> "$out"
  188. echo '-idirafter "$STAGING_DIR/usr/include"} "$@"' >> "$out"
  189. chmod +x "$out"
  190. }
  191. wrap_bin_ld() {
  192. local out="$1"
  193. local bin="$2"
  194. echo '#!/bin/sh' > "$out"
  195. echo -n 'exec "'"$bin"'" ${STAGING_DIR:+' >> "$out"
  196. echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
  197. echo '-rpath-link "$STAGING_DIR/usr/lib"} "$@"' >> "$out"
  198. chmod +x "$out"
  199. }
  200. wrap_bin_other() {
  201. local out="$1"
  202. local bin="$2"
  203. echo '#!/bin/sh' > "$out"
  204. echo 'exec "'"$bin"'" "$@"' >> "$out"
  205. chmod +x "$out"
  206. }
  207. wrap_bins() {
  208. if probe_cc; then
  209. mkdir -p "$1" || return 1
  210. local cmd
  211. for cmd in "${CC%-*}-"*; do
  212. if [ -x "$cmd" ]; then
  213. local out="$1/${cmd##*/}"
  214. local bin="$cmd"
  215. if [ -x "$out" ] && ! grep -q STAGING_DIR "$out"; then
  216. mv "$out" "$out.bin"
  217. bin='$(dirname "$0")/'"${out##*/}"'.bin'
  218. fi
  219. case "${cmd##*/}" in
  220. *-*cc|*-*cc-*|*-*++|*-*++-*|*-cpp)
  221. wrap_bin_cc "$out" "$bin"
  222. ;;
  223. *-ld)
  224. wrap_bin_ld "$out" "$bin"
  225. ;;
  226. *)
  227. wrap_bin_other "$out" "$bin"
  228. ;;
  229. esac
  230. fi
  231. done
  232. return 0
  233. fi
  234. return 1
  235. }
  236. print_config() {
  237. local mktarget="$1"
  238. local mksubtarget
  239. local target="$("$CC" $CFLAGS -dumpmachine)"
  240. local version="$("$CC" $CFLAGS -dumpversion)"
  241. local cpuarch="${target%%-*}"
  242. # get CC; strip version; strip gcc and add - suffix
  243. local prefix="${CC##*/}"; prefix="${prefix%-$version}"; prefix="${prefix%-*}-"
  244. local config="${0%/scripts/*}/.config"
  245. # if no target specified, print choice list and exit
  246. if [ -z "$mktarget" ]; then
  247. # prepare metadata
  248. if [ ! -f "${0%/scripts/*}/tmp/.targetinfo" ]; then
  249. "${0%/*}/scripts/config/mconf" prepare-tmpinfo
  250. fi
  251. local mktargets=$(
  252. sed -ne "
  253. /^Target: / { h };
  254. /^Target-Arch: $cpuarch\$/ { x; s#^Target: ##p }
  255. " "${0%/scripts/*}/tmp/.targetinfo" | sort -u
  256. )
  257. for mktarget in $mktargets; do
  258. case "$mktarget" in */*)
  259. mktargets=$(echo "$mktargets" | sed -e "/^${mktarget%/*}\$/d")
  260. esac
  261. done
  262. if [ -n "$mktargets" ]; then
  263. echo "Available targets:" >&2
  264. echo $mktargets >&2
  265. else
  266. echo -e "Could not find a suitable libreCMC target for " >&2
  267. echo -e "CPU architecture '$cpuarch' - you need to " >&2
  268. echo -e "define one first!" >&2
  269. fi
  270. return 1
  271. fi
  272. # bail out if there is a .config already
  273. if [ -f "$config" ]; then
  274. if [ "$OVERWRITE_CONFIG" == "" ]; then
  275. echo "There already is a .config file, refusing to overwrite!" >&2
  276. return 1
  277. else
  278. echo "There already is a .config file, trying to overwrite!"
  279. fi
  280. fi
  281. case "$mktarget" in */*)
  282. mksubtarget="${mktarget#*/}"
  283. mktarget="${mktarget%/*}"
  284. ;; esac
  285. if [ ! -f "$config" ]; then
  286. touch "$config"
  287. fi
  288. echo "CONFIG_TARGET_${mktarget}=y" >> "$config"
  289. if [ -n "$mksubtarget" ]; then
  290. echo "CONFIG_TARGET_${mktarget}_${mksubtarget}=y" >> "$config"
  291. fi
  292. if test_feature "softfloat"; then
  293. echo "CONFIG_SOFT_FLOAT=y" >> "$config"
  294. else
  295. echo "# CONFIG_SOFT_FLOAT is not set" >> "$config"
  296. fi
  297. if test_feature "ipv6"; then
  298. echo "CONFIG_IPV6=y" >> "$config"
  299. else
  300. echo "# CONFIG_IPV6 is not set" >> "$config"
  301. fi
  302. if test_feature "locale"; then
  303. echo "CONFIG_BUILD_NLS=y" >> "$config"
  304. else
  305. echo "# CONFIG_BUILD_NLS is not set" >> "$config"
  306. fi
  307. echo "CONFIG_DEVEL=y" >> "$config"
  308. echo "CONFIG_EXTERNAL_TOOLCHAIN=y" >> "$config"
  309. echo "CONFIG_TOOLCHAIN_ROOT=\"$TOOLCHAIN\"" >> "$config"
  310. echo "CONFIG_TOOLCHAIN_PREFIX=\"$prefix\"" >> "$config"
  311. echo "CONFIG_TARGET_NAME=\"$target\"" >> "$config"
  312. if [ -f "$config" ]; then
  313. sed -i '/CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_MUSL/d' "$config"
  314. sed -i '/CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_GLIBC/d' "$config"
  315. fi
  316. if [ "$LIBC_TYPE" == glibc ]; then
  317. echo "CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_GLIBC=y" >> "$config"
  318. elif [ "$LIBC_TYPE" == musl ]; then
  319. echo "CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_MUSL=y" >> "$config"
  320. else
  321. echo "Can't detect LIBC type. Aborting!" >&2
  322. return 1
  323. fi
  324. local lib
  325. for lib in C RT PTHREAD GCC STDCPP SSP GFORTRAN GOMP ATOMIC QUADMATH ASAN TSAN LSAN UBSAN; do
  326. local file
  327. local spec=""
  328. local llib="$(echo "$lib" | sed -e 's#.*#\L&#')"
  329. for file in $(find_libs "$lib"); do
  330. spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
  331. done
  332. if [ -n "$spec" ]; then
  333. echo "CONFIG_PACKAGE_lib${llib}=y" >> "$config"
  334. echo "CONFIG_LIB${lib}_FILE_SPEC=\"$spec\"" >> "$config"
  335. else
  336. echo "# CONFIG_PACKAGE_lib${llib} is not set" >> "$config"
  337. fi
  338. done
  339. local bin
  340. for bin in LDD LDCONFIG; do
  341. local file
  342. local spec=""
  343. local lbin="$(echo "$bin" | sed -e 's#.*#\L&#')"
  344. for file in $(find_bins "$bin"); do
  345. spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
  346. done
  347. if [ -n "$spec" ]; then
  348. echo "CONFIG_PACKAGE_${lbin}=y" >> "$config"
  349. echo "CONFIG_${bin}_FILE_SPEC=\"$spec\"" >> "$config"
  350. else
  351. echo "# CONFIG_PACKAGE_${lbin} is not set" >> "$config"
  352. fi
  353. done
  354. # inflate
  355. make -C "${0%/scripts/*}" defconfig
  356. return 0
  357. }
  358. probe_cc() {
  359. if [ -z "$CC" ]; then
  360. local bin
  361. for bin in "bin" "usr/bin" "usr/local/bin"; do
  362. local cmd
  363. for cmd in "$TOOLCHAIN/$bin/"*-*cc*; do
  364. if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
  365. CC="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
  366. return 0
  367. fi
  368. done
  369. done
  370. return 1
  371. fi
  372. return 0
  373. }
  374. probe_cxx() {
  375. if [ -z "$CXX" ]; then
  376. local bin
  377. for bin in "bin" "usr/bin" "usr/local/bin"; do
  378. local cmd
  379. for cmd in "$TOOLCHAIN/$bin/"*-*++*; do
  380. if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
  381. CXX="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
  382. return 0
  383. fi
  384. done
  385. done
  386. return 1
  387. fi
  388. return 0
  389. }
  390. probe_cpp() {
  391. if [ -z "$CPP" ]; then
  392. local bin
  393. for bin in "bin" "usr/bin" "usr/local/bin"; do
  394. local cmd
  395. for cmd in "$TOOLCHAIN/$bin/"*-cpp*; do
  396. if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
  397. CPP="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
  398. return 0
  399. fi
  400. done
  401. done
  402. return 1
  403. fi
  404. return 0
  405. }
  406. probe_libc() {
  407. if [ -f $TOOLCHAIN/info.mk ]; then
  408. LIBC_TYPE=$(grep LIBC_TYPE $TOOLCHAIN/info.mk | sed 's/LIBC_TYPE=//')
  409. return 0
  410. fi
  411. echo "Warning! Can't find info.mk, trying to detect with alternative way."
  412. if [ -z "$LIBC_TYPE" ]; then
  413. if test_uclibc; then
  414. LIBC_TYPE="uclibc"
  415. else
  416. LIBC_TYPE="glibc"
  417. fi
  418. fi
  419. return 0
  420. }
  421. while [ -n "$1" ]; do
  422. arg="$1"; shift
  423. case "$arg" in
  424. --toolchain)
  425. [ -d "$1" ] || {
  426. echo "Toolchain directory '$1' does not exist." >&2
  427. exit 1
  428. }
  429. TOOLCHAIN="$(cd "$1"; pwd)"; shift
  430. ;;
  431. --cflags)
  432. CFLAGS="${CFLAGS:+$CFLAGS }$1"; shift
  433. ;;
  434. --print-libc)
  435. if probe_cc; then
  436. probe_libc
  437. echo "$LIBC_TYPE"
  438. exit 0
  439. fi
  440. echo "No C compiler found in '$TOOLCHAIN'." >&2
  441. exit 1
  442. ;;
  443. --print-target)
  444. if probe_cc; then
  445. exec "$CC" $CFLAGS -dumpmachine
  446. fi
  447. echo "No C compiler found in '$TOOLCHAIN'." >&2
  448. exit 1
  449. ;;
  450. --print-bin)
  451. if [ -z "$1" ]; then
  452. echo "Available programs:" >&2
  453. echo $(echo "$BIN_SPECS" | sed -ne 's#:.*$##p') >&2
  454. exit 1
  455. fi
  456. find_bins "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-bin
  457. exit 0
  458. ;;
  459. --print-libs)
  460. if [ -z "$1" ]; then
  461. echo "Available libraries:" >&2
  462. echo $(echo "$LIB_SPECS" | sed -ne 's#:.*$##p') >&2
  463. exit 1
  464. fi
  465. find_libs "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-libs
  466. exit 0
  467. ;;
  468. --test)
  469. test_feature "$1"
  470. exit $?
  471. ;;
  472. --wrap)
  473. [ -n "$1" ] || exec "$0" --help
  474. wrap_bins "$1"
  475. exit $?
  476. ;;
  477. --overwrite-config)
  478. OVERWRITE_CONFIG=y
  479. ;;
  480. --config)
  481. if probe_cc; then
  482. probe_libc
  483. print_config "$1"
  484. exit $?
  485. fi
  486. echo "No C compiler found in '$TOOLCHAIN'." >&2
  487. exit 1
  488. ;;
  489. -h|--help)
  490. me="$(basename "$0")"
  491. echo -e "\nUsage:\n" >&2
  492. echo -e " $me --toolchain {directory} --print-libc" >&2
  493. echo -e " Print the libc implementation and exit.\n" >&2
  494. echo -e " $me --toolchain {directory} --print-target" >&2
  495. echo -e " Print the GNU target name and exit.\n" >&2
  496. echo -e " $me --toolchain {directory} --print-bin {program}" >&2
  497. echo -e " Print executables belonging to given program," >&2
  498. echo -e " omit program argument to get a list of names.\n" >&2
  499. echo -e " $me --toolchain {directory} --print-libs {library}" >&2
  500. echo -e " Print shared objects belonging to given library," >&2
  501. echo -e " omit library argument to get a list of names.\n" >&2
  502. echo -e " $me --toolchain {directory} --test {feature}" >&2
  503. echo -e " Test given feature, exit code indicates success." >&2
  504. echo -e " Possible features are 'c', 'c++', 'softfloat'," >&2
  505. echo -e " 'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and " >&2
  506. echo -e " 'threads'.\n" >&2
  507. echo -e " $me --toolchain {directory} --wrap {directory}" >&2
  508. echo -e " Create wrapper scripts for C and C++ compiler, " >&2
  509. echo -e " linker, assembler and other key executables in " >&2
  510. echo -e " the directory given with --wrap.\n" >&2
  511. echo -e " $me --toolchain {directory} --config {target}" >&2
  512. echo -e " Analyze the given toolchain and print a suitable" >&2
  513. echo -e " .config for the given target. Omit target " >&2
  514. echo -e " argument to get a list of names.\n" >&2
  515. echo -e " $me --help" >&2
  516. echo -e " Display this help text and exit.\n\n" >&2
  517. echo -e " Most commands also take a --cflags parameter which " >&2
  518. echo -e " is used to specify C flags to be passed to the " >&2
  519. echo -e " cross compiler when performing tests." >&2
  520. echo -e " This parameter may be repeated multiple times." >&2
  521. echo -e " Use --overwrite-config before --config to overwrite" >&2
  522. echo -e " an already present config with the required changes.">&2
  523. exit 1
  524. ;;
  525. *)
  526. echo "Unknown argument '$arg'" >&2
  527. exec $0 --help
  528. ;;
  529. esac
  530. done
  531. exec $0 --help