This commit was manufactured by cvs2svn to create tag
[build.git] / Makerules
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$
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/%\([[:alnum:]]\+\)/$${\1}/g' \
53         -e 's/^$${define}[       ]*\([^  ]*\)[   ]*\([^  ]*\)/\1 := \2/p' \
54         -e 's/^\([^      ]*\):[  ]*\([^  ]*\)/\1 := \2/p' \
55         $< > $@
56 ifneq ($(INITIAL),$(TAG))
57         # Get list of PatchSets
58         cvsps --cvs-direct --root $(CVSROOT) -r $(INITIAL) $(if $(TAG:HEAD=),-r $(TAG)) $(MODULE) | \
59         sed -ne 's|^PatchSet[    ]*\([0-9]*\)|PATCHES += \1|p' >> $@
60 ifeq ($(shell echo $(MAKE_VERSION) | awk '{ print ($$1 < 3.80) }'),1)
61         # make-3.80 can use $(eval) instead (see below)
62         cvsps --cvs-direct --root $(CVSROOT) -r $(INITIAL) $(if $(TAG:HEAD=),-r $(TAG)) $(MODULE) | \
63         sh Patchrules >> $@
64 endif
65 endif
66
67 SPECS/$(notdir $(SPEC)).in:
68         mkdir -p SPECS
69         cvs -d $(CVSROOT) checkout -r $(TAG) -p $(SPEC) > $@
70
71 include $(MK)
72
73 #
74 # Generate tarball
75 #
76
77 # Get rid of URL
78 Source0 := $(notdir $(if $(Source),$(Source),$(Source0)))
79
80 # Add tarball to the list of sources
81 SOURCES += SOURCES/$(Source0)
82
83 # Get rid of .tar.bz2 or .tar.gz or .tgz
84 Base0 := $(Source0:.tgz=)
85 Base0 := $(Base0:.bz2=)
86 Base0 := $(Base0:.gz=)
87 Base0 := $(Base0:.tar=)
88
89 # Export module
90 SOURCES/$(Base0):
91         mkdir -p SOURCES
92         cd SOURCES && cvs -d $(CVSROOT) export -r $(INITIAL) -d $(Base0) $(MODULE)
93
94 .SECONDARY: $(SOURCES)/$(Base0)
95
96 # Generate tarball
97 SOURCES/$(Base0).tar.bz2: SOURCES/$(Base0)
98         tar cpjf $@ -C SOURCES $(Base0)
99
100 SOURCES/$(Base0).tar.gz SOURCES/$(Base0).tgz: SOURCES/$(Base0)
101         tar cpzf $@ -C SOURCES $(Base0)
102
103 SOURCES/$(Base0).tar: SOURCES/$(Base0)
104         tar cpf $@ -C SOURCES $(Base0)
105
106 #
107 # Generate patches
108 #
109
110 define PATCH_template
111
112 # In case the spec file did not explicitly list the PatchSet
113 ifeq ($$(origin Patch$(1)),undefined)
114 Patch$(1) := $$(package)-$(1).patch.bz2
115 endif
116
117 # Get rid of URL
118 Patch$(1) := $$(notdir $$(Patch$(1)))
119
120 # Add patch to the list of sources
121 SOURCES += SOURCES/$$(Patch$(1))
122
123 # Generate uncompressed patch
124 SOURCES/$$(patsubst %.gz,%,$$(patsubst %.bz2,%,$$(Patch$(1)))):
125         mkdir -p SOURCES
126         cvsps --cvs-direct --root $$(CVSROOT) -g -s $(1) $$(MODULE) > $$@
127
128 endef
129
130 # bzip2
131 %.bz2: %
132         bzip2 -c $< > $@
133
134 # gzip
135 %.gz: %
136         gzip -c $< > $@
137
138 # Generate rules to generate patches (make-3.80 and above expands this)
139 $(foreach n,$(PATCHES),$(eval $(call PATCH_template,$(n))))
140
141 #
142 # Generate spec file
143 #
144
145 ifeq ($(TAG),HEAD)
146 # Define date for untagged builds
147 DATE := $(shell date +%Y.%m.%d)
148 endif
149
150 # Generate spec file
151 SPECS/$(notdir $(SPEC)): SPECS/$(notdir $(SPEC)).in
152         rm -f $@
153 ifeq ($(TAG),HEAD)
154         # Define date for untagged builds
155         echo "%define date $(DATE)" >> $@
156 endif
157         # Rewrite patch sections of spec file
158         perl -n -e ' \
159         next if /^Patch.*/; \
160         next if /^%patch.*/; \
161         print; \
162         if (/^Source.*/) { $(foreach n,$(PATCHES),print "Patch$(n): $(Patch$(n))\n";) } \
163         if (/^%setup.*/) { $(foreach n,$(PATCHES),print "%patch$(n) -p1\n";) } \
164         ' $< >> $@
165
166 #
167 # Build
168 #
169
170 ifeq ($(TAG),HEAD)
171 RPMFLAGS += --define "date $(DATE)"
172 endif
173 NVR := $(shell rpmquery $(RPMFLAGS) --specfile SPECS/$(notdir $(SPEC)).in 2>/dev/null | head -1)
174 ARCH := $(shell rpmquery $(RPMFLAGS) --queryformat '%{ARCH}\n' --specfile SPECS/$(notdir $(SPEC)).in 2>/dev/null | head -1)
175
176 all: RPMS/$(ARCH)/$(NVR).$(ARCH).rpm SRPMS/$(NVR).src.rpm
177
178 # Build RPM
179 RPMS/$(ARCH)/$(NVR).$(ARCH).rpm: SPECS/$(notdir $(SPEC)) $(SOURCES) .rpmmacros
180         mkdir -p BUILD RPMS
181         rpmbuild $(RPMFLAGS) -bb $<
182
183 # Build SRPM
184 SRPMS/$(NVR).src.rpm: SPECS/$(notdir $(SPEC)) $(SOURCES) .rpmmacros
185         mkdir -p SRPMS
186         rpmbuild $(RPMFLAGS) -bs $<
187
188 # Base rpmbuild in the current directory
189 .rpmmacros:
190         echo "%_topdir $(HOME)" > $@
191
192 # Remove files generated by this package
193 clean:
194         rm -rf \
195         BUILD/$(Base0) \
196         RPMS/$(ARCH)/$(NVR).$(ARCH).rpm \
197         SOURCES/$(Base0)* SOURCES/$(package)* \
198         SPECS/$(notdir $(SPEC)).in SPECS/$(notdir $(SPEC)) $(MK) \
199         SRPMS/$(NVR).src.rpm \
200         .cvsps/$(subst /,#,$(CVSROOT)/$(MODULE))
201
202 .PHONY: all clean