+# for each package, compute whether we need to set date (i.e. whether we use the trunk)
+# the myplc package is forced to have a date, because it is more convenient
+# (we cannot bump its number everytime something changes in the system)
+#
+define package_hasdate
+$(1)_hasdate = $(if $(subst myplc,,$(1)), \
+ $(if $($(1)-SVNPATH),\
+ $(if $(findstring /trunk,$($(1)-SVNPATH)),yes,),\
+ $(if $(findstring HEAD,$($(1)-TAG)),yes,)), \
+ yes)
+endef
+
+$(foreach package, $(ALL), $(eval $(call package_hasdate,$(package))))
+
+### extract spec file from scm
+# usage: extract_spec_file package
+# see early releases for comments on other possible implementations
+# cannot use variables in such rules, we need to inline everything, sigh
+define target_spec
+$($(1)_specpath):
+ mkdir -p CODESPECS
+ (echo -n "# Generated by planetlab build from $($(1)-SPEC) on " ; date) > $($(1)_specpath)
+ echo "%define distroname $(DISTRO)" >> $($(1)_specpath)
+ echo "%define distrorelease $(RELEASE)" >> $($(1)_specpath)
+ echo "%define pldistro $(PLDISTRO)" >> $($(1)_specpath)
+ $(if $($(1)_hasdate),echo "%define date $(shell date +%Y.%m.%d)" >> $($(1)_specpath),)
+ echo "# included from codebase specfile" >> $($(1)_specpath)
+ $(if $($(1)-SPECVARS), \
+ $(foreach line,$($(1)-SPECVARS), \
+ echo "%define" $(word 1,$(subst =, ,$(line))) "$(word 2,$(subst =, ,$(line)))" >> $($(1)_specpath) ;))
+ $(if $($($(1)_module)-SVNPATH),\
+ svn cat $($($(1)_module)-SVNPATH)/$($(1)-SPEC) >> $($(1)_specpath),\
+ cvs -d $($($(1)_module)-CVSROOT) checkout \
+ -r $($($(1)_module)-TAG) \
+ -p $($(1)_module)/$($(1)-SPEC) >> $($(1)_specpath))
+ @if [ -z $($(1)_specpath) ] ; then rm $($(1)_specpath) ; exit 1 ; fi
+endef
+
+$(foreach package,$(ALL),$(eval $(call target_spec,$(package))))
+
+### 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
+
+# Base rpmbuild in the current directory
+# trying a longer topdir
+# http://forums.fedoraforum.org/showthread.php?t=39625
+# and more specifically post#6
+# hard-wired for now
+export HOME := /building
+.rpmmacros:
+ rm -f /building ; ln -s /build /building
+ rm -f $@
+ echo "%_topdir $(HOME)" >> $@
+ echo "%_tmppath $(HOME)/tmp" >> $@
+ echo "%_netsharedpath /proc:/dev/pts" >> $@
+ echo "%_install_langs C:de:en:es:fr" >> $@
+ echo "%_excludedocs yes" >> $@
+
+### 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
+
+####################
+### 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 "$(subst srpm,,$(1))" "$(1)"
+$($(1)-SRPM): $($(1)_specpath) .rpmmacros $($(1)-TARBALLS)
+ mkdir -p BUILD SRPMS tmp
+ @(echo -n "XXXXXXXXXXXXXXX -- BEG SRPM $(1) " ; date)
+ -$(foreach devel,$($(1)-DEPENDDEVELS), $(if $($(devel)-DEVEL-RPMS),rpm --force -Uvh $($(devel)-DEVEL-RPMS);))
+ -$(foreach rpm,$($(1)-DEPENDDEVELRPMS), rpm --force -Uvh $($(rpm)-RPM-PATH);)
+ $(if $($(1)-RPMBUILD),\
+ $($(1)-RPMBUILD) $($(1)-RPMFLAGS) -bs $($(1)_specpath),
+ $(RPMBUILD) $($(1)-RPMFLAGS) -bs $($(1)_specpath))
+ @(echo -n "XXXXXXXXXXXXXXX -- END SRPM $(1) " ; date)