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