ext-toolchain.sh 14 KB

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