+export HOME := $(PWD)
+endif
+.rpmmacros:
+ifeq "$(shell pwd)" "/build"
+ rm -f $(FAKEROOT) ; ln -s $(REALROOT) $(FAKEROOT)
+endif
+ rm -f $@
+ echo "%_topdir $(HOME)" >> $@
+ echo "%_tmppath $(HOME)/tmp" >> $@
+ ./getrpmmacros.sh >> $@
+
+### this utility allows to extract various info from a spec file
+### and to define them in makefiles
+spec2make: spec2make.c
+ $(CC) -g -Wall $< -o $@ -lrpm -lrpmbuild
+
+### run spec2make on the spec file and include the result
+# usage: spec2make package
+define target_mk
+MAKE/$(1).mk: $($(1).specpath) spec2make .rpmmacros
+ mkdir -p MAKE
+ ./spec2make $($(1)-RPMFLAGS) $($(1).specpath) $(1) > MAKE/$(1).mk
+ @if [ -z MAKE/$(1).mk ] ; then rm MAKE/$(1).mk ; exit 1 ; fi
+endef
+
+$(foreach package,$(ALL),$(eval $(call target_mk,$(package))))
+
+# stores PLDISTRO in a file
+# this is done at stage1. later run wont get confused
+pldistro.mk:
+ echo "PLDISTRO:=$(PLDISTRO)" > $@
+ echo "PLDISTROTAGS:=$(PLDISTROTAGS)" >> $@
+
+savepldistro: pldistro.mk
+.PHONY: savepldistro
+
+# always refresh this
+all: savepldistro
+
+#################### regular make
+
+define stage2_variables
+### devel dependencies
+$(1).rpmbuild = $(if $($(1)-RPMBUILD),$($(1)-RPMBUILD),$(RPMBUILD)) $($(1)-RPMFLAGS)
+$(1).all-devel-rpm-paths := $(foreach rpm,$($(1)-DEPEND-DEVEL-RPMS),$($(rpm).rpm-path))
+$(1).depend-devel-packages := $(sort $(foreach rpm,$($(1)-DEPEND-DEVEL-RPMS),$($(rpm).package)))
+ALL-DEVEL-RPMS += $($(1)-DEPEND-DEVEL-RPMS)
+endef
+
+$(foreach package,$(ALL),$(eval $(call stage2_variables,$(package))))
+ALL-DEVEL-RPMS := $(sort $(ALL-DEVEL-RPMS))
+
+
+### pack sources into tarballs
+ALLTARBALLS:= $(foreach package, $(ALL), $($(package).tarballs))
+tarballs: $(ALLTARBALLS)
+ @echo $(words $(ALLTARBALLS)) source tarballs OK
+.PHONY: tarballs
+
+SOURCES/%.tar.bz2: SOURCES/%
+ tar chpjf $@ -C SOURCES $*
+
+SOURCES/%.tar.gz: SOURCES/%
+ tar chpzf $@ -C SOURCES $*
+
+SOURCES/%.tgz: SOURCES/%
+ tar chpzf $@ -C SOURCES $*
+
+##
+URLS/%: url=$(subst @colon@,:,$(subst @slash@,/,$(notdir $@)))
+URLS/%: basename=$(notdir $(url))
+URLS/%:
+ echo curl $(url) -o SOURCES/$(basename)
+ touch $@
+
+### the directory SOURCES/<package>-<version> is made
+# with a copy -rl from CODEBASES/<package>
+# the former is $(package.source) and the latter is $(package.codebase)
+ALLSOURCES:=$(foreach package, $(ALL), $($(package).source))
+# so that make does not use the rule below directly for creating the tarball files
+.SECONDARY: $(ALLSOURCES)
+
+sources: $(ALLSOURCES)
+ @echo $(words $(ALLSOURCES)) versioned source trees OK
+.PHONY: sources
+
+define target_link_codebase_sources
+$($(1).source): $($(1).codebase) ; mkdir -p SOURCES ; cp -rl $($(1).codebase) $($(1).source)
+endef
+
+$(foreach package,$(ALL),$(eval $(call target_link_codebase_sources,$(package))))
+
+### codebase extraction
+ALLCODEBASES:=$(foreach package, $(ALL), $($(package).codebase))
+# so that make does not use the rule below directly for creating the tarball files
+.SECONDARY: $(ALLCODEBASES)
+
+codebases : $(ALLCODEBASES)
+ @echo $(words $(ALLCODEBASES)) codebase OK
+.PHONY: codebases
+
+### extract codebase
+# usage: extract_single_module package
+define extract_single_module
+ mkdir -p CODEBASES
+ $(if $($($(1).module)-SVNPATH), cd CODEBASES && svn export $($($(1).module)-SVNPATH) $(1), cd CODEBASES && cvs -d $($($(1).module)-CVSROOT) export -r $($($(1).module)-TAG) -d $(1) $($(1).module))
+endef
+
+# usage: extract_multi_module package
+define extract_multi_module
+ mkdir -p CODEBASES/$(1) && cd CODEBASES/$(1) && (\
+ $(foreach m,$($(1)-MODULES), $(if $($(m)-SVNPATH), svn export $($(m)-SVNPATH) $(m);, cvs -d $($(m)-CVSROOT) export -r $($(m)-TAG) $(m);)))
+endef
+
+CODEBASES/%: package=$(notdir $@)
+CODEBASES/%: multi_module=$(word 2,$($(package)-MODULES))
+CODEBASES/%:
+ @(echo -n "XXXXXXXXXXXXXXX -- BEG CODEBASE $(package) : $@ " ; date)
+ $(if $(multi_module),\
+ $(call extract_multi_module,$(package)),\
+ $(call extract_single_module,$(package)))
+ @(echo -n "XXXXXXXXXXXXXXX -- END CODEBASE $(package) : $@ " ; date)
+
+### source rpms
+ALLSRPMS:=$(foreach package,$(ALL),$($(package).srpm))
+srpms: $(ALLSRPMS)
+ @echo $(words $(ALLSRPMS)) source rpms OK
+.PHONY: srpms
+
+# usage: target_source_rpm package
+# select upon the package name, whether it contains srpm or not
+define target_source_rpm
+ifeq "$($(1)-BUILD-FROM-SRPM)" ""
+$($(1).srpm): $($(1).specpath) .rpmmacros $($(1).tarballs)
+ mkdir -p BUILD SRPMS tmp
+ @(echo -n "XXXXXXXXXXXXXXX -- BEG SRPM $(1) (using SOURCES) " ; date)
+ $(if $($(1).all-devel-rpm-paths), $(RPM-INSTALL-DEVEL) $($(1).all-devel-rpm-paths))
+ $($(1).rpmbuild) -bs $($(1).specpath)
+ $(if $($(1)-DEPEND-DEVEL-RPMS), $(RPM-UNINSTALL-DEVEL) $($(1)-DEPEND-DEVEL-RPMS))
+ @(echo -n "XXXXXXXXXXXXXXX -- END SRPM $(1) " ; date)
+else
+$($(1).srpm): $($(1).specpath) .rpmmacros $($(1).codebase)
+ mkdir -p BUILD SRPMS tmp
+ @(echo -n "XXXXXXXXXXXXXXX -- BEG SRPM $(1) (using make srpm) " ; date)
+ $(if $($(1).all-devel-rpm-paths), $(RPM-INSTALL-DEVEL) $($(1).all-devel-rpm-paths))
+ make -C $($(1).codebase) srpm SPECFILE=$(HOME)/$($(1).specpath) && \
+ rm -f SRPMS/$(notdir $($(1).srpm)) && \
+ ln $($(1).codebase)/$(notdir $($(1).srpm)) SRPMS/$(notdir $($(1).srpm))
+ $(if $($(1)-DEPEND-DEVEL-RPMS), $(RPM-UNINSTALL-DEVEL) $($(1)-DEPEND-DEVEL-RPMS))
+ @(echo -n "XXXXXXXXXXXXXXX -- END SRPM $(1) " ; date)
+endif
+endef
+
+$(foreach package,$(ALL),$(eval $(call target_source_rpm,$(package))))
+
+### binary rpms are made from source rpm
+ALLRPMS:=$(foreach package,$(ALL),$($(package).rpms))
+# same as above, mention $(ALL) and not $(ALLRPMS)
+rpms: $(ALLRPMS)
+ @echo $(words $(ALLRPMS)) binary rpms OK
+.PHONY: rpms
+
+# use tmp dirs when building binary rpm so make remains idempotent
+# otherwise SOURCES/ or SPEC gets touched again - which leads to rebuilding
+RPM-USE-TMP-DIRS = --define "_sourcedir $(HOME)/tmp" --define "_specdir $(HOME)/tmp"
+RPM-USE-COMPILE-DIRS = --define "_sourcedir $(HOME)/COMPILE" --define "_specdir $(HOME)/COMPILE"
+
+# usage: build_binary_rpm package
+# xxx hacky - invoke createrepo if DEPEND-FILES mentions RPMS/yumgroups.xml
+define target_binary_rpm
+$($(1).rpms): $($(1).srpm)
+ mkdir -p RPMS tmp
+ @(echo -n "XXXXXXXXXXXXXXX -- BEG RPM $(1) " ; date)
+ $(if $(findstring RPMS/yumgroups.xml,$($(1)-DEPEND-FILES)), $(createrepo) , )
+ $(if $($(1).all-devel-rpm-paths), $(RPM-INSTALL-DEVEL) $($(1).all-devel-rpm-paths))
+ $($(1).rpmbuild) --rebuild $(RPM-USE-TMP-DIRS) $($(1).srpm)
+ $(if $($(1)-DEPEND-DEVEL-RPMS), $(RPM-UNINSTALL-DEVEL) $($(1)-DEPEND-DEVEL-RPMS))
+ @(echo -n "XXXXXXXXXXXXXXX -- END RPM $(1) " ; date)
+# for manual use only - in case we need to investigate the results of an rpmbuild
+$(1)-compile: $($(1).srpm)
+ mkdir -p COMPILE tmp
+ @(echo -n "XXXXXXXXXXXXXXX -- BEG compile $(1) " ; date)
+ $(if $(findstring RPMS/yumgroups.xml,$($(1)-DEPEND-FILES)), $(createrepo) , )
+ $(if $($(1).all-devel-rpm-paths), $(RPM-INSTALL-DEVEL) $($(1).all-devel-rpm-paths))
+ $($(1).rpmbuild) --recompile $(RPM-USE-TMP-DIRS) $($(1).srpm)
+ $(if $($(1)-DEPEND-DEVEL-RPMS), $(RPM-UNINSTALL-DEVEL) $($(1)-DEPEND-DEVEL-RPMS))
+ @(echo -n "XXXXXXXXXXXXXXX -- END compile $(1) " ; date)
+.PHONY: $(1)-compile
+endef
+
+$(foreach package,$(ALL),$(eval $(call target_binary_rpm,$(package))))
+### shorthand target
+# e.g. make proper -> does propers rpms
+# usage shorthand_target package
+define target_shorthand
+$(1): $($(1).rpms)
+.PHONY: $(1)
+$(1)-spec: $($(1)-SPEC)
+.PHONY: $(1)-spec
+$(1)-mk: $($(1)-MK)
+.PHONY: $(1)-mk
+$(1)-tarball: $($(1).tarballs)
+.PHONY: $(1)-tarball
+$(1)-codebase: $($(1).codebase)
+.PHONY: $(1)-source
+$(1)-source: $($(1).source)
+.PHONY: $(1)-codebase
+$(1)-rpms: $($(1).rpms)
+.PHONY: $(1)-rpms
+$(1)-srpm: $($(1).srpm)
+.PHONY: $(1)-srpm
+endef
+
+$(foreach package,$(ALL),$(eval $(call target_shorthand,$(package))))
+
+### file dependencies
+define package_depends_on_file
+$(1):$(2)
+$($(1).srpm):$(2)
+endef
+
+define target_dependfiles
+$(foreach file,$($(1)-DEPEND-FILES),$(eval $(call package_depends_on_file,$(1),$(file))))
+endef
+
+$(foreach package,$(ALL),$(eval $(call target_dependfiles,$(package))))
+
+### package dependencies
+define package_depends_on_package
+$(1):$(2)
+$(1):$($(2).rpms)
+$($(1).srpm):$($(2).rpms)
+endef
+
+define target_depends
+$(foreach package,$($(1)-DEPEND-PACKAGES) $($(1).depend-devel-packages),$(eval $(call package_depends_on_package,$(1),$(package))))
+endef
+
+$(foreach package,$(ALL),$(eval $(call target_depends,$(package))))
+
+### clean target
+# usage: target_clean package
+define target_clean
+$(1)-clean-codebase:
+ rm -rf $($(1).codebase)
+.PHONY: $(1)-clean-codebase
+CLEANS += $(1)-clean-codebase
+$(1)-clean-source:
+ rm -rf $($(1).source)
+.PHONY: $(1)-clean-source
+CLEANS += $(1)-clean-source
+$(1)-clean-tarball:
+ rm -rf $($(1).tarballs)
+.PHONY: $(1)-clean-tarball
+CLEANS += $(1)-clean-tarball
+$(1)-clean-build:
+ rm -rf BUILD/$(notdir $($(1).source))
+CLEANS += $(1)-clean-build
+$(1)-clean-rpms:
+ rm -rf $($(1).rpms)
+.PHONY: $(1)-clean-rpms
+CLEANS += $(1)-clean-rpms
+$(1)-clean-srpm:
+ rm -rf $($(1).srpm)
+.PHONY: $(1)-clean-srpm
+CLEANS += $(1)-clean-srpm
+$(1)-codeclean: $(1)-clean-source $(1)-clean-tarball $(1)-clean-build $(1)-clean-rpms $(1)-clean-srpm
+$(1)-clean: $(1)-clean-codebase $(1)-codeclean
+.PHONY: $(1)-codeclean $(1)-clean
+$(1)-clean-spec:
+ rm -rf $($(1).specpath)
+.PHONY: $(1)-clean-spec
+$(1)-clean-make:
+ rm -rf MAKE/$(1).mk
+.PHONY: $(1)-clean-make
+$(1)-distclean: $(1)-distclean1 $(1)-distclean2
+$(1)-distclean1: $(1)-clean-spec $(1)-clean-make
+$(1)-distclean2: $(1)-clean
+.PHONY: $(1)-distclean $(1)-distclean1 $(1)-distclean2
+endef
+
+$(foreach package,$(ALL),$(eval $(call target_clean,$(package))))
+
+### clean precisely
+clean:
+ $(MAKE) $(CLEANS)
+.PHONY: clean
+
+clean-help:
+ @echo Available clean targets
+ @echo $(CLEANS)
+
+### brute force clean
+distclean1:
+ rm -rf pldistro.mk .rpmmacros spec2make SPECS MAKE
+distclean2:
+ rm -rf CODEBASES SOURCES BUILD RPMS SRPMS tmp
+distclean: distclean1 distclean2
+.PHONY: distclean1 distclean2 distclean
+
+develclean:
+ $(RPM-UNINSTALL-DEVEL) $(ALL-DEVEL-RPMS)
+
+####################
+# gather build information for the 'About' page
+# when run from crontab, INIT_CWD not properly set (says /root ..)
+# so, the nightly build passes NIGHTLY_BASE here
+# also store the nightly_base in .base for any post-processing purposes
+myplc-release:
+ @echo 'Creating myplc-release'
+ rm -f $@
+ echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx build info" >> $@
+ $(MAKE) --no-print-directory version-build >> $@
+ echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx svn info" >> $@
+ $(MAKE) --no-print-directory version-svns >> $@
+ echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx rpm info" >> $@
+ $(MAKE) --no-print-directory version-rpms >> $@
+ @echo $(NIGHTLY_BASE) > .base
+
+version-build:
+ @echo -n 'Build build-date: ' ; date '+%Y.%m.%d'
+ @echo -n 'Build build-time: ' ; date '+%H:%M'
+ @echo -n 'Build build-hostname: ' ; hostname
+ @echo "Build build-base: $(NIGHTLY_BASE)"
+ @echo "Build planetab-distro: $(PLDISTRO)"
+ @echo "Build planetlab-tags: $(PLDISTROTAGS)"
+ @echo -n 'Build planetlab-tagsid: ' ; fgrep '$$''Id' $(PLDISTROTAGS)
+ @echo "Build target-arch: $(HOSTARCH)"
+ @echo "Build target-distro: $(DISTRO)"
+ @echo "Build target-release: $(RELEASE)"
+ @echo "Build target-personality: $(NIGHTLY_PERSONALITY)"
+
+####################
+# for a given module
+VFORMAT="%30s := %s\n"
+define svn_version_target
+$(1)-version-svn:
+ @$(if $($(1)-SVNPATH),\
+ printf $(VFORMAT) $(1)-SVNPATH "$($(1)-SVNPATH)",\
+ printf $(VFORMAT) $(1)-CVSROOT "$($(1)-CVSROOT)" ; printf $(VFORMAT) $(1)-TAG "$($(1)-TAG)")
+endef
+
+# compute all modules
+ALL-MODULES :=
+$(foreach package,$(ALL), $(eval ALL-MODULES+=$($(package)-MODULES)))
+ALL-MODULES:=$(sort $(ALL-MODULES))
+
+$(foreach module,$(ALL-MODULES), $(eval $(call svn_version_target,$(module))))
+
+version-svns: $(foreach module, $(ALL-MODULES), $(module)-version-svn)
+
+RFORMAT="%20s :: version=%s release=%s\n"
+define rpm_version_target
+$(1)-version-rpm:
+ @printf $(RFORMAT) $($(1).rpm-name) $($(1).rpm-version) $($(1).rpm-release)
+version-rpms: $(1)-version-rpm
+endef
+
+$(foreach package,$(sort $(ALL)), $(eval $(call rpm_version_target,$(package))))
+
+versions: myplc-release version-build version-svns version-rpms
+.PHONY: versions version-build version-rpms version-svns
+
+#################### include install Makefile
+# the default is to use the distro-dependent install file
+# however the main distro file can redefine PLDISTROINSTALL
+ifndef PLDISTROINSTALL
+PLDISTROINSTALL := $(PLDISTRO)-install.mk