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