Fixes so that things work properly when we use SVN or CVS.
[build.git] / Rules.mk
1 #
2 # PlanetLab RPM generation
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2003-2006 The Trustees of Princeton University
6 #
7 # $Id: Rules.mk,v 1.37 2007/09/10 22:24:20 mef Exp $
8 #
9
10 # Base rpmbuild in the current directory
11 export HOME := $(shell pwd)
12 export CVSROOT CVS_RSH
13
14 #
15 # Create spec file
16 #
17
18 SPECFILE := SPECS/$(notdir $(SPEC))
19 MULTI_MODULE := $(word 2,$(MODULE))
20 # is there a single module ? to mimick cvs export -d behaviour
21 ifeq "$(MULTI_MODULE)" ""
22         _MAINMODULE := $(MODULE)
23 else
24         _MAINMODULE := $(firstword $(MODULE))
25 endif 
26
27 $(SPECFILE):
28         mkdir -p SPECS
29         echo "%define pldistro $(PLDISTRO)" > $@
30 ifeq ($(TAG),HEAD)
31         # Define date for untagged builds
32         echo "%define date $(shell date +%Y.%m.%d)" >> $@
33 else
34         # Define cvstag for tagged builds
35         echo "%define cvstag $(TAG)" >> $@
36 endif
37         $(if $(CVSROOT),\
38           cvs -d $(if $($(package)-CVSROOT),$($(package)-CVSROOT),$(CVSROOT)) checkout -r $(TAG) -p $(_MAINMODULE)/$(SPEC) >> $@; \
39         )
40
41         $(if $(SVNPATH),\
42           svn cat $(if $($(package)-SVNPATAH),$($(package)-SVNPATH),$(SVNPATH))/$(_MAINMODULE)/$(TAG)/$(SPEC) >> $@; \
43         )
44
45 #
46 # Parse spec file into Makefile fragment
47 #
48
49 MK := tmp/$(package).mk
50
51 parseSpec: CFLAGS := -g -Wall -D_GNU_SOURCE
52
53 parseSpec: LDFLAGS := -lrpm -lrpmbuild
54
55 $(MK): $(SPECFILE) parseSpec .rpmmacros
56         mkdir -p tmp
57         ./parseSpec $(RPMFLAGS) $(SPECFILE) > $@
58
59 # Defines SOURCES, SRPM, RPMS
60 include $(MK)
61
62 #
63 # Generate tarball(s)
64 #
65
66 # Get rid of any extensions
67 stripext = \
68 $(patsubst %.tar.bz2,%, \
69 $(patsubst %.tar.gz,%, \
70 $(patsubst %.tgz,%, \
71 $(patsubst %.zip,%, \
72 $(patsubst %.tar,%,$(1))))))
73
74 SOURCEDIRS := $(call stripext,$(SOURCES))
75
76 # Thierry - Jan 29 2007
77 # Allow different modules to have  different CVSROOT
78 # and/or to be extracted from their SVNPATH
79 #
80 # is there a single module ? to mimick cvs export -d behaviour
81 ifeq "$(MULTI_MODULE)" ""
82 # single module: do as before
83 SOURCES/$(package):
84         mkdir -p SOURCES && cd SOURCES && (\
85          $(if $(CVSROOT), cvs -d $(if $($(package)-CVSROOT),$($(package)-CVSROOT),$(CVSROOT)) export -r $(TAG) -d $(package) $(MODULE);) \
86          $(if $(SVNPATH), svn export $(if $($(package)-SVNPATH),$($(package)-SVNPATH),$(SVNPATH))/$(MODULE)/$(TAG) $(package) ;) \
87         )
88 else
89 # multiple modules : iterate 
90 SOURCES/$(package):
91         mkdir -p SOURCES/$(package) && cd SOURCES/$(package) && (\
92          $(foreach module,$(MODULE),\
93           $(if $(CVSROOT), cvs -d $(if $($(module)-CVSROOT),$($(module)-CVSROOT),$(CVSROOT)) export -r $(TAG)  $(module);)\
94           $(if $(SVNPATH), svn export $(if $($(module)-SVNPATH),$($(module)-SVNPATH),$(SVNPATH))/$(module)/$(TAG) $(module);)\
95          )\
96         )
97 endif
98
99 # Make a hard-linked copy of the exported directory for each Source
100 # defined in the spec file. However, our convention is that there
101 # should be only one Source file and one CVS module per RPM. It's okay
102 # if the CVS module consists of multiple directories, as long as the
103 # spec file knows what's going on.
104 $(SOURCEDIRS): SOURCES/$(package)
105         cp -rl $< $@
106
107 .SECONDARY: SOURCES/$(package) $(SOURCEDIRS)
108
109 # Generate tarballs
110 SOURCES/%.tar.bz2: SOURCES/%
111         tar cpjf $@ -C SOURCES $*
112
113 SOURCES/%.tar.gz: SOURCES/%
114         tar cpzf $@ -C SOURCES $*
115
116 SOURCES/%.tgz: SOURCES/%
117         tar cpzf $@ -C SOURCES $*
118
119 SOURCES/%.zip: SOURCES/%
120         cd SOURCES && zip -r ../$@ $*
121
122 SOURCES/%.tar: SOURCES/%
123         tar cpf $@ -C SOURCES $*
124
125 #
126 # Build
127 #
128
129 all: $(RPMS) $(SRPM)
130
131 # Build RPMS
132 $(RPMS): $(SPECFILE) $(SOURCES)
133         mkdir -p BUILD RPMS
134         $(RPMBUILD) $(RPMFLAGS) -bb $<
135
136 # Make the rest of the RPMS depend on the first one since building one
137 # builds them all.
138 ifneq ($(words $(RPMS)),1)
139 $(wordlist 2,$(words $(RPMS)),$(RPMS)): $(firstword $(RPMS))
140 endif
141
142 # Build SRPM
143 $(SRPM): $(SPECFILE) $(SOURCES)
144         mkdir -p SRPMS
145         $(RPMBUILD) $(RPMFLAGS) -bs $<
146
147 # Base rpmbuild in the current directory
148 .rpmmacros:
149         echo "%_topdir $(HOME)" > $@
150         echo "%_tmppath $(HOME)/tmp" >> $@
151
152 # Remove files generated by this package
153 clean:
154         rm -rf \
155         $(RPMS) $(SRPM) \
156         $(patsubst SOURCES/%,BUILD/%,$(SOURCEDIRS)) \
157         $(SOURCES) $(SOURCEDIRS) SOURCES/$(package) \
158         $(MK) $(SPECFILE)
159
160 .PHONY: all clean
161
162 #################### convenience, for debugging only
163 # make +foo : prints the value of $(foo)
164 # make ++foo : idem but verbose, i.e. foo=$(foo)
165 ++%: varname=$(subst +,,$@)
166 ++%:
167         @echo $(varname)=$($(varname))
168 +%: varname=$(subst +,,$@)
169 +%:
170         @echo $($(varname))