ext-toolchain.sh 14 KB

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