PageRenderTime 2269ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/toolchain/scripts/crosstool-NG.sh

https://gitlab.com/thanhnhat041/padavan-ng
Shell | 742 lines | 531 code | 59 blank | 152 comment | 46 complexity | 53ef0481aa88e389661b28d89049932a MD5 | raw file
  1. # Copyright 2007 Yann E. MORIN
  2. # Licensed under the GPL v2. See COPYING in the root of this package.
  3. # This is the main entry point to crosstool
  4. # This will:
  5. # - download, extract and patch the toolchain components
  6. # - build and install each components in turn
  7. # - and eventually test the resulting toolchain
  8. # What this file does is prepare the environment, based upon the user-choosen
  9. # options. It also checks the existing environment for un-friendly variables,
  10. # and builds the tools.
  11. # Parse the common functions
  12. # Note: some initialisation and sanitizing is done while parsing this file,
  13. # most notably:
  14. # - set trap handler on errors,
  15. # - don't hash commands lookups,
  16. . "${CT_LIB_DIR}/scripts/functions"
  17. # Read the sample settings
  18. CT_LoadConfig
  19. # Yes! We can do full logging from now on! Clean any old log file content.
  20. CT_LogEnable clean=yes
  21. # Check running as root
  22. if [ -z "${CT_ALLOW_BUILD_AS_ROOT_SURE}" ]; then
  23. if [ $(id -u) -eq 0 ]; then
  24. CT_DoLog ERROR "You must NOT be root to run crosstool-NG"
  25. exit 1
  26. fi
  27. fi
  28. CT_TestAndAbort "Invalid configuration. Run 'ct-ng menuconfig' and check which options select INVALID_CONFIGURATION." -n "${CT_INVALID_CONFIGURATION}"
  29. # If we want an interactive debug-shell, we must ensure these FDs
  30. # are indeed connected to a terminal (and not redirected in any way).
  31. if [ "${CT_DEBUG_INTERACTIVE}" = "y" -a ! \( -t 0 -a -t 6 -a -t 2 \) ]; then
  32. CT_DoLog ERROR "Can't spawn interactive debug-shell,"
  33. CT_DoLog ERROR "because stdout/stderr has been redirected."
  34. exit 1
  35. fi
  36. CT_TrapEnvExport
  37. # Override the locale early, in case we ever translate crosstool-NG messages
  38. if [ -z "${CT_NO_OVERRIDE_LC_MESSAGES}" ]; then
  39. export LC_ALL=C
  40. export LANG=C
  41. fi
  42. # remove . from PATH since it can cause gcc build failures
  43. CT_SanitizePath
  44. # Some sanity checks in the environment and needed tools
  45. CT_DoLog INFO "Performing some trivial sanity checks"
  46. CT_TestAndAbort "Don't set LD_LIBRARY_PATH. It screws up the build." -n "${LD_LIBRARY_PATH+set}"
  47. CT_TestAndAbort "Don't set LIBRARY_PATH. It screws up the build." -n "${LIBRARY_PATH+set}"
  48. CT_TestAndAbort "Don't set LPATH. It screws up the build." -n "${LPATH+set}"
  49. CT_TestAndAbort "Don't set CPATH. It screws up the build." -n "${CPATH+set}"
  50. CT_TestAndAbort "Don't set C_INCLUDE_PATH. It screws up the build." -n "${C_INCLUDE_PATH+set}"
  51. CT_TestAndAbort "Don't set CPLUS_INCLUDE_PATH. It screws up the build." -n "${CPLUS_INCLUDE_PATH+set}"
  52. CT_TestAndAbort "Don't set OBJC_INCLUDE_PATH. It screws up the build." -n "${OBJC_INCLUDE_PATH+set}"
  53. CT_TestAndAbort "Don't set CFLAGS. It screws up the build." -n "${CFLAGS+set}"
  54. CT_TestAndAbort "Don't set CXXFLAGS. It screws up the build." -n "${CXXFLAGS+set}"
  55. CT_TestAndAbort "Don't set CC. It screws up the build." -n "${CC+set}"
  56. CT_TestAndAbort "Don't set CXX. It screws up the build." -n "${CXX+set}"
  57. CT_Test "GREP_OPTIONS screws up the build. Unsetting." -n "${GREP_OPTIONS+set}"
  58. unset GREP_OPTIONS
  59. # Workaround against openSUSE 12.1 that breaks ./configure for cross-compilation:
  60. export CONFIG_SITE=
  61. # Some sanity checks on paths content
  62. for d in \
  63. LOCAL_TARBALLS \
  64. WORK \
  65. PREFIX \
  66. BUILD_TOP \
  67. INSTALL \
  68. ; do
  69. eval dir="\${CT_${d}_DIR}"
  70. case "${dir}" in
  71. *" "*)
  72. CT_Abort "'CT_${d}_DIR'='${dir}' contains a space in it.\nDon't use spaces in paths, it breaks things."
  73. ;;
  74. *:*)
  75. CT_Abort "'CT_${d}_DIR'='${dir}' contains a colon in it.\nDon't use colons in paths, it breaks things."
  76. ;;
  77. *,*)
  78. CT_Abort "'CT_${d}_DIR'='${dir}' contains a comma in it.\nDon't use commas in paths, it breaks things."
  79. ;;
  80. esac
  81. case "${dir}" in
  82. /*)
  83. # Absolute path, okay
  84. ;;
  85. *)
  86. # Relative path from CT_TOP_DIR, make absolute
  87. eval CT_${d}_DIR="${CT_TOP_DIR}/${dir}"
  88. # Having .. inside CT_PREFIX breaks relocatability.
  89. CT_SanitizeVarDir CT_${d}_DIR
  90. ;;
  91. esac
  92. done
  93. n_open_files=$(ulimit -n)
  94. if [ "${n_open_files}" -lt 2048 ]; then
  95. # Newer ld seems to keep a lot of open file descriptors, hitting the default limit
  96. # (1024) for example during uClibc-ng link.
  97. CT_DoLog WARN "Number of open files ${n_open_files} may not be sufficient to build the toolchain; increasing to 2048"
  98. ulimit -n 2048
  99. fi
  100. # Where will we work?
  101. CT_WORK_DIR="${CT_WORK_DIR:-${CT_TOP_DIR}/.build}"
  102. CT_BUILD_DIR="${CT_BUILD_TOP_DIR}/build"
  103. CT_DoExecLog ALL mkdir -p "${CT_WORK_DIR}"
  104. CT_DoExecLog DEBUG rm -f "${CT_WORK_DIR}/backtrace"
  105. # Check build file system case-sensitiveness
  106. CT_DoExecLog DEBUG touch "${CT_WORK_DIR}/foo"
  107. CT_TestAndAbort "Your file system in '${CT_WORK_DIR}' is *not* case-sensitive!" -f "${CT_WORK_DIR}/FOO"
  108. CT_DoExecLog DEBUG rm -f "${CT_WORK_DIR}/foo"
  109. # Check the user is using an existing SHELL to be used by ./configure and Makefiles
  110. CT_TestOrAbort "The CONFIG_SHELL '${CT_CONFIG_SHELL}' is not valid" -f "${CT_CONFIG_SHELL}" -a -x "${CT_CONFIG_SHELL}"
  111. # Create the bin-override early
  112. # Contains symlinks to the tools found by ./configure
  113. # Note: CT_DoLog and CT_DoExecLog do not use any of those tool, so
  114. # they can be safely used
  115. CT_TOOLS_OVERRIDE_DIR="${CT_WORK_DIR}/tools"
  116. CT_DoLog DEBUG "Creating bin-override for tools in '${CT_TOOLS_OVERRIDE_DIR}'"
  117. CT_DoExecLog DEBUG mkdir -p "${CT_TOOLS_OVERRIDE_DIR}/bin"
  118. cat "${paths_sh_location}" |while read trash line; do
  119. tool="${line%%=*}"
  120. # Suppress extra quoting
  121. eval path=${line#*=}
  122. if [ ! -r "${CT_LIB_DIR}/scripts/override/$tool" ]; then
  123. if [ -n "${path}" ]; then
  124. CT_DoExecLog ALL rm -f "${CT_TOOLS_OVERRIDE_DIR}/bin/${tool}"
  125. CT_DoExecLog ALL ln -s "${path}" "${CT_TOOLS_OVERRIDE_DIR}/bin/${tool}"
  126. fi
  127. continue
  128. fi
  129. tmpl="${CT_LIB_DIR}/scripts/override/$tool"
  130. CT_DoLog DEBUG "Creating script-override for '${tool}' -> '${path}' using '${tmpl}' template"
  131. CT_DoExecLog ALL cp "${tmpl}" "${CT_TOOLS_OVERRIDE_DIR}/bin/${tool}"
  132. CT_DoExecLog ALL ${sed} -i -r \
  133. -e "s#@INSTALL_WITH_STRIP_PROGRAM@#${CT_CONFIGURE_has_install_with_strip_program}#g" \
  134. -e "s#@CONFIG_SHELL@#${CT_CONFIG_SHELL}#g" \
  135. -e "s#@TOOL_PATH@#${path}#g" \
  136. -e "s#@TOOLS_OVERRIDE_DIR@#${CT_TOOLS_OVERRIDE_DIR}#g" \
  137. "${CT_TOOLS_OVERRIDE_DIR}/bin/${tool}"
  138. CT_DoExecLog ALL chmod 700 "${CT_TOOLS_OVERRIDE_DIR}/bin/${tool}"
  139. done
  140. export PATH="${CT_TOOLS_OVERRIDE_DIR}/bin:${PATH}"
  141. # Start date. Can't be done until we know the locale
  142. # Also requires the bin-override tools
  143. CT_STAR_DATE=$(CT_DoDate +%s%N)
  144. CT_STAR_DATE_HUMAN=$(CT_DoDate +%Y%m%d.%H%M%S)
  145. # Log real begining of build, now
  146. CT_DoLog INFO "Build started ${CT_STAR_DATE_HUMAN}"
  147. CT_DoStep DEBUG "Dumping user-supplied crosstool-NG configuration"
  148. CT_DoExecLog DEBUG ${grep} -E '^(# )?CT_' .config
  149. CT_EndStep
  150. CT_DoLog DEBUG "Unsetting MAKEFLAGS"
  151. unset MAKEFLAGS
  152. # Set the shell to be used by ./configure scripts and by Makefiles (those
  153. # that support it!).
  154. export CONFIG_SHELL="${CT_CONFIG_SHELL}" # for ./configure
  155. export SHELL="${CT_CONFIG_SHELL}" # for Makefiles
  156. CT_DoLog INFO "Building environment variables"
  157. # Sanity check some directories
  158. CT_TestAndAbort "'CT_PREFIX_DIR' is not set: where should I install?" -z "${CT_PREFIX_DIR}"
  159. # Avoid multiple '/' in the prefix dir, it breaks relocatability
  160. CT_PREFIX_DIR="$( ${sed} -r -e 's:/+:/:g; s:/*$::;' <<<"${CT_PREFIX_DIR}" )"
  161. # Second kludge: merge user-supplied target CFLAGS with architecture-provided
  162. # target CFLAGS. Do the same for LDFLAGS in case it happens in the future.
  163. # Put user-supplied flags at the end, so that they take precedence.
  164. CT_ALL_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_TARGET_CFLAGS}"
  165. CT_ALL_TARGET_LDFLAGS="${CT_ARCH_TARGET_LDFLAGS} ${CT_TARGET_LDFLAGS}"
  166. # FIXME move to gcc.sh
  167. CT_CC_GCC_CORE_EXTRA_CONFIG_ARRAY=( ${CT_ARCH_CC_CORE_EXTRA_CONFIG} "${CT_CC_GCC_CORE_EXTRA_CONFIG_ARRAY[@]}" )
  168. CT_CC_GCC_EXTRA_CONFIG_ARRAY=( ${CT_ARCH_CC_EXTRA_CONFIG} "${CT_CC_GCC_EXTRA_CONFIG_ARRAY[@]}" )
  169. # Starting with 1.0.20, applications using uClibc-ng do not link with
  170. # the default libgcc_c_spec used by GCC if only static libc.a exists - unless
  171. # -static is thrown in. The difference is that with -static, gcc passes
  172. # "--start-group -lgcc -lc --end-group" and without -static, it passes
  173. # "-lgcc -lc -lgcc" instead. The latter leaves a symbol from 2nd libgcc
  174. # (dl_iterate_phdr) unresolved because -lc is already done at this point.
  175. # Force static link on the target.
  176. if [ "${CT_SHARED_LIBS}" != "y" ]; then
  177. CT_TARGET_LDFLAGS+=" -static"
  178. fi
  179. # Compute the package version string
  180. if [ "${CT_SHOW_CT_VERSION}" = "y" ]; then
  181. CT_PKGVERSION="crosstool-NG ${CT_VERSION}${CT_TOOLCHAIN_PKGVERSION:+ - ${CT_TOOLCHAIN_PKGVERSION}}"
  182. else
  183. CT_PKGVERSION="${CT_TOOLCHAIN_PKGVERSION}"
  184. fi
  185. # Compute the working directories names
  186. CT_TARBALLS_DIR="${CT_WORK_DIR}/tarballs"
  187. CT_COMMON_SRC_DIR="${CT_WORK_DIR}/src"
  188. CT_SRC_DIR="${CT_BUILD_TOP_DIR}/src"
  189. CT_BUILDTOOLS_PREFIX_DIR="${CT_BUILD_TOP_DIR}/buildtools"
  190. CT_STATE_DIR="${CT_BUILD_TOP_DIR}/state"
  191. # Note about HOST_COMPLIBS_DIR: it's always gonna be in the buildtools dir, or a
  192. # sub-dir. So we won't have to save/restore it, not even create it.
  193. # In case of cross or native, host-complibs are used for build-complibs;
  194. # in case of canadian or cross-native, host-complibs are specific
  195. # Note about BUILD_COMPTOOLS_DIR: if installing companion tools for "host" in
  196. # a native or simple cross, we can can use the same binaries we built for
  197. # "build". However, we need companion tools for "build" early - as other
  198. # components may depend on them - so we may skip building for "host" rather
  199. # than for "build" in that case.
  200. case "${CT_TOOLCHAIN_TYPE}" in
  201. native|cross)
  202. CT_HOST_COMPLIBS_DIR="${CT_BUILDTOOLS_PREFIX_DIR}"
  203. if [ -n "${CT_COMP_TOOLS_FOR_HOST}" ]; then
  204. CT_BUILD_COMPTOOLS_DIR="${CT_PREFIX_DIR}"
  205. else
  206. CT_BUILD_COMPTOOLS_DIR="${CT_BUILDTOOLS_PREFIX_DIR}"
  207. fi
  208. ;;
  209. canadian|cross-native)
  210. CT_HOST_COMPLIBS_DIR="${CT_BUILDTOOLS_PREFIX_DIR}/complibs-host"
  211. CT_BUILD_COMPTOOLS_DIR="${CT_BUILDTOOLS_PREFIX_DIR}"
  212. ;;
  213. esac
  214. # Compute test suite install directory
  215. CT_TEST_SUITE_DIR=${CT_PREFIX_DIR}/test-suite
  216. # We must ensure that we can restart if asked for!
  217. if [ -n "${CT_RESTART}" -a ! -d "${CT_STATE_DIR}" ]; then
  218. CT_DoLog ERROR "You asked to restart a non-restartable build"
  219. CT_DoLog ERROR "This happened because you didn't set CT_DEBUG_CT_SAVE_STEPS"
  220. CT_DoLog ERROR "in the config options for the previous build, or the state"
  221. CT_DoLog ERROR "directory for the previous build was deleted."
  222. CT_Abort "I will stop here to avoid any carnage"
  223. fi
  224. # If the local tarball directory does not exist, say so, and don't try to save there!
  225. if [ "${CT_SAVE_TARBALLS}" = "y" \
  226. -a ! -d "${CT_LOCAL_TARBALLS_DIR}" ]; then
  227. CT_DoLog WARN "Directory '${CT_LOCAL_TARBALLS_DIR}' does not exist."
  228. CT_DoLog WARN "Will not save downloaded tarballs to local storage."
  229. CT_SAVE_TARBALLS=
  230. fi
  231. # Good, now grab a bit of informations on the system we're being run on,
  232. # just in case something goes awok, and it's not our fault:
  233. CT_SYS_USER=$(id -un)
  234. CT_SYS_HOSTNAME=$(hostname -f 2>/dev/null || true)
  235. # Hmmm. Some non-DHCP-enabled machines do not have an FQDN... Fall back to node name.
  236. CT_SYS_HOSTNAME="${CT_SYS_HOSTNAME:-$(uname -n)}"
  237. CT_SYS_KERNEL=$(uname -s)
  238. CT_SYS_REVISION=$(uname -r)
  239. CT_SYS_OS=$(uname -s)
  240. CT_SYS_MACHINE=$(uname -m)
  241. CT_SYS_PROCESSOR=$(uname -p)
  242. CT_SYS_GCC=$(${CT_BUILD_PREFIX}gcc${CT_BUILD_SUFFIX} -dumpversion)
  243. CT_SYS_TARGET=$(CT_DoConfigGuess)
  244. CT_TOOLCHAIN_ID="crosstool-${CT_VERSION} build ${CT_STAR_DATE_HUMAN} by ${CT_SYS_USER}@${CT_SYS_HOSTNAME}"
  245. # Adjust the list of multilibs, if needed
  246. CT_DoArchMultilibList
  247. CT_DoLog EXTRA "Preparing working directories"
  248. # Ah! The build directory shall be eradicated, even if we restart!
  249. # Ditto for the build tools install dir
  250. CT_DoForceRmdir "${CT_BUILD_DIR}" "${CT_BUILDTOOLS_PREFIX_DIR}"
  251. # Don't eradicate directories if we need to restart
  252. if [ -z "${CT_RESTART}" ]; then
  253. # Per-target sources: eliminate
  254. CT_DoForceRmdir "${CT_SRC_DIR}"
  255. # Get rid of pre-existing installed toolchain and previous build directories.
  256. if [ "${CT_FORCE_DOWNLOAD}" = "y" -a -d "${CT_TARBALLS_DIR}" ]; then
  257. CT_DoForceRmdir "${CT_TARBALLS_DIR}"
  258. fi
  259. if [ "${CT_FORCE_EXTRACT}" = "y" -a -d "${CT_COMMON_SRC_DIR}" ]; then
  260. CT_DoForceRmdir "${CT_COMMON_SRC_DIR}"
  261. fi
  262. if [ -d "${CT_PREFIX_DIR}" -a "${CT_RM_RF_PREFIX_DIR}" = "y" ]; then
  263. CT_DoForceRmdir "${CT_PREFIX_DIR}"
  264. fi
  265. # In case we start anew, get rid of the previously saved state directory
  266. if [ -d "${CT_STATE_DIR}" ]; then
  267. CT_DoForceRmdir "${CT_STATE_DIR}"
  268. fi
  269. fi
  270. # Create the directories we'll use, even if restarting: it does no harm to
  271. # create already existent directories, and CT_BUILD_DIR needs to be created
  272. # anyway
  273. CT_DoExecLog ALL mkdir -p "${CT_TARBALLS_DIR}"
  274. CT_DoExecLog ALL mkdir -p "${CT_COMMON_SRC_DIR}"
  275. CT_DoExecLog ALL mkdir -p "${CT_SRC_DIR}"
  276. CT_DoExecLog ALL mkdir -p "${CT_BUILD_DIR}"
  277. CT_DoExecLog ALL mkdir -p "${CT_BUILDTOOLS_PREFIX_DIR}/bin"
  278. CT_DoExecLog ALL mkdir -p "${CT_PREFIX_DIR}"
  279. CT_DoExecLog ALL mkdir -p "${CT_HOST_COMPLIBS_DIR}"
  280. # Only create the state dir if asked for a restartable build
  281. [ -n "${CT_DEBUG_CT_SAVE_STEPS}" ] && CT_DoExecLog ALL mkdir -p "${CT_STATE_DIR}"
  282. # Kludge: CT_PREFIX_DIR might have grown read-only if
  283. # the previous build was successful.
  284. CT_DoExecLog ALL chmod -R u+w "${CT_PREFIX_DIR}"
  285. # Check install file system case-sensitiveness
  286. CT_DoExecLog DEBUG touch "${CT_PREFIX_DIR}/foo"
  287. CT_TestAndAbort "Your file system in '${CT_PREFIX_DIR}' is *not* case-sensitive!" -f "${CT_PREFIX_DIR}/FOO"
  288. CT_DoExecLog DEBUG rm -f "${CT_PREFIX_DIR}/foo"
  289. # Setting up the rest of the environment only if not restarting
  290. if [ -z "${CT_RESTART}" ]; then
  291. case "${CT_SYSROOT_NAME}" in
  292. "") CT_SYSROOT_NAME="sysroot";;
  293. .) CT_Abort "Sysroot name is set to '.' which is forbidden";;
  294. *' '*) CT_Abort "Sysroot name contains forbidden space(s): '${CT_SYSROOT_NAME}'";;
  295. */*) CT_Abort "Sysroot name contains forbidden slash(es): '${CT_SYSROOT_NAME}'";;
  296. esac
  297. # Arrange paths depending on whether we use sysroot or not.
  298. if [ "${CT_USE_SYSROOT}" = "y" ]; then
  299. CT_SYSROOT_DIR="${CT_PREFIX_DIR}/${CT_TARGET}/${CT_SYSROOT_DIR_PREFIX}/${CT_SYSROOT_NAME}"
  300. CT_DEBUGROOT_DIR="${CT_PREFIX_DIR}/${CT_TARGET}/${CT_SYSROOT_DIR_PREFIX}/debug-root"
  301. CT_HEADERS_DIR="${CT_SYSROOT_DIR}/usr/include"
  302. CT_SanitizeVarDir CT_SYSROOT_DIR CT_DEBUGROOT_DIR CT_HEADERS_DIR
  303. CT_BINUTILS_SYSROOT_ARG="--with-sysroot=${CT_SYSROOT_DIR}"
  304. CT_CC_CORE_SYSROOT_ARG="--with-sysroot=${CT_SYSROOT_DIR}"
  305. CT_CC_SYSROOT_ARG="--with-sysroot=${CT_SYSROOT_DIR}"
  306. # glibc's prefix must be exactly /usr, else --with-sysroot'd gcc will get
  307. # confused when $sysroot/usr/include is not present.
  308. # Note: --prefix=/usr is magic!
  309. # See http://www.gnu.org/software/libc/FAQ.html#s-2.2
  310. else
  311. # plain old way. All libraries in prefix/target/lib
  312. CT_SYSROOT_DIR="${CT_PREFIX_DIR}/${CT_TARGET}"
  313. CT_DEBUGROOT_DIR="${CT_SYSROOT_DIR}"
  314. CT_HEADERS_DIR="${CT_SYSROOT_DIR}/include"
  315. CT_SanitizeVarDir CT_SYSROOT_DIR CT_DEBUGROOT_DIR CT_HEADERS_DIR
  316. # hack! Always use --with-sysroot for binutils.
  317. # binutils 2.14 and later obey it, older binutils ignore it.
  318. # Lets you build a working 32->64 bit cross gcc
  319. CT_BINUTILS_SYSROOT_ARG="--with-sysroot=${CT_SYSROOT_DIR}"
  320. # Use --with-headers, else final gcc will define disable_glibc while
  321. # building libgcc, and you'll have no profiling
  322. CT_CC_CORE_SYSROOT_ARG="--without-headers"
  323. CT_CC_SYSROOT_ARG="--with-headers=${CT_HEADERS_DIR}"
  324. fi
  325. CT_DoExecLog ALL mkdir -p "${CT_SYSROOT_DIR}"
  326. CT_DoExecLog ALL mkdir -p "${CT_DEBUGROOT_DIR}"
  327. CT_DoExecLog ALL mkdir -p "${CT_HEADERS_DIR}"
  328. # Need the non-multilib directories: GCC's multi-os-directory is based off them, so
  329. # even if the /lib is not used for any of the multilibs, it must be present so that
  330. # the paths like 'lib/../lib64' still work.
  331. CT_DoExecLog ALL mkdir -p "${CT_PREFIX_DIR}/lib"
  332. CT_DoExecLog ALL mkdir -p "${CT_SYSROOT_DIR}/lib"
  333. CT_DoExecLog ALL mkdir -p "${CT_SYSROOT_DIR}/usr/lib"
  334. # Determine build system if not set by the user
  335. if [ -z "${CT_BUILD}" ]; then
  336. CT_BUILD=$(CT_DoConfigGuess)
  337. fi
  338. # Prepare mangling patterns to later modify BUILD and HOST (see below)
  339. case "${CT_TOOLCHAIN_TYPE}" in
  340. cross)
  341. # A cross-compiler runs on the same machine it is built on
  342. CT_HOST="${CT_BUILD}"
  343. build_mangle="build_"
  344. host_mangle="build_"
  345. target_mangle=""
  346. install_build_tools_for="BUILD"
  347. ;;
  348. canadian)
  349. build_mangle="build_"
  350. host_mangle="host_"
  351. target_mangle=""
  352. install_build_tools_for="BUILD HOST"
  353. ;;
  354. *) CT_Abort "No code for '${CT_TOOLCHAIN_TYPE}' toolchain type!"
  355. ;;
  356. esac
  357. # Save the real tuples to generate shell-wrappers to the real tools
  358. CT_REAL_BUILD="${CT_BUILD}"
  359. CT_REAL_HOST="${CT_HOST}"
  360. CT_REAL_TARGET="${CT_TARGET}"
  361. # Canonicalise CT_BUILD and CT_HOST
  362. # Not only will it give us full-qualified tuples, but it will also ensure
  363. # that they are valid tuples (in case of typo with user-provided tuples)
  364. # That's way better than trying to rewrite config.sub ourselves...
  365. # CT_TARGET is already made canonical in CT_DoBuildTargetTuple
  366. CT_BUILD=$(CT_DoConfigSub "${CT_BUILD}")
  367. CT_HOST=$(CT_DoConfigSub "${CT_HOST}")
  368. # Modify BUILD and HOST so that gcc always generate a cross-compiler
  369. # even if any of the build, host or target machines are the same.
  370. # NOTE: we'll have to mangle the (BUILD|HOST)->TARGET x-compiler to
  371. # support canadain build, later...
  372. CT_BUILD="${CT_BUILD/-/-${build_mangle}}"
  373. CT_HOST="${CT_HOST/-/-${host_mangle}}"
  374. CT_TARGET="${CT_TARGET/-/-${target_mangle}}"
  375. # Now we have mangled our BUILD and HOST tuples, we must fake the new
  376. # cross-tools for those mangled tuples.
  377. CT_DoLog DEBUG "Making build system tools available"
  378. for m in ${install_build_tools_for}; do
  379. r="CT_REAL_${m}"
  380. v="CT_${m}"
  381. p="CT_${m}_PREFIX"
  382. s="CT_${m}_SUFFIX"
  383. if [ -n "${!p}" ]; then
  384. t="${!p}"
  385. else
  386. t="${!r}-"
  387. fi
  388. for tool in ar as dlltool gcc g++ gcj gnatbind gnatmake ld nm objcopy objdump ranlib strip windres; do
  389. # First try with prefix + suffix
  390. # Then try with prefix only
  391. # Then try with suffix only, but only for BUILD, and HOST iff REAL_BUILD == REAL_HOST
  392. # Finally try with neither prefix nor suffix, but only for BUILD, and HOST iff REAL_BUILD == REAL_HOST
  393. # This is needed, because some tools have a prefix and
  394. # a suffix (eg. gcc), while others may have only one,
  395. # or even none (eg. binutils)
  396. where=$(CT_Which "${t}${tool}${!s}")
  397. [ -z "${where}" ] && where=$(CT_Which "${t}${tool}")
  398. if [ -z "${where}" \
  399. -a \( "${m}" = "BUILD" \
  400. -o "${CT_REAL_BUILD}" = "${!r}" \) ]; then
  401. where=$(CT_Which "${tool}${!s}")
  402. fi
  403. if [ -z "${where}" \
  404. -a \( "${m}" = "BUILD" \
  405. -o "${CT_REAL_BUILD}" = "${!r}" \) ]; then
  406. where=$(CT_Which "${tool}")
  407. fi
  408. # Not all tools are available for all platforms, but some are required.
  409. # TBD do we need these as shell wrappers? exec is slow on Cygwin, and this makes exec twice for each compiler/linker run
  410. if [ -n "${where}" ]; then
  411. CT_DoLog DEBUG " '${!v}-${tool}' -> '${where}'"
  412. printf "#${BANG}${CT_CONFIG_SHELL}\nexec '${where}' \"\${@}\"\n" >"${CT_BUILDTOOLS_PREFIX_DIR}/bin/${!v}-${tool}"
  413. CT_DoExecLog ALL chmod 700 "${CT_BUILDTOOLS_PREFIX_DIR}/bin/${!v}-${tool}"
  414. else
  415. case "${tool}" in
  416. # We'll at least need some of them...
  417. ar|as|gcc|ld|nm|objcopy|objdump|ranlib)
  418. CT_Abort "Missing: '${t}${tool}${!s}' or '${t}${tool}' or '${tool}' : either needed!"
  419. ;;
  420. # Some are conditionally required
  421. # Add them in alphabetical (C locale) ordering
  422. g++)
  423. # g++ (needed for companion lib), only needed for HOST
  424. CT_TestAndAbort "Missing: '${t}${tool}${!s}' or '${t}${tool}' or '${tool}' : either needed!" "${m}" = "HOST"
  425. ;;
  426. gcj)
  427. CT_TestAndAbort "Missing: '${t}${tool}${!s}' or '${t}${tool}' or '${tool}' : either needed!" "${CT_CC_LANG_JAVA}" = "y"
  428. ;;
  429. strip)
  430. CT_TestAndAbort "Missing: '${t}${tool}${!s}' or '${t}${tool}' or '${tool}' : either needed!" "${CT_STRIP_HOST_TOOLCHAIN_EXECUTABLES}" = "y"
  431. ;;
  432. # If any other is missing, only warn at low level
  433. *)
  434. # It does not deserve a WARN level.
  435. CT_DoLog DEBUG " Missing: '${t}${tool}${!s}' or '${t}${tool}' or '${tool}' : not required."
  436. ;;
  437. esac
  438. fi
  439. done
  440. done
  441. # Some makeinfo versions are a pain in [put your most sensible body part here].
  442. # Go ahead with those, by creating a wrapper that keeps partial files, and that
  443. # never fails:
  444. CT_DoLog DEBUG " 'makeinfo' -> '$(CT_Which makeinfo)'"
  445. printf "#${BANG}${CT_CONFIG_SHELL}\n$(CT_Which makeinfo) --force \"\${@}\"\ntrue\n" >"${CT_BUILDTOOLS_PREFIX_DIR}/bin/makeinfo"
  446. CT_DoExecLog ALL chmod 700 "${CT_BUILDTOOLS_PREFIX_DIR}/bin/makeinfo"
  447. # Carefully add paths in the order we want them:
  448. # - first try in ${CT_PREFIX_DIR}/bin
  449. # - then try the buildtools dir
  450. # - fall back to searching user's PATH
  451. # Of course, neither cross-native nor canadian can run on BUILD,
  452. # so don't add those PATHs in this case...
  453. # For native and simple cross, build==host, combine the extra CFLAGS/LDFLAGS
  454. # supplied for both (so that it doesn't matter where the user supplied them).
  455. case "${CT_TOOLCHAIN_TYPE}" in
  456. cross|native)
  457. export PATH="${CT_PREFIX_DIR}/bin:${CT_BUILDTOOLS_PREFIX_DIR}/bin:${PATH}"
  458. bh_cflags="${CT_EXTRA_CFLAGS_FOR_BUILD} ${CT_EXTRA_CFLAGS_FOR_HOST}"
  459. bh_ldflags="${CT_EXTRA_LDFLAGS_FOR_BUILD} ${CT_EXTRA_LDFLAGS_FOR_HOST}"
  460. CT_EXTRA_CFLAGS_FOR_BUILD="${bh_cflags}"
  461. CT_EXTRA_CFLAGS_FOR_HOST="${bh_cflags}"
  462. CT_EXTRA_LDFLAGS_FOR_BUILD="${bh_ldflags}"
  463. CT_EXTRA_LDFLAGS_FOR_HOST="${bh_ldflags}"
  464. ;;
  465. canadian|cross-native)
  466. export PATH="${CT_BUILDTOOLS_PREFIX_DIR}/bin:${PATH}"
  467. # build!=host in this case
  468. ;;
  469. *)
  470. ;;
  471. esac
  472. # Help build gcc
  473. # Explicitly optimise, else the lines below will overide the
  474. # package's default optimisation flags
  475. CT_CFLAGS_FOR_BUILD="-O2 -g -I${CT_BUILDTOOLS_PREFIX_DIR}/include"
  476. CT_CFLAGS_FOR_BUILD+=" ${CT_EXTRA_CFLAGS_FOR_BUILD}"
  477. CT_LDFLAGS_FOR_BUILD="-L${CT_BUILDTOOLS_PREFIX_DIR}/lib"
  478. CT_LDFLAGS_FOR_BUILD+=" ${CT_EXTRA_LDFLAGS_FOR_BUILD}"
  479. if ${CT_BUILD}-gcc --version 2>&1 | grep clang; then
  480. CT_CFLAGS_FOR_BUILD+=" -Qunused-arguments"
  481. fi
  482. case "${CT_BUILD}" in
  483. *darwin*)
  484. # Two issues while building on MacOS. Really, we should be checking for
  485. # clang instead.
  486. # - gettext static library fails to link unless CoreFoundation framework
  487. # is included
  488. # - ranlib on MacOS does not include common symbols into the symbol index
  489. # for a static library, and hence linker fails to pull in the right
  490. # archive members; hence, avoid common symbols. Alternative is to
  491. # have ranlib wrapper in buildtools/bin supply -c option.
  492. CT_CFLAGS_FOR_BUILD+=" -fno-common"
  493. CT_LDFLAGS_FOR_BUILD+=" -framework CoreFoundation"
  494. ;;
  495. esac
  496. CT_DoLog DEBUG "CFLAGS for build compiler: '${CT_CFLAGS_FOR_BUILD}'"
  497. CT_DoLog DEBUG "LDFLAGS for build compiler: '${CT_LDFLAGS_FOR_BUILD}'"
  498. # Help host gcc
  499. # Explicitly optimise, else the lines below will overide the
  500. # package's default optimisation flags
  501. CT_CFLAGS_FOR_HOST="-O2 -g"
  502. [ "${CT_USE_PIPES}" = "y" ] && CT_CFLAGS_FOR_HOST+=" -pipe"
  503. CT_CFLAGS_FOR_HOST+=" -I${CT_HOST_COMPLIBS_DIR}/include"
  504. CT_CFLAGS_FOR_HOST+=" ${CT_EXTRA_CFLAGS_FOR_HOST}"
  505. CT_LDFLAGS_FOR_HOST="-L${CT_HOST_COMPLIBS_DIR}/lib"
  506. CT_LDFLAGS_FOR_HOST+=" ${CT_EXTRA_LDFLAGS_FOR_HOST}"
  507. if ${CT_HOST}-gcc --version 2>&1 | grep clang; then
  508. CT_CFLAGS_FOR_HOST+=" -Qunused-arguments"
  509. fi
  510. case "${CT_HOST}" in
  511. *darwin*)
  512. # Same as above, for host
  513. CT_CFLAGS_FOR_HOST+=" -fno-common"
  514. CT_LDFLAGS_FOR_HOST+=" -framework CoreFoundation"
  515. ;;
  516. esac
  517. CT_DoLog DEBUG "CFLAGS for host compiler: '${CT_CFLAGS_FOR_HOST}'"
  518. CT_DoLog DEBUG "LDFLAGS for host compiler: '${CT_LDFLAGS_FOR_HOST}'"
  519. # And help make go faster
  520. CT_JOBSFLAGS=
  521. # Override the configured jobs with what's been given on the command line
  522. if [ -n "${CT_JOBS}" ]; then
  523. if [ ! -z "`echo "${CT_JOBS}" | ${sed} 's/[0-9]//g'`" ]; then
  524. CT_Abort "Number of parallel jobs must be integer."
  525. fi
  526. CT_PARALLEL_JOBS="${CT_JOBS}"
  527. fi
  528. # Use the number of processors+1 when automatically setting the number of
  529. # parallel jobs.
  530. AUTO_JOBS=$[ BUILD_NCPUS + 1 ]
  531. [ ${CT_PARALLEL_JOBS} -eq 0 ] && CT_JOBSFLAGS="${CT_JOBSFLAGS} -j${AUTO_JOBS}"
  532. [ ${CT_PARALLEL_JOBS} -gt 0 ] && CT_JOBSFLAGS="${CT_JOBSFLAGS} -j${CT_PARALLEL_JOBS}"
  533. CT_JOBSFLAGS="${CT_JOBSFLAGS} -l${CT_LOAD}"
  534. # Override 'download only' option
  535. [ -n "${CT_SOURCE}" ] && CT_ONLY_DOWNLOAD=y
  536. # Now that we've set up $PATH and $CT_CFLAGS_FOR_HOST, sanity test that gcc
  537. # is runnable so that the user can troubleshoot problems if not.
  538. CT_DoStep DEBUG "Checking that we can run gcc -v"
  539. CT_DoExecLog DEBUG "${CT_HOST}-gcc" -v
  540. CT_EndStep
  541. # Create a simple C program for testing.
  542. testc="${CT_BUILD_DIR}/test.c"
  543. printf "int main() { return 0; }\n" >"${testc}"
  544. gccout="${CT_BUILD_DIR}/.gccout"
  545. CT_DoStep DEBUG "Checking that gcc can compile a trivial program"
  546. CT_DoExecLog DEBUG "${CT_HOST}-gcc" ${CT_CFLAGS_FOR_HOST} ${CT_LDFLAGS_FOR_HOST} "${testc}" -o "${gccout}"
  547. rm -f "${gccout}"
  548. CT_EndStep
  549. # These tests are only enabled if we need static linking on the *build*
  550. if [ "${CT_WANTS_STATIC_LINK}" = "y" ]; then
  551. CT_DoStep DEBUG "Checking that gcc can compile a trivial statically linked program (CT_WANTS_STATIC_LINK)"
  552. CT_DoLog DEBUG "You may need to ensure that static libraries such as libc.a are installed on your system"
  553. CT_DoExecLog DEBUG "${CT_HOST}-gcc" ${CT_CFLAGS_FOR_BUILD} ${CT_LDFLAGS_FOR_BUILD} "${testc}" -static -o "${gccout}"
  554. rm -f "${gccout}"
  555. CT_EndStep
  556. fi
  557. if [ "${CT_WANTS_STATIC_LINK_CXX}" = "y" ]; then
  558. CT_DoStep DEBUG "Checking that gcc can statically link libstdc++ (CT_WANTS_STATIC_LINK_CXX)"
  559. CT_DoLog DEBUG "You may need to ensure that libstdc++.a is installed on your system"
  560. CT_DoExecLog DEBUG "${CT_HOST}-gcc" ${CT_CFLAGS_FOR_BUILD} ${CT_LDFLAGS_FOR_BUILD} "${testc}" -static -lstdc++ -o "${gccout}"
  561. rm -f "${gccout}"
  562. CT_EndStep
  563. fi
  564. rm -f "${testc}"
  565. CT_DoLog EXTRA "Installing user-supplied crosstool-NG configuration"
  566. CT_InstallConfigurationFile .config ct-ng
  567. CT_DoStep EXTRA "Dumping internal crosstool-NG configuration"
  568. CT_DoLog EXTRA "Building a toolchain for:"
  569. CT_DoLog EXTRA " build = ${CT_REAL_BUILD}"
  570. CT_DoLog EXTRA " host = ${CT_REAL_HOST}"
  571. CT_DoLog EXTRA " target = ${CT_TARGET}"
  572. set |${grep} -E '^CT_.+=' |sort |CT_DoLog DEBUG
  573. CT_DoLog DEBUG "Other environment:"
  574. printenv |${grep} -v -E '^CT_.+=' |CT_DoLog DEBUG
  575. CT_EndStep
  576. CT_DoStep INFO "Retrieving needed toolchain components' tarballs"
  577. do_companion_tools_get
  578. do_kernel_get
  579. do_companion_libs_get
  580. do_binutils_get
  581. do_cc_get
  582. do_libc_get
  583. do_debug_get
  584. do_test_suite_get
  585. CT_EndStep
  586. if [ "${CT_ONLY_DOWNLOAD}" != "y" ]; then
  587. if [ "${CT_FORCE_EXTRACT}" = "y" ]; then
  588. CT_DoForceRmdir "${CT_SRC_DIR}"
  589. CT_DoExecLog ALL mkdir -p "${CT_SRC_DIR}"
  590. fi
  591. CT_DoStep INFO "Extracting and patching toolchain components"
  592. do_companion_tools_extract
  593. do_kernel_extract
  594. do_companion_libs_extract
  595. do_binutils_extract
  596. do_cc_extract
  597. do_libc_extract
  598. do_debug_extract
  599. do_test_suite_extract
  600. CT_EndStep
  601. fi
  602. fi
  603. # Now for the job by itself. Go have a coffee!
  604. if [ "${CT_ONLY_DOWNLOAD}" != "y" -a "${CT_ONLY_EXTRACT}" != "y" ]; then
  605. # Because of CT_RESTART, this becomes quite complex
  606. do_stop=0
  607. prev_step=
  608. [ -n "${CT_RESTART}" ] && do_it=0 || do_it=1
  609. for step in ${CT_STEPS}; do
  610. if [ ${do_it} -eq 0 ]; then
  611. if [ "${CT_RESTART}" = "${step}" ]; then
  612. CT_DoLoadState "${step}"
  613. do_it=1
  614. do_stop=0
  615. fi
  616. else
  617. CT_DoSaveState ${step}
  618. if [ ${do_stop} -eq 1 ]; then
  619. CT_DoLog INFO "Stopping just after step '${prev_step}', as requested."
  620. exit 0
  621. fi
  622. fi
  623. if [ ${do_it} -eq 1 ]; then
  624. ( do_${step} )
  625. # POSIX 1003.1-2008 does not say if "set -e" should catch a
  626. # sub-shell ending with !0. bash-3 does not, while bash-4 does,
  627. # so the following line is for bash-3; bash-4 would choke above.
  628. [ $? -eq 0 ]
  629. # Pick up environment changes.
  630. if [ -r "${CT_BUILD_DIR}/env.modify.sh" ]; then
  631. CT_DoLog DEBUG "Step '${step}' modified the environment:"
  632. CT_DoExecLog DEBUG cat "${CT_BUILD_DIR}/env.modify.sh"
  633. . "${CT_BUILD_DIR}/env.modify.sh"
  634. CT_DoExecLog DEBUG rm -f "${CT_BUILD_DIR}/env.modify.sh"
  635. fi
  636. if [ "${CT_STOP}" = "${step}" ]; then
  637. do_stop=1
  638. fi
  639. if [ "${CT_DEBUG_PAUSE_STEPS}" = "y" ]; then
  640. CT_DoPause "Step '${step}' finished"
  641. fi
  642. fi
  643. prev_step="${step}"
  644. done
  645. fi
  646. CT_DoEnd INFO
  647. # From now-on, it can become impossible to log any time, because
  648. # either we're compressing the log file, or it can become RO any
  649. # moment...
  650. CT_DoLog INFO "Finishing installation (may take a few seconds)..."
  651. CT_LogDisable
  652. rm -f ${CT_PREFIX_DIR}/build.log.bz2
  653. if [ "${CT_LOG_TO_FILE}" = "y" ]; then
  654. cp "${CT_BUILD_LOG}" "${CT_PREFIX_DIR}/build.log"
  655. if [ "${CT_LOG_FILE_COMPRESS}" = y ]; then
  656. bzip2 -9 "${CT_PREFIX_DIR}/build.log"
  657. fi
  658. fi
  659. if [ "${CT_PREFIX_DIR_RO}" = "y" ]; then
  660. chmod -R a-w "${CT_PREFIX_DIR}"
  661. fi
  662. # CT_TEST_SUITE_DIR may not exist if only downloading or extracting
  663. if [ "${CT_TEST_SUITE}" = "y" -a -d "${CT_TEST_SUITE_DIR}" ]; then
  664. chmod -R u+w "${CT_TEST_SUITE_DIR}"
  665. fi
  666. trap - EXIT