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