- add "--define date" to RPMFLAGS if DATE is defined
[build.git] / Rules.mk
1 #
2 # PlanetLab RPM generation
3 #
4 # Copyright (c) 2003  The Trustees of Princeton University (Trustees).
5 # All Rights Reserved.
6
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met: 
10
11 #     * Redistributions of source code must retain the above copyright
12 #       notice, this list of conditions and the following disclaimer.
13
14 #     * Redistributions in binary form must reproduce the above
15 #       copyright notice, this list of conditions and the following
16 #       disclaimer in the documentation and/or other materials provided
17 #       with the distribution.
18
19 #     * Neither the name of the copyright holder nor the names of its
20 #       contributors may be used to endorse or promote products derived
21 #       from this software without specific prior written permission.
22
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
27 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #
35 # $Id: Makerules,v 1.6 2004/04/12 19:56:02 mlh-pl_rpm Exp $
36 #
37
38 # Base cvsps and rpmbuild in the current directory
39 export HOME := $(shell pwd)
40 export CVSROOT CVS_RSH
41
42 #
43 # Parse spec file template
44 #
45
46 MK := SPECS/$(patsubst %.spec,%.mk,$(notdir $(SPEC)))
47
48 $(MK): SPECS/$(notdir $(SPEC)).in
49         # Substitute '$' for '%' and 'name := value' for '%define name value' or 'name: value'
50         sed -n \
51         -e 's/%{/$${/g' \
52         -e 's/^%define[  ]*\([^  ]*\)[   ]*\([^  ]*\)/\1 := \2/p' \
53         -e 's/^\([^      ]*\):[  ]*\([^  ]*\)/\1 := \2/p' \
54         $< > $@
55 ifneq ($(INITIAL),$(TAG))
56         # Get list of PatchSets
57         cvsps --cvs-direct --root $(CVSROOT) -r $(INITIAL) $(if $(TAG:HEAD=),-r $(TAG)) $(MODULE) | \
58         sed -ne 's|^PatchSet[    ]*\([0-9]*\)|PATCHES += \1|p' >> $@
59 ifeq ($(shell echo $(MAKE_VERSION) | awk '{ print ($$1 < 3.80) }'),1)
60         # make-3.80 can use $(eval) instead (see below)
61         cvsps --cvs-direct --root $(CVSROOT) -r $(INITIAL) $(if $(TAG:HEAD=),-r $(TAG)) $(MODULE) | \
62         sh Patchrules >> $@
63 endif
64 endif
65
66 SPECS/$(notdir $(SPEC)).in:
67         mkdir -p SPECS
68         cvs -d $(CVSROOT) checkout -r $(TAG) -p $(SPEC) > $@
69
70 include $(MK)
71
72 #
73 # Generate tarball
74 #
75
76 # Get rid of URL
77 Source0 := $(notdir $(if $(Source),$(Source),$(Source0)))
78
79 # Add tarball to the list of sources
80 SOURCES += SOURCES/$(Source0)
81
82 # Get rid of .tar.bz2 or .tar.gz or .tgz
83 Base0 := $(basename $(basename $(Source0)))
84
85 # Export module
86 SOURCES/$(Base0):
87         mkdir -p SOURCES
88         cd SOURCES && cvs -d $(CVSROOT) export -r $(INITIAL) -d $(Base0) $(MODULE)
89
90 .SECONDARY: $(SOURCES)/$(Base0)
91
92 # Generate tarball
93 SOURCES/$(Base0).tar.bz2: SOURCES/$(Base0)
94         tar cpjf $@ -C SOURCES $(Base0)
95
96 SOURCES/$(Base0).tar.gz SOURCES/$(Base0).tgz: SOURCES/$(Base0)
97         tar cpzf $@ -C SOURCES $(Base0)
98
99 SOURCES/$(Base0).tar: SOURCES/$(Base0)
100         tar cpf $@ -C SOURCES $(Base0)
101
102 #
103 # Generate patches
104 #
105
106 define PATCH_template
107
108 # In case the spec file did not explicitly list the PatchSet
109 ifeq ($$(origin Patch$(1)),undefined)
110 Patch$(1) := $$(package)-$(1).patch.bz2
111 endif
112
113 # Get rid of URL
114 Patch$(1) := $$(notdir $$(Patch$(1)))
115
116 # Add patch to the list of sources
117 SOURCES += SOURCES/$$(Patch$(1))
118
119 # Generate uncompressed patch
120 SOURCES/$$(patsubst %.gz,%,$$(patsubst %.bz2,%,$$(Patch$(1)))):
121         mkdir -p SOURCES
122         cvsps --cvs-direct --root $$(CVSROOT) -g -s $(1) $$(MODULE) > $$@
123
124 endef
125
126 # bzip2
127 %.bz2: %
128         bzip2 -c $< > $@
129
130 # gzip
131 %.gz: %
132         gzip -c $< > $@
133
134 # Generate rules to generate patches (make-3.80 and above expands this)
135 $(foreach n,$(PATCHES),$(eval $(call PATCH_template,$(n))))
136
137 #
138 # Generate spec file
139 #
140
141 ifeq ($(TAG),HEAD)
142 # Define date for untagged builds
143 DATE := $(shell date +%Y.%m.%d)
144 endif
145
146 # Generate spec file
147 SPECS/$(notdir $(SPEC)): SPECS/$(notdir $(SPEC)).in
148         rm -f $@
149 ifeq ($(TAG),HEAD)
150         # Define date for untagged builds
151         echo "%define date $(DATE)" >> $@
152 endif
153         # Rewrite patch sections of spec file
154         perl -n -e ' \
155         next if /^Patch.*/; \
156         next if /^%patch.*/; \
157         print; \
158         if (/^Source.*/) { $(foreach n,$(PATCHES),print "Patch$(n): $(Patch$(n))\n";) } \
159         if (/^%setup.*/) { $(foreach n,$(PATCHES),print "%patch$(n) -p1\n";) } \
160         ' $< >> $@
161
162 #
163 # Build
164 #
165
166 RPMFLAGS += $(if $(DATE),--define "date $(DATE)")
167 NVR := $(shell rpmquery $(RPMFLAGS) --specfile SPECS/$(notdir $(SPEC)).in 2>/dev/null | head -1)
168 ARCH := $(shell rpmquery $(RPMFLAGS) --queryformat '%{ARCH}\n' --specfile SPECS/$(notdir $(SPEC)).in 2>/dev/null | head -1)
169
170 all: RPMS/$(ARCH)/$(NVR).$(ARCH).rpm SRPMS/$(NVR).src.rpm
171
172 # Build RPM
173 RPMS/$(ARCH)/$(NVR).$(ARCH).rpm: SPECS/$(notdir $(SPEC)) $(SOURCES) .rpmmacros
174         mkdir -p BUILD RPMS
175         rpmbuild $(RPMFLAGS) -bb $<
176
177 # Build SRPM
178 SRPMS/$(NVR).src.rpm: SPECS/$(notdir $(SPEC)) $(SOURCES) .rpmmacros
179         mkdir -p SRPMS
180         rpmbuild $(RPMFLAGS) -bs $<
181
182 # Base rpmbuild in the current directory
183 .rpmmacros:
184         echo "%_topdir $(HOME)" > $@
185
186 # Remove files generated by this package
187 clean:
188         rm -rf \
189         BUILD/$(Base0) \
190         RPMS/$(ARCH)/$(NVR).$(ARCH).rpm \
191         SOURCES/$(Base0)* SOURCES/$(package)* \
192         SPECS/$(notdir $(SPEC)).in SPECS/$(notdir $(SPEC)) $(MK) \
193         SRPMS/$(NVR).src.rpm \
194         .cvsps/$(subst /,#,$(CVSROOT)/$(MODULE))
195
196 .PHONY: all clean