Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Build a gcc cross compiler with binutils.
- # Common data.
- script=$(basename "$0") # Script name.
- dry=0 # Whether to perform a dry run.
- target= # Target triplet.
- url_base="http://ftp.gnu.org/gnu" # Base URL to retrieve files from.
- prefix="${HOME}/opt/cross" # Installation path.
- # Binutils data.
- bin_dl=0 # Whether to download binutils.
- bin_build=0 # Whether to build binutils.
- bin_name="binutils" # Binutils name.
- bin_ver="2.24" # Binutils version.
- bin_ext="tar.gz" # Binutils file extension.
- bin_targ="all install" # Binutils makefile targets.
- bin_opt="--disable-nls --disable-werror --with-sysroot" # Binutils configure options.
- # GCC data.
- gcc_dl=0 # Whether to download GCC.
- gcc_build=0 # Whether to build GCC.
- gcc_name="gcc" # GCC name.
- gcc_ver="4.9.2" # GCC version.
- gcc_ext="tar.gz" # GCC file extension.
- gcc_targ="all-gcc all-target-libgcc install-gcc install-target-libgcc" # GCC makefile targets.
- gcc_lang="c,c++" # GCC languages.
- gcc_opt="--disable-nls --disable-werror --without-headers" # GCC configure options.
- # Print a message
- # Args:
- # 1. The message to print.
- function say
- {
- echo -e ">> $1"
- }
- # Print an error message
- # Args:
- # 1. The error message to print.
- function saye
- {
- say "$1" >&2
- }
- # Print an error message and quit.
- # Args:
- # 1. The error message to print.
- function die
- {
- saye "$1"
- exit 1
- }
- # Print the usage message to stdout.
- function show_usage
- {
- cat <<EOF
- Usage: $0 [options] target
- Options:
- [--]help Show this help.
- --dry Echo commands instead of executing them.
- --url-base <url> Set the base URL for packages. Default: ${url_base}
- --prefix <path> Set the installation path. Default: ${prefix}
- --bin-dl Download binutils.
- --bin-build Build binutils.
- --bin-name <string> Set the name of binutils. Default: ${bin_name}
- --bin-ver <string> Set the version of binutils. Default: ${bin_ver}
- --bin-ext <string> Set the file extension of binutils. Default: ${bin_ext}
- --bin-targ <list> Set the make targets for binutils. Default: ${bin_targ}
- --bin-opt <list> Set the configure options for binutils. Default: ${bin_opt}
- --gcc-dl Download GCC.
- --gcc-build Build GCC.
- --gcc-ver <string> Set the version of GCC. Default: ${gcc_ver}
- --gcc-ext <string> Set the file extension of GCC. Default: ${gcc_ext}
- --gcc-targ <list> Set the make targets for GCC. Default: ${gcc_targ}
- --gcc-lang <list> Set the language targets for GCC. Default: ${gcc_lang}
- --gcc-opt <list> Set the configure options for GCC. Default: ${gcc_opt}
- Note: if any one of --{bin,gcc}-{dl,build} is passed, it implicitly disables the other three. By default, all four are enabled.
- EOF
- }
- # Print the usage message to stdout and exit with success.
- function quit_usage
- {
- show_usage
- exit 0
- }
- # Print the usage message to stderr and exit with failure.
- function die_usage
- {
- show_usage >&2
- exit 1
- }
- # Check the command line.
- [[ $# -lt 1 ]] && die_usage
- # Grab the options.
- say "Reading command-line..."
- while [[ $# -gt 0 ]]; do
- case "$1" in
- --help) quit_usage ;;
- --dry) dry=1 ;;
- --url-base) url_base=$2 ; shift ;;
- --prefix) prefix=$2 ; shift ;;
- --bin-dl) bin_dl=1 ;;
- --bin-build) bin_build=1 ;;
- --bin-name) bin_name=$2 ; shift ;;
- --bin-ver) bin_ver=$2 ; shift ;;
- --bin-ext) bin_ext=$2 ; shift ;;
- --bin-targ) bin_targ=$2 ; shift ;;
- --bin-opt) bin_opt=$2 ; shift ;;
- --gcc-dl) gcc_dl=1 ;;
- --gcc-build) gcc_build=1 ;;
- --gcc-name) gcc_name=$2 ; shift ;;
- --gcc-ver) gcc_ver=$2 ; shift ;;
- --gcc-ext) gcc_ext=$2 ; shift ;;
- --gcc-targ) gcc_targ=$2 ; shift ;;
- --gcc-lang) gcc_lang=$2 ; shift ;;
- --gcc-opt) gcc_opt=$2 ; shift ;;
- *) target=$1 ; shift ;;
- esac
- shift
- done
- [[ "x$target" = "x" ]] && die_usage
- # Print chosen options.
- say "Selected build options:"
- cat <<EOF
- * target: ${target}
- * dry: ${dry}
- * url-base: ${url_base}
- * prefix: ${prefix}
- * bin-dl: ${bin_dl}
- * bin-build: ${bin_build}
- * bin-name: ${bin_name}
- * bin-ver: ${bin_ver}
- * bin-ext: ${bin_ext}
- * bin-targ: ${bin_targ}
- * bin-opt: ${bin_opt}
- * gcc-dl: ${gcc_dl}
- * gcc-build: ${gcc_build}
- * gcc-name: ${gcc_name}
- * gcc-ver: ${gcc_ver}
- * gcc-ext: ${gcc_ext}
- * gcc-targ: ${gcc_targ}
- * gcc-lang: ${gcc_lang}
- * gcc-opt: ${gcc_opt}
- EOF
- # Call a command. If $dry != 0, echo the command instead.
- # Args:
- # 1. The command to run.
- # ... Any arguments to the ecommand.
- function call
- {
- local cmd="$1"
- shift
- if [[ $dry -eq 0 ]]; then
- "${cmd}" $*
- if [[ $? -ne 0 ]]; then
- die "Command failed: ${cmd}"
- fi
- else
- echo " * ${cmd} $*"
- fi
- }
- # Download a package.
- # Args:
- # 1. package name
- # 2. package version
- # 3. file extension
- # 4. package URL (if ends with /, "$name-$version.$ext" is appended)
- function get_package
- {
- [[ $# -lt 4 ]] && die "$FUNCNAME: Not enough arguments: got $# expected 4"
- local name="$1"
- local version="$2"
- local ext="$3"
- local url="$4"
- local filename="${name}-${version}.${ext}"
- if [[ "${url}" = */ ]]; then
- url="${url}${filename}"
- fi
- say "Downloading ${name}..."
- call wget "${url}"
- }
- function process_exists
- {
- local pid="$1"
- call kill -0 $pid #2>/dev/null
- return $?
- }
- # Extract a tarball with gzip or bzip2.
- # Args:
- # 1. The filename.
- function extract
- {
- [[ $# -lt 1 ]] && die "$FUNCNAME: Not enough arguments: got $# expected 1"
- local filename="$1"
- local cmd=
- case "$filename" in
- *.tar.gz|*.tgz)
- # Extract with tar and gzip.
- cmd="tar --checkpoint=.100 -xzf"
- ;;
- *.tar.bz2|*.tbz)
- # Extract with tar and bzip2.
- cmd="tar --checkpoint=.100 -xJf"
- ;;
- *.tar)
- # Extract with just tar.
- cmd="tar --checkpoint=.100 -xf"
- ;;
- *.7z)
- # Extract with p7zip.
- cmd="p7zip -d"
- ;;
- *.)
- # Directory: do nothing.
- ;;
- *)
- die "Unsupported file format $filename" ;; # Unknown archive: give up.
- esac
- if [[ "x$cmd" != "x" ]]; then
- say "Extracting archive..."
- call $cmd "$filename"
- echo
- fi
- }
- # Build and install a downloaded package.
- # Args:
- # 1. package name
- # 2. package version
- # 3. file extension
- # 4. target
- # 5. prefix
- # 6. makefile targets
- # 7. [configure options]
- function build_package
- {
- [[ $# -lt 6 ]] && die "$FUNCNAME: Not enough arguments: got $# expected 6"
- local name="$1"
- local version="$2"
- local ext="$3"
- local target="$4"
- local prefix="$5"
- local maketarg="$6"
- local options="$7"
- local filename="${name}-${version}.${ext}"
- local extract_dir="${name}-${version}"
- # Extract package.
- extract "${filename}"
- # Build it.
- say "Building ${name}..."
- local build_dir="build-${name}-${version}"
- [[ ! -d "${build_dir}" ]] && call mkdir "${build_dir}"
- [[ $dry -eq 0 ]] && cd "${build_dir}" # Can't use call as it runs commands in sub-shell.
- call "../${extract_dir}/configure" --prefix="${prefix}" --target="${target}" ${options}
- for targ in ${maketarg}; do
- call make "${targ}"
- done
- }
- # Download binutils.
- function get_binutils
- {
- say "Starting download (binutils)..."
- get_package "${bin_name}" "${bin_ver}" "${bin_ext}" "${url_base}/${bin_name}/"
- }
- # Build binutils.
- function build_binutils
- {
- say "Starting build (binutils)..."
- build_package "${bin_name}" "${bin_ver}" "${bin_ext}" "${target}" "${prefix}" "${bin_targ}" "${bin_opt}"
- }
- # Download and build binutils.
- function do_binutils
- {
- [[ ${bin_dl} -eq 1 ]] && get_binutils
- [[ ${bin_build} -eq 1 ]] && build_binutils
- }
- # Download gcc.
- function get_gcc
- {
- say "Starting download (gcc)..."
- get_package "${gcc_name}" "${gcc_ver}" "${gcc_ext}" "${url_base}/${gcc_name}/${gcc_name}-${gcc_ver}/"
- }
- # Build gcc.
- function build_gcc
- {
- say "Starting build (gcc)..."
- build_package "${gcc_name}" "${gcc_ver}" "${gcc_ext}" "${target}" "${prefix}" "${gcc_targ}" "${gcc_opt}"
- }
- # Download and build gcc.
- function do_gcc
- {
- [[ ${gcc_dl} -eq 1 ]] && get_gcc
- [[ ${gcc_build} -eq 1 ]] && build_gcc
- }
- # If {bin,gcc}_{dl,build} are all 0, set them all to 1. Otherwise, leave them.
- if [[ ${bin_dl} -eq 0 && ${bin_build} -eq 0 && ${gcc_dl} -eq 0 && ${gcc_build} -eq 0 ]]; then
- bin_dl=1
- bin_build=1
- gcc_dl=1
- gcc_build=1
- fi
- # Perform the builds.
- do_binutils
- gcc_opt="${gcc_opt} --enable-languages=${gcc_lang}"
- do_gcc
- say "All done."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement