on THomas's behalf, dusting off this repo for building
[linux-3.git] / Makefile
1 #
2 CURL    ?= $(shell if test -f /usr/bin/curl ; then echo "curl -H Pragma: -O -R -S --fail --show-error" ; fi)
3 WGET    ?= $(shell if test -f /usr/bin/wget ; then echo "wget -nd -m" ; fi)
4 CLIENT  ?= $(if $(CURL),$(CURL),$(if $(WGET),$(WGET)))
5 AWK     = awk
6 SHA1SUM = sha1sum
7 SED     = sed
8
9 # TD 21.03.2014: Using "--with baseonly" to avoid building all the special variants.
10 RPMBUILDOPT = --with baseonly --without tools --without debug --without debuginfo
11 # this is passed on the command line as the full path to <build>/SPECS/kernel.spec
12
13 SPECFILE = kernel.spec
14
15 # Thierry - when called from within the build, PWD is /build
16 PWD=$(shell pwd)
17
18 # get nevr from specfile.
19 ifndef NAME
20 NAME := $(shell rpm $(RPMDEFS) $(DISTDEFS) -q --qf "%{NAME}\n" --specfile $(SPECFILE) | head -1)
21 endif
22 ifndef EPOCH
23 EPOCH := $(shell rpm $(RPMDEFS) $(DISTDEFS) -q --qf "%{EPOCH}\n" --specfile $(SPECFILE) | head -1 | sed 's/(none)//')
24 endif
25 ifeq ($(EPOCH),(none))
26 override EPOCH := ""
27 endif
28 ifndef VERSION
29 VERSION := $(shell rpm $(RPMDEFS) $(DISTDEFS) -q --qf "%{VERSION}\n" --specfile $(SPECFILE)| head -1)
30 endif
31 ifndef RELEASE
32 RELEASE := $(shell rpm $(RPMDEFS) $(DISTDEFS) -q --qf "%{RELEASE}\n" --specfile $(SPECFILE)| head -1)
33 endif
34
35 define get_sources_sha1
36 $(shell cat sources 2>/dev/null | awk 'gensub("^.*/", "", 1, $$2) == "$@" { print $$1; exit; }')
37 endef
38 define get_sources_url1
39 $(shell cat sources 2>/dev/null | awk 'gensub("^.*/", "", 1, $$2) == "$@" { print $$2; exit; }')
40 endef
41 define get_sources_url2
42 $(shell cat sources 2>/dev/null | awk 'gensub("^.*/", "", 1, $$2) == "$@" { print $$3; exit; }')
43 endef
44 SOURCEFILES := $(shell cat sources 2>/dev/null | awk '{ print gensub("^.*/", "", 1, $$2) }')
45 SOURCE_RPM := $(firstword $(SOURCEFILES))
46
47 sources: $(SOURCEFILES) $(TARGETS)
48
49 $(SOURCEFILES): #FORCE
50         @if [ ! -e "$@" ] ; then \
51          { echo Using primary; echo "$(CLIENT) $(get_sources_url1)" ; $(CLIENT) $(get_sources_url1) ; } || \
52          { echo Using secondary; echo "$(CLIENT) $(get_sources_url2)" ; $(CLIENT) $(get_sources_url2) ; } ; fi
53         @if [ ! -e "$@" ] ; then echo "Could not download source file: $@ does not exist" ; exit 1 ; fi
54         @if test "$$(sha1sum $@ | awk '{print $$1}')" != "$(get_sources_sha1)" ; then \
55             echo "sha1sum of the downloaded $@ does not match the one from 'sources' file" ; \
56             echo "Local copy: $$(sha1sum $@)" ; \
57             echo "In sources: $$(grep $@ sources)" ; \
58             exit 1 ; \
59         else \
60             ls -l $@ ; \
61         fi
62
63 download-sources:
64         @for i in $(SOURCES); do \
65                 if [ ! -e "$${i##*/}" ]; then \
66                         echo "$(CLIENT) $$i"; \
67                         $(CLIENT) $$i; \
68                 fi; \
69         done
70
71 replace-sources:
72         rm -f sources
73         @$(MAKE) new-sources
74
75 new-sources: download-sources
76         @for i in $(SOURCES); do \
77                 echo "$(SHA1SUM) $$i >> sources"; \
78                 $(SHA1SUM) $${i##*/} | $(AWK) '{ printf "%s  %s\n", $$1, "'"$$i"'" }' >> sources; \
79         done
80
81 # TD 21.03.2014: Needs to define _specdir. Otherwise, the spec file is not found.
82 #                This triggers "patch  xxxxx  not listed as a source patch in specfile",
83 #                since the specfile cannot be opened by "grep".
84 # TD 21.03.2014: The spec file relies on "bash" for regexp and "[[". Set it by _buildshell.
85 PREPARCH ?= noarch
86 RPMDIRDEFS = \
87    --define "_specdir $(PWD)" \
88    --define "_sourcedir $(PWD)/SOURCES" \
89    --define "_builddir $(PWD)" \
90    --define "_srcrpmdir $(PWD)" \
91    --define "_rpmdir $(PWD)" \
92    --define "_buildshell /bin/bash"
93 trees: sources
94         rpmbuild $(RPMDIRDEFS) $(RPMDEFS) $(RPMBUILDOPT) --nodeps -bp --target $(PREPARCH) $(SPECFILE)
95
96 # use the stock source rpm, unwrap it,
97 # copy the downloaded material
98 # install our own specfile and patched patches
99 # and patch configs for IPV6
100 # then rewrap with rpm
101 srpm: sources
102         mkdir -p SOURCES SRPMS
103         (cd SOURCES; rpm2cpio ../$(SOURCE_RPM) | cpio -diu; \
104          cp ../$(notdir $(SPECFILE)) . ; cp ../*.patch .; cp ../config-planetlab .; \
105          for downloaded in $(SOURCEFILES) ; do cp ../$$downloaded . ; done ; \
106          cat config-planetlab >> config-generic)
107         ./rpmmacros.sh
108         export HOME=$(shell pwd) ; rpmbuild $(RPMDIRDEFS) $(RPMDEFS) --nodeps -bs $(SPECFILE)
109
110 TARGET ?= $(shell uname -m)
111 rpm: srpm
112         rpmbuild $(RPMDIRDEFS) $(RPMDEFS) $(RPMBUILDOPT) --nodeps --target $(TARGET) -bb $(SPECFILE)
113
114 distclean: whipe
115
116 whipe: clean
117         rm -f *.rpm
118         rm -rf kernel-*
119         rm -rf x86_64
120
121 clean:
122         rm -f kernel-*.src.rpm
123         rm -rf BUILDROOT SOURCES SPECS SRPMS tmp