use MD5 and gzip for binary and source rpms. this doesn't change
[build.git] / Makefile
1 #
2 # Thierry Parmentelat - INRIA Sophia Antipolis 
3 #
4 ### $Id$
5 ### $URL$
6
7 ####################
8 # invocation:
9 #
10 # (*) make stage1=true
11 #     this extracts all specfiles and computes .mk from specfiles
12 #     you need to specify PLDISTRO here if relevant - see below
13 # (*) make help
14 #     for more info on how to invoke this stuff
15 #
16 #################### (fedora) distributions
17 #
18 # (*) as of nov. 2007, myplc-devel is deprecated
19 # (*) instead, we create a fresh vserver that holds required tools (see e.g. planetlab-devel.lst)
20 # (*) the build uses the current fedora version as a target for the produced images
21 # (*) so you simply need to create a fedora 8 build image for building fedora-8 images 
22 #     
23 #################### (planetlab) distributions
24 #
25 # (*) see README-pldistros.txt
26 # (*) then you need to run 
27 #     make stage1=true PLDISTRO=onelab
28 #
29 #################### 
30 # This build deals with 3 kinds of objects
31
32 # (*) packages are named upon the RPM name; they are mostly lowercase
33 #     Add a package to ALL if you want it built as part of the default set.
34 # (*) modules are named after the subversion tree; as of this writing their names 
35 #     are mostly mixed case like MyPLC or VserverReference
36 # (*) rpms are named in the spec files. A package typically defines several rpms;
37 #     rpms are used for defining DEPEND-DEVEL-RPMS. See also package.rpmnames
38
39 #################### packages
40 # basics: how to build a package - you need/may define the following variables
41
42 # (*) package-MODULES
43 #     a package needs one or several modules to build. 
44 #     to this end, define 
45 # (*) package-SPEC
46 #     the package's specfile; this is relative to the FIRST module in package-MODULES
47 #     see 'codebase' below
48 #
49 # Optional:
50 #
51 # (*) package-SPECVARS
52 #     space-separated list of spec variable definitions, where you can reference make variable that relate to 
53 #     packages defined BEFORE the current one (note: you should use = - as opposed to := - to define these)
54 #     e.g. mydriver-SPECVARS = foo=$(kernel-rpm-release) 
55 #     would let you use the %release from the kernel's package when rpmbuild'ing mydriver - see automatic below
56 # (*) package-DEPEND-PACKAGES
57 #     a set of *packages* that this package depends on
58 # (*) package-DEPEND-DEVEL-RPMS
59 #     a set of *rpms* that the build will rpm-install before building <package>
60 #     the build will attempt to uninstall those once the package is built, this is not fatal though
61 #     this is intended to denote local rpms, i.e. ones that are results of our own build
62 #     stock rpms should be mentioned in config.planetlab/devel.pkgs
63 # (*) package-DEPEND-FILES
64 #     a set of files that the package depends on - and that make needs to know about
65 #     if this contains RPMS/yumgroups.xml, then the toplevel RPMS's index 
66 #     is refreshed with createrepo prior to running rpmbuild
67 # (*) package-EXCLUDE-DEVEL-RPMS
68 #     a set of *rpms* that the build will rpm-uninstall before building <package>
69 #     this is intended to denote stock rpms, and the build will attempt to yum-install them
70 #     back after the package is rebuilt
71 # (*) package-RPMFLAGS: Miscellaneous RPM flags
72 # (*) package-RPMBUILD: If not rpmbuild - mostly used for sudo'ing rpmbuild
73 # (*) package-BUILD-FROM-SRPM: set this to any non-empty value, if your package is able to produce 
74 #     a source rpms by running 'make srpm'
75 # (*) package-RPMDATE: set this to any non-empty value to get the rpm package's release field hold the current date
76 #     this is useful for container packages, like e.g. bootstrapfs or vserver, that contains much more than the
77 #     correspondng module
78 #
79 #################### modules
80 # Required information about the various modules (set this in e.g. planetlab-tags.mk)
81 #
82 # (*) module-SVNPATH
83 #     the complete path where this module lies; 
84 #     you can specify the trunk or a given tag with this variable
85
86 # OR if the module is managed under cvs (will be obsoleted)
87
88 # (*) module-CVSROOT
89 # (*) module-TAG
90 #
91 #################### automatic variables
92 #
93 # the build defines some make variables that are extracted from spec files
94 # see for example
95 # (*)  $ make ulogd-pkginfo
96 #        to see the list f variables attached to a given package
97 # (*)  $ make kernel-devel-rpminfo
98 #        to see the list of variables attached to a given rpm
99 #
100 ####################
101
102 # exported to spec files as plrelease
103 PLANETLAB_RELEASE = 5.0
104
105 #
106 # Default values
107 #
108 # minimal compat with macos, just so this does not complain 
109 HOSTARCH := $(shell uname -i 2> /dev/null || uname -m 2> /dev/null)
110 DISTRO := $(shell ./getdistro.sh)
111 RELEASE := $(shell ./getrelease.sh)
112 DISTRONAME := $(shell ./getdistroname.sh)
113 RPM-INSTALL-DEVEL := rpm --force -Uvh
114 # uninstall -- cannot force rpm -e
115 # need to ignore result, kernel-headers cannot be uninstalled as glibc depends on it
116 RPM-UNINSTALL-DEVEL := rpm -e
117 YUM-INSTALL-DEVEL := yum -y install
118
119 # see also below
120 REMOTE-PLDISTROS="wextoolbox"
121
122 #################### Makefile
123 # Default target
124 all:
125 .PHONY:all
126
127 ### default values
128 PLDISTRO := planetlab
129 RPMBUILD := rpmbuild
130 export CVS_RSH := ssh
131
132 ########## savedpldistro.mk holds PLDISTRO - it is generated at stage1 (see below)
133 ifeq "$(stage1)" ""
134 include savedpldistro.mk
135 endif
136
137 # when re-running the nightly build after failure, we need to gather the former values
138 # do this by running make stage1=skip +PLDISTRO
139 ifeq "$(stage1)" "skip"
140 include savedpldistro.mk
141 endif
142
143 #################### include onelab.mk
144 # describes the set of components
145 PLDISTROCONTENTS := $(PLDISTRO).mk
146 include $(PLDISTROCONTENTS)
147
148 #################### include <pldistro>-tags.mk
149 # describes where to fetch components, and the related tags if using cvs
150 ifeq "$(PLDISTROTAGS)" ""
151 PLDISTROTAGS := $(PLDISTRO)-tags.mk
152 endif
153 include $(PLDISTROTAGS)
154
155 # this used to be set in the -tags.mk files, but that turned out to require
156 # error-prone duplicate changes 
157 # so now the nightly build script sets this to what it is currently using
158 # we set a default in case we run the build manually:
159 # if the local directory was svn checked out, then use the corresponding URL
160 svn-info-url-line := $(shell svn info 2> /dev/null | grep URL:)
161 default-build-SVNPATH := $(lastword $(svn-info-url-line))
162 # otherwise, use this hard-coded default
163 ifeq "$(default-build-SVNPATH)" ""
164 default-build-SVNPATH := http://svn.planet-lab.org/svn/build/trunk
165 endif
166 # use default if necessary
167 build-SVNPATH ?= $(default-build-SVNPATH)
168
169 ####################
170 define remote_pldistro
171 $(1).mk: config.$(1)/$(1).mk
172         @echo 'creating $(1) from config subdir'
173         cp config.$(1)/$(1).mk $(1).mk
174
175 $(2).mk: config.$(1)/$(2).mk
176         @echo 'creating $(1) tags from config subdir'
177         cp config.$(1)/$(2).mk $(2).mk
178
179 config.$(1)/$(1).mk: config.$(1)
180 config.$(1)/$(2).mk: config.$(1)
181
182 config.$(1): config.$(1).svnpath
183         @echo "Fetching details for pldistro $(1)"
184         svn export $(shell grep -v "^#" config.$(1).svnpath) config.$(1)
185
186 DISTCLEANS += $(1).mk $(2).mk config.$(1)
187
188 endef
189
190 # somehow this does not work, handle manually instead
191 #$(foreach distro, $(REMOTE-PLDISTROS), $(eval $(call remote_pldistro,$(distro),$(distro)-tags)))
192 $(eval $(call remote_pldistro,wextoolbox,wextoolbox-tags))
193
194 ########## stage1 and stage1iter
195 # extract specs and compute .mk files by running 
196 # make stage1=true
197 # entering stage1, we compute all the spec files
198 # then we use stage1iter to compute the .mk iteratively, 
199 # ensuring that the n-1 first makefiles are loaded when creating the n-th one
200 # when stage1iter is set, it is supposed to be an index (starting at 1) in $(ALL)
201
202 ALLMKS := $(foreach package, $(ALL), MAKE/$(package).mk)
203
204 ### stage1iter : need some arithmetic, see
205 # http://www.cmcrossroads.com/articles/ask-mr.-make/learning-gnu-make-functions-with-arithmetic.html
206 ifneq "$(stage1iter)" ""
207 # the first n packages
208 packages := $(wordlist 1,$(words $(stage1iter)),$(ALL))
209 # the n-th package
210 package := $(word $(words $(packages)),$(packages))
211 # the n-1 first packages
212 stage1iter_1 := $(wordlist 2,$(words $(stage1iter)),$(stage1iter))
213 previous := $(wordlist 1,$(words $(stage1iter_1)),$(ALL))
214 previousmks := $(foreach package,$(previous),MAKE/$(package).mk)
215 include $(previousmks)
216 all: verbose
217 verbose:
218         @echo "========== stage1iter : $(package)"
219 #       @echo "stage1iter : included .mk files : $(previousmks)"
220 all: $($(package).specpath)
221 all: MAKE/$(package).mk
222 else
223 ### stage1
224 ifneq "$(stage1)" ""
225 all : verbose
226 verbose :
227         @echo "========== stage1"
228 all : spec2make
229 all : .rpmmacros
230 # specs and makes are done sequentially by stage1iter
231 all : stage1iter
232 stage1iter:
233         arg=""; for n in $(ALL) ; do arg="$$arg x"; $(MAKE) --no-print-directory stage1iter="$$arg"; done
234 ### regular make
235 else
236 ### once .mks are OK, you can run make normally
237 include $(ALLMKS)
238 #all : tarballs
239 #all : sources
240 #all : codebases
241 #all : rpms
242 #all : srpms
243 # mention $(ALL) here rather than rpms 
244 # this is because the inter-package dependencies are expressed like
245 # util-vserver: util-python
246 all: rpms
247 all: repo
248 endif
249 endif
250
251 ### yumgroups.xml : compute from all known .pkgs files
252 RPMS/yumgroups.xml: 
253         mkdir -p RPMS
254         ./yumgroups.sh $(PLDISTRO) > $@
255
256 createrepo = createrepo --quiet -g yumgroups.xml RPMS/ 
257
258 repo: RPMS/yumgroups.xml
259         $(createrepo)
260
261 .PHONY: repo
262
263 ####################
264 # notes: 
265 # * to make configuration easier, we always use the first module's
266 # definitions (CVSROOT,TAG, or SVNPATH) to extract the spec file
267 # * for the same reason, in case cvs is used, the first module name is added to 
268 # $(package)-SPEC - otherwise the cvs modules have to define spec as 
269 # <module>/<module>.spec while svn modules just define it as <module>.spec
270 #
271 define stage1_package_vars
272 $(1).spec := $(notdir $($(1)-SPEC))
273 $(1).specpath := SPECS/$(1).spec
274 $(1).module := $(firstword $($(1)-MODULES))
275 endef
276
277 $(foreach package, $(ALL), $(eval $(call stage1_package_vars,$(package))))
278
279 # compute all modules
280 ALL-MODULES :=
281 $(foreach package,$(ALL), $(eval ALL-MODULES+=$($(package)-MODULES)))
282 ALL-MODULES:=$(sort $(ALL-MODULES))
283
284 # extract revision from -SVNPATH
285 define stage1_module_vars
286 $(1)-SVNPATH := $(strip $($(1)-SVNPATH))
287 $(1).svnpath := $(firstword $(subst @, ,$($(1)-SVNPATH)))
288 $(1).svnrev := $(word 2,$(subst @, @,$($(1)-SVNPATH)))
289 endef
290
291 $(foreach module,$(ALL-MODULES), $(eval $(call stage1_module_vars,$(module))))
292
293 #
294 # for each package, compute whether we need to set date 
295 # the heuristic is that we mention the date as part of the rpm release flag if
296 # (*) the package has requested it by setting package-RPMDATE (container packages should do that)
297 # (*) or SVNPATH contains 'trunk' or 'branches' 
298
299 define package_hasdate
300 $(1).has-date = $(if $($(1)-RPMDATE),yes, \
301                   $(if $($($(1).module)-SVNPATH), \
302                      $(if $(findstring /trunk,$($($(1).module)-SVNPATH)),yes, \
303                         $(if $(findstring /branches,$($($(1).module)-SVNPATH)),yes,)), \
304                      $(if $(findstring HEAD,$($($(1).module)-TAG)),yes,)))
305 endef
306
307 $(foreach package, $(ALL), $(eval $(call package_hasdate,$(package))))
308
309 ### the common header for generated specfiles
310 # useful when trying new specfiles manually
311 header.spec:
312         (echo -n "# Generated by planetlab build from $($(1)-SPEC) on " ; date) > $@
313         echo "%define distro $(DISTRO)" >> $@
314         echo "%define distrorelease $(RELEASE)" >> $@
315         echo "%define distroname $(DISTRONAME)" >> $@
316         echo "%define pldistro $(PLDISTRO)" >> $@
317         echo "%define plrelease $(PLANETLAB_RELEASE)" >> $@
318         echo "# use MD5 and gzip for binary and source files" << $@
319         echo "%global _binary_filedigest_algorithm 1" >> $@
320         echo "%global _source_filedigest_algorithm 1" >> $@
321         echo "%global _source_payload       w9.gzdio" >> $@
322         echo "%global _binary_payload       w9.gzdio" >> $@
323
324 ### extract spec file from scm
325 define target_spec
326 $($(1).specpath): header.spec
327         mkdir -p SPECS
328         cat header.spec > $($(1).specpath)
329         $(if $($(1).has-date),echo "%define date $(shell date +%Y.%m.%d)" >> $($(1).specpath),)
330         $(if $($(1)-SPECVARS), \
331           $(foreach line,$($(1)-SPECVARS), \
332             echo "%define" $(word 1,$(subst =, ,$(line))) "$(word 2,$(subst =, ,$(line)))" >> $($(1).specpath) ;))
333         echo "# included from $($(1)-SPEC)" >> $($(1).specpath)
334         $(if $($($(1).module)-SVNPATH),\
335           svn cat $($($(1).module).svnpath)/$($(1)-SPEC)$($($(1).module).svnrev) >> $($(1).specpath) || rm $($(1).specpath),\
336           cvs -d $($($(1).module)-CVSROOT) checkout \
337               -r $($($(1).module)-TAG) \
338               -p $($(1).module)/$($(1)-SPEC) >> $($(1).specpath))
339 endef
340
341 $(foreach package,$(ALL),$(eval $(call target_spec,$(package))))
342
343 ###
344 # Base rpmbuild in the current directory
345 # issues on fedora 8 : see the following posts
346 # http://forums.fedoraforum.org/showthread.php?t=39625 - and more specifically post#6
347 # https://www.redhat.com/archives/fedora-devel-list/2007-November/msg00171.html
348 REALROOT=/build
349 FAKEROOT=/longbuildroot
350 PWD=$(shell /bin/pwd)
351 ifeq "$(PWD)" "$(REALROOT)"
352 export HOME := $(FAKEROOT)
353 else
354 export HOME := $(PWD)
355 endif
356 .rpmmacros:
357 ifeq "$(shell pwd)" "/build"
358         rm -f $(FAKEROOT) ; ln -s $(REALROOT) $(FAKEROOT)
359 endif
360         rm -f $@ 
361         echo "%_topdir $(HOME)" >> $@
362         echo "%_tmppath $(HOME)/tmp" >> $@
363         echo "%__spec_install_pre %{___build_pre}" >> $@
364         ./getrpmmacros.sh >> $@
365
366 ### this utility allows to extract various info from a spec file
367 ### and to define them in makefiles
368 spec2make: spec2make.c
369         $(CC) -g -Wall $< -o $@ -lrpm -lrpmbuild
370
371 ### run spec2make on the spec file and include the result
372 # usage: spec2make package
373 define target_mk
374 MAKE/$(1).mk: $($(1).specpath) spec2make .rpmmacros
375         mkdir -p MAKE
376         ./spec2make $($(1)-RPMFLAGS) $($(1).specpath) $(1) > MAKE/$(1).mk || { rm MAKE/$(1).mk; exit 1; }
377 endef
378
379 $(foreach package,$(ALL),$(eval $(call target_mk,$(package))))
380
381 # stores PLDISTRO in a file
382 # this is done at stage1. later run wont get confused
383 SAVED_VARS=PLDISTRO PLDISTROTAGS build-SVNPATH PERSONALITY MAILTO BASE WEBPATH TESTBUILDURL WEBROOT
384 savedpldistro.mk:
385         @echo "# do not edit" > $@
386         @$(foreach var,$(SAVED_VARS),echo "$(var):=$($(var))" >> $@ ;)
387         @echo "# do not edit" > aliases
388         @echo -n "alias m=\"make " >> aliases
389         @$(foreach var,$(SAVED_VARS),echo -n " $(var)=$($(var))" >> aliases ;)
390         @echo "\"" >> aliases
391         @echo "alias m1=\"m stage1=true\"" >> aliases
392
393 savedpldistro: savedpldistro.mk
394 .PHONY: savedpldistro
395
396 # always refresh this
397 all: savedpldistro
398
399 #################### regular make
400
401 define stage2_variables
402 ### devel dependencies
403 $(1).rpmbuild = $(if $($(1)-RPMBUILD),$($(1)-RPMBUILD),$(RPMBUILD)) $($(1)-RPMFLAGS)
404 $(1).all-devel-rpm-paths := $(foreach rpm,$($(1)-DEPEND-DEVEL-RPMS),$($(rpm).rpm-path))
405 $(1).depend-devel-packages := $(sort $(foreach rpm,$($(1)-DEPEND-DEVEL-RPMS),$($(rpm).package)))
406 ALL-DEVEL-RPMS += $($(1)-DEPEND-DEVEL-RPMS)
407 endef
408
409 $(foreach package,$(ALL),$(eval $(call stage2_variables,$(package))))
410 ALL-DEVEL-RPMS := $(sort $(ALL-DEVEL-RPMS))
411
412
413 ### pack sources into tarballs
414 ALLTARBALLS:= $(foreach package, $(ALL), $($(package).tarballs))
415 tarballs: $(ALLTARBALLS)
416         @echo $(words $(ALLTARBALLS)) source tarballs OK
417 .PHONY: tarballs
418
419 SOURCES/%.tar.bz2: SOURCES/%
420         tar chpjf $@ -C SOURCES $*
421
422 SOURCES/%.tar.gz: SOURCES/%
423         tar chpzf $@ -C SOURCES $*
424
425 SOURCES/%.tgz: SOURCES/%
426         tar chpzf $@ -C SOURCES $*
427
428 ##
429 URLS/%: url=$(subst @colon@,:,$(subst @slash@,/,$(notdir $@)))
430 URLS/%: basename=$(notdir $(url))
431 URLS/%: 
432         echo curl $(url) -o SOURCES/$(basename)
433         touch $@
434
435 ### the directory SOURCES/<package>-<version> is made 
436 # with a copy -rl from CODEBASES/<package>
437 # the former is $(package.source) and the latter is $(package.codebase)
438 ALLSOURCES:=$(foreach package, $(ALL), $($(package).source))
439 # so that make does not use the rule below directly for creating the tarball files
440 .SECONDARY: $(ALLSOURCES)
441
442 sources: $(ALLSOURCES)
443         @echo $(words $(ALLSOURCES)) versioned source trees OK
444 .PHONY: sources
445
446 define target_link_codebase_sources
447 $($(1).source): $($(1).codebase) ; mkdir -p SOURCES ; cp -rl $($(1).codebase) $($(1).source)
448 endef
449
450 $(foreach package,$(ALL),$(eval $(call target_link_codebase_sources,$(package))))
451
452 ### codebase extraction
453 ALLCODEBASES:=$(foreach package, $(ALL), $($(package).codebase))
454 # so that make does not use the rule below directly for creating the tarball files
455 .SECONDARY: $(ALLCODEBASES)
456
457 codebases : $(ALLCODEBASES)
458         @echo $(words $(ALLCODEBASES)) codebase OK
459 .PHONY: codebases
460
461 ### extract codebase 
462 # usage: extract_single_module package 
463 define extract_single_module
464         mkdir -p CODEBASES
465         $(if $($($(1).module)-SVNPATH), cd CODEBASES && svn export $($($(1).module)-SVNPATH) $(1), cd CODEBASES && cvs -d $($($(1).module)-CVSROOT) export -r $($($(1).module)-TAG) -d $(1) $($(1).module))
466 endef
467
468 # usage: extract_multi_module package 
469 define extract_multi_module
470         mkdir -p CODEBASES/$(1) && cd CODEBASES/$(1) && (\
471         $(foreach m,$($(1)-MODULES), $(if $($(m)-SVNPATH), svn export $($(m)-SVNPATH) $(m);, cvs -d $($(m)-CVSROOT) export -r $($(m)-TAG) $(m);)))
472 endef
473
474 CODEBASES/%: package=$(notdir $@)
475 CODEBASES/%: multi_module=$(word 2,$($(package)-MODULES))
476 CODEBASES/%: 
477         @(echo -n "XXXXXXXXXXXXXXX -- BEG CODEBASE $(package) : $@ " ; date)
478         $(if $(multi_module),\
479           $(call extract_multi_module,$(package)),\
480           $(call extract_single_module,$(package)))
481         @(echo -n "XXXXXXXXXXXXXXX -- END CODEBASE $(package) : $@ " ; date)
482
483 ### source rpms
484 ALLSRPMS:=$(foreach package,$(ALL),$($(package).srpm))
485 srpms: $(ALLSRPMS)
486         @echo $(words $(ALLSRPMS)) source rpms OK
487 .PHONY: srpms
488
489 ### these macro handles the DEPEND-DEVEL-RPMS and EXCLUDE-DEVEL-RPMS tags for a hiven package
490 # before building : rpm-install DEPEND-DEVEL-RPMS and rpm-uninstall EXCLUDE
491 define handle_devel_rpms_pre 
492         $(if $($(1).all-devel-rpm-paths), echo "Installing for $(1)-DEPEND-DEVEL-RPMS" ; $(RPM-INSTALL-DEVEL) $($(1).all-devel-rpm-paths)) 
493         $(if $($(1)-EXCLUDE-DEVEL-RPMS), echo "Uninstalling for $(1)-EXCLUDE-DEVEL-RPMS" ; $(RPM-UNINSTALL-DEVEL) $($(1)-EXCLUDE-DEVEL-RPMS))
494 endef
495
496 define handle_devel_rpms_post
497         -$(if $($(1)-DEPEND-DEVEL-RPMS), echo "Unstalling for $(1)-DEPEND-DEVEL-RPMS" ; $(RPM-UNINSTALL-DEVEL) $($(1)-DEPEND-DEVEL-RPMS))
498         $(if $($(1)-EXCLUDE-DEVEL-RPMS), "Reinstalling for $(1)-EXCLUDE-DEVEL-RPMS" ; $(YUM-INSTALL-DEVEL) $($(1)-EXCLUDE-DEVEL-RPMS) )
499 endef
500
501 # usage: target_source_rpm package
502 define target_source_rpm 
503 ifeq "$($(1)-BUILD-FROM-SRPM)" ""
504 $($(1).srpm): $($(1).specpath) .rpmmacros $($(1).tarballs) 
505         mkdir -p BUILD SRPMS tmp
506         @(echo -n "XXXXXXXXXXXXXXX -- BEG SRPM $(1) (using SOURCES) " ; date)
507         $(call handle_devel_rpms_pre,$(1))
508         $($(1).rpmbuild) -bs $($(1).specpath)
509         $(call handle_devel_rpms_post,$(1))
510         @(echo -n "XXXXXXXXXXXXXXX -- END SRPM $(1) " ; date)
511 else
512 $($(1).srpm): $($(1).specpath) .rpmmacros $($(1).codebase)
513         mkdir -p BUILD SRPMS tmp
514         @(echo -n "XXXXXXXXXXXXXXX -- BEG SRPM $(1) (using make srpm) " ; date)
515         $(call handle_devel_rpms_pre,$(1))
516         make -C $($(1).codebase) srpm SPECFILE=$(HOME)/$($(1).specpath) EXPECTED_SRPM=$(notdir $($(1).srpm)) && \
517            rm -f SRPMS/$(notdir $($(1).srpm)) && \
518            ln $($(1).codebase)/$(notdir $($(1).srpm)) SRPMS/$(notdir $($(1).srpm)) 
519         $(call handle_devel_rpms_post,$(1))
520         @(echo -n "XXXXXXXXXXXXXXX -- END SRPM $(1) " ; date)
521 endif
522 endef
523
524 $(foreach package,$(ALL),$(eval $(call target_source_rpm,$(package))))
525
526 ### binary rpms are made from source rpm
527 ALLRPMS:=$(foreach package,$(ALL),$($(package).rpms))
528 # same as above, mention $(ALL) and not $(ALLRPMS)
529 rpms: $(ALLRPMS)
530         @echo $(words $(ALLRPMS)) binary rpms OK
531 .PHONY: rpms
532
533 # use tmp dirs when building binary rpm so make remains idempotent 
534 # otherwise SOURCES/ or SPEC gets touched again - which leads to rebuilding
535 RPM-USE-TMP-DIRS = --define "_sourcedir $(HOME)/tmp" --define "_specdir $(HOME)/tmp"
536 RPM-USE-COMPILE-DIRS = --define "_sourcedir $(HOME)/COMPILE" --define "_specdir $(HOME)/COMPILE"
537
538 # usage: build_binary_rpm package
539 # xxx hacky - invoke createrepo if DEPEND-FILES mentions RPMS/yumgroups.xml
540 define target_binary_rpm 
541 $($(1).rpms): $($(1).srpm)
542         mkdir -p RPMS tmp
543         @(echo -n "XXXXXXXXXXXXXXX -- BEG RPM $(1) " ; date)
544         $(if $(findstring RPMS/yumgroups.xml,$($(1)-DEPEND-FILES)), $(createrepo) , )
545         $(call handle_devel_rpms_pre,$(1))
546         $($(1).rpmbuild) --rebuild $(RPM-USE-TMP-DIRS) $($(1).srpm)
547         $(call handle_devel_rpms_post,$(1))
548         @(echo -n "XXXXXXXXXXXXXXX -- END RPM $(1) " ; date)
549 # for manual use only - in case we need to investigate the results of an rpmbuild
550 $(1)-compile: $($(1).srpm)
551         mkdir -p COMPILE tmp
552         @(echo -n "XXXXXXXXXXXXXXX -- BEG compile $(1) " ; date)
553         $(if $(findstring RPMS/yumgroups.xml,$($(1)-DEPEND-FILES)), $(createrepo) , )
554         $(call handle_devel_rpms_pre,$(1))
555         $($(1).rpmbuild) --recompile $(RPM-USE-TMP-DIRS) $($(1).srpm)
556         $(call handle_devel_rpms_post,$(1))
557         @(echo -n "XXXXXXXXXXXXXXX -- END compile $(1) " ; date)
558 .PHONY: $(1)-compile
559 endef
560
561 $(foreach package,$(ALL),$(eval $(call target_binary_rpm,$(package))))
562 ### shorthand target
563 # e.g. make proper -> does propers rpms
564 # usage shorthand_target package
565 define target_shorthand 
566 $(1): $($(1).rpms)
567 .PHONY: $(1)
568 $(1)-spec: $($(1)-SPEC)
569 .PHONY: $(1)-spec
570 $(1)-mk: $($(1)-MK)
571 .PHONY: $(1)-mk
572 $(1)-tarball: $($(1).tarballs)
573 .PHONY: $(1)-tarball
574 $(1)-codebase: $($(1).codebase)
575 .PHONY: $(1)-source
576 $(1)-source: $($(1).source)
577 .PHONY: $(1)-codebase
578 $(1)-rpms: $($(1).rpms)
579 .PHONY: $(1)-rpms
580 $(1)-srpm: $($(1).srpm)
581 .PHONY: $(1)-srpm
582 endef
583
584 $(foreach package,$(ALL),$(eval $(call target_shorthand,$(package))))
585
586 ### file dependencies
587 define package_depends_on_file
588 $(1):$(2)
589 $($(1).srpm):$(2)
590 endef
591
592 define target_dependfiles
593 $(foreach file,$($(1)-DEPEND-FILES),$(eval $(call package_depends_on_file,$(1),$(file))))
594 endef
595
596 $(foreach package,$(ALL),$(eval $(call target_dependfiles,$(package))))
597
598 ### package dependencies
599 define package_depends_on_package
600 $(1):$(2)
601 $(1):$($(2).rpms)
602 $($(1).srpm):$($(2).rpms)
603 endef
604
605 define target_depends
606 $(foreach package,$($(1)-DEPEND-PACKAGES) $($(1).depend-devel-packages),$(eval $(call package_depends_on_package,$(1),$(package))))
607 endef
608
609 $(foreach package,$(ALL),$(eval $(call target_depends,$(package))))
610
611 ### clean target
612 # usage: target_clean package
613 define target_clean
614 $(1)-clean-codebase:
615         rm -rf $($(1).codebase)
616 .PHONY: $(1)-clean-codebase
617 CLEANS += $(1)-clean-codebase
618 $(1)-clean-source:
619         rm -rf $($(1).source)
620 .PHONY: $(1)-clean-source
621 CLEANS += $(1)-clean-source
622 $(1)-clean-tarball:
623         rm -rf $($(1).tarballs)
624 .PHONY: $(1)-clean-tarball
625 CLEANS += $(1)-clean-tarball
626 $(1)-clean-build:
627         rm -rf BUILD/$(notdir $($(1).source))
628 CLEANS += $(1)-clean-build
629 $(1)-clean-rpms:
630         rm -rf $($(1).rpms)
631 .PHONY: $(1)-clean-rpms
632 CLEANS += $(1)-clean-rpms
633 $(1)-clean-srpm:
634         rm -rf $($(1).srpm)
635 .PHONY: $(1)-clean-srpm
636 CLEANS += $(1)-clean-srpm
637 $(1)-codeclean: $(1)-clean-source $(1)-clean-tarball $(1)-clean-build $(1)-clean-rpms $(1)-clean-srpm
638 $(1)-clean: $(1)-clean-codebase $(1)-codeclean
639 .PHONY: $(1)-codeclean $(1)-clean 
640 $(1)-clean-spec:
641         rm -rf $($(1).specpath)
642 .PHONY: $(1)-clean-spec
643 $(1)-clean-make:
644         rm -rf MAKE/$(1).mk
645 .PHONY: $(1)-clean-make
646 $(1)-distclean: $(1)-distclean1 $(1)-distclean2
647 $(1)-distclean1: $(1)-clean-spec $(1)-clean-make
648 $(1)-distclean2: $(1)-clean
649 .PHONY: $(1)-distclean $(1)-distclean1 $(1)-distclean2
650 endef
651
652 $(foreach package,$(ALL),$(eval $(call target_clean,$(package))))
653
654 ### clean precisely
655 clean:
656         $(MAKE) $(CLEANS)
657 .PHONY: clean
658
659 clean-help:
660         @echo Available clean targets
661         @echo $(CLEANS)
662
663 ### brute force clean
664 distclean1:
665         rm -rf savedpldistro.mk .rpmmacros spec2make header.spec SPECS MAKE $(DISTCLEANS)
666 distclean2:
667         rm -rf CODEBASES SOURCES BUILD BUILDROOT RPMS SRPMS tmp
668 distclean: distclean1 distclean2
669 .PHONY: distclean1 distclean2 distclean
670
671 develclean:
672         -$(RPM-UNINSTALL-DEVEL) $(ALL-DEVEL-RPMS)
673
674 ####################
675 # gather build information for the 'About' page
676 # when run from crontab, INIT_CWD not properly set (says /root ..)
677 # so, the nightly build passes BASE here
678 # also store BASE in .base for any post-processing purposes
679 myplc-release:
680         @echo 'Creating myplc-release'
681         rm -f $@
682         echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx build info" >> $@
683         $(MAKE) --no-print-directory version-build >> $@
684         echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx svn info" >> $@
685         $(MAKE) --no-print-directory version-svns >> $@
686         echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx rpm info" >> $@
687         $(MAKE) --no-print-directory version-rpms >> $@
688         @echo $(BASE) > .base
689
690 version-build:
691         @echo -n 'Build build-date: ' ; date '+%Y.%m.%d'
692         @echo -n 'Build build-time: ' ; date '+%H:%M-%Z'
693         @echo -n 'Build build-hostname: ' ; hostname
694         @echo    "Build build-base: $(BASE)"
695         @echo    "Build planetlab-distro: $(PLDISTRO)"
696         @echo    "Build planetlab-tags: $(PLDISTROTAGS)"
697         @echo -n 'Build planetlab-tagsid: ' ; fgrep '$$''Id' $(PLDISTROTAGS)
698         @echo    "Build target-arch: $(HOSTARCH)"
699         @echo    "Build target-distro: $(DISTRO)"
700         @echo    "Build target-distroname: $(DISTRONAME)"
701         @echo    "Build target-release: $(RELEASE)"     
702         @echo    "Build target-personality: $(PERSONALITY)"     
703
704 #################### 
705 # for a given module
706 VFORMAT="%30s := %s\n"
707 define svn_version_target
708 $(1)-version-svn:
709         @$(if $($(1)-SVNPATH),\
710            printf $(VFORMAT) $(1)-SVNPATH "$($(1)-SVNPATH)",\
711            printf $(VFORMAT) $(1)-CVSROOT "$($(1)-CVSROOT)" ; printf $(VFORMAT) $(1)-TAG "$($(1)-TAG)")
712 endef
713
714 $(foreach module,$(ALL-MODULES), $(eval $(call svn_version_target,$(module))))
715
716 version-svns: $(foreach module, $(ALL-MODULES), $(module)-version-svn)
717
718 RFORMAT="%20s :: version=%s release=%s\n"
719 define rpm_version_target
720 $(1)-version-rpm:
721         @printf $(RFORMAT) $($(1).rpm-name) $($(1).rpm-version) $($(1).rpm-release)
722 version-rpms: $(1)-version-rpm
723 endef
724
725 $(foreach package,$(sort $(ALL)), $(eval $(call rpm_version_target,$(package))))
726
727 versions: myplc-release version-build version-svns version-rpms
728 .PHONY: versions version-build version-rpms version-svns
729
730 #################### package info
731 PKGKEYS := tarballs source codebase srpm rpms rpmnames rpm-release rpm-name rpm-version rpm-subversion
732 %-pkginfo: package=$(subst -pkginfo,,$@)
733 %-pkginfo: 
734         @$(foreach key,$(PKGKEYS),echo "$(package).$(key)=$($(package).$(key))";)
735 ## rpm info
736 RPMKEYS := rpm-path package
737 %-rpminfo: rpm=$(subst -rpminfo,,$@)
738 %-rpminfo: 
739         @$(foreach key,$(RPMKEYS),echo "$(rpm).$(key)=$($(rpm).$(key))";)
740
741 #################### various lists - designed to run with stage1=true
742 packages:
743         @$(foreach package,$(ALL), echo package=$(package) ref_module=$($(package).module) modules=$($(package)-MODULES) rpmnames=$($(package).rpmnames); )
744
745 modules:
746         @$(foreach module,$(ALL-MODULES), echo module=$(module) svnpath=$($(module)-SVNPATH); )
747
748 branches:
749         @$(foreach module,$(ALL-MODULES), \
750           $(if $($(module)-SVNBRANCH),echo module=$(module) branch=$($(module)-SVNBRANCH);))
751
752 module-tools:
753         @$(foreach module,$(ALL-MODULES), \
754          $(if $($(module)-SVNPATH), \
755           $(if $($(module)-SVNBRANCH), \
756              echo $(module):$($(module)-SVNBRANCH); , \
757              echo $(module); )))
758
759 info: packages modules branches 
760
761 .PHONY: info packages modules branches module-tools
762
763 ####################
764 tests_svnpath:
765         @$(if $(tests-SVNPATH), echo $(tests-SVNPATH) > $@, \
766         echo "http://svn.planet-lab.org/svn/tests/trunk" > $@)
767
768
769 ####################
770 help:
771         @echo "********** Run make in two stages:"
772         @echo ""
773         @echo "make stage1=true PLDISTRO=onelab"
774         @echo " -> extracts all spec files in SPECS/ and mk files in MAKE/"
775         @echo "    as well as save PLDISTRO for subsequent runs"
776         @echo ""
777         @echo "********** Then you can use the following targets"
778         @echo 'make'
779         @echo "  rebuilds everything"
780         @echo 'make util-vserver'
781         @echo "  makes the RPMS related to util-vserver"
782         @echo "  equivalent to 'make util-vserver-rpms'"
783         @echo ""
784         @echo "********** Or, vertically - step-by-step for a given package"
785         @echo 'make util-vserver-codebase'
786         @echo "  performs codebase extraction in CODEBASES/util-vserver"
787         @echo 'make util-vserver-source'
788         @echo "  creates source link in SOURCES/util-vserver-<version>"
789         @echo 'make util-vserver-tarball'
790         @echo "  creates source tarball in SOURCES/util-vserver-<version>.<tarextension>"
791         @echo 'make util-vserver-srpm'
792         @echo "  build source rpm in SRPMS/"
793         @echo 'make util-vserver-rpms'
794         @echo "  build rpm(s) in RPMS/"
795         @echo ""
796         @echo "********** Or, horizontally, reach a step for all known packages"
797         @echo 'make codebases'
798         @echo 'make sources'
799         @echo 'make tarballs'
800         @echo 'make srpms'
801         @echo 'make rpms'
802         @echo ""
803         @echo "********** Manual targets"
804         @echo "make package-compile"
805         @echo "  The regular process uses rpmbuild --rebuild, that performs"
806         @echo "  a compilation directory cleanup upon completion. If you need to investigate"
807         @echo "  the intermediate compilation directory, use the -compile targets"
808         @echo "********** Cleaning examples"
809         @echo "make clean"
810         @echo "  removes the files made by make"
811         @echo "make distclean"
812         @echo "  brute-force cleaning, removes entire directories - requires a new stage1"
813         @echo "make develclean"
814         @echo "  rpm-uninstalls all devel packages installed during build"
815         @echo ""
816         @echo "make iptables-distclean"
817         @echo "  deep clean for a given package"
818         @echo "make iptables-codeclean"
819         @echo "  run this if you've made a change in the CODEBASES area for iptables"
820         @echo ""
821         @echo "make util-vserver-clean"
822         @echo "  removes codebase, source, tarball, build, rpm and srpm for util-vserver"
823         @echo "make util-vserver-clean-codebase"
824         @echo "  and so on for source, tarball, build, rpm and srpm"
825         @echo ""
826         @echo "********** Info examples"
827         @echo "make ++ALL"
828         @echo "  Displays the value of a given variable (here ALL)"
829         @echo "  with only a single plus sign only the value is displayed"
830         @echo "make info"
831         @echo "  is equivalent to make packages modules branches"
832         @echo "  provides various info on these objects"
833         @echo "make ulogd-pkginfo"
834         @echo "  Displays know attributes of a package"
835         @echo "make kernel-devel-rpminfo"
836         @echo "  Displays know attributes of an rpm"
837         @echo "make stage1=true PLDISTROTAGS=planetlab-tags-4.2.mk packages modules branches module-tools"
838         @echo "  Lists mentioned items - module-tools is used in modules.update"
839         @echo ""
840         @echo "********** Known pakages are"
841         @echo "$(ALL)"
842
843 #################### convenience, for debugging only
844 # make +foo : prints the value of $(foo)
845 # make ++foo : idem but verbose, i.e. foo=$(foo)
846 ++%: varname=$(subst +,,$@)
847 ++%:
848         @echo "$(varname)=$($(varname))"
849 +%: varname=$(subst +,,$@)
850 +%:
851         @echo "$($(varname))"