Skip to content

Instantly share code, notes, and snippets.

@lilianmoraru
Last active December 17, 2016 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lilianmoraru/4ed4aa11c205b8499e8a to your computer and use it in GitHub Desktop.
Save lilianmoraru/4ed4aa11c205b8499e8a to your computer and use it in GitHub Desktop.
#! /bin/sh
## Warning: Please don't source this script from within' another script(note the use of "exit")
red_color="\033[1;31m"
green_color="\033[1;32m"
no_color="\033[0m"
throw_error() {
if [ ! -z "$1" ]; then
printf "${red_color}$1${no_color}\n"
fi
printf "\n${red_color}Compilation failed!${no_color}\n"
exit 1
}
## Commodity function to ensure that an error is thrown if a command fails to execute
## Usage: run (command) [error message if it fails]
run() {
eval "$1" || throw_error "$2"
}
ensure_tool_is_avaiable() {
if [ -z "$(which $1)" ]; then
printf "Installing $1:\n"
run \
"sudo apt-get install $1 -y" \
"\
Failed to install $1: check that you have an internet connection or\n\
that you have the rights to run 'sudo apt-get install $1'"
fi
}
ensure_tools_are_available() {
for tool in "$@"; do
ensure_tool_is_avaiable "$1"
done
}
## [error handling needed - "run" function] - did we have rights to remove the folder?
ensure_empty_folder() {
if [ -d "$1" ]; then
rm -rf "$1"
fi
mkdir -p "$1"
}
set_rust_build_folder() {
rust_build_location="${1:-"$HOME/build-rust"}"
ensure_empty_folder "${rust_build_location}"
}
## [error handling needed - "run" function] - did wget succeed? tar xf? rm -f?
get_and_extract_openwrt() {
local openwrt_link
local openwrt_archive
openwrt_link="${1:-"http://downloads.openwrt.org/chaos_calmer/15.05/ar71xx/generic/OpenWrt-SDK-15.05-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-x86_64.tar.bz2"}"
wget -c "${openwrt_link}"
openwrt_archive="$(basename ${openwrt_link})"
if [ -e "${openwrt_archive}" ]; then
tar xf "${openwrt_archive}"
rm -f "${openwrt_archive}"
mv */ openwrt-sdk
else
throw_error
fi
}
## [error handling needed - "run" function] - find worked? ls and ln -s?
setup_openwrt_toolchain() {
local openwrt_compiler_path
local openwrt_tools_path
openwrt_compiler_path="$(readlink -m $(find . -name 'mips-openwrt-linux-gcc' | head -1))"
openwrt_tools_path="$(dirname ${openwrt_compiler_path})"
## I don't take into consideration folder names with spaces but this should be ok for this purpose
for tool in $(ls ${openwrt_tools_path}/*uclibc-*); do
ln -sf "${tool}" "$(echo "${tool}" | sed "s/mips-openwrt-linux-uclibc/mips-linux-gnu/")"
done
export PATH="${openwrt_tools_path}:$PATH"
}
## [error handling needed - "run" function] - assuming git 1.7.3+, ...
prepare_rust_repository() {
local rust_remote_git
rust_remote_git="${1:-"https://github.com/rust-lang/rust.git"}"
rust_local_git="${2:-"$HOME/Git/rust"}"
if [ -d "${rust_local_git}" ]; then
cd "${rust_local_git}"
git pull --recurse-submodules
git submodule update --recursive
else
local git_folder
git_folder="$(dirname ${rust_local_git})"
mkdir -p "${git_folder}"
cd "${git_folder}"
git clone --recursive "${rust_remote_git}" rust
fi
cd "${rust_local_git}"
git reset --hard $(rustc -V | cut -d '(' -f 2 | cut -d ' ' -f 1)
}
prepare_final_installation_path() {
final_rust_destination="${1:-"$HOME/rust-mips-openwrt-linux"}"
ensure_empty_folder "${final_rust_destination}"
}
build_rust() {
cd "${rust_build_location}"
ensure_empty_folder "${rust_build_location}/build"
ln -s "${rust_local_git}" rust
cd build
../rust/configure --build=x86_64-unknown-linux-gnu --host=x86_64-unknown-linux-gnu --target=mips-unknown-linux-gnu --prefix="${final_rust_destination}"
local number_of_jobs
number_of_jobs="$(( $(nproc) + 1 ))"
make -j ${number_of_jobs}
make -j ${number_of_jobs} install
}
main() {
## Because we will need sudo rights later
sudo -v
ensure_tools_are_available git wget gcc build-essential
set_rust_build_folder
cd "${rust_build_location}"
get_and_extract_openwrt
setup_openwrt_toolchain
prepare_rust_repository
prepare_final_installation_path
build_rust
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment