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