3 # Exit if an error or an unset variable
6 # make sure unmatched glob gives empty
9 root_dir
=$
(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
10 tools_dir
=${TOOLS_DIR:-${root_dir}/tools}
11 downloads_dir
=${DL_DIR:-${root_dir}/downloads}
12 tool_overrides_dir
=$root_dir/make
/tool_install
21 [ "$arg" = "-r" ] && remove
=true
22 [ "$arg" = "-f" ] && force
=true
23 [ "$arg" = "-n" ] && includes
=false
30 if [[ "$uname" != [LD
]* ]]
39 CURL_OPTIONS
=(--silent -L)
44 ################################################################################
46 ################################################################################
55 ## Downloads a file if it doesn't exist
57 #2 Output filename (optional)
58 #3+ Additional options to pass to curl
60 #out_file: path of the downloaded file
61 function download_file
63 out_file
="$downloads_dir/${2:-$(basename "$1")}"
65 if ! [ -f "$out_file" ]
67 mkdir
-p "$downloads_dir" && \
68 cd "$downloads_dir" && \
69 echo "Downloading $1" && \
70 curl
"${CURL_OPTIONS[@]}" "${@:3}" -o "$out_file" "$1"
76 #2 The output directory
82 ## Extracts a 7zip file
83 #1 The file to extract
84 #2 The output directory
85 function sevenzip_extract
87 if [ "$uname" = Windows
]
95 ## Extracts a tar file
96 #1 The file to extract
97 #2 The output directory
105 #2 Extract directory (optional)
106 no_extract
=false
# Optional
108 #out_dir: directory the file was extracted into
109 function extract_file
115 mkdir
-p "$out_dir" && \
118 zip_extract
"$1" "$out_dir"
121 sevenzip_extract
"$1" "$out_dir"
124 tar_extract
"$1" "$out_dir"
136 ## Verifies an md5 file
139 function md5_verify_file
141 if [ "$uname" = Darwin
]
143 [[ "$(md5 "$2")" = *"$(awk '{print $1}' "$1")"* ]]
145 ( cd "$downloads_dir" && md5sum -c "$1" )
149 ################################################################################
151 ################################################################################
153 function validate_target
{ false
; }
157 rm -rf "$tools_dir/$tool_install_name"
158 rm -f "$tools_dir/$tool".
{sh
,mk
}
161 declare -a depends
=()
162 function install_deps
164 # Workaround for set -u and empty array
165 for dep
in "${depends[@]:+${depends}}"
167 BATCH
="$batch" "${BASH_SOURCE[0]}" "$dep"
172 ## Downloads and verifies the tool
174 #tool_url: the url to download the tool from
178 function download_and_verify
181 download_file
"$tool_url" && \
182 downloaded_file
=$out_file && \
183 if [ -n "${tool_md5_url:-}" ]
185 download_file
"$tool_md5_url" "$(basename "$downloaded_file").md5" --silent && \
186 if ! md5_verify_file
"$out_file" "$downloaded_file"
188 mv -f "$downloaded_file"{,.rej
} && \
189 mv -f "$downloaded_file".md5
{,.rej
} && \
192 elif [ -n "${tool_md5:-}" ]
194 if [[ "$tool_md5"* != "$(cd "$downloads_dir" && md5sum "$downloaded_file")" ]]
196 mv -f "$downloaded_file"{,.rej
} && \
203 function tool_is_installed
{ [ -e "$full_tool_install_name" ] ||
which "$tool" &>/dev
/null
; }
205 ## Downloads and extracts the tool
207 #tool_url: the url to download the tool from
208 #tool_install_name: the directory or file the tool will be installed as
210 #tool_extract_dir: Directory to extract into (useful if build required)
211 function download_and_extract
213 local full_tool_install_name
="$tools_dir/$tool_install_name"
214 if ! tool_is_installed ||
$force
216 download_and_verify || exit_error
"Failed to verify $downloaded_file"
217 rm -rf "$full_tool_install_name" && \
218 extract_file
"$downloaded_file" "${tool_extract_dir:-$tools_dir}"
222 function build_and_install
{ true
; } # Most tools don't need this step
224 ## Write modules that are included by this script and make
229 function write_modules
231 if [ -n "$bin_subdir" ]
233 bin_dir
="$tools_dir/$tool_install_name/$bin_subdir"
238 local new_path
="$bin_dir"':${PATH}'
239 # Write shell module file
240 echo 'if [[ ":$PATH:" != *":'"$bin_dir"':"* ]]; then export PATH='"$new_path"'; fi' > "$tools_dir/$module_file".sh
241 # Write make module file
242 echo 'ifeq ($(findstring :'"$bin_dir"':,:$(PATH):),)' > "$tools_dir/$module_file".mk
243 echo "export PATH := $new_path" >> "$tools_dir/$module_file".mk
244 echo "endif" >> "$tools_dir/$module_file".mk
249 function source_includes
253 for module
in "$tools_dir"/*.sh
260 ################################################################################
261 # Peform tool install
262 ################################################################################
263 source_includes || exit_error
"failed to source includes"
265 source "$tool_overrides_dir/${tool}.sh"
269 remove || exit_error
"Failed to remove ${tool}"
271 validate_target || exit_error
"${tool} is not a valid target"
273 install_deps || exit_error
"Failed to install dependencies for ${tool}"
275 download_and_extract || exit_error
"Failed to download and extract ${tool}"
277 build_and_install || exit_error
"Failed to build and install ${tool}"
279 write_modules || exit_error
"Failed to write modules for ${tool}"