- get rid of possible URLs in Source and Patch tags
[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.2 2004/04/07 22:37:50 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 endif
60
61 SPECS/$(notdir $(SPEC)).in:
62         mkdir -p SPECS
63         cvs -d $(CVSROOT) checkout -r $(TAG) -p $(SPEC) > $@
64
65 include $(MK)
66
67 #
68 # Generate tarball
69 #
70
71 # Get rid of URL
72 Source0 := $(notdir $(Source0))
73
74 # Add tarball to the list of sources
75 SOURCES += SOURCES/$(Source0)
76
77 # Get rid of .tar.bz2 or .tar.gz or .tgz
78 Base0 := $(basename $(basename $(Source0)))
79
80 # Export module
81 SOURCES/$(Base0):
82         mkdir -p SOURCES
83         cd SOURCES && cvs -d $(CVSROOT) export -r $(INITIAL) -d $(Base0) $(MODULE)
84
85 .SECONDARY: $(SOURCES)/$(Base0)
86
87 # Generate tarball
88 SOURCES/$(Base0).tar.bz2: SOURCES/$(Base0)
89         tar cpjf $@ -C SOURCES $(Base0)
90
91 SOURCES/$(Base0).tar.gz SOURCES/$(Base0).tgz: SOURCES/$(Base0)
92         tar cpzf $@ -C SOURCES $(Base0)
93
94 SOURCES/$(Base0).tar: SOURCES/$(Base0)
95         tar cpf $@ -C SOURCES $(Base0)
96
97 #
98 # Generate patches
99 #
100
101 define PATCH_template
102
103 # In case the spec file did not explicitly list the PatchSet
104 ifeq ($$(origin Patch$(1)),undefined)
105 Patch$(1) := $(1).patch.bz2
106 endif
107
108 # Get rid of URL
109 Patch$(1) := $(notdir $$(Patch$(1)))
110
111 # Add patch to the list of sources
112 SOURCES += SOURCES/$$(Patch$(1))
113
114 # Generate uncompressed patch
115 SOURCES/$$(patsubst %.gz,%,$$(patsubst %.bz2,%,$$(Patch$(1)))):
116         mkdir -p SOURCES
117         cvsps --cvs-direct --root $(CVSROOT) -g -s $(1) $(MODULE) > $$@
118
119 endef
120
121 # bzip2
122 %.bz2: %
123         bzip2 -c $< > $@
124
125 # gzip
126 %.gz: %
127         gzip -c $< > $@
128
129 # Generate rules to generate patches
130 $(foreach n,$(PATCHES),$(eval $(call PATCH_template,$(n))))
131
132 #
133 # Generate spec file
134 #
135
136 ifeq ($(TAG),HEAD)
137 # Define date for untagged builds
138 DATE := $(shell date +%Y.%m.%d)
139 endif
140
141 # Generate spec file
142 SPECS/$(notdir $(SPEC)): SPECS/$(notdir $(SPEC)).in
143         rm -f $@
144 ifeq ($(TAG),HEAD)
145         # Define date for untagged builds
146         echo "%define date $(DATE)" >> $@
147 endif
148         # Rewrite patch sections of spec file
149         perl -n -e ' \
150         next if /^Patch.*/; \
151         next if /^%patch.*/; \
152         print; \
153         if (/^Source.*/) { $(foreach n,$(PATCHES),print "Patch$(n): $(Patch$(n))\n";) } \
154         if (/^%setup.*/) { $(foreach n,$(PATCHES),print "%patch$(n) -p1\n";) } \
155         ' $< >> $@
156
157 #
158 # Build
159 #
160
161 NVR := $(shell rpmquery $(RPMFLAGS) $(if $(DATE),--define "date $(DATE)") --specfile SPECS/$(notdir $(SPEC)).in | head -1)
162 ARCH := $(shell rpm $(RPMFLAGS) --showrc | sed -ne 's/^build arch[       ]*:[    ]*\(.*\)/\1/p')
163
164 all: RPMS/$(ARCH)/$(NVR).$(ARCH).rpm SRPMS/$(NVR).src.rpm
165
166 # Build RPM
167 RPMS/$(ARCH)/$(NVR).$(ARCH).rpm: SPECS/$(notdir $(SPEC)) $(SOURCES) .rpmmacros
168         mkdir -p BUILD RPMS
169         rpmbuild $(RPMFLAGS) -bb $<
170
171 # Build SRPM
172 SRPMS/$(NVR).src.rpm: SPECS/$(notdir $(SPEC)) $(SOURCES) .rpmmacros
173         mkdir -p SRPMS
174         rpmbuild $(RPMFLAGS) -bs $<
175
176 # Base rpmbuild in the current directory
177 .rpmmacros:
178         echo "%_topdir $(HOME)" > $@
179
180 .PHONY: all