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