4 # Copyright 2019-2020 Google LLC. All Rights Reserved.
5 # Copyright 2014-2017 Nest Labs Inc. All Rights Reserved.
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
22 # This file is a convenience script that will bootstrap the GNU
23 # autotools system for a project after any build system changes.
29 # Display program usage.
32 name="$(basename "${0}")"
34 echo "Usage: ${name} [ options ] [ -w <what> ]"
36 if [ "${1}" -ne 0 ]; then
37 echo "Try '${name} -h' for more information."
40 if [ "${1}" -ne 1 ]; then
42 echo " -h, --help Print this help, then exit."
43 echo " -I DIR Specify directory DIR as the root of the "
44 echo " nlbuild-autotools repository."
45 echo " -v, --verbose Verbosely report bootstrap progress."
46 echo " -w, --what WHAT Specify what part of the package should be "
47 echo " bootstrapped: all, config, make, or none "
48 echo " (default: all)."
56 # check_required_executable <path variable> <executable name>
58 # Check to ensure that the named executable with the path in the
59 # provided variable exists and is executable by the current user.
61 check_required_executable() {
66 stem="The required executable '${name}'"
68 if [ -z "${path}" ]; then
69 echo "${stem} could not be found."
74 elif [ ! -e "${path}" ] || [ ! -x "${path}" ]; then
75 stem="${stem} at '${path}'"
77 if [ ! -e "${path}" ]; then
78 echo "${stem} does not exist."
79 elif [ ! -x "${path}" ]; then
80 echo "${stem} is not executable."
83 echo "Please ensure '${name}' is available in PATH."
95 check_required_executables()
101 local have_libtoolize
104 check_required_executable ACLOCAL aclocal have_aclocal
105 check_required_executable AUTOCONF autoconf have_autoconf
106 check_required_executable AUTOHEADER autoheader have_autoheader
107 check_required_executable AUTOMAKE automake have_automake
108 check_required_executable LIBTOOLIZE libtoolize have_libtoolize
109 check_required_executable M4 m4 have_m4
111 if [ "${have_aclocal}" = "no" ] || [ "${have_autoconf}" = "no" ] || [ "${have_autoheader}" = "no" ] || [ "${have_automake}" = "no" ] || [ "${have_libtoolize}" = "no" ] || [ "${have_m4}" = "no" ]; then
113 --------------------------------------------------------------------------------
114 Your build host system is missing one or more of the above executables required
115 for bootstrapping this nlbuild-autotools-based package. You can either install
116 these yourself using package management tools available and appropriate for your
117 build host platform or you can build preselected versions of these executables
118 from source for this package on this build host by invoking:
120 make -C ${nlbuild_autotools_dir} tools
122 and then re-running this script which will use those executables.
123 --------------------------------------------------------------------------------
134 # Remove temporary files and directories used during the run of this
138 if [ -n "${LIBTOOLIZE}" ] && [ "${LIBTOOLIZE}" = "${BOOTSTRAP_TMPDIR}/libtoolize" ]; then
139 rm -f "${LIBTOOLIZE}"
142 if [ -n "${AUTOM4TE_CFG}" ]; then
143 rm -f "${AUTOM4TE_CFG}"
146 rm -r -f "${BOOTSTRAP_TMPDIR}"
150 # try_tool_action <tool executable>
152 # This attempts to run the specified build bootstrap tool, displaying
153 # the tool action to standard output if the verbose flag has been
156 # If the tool has failure status or if the verbose flag has been
157 # asserted, any error output from the tool is displayed to standard
158 # error. Likewise, if the tool has failure status, temporary files are
159 # removed and the script exits with that failed status.
169 # If the verbose flag is asserted, display to standard output the
170 # tool action that will be run.
172 if [[ -n "${verbose}" ]]; then
176 # Run the tool, capturing any errors and exit status.
178 errors="$(${action} 2>&1)"
181 # If there was failed exit status or if the verbose flag is
182 # asserted and if there were errors captured, display them to
185 if [[ ${status} -ne 0 ]] || [[ -n "${verbose}" ]]; then
186 if [[ -n "${errors}" ]]; then
191 # If there was failed exit status, clean up any temporary files
192 # and exit with the failed status.
194 if [[ ${status} -ne 0 ]]; then
203 nlbuild_autotools_dir=
205 # Parse out any command line options
207 while [ ${#} -gt 0 ]; do
214 nlbuild_autotools_dir="${2}"
226 all|make*|conf*|none)
232 echo "Unknown what value '${2}'." >&2
246 # Check to ensure that the location of the nlbuild-autotools directory
249 if [ -z "${nlbuild_autotools_dir}" ]; then
250 echo "$0: No -I option specified. Please provide the location of the nlbuild-autotools directory." >&2
253 elif [ ! -d "${nlbuild_autotools_dir}" ]; then
254 echo "$0: No such directory: ${nlbuild_autotools_dir}. Please provide a valid path to the nlbuild-autotools directory." >&2
259 # Establish some key directories
261 abs_top_hostdir="${nlbuild_autotools_dir}/tools/host"
263 # Figure out what sort of build host we are running on, stripping off
264 # any trailing version number information typically included on Darwin
267 host="$("${nlbuild_autotools_dir}/third_party/autoconf/config.guess" | sed -e 's/[[:digit:].]*$//g')"
269 # If possible, attempt to be self-sufficient, relying on GNU autotools
270 # executables installed along with the SDK itself.
272 if [ -d "${abs_top_hostdir}/${host}/bin" ]; then
273 export PATH="${abs_top_hostdir}/${host}/bin:${PATH}"
276 if [ -d "${abs_top_hostdir}/bin" ]; then
277 export PATH="${abs_top_hostdir}/bin:${PATH}"
280 ACLOCAL="$(command -v aclocal)"
281 AUTOCONF="$(command -v autoconf)"
282 AUTOHEADER="$(command -v autoheader)"
283 AUTOM4TE="$(command -v autom4te)"
284 AUTOMAKE="$(command -v automake)"
285 LIBTOOLIZE="$(command -v libtoolize || command -v glibtoolize)"
286 M4="$(command -v m4)"
287 TRUE="$(command -v true)"
297 # Establish, if necessary, some SDK-specific directories needed to
298 # override various paths in GNU autotools that otherwise expect to be
299 # absolute (e.g. /usr/share, etc.).
301 if [ -d "${abs_top_hostdir}/share" ]; then
302 if [ -d "${abs_top_hostdir}/share/autoconf" ]; then
303 export AC_MACRODIR="${abs_top_hostdir}/share/autoconf"
305 export autom4te_perllibdir="${abs_top_hostdir}/share/autoconf"
308 if [ -d "${abs_top_hostdir}/share/automake-1.14" ]; then
309 export PERL5LIB="${abs_top_hostdir}/share/automake-1.14:${PERL5LIB}"
311 make_action_options="--libdir ${abs_top_hostdir}/share/automake-1.14"
314 if [ -d "${abs_top_hostdir}/share/aclocal-1.14" ]; then
315 local_action_options="--automake-acdir=${abs_top_hostdir}/share/aclocal-1.14 ${local_action_options}"
318 if [ -d "${abs_top_hostdir}/share/aclocal" ]; then
319 local_action_options="--system-acdir=${abs_top_hostdir}/share/aclocal ${local_action_options}"
323 # Both autom4te.cfg and libtoolize, as installed from source, want to
324 # use absolute file system paths that cannot be
325 # overridden. Consequently, we create temporary, local versions of
326 # these, patched up with SDK-specific paths.
328 BOOTSTRAP_TMPDIR="$(mktemp -d /tmp/tmp.bootstrapXXXXXX)"
330 trap "removetmp" 1 2 3 15
333 # Generate any temporary files that need to be patched at run time
334 # with the location of the SDK tree, including:
336 # - The autom4te configuration file
337 # - The libtoolize executable script
340 if [ -r "${abs_top_hostdir}/share/autoconf/autom4te.cfg" ]; then
341 export AUTOM4TE_CFG="${BOOTSTRAP_TMPDIR}/autom4te.cfg"
343 sed -e "s,//share/autoconf,${abs_top_hostdir}/share/autoconf,g" < "${abs_top_hostdir}/share/autoconf/autom4te.cfg" > "${AUTOM4TE_CFG}"
346 if [ -r "${abs_top_hostdir}/${host}/bin/libtoolize" ]; then
347 export LIBTOOLIZE="${BOOTSTRAP_TMPDIR}/libtoolize"
349 sed -e "s,//share/libtool,${abs_top_hostdir}/share/libtool,g" -e "s,//share/aclocal,${abs_top_hostdir}/share/aclocal,g" < "${abs_top_hostdir}/${host}/bin/libtoolize" > "${LIBTOOLIZE}"
351 chmod 775 "${LIBTOOLIZE}"
354 # Before we get much further, check to ensure that the required
355 # executables are available and, if not, provide some actionable
358 check_required_executables
360 if [ -n "${verbose}" ]; then
363 echo ACLOCAL="${ACLOCAL}"
364 echo AUTOCONF="${AUTOCONF}"
365 echo AUTOHEADER="${AUTOHEADER}"
366 echo AUTOM4TE="${AUTOM4TE}"
367 echo AUTOMAKE="${AUTOMAKE}"
368 echo LIBTOOLIZE="${LIBTOOLIZE}"
372 echo AC_MACRODIR="${AC_MACRODIR}"
373 echo AUTOM4TE_CFG="${AUTOM4TE_CFG}"
374 echo PERL5LIB="${PERL5LIB}"
375 echo autom4te_perllibdir="${autom4te_perllibdir}"
377 echo local_action_options="${local_action_options}"
378 echo make_action_options="${make_action_options}"
381 # Set up the default actions for each bootstrap stage.
383 local_action="${ACLOCAL} ${verbose} ${local_action_options}"
384 header_action="${AUTOHEADER} ${verbose}"
385 tool_action="${LIBTOOLIZE} ${verbose} --automake --copy --force"
386 make_action="${AUTOMAKE} ${verbose} ${make_action_options} --add-missing --copy"
387 config_action="${AUTOCONF} ${verbose}"
389 # Determine what needs to be short-circuited based on the
390 # user-specified "what".
398 local_action="${TRUE}"
399 header_action="${TRUE}"
400 tool_action="${TRUE}"
401 make_action="${TRUE}"
405 local_action="${TRUE}"
406 header_action="${TRUE}"
407 config_action="${TRUE}"
411 local_action="${TRUE}"
412 header_action="${TRUE}"
413 tool_action="${TRUE}"
414 make_action="${TRUE}"
415 config_action="${TRUE}"
420 # Bootstrap the package.
422 try_tool_action "${local_action}"
423 try_tool_action "${tool_action}"
424 try_tool_action "${header_action}"
425 try_tool_action "${make_action}"
426 try_tool_action "${config_action}"
428 # Clean up any temporary files created.