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