ext-toolchain.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 <[email protected]>
  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 OpenWrt 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 [ "$LIBC_TYPE" != glibc ]; then
  307. echo "CONFIG_TOOLCHAIN_LIBC=\"$LIBC_TYPE\"" >> "$config"
  308. fi
  309. local lib
  310. for lib in C RT PTHREAD GCC STDCPP SSP GFORTRAN GOMP; do
  311. local file
  312. local spec=""
  313. local llib="$(echo "$lib" | sed -e 's#.*#\L&#')"
  314. for file in $(find_libs "$lib"); do
  315. spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
  316. done
  317. if [ -n "$spec" ]; then
  318. echo "CONFIG_PACKAGE_lib${llib}=y" >> "$config"
  319. echo "CONFIG_LIB${lib}_FILE_SPEC=\"$spec\"" >> "$config"
  320. else
  321. echo "# CONFIG_PACKAGE_lib${llib} is not set" >> "$config"
  322. fi
  323. done
  324. local bin
  325. for bin in LDD LDCONFIG; do
  326. local file
  327. local spec=""
  328. local lbin="$(echo "$bin" | sed -e 's#.*#\L&#')"
  329. for file in $(find_bins "$bin"); do
  330. spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
  331. done
  332. if [ -n "$spec" ]; then
  333. echo "CONFIG_PACKAGE_${lbin}=y" >> "$config"
  334. echo "CONFIG_${bin}_FILE_SPEC=\"$spec\"" >> "$config"
  335. else
  336. echo "# CONFIG_PACKAGE_${lbin} is not set" >> "$config"
  337. fi
  338. done
  339. # inflate
  340. make -C "${0%/scripts/*}" defconfig
  341. return 0
  342. }
  343. probe_cc() {
  344. if [ -z "$CC" ]; then
  345. local bin
  346. for bin in "bin" "usr/bin" "usr/local/bin"; do
  347. local cmd
  348. for cmd in "$TOOLCHAIN/$bin/"*-*cc*; do
  349. if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
  350. CC="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
  351. return 0
  352. fi
  353. done
  354. done
  355. return 1
  356. fi
  357. return 0
  358. }
  359. probe_cxx() {
  360. if [ -z "$CXX" ]; then
  361. local bin
  362. for bin in "bin" "usr/bin" "usr/local/bin"; do
  363. local cmd
  364. for cmd in "$TOOLCHAIN/$bin/"*-*++*; do
  365. if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
  366. CXX="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
  367. return 0
  368. fi
  369. done
  370. done
  371. return 1
  372. fi
  373. return 0
  374. }
  375. probe_cpp() {
  376. if [ -z "$CPP" ]; then
  377. local bin
  378. for bin in "bin" "usr/bin" "usr/local/bin"; do
  379. local cmd
  380. for cmd in "$TOOLCHAIN/$bin/"*-cpp*; do
  381. if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
  382. CPP="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
  383. return 0
  384. fi
  385. done
  386. done
  387. return 1
  388. fi
  389. return 0
  390. }
  391. probe_libc() {
  392. if [ -z "$LIBC_TYPE" ]; then
  393. if test_uclibc; then
  394. LIBC_TYPE="uclibc"
  395. else
  396. LIBC_TYPE="glibc"
  397. fi
  398. fi
  399. return 0
  400. }
  401. while [ -n "$1" ]; do
  402. arg="$1"; shift
  403. case "$arg" in
  404. --toolchain)
  405. [ -d "$1" ] || {
  406. echo "Toolchain directory '$1' does not exist." >&2
  407. exit 1
  408. }
  409. TOOLCHAIN="$(cd "$1"; pwd)"; shift
  410. ;;
  411. --cflags)
  412. CFLAGS="${CFLAGS:+$CFLAGS }$1"; shift
  413. ;;
  414. --print-libc)
  415. if probe_cc; then
  416. probe_libc
  417. echo "$LIBC_TYPE"
  418. exit 0
  419. fi
  420. echo "No C compiler found in '$TOOLCHAIN'." >&2
  421. exit 1
  422. ;;
  423. --print-target)
  424. if probe_cc; then
  425. exec "$CC" $CFLAGS -dumpmachine
  426. fi
  427. echo "No C compiler found in '$TOOLCHAIN'." >&2
  428. exit 1
  429. ;;
  430. --print-bin)
  431. if [ -z "$1" ]; then
  432. echo "Available programs:" >&2
  433. echo $(echo "$BIN_SPECS" | sed -ne 's#:.*$##p') >&2
  434. exit 1
  435. fi
  436. find_bins "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-bin
  437. exit 0
  438. ;;
  439. --print-libs)
  440. if [ -z "$1" ]; then
  441. echo "Available libraries:" >&2
  442. echo $(echo "$LIB_SPECS" | sed -ne 's#:.*$##p') >&2
  443. exit 1
  444. fi
  445. find_libs "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-libs
  446. exit 0
  447. ;;
  448. --test)
  449. test_feature "$1"
  450. exit $?
  451. ;;
  452. --wrap)
  453. [ -n "$1" ] || exec "$0" --help
  454. wrap_bins "$1"
  455. exit $?
  456. ;;
  457. --overwrite-config)
  458. OVERWRITE_CONFIG=y
  459. ;;
  460. --config)
  461. if probe_cc; then
  462. print_config "$1"
  463. exit $?
  464. fi
  465. echo "No C compiler found in '$TOOLCHAIN'." >&2
  466. exit 1
  467. ;;
  468. -h|--help)
  469. me="$(basename "$0")"
  470. echo -e "\nUsage:\n" >&2
  471. echo -e " $me --toolchain {directory} --print-libc" >&2
  472. echo -e " Print the libc implementation and exit.\n" >&2
  473. echo -e " $me --toolchain {directory} --print-target" >&2
  474. echo -e " Print the GNU target name and exit.\n" >&2
  475. echo -e " $me --toolchain {directory} --print-bin {program}" >&2
  476. echo -e " Print executables belonging to given program," >&2
  477. echo -e " omit program argument to get a list of names.\n" >&2
  478. echo -e " $me --toolchain {directory} --print-libs {library}" >&2
  479. echo -e " Print shared objects belonging to given library," >&2
  480. echo -e " omit library argument to get a list of names.\n" >&2
  481. echo -e " $me --toolchain {directory} --test {feature}" >&2
  482. echo -e " Test given feature, exit code indicates success." >&2
  483. echo -e " Possible features are 'c', 'c++', 'softfloat'," >&2
  484. echo -e " 'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and " >&2
  485. echo -e " 'threads'.\n" >&2
  486. echo -e " $me --toolchain {directory} --wrap {directory}" >&2
  487. echo -e " Create wrapper scripts for C and C++ compiler, " >&2
  488. echo -e " linker, assembler and other key executables in " >&2
  489. echo -e " the directory given with --wrap.\n" >&2
  490. echo -e " $me --toolchain {directory} --config {target}" >&2
  491. echo -e " Analyze the given toolchain and print a suitable" >&2
  492. echo -e " .config for the given target. Omit target " >&2
  493. echo -e " argument to get a list of names.\n" >&2
  494. echo -e " $me --help" >&2
  495. echo -e " Display this help text and exit.\n\n" >&2
  496. echo -e " Most commands also take a --cflags parameter which " >&2
  497. echo -e " is used to specify C flags to be passed to the " >&2
  498. echo -e " cross compiler when performing tests." >&2
  499. echo -e " This parameter may be repeated multiple times." >&2
  500. echo -e " Use --overwrite-config before --config to overwrite" >&2
  501. echo -e " an already present config with the required changes.">&2
  502. exit 1
  503. ;;
  504. *)
  505. echo "Unknown argument '$arg'" >&2
  506. exec $0 --help
  507. ;;
  508. esac
  509. done
  510. exec $0 --help