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