Merge branch 'master' into martins-merge
[pcp.git] / Makepkgs
blob7ca744147410689fb12d26e767d7d2a102023260
1 #!/bin/sh
3 # Make whichever packages the system supports
5 # Copyright (c) 2012-2016 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 dorpm=unknown
24 dodeb=unknown
25 parfait=false
26 vector=false
27 webjs=false
28 docker=false
30 if [ ! -d .git ]
31 then
32 echo "Error: can only be run from within the PCP git repository"
33 exit 1
36 if [ ! -f VERSION.pcp ]
37 then
38 echo "Error: can only be run from within the PCP source tree"
39 exit 1
42 # Find where a GNU tool is hiding, look for this line:
43 # "Report bugs to <bug-$gnutool@gnu.org>"
44 # or this line:
45 # "FreeBSD $gnutool YYYYMMDD"
46 # in the $gnutool --help output
48 gnu_tool()
50 prefix="$1"
51 shift
52 envvar="$1"
53 shift
54 oldcmd="$1"
55 shift
57 # Not in the environment, go search for it in the known locations.
59 if [ -z "$envvar" ]
60 then
61 for command in $oldcmd $@
63 exec=`which $command 2>/dev/null`
64 if [ -n "$exec" ]
65 then
66 if $command --help 2>&1 | egrep "(bug-$oldcmd@gnu.org)|(FreeBSD $gnutool)|(NetBSD $gnutool)|(Apple $gnutool)" >/dev/null
67 then
68 echo $exec
69 return 0
71 # special cases
73 if [ "$target" = openbsd -a $command = gzip ]
74 then
75 echo $exec
76 return 0
79 done
80 echo >&2 "$prefix: can't find GNU $oldcmd in the commands $oldcmd or $@"
81 [ $prefix = Error ] && exit 1
82 return 1
85 # Use the environment request and ensure it is a full path (do not
86 # change this behaviour, it is required for backward-compatibility
87 # - QA failures will result if you choose to ignore this warning).
89 command=`which $envvar 2>/dev/null`
90 if [ -z "$command" ]
91 then
92 echo >&2 "$prefix: $oldcmd (from the environment) does not exist!"
93 [ $prefix = Error ] && exit 1
94 return 1
96 echo $command
97 return 0
100 target=`uname -s | tr 'A-Z' 'a-z'`
102 export MAKE=`gnu_tool Error "$MAKE" make gmake`
103 export TAR=`gnu_tool Warning "$TAR" tar gtar gnutar`
104 export ZIP=`gnu_tool Error "$ZIP" gzip gnuzip`
106 # check if tar supports --transform else we must use git-archive later on (e.g. RHEL5)
107 [ ! -z "$TAR" ] && $TAR --help | 2>&1 grep -q transform || USE_GIT_ARCHIVE=1
109 tmp=`mktemp -d /tmp/pcp.XXXXXXXXX` || exit 1
110 trap "rm -rf $tmp" 0 1 2 3 15
112 configure=./configure
113 cmdline_configopts=''
115 for opt in $*
117 case "$opt" in
118 -clean|--clean)
119 # we always clean nowadays, so silently ignore
120 shift
122 -verbose|--verbose)
123 verbose=true
124 shift
126 --nonrpm)
127 dorpm=false
128 shift
130 --nondeb)
131 dodeb=false
132 shift
134 --signed)
135 signed=true
136 shift
138 --source)
139 srconly=true
140 shift
142 --target)
143 shift
144 target=$1
145 shift
147 --with-parfait)
148 cmdline_configopts="$cmdline_configopts $opt"
149 parfait=true
150 shift
152 --with-vector)
153 cmdline_configopts="$cmdline_configopts $opt"
154 vector=true
155 shift
157 --with-webjs)
158 cmdline_configopts="$cmdline_configopts $opt"
159 webjs=true
160 shift
162 --with-docker)
163 docker=true
164 shift
166 --with-make)
167 MAKE=$1
168 shift
170 --with-tar)
171 TAR=$1
172 shift
174 --with-zip)
175 ZIP=$1
176 shift
179 # Add remaining options to the ./configure command line
180 cmdline_configopts="$cmdline_configopts $opt"
181 shift
183 esac
184 done
186 if [ $dorpm = unknown ]
187 then
188 dorpm=false
189 [ -x /usr/bin/rpmbuild ] && dorpm=true
191 if [ $dodeb = unknown ]
192 then
193 dodeb=false
194 [ -x /usr/bin/dpkg-buildpackage ] && dodeb=true
197 # Hmm ...
199 if $dodeb && $dorpm
200 then
201 echo >&2 "Do not know how to build RPM and Debian packages at the same time!"
202 echo >&2 "Suggest --nonrpm or --nondeb on the command line."
203 exit 1
206 if $dodeb
207 then
208 # On Debian-based platforms, the debian/rules file already captures
209 # most of the required configure settings in $(configure_paths)
211 configopts=''
212 elif $dorpm
213 then
214 # On RPM-based platforms, rpm macros provide an excellent set of defaults.
215 # This set is based on Fedora 22 %configure macro from /usr/lib/rpm/macros,
216 # and is known to work as far back as RHEL5.
217 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`"
218 # rpmbuild clears the environment, so force these settings into the
219 # rpm build configure
221 configopts="$configopts --with-make=$MAKE --with-tar=$TAR --with-zip=$ZIP"
222 elif [ $target = darwin ]
223 then
224 # On Mac OS X platforms, we install away from /usr/bin, /usr/lib
225 # and so on because of security requirements in recent versions.
227 configopts="--prefix=/usr/local --sysconfdir=/etc --localstatedir=/var"
228 elif [ $target = netbsd ]
229 then
230 # Try to mimic where pkgsrc/pkgin place things
232 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"
233 else
234 # Bog-standard options suitable for a package on any platform.
235 # Command line can be used to extend this set, and any unusual
236 # platform-specific requirements are also appended later on.
237 configopts="--prefix=/usr --sysconfdir=/etc --localstatedir=/var"
240 # Platform-specific variants ...
242 case "$target"
244 mingw|mingw64|windows)
245 target=mingw
246 dorpm=false
247 configopts="$configopts --disable-ssp"
248 export configure="/usr/bin/mingw64-configure"
249 export MAKE="/usr/bin/mingw64-make"
250 export QMAKE="/usr/bin/mingw64-qmake-qt4"
251 export PKG_CONFIG="/usr/bin/mingw64-pkg-config"
253 linux)
254 ARCH=`uname -m`
255 [ -f /etc/slackware-version -a "$ARCH" = x86_64 ] && configopts="$configopts --libdir=/usr/lib64"
257 sunos|SunOS)
258 ARCH=`isainfo -k`
259 [ "$ARCH" = amd64 -o "$ARCH" = sparcv9 ] && configopts="$configopts --libdir=/usr/lib/64"
260 export YACC=bison
261 export LEX=flex
263 esac
265 # now some specific QA/development hosts with special needs ...
267 case `hostname`
269 vm04|vm04.localdomain|vm11|vm11.localdomain)
270 # gcc -O2 is broken here (for the linux PMDA)
272 configopts="$configopts --with-optimization=no"
273 # over-ride the settings from dpkg-buildflags
275 export DEB_CFLAGS_SET=''
276 export DEB_CXXFLAGS_SET=''
278 esac
280 # and finally add in any from the command line
282 [ -n "$cmdline_configopts" ] && configopts="$configopts $cmdline_configopts"
284 . ./VERSION.pcp
285 VERSION=${PACKAGE_MAJOR}.${PACKAGE_MINOR}.${PACKAGE_REVISION}
287 LOGDIR=$topdir/Logs
288 rm -rf "$LOGDIR"
289 mkdir -p "$LOGDIR"
291 source=pcp-${VERSION}
292 SRCDIR="$topdir/$source"
293 SRCTREE="$topdir/$source"
294 SRCTAR=$topdir/build/tar/$source.src.tar.gz
295 SRCWEBJS=$topdir/build/tar/webjs.tar.gz
296 SRCVECTOR=$topdir/build/tar/vector.tar.gz
297 SRCPARFAIT=$topdir/build/tar/parfait.tar.gz
299 build_failure()
301 # only print in non-verbose mode, otherwise we see double
302 if ! $verbose ; then
303 echo $@ failed, see log in $LOGDIR/pcp
304 tail $LOGDIR/pcp
306 exit 1
309 clean_packaging()
311 echo
312 echo "== Cleaning build tree for packaging build"
313 rm -rf "$SRCDIR" "$SRCTAR" .gitcensus
316 fix_version()
318 if [ -f "$1" -a -f "$2" ]
319 then
320 # as expected, both present
321 if diff "$1" "$2" >/dev/null 2>&1
322 then
324 else
325 # must be using git-archive(1) and VERSION.pcp has been
326 # modified, e.g. by pcp-daily script ... use the modified
327 # one to bump the build number in the packages
329 cp "$1" "$2"
331 else
332 echo "Arrgh ... expect both these to exist ..."
333 ls -l "$1" "$2"
334 exit 1
338 # Download sources from external repositories that we might want
339 # to include with the PCP packages.
341 third_parties()
343 if $parfait
344 then
345 if [ ! -f "$SRCPARFAIT" ]
346 then
347 echo "== Downloading Parfait package"
348 url=https://github.com/performancecopilot/parfait/archive/master.zip
349 wget -H -q -O $tmp/java.zip "$url"
350 [ $? -eq 0 ] && ./scripts/zip2tar $tmp/java.zip "$SRCPARFAIT"
352 if [ -f "$SRCPARFAIT" ]
353 then
354 DESTPARFAIT="$SRCTREE/build/tar/"`basename "$SRCPARFAIT"`
355 cp "$SRCPARFAIT" "$DESTPARFAIT"
359 if $vector
360 then
361 if [ ! -f "$SRCVECTOR" ]
362 then
363 echo "== Downloading Vector webapp"
364 url=https://bintray.com/artifact/download/netflixoss/downloads/vector.tar.gz
365 wget -H -q -O "$SRCVECTOR" "$url"
366 [ $? -eq 0 ] && echo "Wrote: $SRCVECTOR"
368 if [ -f "$SRCVECTOR" ]
369 then
370 DESTVECTOR="$SRCTREE/build/tar/"`basename "$SRCVECTOR"`
371 mkdir $tmp/vector && pushd $tmp/vector && $TAR -zxf "$SRCVECTOR"
372 $topdir/scripts/build-vector
373 cd dist && $TAR -czf "$DESTVECTOR" *
374 popd
378 if $webjs
379 then
380 if [ ! -f "$SRCWEBJS" ]
381 then
382 echo "== Downloading webjs webapps"
383 url=https://github.com/performancecopilot/pcp-webjs/archive/master.zip
384 wget -H -q -O $tmp/js.zip "$url"
385 [ $? -eq 0 ] && ./scripts/zip2tar $tmp/js.zip "$SRCWEBJS"
387 if [ -f "$SRCWEBJS" ]
388 then
389 DESTWEBJS="$SRCTREE/build/tar/"`basename "$SRCWEBJS"`
390 cp "$SRCWEBJS" "$DESTWEBJS"
395 # Generate a list of all the files git knows about, create the
396 # source tarball and then unpack it. We'll then do the package
397 # build in a guaranteed pristine environment.
399 src_tar_build()
401 if [ -z "$TAR" -o ! -z "$USE_GIT_ARCHIVE" ]
402 then
403 # no suitable GNU tar is available, use git-archive(1) - no local changes
404 if git status -s src man | grep '^.M'
405 then
406 echo "Arrgh, modified src files and either no tar ($TAR) or no --transform"
407 echo "support in tar, so local changes would not be included in the build."
408 exit 1
410 git archive --format=tar --prefix=$source/ HEAD | $ZIP > $SRCTAR
411 else
412 # build git file list and make tarball by hand to pick up any changes
414 git ls-files > .gitcensus
416 # but first ensure a "build everything" debian/control has been
417 # recreated
419 cd debian
420 if ./pre-build
421 then
423 else
424 echo "Error: debian/pre-build failed when creating debian/control"
425 exit 1
427 cd ..
428 echo "debian/control" >> .gitcensus
430 $TAR -cz --transform="s,^,$source/," --files-from=.gitcensus \
431 --file=$SRCTAR
432 rm .gitcensus
434 echo "Wrote: $SRCTAR"
437 # On sanity checks :- this is sick, but I'm really tired of QA failing
438 # because of bad binaries being built from the Debian/Ubuntu toolchain
439 # for # i?86 platforms
440 # - Ken McDonell Apr 2010
442 sanity_check_settings()
444 OPT=false
445 ARCH=`uname -m | sed -e 's/i.86/ia32/'`
446 if [ "$ARCH" != "ia32" ]
447 then
448 OPT=true # no workarounds needed for non-i?86 platforms
449 elif [ -f /etc/lsb-release ]
450 then
451 if grep -q 'DISTRIB_ID=Ubuntu' /etc/lsb-release
452 then
453 eval `grep DISTRIB_RELEASE= /etc/lsb-release`
454 XDISTRIB_RELEASE=`echo $DISTRIB_RELEASE | sed -e 's/[^0-9]//g'`
455 [ -z "$XDISTRIB_RELEASE" ] && XDISTRIB_RELEASE=0000
456 if [ $XDISTRIB_RELEASE -gt 1110 ]
457 then
458 # Hope the problem is fixed after Ubuntu 11.10
459 OPT=true
462 $OPT || echo "Building without optimization for Ubuntu $DISTRIB_RELEASE"
463 elif [ -f /etc/debian_version ]
464 then
465 DISTRIB_RELEASE=`cat /etc/debian_version`
466 XDISTRIB_RELEASE=`echo $DISTRIB_RELEASE | sed -e 's/[^0-9]//g'`
467 [ -z "$XDISTRIB_RELEASE" ] && XDISTRIB_RELEASE=0000
468 if [ $XDISTRIB_RELEASE -ge 700 ]
469 then
470 # Hope the problem is fixed in Debian 7.0.0
471 OPT=true
473 $OPT || echo "Building without optimization for Debian $DISTRIB_RELEASE"
476 if ! $OPT
477 then
478 ok=true
479 if grep -q '^my $default_flags .*O2' /usr/bin/dpkg-buildpackage
480 then
481 echo 'dpkg-buildpackage: need to remove O2 from $default_flags'
482 ok=false
484 if grep -q '^[ ]*LDFLAGS.*-Bsymbolic-functions' /usr/bin/dpkg-buildpackage
485 then
486 echo 'dpkg-buildpackage: need to remove -Bsymbolic-function from LDFLAGS'
487 ok=false
489 if ! $ok
490 then
491 echo "Refer to Debian/Ubuntu notes in PCP's ./INSTALL"
492 exit 1
494 # as of Debian 6.0.1 and Ubuntu 10.10, build flags are hidden and
495 # extracted using dpkg-buildflags which fortunately honours some
496 # environment variable settings
498 export DEB_CFLAGS_SET=''
499 export DEB_CXXFLAGS_SET=''
500 export DEB_LDFLAGS_SET=''
504 # Build Debian packages using the dpkg-buildpackage utility which runs
505 # the complete build, from configure through to final deb preparation.
507 debian_buildpackage()
509 echo
510 echo "== Building deb packages using dpkg-buildpackage"
512 SUDO=${SUDO:-fakeroot}
513 # don't sign by default
514 sign='-us -uc'
515 # sign if --signed and $DEB_SIGN_KEYID set in the environment
516 $signed && [ -n "$DEB_SIGN_KEYID" ] && sign=''
517 rm -f $tmp/failed
518 if $verbose ; then
519 (dpkg-buildpackage $sign -r$SUDO || touch $tmp/failed) 2>&1 | tee -a $LOGDIR/pcp
520 else
521 (dpkg-buildpackage $sign -r$SUDO || touch $tmp/failed) >>$LOGDIR/pcp 2>&1
523 [ -f $tmp/failed ] && build_failure debian buildpackage
526 prepare_debian_sources()
528 # shortcut for dpkg-buildpackage which does everything itself from here
529 rm -rf "$SRCDIR"
530 mkdir -p "$SRCDIR" 2>/dev/null
532 cd "$SRCDIR" || exit 1
533 $TAR -zxf "$SRCTAR" || exit 1
535 cd "$SRCTREE" || exit 1
536 third_parties
538 fix_version ${topdir}/VERSION.pcp VERSION.pcp
541 prepare_debian_control()
543 echo
544 echo "== Preparing source tree for dpkg-buildpackage"
545 rm -f $tmp/failed debian/control
546 if $verbose ; then
547 ($MAKE -C debian control 2>&1 || touch $tmp/failed) 2>&1 | tee -a $LOGDIR/pcp
548 else
549 ($MAKE -C debian control 2>&1 || touch $tmp/failed) >>$LOGDIR/pcp 2>&1
551 [ -f $tmp/failed ] && build_failure debian control
554 debian_build()
556 SRCDIR="${topdir}/build/deb"
557 SRCTREE="${topdir}/build/deb/$source"
558 prepare_debian_sources
560 # Note: we need to run configure here to get things like builddefs
561 # created, but configure will be run _again_ from
562 # debian_buildpackage, but we need to make sure the same
563 # configure options are used in both places
565 export configure_opts="$configopts"
566 if [ ! -f debian/rules ]
567 then
568 echo "Botch: cannot find debian/rules"
569 exit 1
571 debian_opts=`sed -n <debian/rules -e '/^configure_paths/s/^[^=]*= *//p'`
572 if [ -z "$debian_opts" ]
573 then
574 echo "Botch: cannot get config options from configure_paths in debian/rules"
575 exit 1
577 configopts="$debian_opts $configopts"
578 configure_pcp
579 prepare_debian_control
580 sanity_check_settings
581 debian_buildpackage
583 # success!
584 if ! $verbose ; then
585 grep ^dpkg-deb $LOGDIR/pcp | \
586 sed -e 's,\.$,,g' -e "s,',,g" -e "s,\.\.,$topdir/build/deb,g" | \
587 awk '{ print "Wrote:", $NF }'
589 exit 0
592 prepare_build()
594 echo
595 echo "== Preparing fresh build tree for packaging build"
597 if [ ! -d "$SRCDIR" ]; then
598 # extract gzipped tar archive
599 $TAR -xz --file="$SRCTAR" || exit 1
600 fix_version VERSION.pcp $SRCDIR/VERSION.pcp
604 configure_pcp()
606 echo
607 echo "== Configuring pcp, log is in $LOGDIR/pcp ($configopts)"
609 rm -f $tmp/failed
610 if $verbose
611 then
612 ($configure $configopts 2>&1 || touch $tmp/failed) 2>&1 | tee -a $LOGDIR/pcp
613 else
614 ($configure $configopts 2>&1 || touch $tmp/failed) >>$LOGDIR/pcp 2>&1
616 [ -f $tmp/failed ] && build_failure Configure
619 default_build()
621 echo
622 echo "== Building pcp, log is in $LOGDIR/pcp"
624 rm -f $tmp/failed
625 if $verbose ; then
626 ($MAKE default_pcp 2>&1 || touch $tmp/failed) 2>&1 | tee -a $LOGDIR/pcp
627 else
628 ($MAKE default_pcp 2>&1 || touch $tmp/failed) >>$LOGDIR/pcp 2>&1
630 [ -f $tmp/failed ] && build_failure Make default_pcp
633 packaging_pcp()
635 echo
636 echo "== Packaging pcp, log is in $LOGDIR/pcp" | tee -a $LOGDIR/pcp
638 rm -f $tmp/failed
639 if $verbose ; then
640 ($MAKE -C build pack_pcp 2>&1 || touch $tmp/failed) 2>&1 | tee -a $LOGDIR/pcp
641 else
642 ($MAKE -C build pack_pcp 2>&1 || touch $tmp/failed) >>$LOGDIR/pcp 2>&1
644 [ -f $tmp/failed ] && build_failure Packaging via pack_pcp
647 packaging_rpm()
649 echo
650 echo "== Building rpm packages, log is in $LOGDIR/pcp" | tee -a $LOGDIR/pcp
652 rm -f $tmp/failed
653 export DIST_ROOT=${topdir}/build/rpm/${source}
654 # sign if --signed and $RPM_SIGN_KEYID set in the environment
655 $signed && [ -z "$RPM_SIGN_KEYID" ] && unset RPM_SIGN_KEYID
656 if $verbose ; then
657 ($MAKE -C build/rpm pack_pcp 2>&1 || touch $tmp/failed) 2>&1 | tee -a $LOGDIR/pcp
658 else
659 ($MAKE -C build/rpm pack_pcp 2>&1 || touch $tmp/failed) >>$LOGDIR/pcp 2>&1
661 [ -f $tmp/failed ] && build_failure Packaging RPMs via pack_pcp
664 packaging_docker()
666 echo
667 echo "== Building docker container images, log is in $LOGDIR/pcp" | tee -a $LOGDIR/pcp
668 rm -f $tmp/failed
670 if docker version >/dev/null 2>&1; then
671 v=`docker version | sed -e 's/\./ /g' | \
672 awk '/^Server/ {s=1; if($2 == "version:") {print 10*$3+$4; exit}}
673 /Version:/ {if (s) {print 10*$2+$3; exit}}'`
674 if [ -z "$v" -o "$v" -lt 14 ]; then
675 echo 'Error: docker version too old. Containers build skipped.'
676 touch $tmp/failed
678 else
679 echo 'Error: docker not enabled or not properly configured for non-root access.'
680 echo 'Check the docker service is enabled and running. You can allow non-root access'
681 echo 'as follows: edit /etc/sysconfig/docker and add OPTIONS="-G gid" where gid is a'
682 echo 'group name that you are a member of. Then restart the docker service.'
683 touch $tmp/failed
686 [ -f $tmp/failed ] && build_failure Packaging docker images, docker configuration
688 if $verbose ; then
689 ($MAKE -C build/containers pack_pcp 2>&1 || touch $tmp/failed) 2>&1 | tee -a $LOGDIR/pcp
690 else
691 ($MAKE -C build/containers pack_pcp 2>&1 || touch $tmp/failed) >>$LOGDIR/pcp 2>&1
694 [ -f $tmp/failed ] && build_failure Packaging docker containers via pack_pcp
697 # real build starts here
698 clean_packaging
699 src_tar_build
700 $srconly && exit 0
702 # short-circuit for performing builds with dpkg-buildpackage
703 $dodeb && debian_build
705 prepare_build
707 # download any requested third party sources we're packaging
708 third_parties
710 # shift into the clean packaging build tree from here onward
711 cd "$SRCDIR"
713 configure_pcp
715 # short-circuit for performing rpm-only builds with rpmbuild
716 # (skips over the default_build stage as rpmbuild does that).
717 if $dorpm
718 then
719 packaging_rpm
720 else
721 default_build # perform a complete build in-situ
722 packaging_pcp # create all known package formats
726 # --with-docker, currently only for RPM platforms with docker
727 if $docker
728 then
729 packaging_docker
732 # success!
733 if ! $verbose ; then
734 grep '^Wrote:' $LOGDIR/pcp | sed -e 's/\.\.\/\.\.\///'
736 exit 0