- somehow I messed up the last check-in and removed most of the Makefile
[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.9 2004/08/11 21:41:16 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/%\([[: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 := $(basename $(basename $(Source0)))
85
86 # Export module
87 SOURCES/$(Base0):
88         mkdir -p SOURCES
89         cd SOURCES && cvs -d $(CVSROOT) export -r $(INITIAL) -d $(Base0) $(MODULE)
90
91 .SECONDARY: $(SOURCES)/$(Base0)
92
93 # Generate tarball
94 SOURCES/$(Base0).tar.bz2: SOURCES/$(Base0)
95         tar cpjf $@ -C SOURCES $(Base0)
96
97 SOURCES/$(Base0).tar.gz SOURCES/$(Base0).tgz: SOURCES/$(Base0)
98         tar cpzf $@ -C SOURCES $(Base0)
99
100 SOURCES/$(Base0).tar: SOURCES/$(Base0)
101         tar cpf $@ -C SOURCES $(Base0)
102
103 #
104 # Generate patches
105 #
106
107 define PATCH_template
108
109 # In case the spec file did not explicitly list the PatchSet
110 ifeq ($$(origin Patch$(1)),undefined)
111 Patch$(1) := $$(package)-$(1).patch.bz2
112 endif
113
114 # Get rid of URL
115 Patch$(1) := $$(notdir $$(Patch$(1)))
116
117 # Add patch to the list of sources
118 SOURCES += SOURCES/$$(Patch$(1))
119
120 # Generate uncompressed patch
121 SOURCES/$$(patsubst %.gz,%,$$(patsubst %.bz2,%,$$(Patch$(1)))):
122         mkdir -p SOURCES
123         cvsps --cvs-direct --root $$(CVSROOT) -g -s $(1) $$(MODULE) > $$@
124
125 endef
126
127 # bzip2
128 %.bz2: %
129         bzip2 -c $< > $@
130
131 # gzip
132 %.gz: %
133         gzip -c $< > $@
134
135 # Generate rules to generate patches (make-3.80 and above expands this)
136 $(foreach n,$(PATCHES),$(eval $(call PATCH_template,$(n))))
137
138 #
139 # Generate spec file
140 #
141
142 ifeq ($(TAG),HEAD)
143 # Define date for untagged builds
144 DATE := $(shell date +%Y.%m.%d)
145 endif
146
147 # Generate spec file
148 SPECS/$(notdir $(SPEC)): SPECS/$(notdir $(SPEC)).in
149         rm -f $@
150 ifeq ($(TAG),HEAD)
151         # Define date for untagged builds
152         echo "%define date $(DATE)" >> $@
153 endif
154         # Rewrite patch sections of spec file
155         perl -n -e ' \
156         next if /^Patch.*/; \
157         next if /^%patch.*/; \
158         print; \
159         if (/^Source.*/) { $(foreach n,$(PATCHES),print "Patch$(n): $(Patch$(n))\n";) } \
160         if (/^%setup.*/) { $(foreach n,$(PATCHES),print "%patch$(n) -p1\n";) } \
161         ' $< >> $@
162
163 #
164 # Build
165 #
166
167 ifeq ($(TAG),HEAD)
168 RPMFLAGS += --define "date $(DATE)"
169 endif
170 NVR := $(shell rpmquery $(RPMFLAGS) --specfile SPECS/$(notdir $(SPEC)).in 2>/dev/null | head -1)
171 ARCH := $(shell rpmquery $(RPMFLAGS) --queryformat '%{ARCH}\n' --specfile SPECS/$(notdir $(SPEC)).in 2>/dev/null | head -1)
172
173 all: RPMS/$(ARCH)/$(NVR).$(ARCH).rpm SRPMS/$(NVR).src.rpm
174
175 # Build RPM
176 RPMS/$(ARCH)/$(NVR).$(ARCH).rpm: SPECS/$(notdir $(SPEC)) $(SOURCES) .rpmmacros
177         mkdir -p BUILD RPMS
178         rpmbuild $(RPMFLAGS) -bb $<
179
180 # Build SRPM
181 SRPMS/$(NVR).src.rpm: SPECS/$(notdir $(SPEC)) $(SOURCES) .rpmmacros
182         mkdir -p SRPMS
183         rpmbuild $(RPMFLAGS) -bs $<
184
185 # Base rpmbuild in the current directory
186 .rpmmacros:
187         echo "%_topdir $(HOME)" > $@
188
189 # Remove files generated by this package
190 clean:
191         rm -rf \
192         BUILD/$(Base0) \
193         RPMS/$(ARCH)/$(NVR).$(ARCH).rpm \
194         SOURCES/$(Base0)* SOURCES/$(package)* \
195         SPECS/$(notdir $(SPEC)).in SPECS/$(notdir $(SPEC)) $(MK) \
196         SRPMS/$(NVR).src.rpm \
197         .cvsps/$(subst /,#,$(CVSROOT)/$(MODULE))
198
199 .PHONY: all clean