ext-toolchain.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. 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_bins() {
  167. if probe_cc; then
  168. mkdir -p "$1" || return 1
  169. local cmd
  170. for cmd in "${CC%-*}-"*; do
  171. if [ -x "$cmd" ]; then
  172. local out="$1/${cmd##*/}"
  173. echo '#!/bin/sh' > "$out"
  174. case "${cmd##*/}" in
  175. *-*cc|*-*cc-*|*-*++|*-*++-*|*-cpp)
  176. echo -n 'exec "'"$cmd"'" '"$CFLAGS"' ' >> "$out"
  177. echo -n '${STAGING_DIR:+-idirafter ' >> "$out"
  178. echo -n '"$STAGING_DIR/usr/include" ' >> "$out"
  179. echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
  180. echo -n '-Wl,-rpath-link,' >> "$out"
  181. echo '"$STAGING_DIR/usr/lib"} "$@"' >> "$out"
  182. ;;
  183. *-ld)
  184. echo -n 'exec "'"$cmd"'" ${STAGING_DIR:+' >> "$out"
  185. echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
  186. echo -n '-rpath-link ' >> "$out"
  187. echo '"$STAGING_DIR/usr/lib"} "$@"' >> "$out"
  188. ;;
  189. *)
  190. echo "exec '$cmd' \"\$@\"" >> "$out"
  191. ;;
  192. esac
  193. chmod +x "$out"
  194. fi
  195. done
  196. return 0
  197. fi
  198. return 1
  199. }
  200. print_config() {
  201. local mktarget="$1"
  202. local mksubtarget
  203. local target="$("$CC" $CFLAGS -dumpmachine)"
  204. local cpuarch="${target%%-*}"
  205. local prefix="${CC##*/}"; prefix="${prefix%-*}-"
  206. local config="${0%/scripts/*}/.config"
  207. # if no target specified, print choice list and exit
  208. if [ -z "$mktarget" ]; then
  209. # prepare metadata
  210. if [ ! -f "${0%/scripts/*}/tmp/.targetinfo" ]; then
  211. "${0%/*}/scripts/config/mconf" prepare-tmpinfo
  212. fi
  213. local mktargets=$(
  214. sed -ne "
  215. /^Target: / { h };
  216. /^Target-Arch: $cpuarch\$/ { x; s#^Target: ##p }
  217. " "${0%/scripts/*}/tmp/.targetinfo" | sort -u
  218. )
  219. for mktarget in $mktargets; do
  220. case "$mktarget" in */*)
  221. mktargets=$(echo "$mktargets" | sed -e "/^${mktarget%/*}\$/d")
  222. esac
  223. done
  224. if [ -n "$mktargets" ]; then
  225. echo "Available targets:" >&2
  226. echo $mktargets >&2
  227. else
  228. echo -e "Could not find a suitable OpenWrt target for " >&2
  229. echo -e "CPU architecture '$cpuarch' - you need to " >&2
  230. echo -e "define one first!" >&2
  231. fi
  232. return 1
  233. fi
  234. # bail out if there is a .config already
  235. if [ -f "${0%/scripts/*}/.config" ]; then
  236. echo "There already is a .config file, refusing to overwrite!" >&2
  237. return 1
  238. fi
  239. case "$mktarget" in */*)
  240. mksubtarget="${mktarget#*/}"
  241. mktarget="${mktarget%/*}"
  242. ;; esac
  243. echo "CONFIG_TARGET_${mktarget}=y" > "$config"
  244. if [ -n "$mksubtarget" ]; then
  245. echo "CONFIG_TARGET_${mktarget}_${mksubtarget}=y" >> "$config"
  246. fi
  247. if test_feature "softfloat"; then
  248. echo "CONFIG_SOFT_FLOAT=y" >> "$config"
  249. else
  250. echo "# CONFIG_SOFT_FLOAT is not set" >> "$config"
  251. fi
  252. if test_feature "ipv6"; then
  253. echo "CONFIG_IPV6=y" >> "$config"
  254. else
  255. echo "# CONFIG_IPV6 is not set" >> "$config"
  256. fi
  257. if test_feature "locale"; then
  258. echo "CONFIG_NLS=y" >> "$config"
  259. else
  260. echo "# CONFIG_NLS is not set" >> "$config"
  261. fi
  262. echo "CONFIG_DEVEL=y" >> "$config"
  263. echo "CONFIG_EXTERNAL_TOOLCHAIN=y" >> "$config"
  264. echo "CONFIG_TOOLCHAIN_ROOT=\"$TOOLCHAIN\"" >> "$config"
  265. echo "CONFIG_TOOLCHAIN_PREFIX=\"$prefix\"" >> "$config"
  266. echo "CONFIG_TARGET_NAME=\"$target\"" >> "$config"
  267. local lib
  268. for lib in C RT PTHREAD GCC STDCPP SSP GFORTRAN; do
  269. local file
  270. local spec=""
  271. local llib="$(echo "$lib" | sed -e 's#.*#\L&#')"
  272. for file in $(find_libs "$lib"); do
  273. spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
  274. done
  275. if [ -n "$spec" ]; then
  276. echo "CONFIG_PACKAGE_lib${llib}=y" >> "$config"
  277. echo "CONFIG_LIB${lib}_FILE_SPEC=\"$spec\"" >> "$config"
  278. else
  279. echo "# CONFIG_PACKAGE_lib${llib} is not set" >> "$config"
  280. fi
  281. done
  282. local bin
  283. for bin in LDD LDCONFIG; do
  284. local file
  285. local spec=""
  286. local lbin="$(echo "$bin" | sed -e 's#.*#\L&#')"
  287. for file in $(find_bins "$bin"); do
  288. spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
  289. done
  290. if [ -n "$spec" ]; then
  291. echo "CONFIG_PACKAGE_${lbin}=y" >> "$config"
  292. echo "CONFIG_${bin}_FILE_SPEC=\"$spec\"" >> "$config"
  293. else
  294. echo "# CONFIG_PACKAGE_${lbin} is not set" >> "$config"
  295. fi
  296. done
  297. # inflate
  298. make -C "${0%/scripts/*}" defconfig
  299. return 0
  300. }
  301. probe_cc() {
  302. if [ -z "$CC" ]; then
  303. local bin
  304. for bin in "bin" "usr/bin" "usr/local/bin"; do
  305. local cmd
  306. for cmd in "$TOOLCHAIN/$bin/"*-*cc*; do
  307. if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
  308. CC="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
  309. return 0
  310. fi
  311. done
  312. done
  313. return 1
  314. fi
  315. return 0
  316. }
  317. probe_cxx() {
  318. if [ -z "$CXX" ]; then
  319. local bin
  320. for bin in "bin" "usr/bin" "usr/local/bin"; do
  321. local cmd
  322. for cmd in "$TOOLCHAIN/$bin/"*-*++*; do
  323. if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
  324. CXX="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
  325. return 0
  326. fi
  327. done
  328. done
  329. return 1
  330. fi
  331. return 0
  332. }
  333. probe_cpp() {
  334. if [ -z "$CPP" ]; then
  335. local bin
  336. for bin in "bin" "usr/bin" "usr/local/bin"; do
  337. local cmd
  338. for cmd in "$TOOLCHAIN/$bin/"*-cpp*; do
  339. if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
  340. CPP="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
  341. return 0
  342. fi
  343. done
  344. done
  345. return 1
  346. fi
  347. return 0
  348. }
  349. probe_libc() {
  350. if [ -z "$LIBC_TYPE" ]; then
  351. if test_uclibc; then
  352. LIBC_TYPE="uclibc"
  353. else
  354. LIBC_TYPE="glibc"
  355. fi
  356. fi
  357. return 0
  358. }
  359. while [ -n "$1" ]; do
  360. arg="$1"; shift
  361. case "$arg" in
  362. --toolchain)
  363. [ -d "$1" ] || {
  364. echo "Toolchain directory '$1' does not exist." >&2
  365. exit 1
  366. }
  367. TOOLCHAIN="$(cd "$1"; pwd)"; shift
  368. ;;
  369. --cflags)
  370. CFLAGS="${CFLAGS:+$CFLAGS }$1"; shift
  371. ;;
  372. --print-libc)
  373. if probe_cc; then
  374. probe_libc
  375. echo "$LIBC_TYPE"
  376. exit 0
  377. fi
  378. echo "No C compiler found in '$TOOLCHAIN'." >&2
  379. exit 1
  380. ;;
  381. --print-target)
  382. if probe_cc; then
  383. exec "$CC" $CFLAGS -dumpmachine
  384. fi
  385. echo "No C compiler found in '$TOOLCHAIN'." >&2
  386. exit 1
  387. ;;
  388. --print-bin)
  389. if [ -z "$1" ]; then
  390. echo "Available programs:" >&2
  391. echo $(echo "$BIN_SPECS" | sed -ne 's#:.*$##p') >&2
  392. exit 1
  393. fi
  394. find_bins "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-bin
  395. exit 0
  396. ;;
  397. --print-libs)
  398. if [ -z "$1" ]; then
  399. echo "Available libraries:" >&2
  400. echo $(echo "$LIB_SPECS" | sed -ne 's#:.*$##p') >&2
  401. exit 1
  402. fi
  403. find_libs "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-libs
  404. exit 0
  405. ;;
  406. --test)
  407. test_feature "$1"
  408. exit $?
  409. ;;
  410. --wrap)
  411. [ -n "$1" ] || exec "$0" --help
  412. wrap_bins "$1"
  413. exit $?
  414. ;;
  415. --config)
  416. if probe_cc; then
  417. print_config "$1"
  418. exit $?
  419. fi
  420. echo "No C compiler found in '$TOOLCHAIN'." >&2
  421. exit 1
  422. ;;
  423. -h|--help)
  424. me="$(basename "$0")"
  425. echo -e "\nUsage:\n" >&2
  426. echo -e " $me --toolchain {directory} --print-libc" >&2
  427. echo -e " Print the libc implementation and exit.\n" >&2
  428. echo -e " $me --toolchain {directory} --print-target" >&2
  429. echo -e " Print the GNU target name and exit.\n" >&2
  430. echo -e " $me --toolchain {directory} --print-bin {program}" >&2
  431. echo -e " Print executables belonging to given program," >&2
  432. echo -e " omit program argument to get a list of names.\n" >&2
  433. echo -e " $me --toolchain {directory} --print-libs {library}" >&2
  434. echo -e " Print shared objects belonging to given library," >&2
  435. echo -e " omit library argument to get a list of names.\n" >&2
  436. echo -e " $me --toolchain {directory} --test {feature}" >&2
  437. echo -e " Test given feature, exit code indicates success." >&2
  438. echo -e " Possible features are 'c', 'c++', 'softfloat'," >&2
  439. echo -e " 'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and " >&2
  440. echo -e " 'threads'.\n" >&2
  441. echo -e " $me --toolchain {directory} --wrap {directory}" >&2
  442. echo -e " Create wrapper scripts for C and C++ compiler, " >&2
  443. echo -e " linker, assembler and other key executables in " >&2
  444. echo -e " the directory given with --wrap.\n" >&2
  445. echo -e " $me --toolchain {directory} --config {target}" >&2
  446. echo -e " Analyze the given toolchain and print a suitable" >&2
  447. echo -e " .config for the given target. Omit target " >&2
  448. echo -e " argument to get a list of names." >&2
  449. echo -e " suitable to prime .config with.\n" >&2
  450. echo -e " $me --help" >&2
  451. echo -e " Display this help text and exit.\n\n" >&2
  452. echo -e " Most commands also take a --cflags parameter which " >&2
  453. echo -e " is used to specify C flags to be passed to the " >&2
  454. echo -e " cross compiler when performing tests." >&2
  455. echo -e " This paremter may be repeated multiple times." >&2
  456. exit 1
  457. ;;
  458. *)
  459. echo "Unknown argument '$arg'" >&2
  460. exec $0 --help
  461. ;;
  462. esac
  463. done
  464. exec $0 --help