obsoletes useles -RPMBUILD
[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 endef
299
300 $(foreach package, $(ALL), $(eval $(call stage1_package_vars,$(package))))
301
302 # compute all modules
303 ALL.modules := build tests
304 $(foreach package,$(ALL), $(eval ALL.modules+=$($(package)-MODULES)))
305 ALL.modules:=$(sort $(ALL.modules))
306
307 # extract revision from -SVNPATH or tag from -GITPATH
308 define stage1_module_vars
309 ifneq "$($(1)-SVNPATH)" ""
310 $(1)-SVNPATH := $(strip $($(1)-SVNPATH))
311 $(1)-SCMPATH := $(strip $($(1)-SVNPATH))
312 else
313 $(1)-GITPATH := $$(strip $$($(1)-GITPATH))
314 $(1)-SCMPATH := $$(strip $$($(1)-GITPATH))
315 $(1).gitrepo := $$(firstword $$(subst @, ,$$($(1)-GITPATH)))
316 $(1).gittag := $$(word 2,$$(subst @, ,$$($(1)-GITPATH)))
317 $(1).gittag := $$(if $$($(1).gittag),$$($(1).gittag),master)
318 endif
319 endef
320
321 $(foreach module,$(ALL.modules), $(eval $(call stage1_module_vars,$(module))))
322
323 #
324 # for each package, compute whether we need to set date 
325 # the heuristic is that we mention the date as part of the rpm release flag if
326 # (*) the package has requested it by setting package-RPMDATE (container packages should do that)
327 # (*) or SVNPATH contains 'trunk' or 'branches' 
328 # (*) or GITPATH has no '@' (trunk) 
329 # (*) or GITPATH contains a '@', and the gittag part has no '-' (this is to tell a tag from a branch..)
330
331 define package_hasdate
332 $(1).has-date = $(if $($(1)-RPMDATE),yes, \
333                   $(if $($($(1).module)-SVNPATH), \
334                      $(if $(findstring /trunk,$($($(1).module)-SVNPATH)),yes, \
335                         $(if $(findstring /branches,$($($(1).module)-SVNPATH)),yes,)), \
336                      $(if $(not $($($(1).module).gittag)), yes,\
337                         $(if $(findstring -,$($($(1).module).gittag)),,yes))))
338 endef
339
340 $(foreach package, $(ALL), $(eval $(call package_hasdate,$(package))))
341
342 ### the common header for generated specfiles
343 # useful when trying new specfiles manually
344 header.spec:
345         (echo -n "# Generated by planetlab build from $($(1)-SPEC) on " ; date) > $@
346         echo "%define distro $(DISTRO)" >> $@
347         echo "%define distrorelease $(RELEASE)" >> $@
348         echo "%define distroname $(DISTRONAME)" >> $@
349         echo "%define pldistro $(PLDISTRO)" >> $@
350         echo "%define plrelease $(PLANETLAB_RELEASE)" >> $@
351         echo "# use MD5 and gzip for binary and source files" >> $@
352         echo "%global _binary_filedigest_algorithm 1" >> $@
353         echo "%global _source_filedigest_algorithm 1" >> $@
354         echo "%global _source_payload       w9.gzdio" >> $@
355         echo "%global _binary_payload       w9.gzdio" >> $@
356
357 ### make up spec file - extract module first
358 define target_spec
359 $($(1).specpath): header.spec $($(1).codespec)
360         mkdir -p SPECS
361         cat header.spec > $($(1).specpath)
362         echo "%define SCMURL $($($(1).module)-SCMPATH)" >> $($(1).specpath)
363         $(if $($(1).has-date),echo "%define date $(shell date +%Y.%m.%d)" >> $($(1).specpath),)
364         $(if $($(1)-SPECVARS), \
365           $(foreach line,$($(1)-SPECVARS), \
366             echo "%define" $(word 1,$(subst =, ,$(line))) "$(word 2,$(subst =, ,$(line)))" >> $($(1).specpath) ;))
367         echo "# included from $($(1)-SPEC)" >> $($(1).specpath)
368         cat $($(1).codespec) >> $($(1).specpath)
369
370 $($(1).codespec): $($(1).moduledir)
371
372 endef
373
374 $(foreach package,$(ALL),$(eval $(call target_spec,$(package))))
375
376 ### module extraction
377 define target_extract_module
378 MODULES/$(1):
379         @(echo -n "XXXXXXXXXXXXXXX -- BEG MODULE $(1) : $@ " ; date)
380         mkdir -p MODULES
381         cd MODULES && \
382         $(if $($(1)-SVNPATH),\
383           svn export $($(1)-SVNPATH) $(1),\
384           mkdir $(1) ; (git archive --remote=$($(1).gitrepo) $($(1).gittag) | tar -C $(1) -xf - ) \
385            || { rm -rf $(1); false; } )
386         @(echo -n "XXXXXXXXXXXXXXX -- END MODULE $(1) : $@ " ; date)
387
388 $(1)-module: MODULES/$(1)
389 .PHONY: $(1)-module
390 endef
391
392 $(foreach module,$(ALL.modules),$(eval $(call target_extract_module,$(module))))
393
394 ### the tests area
395 # use this makefile to extract tests rather than extracting manually in vbuild-nightly
396 tests-clean:
397         rm -rf MODULES/tests
398 .PHONY: tests-clean
399
400 ###
401 # Base rpmbuild in the current directory
402 # issues on fedora 8 : see the following posts
403 # http://forums.fedoraforum.org/showthread.php?t=39625 - and more specifically post#6
404 # https://www.redhat.com/archives/fedora-devel-list/2007-November/msg00171.html
405 REALROOT=/build
406 FAKEROOT=/longbuildroot
407 PWD=$(shell /bin/pwd)
408 ifeq "$(PWD)" "$(REALROOT)"
409 export HOME := $(FAKEROOT)
410 else
411 export HOME := $(PWD)
412 endif
413 .rpmmacros:
414 ifeq "$(shell pwd)" "/build"
415         rm -f $(FAKEROOT) ; ln -s $(REALROOT) $(FAKEROOT)
416 endif
417         rm -f $@ 
418         echo "%_topdir $(HOME)" >> $@
419         echo "%_tmppath $(HOME)/tmp" >> $@
420         echo "%__spec_install_pre %{___build_pre}" >> $@
421         ./getrpmmacros.sh >> $@
422
423 ### this utility allows to extract various info from a spec file
424 ### and to define them in makefiles
425 spec2make: spec2make.c
426         $(CC) -g -Wall $< -o $@ -lrpm -lrpmbuild -lrpmio -lpopt
427
428 ### run spec2make on the spec file and include the result
429 # usage: spec2make package
430 define target_mk
431 MAKE/$(1).mk: $($(1).specpath) spec2make .rpmmacros
432         mkdir -p MAKE
433         ./spec2make $($(1)-RPMFLAGS) $($(1).specpath) $(1) > MAKE/$(1).mk || { rm MAKE/$(1).mk; exit 1; }
434 endef
435
436 $(foreach package,$(ALL),$(eval $(call target_mk,$(package))))
437
438 # stores PLDISTRO in a file
439 # this is done at stage1. later run wont get confused
440 SAVED_VARS=PLDISTRO PLDISTROTAGS build-SVNPATH PERSONALITY MAILTO BASE WEBPATH TESTBUILDURL WEBROOT
441 savedpldistro.mk:
442         @echo "# do not edit" > $@
443         @$(foreach var,$(SAVED_VARS),echo "$(var):=$($(var))" >> $@ ;)
444         @echo "# do not edit" > aliases
445         @echo -n "alias m=\"make " >> aliases
446         @$(foreach var,$(SAVED_VARS),echo -n " $(var)=$($(var))" >> aliases ;)
447         @echo "\"" >> aliases
448         @echo "alias m1=\"m stage1=true\"" >> aliases
449
450 savedpldistro: savedpldistro.mk
451 .PHONY: savedpldistro
452
453 # always refresh this
454 all: savedpldistro
455
456 #################### regular make
457
458 define stage2_variables
459 ### devel dependencies
460 $(1).rpmbuild = $(RPMBUILD) $($(1)-RPMFLAGS)
461 $(1).all-devel-rpm-paths := $(foreach rpm,$($(1)-DEPEND-DEVEL-RPMS),$($(rpm).rpm-path))
462 $(1).depend-devel-packages := $(sort $(foreach rpm,$($(1)-DEPEND-DEVEL-RPMS),$($(rpm).package)))
463 ALL-DEVEL-RPMS += $($(1)-DEPEND-DEVEL-RPMS)
464 endef
465
466 $(foreach package,$(ALL),$(eval $(call stage2_variables,$(package))))
467 ALL-DEVEL-RPMS := $(sort $(ALL-DEVEL-RPMS))
468
469
470 ### pack sources into tarballs
471 ALLTARBALLS:= $(foreach package, $(ALL), $($(package).tarballs))
472 tarballs: $(ALLTARBALLS)
473         @echo $(words $(ALLTARBALLS)) source tarballs OK
474 .PHONY: tarballs
475
476 SOURCES/%.tar.bz2: SOURCES/%
477         tar chpjf $@ -C SOURCES $*
478
479 SOURCES/%.tar.gz: SOURCES/%
480         tar chpzf $@ -C SOURCES $*
481
482 SOURCES/%.tgz: SOURCES/%
483         tar chpzf $@ -C SOURCES $*
484
485 ##
486 URLS/%: url=$(subst @colon@,:,$(subst @slash@,/,$(notdir $@)))
487 URLS/%: basename=$(notdir $(url))
488 URLS/%: 
489         echo curl $(url) -o SOURCES/$(basename)
490         touch $@
491
492 ### the directory SOURCES/<package>-<version> is made 
493 # with a (set of) copy -rl from MODULES/<module>
494 # the former is $(package.source) 
495 ALLSOURCES:=$(foreach package, $(ALL), $($(package).source))
496 # so that make does not use the rule below directly for creating the tarball files
497 .SECONDARY: $(ALLSOURCES)
498
499 sources: $(ALLSOURCES)
500         @echo $(words $(ALLSOURCES)) versioned source trees OK
501 .PHONY: sources
502
503 # argument is a package
504 # do things differently if multiple modules are mentioned (sigh..)
505 define target_copy_link_modules_sources
506 $($(1).source): $(foreach module,$($(1)-MODULES),MODULES/$(module))
507         $(if $(word 2,$($(1)-MODULES)),\
508          mkdir -p $($(1).source) && $(foreach module,$($(1)-MODULES), cp -rl MODULES/$(module) $($(1).source)/$(module);) ,\
509          mkdir -p SOURCES && cp -rl MODULES/$($(1)-MODULES) $($(1).source))
510 endef
511
512 $(foreach package,$(ALL),$(eval $(call target_copy_link_modules_sources,$(package))))
513
514 ### code extraction
515 ALLMODULES:=$(foreach module, $(ALL.modules), MODULES/$(module))
516 .SECONDARY: $(ALLMODULES)
517 modules: $(ALLMODULES)
518         @echo $(words $(ALLMODULES)) modules OK "(fetched from scm)"
519 .PHONY: modules
520
521 ### source rpms
522 ALLSRPMS:=$(foreach package,$(ALL),$($(package).srpm))
523 srpms: $(ALLSRPMS)
524         @echo $(words $(ALLSRPMS)) source rpms OK
525 .PHONY: srpms
526
527 ### these macro handles the DEPEND-DEVEL-RPMS and EXCLUDE-DEVEL-RPMS tags for a hiven package
528 # before building : rpm-install DEPEND-DEVEL-RPMS and rpm-uninstall EXCLUDE
529 define handle_devel_rpms_pre 
530         $(if $($(1).all-devel-rpm-paths), echo "Installing for $(1)-DEPEND-DEVEL-RPMS" ; $(RPM-INSTALL-DEVEL) $($(1).all-devel-rpm-paths)) 
531         $(if $($(1)-EXCLUDE-DEVEL-RPMS), echo "Uninstalling for $(1)-EXCLUDE-DEVEL-RPMS" ; $(RPM-UNINSTALL-DEVEL) $($(1)-EXCLUDE-DEVEL-RPMS))
532 endef
533
534 define handle_devel_rpms_post
535         -$(if $($(1)-DEPEND-DEVEL-RPMS), echo "Unstalling for $(1)-DEPEND-DEVEL-RPMS" ; $(RPM-UNINSTALL-DEVEL) $($(1)-DEPEND-DEVEL-RPMS))
536         $(if $($(1)-EXCLUDE-DEVEL-RPMS), "Reinstalling for $(1)-EXCLUDE-DEVEL-RPMS" ; $(YUM-INSTALL-DEVEL) $($(1)-EXCLUDE-DEVEL-RPMS) )
537 endef
538
539 # usage: target_source_rpm package
540 define target_source_rpm 
541 ifeq "$($(1)-BUILD-FROM-SRPM)" ""
542 $($(1).srpm): $($(1).specpath) .rpmmacros $($(1).tarballs) 
543         mkdir -p BUILD SRPMS tmp
544         @(echo -n "XXXXXXXXXXXXXXX -- BEG SRPM $(1) (using SOURCES) " ; date)
545         $(call handle_devel_rpms_pre,$(1))
546         $($(1).rpmbuild) -bs $($(1).specpath)
547         $(call handle_devel_rpms_post,$(1))
548         @(echo -n "XXXXXXXXXXXXXXX -- END SRPM $(1) " ; date)
549 else
550 $($(1).srpm): $($(1).specpath) .rpmmacros $($(1).source)
551         mkdir -p BUILD SRPMS tmp
552         @(echo -n "XXXXXXXXXXXXXXX -- BEG SRPM $(1) (using make srpm) " ; date)
553         $(call handle_devel_rpms_pre,$(1))
554         make -C $($(1).source) srpm SPECFILE=$(HOME)/$($(1).specpath) EXPECTED_SRPM=$(notdir $($(1).srpm)) && \
555            rm -f SRPMS/$(notdir $($(1).srpm)) && \
556            ln $($(1).source)/$(notdir $($(1).srpm)) SRPMS/$(notdir $($(1).srpm)) 
557         $(call handle_devel_rpms_post,$(1))
558         @(echo -n "XXXXXXXXXXXXXXX -- END SRPM $(1) " ; date)
559 endif
560 endef
561
562 $(foreach package,$(ALL),$(eval $(call target_source_rpm,$(package))))
563
564 ### binary rpms are made from source rpm
565 ALLRPMS:=$(foreach package,$(ALL),$($(package).rpms))
566 # same as above, mention $(ALL) and not $(ALLRPMS)
567 rpms: $(ALLRPMS)
568         @echo $(words $(ALLRPMS)) binary rpms OK
569 .PHONY: rpms
570
571 # use tmp dirs when building binary rpm so make remains idempotent 
572 # otherwise SOURCES/ or SPEC gets touched again - which leads to rebuilding
573 RPM-USE-TMP-DIRS = --define "_sourcedir $(HOME)/tmp" --define "_specdir $(HOME)/tmp"
574 RPM-USE-COMPILE-DIRS = --define "_sourcedir $(HOME)/COMPILE" --define "_specdir $(HOME)/COMPILE"
575
576 # usage: build_binary_rpm package
577 # xxx hacky - invoke createrepo if DEPEND-FILES mentions RPMS/yumgroups.xml
578 define target_binary_rpm 
579 $($(1).rpms): $($(1).srpm)
580         mkdir -p RPMS tmp
581         @(echo -n "XXXXXXXXXXXXXXX -- BEG RPM $(1) " ; date)
582         $(if $(findstring RPMS/yumgroups.xml,$($(1)-DEPEND-FILES)), $(createrepo) , )
583         $(call handle_devel_rpms_pre,$(1))
584         $($(1).rpmbuild) --rebuild $(RPM-USE-TMP-DIRS) $($(1).srpm)
585         $(call handle_devel_rpms_post,$(1))
586         @(echo -n "XXXXXXXXXXXXXXX -- END RPM $(1) " ; date)
587 # for manual use only - in case we need to investigate the results of an rpmbuild
588 $(1)-compile: $($(1).srpm)
589         mkdir -p COMPILE tmp
590         @(echo -n "XXXXXXXXXXXXXXX -- BEG compile $(1) " ; date)
591         $(if $(findstring RPMS/yumgroups.xml,$($(1)-DEPEND-FILES)), $(createrepo) , )
592         $(call handle_devel_rpms_pre,$(1))
593         $($(1).rpmbuild) --recompile $(RPM-USE-TMP-DIRS) $($(1).srpm)
594         $(call handle_devel_rpms_post,$(1))
595         @(echo -n "XXXXXXXXXXXXXXX -- END compile $(1) " ; date)
596 .PHONY: $(1)-compile
597 endef
598
599 $(foreach package,$(ALL),$(eval $(call target_binary_rpm,$(package))))
600 ### shorthand target
601 # e.g. make proper -> does propers rpms
602 # usage shorthand_target package
603 define target_shorthand 
604 $(1): $($(1).rpms)
605 .PHONY: $(1)
606 $(1)-spec: $($(1)-SPEC)
607 .PHONY: $(1)-spec
608 $(1)-mk: $($(1)-MK)
609 .PHONY: $(1)-mk
610 $(1)-tarball: $($(1).tarballs)
611 .PHONY: $(1)-tarball
612 .PHONY: $(1)-source
613 $(1)-source: $($(1).source)
614 $(1)-rpms: $($(1).rpms)
615 .PHONY: $(1)-rpms
616 $(1)-srpm: $($(1).srpm)
617 .PHONY: $(1)-srpm
618 endef
619
620 $(foreach package,$(ALL),$(eval $(call target_shorthand,$(package))))
621
622 ### file dependencies
623 define package_depends_on_file
624 $(1):$(2)
625 $($(1).srpm):$(2)
626 endef
627
628 define target_dependfiles
629 $(foreach file,$($(1)-DEPEND-FILES),$(eval $(call package_depends_on_file,$(1),$(file))))
630 endef
631
632 $(foreach package,$(ALL),$(eval $(call target_dependfiles,$(package))))
633
634 ### package dependencies
635 define package_depends_on_package
636 $(1):$(2)
637 $(1):$($(2).rpms)
638 $($(1).srpm):$($(2).rpms)
639 endef
640
641 define target_depends
642 $(foreach package,$($(1)-DEPEND-PACKAGES) $($(1).depend-devel-packages),$(eval $(call package_depends_on_package,$(1),$(package))))
643 endef
644
645 $(foreach package,$(ALL),$(eval $(call target_depends,$(package))))
646
647 ### clean target
648 # usage: target_clean package
649 define target_clean
650 $(1)-clean-modules:
651         $(foreach module,$($(1)-MODULES),rm -rf MODULES/$(module);)
652 .PHONY: $(1)-clean-modules
653 CLEANS += $(1)-clean-modules
654 $(1)-clean-source:
655         rm -rf $($(1).source)
656 .PHONY: $(1)-clean-source
657 CLEANS += $(1)-clean-source
658 $(1)-clean-tarball:
659         rm -rf $($(1).tarballs)
660 .PHONY: $(1)-clean-tarball
661 CLEANS += $(1)-clean-tarball
662 $(1)-clean-build:
663         rm -rf BUILD/$(notdir $($(1).source))
664 CLEANS += $(1)-clean-build
665 $(1)-clean-rpms:
666         rm -rf $($(1).rpms)
667 .PHONY: $(1)-clean-rpms
668 CLEANS += $(1)-clean-rpms
669 $(1)-clean-srpm:
670         rm -rf $($(1).srpm)
671 .PHONY: $(1)-clean-srpm
672 CLEANS += $(1)-clean-srpm
673 $(1)-codeclean: $(1)-clean-source $(1)-clean-tarball $(1)-clean-build $(1)-clean-rpms $(1)-clean-srpm
674 $(1)-clean: $(1)-clean-modules $(1)-codeclean
675 .PHONY: $(1)-codeclean $(1)-clean 
676 $(1)-clean-spec:
677         rm -rf $($(1).specpath)
678 .PHONY: $(1)-clean-spec
679 $(1)-clean-make:
680         rm -rf MAKE/$(1).mk
681 .PHONY: $(1)-clean-make
682 $(1)-distclean: $(1)-distclean1 $(1)-distclean2
683 $(1)-distclean1: $(1)-clean-spec $(1)-clean-make
684 $(1)-distclean2: $(1)-clean
685 .PHONY: $(1)-distclean $(1)-distclean1 $(1)-distclean2
686 endef
687
688 $(foreach package,$(ALL),$(eval $(call target_clean,$(package))))
689
690 ### clean precisely
691 clean:
692         $(MAKE) $(CLEANS)
693 .PHONY: clean
694
695 clean-help:
696         @echo Available clean targets
697         @echo $(CLEANS)
698
699 ### brute force clean
700 distclean1:
701         rm -rf savedpldistro.mk .rpmmacros spec2make header.spec SPECS MAKE $(DISTCLEANS)
702 distclean2:
703         rm -rf MODULES SOURCES BUILD BUILDROOT RPMS SRPMS tmp
704 distclean: distclean1 distclean2
705 .PHONY: distclean1 distclean2 distclean
706
707 develclean:
708         -$(RPM-UNINSTALL-DEVEL) $(ALL-DEVEL-RPMS)
709
710 ####################
711 # gather build information for the 'About' page
712 # when run from crontab, INIT_CWD not properly set (says /root ..)
713 # so, the nightly build passes BASE here
714 # also store BASE in .base for any post-processing purposes
715 myplc-release:
716         @echo 'Creating myplc-release'
717         rm -f $@
718         echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx build info" >> $@
719         $(MAKE) --no-print-directory version-build >> $@
720         echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx svn info" >> $@
721         $(MAKE) --no-print-directory version-tags >> $@
722         echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx rpm info" >> $@
723         $(MAKE) --no-print-directory version-rpms >> $@
724         @echo $(BASE) > .base
725
726 version-build:
727         @echo -n 'Build build-date: ' ; date '+%Y.%m.%d'
728         @echo -n 'Build build-time: ' ; date '+%H:%M-%Z'
729         @echo -n 'Build build-hostname: ' ; hostname
730         @echo    "Build build-base: $(BASE)"
731         @echo    "Build planetlab-distro: $(PLDISTRO)"
732         @echo    "Build planetlab-tags: $(PLDISTROTAGS)"
733         @echo    "Build target-arch: $(HOSTARCH)"
734         @echo    "Build target-distro: $(DISTRO)"
735         @echo    "Build target-distroname: $(DISTRONAME)"
736         @echo    "Build target-release: $(RELEASE)"     
737         @echo    "Build target-personality: $(PERSONALITY)"     
738
739 #################### 
740 # for a given module
741 VFORMAT="%30s := %s\n"
742 define print_tag_target
743 $(1)-version-tag:
744         @$(if $($(1)-SVNPATH),\
745            printf $(VFORMAT) $(1)-SVNPATH "$($(1)-SVNPATH)",\
746            printf $(VFORMAT) $(1)-GITPATH "$($(1)-GITPATH)" )
747 endef
748
749 $(foreach module,$(ALL.modules), $(eval $(call print_tag_target,$(module))))
750
751 version-tags: $(foreach module, $(ALL.modules), $(module)-version-tag)
752
753 RFORMAT="%20s :: version=%s release=%s\n"
754 define rpm_version_target
755 $(1)-version-rpm:
756         @printf $(RFORMAT) $($(1).rpm-name) $($(1).rpm-version) $($(1).rpm-release)
757 version-rpms: $(1)-version-rpm
758 endef
759
760 $(foreach package,$(sort $(ALL)), $(eval $(call rpm_version_target,$(package))))
761
762 versions: myplc-release version-build version-tags version-rpms
763 .PHONY: versions version-build version-rpms version-tags
764
765 #################### package info
766 PKGKEYS := tarballs source srpm rpms rpmnames rpm-release rpm-name rpm-version rpm-subversion
767 %-pkginfo: package=$(subst -pkginfo,,$@)
768 %-pkginfo: 
769         @$(foreach key,$(PKGKEYS),echo "$(package).$(key)=$($(package).$(key))";)
770 ## rpm info
771 RPMKEYS := rpm-path package
772 %-rpminfo: rpm=$(subst -rpminfo,,$@)
773 %-rpminfo: 
774         @$(foreach key,$(RPMKEYS),echo "$(rpm).$(key)=$($(rpm).$(key))";)
775
776 #################### various lists - designed to run with stage1=true
777 info-packages:
778         @$(foreach package,$(ALL), echo package=$(package) ref_module=$($(package).module) modules=$($(package)-MODULES) rpmnames=$($(package).rpmnames); )
779
780 info-modules:
781         @$(foreach module,$(ALL.modules), echo module=$(module) \
782            $(if $($(module)-SVNPATH),svnpath=$($(module)-SVNPATH),gitpath=$($(module)-GITPATH)) ; )
783
784 info-branches:
785         @$(foreach module,$(ALL.modules), \
786           $(if $($(module)-BRANCH),echo module=$(module) branch=$($(module)-BRANCH);))
787
788 module-tools:
789         @$(foreach module,$(ALL.modules), \
790          $(if $($(module)-GITPATH), \
791           $(if $($(module)-BRANCH), \
792              echo $(module):$($(module)-BRANCH); , \
793              echo $(module); ), \
794           $(if $($(module)-BRANCH), \
795              echo $(module):$($(module)-BRANCH); , \
796              echo $(module); )))
797
798 info: info-packages info-modules info-branches 
799
800 .PHONY: info info-packages info-modules info-branches module-tools
801
802 show-multi-modules:
803         @$(foreach package,$(ALL),$(if $(subst x1,,x$(words $($(package)-MODULES))), echo p=$(package) mods=$($(package)-MODULES) ;))
804 .PHONY: show-multi-modules
805 ####################
806 help:
807         @echo "********** Run make in two stages:"
808         @echo ""
809         @echo "make stage1=true PLDISTRO=onelab"
810         @echo " -> extracts all spec files in SPECS/ and mk files in MAKE/"
811         @echo "    as well as save PLDISTRO for subsequent runs"
812         @echo ""
813         @echo "********** Then you can use the following targets"
814         @echo 'make'
815         @echo "  rebuilds everything"
816         @echo 'make util-vserver'
817         @echo "  makes the RPMS related to util-vserver"
818         @echo "  equivalent to 'make util-vserver-rpms'"
819         @echo ""
820         @echo "********** Or, vertically - step-by-step for a given package"
821         @echo 'make util-vserver-source'
822         @echo "  creates source link in SOURCES/util-vserver-<version>"
823         @echo 'make util-vserver-tarball'
824         @echo "  creates source tarball in SOURCES/util-vserver-<version>.<tarextension>"
825         @echo 'make util-vserver-srpm'
826         @echo "  build source rpm in SRPMS/"
827         @echo 'make util-vserver-rpms'
828         @echo "  build rpm(s) in RPMS/"
829         @echo ""
830         @echo "********** Or, horizontally, reach a step for all known packages"
831         @echo 'make sources'
832         @echo 'make tarballs'
833         @echo 'make srpms'
834         @echo 'make rpms'
835         @echo ""
836         @echo "********** Manual targets"
837         @echo "make package-compile"
838         @echo "  The regular process uses rpmbuild --rebuild, that performs"
839         @echo "  a compilation directory cleanup upon completion. If you need to investigate"
840         @echo "  the intermediate compilation directory, use the -compile targets"
841         @echo "********** Cleaning examples"
842         @echo "make clean"
843         @echo "  removes the files made by make"
844         @echo "make distclean"
845         @echo "  brute-force cleaning, removes entire directories - requires a new stage1"
846         @echo "make develclean"
847         @echo "  rpm-uninstalls all devel packages installed during build"
848         @echo ""
849         @echo "make iptables-distclean"
850         @echo "  deep clean for a given package"
851         @echo "make iptables-codeclean"
852         @echo "  run this if you've made a local/manual change in the MODULES area for iptables"
853         @echo ""
854         @echo "make util-vserver-clean"
855         @echo "  removes codebase, source, tarball, build, rpm and srpm for util-vserver"
856         @echo "make util-vserver-clean-modules"
857         @echo "  and so on for source, tarball, build, rpm and srpm"
858         @echo ""
859         @echo "********** Info examples"
860         @echo "make ++ALL"
861         @echo "  Displays the value of a given variable (here ALL)"
862         @echo "  with only a single plus sign only the value is displayed"
863         @echo "make info"
864         @echo "  is equivalent to make packages modules branches"
865         @echo "  provides various info on these objects"
866         @echo "make ulogd-pkginfo"
867         @echo "  Displays know attributes of a package"
868         @echo "make kernel-devel-rpminfo"
869         @echo "  Displays know attributes of an rpm"
870         @echo "make stage1=true PLDISTROTAGS=planetlab-tags-4.2.mk packages modules branches module-tools"
871         @echo "  Lists mentioned items - module-tools is used in modules.update"
872         @echo ""
873         @echo "********** Known pakages are"
874         @echo "$(ALL)"
875
876 #################### convenience, for debugging only
877 # make +foo : prints the value of $(foo)
878 # make ++foo : idem but verbose, i.e. foo=$(foo)
879 ++%: varname=$(subst +,,$@)
880 ++%:
881         @echo "$(varname)=$($(varname))"
882 +%: varname=$(subst +,,$@)
883 +%:
884         @echo "$($(varname))"