Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / nlfaultinjection / repo / third_party / nlbuild-autotools / repo / tools / packages / build
1 #!/usr/bin/env bash
2
3 #
4 #    Copyright (c) 2020 Project nlbuild-autotools Authors. All Rights Reserved.
5 #    Copyright (c) 2018 Google LLC. All Rights Reserved.
6 #    Copyright (c) 2014-2018 Nest Labs Inc. All Rights Reserved.
7 #
8 #    Licensed under the Apache License, Version 2.0 (the "License");
9 #    you may not use this file except in compliance with the License.
10 #    You may obtain a copy of the License at
11 #
12 #    http://www.apache.org/licenses/LICENSE-2.0
13 #
14 #    Unless required by applicable law or agreed to in writing, software
15 #    distributed under the License is distributed on an "AS IS" BASIS,
16 #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 #    See the License for the specific language governing permissions and
18 #    limitations under the License.
19 #
20
21 #
22 #    Description:
23 #      This file is a convenience script for building, for the current
24 #      build host system, the minimal, core set of GNU autotools on
25 #      which other projects' build systems depend.
26 #
27
28 EXTENSIONS="tar.gz tar.xz"
29
30 VERBOSE=0
31
32 stem=tools/packages
33 abs_srcdir=`pwd`
34 abs_top_srcdir=`echo ${abs_srcdir} | sed -e s,/${stem},,g`
35
36 abs_top_hostdir="${abs_top_srcdir}/tools/host"
37
38 #
39 # usage
40 #
41 # Display program usage.
42 #
43 usage() {
44     name=`basename ${0}`
45
46     echo "Usage: ${name} [ options ] [ -- <package> ... ]"
47
48     if [ $1 -ne 0 ]; then
49         echo "Try '${name} -h' for more information."
50     fi
51
52     if [ $1 -ne 1 ]; then
53         echo ""
54         echo "  --arch ARCH      Build the package for ARCH host architecture."
55         echo "  -h, --help       Print this help, then exit."
56         echo "  --builddir DIR   Build the packages in DIR."
57         echo "  --destdir DIR    Install the built tools to directory DIR."
58         echo "  --srcdir DIR     Find package, version, and URL metadata in DIR."
59         echo "  -v, --verbose    Enable verbose output."
60         echo ""
61     fi
62
63     exit $1
64 }
65
66 #
67 # error <...>
68 #
69 # Display to standard error the specified arguments
70 #
71 error() {
72     echo "${*}" >&2
73 }
74
75 #
76 # verbose <...>
77 #
78 # Display to standard error the specified arguments
79 #
80 verbose() {
81     echo "${*}" >&2
82 }
83
84 #
85 # separator
86 #
87 # Display to standard error a log separator.
88 #
89 separator() {
90     verbose "--------------------------------------------------------------------------------"
91 }
92
93 #
94 # trailer
95 #
96 # Display to standard error a log trailer.
97 #
98 trailer() {
99     verbose "================================================================================"
100 }
101
102 #
103 # banner <message ...>
104 #
105 # Display to standard error a log banner with a message.
106 #
107 banner() {
108     trailer
109
110     verbose "${*}"
111 }
112
113 #
114 # subbanner <message ...>
115 #
116 # Display to standard error a log subbanner with a message.
117 #
118 subbanner() {
119     separator
120
121     verbose "${*}"
122 }
123
124 #
125 # find_archive <subdir> <package> <extensions...>
126 #
127 # Attempt to find the specified package archive in the provided
128 # subdirectory matching one of the specified extensions.
129 #
130 find_archive() {
131     local subdir="${1}"
132     local package="${2}"
133     local archive
134
135     shift 2
136
137     for extension in ${*}; do
138         local maybe_archive="${subdir}/${package}.${extension}"
139
140         verbose "  FIND     ${maybe_archive}"
141
142         if [ -r "${maybe_archive}" ]; then
143             archive="${maybe_archive}"
144             break
145         fi
146     done
147
148     echo "${archive}"
149 }
150
151 #
152 # find_versioned_or_nonversioned_archive <subdir> <package> <version> <extensions...>
153 #
154 # Attempt to find the specified version-qualified or non-qualified
155 # package archive in the provided subdirectory matching one of the
156 # specified extensions.
157 #
158 find_versioned_or_nonverioned_archive() {
159     local subdir="${1}"
160     local package="${2}"
161     local version="${3}"
162     local fqpackage="${package}-${version}"
163     local archive
164
165     shift 3
166
167     archive="`find_archive ${subdir} ${fqpackage} ${*}`"
168
169     if [ -z "${archive}" ]; then
170         archive="`find_archive ${subdir} ${package} ${*}`"
171     fi
172
173     echo "${archive}"
174 }
175
176 #
177 # fetch_url_with_command <fetchdir> <url> <command ...>
178 #
179 # Attempt to fetch the specified URL to the provided directory with
180 # the provided command.
181 #
182 fetch_url_with_command() {
183     local fetchdir="${1}"
184     local url="${2}"
185     local executable="`which ${3}`"
186     local curdir="`pwd`"
187     local status=1
188
189     shift 2
190
191     if [ -x "${executable}" ]; then
192         cd "${fetchdir}"
193
194             verbose "  `echo ${1} | tr '[[:lower:]]' '[[:upper:]]'`     ${url}"
195
196             ${*} "${url}"
197
198             status=${?}
199
200             if [ ${?} -ne 0 ]; then
201                     error "Failed to fetch ${url} with ${1}."
202             fi
203
204         cd "${curdir}"
205     fi
206
207     return ${status}
208 }
209
210 #
211 # fetch_url <fetchdir> <url>
212 #
213 # Attempt to fetch the specified URL to the provided directory with
214 # either wget or curl.
215 #
216 fetch_url() {
217     local fetchdir="${1}"
218     local url="${2}"
219
220     # Try to fetch the package using wget or curl
221
222     fetch_url_with_command "${fetchdir}" "${url}" wget --tries 4 --no-check-certificate --quiet ||
223         fetch_url_with_command "${fetchdir}" "${url}" curl --retry 4 --insecure --silent --remote-name
224 }
225
226 #
227 # fetch_package <finddir> <fetchdir> <package> <version> <url>
228 #
229 # Attempt to fetch the specified package version to <fetchdir>, if a
230 # local archive does not already exist in <finddir> or <fetchdir>,
231 # from the provided URL.
232 #
233 fetch_package() {
234     local finddir="${1}"
235     local fetchdir="${2}"
236     local package="${3}"
237     local version="${4}"
238     local url="${5}"
239     local fqpackage="${package}-${version}"
240
241     # Check whether a local archive already exists in <finddir> or <fetchdir>.
242
243     archive="`find_versioned_or_nonverioned_archive ${finddir} ${package} ${version} ${EXTENSIONS}`"
244
245     if [ -z "${archive}" ]; then
246         archive="`find_versioned_or_nonverioned_archive ${fetchdir} ${package} ${version} ${EXTENSIONS}`"
247     fi
248
249     # If no archive was found, attempt to fetch it.
250
251     if [ -z "${archive}" ]; then
252         verbose "  FETCH    ${url}"
253
254         fetch_url "${fetchdir}" "${url}"
255
256     else
257         error "No, found ${archive} locally."
258
259     fi
260 }
261
262 #
263 # removetmp
264 #
265 # Remove temporary files and directories used during the run of this
266 # script.
267 #
268 removetmp() {
269     if [ -O "${LIBTOOLIZE}" ]; then
270         rm -f "${LIBTOOLIZE}"
271     fi
272     if [ -n "${AUTOM4TE_CFG}" ]; then
273         rm -f "${AUTOM4TE_CFG}"
274     fi
275 }
276
277 #
278 # file_extension
279 #
280 # Returns a filename's extension, if any.
281 #
282 file_extension() {
283     ext=${1##*.}
284     [[ $ext != $1 ]] || ext=
285     echo $ext
286 }
287
288 #
289 # patch_directory <directory> <patch arguments> <patch file> [ ... ]
290 #
291 # Patch the specified directory by applying the provided patched files
292 # using the specified patch arguments. The specified patch files may
293 # be uncompressed or compressed with any of bz2, gz, xz, compress, or
294 # zip.
295 #
296 patch_directory() {
297     local directory="${1}"
298     local patchargs="${2}"
299
300     shift 2
301
302     verbose "  PATCH    ${directory}"
303
304     for patch in "${*}"; do
305         verbose "  PATCH    ${patch}"
306
307         case $(file_extension "${patch}") in
308
309             bz2)
310                 uncompressor="bunzip2 -c"
311                 ;;
312
313             gz)
314                 uncompressor="gunzip -c"
315                 ;;
316
317             xz)
318                 uncompressor="xz -d -c"
319                 ;;
320
321             Z)
322                 uncompressor="uncompress -c"
323                 ;;
324
325             zip)
326                 uncompressor="unzip -p"
327                 ;;
328
329             *)
330                 uncompressor="cat"
331                 ;;
332
333         esac
334
335         ${uncompressor} "${patch}" | patch ${patchargs} -d "${directory}" || exit 1;
336
337     done
338 }
339
340 #
341 # Determine the number of CPU threads available to this process
342 #
343 nthreads() {
344     local nthreads
345
346     case `uname -s` in
347
348     Darwin)
349         nthreads=$(sysctl -n hw.ncpu)
350         ;;
351
352     Linux)
353         nthreads=$(nproc)
354         ;;
355
356     *)
357         nthreads=1
358         ;;
359
360     esac
361
362     echo "${nthreads}"
363 }
364
365 #
366 # build_package <package> <version> <host> <patchdir> <archivedir> <builddir> <destdir>
367 #
368 # Build the specified package version for <host> in <builddir> from
369 # the archive found in <archivedir>, after applying the patches in
370 # <patchdir> (if any), and install it into the specified destination
371 # directory, <destdir>.
372 #
373 build_package() {
374     local package="${1}"
375     local version="${2}"
376     local host="${3}"
377     local patchdir="${4}"
378     local archivedir="${5}"
379     local builddir="${6}"
380     local destdir="${7}"
381     local fqpackage="${package}-${version}"
382     local curdir=`pwd`
383     local archive
384     local jobs
385
386     verbose "  CHECK    ${package}"
387
388     archive="`find_versioned_or_nonverioned_archive ${archivedir} ${package} ${version} ${EXTENSIONS}`"
389
390     if [ -z "${archive}" ]; then
391         error "Could not find an archive for ${package}."
392         exit 1
393     fi
394
395     if [ ! -d "${archivedir}/${fqpackage}" ]; then
396         verbose "  TAR      ${archive}"
397         tar --directory "${archivedir}" -xf "${archive}" || exit ${?}
398     fi
399
400     # If necessary, patch the expanded package.
401
402     if [ -d "${patchdir}" ]; then
403         patch_directory "${archivedir}/${fqpackage}" "-s -p1" ${patchdir}/*.patch*
404     fi
405
406     # If possible, attempt to be self-sufficient, relying on GNU autotools
407     # executables installed along with the SDK itself.
408
409     if [ -d "${destdir}/${host}/bin" ]; then
410         export PATH="${destdir}/${host}/bin:${PATH}"
411     fi
412
413     if [ -d "${destdir}/bin" ]; then
414         export PATH="${destdir}/bin:${PATH}"
415     fi
416
417     export ACLOCAL=`which aclocal`
418     export AUTOCONF="`which autoconf`"
419     export AUTOHEADER="`which autoheader`"
420     export AUTOM4TE="`which autom4te`"
421     export AUTOMAKE="`which automake`"
422     export LIBTOOLIZE="`which libtoolize || which glibtoolize`"
423     export M4=`which m4`
424
425     # Establish, if necessary, some SDK-specific directories needed to
426     # override various paths in GNU autotools that otherwise expect to be
427     # absolute (e.g. /usr/share, etc.).
428
429     if [ -d "${destdir}/share" ]; then
430         if [ -d "${destdir}/share/autoconf" ]; then
431             export AC_MACRODIR="${destdir}/share/autoconf"
432
433             export autom4te_perllibdir="${destdir}/share/autoconf"
434         fi
435
436         if [ -d "${destdir}/share/automake-1.14" ]; then
437             export PERL5LIB="${destdir}/share/automake-1.14:${PERL5LIB}"
438         fi
439     fi
440
441     trap "removetmp" 1 2 3 9 15
442
443     #
444     # Generate any temporary files that need to be patched at run time
445     # with the location of the SDK tree, including:
446     #
447     #   -  The autom4te configuration file
448     #   -  The libtoolize executable script
449     #
450
451     if [ -r "${destdir}/share/autoconf/autom4te.cfg" ]; then
452         export AUTOM4TE_CFG="${destdir}/autom4te.cfg"
453
454         sed -e "s,//share/autoconf,${destdir}/share/autoconf,g" < "${destdir}/share/autoconf/autom4te.cfg" > "${AUTOM4TE_CFG}"
455     fi
456
457     if [ -r "${destdir}/${host}/bin/libtoolize" ]; then
458         export LIBTOOLIZE="${destdir}/libtoolize"
459
460         sed -e "s,//share/libtool,${destdir}/share/libtool,g" -e "s,//share/aclocal,${destdir}/share/aclocal,g" < "${destdir}/${host}/bin/libtoolize" > "${LIBTOOLIZE}"
461
462         chmod 775 "${LIBTOOLIZE}"
463     fi
464
465     # Configure the package
466
467     if [ ${VERBOSE} -gt 0 ]; then
468         CONFIGURE_FLAGS=""
469     else
470         CONFIGURE_FLAGS="--quiet"
471     fi
472
473     verbose "  CONFIG   ${package}"
474
475     cd "${builddir}"
476
477         ${archivedir}/${fqpackage}/configure ${CONFIGURE_FLAGS} --prefix=/ --exec-prefix=/${host} || exit ${?}
478
479     cd "${curdir}"
480
481     # Build the package
482
483     jobs=$(nthreads)
484
485     verbose "  JOBS     ${jobs}"
486
487     verbose "  MAKE     ${package}"
488
489     make -j ${jobs} V=${VERBOSE} -C "${builddir}" all || exit ${?}
490
491     # Install / stage the package
492
493     verbose "  INSTALL  ${package}"
494
495     make -j ${jobs} V=${VERBOSE} -C "${builddir}" DESTDIR="${destdir}" install || exit ${?}
496
497     # Remove any temporary files created.
498
499     removetmp
500 }
501
502 # Parse out any command line options
503
504 while [ ${#} -gt 0 ]; do
505     case ${1} in
506         --arch)
507             ARCH=${2}
508             shift 2
509             ;;
510
511         -h|--help)
512             usage 0
513             ;;
514
515         --builddir)
516             BUILDDIR=${2}
517             shift 2
518             ;;
519
520         --destdir)
521             DESTDIR=${2}
522             shift 2
523             ;;
524
525         --srcdir)
526             SRCDIR=${2}
527             shift 2
528             ;;
529
530         -v|--verbose)
531             $((VERBOSE++))
532             shift 1
533             ;;
534
535         --)
536             shift 1
537             break
538             ;;
539
540         *)
541             usage 1
542             ;;
543     esac
544 done
545
546 # If the architecture is not specified, fail.
547
548 if [ -z "${ARCH}" ]; then
549     error "The host architecture was not specified via --arch."
550
551     usage 1
552 fi
553
554 # If the --builddir option wasn't specified, then provide a default.
555
556 if [ -z "${BUILDDIR}" ]; then
557     BUILDDIR="`pwd`"
558 fi
559
560 # If the --destdir option wasn't specified, then provide a default.
561
562 if [ -z "${DESTDIR}" ]; then
563     DESTDIR=${abs_top_hostdir}
564 fi
565
566 # If the --srcdir option wasn't specified, then provide a default.
567
568 if [ -z "${SRCDIR}" ]; then
569     SRCDIR="`pwd`"
570 fi
571
572 # Determine what packages to build
573
574 if [ ${#} -gt 0 ]; then
575     PACKAGES="${*}"
576
577 else
578     PACKAGES="`cat ${SRCDIR}/packages`"
579
580 fi
581
582 # Build each package
583
584 banner "Building GNU autotools for ${ARCH}..."
585
586 for package in ${PACKAGES}; do
587     url="`cat ${SRCDIR}/${package}/${package}.url`"
588     version="`cat ${SRCDIR}/${package}/${package}.version`"
589     patchdir="${SRCDIR}/${package}/${package}.patches"
590
591     # Fetch, if necessary, the package from the canonical source location.
592
593     subbanner "  CHECK    ${package}"
594
595     fetch_package "${SRCDIR}/${package}" "${BUILDDIR}" "${package}" "${version}" "${url}"
596
597     # Build and install the package.
598
599     verbose "  MKDIR    ${BUILDDIR}/build/${ARCH}/${package}-${version}"
600
601     mkdir -p "${BUILDDIR}/build/${ARCH}/${package}-${version}" || exit ${?}
602
603     build_package "${package}" "${version}" "${ARCH}" "${patchdir}" "${BUILDDIR}" "${BUILDDIR}/build/${ARCH}/${package}-${version}" "${DESTDIR}"
604 done
605
606 trailer