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