2 # Thierry Parmentelat - INRIA Sophia Antipolis
10 # this extracts all specfiles and computes .mk from specfiles
11 # you need to specify PLDISTRO here if relevant - see below
13 # for more info on how to invoke this stuff
15 #################### (fedora) distributions
17 # (*) as of nov. 2007, myplc-devel is deprecated
18 # (*) instead, we create a fresh vserver that holds required tools (see e.g. planetlab-devel.lst)
19 # (*) the build uses the current fedora version as a target for the produced images
20 # (*) so you simply need to create a fedora 8 build image for building fedora-8 images
22 #################### (planetlab) distributions
24 # (*) the default distribution is called 'planetlab'
25 # (*) you may define an alternative distribution, e.g. onelab
26 # in this case you need to
27 # (*) create onelab.mk that defines your *packages* (see below)
28 # (*) create onelab-tags.mk that defines where to fetch your *modules*
29 # (*) create your main yumgroups.xml as groups/<distro>.xml
30 # (*) there are also various places where a set of modules are defined.
31 # check for .lst files in the various modules that build root images
32 # and mimick what's done for planetlab
33 # (*) then you need to run
34 # make stage1=true PLDISTRO=onelab
37 # This build deals with 2 kinds of objects
39 # (*) packages are named upon the RPM name; they are moslty lowercase
40 # Add a package to ALL if you want it built as part of the default set.
41 # (*) modules are named after the subversion tree; as of this writing their names
42 # are mostly mixedcase like MyPLC or Vserverreference
44 #################### packages
45 # basics: how to build a package - you need/may define the following variables
48 # a package needs one or several modules to build.
51 # the package's specfile; this is relative to the FIRST module in package-MODULES
52 # see 'codebase' below
56 # (*) package-SPECVARS
57 # space-separated list of spec variable definitions, where you can reference make variable that relate to
58 # packages defined BEFORE the current one (note: you should use = - as opposed to := - to define these)
59 # e.g. mydriver-SPECVARS = foo=$(kernel-rpm-release)
60 # would let you use the %release from the kernel's package when rpmbuild'ing mydriver - see automatic below
62 # a set of *packages* that this package depends on
63 # (*) package-DEPENDFILES
64 # a set of files that the package depends on - and that make needs to know about
65 # if this contains RPMS/yumgroups.xml, then the toplevel RPMS's index
66 # is refreshed with createrepo prior to running rpmbuild
67 # (*) package-RPMFLAGS: Miscellaneous RPM flags
68 # (*) package-RPMBUILD: If not rpmbuild - mostly used for sudo'ing rpmbuild
70 #################### modules
71 # Required information about the various modules (set this in e.g. planetlab-tags.mk)
74 # the complete path where this module lies;
75 # you can specify the trunk or a given tag with this variable
77 # OR if the module is managed under cvs
82 #################### automatic variables
84 # the build defines the following make variables - these are extracted from spec files
85 # (*) package-TARBALL : from the Source<n>: declaration
86 # example: kernel-i386-TARBALL = SOURCES/linux-2.6.20.tar.bz2
87 # (*) package-SOURCE :
88 # example: kernel-i386-SOURCE = SOURCES/linux-2.6.20
90 # example: kernel-i386-SRPM = SRPMS/kernel-2.6.20-1.2949.fc6.vs2.2.0.1.0.planetlab.src.rpm
92 # example: kernel-i386-RPM = \
93 # RPMS/i686/kernel-2.6.20-1.2949.fc6.vs2.2.0.1.0.planetlab.i686.rpm \
94 # RPMS/i686/kernel-devel-2.6.20-1.2949.fc6.vs2.2.0.1.0.planetlab.i686.rpm \
95 # RPMS/i686/kernel-vserver-2.6.20-1.2949.fc6.vs2.2.0.1.0.planetlab.i686.rpm \
96 # RPMS/i686/kernel-debuginfo-2.6.20-1.2949.fc6.vs2.2.0.1.0.planetlab.i686.rpm
97 # (*) package-rpm-name
98 # example: kernel-i386-rpm-name = kernel
99 # (*) package-rpm-release
100 # example: kernel-i386-rpm-release = 1.2949.fc6.vs2.2.0.1.0.planetlab
101 # (*) package-version
102 # example: kernel-i386-rpm-version = 2.6.20
103 # (*) package-subversion
104 # example: myplc-rpm-subversion = 15
110 HOSTARCH := $(shell uname -i)
111 DISTRO := $(shell ./getdistro.sh)
112 RELEASE := $(shell ./getrelease.sh)
114 #################### Makefile
120 PLDISTRO := planetlab
122 export CVS_RSH := ssh
124 ########## pldistro.mk holds PLDISTRO - it is generated at stage1 (see below)
129 #################### include onelab.mk
130 # describes the set of components
131 PLDISTROCONTENTS := $(PLDISTRO).mk
132 include $(PLDISTROCONTENTS)
134 #################### include <pldistro>-tags.mk
135 # describes where to fetch components, and the related tags if using cvs
136 PLDISTROTAGS := $(PLDISTRO)-tags.mk
137 include $(PLDISTROTAGS)
139 ########## stage1 and stage2
140 # extract specs and compute .mk files by running
142 # entering stage1, we compute all the spec files
143 # then we use stage2 to compute the .mk iteratively,
144 # ensuring that the n-1 first makefiles are loaded when creating the n-th one
145 # when stage2 is set, it is supposed to be an index (starting at 1) in $(ALL)
147 ALLMKS := $(foreach package, $(ALL), MAKE/$(package).mk)
149 ### stage2 : need some arithmetic, see
150 # http://www.cmcrossroads.com/articles/ask-mr.-make/learning-gnu-make-functions-with-arithmetic.html
152 # the first n packages
153 packages := $(wordlist 1,$(words $(stage2)),$(ALL))
155 package := $(word $(words $(packages)),$(packages))
156 # the n-1 first packages
157 stage2_1 := $(wordlist 2,$(words $(stage2)),$(stage2))
158 previous := $(wordlist 1,$(words $(stage2_1)),$(ALL))
159 previousmks := $(foreach package,$(previous),MAKE/$(package).mk)
160 include $(previousmks)
163 @echo "========== stage2 : $(package)"
164 # @echo "stage2 : included .mk files : $(previousmks)"
165 all: $($(package)_specpath)
166 all: MAKE/$(package).mk
172 @echo "========== stage1"
175 # specs and makes are done sequentially by stage2
178 arg=""; for n in $(ALL) ; do arg="$$arg x"; $(MAKE) --no-print-directory stage2="$$arg"; done
181 ### once .mks are OK, you can run make normally
188 # mention $(ALL) here rather than rpms
189 # this is because the inter-package dependencies are expressed like
190 # util-vserver: util-python
197 # gather build information for the 'About' page
198 SOURCES/myplc-release:
199 @echo 'Creating myplc-release'
202 (echo -n 'Build bdate: ' ; date '+%Y.%m.%d') >> $@
203 (echo -n 'Build btime: ' ; date '+%H:%M') >> $@
204 (echo -n 'Build hostname: ' ; hostname) >> $@
205 (echo -n 'Build location: ' ; pwd) >> $@
206 (echo -n 'Build tags file: ' ; fgrep '$$''Id' $(PLDISTROTAGS)) >> $@
207 echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx modules versions info" >> $@
208 $(MAKE) --no-print-directory versions >> $@
212 # * to make configuration easier, we always use the first module's
213 # definitions (CVSROOT,TAG, or SVNPATH) to extract the spec file
214 # * for the same reason, in case cvs is used, the first module name is added to
215 # $(package)-SPEC - otherwise the cvs modules have to define spec as
216 # <module>/<module>.spec while svn modules just define it as <module>.spec
218 define stage1_variables
219 $(1)_spec = $(notdir $($(1)-SPEC))
220 $(1)_specpath = SPECS/$(notdir $($(1)-SPEC))
221 $(1)_module = $(firstword $($(1)-MODULES))
224 $(foreach package, $(ALL), $(eval $(call stage1_variables,$(package))))
227 # for each package, compute whether we need to set date (i.e. whether we use the trunk)
228 # the myplc package is forced to have a date, because it is more convenient
229 # (we cannot bump its number everytime something changes in the system)
231 define package_hasdate
232 $(1)_hasdate = $(if $(subst myplc,,$(1)), \
233 $(if $($(1)-SVNPATH),\
234 $(if $(findstring /trunk,$($(1)-SVNPATH)),yes,),\
235 $(if $(findstring HEAD,$($(1)-TAG)),yes,)), \
239 $(foreach package, $(ALL), $(eval $(call package_hasdate,$(package))))
241 ### extract spec file from scm
242 # usage: extract_spec_file package
243 # see early releases for comments on other possible implementations
244 # cannot use variables in such rules, we need to inline everything, sigh
248 (echo -n "# Generated by planetlab build from $($(1)-SPEC) on " ; date) > $($(1)_specpath)
249 echo "%define distroname $(DISTRO)" >> $($(1)_specpath)
250 echo "%define distrorelease $(RELEASE)" >> $($(1)_specpath)
251 echo "%define pldistro $(PLDISTRO)" >> $($(1)_specpath)
252 $(if $($(1)_hasdate),echo "%define date $(shell date +%Y.%m.%d)" >> $($(1)_specpath),)
253 echo "# included from codebase specfile" >> $($(1)_specpath)
254 $(if $($(1)-SPECVARS), \
255 $(foreach line,$($(1)-SPECVARS), \
256 echo "%define" $(word 1,$(subst =, ,$(line))) "$(word 2,$(subst =, ,$(line)))" >> $($(1)_specpath) ;))
257 $(if $($($(1)_module)-SVNPATH),\
258 svn cat $($($(1)_module)-SVNPATH)/$($(1)-SPEC) >> $($(1)_specpath),\
259 cvs -d $($($(1)_module)-CVSROOT) checkout \
260 -r $($($(1)_module)-TAG) \
261 -p $($(1)_module)/$($(1)-SPEC) >> $($(1)_specpath))
262 @if [ -z $($(1)_specpath) ] ; then rm $($(1)_specpath) ; exit 1 ; fi
265 $(foreach package,$(ALL),$(eval $(call target_spec,$(package))))
267 ### this utility allows to extract various info from a spec file
268 ### and to define them in makefiles
269 spec2make: spec2make.c
270 $(CC) -g -Wall $< -o $@ -lrpm -lrpmbuild
272 # Base rpmbuild in the current directory
273 export HOME := $(shell pwd)
275 echo "%_topdir $(HOME)" > $@
276 echo "%_tmppath $(HOME)/tmp" >> $@
278 ### run spec2make on the spec file and include the result
279 # usage: spec2make package
281 MAKE/$(1).mk: $($(1)_specpath) spec2make .rpmmacros
283 ./spec2make $($(1)-RPMFLAGS) $($(1)_specpath) $(1) > MAKE/$(1).mk
284 @if [ -z MAKE/$(1).mk ] ; then rm MAKE/$(1).mk ; exit 1 ; fi
287 $(foreach package,$(ALL),$(eval $(call target_mk,$(package))))
289 # stores PLDISTRO in a file
290 # this is done at stage1. later run wont get confused
292 echo "PLDISTRO:=$(PLDISTRO)" > $@
293 echo "PLDISTROTAGS:=$(PLDISTROTAGS)" >> $@
295 savepldistro: pldistro.mk
298 # always refresh this
302 ### pack sources into tarballs
303 ALLTARBALLS:= $(foreach package, $(ALL), $($(package)-TARBALL))
304 tarballs: $(ALLTARBALLS)
305 @echo $(words $(ALLTARBALLS)) source tarballs OK
308 SOURCES/%.tar.bz2: SOURCES/%
309 tar chpjf $@ -C SOURCES $*
311 SOURCES/%.tar.gz: SOURCES/%
312 tar chpzf $@ -C SOURCES $*
314 SOURCES/%.tgz: SOURCES/%
315 tar chpzf $@ -C SOURCES $*
318 URLS/%: url=$(subst @colon@,:,$(subst @slash@,/,$(notdir $@)))
319 URLS/%: basename=$(notdir $(url))
321 echo curl $(url) -o SOURCES/$(basename)
324 ### the directory SOURCES/<package>-<version> is made
325 # with a copy -rl from CODEBASES/<package>
326 # the former is $(package-SOURCE) and the latter is $(package-CODEBASE)
327 ALLSOURCES:=$(foreach package, $(ALL), $($(package)-SOURCE))
328 # so that make does not use the rule below directly for creating the tarball files
329 .SECONDARY: $(ALLSOURCES)
331 sources: $(ALLSOURCES)
332 @echo $(words $(ALLSOURCES)) versioned source trees OK
335 define target_link_codebase_sources
336 $($(1)-SOURCE): $($(1)-CODEBASE) ; mkdir -p SOURCES ; cp -rl $($(1)-CODEBASE) $($(1)-SOURCE)
339 $(foreach package,$(ALL),$(eval $(call target_link_codebase_sources,$(package))))
341 ### codebase extraction
342 ALLCODEBASES:=$(foreach package, $(ALL), $($(package)-CODEBASE))
343 # so that make does not use the rule below directly for creating the tarball files
344 .SECONDARY: $(ALLCODEBASES)
346 codebases : $(ALLCODEBASES)
347 @echo $(words $(ALLCODEBASES)) codebase OK
351 # usage: extract_single_module package
352 define extract_single_module
354 $(if $($($(1)_module)-SVNPATH), cd CODEBASES && svn export $($($(1)_module)-SVNPATH) $(1), cd CODEBASES && cvs -d $($($(1)_module)-CVSROOT) export -r $($($(1)_module)-TAG) -d $(1) $($(1)_module))
357 # usage: extract_multi_module package
358 define extract_multi_module
359 mkdir -p CODEBASES/$(1) && cd CODEBASES/$(1) && (\
360 $(foreach m,$($(1)-MODULES), $(if $($(m)-SVNPATH), svn export $($(m)-SVNPATH) $(m);, cvs -d $($(m)-CVSROOT) export -r $($(m)-TAG) $(m);)))
363 CODEBASES/%: package=$(notdir $@)
364 CODEBASES/%: multi_module=$(word 2,$($(package)-MODULES))
366 @(echo -n "XXXXXXXXXXXXXXX -- BEG CODEBASE $(package) : $@ " ; date)
367 $(if $(multi_module),\
368 $(call extract_multi_module,$(package)),\
369 $(call extract_single_module,$(package)))
370 @(echo -n "XXXXXXXXXXXXXXX -- END CODEBASE $(package) : $@ " ; date)
372 ### rpmbuild invokation
373 ALLRPMS:=$(foreach package,$(ALL),$($(package)-RPM))
374 # same as above, mention $(ALL) and not $(ALLRPMS)
376 @echo $(words $(ALLRPMS)) binary rpms OK
379 # usage: build_binary_rpm package
380 # xxx hacky - invoke createrepo if DEPENDFILES mentions RPMS/yumgroups.xml
381 define target_binary_rpm
382 $($(1)-RPM): $($(1)_specpath) $($(1)-TARBALL) .rpmmacros
383 mkdir -p BUILD RPMS tmp
384 @(echo -n "XXXXXXXXXXXXXXX -- BEG RPM $(1) " ; date)
385 $(if $(findstring RPMS/yumgroups.xml,$($(1)-DEPENDFILES)), createrepo --quiet -g yumgroups.xml RPMS/ , )
386 $(if $($(1)-RPMBUILD),\
387 $($(1)-RPMBUILD) $($(1)-RPMFLAGS) -bb $($(1)_specpath), \
388 $(RPMBUILD) $($(1)-RPMFLAGS) -bb $($(1)_specpath))
389 @(echo -n "XXXXXXXXXXXXXXX -- END RPM $(1) " ; date)
392 $(foreach package,$(ALL),$(eval $(call target_binary_rpm,$(package))))
395 ALLSRPMS:=$(foreach package,$(ALL),$($(package)-SRPM))
397 @echo $(words $(ALLSRPMS)) source rpms OK
400 # usage: build_source_rpm package
401 define target_source_rpm
402 $($(1)-SRPM): $($(1)_specpath) $($(1)-TARBALL) .rpmmacros
403 mkdir -p BUILD SRPMS tmp
404 @(echo -n "XXXXXXXXXXXXXXX -- BEG SRPM $(1) " ; date)
405 $(if $($(1)-RPMBUILD),\
406 $($(1)-RPMBUILD) $($(1)-RPMFLAGS) -bs $($(1)_specpath),
407 $(RPMBUILD) $($(1)-RPMFLAGS) -bs $($(1)_specpath))
408 @(echo -n "XXXXXXXXXXXXXXX -- END SRPM $(1) " ; date)
411 $(foreach package,$(ALL),$(eval $(call target_source_rpm,$(package))))
413 ### RPMS/yumgroups.xml
415 YUMGROUPS := groups/$(PLDISTRO).xml
417 RPMS/yumgroups.xml: $(YUMGROUPS)
418 install -D -m 644 $(YUMGROUPS) $@
421 # e.g. make proper -> does propers rpms
422 # usage shorthand_target package
423 define target_shorthand
424 $(1): $($(package)-RPM)
426 $(1)-spec: $($(package)-SPEC)
428 $(1)-mk: $($(package)-MK)
430 $(1)-tarball: $($(package)-TARBALL)
432 $(1)-codebase: $($(package)-CODEBASE)
434 $(1)-source: $($(package)-SOURCE)
435 .PHONY: $(1)-codebase
436 $(1)-rpm: $($(package)-RPM)
438 $(1)-srpm: $($(package)-SRPM)
442 $(foreach package,$(ALL),$(eval $(call target_shorthand,$(package))))
445 define package_depends_on_file
450 define target_dependfiles
451 $(foreach file,$($(1)-DEPENDFILES),$(eval $(call package_depends_on_file,$(1),$(file))))
454 define package_depends_on_package
457 $($(1)-RPM):$($(2)-RPM)
460 define target_depends
461 $(foreach package,$($(1)-DEPENDS),$(eval $(call package_depends_on_package,$(1),$(package))))
464 $(foreach package,$(ALL),$(eval $(call target_depends,$(package))))
465 $(foreach package,$(ALL),$(eval $(call target_dependfiles,$(package))))
469 # usage: target_clean package
472 rm -rf $($(1)-CODEBASE)
473 .PHONY: $(1)-clean-codebase
474 CLEANS += $(1)-clean-codebase
476 rm -rf $($(1)-SOURCE)
477 .PHONY: $(1)-clean-source
478 CLEANS += $(1)-clean-source
480 rm -rf $($(1)-TARBALL)
481 .PHONY: $(1)-clean-tarball
482 CLEANS += $(1)-clean-tarball
484 rm -rf BUILD/$(notdir $($(1)-SOURCE))
485 CLEANS += $(1)-clean-build
488 .PHONY: $(1)-clean-rpm
489 CLEANS += $(1)-clean-rpm
492 .PHONY: $(1)-clean-srpm
493 CLEANS += $(1)-clean-srpm
494 $(1)-clean: $(1)-clean-codebase $(1)-clean-source $(1)-clean-tarball $(1)-clean-build $(1)-clean-rpm $(1)-clean-srpm
498 $(foreach package,$(ALL),$(eval $(call target_clean,$(package))))
506 @echo Available clean targets
509 ### brute force clean
511 rm -rf pldistro.mk .rpmmacros spec2make SPECS MAKE
513 rm -rf CODEBASES SOURCES BUILD RPMS SRPMS tmp
514 distclean: distclean1 distclean2
515 .PHONY: distclean1 distclean2 distclean
517 # xxx tmp - I cannot use this on my mac for local testing
518 ISMACOS=$(findstring Darwin,$(shell uname))
519 ifneq "$(ISMACOS)" ""
520 #################### produce reliable version information
522 VFORMAT="%30s := %s\n"
525 @$(if $($(1)-SVNPATH),\
526 printf $(VFORMAT) $(1)-SVNPATH "$($(1)-SVNPATH)",\
527 printf $(VFORMAT) $(1)-CVSROOT "$($(1)-CVSROOT)" ; printf $(VFORMAT) $(1)-TAG "$($(1)-TAG)")
530 # compute all modules
532 $(foreach package,$(ALL), $(eval ALL-MODULES+=$($(package)-MODULES)))
533 ALL-MODULES:=$(sort $(ALL-MODULES))
535 $(foreach module,$(ALL-MODULES), $(eval $(call print_version,$(module))))
537 versions: $(foreach module, $(ALL-MODULES), $(module)-version)
540 @echo "warning : the 'versions' target is not supported on macos"
543 #################### include install Makefile
544 # the default is to use the distro-dependent install file
545 # however the main distro file can redefine PLDISTROINSTALL
546 ifndef PLDISTROINSTALL
547 PLDISTROINSTALL := $(PLDISTRO)-install.mk
550 -include $(PLDISTROINSTALL)
554 @echo "Known pakages are"
556 @echo "Run make in two stages:"
558 @echo "make stage1=true PLDISTRO=onelab"
559 @echo " -> extracts all spec files in SPECS/ and mk files in MAKE/"
560 @echo " as well as save PLDISTRO for subsequent runs"
562 @echo "Then you can use the following targets"
564 @echo " rebuilds everything"
565 @echo '$ make util-vserver'
566 @echo " makes the RPMS related to util-vserver"
567 @echo " equivalent to 'make util-vserver-rpm'"
569 @echo "Or, vertically - step-by-step for a given package"
570 @echo '$ make util-vserver-codebase'
571 @echo " performs codebase extraction in CODEBASES/util-vserver"
572 @echo '$ make util-vserver-source'
573 @echo " creates source link in SOURCES/util-vserver-<version>"
574 @echo '$ make util-vserver-tarball'
575 @echo " creates source tarball in SOURCES/util-vserver-<version>.<tarextension>"
576 @echo '$ make util-vserver-rpm'
577 @echo " build rpm(s) in RPMS/"
578 @echo '$ make util-vserver-srpm'
579 @echo " build source rpm in SRPMS/"
581 @echo "Or, horizontally, reach a step for all known packages"
582 @echo '$ make codebases'
583 @echo '$ make sources'
584 @echo '$ make tarballs'
588 @echo "Cleaning examples"
590 @echo " removes the files made by make"
591 @echo "$ make distclean"
592 @echo " brute-force cleaning, removes entire directories - requires a new stage1"
593 @echo "$ make util-vserver-clean"
594 @echo " removes codebase, source, tarball, build, rpm and srpm for util-vserver"
595 @echo "$ make util-vserver-clean-codebase"
596 @echo " and so on for source, tarball, build, rpm and srpm"
598 #################### convenience, for debugging only
599 # make +foo : prints the value of $(foo)
600 # make ++foo : idem but verbose, i.e. foo=$(foo)
601 ++%: varname=$(subst +,,$@)
603 @echo "$(varname)=$($(varname))"
604 +%: varname=$(subst +,,$@)
606 @echo "$($(varname))"