ext-toolchain.sh 15 KB

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