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