PageRenderTime 2621ms CodeModel.GetById 41ms RepoModel.GetById 1ms app.codeStats 0ms

/build/tools/prebuilt-common.sh

https://gitlab.com/ftasdelen/platform_ndk
Shell | 1555 lines | 1339 code | 58 blank | 158 comment | 54 complexity | 1a305c4f802448091ace139d11f9ac1c MD5 | raw file
  1. # Common functions for all prebuilt-related scripts
  2. # This is included/sourced by other scripts
  3. #
  4. # ensure stable sort order
  5. export LC_ALL=C
  6. # NDK_BUILDTOOLS_PATH should point to the directory containing
  7. # this script. If it is not defined, assume that this is one of
  8. # the scripts in the same directory that sourced this file.
  9. #
  10. if [ -z "$NDK_BUILDTOOLS_PATH" ]; then
  11. NDK_BUILDTOOLS_PATH=$(dirname $0)
  12. if [ ! -f "$NDK_BUILDTOOLS_PATH/prebuilt-common.sh" ]; then
  13. echo "INTERNAL ERROR: Please define NDK_BUILDTOOLS_PATH to point to \$NDK/build/tools"
  14. exit 1
  15. fi
  16. fi
  17. # Warn if /bin/sh isn't bash.
  18. if [ -z "$BASH_VERSION" ] ; then
  19. echo "WARNING: The shell running this script isn't bash. Although we try to avoid bashism in scripts, things can happen."
  20. fi
  21. NDK_BUILDTOOLS_ABSPATH=$(cd $NDK_BUILDTOOLS_PATH && pwd)
  22. . $NDK_BUILDTOOLS_PATH/ndk-common.sh
  23. . $NDK_BUILDTOOLS_PATH/dev-defaults.sh
  24. # Given an input string of the form <foo>-<bar>-<version>, where
  25. # <version> can be <major>.<minor>, extract <major>
  26. extract_version ()
  27. {
  28. echo $1 | tr '-' '\n' | tail -1
  29. }
  30. # $1: versioned name (e.g. arm-linux-androideabi-4.8)
  31. # Out: major version (e.g. 4)
  32. #
  33. # Examples: arm-linux-androideabi-4.4.3 -> 4
  34. # gmp-0.81 -> 0
  35. #
  36. extract_major_version ()
  37. {
  38. local RET=$(extract_version $1 | cut -d . -f 1)
  39. RET=${RET:-0}
  40. echo $RET
  41. }
  42. # Same as extract_major_version, but for the minor version number
  43. # $1: versioned named
  44. # Out: minor version
  45. #
  46. extract_minor_version ()
  47. {
  48. local RET=$(extract_version $1 | cut -d . -f 2)
  49. RET=${RET:-0}
  50. echo $RET
  51. }
  52. # Compare two version numbers and only succeeds if the first one is
  53. # greater than or equal to the second one.
  54. #
  55. # $1: first version (e.g. 4.9)
  56. # $2: second version (e.g. 4.8)
  57. #
  58. # Example: version_is_at_least 4.9 4.8 --> success
  59. #
  60. version_is_at_least ()
  61. {
  62. local A_MAJOR A_MINOR B_MAJOR B_MINOR
  63. A_MAJOR=$(extract_major_version $1)
  64. B_MAJOR=$(extract_major_version $2)
  65. if [ $A_MAJOR -lt $B_MAJOR ]; then
  66. return 1
  67. elif [ $A_MAJOR -gt $B_MAJOR ]; then
  68. return 0
  69. fi
  70. # We have A_MAJOR == B_MAJOR here
  71. A_MINOR=$(extract_minor_version $1)
  72. B_MINOR=$(extract_minor_version $2)
  73. if [ $A_MINOR -lt $B_MINOR ]; then
  74. return 1
  75. else
  76. return 0
  77. fi
  78. }
  79. #====================================================
  80. #
  81. # UTILITY FUNCTIONS
  82. #
  83. #====================================================
  84. # Return the maximum length of a series of strings
  85. #
  86. # Usage: len=`max_length <string1> <string2> ...`
  87. #
  88. max_length ()
  89. {
  90. echo "$@" | tr ' ' '\n' | awk 'BEGIN {max=0} {len=length($1); if (len > max) max=len} END {print max}'
  91. }
  92. # Translate dashes to underscores
  93. # Usage: str=`dashes_to_underscores <values>`
  94. dashes_to_underscores ()
  95. {
  96. echo "$@" | tr '-' '_'
  97. }
  98. # Translate underscores to dashes
  99. # Usage: str=`underscores_to_dashes <values>`
  100. underscores_to_dashes ()
  101. {
  102. echo "$@" | tr '_' '-'
  103. }
  104. # Translate commas to spaces
  105. # Usage: str=`commas_to_spaces <list>`
  106. commas_to_spaces ()
  107. {
  108. echo "$@" | tr ',' ' '
  109. }
  110. # Translate spaces to commas
  111. # Usage: list=`spaces_to_commas <string>`
  112. spaces_to_commas ()
  113. {
  114. echo "$@" | tr ' ' ','
  115. }
  116. # Remove trailing path of a path
  117. # $1: path
  118. remove_trailing_slash () {
  119. echo ${1%%/}
  120. }
  121. # Reverse a file path directory
  122. # foo -> .
  123. # foo/bar -> ..
  124. # foo/bar/zoo -> ../..
  125. reverse_path ()
  126. {
  127. local path cur item
  128. path=${1%%/} # remove trailing slash
  129. cur="."
  130. if [ "$path" != "." ] ; then
  131. for item in $(echo "$path" | tr '/' ' '); do
  132. cur="../$cur"
  133. done
  134. fi
  135. echo ${cur%%/.}
  136. }
  137. # test_reverse_path ()
  138. # {
  139. # rr=`reverse_path $1`
  140. # if [ "$rr" != "$2" ] ; then
  141. # echo "ERROR: reverse_path '$1' -> '$rr' (expected '$2')"
  142. # fi
  143. # }
  144. #
  145. # test_reverse_path . .
  146. # test_reverse_path ./ .
  147. # test_reverse_path foo ..
  148. # test_reverse_path foo/ ..
  149. # test_reverse_path foo/bar ../..
  150. # test_reverse_path foo/bar/ ../..
  151. # test_reverse_path foo/bar/zoo ../../..
  152. # test_reverse_path foo/bar/zoo/ ../../..
  153. # Sort a space-separated list and remove duplicates
  154. # $1+: slist
  155. # Output: new slist
  156. sort_uniq ()
  157. {
  158. local RET
  159. RET=$(echo "$@" | tr ' ' '\n' | sort -u)
  160. echo $RET
  161. }
  162. # Return the list of all regular files under a given directory
  163. # $1: Directory path
  164. # Output: list of files, relative to $1
  165. list_files_under ()
  166. {
  167. if [ -d "$1" ]; then
  168. (cd $1 && find . -type f | sed -e "s!./!!" | sort -u)
  169. else
  170. echo ""
  171. fi
  172. }
  173. # Returns all words in text that do not match any of the pattern
  174. # $1: pattern
  175. # $2: text
  176. filter_out ()
  177. {
  178. local PATTERN="$1"
  179. local TEXT="$2"
  180. for pat in $PATTERN; do
  181. pat=$"${pat//\//\\/}"
  182. TEXT=$(echo $TEXT | sed -e 's/'$pat' //g' -e 's/'$pat'$//g')
  183. done
  184. echo $TEXT
  185. }
  186. # Assign a value to a variable
  187. # $1: Variable name
  188. # $2: Value
  189. var_assign ()
  190. {
  191. eval $1=\"$2\"
  192. }
  193. #====================================================
  194. #
  195. # OPTION PROCESSING
  196. #
  197. #====================================================
  198. # We recognize the following option formats:
  199. #
  200. # -f
  201. # --flag
  202. #
  203. # -s<value>
  204. # --setting=<value>
  205. #
  206. # NOTE: We translate '-' into '_' when storing the options in global variables
  207. #
  208. OPTIONS=""
  209. OPTION_FLAGS=""
  210. OPTION_SETTINGS=""
  211. # Set a given option attribute
  212. # $1: option name
  213. # $2: option attribute
  214. # $3: attribute value
  215. #
  216. option_set_attr ()
  217. {
  218. eval OPTIONS_$1_$2=\"$3\"
  219. }
  220. # Get a given option attribute
  221. # $1: option name
  222. # $2: option attribute
  223. #
  224. option_get_attr ()
  225. {
  226. echo `var_value OPTIONS_$1_$2`
  227. }
  228. # Register a new option
  229. # $1: option
  230. # $2: small abstract for the option
  231. # $3: optional. default value
  232. #
  233. register_option_internal ()
  234. {
  235. optlabel=
  236. optname=
  237. optvalue=
  238. opttype=
  239. while [ -n "1" ] ; do
  240. # Check for something like --setting=<value>
  241. echo "$1" | grep -q -E -e '^--[^=]+=<.+>$'
  242. if [ $? = 0 ] ; then
  243. optlabel=`expr -- "$1" : '\(--[^=]*\)=.*'`
  244. optvalue=`expr -- "$1" : '--[^=]*=\(<.*>\)'`
  245. opttype="long_setting"
  246. break
  247. fi
  248. # Check for something like --flag
  249. echo "$1" | grep -q -E -e '^--[^=]+$'
  250. if [ $? = 0 ] ; then
  251. optlabel="$1"
  252. opttype="long_flag"
  253. break
  254. fi
  255. # Check for something like -f<value>
  256. echo "$1" | grep -q -E -e '^-[A-Za-z0-9]<.+>$'
  257. if [ $? = 0 ] ; then
  258. optlabel=`expr -- "$1" : '\(-.\).*'`
  259. optvalue=`expr -- "$1" : '-.\(<.+>\)'`
  260. opttype="short_setting"
  261. break
  262. fi
  263. # Check for something like -f
  264. echo "$1" | grep -q -E -e '^-.$'
  265. if [ $? = 0 ] ; then
  266. optlabel="$1"
  267. opttype="short_flag"
  268. break
  269. fi
  270. echo "ERROR: Invalid option format: $1"
  271. echo " Check register_option call"
  272. exit 1
  273. done
  274. log "new option: type='$opttype' name='$optlabel' value='$optvalue'"
  275. optname=`dashes_to_underscores $optlabel`
  276. OPTIONS="$OPTIONS $optname"
  277. OPTIONS_TEXT="$OPTIONS_TEXT $1"
  278. option_set_attr $optname label "$optlabel"
  279. option_set_attr $optname otype "$opttype"
  280. option_set_attr $optname value "$optvalue"
  281. option_set_attr $optname text "$1"
  282. option_set_attr $optname abstract "$2"
  283. option_set_attr $optname default "$3"
  284. }
  285. # Register a new option with a function callback.
  286. #
  287. # $1: option
  288. # $2: name of function that will be called when the option is parsed
  289. # $3: small abstract for the option
  290. # $4: optional. default value
  291. #
  292. register_option ()
  293. {
  294. local optname optvalue opttype optlabel
  295. register_option_internal "$1" "$3" "$4"
  296. option_set_attr $optname funcname "$2"
  297. }
  298. # Register a new option with a variable store
  299. #
  300. # $1: option
  301. # $2: name of variable that will be set by this option
  302. # $3: small abstract for the option
  303. #
  304. # NOTE: The current value of $2 is used as the default
  305. #
  306. register_var_option ()
  307. {
  308. local optname optvalue opttype optlabel
  309. register_option_internal "$1" "$3" "`var_value $2`"
  310. option_set_attr $optname varname "$2"
  311. }
  312. MINGW=no
  313. DARWIN=no
  314. do_mingw_option ()
  315. {
  316. if [ "$DARWIN" = "yes" ]; then
  317. echo "Can not have both --mingw and --darwin"
  318. exit 1
  319. fi
  320. MINGW=yes;
  321. }
  322. do_darwin_option ()
  323. {
  324. if [ "$MINGW" = "yes" ]; then
  325. echo "Can not have both --mingw and --darwin"
  326. exit 1
  327. fi
  328. DARWIN=yes;
  329. }
  330. register_canadian_option ()
  331. {
  332. if [ "$HOST_OS" = "linux" ] ; then
  333. register_option "--mingw" do_mingw_option "Generate windows binaries on Linux."
  334. register_option "--darwin" do_darwin_option "Generate darwin binaries on Linux."
  335. fi
  336. }
  337. TRY64=no
  338. do_try64_option () { TRY64=yes; }
  339. register_try64_option ()
  340. {
  341. register_option "--try-64" do_try64_option "Generate 64-bit only binaries."
  342. }
  343. register_jobs_option ()
  344. {
  345. NUM_JOBS=$BUILD_NUM_CPUS
  346. register_var_option "-j<number>" NUM_JOBS "Use <number> parallel build jobs"
  347. }
  348. # Print the help, including a list of registered options for this program
  349. # Note: Assumes PROGRAM_PARAMETERS and PROGRAM_DESCRIPTION exist and
  350. # correspond to the parameters list and the program description
  351. #
  352. print_help ()
  353. {
  354. local opt text abstract default
  355. echo "Usage: $PROGNAME [options] $PROGRAM_PARAMETERS"
  356. echo ""
  357. if [ -n "$PROGRAM_DESCRIPTION" ] ; then
  358. echo "$PROGRAM_DESCRIPTION"
  359. echo ""
  360. fi
  361. echo "Valid options (defaults are in brackets):"
  362. echo ""
  363. maxw=`max_length "$OPTIONS_TEXT"`
  364. AWK_SCRIPT=`echo "{ printf \"%-${maxw}s\", \\$1 }"`
  365. for opt in $OPTIONS; do
  366. text=`option_get_attr $opt text | awk "$AWK_SCRIPT"`
  367. abstract=`option_get_attr $opt abstract`
  368. default=`option_get_attr $opt default`
  369. if [ -n "$default" ] ; then
  370. echo " $text $abstract [$default]"
  371. else
  372. echo " $text $abstract"
  373. fi
  374. done
  375. echo ""
  376. }
  377. option_panic_no_args ()
  378. {
  379. echo "ERROR: Option '$1' does not take arguments. See --help for usage."
  380. exit 1
  381. }
  382. option_panic_missing_arg ()
  383. {
  384. echo "ERROR: Option '$1' requires an argument. See --help for usage."
  385. exit 1
  386. }
  387. extract_parameters ()
  388. {
  389. local opt optname otype value name fin funcname
  390. PARAMETERS=""
  391. while [ -n "$1" ] ; do
  392. # If the parameter does not begin with a dash
  393. # it is not an option.
  394. param=`expr -- "$1" : '^\([^\-].*\)$'`
  395. if [ -n "$param" ] ; then
  396. if [ -z "$PARAMETERS" ] ; then
  397. PARAMETERS="$1"
  398. else
  399. PARAMETERS="$PARAMETERS $1"
  400. fi
  401. shift
  402. continue
  403. fi
  404. while [ -n "1" ] ; do
  405. # Try to match a long setting, i.e. --option=value
  406. opt=`expr -- "$1" : '^\(--[^=]*\)=.*$'`
  407. if [ -n "$opt" ] ; then
  408. otype="long_setting"
  409. value=`expr -- "$1" : '^--[^=]*=\(.*\)$'`
  410. break
  411. fi
  412. # Try to match a long flag, i.e. --option
  413. opt=`expr -- "$1" : '^\(--.*\)$'`
  414. if [ -n "$opt" ] ; then
  415. otype="long_flag"
  416. value="yes"
  417. break
  418. fi
  419. # Try to match a short setting, i.e. -o<value>
  420. opt=`expr -- "$1" : '^\(-[A-Za-z0-9]\)..*$'`
  421. if [ -n "$opt" ] ; then
  422. otype="short_setting"
  423. value=`expr -- "$1" : '^-.\(.*\)$'`
  424. break
  425. fi
  426. # Try to match a short flag, i.e. -o
  427. opt=`expr -- "$1" : '^\(-.\)$'`
  428. if [ -n "$opt" ] ; then
  429. otype="short_flag"
  430. value="yes"
  431. break
  432. fi
  433. echo "ERROR: Unknown option '$1'. Use --help for list of valid values."
  434. exit 1
  435. done
  436. #echo "Found opt='$opt' otype='$otype' value='$value'"
  437. name=`dashes_to_underscores $opt`
  438. found=0
  439. for xopt in $OPTIONS; do
  440. if [ "$name" != "$xopt" ] ; then
  441. continue
  442. fi
  443. # Check that the type is correct here
  444. #
  445. # This also allows us to handle -o <value> as -o<value>
  446. #
  447. xotype=`option_get_attr $name otype`
  448. if [ "$otype" != "$xotype" ] ; then
  449. case "$xotype" in
  450. "short_flag")
  451. option_panic_no_args $opt
  452. ;;
  453. "short_setting")
  454. if [ -z "$2" ] ; then
  455. option_panic_missing_arg $opt
  456. fi
  457. value="$2"
  458. shift
  459. ;;
  460. "long_flag")
  461. option_panic_no_args $opt
  462. ;;
  463. "long_setting")
  464. option_panic_missing_arg $opt
  465. ;;
  466. esac
  467. fi
  468. found=1
  469. break
  470. break
  471. done
  472. if [ "$found" = "0" ] ; then
  473. echo "ERROR: Unknown option '$opt'. See --help for usage."
  474. exit 1
  475. fi
  476. # Set variable or launch option-specific function.
  477. varname=`option_get_attr $name varname`
  478. if [ -n "$varname" ] ; then
  479. eval ${varname}=\"$value\"
  480. else
  481. eval `option_get_attr $name funcname` \"$value\"
  482. fi
  483. shift
  484. done
  485. }
  486. do_option_help ()
  487. {
  488. print_help
  489. exit 0
  490. }
  491. VERBOSE=no
  492. do_option_verbose ()
  493. {
  494. VERBOSE=yes
  495. }
  496. DRYRUN=no
  497. do_option_dryrun ()
  498. {
  499. DRYRUN=yes
  500. }
  501. register_option "--help" do_option_help "Print this help."
  502. register_option "--verbose" do_option_verbose "Enable verbose mode."
  503. register_option "--dryrun" do_option_dryrun "Set to dryrun mode."
  504. #====================================================
  505. #
  506. # TOOLCHAIN AND ABI PROCESSING
  507. #
  508. #====================================================
  509. # Determine optional variable value
  510. # $1: final variable name
  511. # $2: option variable name
  512. # $3: small description for the option
  513. fix_option ()
  514. {
  515. if [ -n "$2" ] ; then
  516. eval $1="$2"
  517. log "Using specific $3: $2"
  518. else
  519. log "Using default $3: `var_value $1`"
  520. fi
  521. }
  522. # If SYSROOT is empty, check that $1/$2 contains a sysroot
  523. # and set the variable to it.
  524. #
  525. # $1: sysroot path
  526. # $2: platform/arch suffix
  527. check_sysroot ()
  528. {
  529. if [ -z "$SYSROOT" ] ; then
  530. log "Probing directory for sysroot: $1/$2"
  531. if [ -d $1/$2 ] ; then
  532. SYSROOT=$1/$2
  533. fi
  534. fi
  535. }
  536. # Determine sysroot
  537. # $1: Option value (or empty)
  538. #
  539. fix_sysroot ()
  540. {
  541. if [ -n "$1" ] ; then
  542. eval SYSROOT="$1"
  543. log "Using specified sysroot: $1"
  544. else
  545. SYSROOT_SUFFIX=$PLATFORM/arch-$ARCH
  546. SYSROOT=
  547. check_sysroot $ANDROID_BUILD_TOP/prebuilts/ndk/current/platforms $SYSROOT_SUFFIX
  548. check_sysroot $ANDROID_NDK_ROOT/platforms $SYSROOT_SUFFIX
  549. check_sysroot `dirname $ANDROID_NDK_ROOT`/development/ndk/platforms $SYSROOT_SUFFIX
  550. if [ -z "$SYSROOT" ] ; then
  551. echo "ERROR: Could not find NDK sysroot path for $SYSROOT_SUFFIX."
  552. echo " Use --sysroot=<path> to specify one."
  553. exit 1
  554. fi
  555. fi
  556. if [ ! -f $SYSROOT/usr/include/stdlib.h ] ; then
  557. echo "ERROR: Invalid sysroot path: $SYSROOT"
  558. echo " Use --sysroot=<path> to indicate a valid one."
  559. exit 1
  560. fi
  561. }
  562. # Check for the availability of a compatibility SDK in Darwin
  563. # this can be used to generate binaries compatible with either Tiger or
  564. # Leopard.
  565. #
  566. # $1: SDK root path
  567. # $2: Optional MacOS X minimum version (e.g. 10.5)
  568. DARWIN_MINVER=10.6
  569. check_darwin_sdk ()
  570. {
  571. local MACSDK="$1"
  572. local MINVER=$2
  573. if [ -z "$MINVER" ] ; then
  574. # expect SDK root path ended up with either MacOSX##.#.sdk or MacOSX##.#u.sdk
  575. MINVER=${MACSDK##*MacOSX}
  576. MINVER=${MINVER%%.sdk*}
  577. if [ "$MINVER" = "10.4u" ]; then
  578. MINVER=10.4
  579. fi
  580. fi
  581. if [ -d "$MACSDK" ] ; then
  582. HOST_CFLAGS=$HOST_CFLAGS" -isysroot $MACSDK -mmacosx-version-min=$MINVER -DMAXOSX_DEPLOYEMENT_TARGET=$MINVER"
  583. HOST_LDFLAGS=$HOST_LDFLAGS" -Wl,-syslibroot,$MACSDK -mmacosx-version-min=$MINVER"
  584. DARWIN_MINVER=$MINVER
  585. return 0 # success
  586. fi
  587. return 1
  588. }
  589. # Probe Darwin SDK in specified diectory $DARWIN_SYSROOT, or
  590. # /Developer/SDKs/MacOSX10.6.sdk
  591. #
  592. probe_darwin_sdk ()
  593. {
  594. if [ -n "$DARWIN_SYSROOT" ]; then
  595. if check_darwin_sdk "$DARWIN_SYSROOT"; then
  596. log "Use darwin sysroot $DARWIN_SYSROOT"
  597. else
  598. echo "darwin sysroot $DARWIN_SYSROOT is not valid"
  599. exit 1
  600. fi
  601. elif check_darwin_sdk /Developer/SDKs/MacOSX10.6.sdk 10.6; then
  602. log "Generating Snow Leopard-compatible binaries!"
  603. else
  604. local version=`sw_vers -productVersion`
  605. log "Generating $version-compatible binaries!"
  606. fi
  607. }
  608. handle_canadian_build ()
  609. {
  610. HOST_EXE=
  611. if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ] ; then
  612. case $HOST_TAG in
  613. linux-*)
  614. ;;
  615. *)
  616. echo "ERROR: Can only enable --mingw or --darwin on Linux platforms !"
  617. exit 1
  618. ;;
  619. esac
  620. if [ "$MINGW" = "yes" ] ; then
  621. # NOTE: Use x86_64-pc-mingw32msvc or i586-pc-mingw32msvc because wrappers are generated
  622. # using these names
  623. if [ "$TRY64" = "yes" ]; then
  624. ABI_CONFIGURE_HOST=x86_64-pc-mingw32msvc
  625. HOST_TAG=windows-x86_64
  626. else
  627. ABI_CONFIGURE_HOST=i586-pc-mingw32msvc
  628. HOST_TAG=windows
  629. fi
  630. HOST_OS=windows
  631. HOST_EXE=.exe
  632. else
  633. if [ "$TRY64" = "yes" ]; then
  634. ABI_CONFIGURE_HOST=x86_64-apple-darwin
  635. HOST_TAG=darwin-x86_64
  636. else
  637. ABI_CONFIGURE_HOST=i686-apple-darwin
  638. HOST_TAG=darwin-x86
  639. fi
  640. HOST_OS=darwin
  641. fi
  642. fi
  643. }
  644. # Find mingw toolchain
  645. #
  646. # Set MINGW_GCC to the found mingw toolchain
  647. #
  648. find_mingw_toolchain ()
  649. {
  650. if [ "$DEBIAN_NAME" -a "$BINPREFIX" -a "$MINGW_GCC" ]; then
  651. return
  652. fi
  653. # IMPORTANT NOTE: binutils 2.21 requires a cross toolchain named
  654. # i585-pc-mingw32msvc-gcc, or it will fail its configure step late
  655. # in the toolchain build. Note that binutils 2.19 can build properly
  656. # with i585-mingw32mvsc-gcc, which is the name used by the 'mingw32'
  657. # toolchain install on Debian/Ubuntu.
  658. #
  659. # To solve this dilemma, we create a wrapper toolchain named
  660. # i586-pc-mingw32msvc-gcc that really calls i586-mingw32msvc-gcc,
  661. # this works with all versions of binutils.
  662. #
  663. # We apply the same logic to the 64-bit Windows cross-toolchain
  664. #
  665. # Fedora note: On Fedora it's x86_64-w64-mingw32- or i686-w64-mingw32-
  666. # On older Fedora it's 32-bit only and called i686-pc-mingw32-
  667. # so we just add more prefixes to the list to check.
  668. if [ "$HOST_ARCH" = "x86_64" -a "$TRY64" = "yes" ]; then
  669. BINPREFIX=x86_64-pc-mingw32msvc-
  670. BINPREFIXLST="x86_64-pc-mingw32msvc- x86_64-w64-mingw32- amd64-mingw32msvc-"
  671. DEBIAN_NAME=mingw-w64
  672. else
  673. # we are trying 32 bit anyway, so forcing it to avoid build issues
  674. force_32bit_binaries
  675. BINPREFIX=i586-pc-mingw32msvc-
  676. BINPREFIXLST="i586-pc-mingw32msvc- i686-pc-mingw32- i586-mingw32msvc- i686-w64-mingw32-"
  677. DEBIAN_NAME=mingw-w64
  678. fi
  679. # Scan $BINPREFIXLST list to find installed mingw toolchain. It will be
  680. # wrapped later with $BINPREFIX.
  681. for i in $BINPREFIXLST; do
  682. find_program MINGW_GCC ${i}gcc
  683. if [ -n "$MINGW_GCC" ]; then
  684. dump "Found mingw toolchain: $MINGW_GCC"
  685. break
  686. fi
  687. done
  688. }
  689. # Check there is a working cross-toolchain installed.
  690. #
  691. # $1: install directory for mingw/darwin wrapper toolchain
  692. #
  693. prepare_canadian_toolchain ()
  694. {
  695. if [ "$MINGW" != "yes" -a "$DARWIN" != "yes" ]; then
  696. return
  697. fi
  698. CROSS_GCC=
  699. if [ "$MINGW" = "yes" ]; then
  700. find_mingw_toolchain
  701. if [ -z "$MINGW_GCC" ]; then
  702. echo "ERROR: Could not find in your PATH any of:"
  703. for i in $BINPREFIXLST; do echo " ${i}gcc"; done
  704. echo "Please install the corresponding cross-toolchain and re-run this script"
  705. echo "TIP: On Debian or Ubuntu, try: sudo apt-get install $DEBIAN_NAME"
  706. exit 1
  707. fi
  708. CROSS_GCC=$MINGW_GCC
  709. else
  710. if [ -z "$DARWIN_TOOLCHAIN" ]; then
  711. echo "Please set DARWIN_TOOLCHAIN to darwin cross-toolchain"
  712. exit 1
  713. fi
  714. if [ ! -f "${DARWIN_TOOLCHAIN}-gcc" ]; then
  715. echo "darwin cross-toolchain $DARWIN_TOOLCHAIN-gcc doesn't exist"
  716. exit 1
  717. fi
  718. if [ "$HOST_ARCH" = "x86_64" -a "$TRY64" = "yes" ]; then
  719. BINPREFIX=x86_64-apple-darwin-
  720. DEBIAN_NAME=darwin64
  721. HOST_CFLAGS=$HOST_CFLAGS" -m64"
  722. else
  723. force_32bit_binaries
  724. BINPREFIX=i686-apple-darwin-
  725. DEBIAN_NAME=darwin32
  726. HOST_CFLAGS=$HOST_CFLAGS" -m32"
  727. fi
  728. CROSS_GCC=${DARWIN_TOOLCHAIN}-gcc
  729. probe_darwin_sdk
  730. fi
  731. # Create a wrapper toolchain, and prepend its dir to our PATH
  732. CROSS_WRAP_DIR="$1"/$DEBIAN_NAME-wrapper
  733. rm -rf "$CROSS_WRAP_DIR"
  734. mkdir -p "$CROSS_WRAP_DIR"
  735. if [ "$DARWIN" = "yes" ] ; then
  736. cat > "$CROSS_WRAP_DIR/sw_vers" <<EOF
  737. #!/bin/sh
  738. # Tiny utility for the real sw_vers some Makefiles need
  739. case \$1 in
  740. -productVersion)
  741. echo $DARWIN_MINVER
  742. ;;
  743. *)
  744. echo "ERROR: Unknown switch \$1"
  745. exit 1
  746. esac
  747. EOF
  748. chmod 0755 "$CROSS_WRAP_DIR/sw_vers"
  749. fi
  750. DST_PREFIX=${CROSS_GCC%gcc}
  751. if [ "$NDK_CCACHE" ]; then
  752. DST_PREFIX="$NDK_CCACHE $DST_PREFIX"
  753. fi
  754. $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=$BINPREFIX --dst-prefix="$DST_PREFIX" "$CROSS_WRAP_DIR" \
  755. --cflags="$HOST_CFLAGS" --cxxflags="$HOST_CFLAGS" --ldflags="$HOST_LDFLAGS"
  756. # generate wrappers for BUILD toolchain
  757. # this is required for mingw/darwin build to avoid tools canadian cross configuration issues
  758. # 32-bit BUILD toolchain
  759. LEGACY_TOOLCHAIN_DIR="$ANDROID_BUILD_TOP/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.11-4.8"
  760. $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=i386-linux-gnu- \
  761. --cflags="-m32" --cxxflags="-m32" --ldflags="-m elf_i386" --asflags="--32" \
  762. --dst-prefix="$LEGACY_TOOLCHAIN_DIR/bin/x86_64-linux-" "$CROSS_WRAP_DIR"
  763. $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=i386-pc-linux-gnu- \
  764. --cflags="-m32" --cxxflags="-m32" --ldflags="-m elf_i386" --asflags="--32" \
  765. --dst-prefix="$LEGACY_TOOLCHAIN_DIR/bin/x86_64-linux-" "$CROSS_WRAP_DIR"
  766. # 64-bit BUILD toolchain. libbfd is still built in 32-bit.
  767. $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=x86_64-linux-gnu- \
  768. --dst-prefix="$LEGACY_TOOLCHAIN_DIR/bin/x86_64-linux-" "$CROSS_WRAP_DIR"
  769. $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=x86_64-pc-linux-gnu- \
  770. --dst-prefix="$LEGACY_TOOLCHAIN_DIR/bin/x86_64-linux-" "$CROSS_WRAP_DIR"
  771. fail_panic "Could not create $DEBIAN_NAME wrapper toolchain in $CROSS_WRAP_DIR"
  772. export PATH=$CROSS_WRAP_DIR:$PATH
  773. dump "Using $DEBIAN_NAME wrapper: $CROSS_WRAP_DIR/${BINPREFIX}gcc"
  774. }
  775. handle_host ()
  776. {
  777. if [ "$TRY64" != "yes" ]; then
  778. force_32bit_binaries # to modify HOST_TAG and others
  779. HOST_BITS=32
  780. fi
  781. handle_canadian_build
  782. }
  783. setup_ccache ()
  784. {
  785. # Support for ccache compilation
  786. # We can't use this here when building Windows/darwin binaries on Linux with
  787. # binutils 2.21, because defining CC/CXX in the environment makes the
  788. # configure script fail later
  789. #
  790. if [ "$NDK_CCACHE" -a "$MINGW" != "yes" -a "$DARWIN" != "yes" ]; then
  791. NDK_CCACHE_CC=$CC
  792. NDK_CCACHE_CXX=$CXX
  793. # Unfortunately, we can just do CC="$NDK_CCACHE $CC" because some
  794. # configure scripts are not capable of dealing with this properly
  795. # E.g. the ones used to rebuild the GCC toolchain from scratch.
  796. # So instead, use a wrapper script
  797. CC=$NDK_BUILDTOOLS_ABSPATH/ndk-ccache-gcc.sh
  798. CXX=$NDK_BUILDTOOLS_ABSPATH/ndk-ccache-g++.sh
  799. export NDK_CCACHE_CC NDK_CCACHE_CXX
  800. log "Using ccache compilation"
  801. log "NDK_CCACHE_CC=$NDK_CCACHE_CC"
  802. log "NDK_CCACHE_CXX=$NDK_CCACHE_CXX"
  803. fi
  804. }
  805. prepare_common_build ()
  806. {
  807. if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ]; then
  808. if [ "$TRY64" = "yes" ]; then
  809. HOST_BITS=64
  810. else
  811. HOST_BITS=32
  812. fi
  813. if [ "$MINGW" = "yes" ]; then
  814. log "Generating $HOST_BITS-bit Windows binaries"
  815. else
  816. log "Generating $HOST_BITS-bit Darwin binaries"
  817. fi
  818. # Do *not* set CC and CXX when building the Windows/Darwin binaries in canadian build.
  819. # Otherwise, the GCC configure/build script will mess that Canadian cross
  820. # build in weird ways. Instead we rely on the toolchain detected or generated
  821. # previously in prepare_canadian_toolchain.
  822. unset CC CXX
  823. return
  824. fi
  825. # On Linux, detect our legacy-compatible toolchain when in the Android
  826. # source tree, and use it to force the generation of glibc-2.7 compatible
  827. # binaries.
  828. #
  829. # We only do this if the CC variable is not defined to a given value
  830. if [ -z "$CC" ]; then
  831. LEGACY_TOOLCHAIN_DIR=
  832. if [ "$HOST_OS" = "linux" ]; then
  833. LEGACY_TOOLCHAIN_DIR="$ANDROID_BUILD_TOP/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.11-4.8/bin"
  834. LEGACY_TOOLCHAIN_PREFIX="$LEGACY_TOOLCHAIN_DIR/x86_64-linux-"
  835. elif [ "$HOST_OS" = "darwin" ]; then
  836. LEGACY_TOOLCHAIN_DIR="$ANDROID_BUILD_TOP/prebuilts/gcc/darwin-x86/host/i686-apple-darwin-4.2.1/bin"
  837. LEGACY_TOOLCHAIN_PREFIX="$LEGACY_TOOLCHAIN_DIR/i686-apple-darwin10-"
  838. fi
  839. if [ -d "$LEGACY_TOOLCHAIN_DIR" ] ; then
  840. log "Forcing generation of $HOST_OS binaries with legacy toolchain"
  841. CC="${LEGACY_TOOLCHAIN_PREFIX}gcc"
  842. CXX="${LEGACY_TOOLCHAIN_PREFIX}g++"
  843. fi
  844. fi
  845. CC=${CC:-gcc}
  846. CXX=${CXX:-g++}
  847. STRIP=${STRIP:-strip}
  848. case $HOST_TAG in
  849. darwin-*)
  850. probe_darwin_sdk
  851. ;;
  852. esac
  853. # Force generation of 32-bit binaries on 64-bit systems.
  854. # We used to test the value of $HOST_TAG for *-x86_64, but this is
  855. # not sufficient on certain systems.
  856. #
  857. # For example, Snow Leopard can be booted with a 32-bit kernel, running
  858. # a 64-bit userland, with a compiler that generates 64-bit binaries by
  859. # default *even* though "gcc -v" will report --target=i686-apple-darwin10!
  860. #
  861. # So know, simply probe for the size of void* by performing a small runtime
  862. # compilation test.
  863. #
  864. cat > $TMPC <<EOF
  865. /* this test should fail if the compiler generates 64-bit machine code */
  866. int test_array[1-2*(sizeof(void*) != 4)];
  867. EOF
  868. log_n "Checking whether the compiler generates 32-bit binaries..."
  869. log $CC $HOST_CFLAGS -c -o $TMPO $TMPC
  870. $NDK_CCACHE $CC $HOST_CFLAGS -c -o $TMPO $TMPC >$TMPL 2>&1
  871. if [ $? != 0 ] ; then
  872. log "no"
  873. if [ "$TRY64" != "yes" ]; then
  874. # NOTE: We need to modify the definitions of CC and CXX directly
  875. # here. Just changing the value of CFLAGS / HOST_CFLAGS
  876. # will not work well with the GCC toolchain scripts.
  877. CC="$CC -m32"
  878. CXX="$CXX -m32"
  879. fi
  880. else
  881. log "yes"
  882. if [ "$TRY64" = "yes" ]; then
  883. CC="$CC -m64"
  884. CXX="$CXX -m64"
  885. fi
  886. fi
  887. if [ "$TRY64" = "yes" ]; then
  888. HOST_BITS=64
  889. else
  890. force_32bit_binaries # to modify HOST_TAG and others
  891. HOST_BITS=32
  892. fi
  893. }
  894. prepare_host_build ()
  895. {
  896. prepare_common_build
  897. # Now deal with mingw or darwin
  898. if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ]; then
  899. handle_canadian_build
  900. CC=$ABI_CONFIGURE_HOST-gcc
  901. CXX=$ABI_CONFIGURE_HOST-g++
  902. CPP=$ABI_CONFIGURE_HOST-cpp
  903. LD=$ABI_CONFIGURE_HOST-ld
  904. AR=$ABI_CONFIGURE_HOST-ar
  905. AS=$ABI_CONFIGURE_HOST-as
  906. RANLIB=$ABI_CONFIGURE_HOST-ranlib
  907. STRIP=$ABI_CONFIGURE_HOST-strip
  908. export CC CXX CPP LD AR AS RANLIB STRIP
  909. fi
  910. setup_ccache
  911. }
  912. prepare_abi_configure_build ()
  913. {
  914. # detect build tag
  915. case $HOST_TAG in
  916. linux-x86)
  917. ABI_CONFIGURE_BUILD=i386-linux-gnu
  918. ;;
  919. linux-x86_64)
  920. ABI_CONFIGURE_BUILD=x86_64-linux-gnu
  921. ;;
  922. darwin-x86)
  923. ABI_CONFIGURE_BUILD=i686-apple-darwin
  924. ;;
  925. darwin-x86_64)
  926. ABI_CONFIGURE_BUILD=x86_64-apple-darwin
  927. ;;
  928. windows)
  929. ABI_CONFIGURE_BUILD=i686-pc-cygwin
  930. ;;
  931. *)
  932. echo "ERROR: Unsupported HOST_TAG: $HOST_TAG"
  933. echo "Please update 'prepare_host_flags' in build/tools/prebuilt-common.sh"
  934. ;;
  935. esac
  936. }
  937. prepare_target_build ()
  938. {
  939. prepare_abi_configure_build
  940. # By default, assume host == build
  941. ABI_CONFIGURE_HOST="$ABI_CONFIGURE_BUILD"
  942. prepare_common_build
  943. HOST_GMP_ABI=$HOST_BITS
  944. # Now handle the --mingw/--darwin flag
  945. if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ] ; then
  946. handle_canadian_build
  947. STRIP=$ABI_CONFIGURE_HOST-strip
  948. if [ "$MINGW" = "yes" ] ; then
  949. # It turns out that we need to undefine this to be able to
  950. # perform a canadian-cross build with mingw. Otherwise, the
  951. # GMP configure scripts will not be called with the right options
  952. HOST_GMP_ABI=
  953. fi
  954. fi
  955. setup_ccache
  956. }
  957. # $1: Toolchain name
  958. #
  959. parse_toolchain_name ()
  960. {
  961. TOOLCHAIN=$1
  962. if [ -z "$TOOLCHAIN" ] ; then
  963. echo "ERROR: Missing toolchain name!"
  964. exit 1
  965. fi
  966. ABI_CFLAGS_FOR_TARGET=
  967. ABI_CXXFLAGS_FOR_TARGET=
  968. # Determine ABI based on toolchain name
  969. #
  970. case "$TOOLCHAIN" in
  971. arm-linux-androideabi-*)
  972. ARCH="arm"
  973. ABI="armeabi"
  974. ABI_CONFIGURE_TARGET="arm-linux-androideabi"
  975. ABI_CONFIGURE_EXTRA_FLAGS="--with-arch=armv5te"
  976. ;;
  977. arm-eabi-*)
  978. ARCH="arm"
  979. ABI="armeabi"
  980. ABI_CONFIGURE_TARGET="arm-eabi"
  981. ABI_CONFIGURE_EXTRA_FLAGS="--with-arch=armv5te --disable-gold --disable-libgomp"
  982. ;;
  983. aarch64-linux-android-*)
  984. ARCH="arm64"
  985. ABI="arm64-v8a"
  986. ABI_CONFIGURE_TARGET="aarch64-linux-android"
  987. ;;
  988. x86-*)
  989. ARCH="x86"
  990. ABI=$ARCH
  991. ABI_INSTALL_NAME="x86"
  992. ABI_CONFIGURE_TARGET="i686-linux-android"
  993. # Enable C++ exceptions, RTTI and GNU libstdc++ at the same time
  994. # You can't really build these separately at the moment.
  995. ABI_CFLAGS_FOR_TARGET="-fPIC"
  996. ;;
  997. x86_64-*)
  998. ARCH="x86_64"
  999. ABI=$ARCH
  1000. ABI_INSTALL_NAME="x86_64"
  1001. ABI_CONFIGURE_TARGET="x86_64-linux-android"
  1002. # Enable C++ exceptions, RTTI and GNU libstdc++ at the same time
  1003. # You can't really build these separately at the moment.
  1004. ABI_CFLAGS_FOR_TARGET="-fPIC"
  1005. ;;
  1006. mipsel*)
  1007. ARCH="mips"
  1008. ABI=$ARCH
  1009. ABI_INSTALL_NAME="mips"
  1010. ABI_CONFIGURE_TARGET="mipsel-linux-android"
  1011. # Set default to mips32
  1012. ABI_CONFIGURE_EXTRA_FLAGS="--with-arch=mips32"
  1013. # Enable C++ exceptions, RTTI and GNU libstdc++ at the same time
  1014. # You can't really build these separately at the moment.
  1015. # Add -fpic, because MIPS NDK will need to link .a into .so.
  1016. ABI_CFLAGS_FOR_TARGET="-fexceptions -fpic"
  1017. ABI_CXXFLAGS_FOR_TARGET="-frtti -fpic"
  1018. # Add --disable-fixed-point to disable fixed-point support
  1019. ABI_CONFIGURE_EXTRA_FLAGS="$ABI_CONFIGURE_EXTRA_FLAGS --disable-fixed-point"
  1020. ;;
  1021. mips64el*)
  1022. ARCH="mips64"
  1023. ABI=$ARCH
  1024. ABI_INSTALL_NAME="mips64"
  1025. ABI_CONFIGURE_TARGET="mips64el-linux-android"
  1026. # Set default to mips64r6
  1027. ABI_CONFIGURE_EXTRA_FLAGS="--with-arch=mips64r6"
  1028. # Enable C++ exceptions, RTTI and GNU libstdc++ at the same time
  1029. # You can't really build these separately at the moment.
  1030. # Add -fpic, because MIPS NDK will need to link .a into .so.
  1031. ABI_CFLAGS_FOR_TARGET="-fexceptions -fpic"
  1032. ABI_CXXFLAGS_FOR_TARGET="-frtti -fpic"
  1033. # Add --disable-fixed-point to disable fixed-point support
  1034. ABI_CONFIGURE_EXTRA_FLAGS="$ABI_CONFIGURE_EXTRA_FLAGS --disable-fixed-point"
  1035. ;;
  1036. * )
  1037. echo "Invalid toolchain specified. Expected (arm-linux-androideabi-*|arm-eabi-*|x86-*|mipsel*|mips64el*)"
  1038. echo ""
  1039. print_help
  1040. exit 1
  1041. ;;
  1042. esac
  1043. log "Targetting CPU: $ARCH"
  1044. GCC_VERSION=`expr -- "$TOOLCHAIN" : '.*-\([0-9x\.]*\)'`
  1045. log "Using GCC version: $GCC_VERSION"
  1046. # Determine --host value when building gdbserver
  1047. case "$TOOLCHAIN" in
  1048. arm-*)
  1049. GDBSERVER_HOST=arm-eabi-linux
  1050. GDBSERVER_CFLAGS="-fno-short-enums"
  1051. GDBSERVER_LDFLAGS=
  1052. ;;
  1053. aarch64-*)
  1054. GDBSERVER_HOST=aarch64-eabi-linux
  1055. GDBSERVER_CFLAGS="-fno-short-enums -DUAPI_HEADERS"
  1056. GDBSERVER_LDFLAGS=
  1057. ;;
  1058. x86-*)
  1059. GDBSERVER_HOST=i686-linux-android
  1060. GDBSERVER_CFLAGS=
  1061. GDBSERVER_LDFLAGS=
  1062. ;;
  1063. x86_64-*)
  1064. GDBSERVER_HOST=x86_64-linux-android
  1065. GDBSERVER_CFLAGS=-DUAPI_HEADERS
  1066. GDBSERVER_LDFLAGS=
  1067. ;;
  1068. mipsel-*)
  1069. GDBSERVER_HOST=mipsel-linux-android
  1070. GDBSERVER_CFLAGS=
  1071. GDBSERVER_LDFLAGS=
  1072. ;;
  1073. mips64el-*)
  1074. GDBSERVER_HOST=mips64el-linux-android
  1075. GDBSERVER_CFLAGS=-DUAPI_HEADERS
  1076. GDBSERVER_LDFLAGS=
  1077. ;;
  1078. *)
  1079. echo "Unknown TOOLCHAIN=$TOOLCHAIN"
  1080. exit
  1081. esac
  1082. }
  1083. # Return the host "tag" used to identify prebuilt host binaries.
  1084. # NOTE: Handles the case where '$MINGW = true' or '$DARWIN = true'
  1085. # For now, valid values are: linux-x86, darwin-x86 and windows
  1086. get_prebuilt_host_tag ()
  1087. {
  1088. local RET=$HOST_TAG
  1089. if [ "$MINGW" = "yes" ]; then
  1090. if [ "$TRY64" = "no" ]; then
  1091. RET=windows
  1092. else
  1093. RET=windows-x86_64
  1094. fi
  1095. fi
  1096. if [ "$DARWIN" = "yes" ]; then
  1097. RET=darwin-x86_64 # let the following handles 32-bit case
  1098. fi
  1099. case $RET in
  1100. linux-*)
  1101. RET=linux-x86_64
  1102. ;;
  1103. darwin-*)
  1104. RET=darwin-x86_64
  1105. ;;
  1106. esac
  1107. echo $RET
  1108. }
  1109. # Return the executable suffix corresponding to host executables
  1110. get_prebuilt_host_exe_ext ()
  1111. {
  1112. if [ "$MINGW" = "yes" ]; then
  1113. echo ".exe"
  1114. else
  1115. echo ""
  1116. fi
  1117. }
  1118. # Get library suffix for given ABI
  1119. # $1: ABI
  1120. # Return: .so or .bc
  1121. get_lib_suffix_for_abi ()
  1122. {
  1123. local ABI=$1
  1124. echo ".so"
  1125. }
  1126. # Convert an ABI name into an Architecture name
  1127. # $1: ABI name
  1128. # Result: Arch name
  1129. convert_abi_to_arch ()
  1130. {
  1131. local RET
  1132. local ABI=$1
  1133. case $ABI in
  1134. armeabi|armeabi-v7a)
  1135. RET=arm
  1136. ;;
  1137. x86|mips|x86_64|mips64)
  1138. RET=$ABI
  1139. ;;
  1140. mips32r6)
  1141. RET=mips
  1142. ;;
  1143. arm64-v8a)
  1144. RET=arm64
  1145. ;;
  1146. *)
  1147. >&2 echo "ERROR: Unsupported ABI name: $ABI, use one of: armeabi, armeabi-v7a, x86, mips, arm64-v8a, x86_64 or mips64"
  1148. exit 1
  1149. ;;
  1150. esac
  1151. echo "$RET"
  1152. }
  1153. # Take architecture name as input, and output the list of corresponding ABIs
  1154. # Inverse for convert_abi_to_arch
  1155. # $1: ARCH name
  1156. # Out: ABI names list (comma-separated)
  1157. convert_arch_to_abi ()
  1158. {
  1159. local RET
  1160. local ARCH=$1
  1161. case $ARCH in
  1162. arm)
  1163. RET=armeabi,armeabi-v7a
  1164. ;;
  1165. x86|x86_64|mips|mips64)
  1166. RET=$ARCH
  1167. ;;
  1168. arm64)
  1169. RET=arm64-v8a
  1170. ;;
  1171. *)
  1172. >&2 echo "ERROR: Unsupported ARCH name: $ARCH, use one of: arm, x86, mips"
  1173. exit 1
  1174. ;;
  1175. esac
  1176. echo "$RET"
  1177. }
  1178. # Take a list of architecture names as input, and output the list of corresponding ABIs
  1179. # $1: ARCH names list (separated by spaces or commas)
  1180. # Out: ABI names list (comma-separated)
  1181. convert_archs_to_abis ()
  1182. {
  1183. local RET
  1184. for ARCH in $(commas_to_spaces $@); do
  1185. ABI=$(convert_arch_to_abi $ARCH)
  1186. if [ -n "$ABI" ]; then
  1187. if [ -n "$RET" ]; then
  1188. RET=$RET",$ABI"
  1189. else
  1190. RET=$ABI
  1191. fi
  1192. else # Error message is printed by convert_arch_to_abi
  1193. exit 1
  1194. fi
  1195. done
  1196. echo "$RET"
  1197. }
  1198. # Return the default toolchain binary path prefix for given architecture and gcc version
  1199. # For example: arm 4.8 -> toolchains/<system>/arm-linux-androideabi-4.8/bin/arm-linux-androideabi-
  1200. # $1: Architecture name
  1201. # $2: GCC version
  1202. # $3: optional, system name, defaults to $HOST_TAG
  1203. get_toolchain_binprefix_for_arch ()
  1204. {
  1205. local NAME PREFIX DIR BINPREFIX
  1206. local SYSTEM=${3:-$(get_prebuilt_host_tag)}
  1207. NAME=$(get_toolchain_name_for_arch $1 $2)
  1208. PREFIX=$(get_default_toolchain_prefix_for_arch $1)
  1209. DIR=$(get_toolchain_install . $NAME $SYSTEM)
  1210. BINPREFIX=${DIR#./}/bin/$PREFIX-
  1211. echo "$BINPREFIX"
  1212. }
  1213. # Return llvm toolchain binary path prefix for given llvm version
  1214. # $1: optional, system name, defaults to $HOST_TAG
  1215. get_llvm_toolchain_binprefix ()
  1216. {
  1217. local NAME DIR BINPREFIX
  1218. local SYSTEM=${1:-$(get_prebuilt_host_tag)}
  1219. local VERSION=2812033
  1220. SYSTEM=${SYSTEM%_64} # Trim _64 suffix. We only have one LLVM.
  1221. BINPREFIX=$ANDROID_BUILD_TOP/prebuilts/clang/host/$SYSTEM/clang-$VERSION/bin
  1222. echo "$BINPREFIX"
  1223. }
  1224. # Return default API level for a given arch
  1225. # This is the level used to build the toolchains.
  1226. #
  1227. # $1: Architecture name
  1228. get_default_api_level_for_arch ()
  1229. {
  1230. # For now, always build the toolchain against API level 9 for 32-bit arch
  1231. # and API level $FIRST_API64_LEVEL for 64-bit arch
  1232. case $1 in
  1233. *64) echo $FIRST_API64_LEVEL ;;
  1234. *) echo 9 ;;
  1235. esac
  1236. }
  1237. # Return the default platform sysroot corresponding to a given architecture
  1238. # This is the sysroot used to build the toolchain and other binaries like
  1239. # the STLport libraries.
  1240. # $1: Architecture name
  1241. get_default_platform_sysroot_for_arch ()
  1242. {
  1243. local ARCH=$1
  1244. local LEVEL=$(get_default_api_level_for_arch $ARCH)
  1245. if [ "$ARCH" != "${ARCH%%64*}" ] ; then
  1246. LEVEL=$FIRST_API64_LEVEL
  1247. fi
  1248. echo "platforms/android-$LEVEL/arch-$ARCH"
  1249. }
  1250. # Return the default platform sysroot corresponding to a given abi
  1251. # $1: ABI
  1252. get_default_platform_sysroot_for_abi ()
  1253. {
  1254. local ARCH=$(convert_abi_to_arch $1)
  1255. $(get_default_platform_sysroot_for_arch $ARCH)
  1256. }
  1257. # Return the default libs dir corresponding to a given architecture
  1258. # $1: Architecture name
  1259. get_default_libdir_for_arch ()
  1260. {
  1261. case $1 in
  1262. x86_64|mips64) echo "lib64" ;;
  1263. arm64) echo "lib" ;; # return "lib" until aarch64 is built to look for sysroot/usr/lib64
  1264. *) echo "lib" ;;
  1265. esac
  1266. }
  1267. # Return the default libs dir corresponding to a given abi
  1268. # $1: ABI
  1269. get_default_libdir_for_abi ()
  1270. {
  1271. local ARCH
  1272. case $1 in
  1273. mips32r6) echo "libr6" ;;
  1274. *)
  1275. local ARCH=$(convert_abi_to_arch $1)
  1276. echo "$(get_default_libdir_for_arch $ARCH)"
  1277. ;;
  1278. esac
  1279. }
  1280. # Return the host/build specific path for prebuilt toolchain binaries
  1281. # relative to $1.
  1282. #
  1283. # $1: target root NDK directory
  1284. # $2: toolchain name
  1285. # $3: optional, host system name
  1286. #
  1287. get_toolchain_install ()
  1288. {
  1289. local NDK="$1"
  1290. shift
  1291. echo "$NDK/$(get_toolchain_install_subdir "$@")"
  1292. }
  1293. # $1: toolchain name
  1294. # $2: optional, host system name
  1295. get_toolchain_install_subdir ()
  1296. {
  1297. local SYSTEM=${2:-$(get_prebuilt_host_tag)}
  1298. echo "toolchains/$SYSTEM/$1"
  1299. }
  1300. # Return the relative install prefix for prebuilt host
  1301. # executables (relative to the NDK top directory).
  1302. #
  1303. # Out: relative path to prebuilt install prefix
  1304. get_prebuilt_install_prefix ()
  1305. {
  1306. echo "host-tools"
  1307. }
  1308. # Return the relative path of an installed prebuilt host
  1309. # executable.
  1310. #
  1311. # $1: executable name
  1312. # Out: path to prebuilt host executable, relative
  1313. get_prebuilt_host_exec ()
  1314. {
  1315. local PREFIX EXE
  1316. PREFIX=$(get_prebuilt_install_prefix)
  1317. EXE=$(get_prebuilt_host_exe_ext)
  1318. echo "$PREFIX/bin/$1$EXE"
  1319. }
  1320. # Return the name of a given host executable
  1321. # $1: executable base name
  1322. # Out: executable name, with optional suffix (e.g. .exe for windows)
  1323. get_host_exec_name ()
  1324. {
  1325. local EXE=$(get_prebuilt_host_exe_ext)
  1326. echo "$1$EXE"
  1327. }
  1328. # Return the directory where host-specific binaries are installed.
  1329. # $1: target root NDK directory
  1330. get_host_install ()
  1331. {
  1332. echo "$1/$(get_prebuilt_install_prefix)"
  1333. }
  1334. # Set the toolchain target NDK location.
  1335. # this sets TOOLCHAIN_PATH and TOOLCHAIN_PREFIX
  1336. # $1: target NDK path
  1337. # $2: toolchain name
  1338. set_toolchain_ndk ()
  1339. {
  1340. TOOLCHAIN_PATH=`get_toolchain_install "$1" $2`
  1341. log "Using toolchain path: $TOOLCHAIN_PATH"
  1342. TOOLCHAIN_PREFIX=$TOOLCHAIN_PATH/bin/$ABI_CONFIGURE_TARGET
  1343. log "Using toolchain prefix: $TOOLCHAIN_PREFIX"
  1344. }
  1345. # Check that a toolchain is properly installed at a target NDK location
  1346. #
  1347. # $1: target root NDK directory
  1348. # $2: toolchain name
  1349. #
  1350. check_toolchain_install ()
  1351. {
  1352. TOOLCHAIN_PATH=`get_toolchain_install "$1" $2`
  1353. if [ ! -d "$TOOLCHAIN_PATH" ] ; then
  1354. echo "ERROR: Cannot find directory '$TOOLCHAIN_PATH'!"
  1355. echo " Toolchain '$2' not installed in '$NDK_DIR'!"
  1356. echo " Ensure that the toolchain has been installed there before."
  1357. exit 1
  1358. fi
  1359. set_toolchain_ndk $1 $2
  1360. }
  1361. # $1: toolchain source directory
  1362. check_toolchain_src_dir ()
  1363. {
  1364. local SRC_DIR="$1"
  1365. if [ -z "$SRC_DIR" ]; then
  1366. echo "ERROR: Please provide the path to the toolchain source tree. See --help"
  1367. exit 1
  1368. fi
  1369. if [ ! -d "$SRC_DIR" ]; then
  1370. echo "ERROR: Not a directory: '$SRC_DIR'"
  1371. exit 1
  1372. fi
  1373. if [ ! -f "$SRC_DIR/build/configure" -o ! -d "$SRC_DIR/gcc" ]; then
  1374. echo "ERROR: Either the file $SRC_DIR/build/configure or"
  1375. echo " the directory $SRC_DIR/gcc does not exist."
  1376. echo "This is not the top of a toolchain tree: $SRC_DIR"
  1377. exit 1
  1378. fi
  1379. }
  1380. make_repo_prop () {
  1381. local OUT_PATH="$1/repo.prop"
  1382. # The build server generates a repo.prop file that contains the current SHAs
  1383. # of each project.
  1384. if [ -f $DIST_DIR/repo.prop ]; then
  1385. cp $DIST_DIR/repo.prop $OUT_PATH
  1386. else
  1387. # Generate our own if we're building locally.
  1388. pushd $ANDROID_NDK_ROOT
  1389. repo forall \
  1390. -c 'echo $REPO_PROJECT $(git rev-parse HEAD)' > $OUT_PATH
  1391. popd
  1392. fi
  1393. }
  1394. #
  1395. # The NDK_TMPDIR variable is used to specify a root temporary directory
  1396. # when invoking toolchain build scripts. If it is not defined, we will
  1397. # create one here, and export the value to ensure that any scripts we
  1398. # call after that use the same one.
  1399. #
  1400. if [ -z "$NDK_TMPDIR" ]; then
  1401. NDK_TMPDIR=$TMPDIR/tmp/build-$$
  1402. mkdir -p $NDK_TMPDIR
  1403. if [ $? != 0 ]; then
  1404. echo "ERROR: Could not create NDK_TMPDIR: $NDK_TMPDIR"
  1405. exit 1
  1406. fi
  1407. export NDK_TMPDIR
  1408. fi
  1409. # Define HOST_TAG32, as the 32-bit version of HOST_TAG
  1410. # We do this by replacing an -x86_64 suffix by -x86
  1411. HOST_TAG32=$HOST_TAG
  1412. case $HOST_TAG32 in
  1413. *-x86_64)
  1414. HOST_TAG32=${HOST_TAG%%_64}
  1415. ;;
  1416. esac