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