Advertisement
Guest User

BuildXGCC

a guest
May 29th, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.00 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Build a gcc cross compiler with binutils.
  4.  
  5. # Common data.
  6. script=$(basename "$0")                                                 # Script name.
  7. dry=0                                                                   # Whether to perform a dry run.
  8. target=                                                                 # Target triplet.
  9. url_base="http://ftp.gnu.org/gnu"                                       # Base URL to retrieve files from.
  10. prefix="${HOME}/opt/cross"                                              # Installation path.
  11.  
  12. # Binutils data.
  13. bin_dl=0                                                                # Whether to download binutils.
  14. bin_build=0                                                             # Whether to build binutils.
  15. bin_name="binutils"                                                     # Binutils name.
  16. bin_ver="2.24"                                                          # Binutils version.
  17. bin_ext="tar.gz"                                                        # Binutils file extension.
  18. bin_targ="all install"                                                  # Binutils makefile targets.
  19. bin_opt="--disable-nls --disable-werror --with-sysroot"                 # Binutils configure options.
  20.  
  21. # GCC data.
  22. gcc_dl=0                                                                # Whether to download GCC.
  23. gcc_build=0                                                             # Whether to build GCC.
  24. gcc_name="gcc"                                                          # GCC name.
  25. gcc_ver="4.9.2"                                                         # GCC version.
  26. gcc_ext="tar.gz"                                                        # GCC file extension.
  27. gcc_targ="all-gcc all-target-libgcc install-gcc install-target-libgcc"  # GCC makefile targets.
  28. gcc_lang="c,c++"                                                        # GCC languages.
  29. gcc_opt="--disable-nls --disable-werror --without-headers"              # GCC configure options.
  30.  
  31. # Print a message
  32. # Args:
  33. #  1. The message to print.
  34. function say
  35. {
  36.     echo -e ">> $1"
  37. }
  38.  
  39. # Print an error message
  40. # Args:
  41. #  1. The error message to print.
  42. function saye
  43. {
  44.     say "$1" >&2
  45. }
  46.  
  47. # Print an error message and quit.
  48. # Args:
  49. #  1. The error message to print.
  50. function die
  51. {
  52.     saye "$1"
  53.     exit 1
  54. }
  55.  
  56. # Print the usage message to stdout.
  57. function show_usage
  58. {
  59.     cat <<EOF
  60. Usage: $0 [options] target
  61. Options:
  62.     [--]help            Show this help.
  63.     --dry               Echo commands instead of executing them.
  64.     --url-base <url>    Set the base URL for packages. Default: ${url_base}
  65.     --prefix <path>     Set the installation path. Default: ${prefix}
  66.     --bin-dl            Download binutils.
  67.     --bin-build         Build binutils.
  68.     --bin-name <string> Set the name of binutils. Default: ${bin_name}
  69.     --bin-ver <string>  Set the version of binutils. Default: ${bin_ver}
  70.     --bin-ext <string>  Set the file extension of binutils. Default: ${bin_ext}
  71.     --bin-targ <list>   Set the make targets for binutils. Default: ${bin_targ}
  72.     --bin-opt <list>    Set the configure options for binutils. Default: ${bin_opt}
  73.     --gcc-dl            Download GCC.
  74.     --gcc-build         Build GCC.
  75.     --gcc-ver <string>  Set the version of GCC. Default: ${gcc_ver}
  76.     --gcc-ext <string>  Set the file extension of GCC. Default: ${gcc_ext}
  77.     --gcc-targ <list>   Set the make targets for GCC. Default: ${gcc_targ}
  78.     --gcc-lang <list>   Set the language targets for GCC. Default: ${gcc_lang}
  79.     --gcc-opt <list>    Set the configure options for GCC. Default: ${gcc_opt}
  80. Note: if any one of --{bin,gcc}-{dl,build} is passed, it implicitly disables the other three. By default, all four are enabled.
  81. EOF
  82. }
  83.  
  84. # Print the usage message to stdout and exit with success.
  85. function quit_usage
  86. {
  87.     show_usage
  88.     exit 0
  89. }
  90.  
  91. # Print the usage message to stderr and exit with failure.
  92. function die_usage
  93. {
  94.     show_usage >&2
  95.     exit 1
  96. }
  97.  
  98. # Check the command line.
  99. [[ $# -lt 1 ]] && die_usage
  100.  
  101. # Grab the options.
  102. say "Reading command-line..."
  103. while [[ $# -gt 0 ]]; do
  104.     case "$1" in
  105.         --help)      quit_usage          ;;
  106.         --dry)       dry=1               ;;
  107.         --url-base)  url_base=$2 ; shift ;;
  108.         --prefix)    prefix=$2   ; shift ;;
  109.         --bin-dl)    bin_dl=1            ;;
  110.         --bin-build) bin_build=1         ;;
  111.         --bin-name)  bin_name=$2 ; shift ;;
  112.         --bin-ver)   bin_ver=$2  ; shift ;;
  113.         --bin-ext)   bin_ext=$2  ; shift ;;
  114.         --bin-targ)  bin_targ=$2 ; shift ;;
  115.         --bin-opt)   bin_opt=$2  ; shift ;;
  116.         --gcc-dl)    gcc_dl=1            ;;
  117.         --gcc-build) gcc_build=1         ;;
  118.         --gcc-name)  gcc_name=$2 ; shift ;;
  119.         --gcc-ver)   gcc_ver=$2  ; shift ;;
  120.         --gcc-ext)   gcc_ext=$2  ; shift ;;
  121.         --gcc-targ)  gcc_targ=$2 ; shift ;;
  122.         --gcc-lang)  gcc_lang=$2 ; shift ;;
  123.         --gcc-opt)   gcc_opt=$2  ; shift ;;
  124.         *)           target=$1   ; shift ;;
  125.     esac
  126.     shift
  127. done
  128.  
  129. [[ "x$target" = "x" ]] && die_usage
  130.  
  131. # Print chosen options.
  132. say "Selected build options:"
  133. cat <<EOF
  134.  * target:    ${target}
  135.  * dry:       ${dry}
  136.  * url-base:  ${url_base}
  137.  * prefix:    ${prefix}
  138.  * bin-dl:    ${bin_dl}
  139.  * bin-build: ${bin_build}
  140.  * bin-name:  ${bin_name}
  141.  * bin-ver:   ${bin_ver}
  142.  * bin-ext:   ${bin_ext}
  143.  * bin-targ:  ${bin_targ}
  144.  * bin-opt:   ${bin_opt}
  145.  * gcc-dl:    ${gcc_dl}
  146.  * gcc-build: ${gcc_build}
  147.  * gcc-name:  ${gcc_name}
  148.  * gcc-ver:   ${gcc_ver}
  149.  * gcc-ext:   ${gcc_ext}
  150.  * gcc-targ:  ${gcc_targ}
  151.  * gcc-lang:  ${gcc_lang}
  152.  * gcc-opt:   ${gcc_opt}
  153. EOF
  154.  
  155. # Call a command. If $dry != 0, echo the command instead.
  156. # Args:
  157. #  1. The command to run.
  158. #  ... Any arguments to the ecommand.
  159. function call
  160. {
  161.     local cmd="$1"
  162.     shift
  163.     if [[ $dry -eq 0 ]]; then
  164.         "${cmd}" $*
  165.         if [[ $? -ne 0 ]]; then
  166.             die "Command failed: ${cmd}"
  167.         fi
  168.     else
  169.         echo " * ${cmd} $*"
  170.     fi
  171. }
  172.  
  173. # Download a package.
  174. # Args:
  175. #  1. package name
  176. #  2. package version
  177. #  3. file extension
  178. #  4. package URL (if ends with /, "$name-$version.$ext" is appended)
  179. function get_package
  180. {
  181.     [[ $# -lt 4 ]] && die "$FUNCNAME: Not enough arguments: got $# expected 4"
  182.     local name="$1"
  183.     local version="$2"
  184.     local ext="$3"
  185.     local url="$4"
  186.     local filename="${name}-${version}.${ext}"
  187.     if [[ "${url}" = */ ]]; then
  188.         url="${url}${filename}"
  189.     fi
  190.     say "Downloading ${name}..."
  191.     call wget "${url}"
  192. }
  193.  
  194. function process_exists
  195. {
  196.     local pid="$1"
  197.     call kill -0 $pid #2>/dev/null
  198.     return $?
  199. }
  200.  
  201. # Extract a tarball with gzip or bzip2.
  202. # Args:
  203. #  1. The filename.
  204. function extract
  205. {
  206.     [[ $# -lt 1 ]] && die "$FUNCNAME: Not enough arguments: got $# expected 1"
  207.     local filename="$1"
  208.     local cmd=
  209.     case "$filename" in
  210.         *.tar.gz|*.tgz)
  211.             # Extract with tar and gzip.
  212.             cmd="tar --checkpoint=.100 -xzf"
  213.             ;;
  214.         *.tar.bz2|*.tbz)
  215.             # Extract with tar and bzip2.
  216.             cmd="tar --checkpoint=.100 -xJf"
  217.             ;;
  218.         *.tar)
  219.             # Extract with just tar.
  220.             cmd="tar --checkpoint=.100 -xf"
  221.             ;;
  222.         *.7z)
  223.             # Extract with p7zip.
  224.             cmd="p7zip -d"
  225.             ;;
  226.         *.)
  227.             # Directory: do nothing.
  228.             ;;
  229.         *)              
  230.         die "Unsupported file format $filename" ;; # Unknown archive: give up.
  231.     esac
  232.     if [[ "x$cmd" != "x" ]]; then
  233.         say  "Extracting archive..."
  234.         call $cmd "$filename"
  235.         echo
  236.     fi
  237. }
  238.  
  239. # Build and install a downloaded package.
  240. # Args:
  241. #  1. package name
  242. #  2. package version
  243. #  3. file extension
  244. #  4. target
  245. #  5. prefix
  246. #  6. makefile targets
  247. #  7. [configure options]
  248. function build_package
  249. {
  250.     [[ $# -lt 6 ]] && die "$FUNCNAME: Not enough arguments: got $# expected 6"
  251.     local name="$1"
  252.     local version="$2"
  253.     local ext="$3"
  254.     local target="$4"
  255.     local prefix="$5"
  256.     local maketarg="$6"
  257.     local options="$7"
  258.     local filename="${name}-${version}.${ext}"
  259.     local extract_dir="${name}-${version}"
  260.     # Extract package.
  261.     extract "${filename}"
  262.     # Build it.
  263.     say "Building ${name}..."
  264.     local build_dir="build-${name}-${version}"
  265.     [[ ! -d "${build_dir}" ]] && call mkdir "${build_dir}"
  266.     [[ $dry -eq 0 ]] && cd "${build_dir}" # Can't use call as it runs commands in sub-shell.
  267.     call "../${extract_dir}/configure" --prefix="${prefix}" --target="${target}" ${options}
  268.     for targ in ${maketarg}; do
  269.         call make "${targ}"
  270.     done
  271. }
  272.  
  273.  
  274. # Download binutils.
  275. function get_binutils
  276. {
  277.     say "Starting download (binutils)..."
  278.     get_package "${bin_name}" "${bin_ver}" "${bin_ext}" "${url_base}/${bin_name}/"
  279. }
  280.  
  281. # Build binutils.
  282. function build_binutils
  283. {
  284.     say "Starting build (binutils)..."
  285.     build_package "${bin_name}" "${bin_ver}" "${bin_ext}" "${target}" "${prefix}" "${bin_targ}" "${bin_opt}"
  286. }
  287.  
  288. # Download and build binutils.
  289. function do_binutils
  290. {
  291.     [[ ${bin_dl}    -eq 1 ]] && get_binutils
  292.     [[ ${bin_build} -eq 1 ]] && build_binutils
  293. }
  294.  
  295. # Download gcc.
  296. function get_gcc
  297. {
  298.     say "Starting download (gcc)..."
  299.     get_package "${gcc_name}" "${gcc_ver}" "${gcc_ext}" "${url_base}/${gcc_name}/${gcc_name}-${gcc_ver}/"
  300. }
  301.  
  302. # Build gcc.
  303. function build_gcc
  304. {
  305.     say "Starting build (gcc)..."
  306.     build_package "${gcc_name}" "${gcc_ver}" "${gcc_ext}" "${target}" "${prefix}" "${gcc_targ}" "${gcc_opt}"
  307. }
  308.  
  309. # Download and build gcc.
  310. function do_gcc
  311. {
  312.     [[ ${gcc_dl}    -eq 1 ]] && get_gcc
  313.     [[ ${gcc_build} -eq 1 ]] && build_gcc
  314. }
  315.  
  316. # If {bin,gcc}_{dl,build} are all 0, set them all to 1. Otherwise, leave them.
  317. if [[ ${bin_dl} -eq 0 && ${bin_build} -eq 0 && ${gcc_dl} -eq 0 && ${gcc_build} -eq 0 ]]; then
  318.     bin_dl=1
  319.     bin_build=1
  320.     gcc_dl=1
  321.     gcc_build=1
  322. fi
  323.  
  324. # Perform the builds.
  325. do_binutils
  326. gcc_opt="${gcc_opt} --enable-languages=${gcc_lang}"
  327. do_gcc
  328. say "All done."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement