spec files - change pmda_remove to use a subshell
[pcp.git] / Makepkgs
blobb985b29254c554648df7bb7e7453ae674ed7e9f0
1 #!/bin/sh
3 # Make whichever packages the system supports
5 # Copyright (c) 2012-2015 Red Hat.
6 # Copyright (c) 2004 Silicon Graphics, Inc. All Rights Reserved.
7 #
8 # This program is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by the
10 # Free Software Foundation; either version 2 of the License, or (at your
11 # option) any later version.
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 # for more details.
19 topdir=`pwd`
20 signed=false
21 verbose=false
22 srconly=false
23 nonrpm=unknown
24 nondeb=unknown
25 vector=false
26 webjs=false
27 docker=false
29 if [ ! -d .git ]
30 then
31 echo "Error: can only be run from within the PCP git repository"
32 exit 1
35 if [ ! -f VERSION.pcp ]
36 then
37 echo "Error: can only be run from within the PCP source tree"
38 exit 1
41 # Find where a GNU tool is hiding, look for this line:
42 # "Report bugs to <bug-$gnutool@gnu.org>"
43 # or this line:
44 # "FreeBSD $gnutool YYYYMMDD"
45 # in the $gnutool --help output
47 gnu_tool()
49 prefix="$1"
50 shift
51 envvar="$1"
52 shift
53 oldcmd="$1"
54 shift
56 # Not in the environment, go search for it in the known locations.
58 if [ -z "$envvar" ]
59 then
60 for command in $oldcmd $@
62 exec=`which $command 2>/dev/null`
63 if [ -n "$exec" ]
64 then
65 if $command --help 2>&1 | egrep "(bug-$oldcmd@gnu.org)|(FreeBSD $gnutool)|(NetBSD $gnutool)|(Apple $gnutool)" >/dev/null
66 then
67 echo $exec
68 return 0
71 done
72 echo >&2 "$prefix: can't find GNU $oldcmd in the commands $oldcmd or $@"
73 [ $prefix = Error ] && exit 1
74 return 1
77 # Use the environment request and ensure it is a full path (do not
78 # change this behaviour, it is required for backward-compatibility
79 # - QA failures will result if you choose to ignore this warning).
81 command=`which $envvar 2>/dev/null`
82 if [ -z "$command" ]
83 then
84 echo >&2 "$prefix: $oldcmd (from the environment) does not exist!"
85 [ $prefix = Error ] && exit 1
86 return 1
88 echo $command
89 return 0
92 export MAKE=`gnu_tool Error "$MAKE" make gmake`
93 export TAR=`gnu_tool Warning "$TAR" tar gtar gnutar`
94 export ZIP=`gnu_tool Error "$ZIP" gzip gnuzip`
96 # check if tar supports --transform else we must use git-archive later on (e.g. RHEL5)
97 [ ! -z "$TAR" ] && $TAR --help | 2>&1 grep -q transform || USE_GIT_ARCHIVE=1
99 tmp=`mktemp -d /tmp/pcp.XXXXXXXXX` || exit 1
100 trap "rm -rf $tmp" 0 1 2 3 15
102 configure=./configure
103 target=`uname -s | tr 'A-Z' 'a-z'`
105 if [ -x /usr/bin/rpmbuild ]
106 then
107 # On RPM-based platforms, rpm macros provide an excellent set of defaults.
108 # This set is based on Fedora 22 %configure macro from /usr/lib/rpm/macros,
109 # and is known to work as far back as RHEL5.
110 configopts=`rpmbuild --eval '--prefix=%{_prefix} --exec-prefix=%{_exec_prefix} --bindir=%{_bindir} --sbindir=%{_sbindir} --sysconfdir=%{_sysconfdir} --datadir=%{_datadir} --includedir=%{_includedir} --libdir=%{_libdir} --libexecdir=%{_libexecdir} --localstatedir=%{_localstatedir} --sharedstatedir=%{_sharedstatedir} --mandir=%{_mandir}' 2>/dev/null`
111 # rpmbuild clears the environment, so force these settings into the
112 # rpm build configure
114 configopt="$configopts --with-make=$MAKE --with-tar=$TAR --with-zip=$ZIP"
115 elif [ -f /etc/debian_version ]
116 then
117 # On Debian-based platforms, the debian/rules file already captures
118 # most of the required configure settings in $(configure_paths)
120 configopts=''
121 elif [ $target = darwin ]
122 then
123 # On Mac OS X platforms, we install away from /usr/bin, /usr/lib
124 # and so on because of security requirements in recent versions.
126 configopts="--prefix=/usr/local --sysconfdir=/etc --localstatedir=/var"
127 elif [ $target = netbsd ]
128 then
129 # Try to mimic where pkgsrc/pkgin place things
131 configopts="--prefix=/usr/pkg --exec-prefix=/usr/pkg --mandir=/usr/pkg/man --with-rcdir=/etc/rc.d --localstatedir=/usr/pkg --with-rundir=/var/run --with-tmpdir=/var/tmp --with-logdir=/var/log/pcp"
132 else
133 # Bog-standard options suitable for a package on any platform.
134 # Command line can be used to extend this set, and any unusual
135 # platform-specific requirements are also appended later on.
136 configopts="--prefix=/usr --sysconfdir=/etc --localstatedir=/var"
139 for opt in $*
141 case "$opt" in
142 -clean|--clean)
143 # we always clean nowadays, so silently ignore
144 shift
146 -verbose|--verbose)
147 verbose=true
148 shift
150 --nonrpm)
151 nonrpm=true
152 shift
154 --nondeb)
155 nondeb=true
156 shift
158 --signed)
159 signed=true
160 shift
162 --source)
163 srconly=true
164 shift
166 --target)
167 shift
168 target=$1
169 shift
171 --with-vector)
172 configopts="$configopts $opt"
173 vector=true
174 shift
176 --with-webjs)
177 configopts="$configopts $opt"
178 webjs=true
179 shift
181 --with-docker)
182 docker=true
183 shift
185 --with-make)
186 MAKE=$1
187 shift
189 --with-tar)
190 TAR=$1
191 shift
193 --with-zip)
194 ZIP=$1
195 shift
198 # Add remaining options to the ./configure command line
199 configopts="$configopts $opt"
200 shift
202 esac
203 done
205 # now some specific QA/development hosts with special needs ...
207 case `hostname`
209 vm04|vm04.localdomain|vm11|vm11.localdomain)
210 # gcc -O2 is broken here (for the linux PMDA)
212 configopts="$configopts --with-optimization=no"
213 # over-ride the settings from dpkg-buildflags
215 export DEB_CFLAGS_SET=''
216 export DEB_CXXFLAGS_SET=''
218 esac
220 if [ "$nonrpm" = "unknown" ]
221 then
222 nonrpm=true
223 test -x /usr/bin/rpmbuild && nonrpm=false
225 if [ "$nondeb" = "unknown" ]
226 then
227 nondeb=true
228 test -x /usr/bin/dpkg-buildpackage && nondeb=false
231 case "$target"
233 mingw|mingw64|windows)
234 target=mingw
235 nonrpm=true
236 configopts="$configopts --disable-ssp"
237 export configure="/usr/bin/mingw64-configure"
238 export MAKE="/usr/bin/mingw64-make"
239 export QMAKE="/usr/bin/mingw64-qmake-qt4"
240 export PKG_CONFIG="/usr/bin/mingw64-pkg-config"
242 linux)
243 ARCH=`uname -m`
244 [ -f /etc/slackware-version -a "$ARCH" = x86_64 ] && configopts="$configopts --libdir=/usr/lib64"
246 sunos|SunOS)
247 ARCH=`isainfo -k`
248 [ "$ARCH" = amd64 -o "$ARCH" = sparcv9 ] && configopts="$configopts --libdir=/usr/lib/64"
249 export YACC=bison
250 export LEX=flex
252 esac
254 . ./VERSION.pcp
255 VERSION=${PACKAGE_MAJOR}.${PACKAGE_MINOR}.${PACKAGE_REVISION}
257 LOGDIR=$topdir/Logs
258 rm -rf "$LOGDIR"
259 mkdir -p "$LOGDIR"
261 source=pcp-${VERSION}
262 SRCDIR=$topdir/$source
263 SRCTAR=$topdir/build/tar/$source.src.tar.gz
264 SRCWEBJS=$topdir/build/tar/webjs.tar.gz
265 SRCVECTOR=$topdir/build/tar/vector.tar.gz
267 build_failure()
269 # only print in non-verbose mode, otherwise we see double
270 if ! $verbose ; then
271 echo $@ failed, see log in $LOGDIR/pcp
272 tail $LOGDIR/pcp
274 exit 1
277 clean_packaging()
279 echo
280 echo "== Cleaning build tree for packaging build"
281 rm -rf "$SRCDIR" "$SRCTAR" .gitcensus
284 fix_version()
286 if [ -f "$1" -a -f "$2" ]
287 then
288 # as expected, both present
289 if diff "$1" "$2" >/dev/null 2>&1
290 then
292 else
293 # must be using git-archive(1) and VERSION.pcp has been
294 # modified, e.g. by pcp-daily script ... use the modified
295 # one to bump the build number in the packages
297 cp "$1" "$2"
299 else
300 echo "Arrgh ... expect both these to exist ..."
301 ls -l "$1" "$2"
302 exit 1
306 # Download sources from external repositories that we might want
307 # to include with the PCP packages.
309 third_parties()
311 if $vector
312 then
313 if [ ! -f "$SRCVECTOR" ]
314 then
315 echo "== Downloading Vector webapp"
316 wget -q -O "$SRCVECTOR" \
317 https://bintray.com/artifact/download/netflixoss/downloads/vector.tar.gz
318 [ $? -eq 0 ] && echo "Wrote: $SRCVECTOR"
320 if [ -f "$SRCVECTOR" ]
321 then
322 DESTVECTOR="$topdir/$source/build/tar/"`basename $SRCVECTOR`
323 cp "$SRCVECTOR" "$DESTVECTOR"
327 if $webjs
328 then
329 if [ ! -f "$SRCWEBJS" ]
330 then
331 echo "== Downloading webjs webapps"
332 wget -q -O $tmp/js.zip \
333 https://github.com/performancecopilot/pcp-webjs/archive/master.zip
334 [ $? -eq 0 ] && ./scripts/zip2tar $tmp/js.zip $SRCWEBJS
336 if [ -f "$SRCWEBJS" ]
337 then
338 DESTWEBJS="$topdir/$source/build/tar/"`basename $SRCWEBJS`
339 cp "$SRCWEBJS" "$DESTWEBJS"
344 # Generate a list of all the files git knows about, create the
345 # source tarball and then unpack it. We'll then do the package
346 # build in a guaranteed pristine environment.
348 src_tar_build()
350 if [ -z "$TAR" -o ! -z "$USE_GIT_ARCHIVE" ]
351 then
352 # no suitable GNU tar is available, use git-archive(1) - no local changes
353 if git status -s src man | grep '^.M'
354 then
355 echo "Arrgh, modified src files and either no tar ($TAR) or no --transform"
356 echo "support in tar, so local changes would not be included in the build."
357 exit 1
359 git archive --format=tar --prefix=$source/ HEAD | $ZIP > $SRCTAR
360 else
361 # build git file list and make tarball by hand to pick up any changes
362 git ls-files > .gitcensus
363 $TAR -cz --transform="s,^,$source/," --files-from=.gitcensus \
364 --file=$SRCTAR
365 rm .gitcensus
367 echo "Wrote: $SRCTAR"
370 # On sanity checks :- this is sick, but I'm really tired of QA failing
371 # because of bad binaries being built from the Debian/Ubuntu toolchain
372 # for # i?86 platforms
373 # - Ken McDonell Apr 2010
375 sanity_check_settings()
377 OPT=false
378 ARCH=`uname -m | sed -e 's/i.86/ia32/'`
379 if [ "$ARCH" != "ia32" ]
380 then
381 OPT=true # no workarounds needed for non-i?86 platforms
382 elif [ -f /etc/lsb-release ]
383 then
384 if grep -q 'DISTRIB_ID=Ubuntu' /etc/lsb-release
385 then
386 eval `grep DISTRIB_RELEASE= /etc/lsb-release`
387 XDISTRIB_RELEASE=`echo $DISTRIB_RELEASE | sed -e 's/[^0-9]//g'`
388 [ -z "$XDISTRIB_RELEASE" ] && XDISTRIB_RELEASE=0000
389 if [ $XDISTRIB_RELEASE -gt 1110 ]
390 then
391 # Hope the problem is fixed after Ubuntu 11.10
392 OPT=true
395 $OPT || echo "Building without optimization for Ubuntu $DISTRIB_RELEASE"
396 elif [ -f /etc/debian_version ]
397 then
398 DISTRIB_RELEASE=`cat /etc/debian_version`
399 XDISTRIB_RELEASE=`echo $DISTRIB_RELEASE | sed -e 's/[^0-9]//g'`
400 [ -z "$XDISTRIB_RELEASE" ] && XDISTRIB_RELEASE=0000
401 if [ $XDISTRIB_RELEASE -ge 700 ]
402 then
403 # Hope the problem is fixed in Debian 7.0.0
404 OPT=true
406 $OPT || echo "Building without optimization for Debian $DISTRIB_RELEASE"
409 if ! $OPT
410 then
411 ok=true
412 if grep -q '^my $default_flags .*O2' /usr/bin/dpkg-buildpackage
413 then
414 echo 'dpkg-buildpackage: need to remove O2 from $default_flags'
415 ok=false
417 if grep -q '^[ ]*LDFLAGS.*-Bsymbolic-functions' /usr/bin/dpkg-buildpackage
418 then
419 echo 'dpkg-buildpackage: need to remove -Bsymbolic-function from LDFLAGS'
420 ok=false
422 if ! $ok
423 then
424 echo "Refer to Debian/Ubuntu notes in PCP's ./INSTALL"
425 exit 1
427 # as of Debian 6.0.1 and Ubuntu 10.10, build flags are hidden and
428 # extracted using dpkg-buildflags which fortunately honours some
429 # environment variable settings
431 export DEB_CFLAGS_SET=''
432 export DEB_CXXFLAGS_SET=''
433 export DEB_LDFLAGS_SET=''
437 # Build Debian packages using the dpkg-buildpackage utility which runs
438 # the complete build, from configure through to final deb preparation.
440 debian_buildpackage()
442 echo
443 echo "== Building deb packages using dpkg-buildpackage"
445 SUDO=${SUDO:-fakeroot}
446 # don't sign by default
447 sign='-us -uc'
448 # sign if --signed and $DEB_SIGN_KEYID set in the environment
449 $signed && [ -n "$DEB_SIGN_KEYID" ] && sign=''
450 rm -f $tmp/failed
451 if $verbose ; then
452 (dpkg-buildpackage $sign -r$SUDO || touch $tmp/failed) 2>&1 | tee -a $LOGDIR/pcp
453 else
454 (dpkg-buildpackage $sign -r$SUDO || touch $tmp/failed) >>$LOGDIR/pcp 2>&1
456 [ -f $tmp/failed ] && build_failure debian buildpackage
459 prepare_debian_sources()
461 # shortcut for dpkg-buildpackage which does everything itself from here
462 SRCDIR="${topdir}/build/deb"
463 rm -rf "${SRCDIR}"
464 mkdir -p "$SRCDIR" 2>/dev/null
465 cd "$SRCDIR" || exit 1
466 $TAR -zxf "$SRCTAR" || exit 1
467 cd "$SRCDIR/$source" || exit 1
468 fix_version ${topdir}/VERSION.pcp VERSION.pcp
471 prepare_debian_control()
473 echo
474 echo "== Preparing source tree for dpkg-buildpackage"
475 rm -f $tmp/failed debian/control
476 if $verbose ; then
477 ($MAKE -C debian control 2>&1 || touch $tmp/failed) 2>&1 | tee -a $LOGDIR/pcp
478 else
479 ($MAKE -C debian control 2>&1 || touch $tmp/failed) >>$LOGDIR/pcp 2>&1
481 [ -f $tmp/failed ] && build_failure debian control
484 debian_build()
486 prepare_debian_sources
487 # Note: we need to run configure here to get things like builddefs
488 # created, but configure will be run _again_ from
489 # debian_buildpackage, but we need to make sure the same
490 # configure options are used in both places
492 export configure_opts="$configopts"
493 if [ ! -f debian/rules ]
494 then
495 echo "Botch: cannot find debian/rules"
496 exit 1
498 debian_opts=`sed -n <debian/rules -e '/^configure_paths/s/^[^=]*= *//p'`
499 if [ -z "$debian_opts" ]
500 then
501 echo "Botch: cannot get config options from configure_paths in debian/rules"
502 exit 1
504 configopts="$debian_opts $configopts"
505 configure_pcp
506 prepare_debian_control
507 sanity_check_settings
508 debian_buildpackage
509 exit 0
512 prepare_build()
514 echo
515 echo "== Preparing fresh build tree for packaging build"
517 if [ ! -d "$SRCDIR" ]; then
518 # extract gzipped tar archive
519 $TAR -xz --file="$SRCTAR" || exit 1
520 fix_version VERSION.pcp $SRCDIR/VERSION.pcp
524 configure_pcp()
526 echo
527 echo "== Configuring pcp, log is in $LOGDIR/pcp ($configopts)"
529 rm -f $tmp/failed
530 if $verbose
531 then
532 ($configure $configopts 2>&1 || touch $tmp/failed) 2>&1 | tee -a $LOGDIR/pcp
533 else
534 ($configure $configopts 2>&1 || touch $tmp/failed) >>$LOGDIR/pcp 2>&1
536 [ -f $tmp/failed ] && build_failure Configure
539 default_build()
541 echo
542 echo "== Building pcp, log is in $LOGDIR/pcp"
544 rm -f $tmp/failed
545 if $verbose ; then
546 ($MAKE default_pcp 2>&1 || touch $tmp/failed) 2>&1 | tee -a $LOGDIR/pcp
547 else
548 ($MAKE default_pcp 2>&1 || touch $tmp/failed) >>$LOGDIR/pcp 2>&1
550 [ -f $tmp/failed ] && build_failure Make default_pcp
553 packaging_pcp()
555 echo
556 echo "== Packaging pcp, log is in $LOGDIR/pcp" | tee -a $LOGDIR/pcp
558 rm -f $tmp/failed
559 if $verbose ; then
560 ($MAKE -C build pack_pcp 2>&1 || touch $tmp/failed) 2>&1 | tee -a $LOGDIR/pcp
561 else
562 ($MAKE -C build pack_pcp 2>&1 || touch $tmp/failed) >>$LOGDIR/pcp 2>&1
564 [ -f $tmp/failed ] && build_failure Packaging via pack_pcp
567 packaging_rpm()
569 echo
570 echo "== Building rpm packages, log is in $LOGDIR/pcp" | tee -a $LOGDIR/pcp
572 rm -f $tmp/failed
573 export DIST_ROOT=${topdir}/build/rpm/${source}
574 if $verbose ; then
575 ($MAKE -C build/rpm pack_pcp 2>&1 || touch $tmp/failed) 2>&1 | tee -a $LOGDIR/pcp
576 else
577 ($MAKE -C build/rpm pack_pcp 2>&1 || touch $tmp/failed) >>$LOGDIR/pcp 2>&1
579 [ -f $tmp/failed ] && build_failure Packaging RPMs via pack_pcp
582 packaging_docker()
584 echo
585 echo "== Building docker container images, log is in $LOGDIR/pcp" | tee -a $LOGDIR/pcp
586 rm -f $tmp/failed
588 if docker version >/dev/null 2>&1; then
589 v=`docker version | sed -e 's/\./ /g' | \
590 awk '/^Server/ {s=1; if($2 == "version:") {print 10*$3+$4; exit}}
591 /Version:/ {if (s) {print 10*$2+$3; exit}}'`
592 if [ -z "$v" -o "$v" -lt 14 ]; then
593 echo 'Error: docker version too old. Containers build skipped.'
594 touch $tmp/failed
596 else
597 echo 'Error: docker not enabled or not properly configured for non-root access.'
598 echo 'Check the docker service is enabled and running. You can allow non-root access'
599 echo 'as follows: edit /etc/sysconfig/docker and add OPTIONS="-G gid" where gid is a'
600 echo 'group name that you are a member of. Then restart the docker service.'
601 touch $tmp/failed
604 [ -f $tmp/failed ] && build_failure Packaging docker images, docker configuration
606 if $verbose ; then
607 ($MAKE -C build/containers pack_pcp 2>&1 || touch $tmp/failed) 2>&1 | tee -a $LOGDIR/pcp
608 else
609 ($MAKE -C build/containers pack_pcp 2>&1 || touch $tmp/failed) >>$LOGDIR/pcp 2>&1
612 [ -f $tmp/failed ] && build_failure Packaging docker containers via pack_pcp
615 # real build starts here
616 clean_packaging
617 src_tar_build
618 $srconly && exit 0
620 # short-circuit for performing builds with dpkg-buildpackage
621 $nondeb || debian_build
623 prepare_build
625 # download any requested third party sources we're packaging
626 third_parties
628 # shift into the clean packaging build tree from here onward
629 cd "$SRCDIR"
631 configure_pcp
633 # short-circuit for performing rpm-only builds with rpmbuild
634 # (skips over the default_build stage as rpmbuild does that).
635 if $nonrpm
636 then
637 default_build # perform a complete build in-situ
638 packaging_pcp # create all known package formats
639 else
640 packaging_rpm
644 # --with-docker, currently only for RPM platforms with docker
645 if $docker
646 then
647 packaging_docker
650 # success!
651 if ! $verbose ; then
652 grep '^Wrote:' $LOGDIR/pcp | sed -e 's/\.\.\/\.\.\///'
654 exit 0