2
0

ext-toolchain.sh 14 KB

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