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