Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / nlfaultinjection / repo / third_party / nlbuild-autotools / repo / scripts / bootstrap
1 #!/usr/bin/env bash
2
3 #
4 #    Copyright 2019-2020 Google LLC. All Rights Reserved.
5 #    Copyright 2014-2017 Nest Labs Inc. All Rights Reserved.
6 #
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
10 #
11 #    http://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18 #
19
20 #
21 #    Description:
22 #      This file is a convenience script that will bootstrap the GNU
23 #      autotools system for a project after any build system changes.
24 #
25
26 #
27 # usage
28 #
29 # Display program usage.
30 #
31 usage() {
32     name="$(basename "${0}")"
33
34     echo "Usage: ${name} [ options ] [ -w <what> ]"
35
36     if [ "${1}" -ne 0 ]; then
37         echo "Try '${name} -h' for more information."
38     fi
39
40     if [ "${1}" -ne 1 ]; then
41         echo ""
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)."
49         echo ""
50     fi
51
52     exit "${1}"
53 }
54
55 #
56 # check_required_executable <path variable> <executable name>
57 #
58 # Check to ensure that the named executable with the path in the
59 # provided variable exists and is executable by the current user.
60 #
61 check_required_executable() {
62     variable="${1}"
63     path="${!variable}"
64     name="${2}"
65     output="${3}"
66     stem="The required executable '${name}'"
67
68     if [ -z "${path}" ]; then
69         echo "${stem} could not be found."
70
71         eval "${output}=no"
72
73         return 1
74     elif [ ! -e "${path}" ] || [ ! -x "${path}" ]; then
75         stem="${stem} at '${path}'"
76
77         if [ ! -e "${path}" ]; then
78             echo "${stem} does not exist."
79         elif [ ! -x "${path}" ]; then
80             echo "${stem} is not executable."
81         fi
82
83         echo "Please ensure '${name}' is available in PATH."
84
85         eval "${output}=no"
86
87         return 1
88     fi
89
90     eval "${output}=yes"
91
92     return 0
93 }
94
95 check_required_executables()
96 {
97     local have_aclocal
98     local have_autoconf
99     local have_autoheader
100     local have_automake
101     local have_libtoolize
102     local have_m4
103
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
110
111     if [ "${have_aclocal}" = "no" ] || [ "${have_autoconf}" = "no" ] || [ "${have_autoheader}" = "no" ] || [ "${have_automake}" = "no" ] || [ "${have_libtoolize}" = "no" ] || [ "${have_m4}" = "no" ]; then
112         cat << EOF
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:
119
120     make -C ${nlbuild_autotools_dir} tools
121
122 and then re-running this script which will use those executables.
123 --------------------------------------------------------------------------------
124 EOF
125     
126       exit 1
127     
128     fi
129 }
130
131 #
132 # removetmp
133 #
134 # Remove temporary files and directories used during the run of this
135 # script.
136 #
137 removetmp() {
138     if [ -n "${LIBTOOLIZE}"  ] && [ "${LIBTOOLIZE}" = "${BOOTSTRAP_TMPDIR}/libtoolize" ]; then
139         rm -f "${LIBTOOLIZE}"
140     fi
141
142     if [ -n "${AUTOM4TE_CFG}" ]; then
143         rm -f "${AUTOM4TE_CFG}"
144     fi
145
146     rm -r -f "${BOOTSTRAP_TMPDIR}"
147 }
148
149 #
150 #  try_tool_action <tool executable>
151 #
152 #  This attempts to run the specified build bootstrap tool, displaying
153 #  the tool action to standard output if the verbose flag has been
154 #  asserted.
155 #
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.
160 #
161 try_tool_action()
162 {
163     local action
164     local errors
165     local status
166
167     action="${1}"
168
169     # If the verbose flag is asserted, display to standard output the
170     # tool action that will be run.
171
172     if [[ -n "${verbose}" ]]; then
173         echo "${action}"
174     fi
175
176     # Run the tool, capturing any errors and exit status.
177
178     errors="$(${action} 2>&1)"
179     status="${?}"
180
181     # If there was failed exit status or if the verbose flag is
182     # asserted and if there were errors captured, display them to
183     # standard error.
184
185     if [[ ${status} -ne 0 ]] || [[ -n "${verbose}" ]]; then
186         if [[ -n "${errors}" ]]; then
187             echo "${errors}" >&2
188         fi
189     fi
190
191     # If there was failed exit status, clean up any temporary files
192     # and exit with the failed status.
193
194     if [[ ${status} -ne 0 ]]; then
195         removetmp
196
197         exit ${status}
198     fi
199 }
200
201 what="all"
202 verbose=
203 nlbuild_autotools_dir=
204
205 # Parse out any command line options
206
207 while [ ${#} -gt 0 ]; do
208     case ${1} in
209     -h|--help)
210         usage 0
211         ;;
212
213     -I)
214         nlbuild_autotools_dir="${2}"
215         shift 2
216         ;;
217
218     -v|--verbose)
219         verbose="--verbose"
220         shift 1
221         ;;
222
223     -w|--what)
224         case "${2}" in
225
226         all|make*|conf*|none)
227             what="${2}"
228             shift 2
229             ;;
230
231         *)
232             echo "Unknown what value '${2}'." >&2
233             usage 1
234             ;;
235
236         esac
237         ;;
238
239     *)
240         usage 1
241         ;;
242
243     esac
244 done
245
246 # Check to ensure that the location of the nlbuild-autotools directory
247 # is sane.
248
249 if [ -z "${nlbuild_autotools_dir}" ]; then
250     echo "$0: No -I option specified. Please provide the location of the nlbuild-autotools directory." >&2
251     exit 1
252
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
255     exit 1
256
257 fi
258
259 # Establish some key directories
260
261 abs_top_hostdir="${nlbuild_autotools_dir}/tools/host"
262
263 # Figure out what sort of build host we are running on, stripping off
264 # any trailing version number information typically included on Darwin
265 # / Mac OS X.
266
267 host="$("${nlbuild_autotools_dir}/third_party/autoconf/config.guess" | sed -e 's/[[:digit:].]*$//g')"
268
269 # If possible, attempt to be self-sufficient, relying on GNU autotools
270 # executables installed along with the SDK itself.
271
272 if [ -d "${abs_top_hostdir}/${host}/bin" ]; then
273     export PATH="${abs_top_hostdir}/${host}/bin:${PATH}"
274 fi
275
276 if [ -d "${abs_top_hostdir}/bin" ]; then
277     export PATH="${abs_top_hostdir}/bin:${PATH}"
278 fi
279
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)"
288
289 export ACLOCAL
290 export AUTOCONF
291 export AUTOHEADER
292 export AUTOM4TE
293 export AUTOMAKE
294 export LIBTOOLIZE
295 export M4
296
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.).
300
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"
304
305         export autom4te_perllibdir="${abs_top_hostdir}/share/autoconf"
306     fi
307
308     if [ -d "${abs_top_hostdir}/share/automake-1.14" ]; then
309         export PERL5LIB="${abs_top_hostdir}/share/automake-1.14:${PERL5LIB}"
310
311         make_action_options="--libdir ${abs_top_hostdir}/share/automake-1.14"
312     fi
313
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}"
316     fi
317
318     if [ -d "${abs_top_hostdir}/share/aclocal" ]; then
319         local_action_options="--system-acdir=${abs_top_hostdir}/share/aclocal ${local_action_options}"
320     fi
321 fi
322
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.
327
328 BOOTSTRAP_TMPDIR="$(mktemp -d /tmp/tmp.bootstrapXXXXXX)"
329
330 trap "removetmp" 1 2 3 15
331
332 #
333 # Generate any temporary files that need to be patched at run time
334 # with the location of the SDK tree, including:
335 #
336 #   -  The autom4te configuration file
337 #   -  The libtoolize executable script
338 #
339
340 if [ -r "${abs_top_hostdir}/share/autoconf/autom4te.cfg" ]; then
341     export AUTOM4TE_CFG="${BOOTSTRAP_TMPDIR}/autom4te.cfg"
342
343     sed -e "s,//share/autoconf,${abs_top_hostdir}/share/autoconf,g" < "${abs_top_hostdir}/share/autoconf/autom4te.cfg" > "${AUTOM4TE_CFG}"
344 fi
345
346 if [ -r "${abs_top_hostdir}/${host}/bin/libtoolize" ]; then
347     export LIBTOOLIZE="${BOOTSTRAP_TMPDIR}/libtoolize"
348
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}"
350
351     chmod 775 "${LIBTOOLIZE}"
352 fi
353
354 # Before we get much further, check to ensure that the required
355 # executables are available and, if not, provide some actionable
356 # guidance.
357
358 check_required_executables
359  
360 if [ -n "${verbose}" ]; then
361     echo PATH="${PATH}"
362
363     echo ACLOCAL="${ACLOCAL}"
364     echo AUTOCONF="${AUTOCONF}"
365     echo AUTOHEADER="${AUTOHEADER}"
366     echo AUTOM4TE="${AUTOM4TE}"
367     echo AUTOMAKE="${AUTOMAKE}"
368     echo LIBTOOLIZE="${LIBTOOLIZE}"
369     echo M4="${M4}"
370     echo TRUE="${TRUE}"
371
372     echo AC_MACRODIR="${AC_MACRODIR}"
373     echo AUTOM4TE_CFG="${AUTOM4TE_CFG}"
374     echo PERL5LIB="${PERL5LIB}"
375     echo autom4te_perllibdir="${autom4te_perllibdir}"
376
377     echo local_action_options="${local_action_options}"
378     echo make_action_options="${make_action_options}"
379 fi
380
381 # Set up the default actions for each bootstrap stage.
382
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}"
388
389 # Determine what needs to be short-circuited based on the
390 # user-specified "what".
391
392 case "${what}" in
393
394     all)
395         ;;
396
397     conf*)
398         local_action="${TRUE}"
399         header_action="${TRUE}"
400         tool_action="${TRUE}"
401         make_action="${TRUE}"
402         ;;
403
404     make*)
405         local_action="${TRUE}"
406         header_action="${TRUE}"
407         config_action="${TRUE}"
408         ;;
409
410     none)
411         local_action="${TRUE}"
412         header_action="${TRUE}"
413         tool_action="${TRUE}"
414         make_action="${TRUE}"
415         config_action="${TRUE}"
416         ;;
417
418 esac
419
420 # Bootstrap the package.
421
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}"
427
428 # Clean up any temporary files created.
429
430 removetmp