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