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