always use PWD as HOME to get current build directory as home directory for rpmbuild
[build.git] / Rules.mk
1 #
2 # PlanetLab RPM generation
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2003-2006 The Trustees of Princeton University
6 #
7 # $Id: Rules.mk,v 1.35 2007/08/22 14:41:32 mef Exp $
8 #
9
10 # Base rpmbuild in the current directory
11 export HOME := $(shell pwd)
12 export CVSROOT CVS_RSH
13
14 #
15 # Create spec file
16 #
17
18 SPECFILE := SPECS/$(notdir $(SPEC))
19
20 $(SPECFILE):
21         mkdir -p SPECS
22         echo "%define pldistro $(PLDISTRO)" > $@
23 ifeq ($(TAG),HEAD)
24         # Define date for untagged builds
25         echo "%define date $(shell date +%Y.%m.%d)" >> $@
26 else
27         # Define cvstag for tagged builds
28         echo "%define cvstag $(TAG)" >> $@
29 endif
30         $(if $($(package)-SVNPATH),\
31   svn cat $($(package)-SVNPATH)/$(SPEC) >> $@,\
32   cvs -d $(CVSROOT) checkout -r $(TAG) -p $(SPEC) >> $@)
33
34
35 #
36 # Parse spec file into Makefile fragment
37 #
38
39 MK := tmp/$(package).mk
40
41 parseSpec: CFLAGS := -g -Wall -D_GNU_SOURCE
42
43 parseSpec: LDFLAGS := -lrpm -lrpmbuild
44
45 $(MK): $(SPECFILE) parseSpec .rpmmacros
46         mkdir -p tmp
47         ./parseSpec $(RPMFLAGS) $(SPECFILE) > $@
48
49 # Defines SOURCES, SRPM, RPMS
50 include $(MK)
51
52 #
53 # Generate tarball(s)
54 #
55
56 # Get rid of any extensions
57 stripext = \
58 $(patsubst %.tar.bz2,%, \
59 $(patsubst %.tar.gz,%, \
60 $(patsubst %.tgz,%, \
61 $(patsubst %.zip,%, \
62 $(patsubst %.tar,%,$(1))))))
63
64 SOURCEDIRS := $(call stripext,$(SOURCES))
65
66 # Thierry - Jan 29 2007
67 # Allow different modules to have  different CVSROOT
68 # and/or to be extracted from their SVNPATH
69 #
70 # is there a single module ? to mimick cvs export -d behaviour
71 MULTI_MODULE := $(word 2,$(MODULE))
72 ifeq "$(MULTI_MODULE)" ""
73 # single module: do as before
74 SOURCES/$(package):
75         mkdir -p SOURCES
76         $(if $($(package)-SVNPATH),\
77   cd SOURCES && svn export $($(package)-SVNPATH) $(package),\
78   cd SOURCES && cvs -d $(CVSROOT) export -r $(TAG) -d $(package) $(MODULE))
79 else
80 # multiple modules : iterate 
81 SOURCES/$(package):
82         mkdir -p SOURCES/$(package) && cd SOURCES/$(package) && (\
83         $(foreach module,$(MODULE),\
84          $(if $($(module)-SVNPATH), \
85   svn export $($(module)-SVNPATH) $(module);, \
86   cvs -d $(if $($(module)-CVSROOT),$($(module)-CVSROOT),$(CVSROOT)) export -r $(TAG)  $(module);\
87          )))
88 endif
89
90 # Make a hard-linked copy of the exported directory for each Source
91 # defined in the spec file. However, our convention is that there
92 # should be only one Source file and one CVS module per RPM. It's okay
93 # if the CVS module consists of multiple directories, as long as the
94 # spec file knows what's going on.
95 $(SOURCEDIRS): SOURCES/$(package)
96         cp -rl $< $@
97
98 .SECONDARY: SOURCES/$(package) $(SOURCEDIRS)
99
100 # Generate tarballs
101 SOURCES/%.tar.bz2: SOURCES/%
102         tar cpjf $@ -C SOURCES $*
103
104 SOURCES/%.tar.gz: SOURCES/%
105         tar cpzf $@ -C SOURCES $*
106
107 SOURCES/%.tgz: SOURCES/%
108         tar cpzf $@ -C SOURCES $*
109
110 SOURCES/%.zip: SOURCES/%
111         cd SOURCES && zip -r ../$@ $*
112
113 SOURCES/%.tar: SOURCES/%
114         tar cpf $@ -C SOURCES $*
115
116 #
117 # Build
118 #
119
120 all: $(RPMS) $(SRPM)
121
122 # Build RPMS
123 $(RPMS): $(SPECFILE) $(SOURCES) .rpmbuild.sh
124         mkdir -p BUILD RPMS
125         $(RPMBUILD) $(RPMFLAGS) -bb $<
126
127 # Make the rest of the RPMS depend on the first one since building one
128 # builds them all.
129 ifneq ($(words $(RPMS)),1)
130 $(wordlist 2,$(words $(RPMS)),$(RPMS)): $(firstword $(RPMS))
131 endif
132
133 # Build SRPM
134 $(SRPM): $(SPECFILE) $(SOURCES) .rpmbuild.sh
135         mkdir -p SRPMS
136         $(RPMBUILD) $(RPMFLAGS) -bs $<
137
138 # Base rpmbuild in the current directory
139 .rpmmacros:
140         echo "%_topdir $(HOME)" > $@
141         echo "%_tmppath $(HOME)/tmp" >> $@
142
143 .rpmbuild.sh:
144         echo "# GENERATED BY Rules.mk" > $@
145         echo 'export HOME=$(PWD)' >> $@
146         echo "# There must be a more clever way to pass all args to rpmbuild!" >> $@
147         echo 'rpmbuild $$1 $$2 $$3 $$4 $$5 $$6' >> $@
148         chmod 755 .rpmbuild.sh
149
150 # Remove files generated by this package
151 clean:
152         rm -rf \
153         $(RPMS) $(SRPM) \
154         $(patsubst SOURCES/%,BUILD/%,$(SOURCEDIRS)) \
155         $(SOURCES) $(SOURCEDIRS) SOURCES/$(package) \
156         $(MK) $(SPECFILE)
157
158 .PHONY: all clean
159
160 #################### convenience, for debugging only
161 # make +foo : prints the value of $(foo)
162 # make ++foo : idem but verbose, i.e. foo=$(foo)
163 ++%: varname=$(subst +,,$@)
164 ++%:
165         @echo $(varname)=$($(varname))
166 +%: varname=$(subst +,,$@)
167 +%:
168         @echo $($(varname))