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