use the same magic with thelinux-2.6 for build system
authorS.Çağlar Onur <caglar@cs.princeton.edu>
Mon, 10 Jan 2011 17:49:07 +0000 (12:49 -0500)
committerS.Çağlar Onur <caglar@cs.princeton.edu>
Mon, 10 Jan 2011 17:49:07 +0000 (12:49 -0500)
Makefile

index 331ed56..da4a2dc 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,55 +1,91 @@
+#
 # $Id$
 # $URL$
 #
 # $Id$
 # $URL$
 #
-WEBFETCH               := wget
-SHA1SUM                        := sha1sum
+CURL   ?= $(shell if test -f /usr/bin/curl ; then echo "curl -H Pragma: -O -R -S --fail --show-error" ; fi)
+WGET   ?= $(shell if test -f /usr/bin/wget ; then echo "wget -nd -m" ; fi)
+CLIENT ?= $(if $(CURL),$(CURL),$(if $(WGET),$(WGET)))
+AWK    = awk
+SHA1SUM        = sha1sum
+SED    = sed
 
 
-ALL                    += mkinitrd
-mkinitrd-URL           := http://build.planet-lab.org/third-party/mkinitrd-5.1.19.6-61.src.rpm
-mkinitrd-SHA1SUM       := 95acddd474efbee0c13b5b25b55dbf913c00a8db
-mkinitrd               := $(notdir $(mkinitrd-URL))
+# this is passed on the command line as the full path to <build>/SPECS/mkinitrd.spec
+SPECFILE = mkinitrd.spec
 
 
+# Thierry - when called from within the build, PWD is /build
 PWD=$(shell pwd)
 
 PWD=$(shell pwd)
 
-all: $(ALL)
-.PHONY: all
+# get nevr from specfile.
+ifndef NAME
+NAME := $(shell rpm $(RPMDEFS) $(DISTDEFS) -q --qf "%{NAME}\n" --specfile $(SPECFILE) | head -1)
+endif
+ifndef EPOCH
+EPOCH := $(shell rpm $(RPMDEFS) $(DISTDEFS) -q --qf "%{EPOCH}\n" --specfile $(SPECFILE) | head -1 | sed 's/(none)//')
+endif
+ifeq ($(EPOCH),(none))
+override EPOCH := ""
+endif
+ifndef VERSION
+VERSION := $(shell rpm $(RPMDEFS) $(DISTDEFS) -q --qf "%{VERSION}\n" --specfile $(SPECFILE)| head -1)
+endif
+ifndef RELEASE
+RELEASE := $(shell rpm $(RPMDEFS) $(DISTDEFS) -q --qf "%{RELEASE}\n" --specfile $(SPECFILE)| head -1)
+endif
+
+define get_sources_sha1
+$(shell cat sources 2>/dev/null | awk 'gensub("^.*/", "", 1, $$2) == "$@" { print $$1; exit; }')
+endef
+define get_sources_url
+$(shell cat sources 2>/dev/null | awk 'gensub("^.*/", "", 1, $$2) == "$@" { print $$2; exit; }')
+endef
+SOURCEFILES := $(shell cat sources 2>/dev/null | awk '{ print gensub("^.*/", "", 1, $$2) }')
+SOURCE_RPM := $(firstword $(SOURCEFILES))
+
+sources: $(SOURCEFILES) $(TARGETS)
 
 
-##############################
-define download_target
-$(1): $($(1))
-.PHONY: $($(1))
-$($(1)): 
-       @if [ ! -e "$($(1))" ] ; then echo "$(WEBFETCH) $($(1)-URL)" ; $(WEBFETCH) $($(1)-URL) ; fi
-       @if [ ! -e "$($(1))" ] ; then echo "Could not download source file: $($(1)) does not exist" ; exit 1 ; fi
-       @if test "$$$$($(SHA1SUM) $($(1)) | awk '{print $$$$1}')" != "$($(1)-SHA1SUM)" ; then \
-           echo "sha1sum of the downloaded $($(1)) does not match the one from 'Makefile'" ; \
-           echo "Local copy: $$$$($(SHA1SUM) $($(1)))" ; \
-           echo "In Makefile: $($(1)-SHA1SUM)" ; \
-           false ; \
+$(SOURCEFILES): #FORCE
+       @if [ ! -e "$@" ] ; then echo "$(CLIENT) $(get_sources_url)" ; $(CLIENT) $(get_sources_url) ; fi
+       @if [ ! -e "$@" ] ; then echo "Could not download source file: $@ does not exist" ; exit 1 ; fi
+       @if test "$$(sha1sum $@ | awk '{print $$1}')" != "$(get_sources_sha1)" ; then \
+           echo "sha1sum of the downloaded $@ does not match the one from 'sources' file" ; \
+           echo "Local copy: $$(sha1sum $@)" ; \
+           echo "In sources: $$(grep $@ sources)" ; \
+           exit 1 ; \
        else \
        else \
-           ls -l $($(1)) ; \
+           ls -l $@ ; \
        fi
        fi
-endef
 
 
-$(eval $(call download_target,mkinitrd))
+download-sources:
+       @for i in $(SOURCES); do \
+               if [ ! -e "$${i##*/}" ]; then \
+                       echo "$(CLIENT) $$i"; \
+                       $(CLIENT) $$i; \
+               fi; \
+       done
 
 
-sources: $(ALL) 
-.PHONY: sources
+replace-sources:
+       rm -f sources
+       @$(MAKE) new-sources
 
 
-####################
-# default - overridden by the build
-SPECFILE = mkinitrd.spec
+new-sources: download-sources
+       @for i in $(SOURCES); do \
+               echo "$(SHA1SUM) $$i >> sources"; \
+               $(SHA1SUM) $${i##*/} | $(AWK) '{ printf "%s  %s\n", $$1, "'"$$i"'" }' >> sources; \
+       done
 
 
-PWD=$(shell pwd)
 PREPARCH ?= noarch
 RPMDIRDEFS = --define "_sourcedir $(PWD)/SOURCES" --define "_builddir $(PWD)" --define "_srcrpmdir $(PWD)" --define "_rpmdir $(PWD)"
 trees: sources
        rpmbuild $(RPMDIRDEFS) $(RPMDEFS) --nodeps -bp --target $(PREPARCH) $(SPECFILE)
 
 PREPARCH ?= noarch
 RPMDIRDEFS = --define "_sourcedir $(PWD)/SOURCES" --define "_builddir $(PWD)" --define "_srcrpmdir $(PWD)" --define "_rpmdir $(PWD)"
 trees: sources
        rpmbuild $(RPMDIRDEFS) $(RPMDEFS) --nodeps -bp --target $(PREPARCH) $(SPECFILE)
 
+# use the stock source rpm, unwrap it,
+# copy the downloaded material
+# install our own specfile and patched patches
+# then rewrap with rpm
 srpm: sources
        mkdir -p SOURCES SRPMS
 srpm: sources
        mkdir -p SOURCES SRPMS
-       (cd SOURCES; rpm2cpio ../mkinitrd-5.1.19.6-61.src.rpm | cpio -diu; \
-       cp ../$(notdir $(SPECFILE)) .;cp ../*.patch .)  
+       (cd SOURCES; rpm2cpio ../$(SOURCE_RPM) | cpio -diu; \
+        cp ../$(notdir $(SPECFILE)) . ; cp ../*.patch .;)
        ./rpmmacros.sh
        export HOME=$(shell pwd) ; rpmbuild $(RPMDIRDEFS) $(RPMDEFS) --nodeps -bs $(SPECFILE)
 
        ./rpmmacros.sh
        export HOME=$(shell pwd) ; rpmbuild $(RPMDIRDEFS) $(RPMDEFS) --nodeps -bs $(SPECFILE)
 
@@ -58,11 +94,5 @@ rpm: sources
        rpmbuild $(RPMDIRDEFS) $(RPMDEFS) --nodeps --target $(TARGET) -bb $(SPECFILE)
 
 clean:
        rpmbuild $(RPMDIRDEFS) $(RPMDEFS) --nodeps --target $(TARGET) -bb $(SPECFILE)
 
 clean:
-       rm -f *.rpm *.tgz *.bz2 *.gz
-
-++%: varname=$(subst +,,$@)
-++%:
-       @echo "$(varname)=$($(varname))"
-+%: varname=$(subst +,,$@)
-+%:
-       @echo "$($(varname))"
+       rm -f *.rpm
+       rm -rf BUILDROOT SOURCES SPECS SRPMS tmp