lbuild vms now rely on NetworkManager
[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 ###
334 # note for fedora23 (and onwards, probably)
335 # many rpms wouldn't build as-is and issue an weird error
336 ##
337 # Processing files: pl_sshd-debuginfo-1.0-11.lxc.x86_64
338 # error: Empty %files file /longbuildroot/BUILD/pl_sshd-1.0/debugfiles.list
339 ##
340 # so to work around that we define debug_package as being nil
341 # only for f23 for now, let's see what this gives us..
342 header.spec:
343         (echo -n "# Generated by planetlab build on " ; date) > $@
344         echo "%define distro $(DISTRO)" >> $@
345         echo "%define distrorelease $(RELEASE)" >> $@
346         echo "%define distroname $(DISTRONAME)" >> $@
347         echo "%define pldistro $(PLDISTRO)" >> $@
348         echo "%define plrelease $(PLANETLAB_RELEASE)" >> $@
349         echo "# use MD5 and gzip for binary and source files" >> $@
350         echo "%global _binary_filedigest_algorithm 1" >> $@
351         echo "%global _source_filedigest_algorithm 1" >> $@
352         echo "%global _source_payload       w9.gzdio" >> $@
353         echo "%global _binary_payload       w9.gzdio" >> $@
354 ifeq "$(DISTRONAME)""$(filter $(DISTRONAME),f25 f27 f29)"
355         echo "%define debug_package %{nil}" >> $@
356 endif
357
358 ### make up spec file - extract module first
359 define target_spec
360 $($(1).specpath): header.spec $($(1).codespec)
361         mkdir -p SPECS
362         cat header.spec > $($(1).specpath)
363         echo "%define SCMURL $($($(1).module)-SCMPATH)" >> $($(1).specpath)
364         $(if $($(1).has-date),echo "%define date $(shell date +%Y.%m.%d)" >> $($(1).specpath),)
365         $(if $($(1)-SPECVARS), \
366           $(foreach line,$($(1)-SPECVARS), \
367             echo "%define" $(word 1,$(subst =, ,$(line))) "$(word 2,$(subst =, ,$(line)))" >> $($(1).specpath) ;))
368         echo "# included from $($(1)-SPEC)" >> $($(1).specpath)
369         cat $($(1).codespec) >> $($(1).specpath)
370
371 $($(1).codespec): $($(1).moduledir)
372
373 endef
374
375 $(foreach package,$(ALL),$(eval $(call target_spec,$(package))))
376
377 ####################
378 # fetch modules
379 # expected input:
380 #   $(1) is the name of a git module
381 #     $($(1).gitrepo) and $(($1).gittag) are used to determine branch or tag name
382 # expected result
383 #   $(1)/ in the cwd will hold the result
384
385 # fetch svn module
386 define fetch_svn_module
387         svn export $($(1)-SVNPATH) $(1)
388 endef
389
390 # git_fetch_module
391 # as far as possible we use git-archive to fetch a git module
392 # however if that fails we fallback to a git-clone strategy,
393 # for when git-archive is not supported, e.g. for github
394 # side effect
395 #   $(1)/ gets cleaned up if job cannot be done
396 define fetch_git_module
397         mkdir $(1) ; \
398         (git archive --remote=$($(1).gitrepo) $($(1).gittag) | tar -C $(1) -xf - ) || \
399         (echo "==================== git archive FAILED, trying git clone instead" ; \
400          git clone $($(1).gitrepo) $(1); cd $(1) ; git checkout $($(1).gittag) ; rm -rf .git ) || \
401         { rm -rf $(1); false; }
402 endef
403
404 ### module extraction
405 define target_extract_module
406 MODULES/$(1):
407         @(echo -n "XXXXXXXXXXXXXXX -- BEG MODULE $(1) : $@ " ; date)
408         mkdir -p MODULES
409         cd MODULES && \
410         $(if $($(1)-SVNPATH),$(call fetch_svn_module,$(1)),$(call fetch_git_module,$(1)) )
411         @(echo -n "XXXXXXXXXXXXXXX -- END MODULE $(1) : $@ " ; date)
412
413 $(1)-module: MODULES/$(1)
414 .PHONY: $(1)-module
415 endef
416
417 $(foreach module,$(ALL.modules),$(eval $(call target_extract_module,$(module))))
418
419 ### the tests area
420 build-clean:
421         rm -rf MODULES/build
422 .PHONY: build-clean
423
424 tests-clean:
425         rm -rf MODULES/tests
426 .PHONY: tests-clean
427
428 ###
429 # Base rpmbuild in the current directory
430 # issues on fedora 8 : see the following posts
431 # http://forums.fedoraforum.org/showthread.php?t=39625 - and more specifically post#6
432 # https://www.redhat.com/archives/fedora-devel-list/2007-November/msg00171.html
433 REALROOT=/build
434 FAKEROOT=/longbuildroot
435 PWD=$(shell /bin/pwd)
436 ifeq "$(PWD)" "$(REALROOT)"
437 export HOME := $(FAKEROOT)
438 else
439 export HOME := $(PWD)
440 endif
441
442 # rpm macros
443 # build.common (getrpmmacros.sh) defines some utilities we want in place on our images
444 # in addition to these we set a few others for the build
445 .rpmmacros:
446 ifeq "$(shell pwd)" "/build"
447         rm -f $(FAKEROOT) ; ln -s $(REALROOT) $(FAKEROOT)
448 endif
449         rm -f $@
450         echo "%_topdir $(HOME)" >> $@
451         echo "%_tmppath $(HOME)/tmp" >> $@
452         echo "%__spec_install_pre %{___build_pre}" >> $@
453         ./getrpmmacros.sh >> $@
454
455 ### this utility allows to extract various info from a spec file
456 ### and to define them in makefiles
457 # use the C code where it works as it's the original one, use the python code otherwise
458 spec2make: spec2make.c
459         $(CC) -g -Wall $< -o $@ -lrpm -lrpmbuild -lrpmio -lpopt || ln -s spec2make.py $@
460
461 ### run spec2make on the spec file and include the result
462 # usage: spec2make package
463 # with old rpms (f8&c5) and too recent specfiles (the kernel), we need a patch to spec2make
464 # so when <package-NEEDSPECK2MAKEHACK is set, we run spec2make with the --hack flag
465 define target_mk
466 MAKE/$(1).mk: $($(1).specpath) spec2make .rpmmacros
467         mkdir -p MAKE
468         ./spec2make $($(1).spec2makeflags) $($(1)-RPMFLAGS) $($(1).specpath) $(1) > MAKE/$(1).mk || { rm MAKE/$(1).mk; exit 1; }
469 endef
470
471 $(foreach package,$(ALL),$(eval $(call target_mk,$(package))))
472
473 # stores env variables in a file
474 # this is done at stage1. later run won't get confused
475 STATIC_VARS=PLDISTRO PLDISTROTAGS build-GITPATH PERSONALITY MAILTO BASE WEBPATH TESTBUILDURL WEBROOT
476 # find out names for variables set on the command line
477 define assigned_varname
478 $(if $(findstring =,$(1)),$(firstword $(subst =, ,$(1))) )
479 endef
480 ASSIGNED=$(filter-out stage1 stage1iter,$(foreach flag,$(MAKEFLAGS),$(call assigned_varname,$(flag))))
481 SAVED_VARS=$(sort $(STATIC_VARS) $(ASSIGNED))
482 envfrompreviousrun.mk:
483         @echo "# do not edit" > $@
484         @$(foreach var,$(SAVED_VARS),echo "$(var):=$($(var))" >> $@ ;)
485         @echo "# do not edit" > aliases
486         @echo -n "alias m=\"make " >> aliases
487         @$(foreach var,$(SAVED_VARS),echo -n " $(var)=$($(var))" >> aliases ;)
488         @echo "\"" >> aliases
489         @echo "alias m1=\"m stage1=true\"" >> aliases
490
491 envfrompreviousrun: envfrompreviousrun.mk
492 .PHONY: envfrompreviousrun
493
494 # always refresh this
495 all: envfrompreviousrun
496
497 #################### regular make
498
499 define stage2_variables
500 ### devel dependencies
501 $(1).rpmbuild = $(RPMBUILD) $($(1)-RPMFLAGS)
502 $(1).all-local-devel-rpm-paths := $(foreach rpm,$($(1)-LOCAL-DEVEL-RPMS) $($(1)-LOCAL-DEVEL-RPMS-CRUCIAL),$($(rpm).rpm-path))
503 $(1).depend-devel-packages := $(sort $(foreach rpm,$($(1)-LOCAL-DEVEL-RPMS),$($(rpm).package)))
504 ALL-STOCK-DEVEL-RPMS += $($(1)-LOCAL-DEVEL-RPMS)
505 endef
506
507 $(foreach package,$(ALL),$(eval $(call stage2_variables,$(package))))
508 ALL-STOCK-DEVEL-RPMS := $(sort $(ALL-STOCK-DEVEL-RPMS))
509
510
511 ### pack sources into tarballs
512 ALLTARBALLS:= $(foreach package, $(ALL), $($(package).tarballs))
513 tarballs: $(ALLTARBALLS)
514         @echo $(words $(ALLTARBALLS)) source tarballs OK
515 .PHONY: tarballs
516
517 SOURCES/%.tar.bz2: SOURCES/%
518         tar chpjf $@ -C SOURCES $*
519
520 SOURCES/%.tar.gz: SOURCES/%
521         tar chpzf $@ -C SOURCES $*
522
523 SOURCES/%.tgz: SOURCES/%
524         tar chpzf $@ -C SOURCES $*
525
526 ##
527 URLS/%: url=$(subst @colon@,:,$(subst @slash@,/,$(notdir $@)))
528 URLS/%: basename=$(notdir $(url))
529 URLS/%:
530         echo curl $(url) -o SOURCES/$(basename)
531         touch $@
532
533 ### the directory SOURCES/<package>-<version> is made
534 # with a (set of) copy -rl from MODULES/<module>
535 # the former is $(package.source)
536 ALLSOURCES:=$(foreach package, $(ALL), $($(package).source))
537 # so that make does not use the rule below directly for creating the tarball files
538 .SECONDARY: $(ALLSOURCES)
539
540 sources: $(ALLSOURCES)
541         @echo $(words $(ALLSOURCES)) versioned source trees OK
542 .PHONY: sources
543
544 # argument is a package
545 # do things differently if multiple modules are mentioned (sigh..)
546 define target_copy_link_modules_sources
547 $($(1).source): $(foreach module,$($(1)-MODULES),MODULES/$(module))
548         $(if $(word 2,$($(1)-MODULES)),\
549          mkdir -p $($(1).source) && $(foreach module,$($(1)-MODULES), cp -rl MODULES/$(module) $($(1).source)/$(module);) ,\
550          mkdir -p SOURCES && cp -rl MODULES/$($(1)-MODULES) $($(1).source))
551 endef
552
553 $(foreach package,$(ALL),$(eval $(call target_copy_link_modules_sources,$(package))))
554
555 ### code extraction
556 ALLMODULES:=$(foreach module, $(ALL.modules), MODULES/$(module))
557 .SECONDARY: $(ALLMODULES)
558 modules: $(ALLMODULES)
559         @echo $(words $(ALLMODULES)) modules OK "(fetched from scm)"
560 .PHONY: modules
561
562 ### source rpms
563 ALLSRPMS:=$(foreach package,$(ALL),$($(package).srpm))
564 srpms: $(ALLSRPMS)
565         @echo $(words $(ALLSRPMS)) source rpms OK
566 .PHONY: srpms
567
568 #################### manage build requirements
569 # default values
570 RPMYUM-INSTALL-LOCAL := rpm --force -Uvh
571 RPMYUM-INSTALL-STOCK := yum -y install
572 # uninstall -- cannot force rpm -e
573 # need to ignore result, kernel-headers cannot be uninstalled as glibc depends on it
574 RPMYUM-UNINSTALL-STOCK := rpm -e
575
576 ### these macro handles the LOCAL-DEVEL-RPMS and LOCAL-DEVEL-RPMS-CRUCIAL tags for a given package
577 # before building : rpm-install LOCAL-DEVEL-RPMS
578 define rpmyum_install_local_rpms
579         $(if $($(1).all-local-devel-rpm-paths), echo "Installing for $(1)-LOCAL-DEVEL-RPMS" ; $(RPMYUM-INSTALL-LOCAL) $($(1).all-local-devel-rpm-paths))
580 endef
581
582 # install stock rpms if defined
583 define rpmyum_install_stock_rpms
584         $(if $($(1)-STOCK-DEVEL-RPMS), echo "Installing for $(1)-STOCK-DEVEL-RPMS" ; $(RPMYUM-INSTALL-STOCK) $($(1)-STOCK-DEVEL-RPMS) || true)
585 endef
586
587 define rpmyum_uninstall_stock_rpms
588         -$(if $($(1)-STOCK-DEVEL-RPMS), echo "Unstalling for $(1)-STOCK-DEVEL-RPMS" ; $(RPMYUM-UNINSTALL-STOCK) $($(1)-STOCK-DEVEL-RPMS) || true)
589 endef
590
591 # similar for debians
592 # gdebi acts like yum localinstall; gdebi-core should be mentioned in develdeb.pkgs
593 DPKGAPT-INSTALL-LOCAL := gdebi
594 DPKGAPT-INSTALL-STOCK := apt-get -y install
595 DPKGAPT-UNINSTALL-STOCK := echo WARNING uninstalling stock debs not implemented
596
597 define dpkgapt_install_local_debs
598         $(if $($(1)-LOCAL-DEVEL-DEBS), echo "Installing for $(1)-LOCAL-DEVEL-DEBS" ; $(foreach debname,$($(1)-LOCAL-DEVEL-DEBS),$(DPKGAPT-INSTALL-LOCAL) $(wildcard DEBIAN/$(debname)_*.deb);))
599 endef
600
601 define dpkgapt_install_stock_debs
602         $(if $($(1)-STOCK-DEVEL-DEBS), echo "Installing for $(1)-STOCK-DEVEL-DEBS" ; $(DPKGAPT-INSTALL-STOCK) $($(1)-STOCK-DEVEL-DEBS) || true)
603 endef
604
605 define dpkgapt_uninstall_stock_debs
606         -$(if $($(1)-STOCK-DEVEL-DEBS), echo "Unstalling for $(1)-STOCK-DEVEL-DEBS" ; $(DPKGAPT-UNINSTALL-STOCK) $($(1)-STOCK-DEVEL-DEBS) || true)
607 endef
608
609
610 ####################
611 # usage: target_source_rpm package
612 define target_source_rpm
613 ifeq "$($(1)-BUILD-FROM-SRPM)" ""
614 $($(1).srpm): $($(1).specpath) .rpmmacros $($(1).tarballs)
615         mkdir -p BUILD SRPMS tmp
616         @(echo -n "XXXXXXXXXXXXXXX -- BEG SRPM $(1) (using SOURCES) " ; date)
617         $(call rpmyum_install_stock_rpms,$(1))
618         $(call rpmyum_install_local_rpms,$(1))
619         $($(1).rpmbuild) -bs $($(1).specpath)
620         $(call rpmyum_uninstall_stock_rpms,$(1))
621         @(echo -n "XXXXXXXXXXXXXXX -- END SRPM $(1) " ; date)
622 else
623 $($(1).srpm): $($(1).specpath) .rpmmacros $($(1).source)
624         mkdir -p BUILD SRPMS tmp
625         @(echo -n "XXXXXXXXXXXXXXX -- BEG SRPM $(1) (using make srpm) " ; date)
626         $(call rpmyum_install_stock_rpms,$(1))
627         $(call rpmyum_install_local_rpms,$(1))
628         make -C $($(1).source) srpm SPECFILE=$(HOME)/$($(1).specpath) EXPECTED_SRPM=$(notdir $($(1).srpm)) && \
629            rm -f SRPMS/$(notdir $($(1).srpm)) && \
630            ln $($(1).source)/$(notdir $($(1).srpm)) SRPMS/$(notdir $($(1).srpm))
631         $(call rpmyum_uninstall_stock_rpms,$(1))
632         @(echo -n "XXXXXXXXXXXXXXX -- END SRPM $(1) " ; date)
633 endif
634 endef
635
636 $(foreach package,$(ALL),$(eval $(call target_source_rpm,$(package))))
637
638 ### binary rpms are made from source rpm
639 ALLRPMS:=$(foreach package,$(ALL),$($(package).rpms))
640 # same as above, mention $(ALL) and not $(ALLRPMS)
641 rpms: $(ALLRPMS)
642         @echo $(words $(ALLRPMS)) binary rpms OK
643 .PHONY: rpms
644
645 # use tmp dirs when building binary rpm so make remains idempotent
646 # otherwise SOURCES/ or SPEC gets touched again - which leads to rebuilding
647 RPM-USE-TMP-DIRS = --define "_sourcedir $(HOME)/tmp" --define "_specdir $(HOME)/tmp"
648 RPM-USE-COMPILE-DIRS = --define "_sourcedir $(HOME)/COMPILE" --define "_specdir $(HOME)/COMPILE"
649
650 # usage: build_binary_rpm package
651 # xxx hacky - invoke createrepo if DEPEND-FILES mentions RPMS/yumgroups.xml
652 define target_binary_rpm
653 $($(1).rpms): $($(1).srpm)
654         mkdir -p RPMS tmp
655         @(echo -n "XXXXXXXXXXXXXXX -- BEG RPM $(1) " ; date)
656         $(if $(findstring RPMS/yumgroups.xml,$($(1)-DEPEND-FILES)), $(createrepo) , )
657         $(call rpmyum_install_stock_rpms,$(1))
658         $(call rpmyum_install_local_rpms,$(1))
659         $($(1).rpmbuild) --rebuild $(RPM-USE-TMP-DIRS) $($(1).srpm)
660         $(call rpmyum_uninstall_stock_rpms,$(1))
661         @(echo -n "XXXXXXXXXXXXXXX -- END RPM $(1) " ; date)
662 # for manual use only - in case we need to investigate the results of an rpmbuild
663 $(1)-compile: $($(1).srpm)
664         mkdir -p COMPILE tmp
665         @(echo -n "XXXXXXXXXXXXXXX -- BEG compile $(1) " ; date)
666         $(if $(findstring RPMS/yumgroups.xml,$($(1)-DEPEND-FILES)), $(createrepo) , )
667         $(call rpmyum_install_stock_rpms,$(1))
668         $(call rpmyum_install_local_rpms,$(1))
669         $($(1).rpmbuild) --recompile $(RPM-USE-TMP-DIRS) $($(1).srpm)
670         $(call rpmyum_uninstall_stock_rpms,$(1))
671         @(echo -n "XXXXXXXXXXXXXXX -- END compile $(1) " ; date)
672 .PHONY: $(1)-compile
673 endef
674
675 $(foreach package,$(ALL),$(eval $(call target_binary_rpm,$(package))))
676 ### shorthand target
677 # e.g. make proper -> does propers rpms
678 # usage shorthand_target package
679 define target_shorthand
680 $(1): $($(1).rpms)
681 .PHONY: $(1)
682 $(1)-spec: $($(1)-SPEC)
683 .PHONY: $(1)-spec
684 $(1)-mk: $($(1)-MK)
685 .PHONY: $(1)-mk
686 $(1)-tarball: $($(1).tarballs)
687 .PHONY: $(1)-tarball
688 .PHONY: $(1)-source
689 $(1)-source: $($(1).source)
690 $(1)-rpms: $($(1).rpms)
691 .PHONY: $(1)-rpms
692 $(1)-srpm: $($(1).srpm)
693 .PHONY: $(1)-srpm
694 endef
695
696 $(foreach package,$(ALL),$(eval $(call target_shorthand,$(package))))
697
698 ### file dependencies
699 define package_depends_on_file
700 $(1):$(2)
701 $($(1).srpm):$(2)
702 endef
703
704 define target_dependfiles
705 $(foreach file,$($(1)-DEPEND-FILES),$(eval $(call package_depends_on_file,$(1),$(file))))
706 endef
707
708 $(foreach package,$(ALL),$(eval $(call target_dependfiles,$(package))))
709
710 ### package dependencies
711 define package_depends_on_package
712 $(1):$(2)
713 $(1):$($(2).rpms)
714 $($(1).srpm):$($(2).rpms)
715 endef
716
717 define target_depends
718 $(foreach package,$($(1)-DEPEND-PACKAGES) $($(1).depend-devel-packages),$(eval $(call package_depends_on_package,$(1),$(package))))
719 endef
720
721 $(foreach package,$(ALL),$(eval $(call target_depends,$(package))))
722
723 ####################
724 # debian meta-target
725 ALL-DEBIAN := $(foreach target,$(ALL),$(target)-debian)
726 debian: $(ALL-DEBIAN)
727
728 ####################
729 # very rough for now (one module per package), targets only sfa for now
730 # the general idea here is, changing the specfile (for version number and all) is enough, and this
731 # gets passed to "make debian" in the module
732 # PREFIX: at one point we had passed PREFIX=/usr to this 'make debian'
733 # however it turned out we could manage this some other way (see manifold)
734 # so I'm reverting to simplicity
735 define target_debian
736 $(1)-debian: $(1)-tarball
737         $(call dpkgapt_install_local_debs,$(1))
738         $(call dpkgapt_install_stock_debs,$(1))
739         mkdir -p DEBIAN/$(1)
740         rsync -a MODULES/$($(1).module)/ DEBIAN/$(1)/
741         make -C DEBIAN/$(1) "RPMTARBALL=$(HOME)/$($(1).tarballs)" "RPMVERSION=$($(1).rpm-version)" "RPMRELEASE=$($(1).rpm-release)" "RPMNAME=$($(1).rpm-name)" debian
742         $(call dpkgapt_uninstall_stock_debs,$(1))
743 endef
744
745 $(foreach package,$(ALL),$(eval $(call target_debian,$(package))))
746
747 ### clean target
748 # usage: target_clean package
749 define target_clean
750 $(1)-clean-modules:
751         $(foreach module,$($(1)-MODULES),rm -rf MODULES/$(module);)
752 .PHONY: $(1)-clean-modules
753 CLEANS += $(1)-clean-modules
754 $(1)-clean-source:
755         rm -rf $($(1).source)
756 .PHONY: $(1)-clean-source
757 CLEANS += $(1)-clean-source
758 $(1)-clean-tarball:
759         rm -rf $($(1).tarballs)
760 .PHONY: $(1)-clean-tarball
761 CLEANS += $(1)-clean-tarball
762 $(1)-clean-build:
763         rm -rf BUILD/$(notdir $($(1).source))
764 CLEANS += $(1)-clean-build
765 $(1)-clean-rpms:
766         rm -rf $($(1).rpms)
767 .PHONY: $(1)-clean-rpms
768 CLEANS += $(1)-clean-rpms
769 $(1)-clean-srpm:
770         rm -rf $($(1).srpm)
771 .PHONY: $(1)-clean-srpm
772 CLEANS += $(1)-clean-srpm
773 $(1)-clean-debian:
774         rm -rf DEBIAN/$(1)
775 .PHONY: $(1)-clean-srpm
776 CLEANS += $(1)-clean-srpm
777 $(1)-codeclean: $(1)-clean-source $(1)-clean-tarball $(1)-clean-build $(1)-clean-rpms $(1)-clean-srpm $(1)-clean-debian
778 $(1)-clean: $(1)-clean-modules $(1)-codeclean
779 .PHONY: $(1)-codeclean $(1)-clean
780 $(1)-clean-spec:
781         rm -rf $($(1).specpath)
782 .PHONY: $(1)-clean-spec
783 $(1)-clean-make:
784         rm -rf MAKE/$(1).mk
785 .PHONY: $(1)-clean-make
786 $(1)-distclean: $(1)-distclean1 $(1)-distclean2
787 $(1)-distclean1: $(1)-clean-spec $(1)-clean-make
788 $(1)-distclean2: $(1)-clean
789 .PHONY: $(1)-distclean $(1)-distclean1 $(1)-distclean2
790 endef
791
792 $(foreach package,$(ALL),$(eval $(call target_clean,$(package))))
793
794 ### clean precisely
795 clean:
796         $(MAKE) $(CLEANS)
797 .PHONY: clean
798
799 clean-help:
800         @echo Available clean targets
801         @echo $(CLEANS)
802
803 ### brute force clean
804 distclean1:
805         rm -rf envfrompreviousrun.mk .rpmmacros spec2make header.spec SPECS MAKE $(DISTCLEANS)
806 distclean2:
807         rm -rf MODULES SOURCES BUILD BUILDROOT RPMS SRPMS DEBIAN tmp
808 distclean: distclean1 distclean2
809 .PHONY: distclean1 distclean2 distclean
810
811 develclean:
812         -$(RPMYUM-UNINSTALL-STOCK) $(ALL-STOCK-DEVEL-RPMS)
813
814 ####################
815 # gather build information for the 'About' page
816 # when run from crontab, INIT_CWD not properly set (says /root ..)
817 # so, the nightly build passes BASE here
818 # also store BASE in .base for any post-processing purposes
819 myplc-release:
820         @echo 'Creating myplc-release'
821         rm -f $@
822         echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx build info" >> $@
823         $(MAKE) --no-print-directory version-build >> $@
824         echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx svn info" >> $@
825         $(MAKE) --no-print-directory version-tags >> $@
826         echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx rpm info" >> $@
827         $(MAKE) --no-print-directory version-rpms >> $@
828         @echo $(BASE) > .base
829
830 version-build:
831         @echo -n 'Build build-date: ' ; date '+%Y.%m.%d'
832         @echo -n 'Build build-time: ' ; date '+%H:%M-%Z'
833         @echo -n 'Build build-hostname: ' ; hostname
834         @echo    "Build build-base: $(BASE)"
835         @echo    "Build planetlab-distro: $(PLDISTRO)"
836         @echo    "Build planetlab-tags: $(PLDISTROTAGS)"
837         @echo    "Build target-arch: $(HOSTARCH)"
838         @echo    "Build target-distro: $(DISTRO)"
839         @echo    "Build target-distroname: $(DISTRONAME)"
840         @echo    "Build target-release: $(RELEASE)"
841         @echo    "Build target-personality: $(PERSONALITY)"
842
843 ####################
844 # for a given module
845 VFORMAT="%30s := %s\n"
846 define print_tag_target
847 $(1)-version-tag:
848         @$(if $($(1)-SVNPATH),\
849            printf $(VFORMAT) $(1)-SVNPATH "$($(1)-SVNPATH)",\
850            printf $(VFORMAT) $(1)-GITPATH "$($(1)-GITPATH)" )
851 endef
852
853 $(foreach module,$(ALL.modules), $(eval $(call print_tag_target,$(module))))
854
855 version-tags: $(foreach module, $(ALL.modules), $(module)-version-tag)
856
857 RFORMAT="%20s :: version=%s release=%s\n"
858 define rpm_version_target
859 $(1)-version-rpm:
860         @printf $(RFORMAT) $($(1).rpm-name) $($(1).rpm-version) $($(1).rpm-release)
861 version-rpms: $(1)-version-rpm
862 endef
863
864 $(foreach package,$(sort $(ALL)), $(eval $(call rpm_version_target,$(package))))
865
866 versions: myplc-release version-build version-tags version-rpms
867 .PHONY: versions version-build version-rpms version-tags
868
869 #################### package info
870 PKGKEYS := tarballs source srpm rpms rpmnames rpm-release rpm-name rpm-version rpm-subversion
871 %-pkginfo: package=$(subst -pkginfo,,$@)
872 %-pkginfo:
873         @$(foreach key,$(PKGKEYS),echo "$(package).$(key)=$($(package).$(key))";)
874 ## rpm info
875 RPMKEYS := rpm-path package
876 %-rpminfo: rpm=$(subst -rpminfo,,$@)
877 %-rpminfo:
878         @$(foreach key,$(RPMKEYS),echo "$(rpm).$(key)=$($(rpm).$(key))";)
879
880 #################### various lists - designed to run with stage1=true
881 info-packages:
882         @$(foreach package,$(ALL), echo package=$(package) ref_module=$($(package).module) modules=$($(package)-MODULES) rpmnames=$($(package).rpmnames); )
883
884 info-modules:
885         @$(foreach module,$(ALL.modules), echo module=$(module) \
886            $(if $($(module)-SVNPATH),svnpath=$($(module)-SVNPATH),gitpath=$($(module)-GITPATH)) ; )
887
888 info-branches:
889         @$(foreach module,$(ALL.modules), \
890           $(if $($(module)-BRANCH),echo module=$(module) branch=$($(module)-BRANCH);))
891
892 module-tools:
893         @$(foreach module,$(ALL.modules), \
894          $(if $($(module)-GITPATH), \
895           $(if $($(module)-BRANCH), \
896              echo $(module):$($(module)-BRANCH); , \
897              echo $(module); ), \
898           $(if $($(module)-BRANCH), \
899              echo $(module):$($(module)-BRANCH); , \
900              echo $(module); )))
901
902 info: info-packages info-modules info-branches
903
904 .PHONY: info info-packages info-modules info-branches module-tools
905
906 show-multi-modules:
907         @$(foreach package,$(ALL),$(if $(subst x1,,x$(words $($(package)-MODULES))), echo p=$(package) mods=$($(package)-MODULES) ;))
908 .PHONY: show-multi-modules
909 ####################
910 help:
911         @echo "********** Run make in two stages:"
912         @echo ""
913         @echo "make stage1=true PLDISTRO=onelab"
914         @echo " -> extracts all spec files in SPECS/ and mk files in MAKE/"
915         @echo "    as well as save PLDISTRO for subsequent runs"
916         @echo ""
917         @echo "********** Then you can use the following targets"
918         @echo 'make'
919         @echo "  rebuilds everything"
920         @echo 'make util-vserver'
921         @echo "  makes the RPMS related to util-vserver"
922         @echo "  equivalent to 'make util-vserver-rpms'"
923         @echo ""
924         @echo "********** Or, vertically - step-by-step for a given package"
925         @echo 'make util-vserver-source'
926         @echo "  creates source link in SOURCES/util-vserver-<version>"
927         @echo 'make util-vserver-tarball'
928         @echo "  creates source tarball in SOURCES/util-vserver-<version>.<tarextension>"
929         @echo 'make util-vserver-srpm'
930         @echo "  build source rpm in SRPMS/"
931         @echo 'make util-vserver-rpms'
932         @echo "  build rpm(s) in RPMS/"
933         @echo ""
934         @echo "********** Or, horizontally, reach a step for all known packages"
935         @echo 'make sources'
936         @echo 'make tarballs'
937         @echo 'make srpms'
938         @echo 'make rpms'
939         @echo ""
940         @echo "********** Manual targets"
941         @echo "make package-compile"
942         @echo "  The regular process uses rpmbuild --rebuild, that performs"
943         @echo "  a compilation directory cleanup upon completion. If you need to investigate"
944         @echo "  the intermediate compilation directory, use the -compile targets"
945         @echo "********** Cleaning examples"
946         @echo "make clean"
947         @echo "  removes the files made by make"
948         @echo "make distclean"
949         @echo "  brute-force cleaning, removes entire directories - requires a new stage1"
950         @echo "make develclean"
951         @echo "  rpm-uninstalls all devel packages installed during build"
952         @echo ""
953         @echo "make iptables-distclean"
954         @echo "  deep clean for a given package"
955         @echo "make iptables-codeclean"
956         @echo "  run this if you've made a local/manual change in the MODULES area for iptables"
957         @echo ""
958         @echo "make util-vserver-clean"
959         @echo "  removes codebase, source, tarball, build, rpm and srpm for util-vserver"
960         @echo "make util-vserver-clean-modules"
961         @echo "  and so on for source, tarball, build, rpm and srpm"
962         @echo ""
963         @echo "********** Info examples"
964         @echo "make ++ALL"
965         @echo "  Displays the value of a given variable (here ALL)"
966         @echo "  with only a single plus sign only the value is displayed"
967         @echo "make info"
968         @echo "  is equivalent to make packages modules branches"
969         @echo "  provides various info on these objects"
970         @echo "make ulogd-pkginfo"
971         @echo "  Displays know attributes of a package"
972         @echo "make kernel-devel-rpminfo"
973         @echo "  Displays know attributes of an rpm"
974         @echo "make stage1=true PLDISTROTAGS=planetlab-tags-4.2.mk packages modules branches module-tools"
975         @echo "  Lists mentioned items - module-tools is used in modules.update"
976         @echo ""
977         @echo "********** Known pakages are"
978         @echo "$(ALL)"
979
980 #################### convenience, for debugging only
981 # make +foo : prints the value of $(foo)
982 # make ++foo : idem but verbose, i.e. foo=$(foo)
983 ++%: varname=$(subst +,,$@)
984 ++%:
985         @echo "$(varname)=$($(varname))"
986 +%: varname=$(subst +,,$@)
987 +%:
988         @echo "$($(varname))"