add support for multiple substitutions
[alioth/jupp.git] / debian / rules
1 #!/usr/bin/make -f
2 #-
3 # Possible flavourisation in DEB_BUILD_OPTIONS (in addition
4 # to stock Debian rules):
5 # - static (use with system libc; default for all others)
6 # - shared (use with klibc or musl)
7 # - diet, klibc, musl
8 # - debug (adds -g3)
9 # - nopwnam (aids static linkage)
10 # - nofpu (default with klibc)
11 # - nommu
12 # - nolibs (use with system libc; default for all others)
13 # - noautotools (on systems lacking autotools-dev)
14 # - noselinux
15 # The 'nostrip' and 'parallel=n' options are supported as well.
16 #
17 # Using a non-system libc disables -O0/-O2 and -g and, if unset,
18 # sets CC. Cross-compiling also sets CC if unset.
19 #
20 # Statically linking violates the Policy requirement of having a
21 # Built-Using header, but since that is only used in local rebuilds
22 # and not in the Debian archive, it constitutes no Policy violation.
23
24 shellescape='$(subst ','\'',$(1))'
25 shellexport=$(1)=$(call shellescape,${$(1)})
26
27 USE_LIBC:=
28 ifneq (,$(findstring diet,${DEB_BUILD_OPTIONS}))
29 USE_LIBC+=      dietlibc
30 endif
31 ifneq (,$(findstring klibc,${DEB_BUILD_OPTIONS}))
32 USE_LIBC+=      klibc
33 endif
34 ifneq (,$(findstring musl,${DEB_BUILD_OPTIONS}))
35 USE_LIBC+=      musl
36 endif
37 ifeq (,$(findstring diet,${DEB_BUILD_OPTIONS})$(findstring klibc,${DEB_BUILD_OPTIONS})$(findstring musl,${DEB_BUILD_OPTIONS}))
38 USE_LIBC+=      system
39 endif
40 ifneq ($(strip ${USE_LIBC}),$(firstword ${USE_LIBC}))
41 $(error multiple libcs (${USE_LIBC}) found in (${DEB_BUILD_OPTIONS}))
42 endif
43 USE_LIBC:=$(strip ${USE_LIBC})
44
45 ifeq (${USE_LIBC},system)
46 USE_SHARED:=1
47 USE_LIBS:=1
48 else
49 USE_SHARED:=0
50 USE_LIBS:=0
51 endif
52
53 ifneq (,$(findstring shared,${DEB_BUILD_OPTIONS}))
54 USE_SHARED:=1
55 endif
56 ifneq (,$(findstring static,${DEB_BUILD_OPTIONS}))
57 USE_SHARED:=0
58 endif
59
60 USE_PWNAM:=1
61 ifneq (,$(findstring nopwnam,${DEB_BUILD_OPTIONS}))
62 USE_PWNAM:=0
63 endif
64
65 USE_FPU:=1
66 ifneq (,$(findstring nofpu,${DEB_BUILD_OPTIONS}))
67 USE_FPU:=0
68 endif
69
70 USE_MMU:=1
71 ifneq (,$(findstring nommu,${DEB_BUILD_OPTIONS}))
72 USE_MMU:=0
73 endif
74
75 ifneq (,$(findstring nolibs,${DEB_BUILD_OPTIONS}))
76 USE_LIBS:=0
77 endif
78
79 USE_SELINUX:=1
80 ifneq (,$(findstring noselinux,${DEB_BUILD_OPTIONS}))
81 USE_SELINUX:=0
82 endif
83 ifeq (0,${USE_LIBS})
84 USE_SELINUX:=0
85 endif
86
87 DEB_BUILD_ARCH?=$(shell dpkg-architecture -qDEB_BUILD_ARCH)
88 DEB_BUILD_GNU_TYPE?=$(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
89 DEB_HOST_ARCH?=$(shell dpkg-architecture -qDEB_HOST_ARCH)
90 DEB_HOST_GNU_TYPE?=$(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
91
92 # is ${CC} defined anywhere (other than implicit rules?)
93 ifneq (,$(findstring $(origin CC),default undefined))
94 # no - then default to gcc (or cross-gcc)
95 ifneq (${DEB_BUILD_ARCH},${DEB_HOST_ARCH})
96 CC=                     ${DEB_HOST_GNU_TYPE}-gcc
97 else
98 ifeq (${USE_LIBC},dietlibc)
99 CC=                     diet -Os gcc
100 endif
101 ifeq (${USE_LIBC},klibc)
102 CC=                     klcc
103 endif
104 ifeq (${USE_LIBC},musl)
105 CC=                     musl-gcc
106 endif
107 CC?=                    gcc
108 endif
109 endif
110
111 EXTRA_CFLAGS+=          -Wall -Wformat
112 EXTRA_CFLAGS+=          -fno-omit-frame-pointer -fwrapv -fno-strict-aliasing
113 # adjust when backporting
114 EXTRA_CFLAGS+=          -Wextra -Wno-pointer-sign -Wno-unused-parameter \
115                         -Wno-old-style-definition -Wno-strict-prototypes \
116                         -Wno-cast-qual \
117                         -Wno-missing-prototypes -Wno-missing-declarations
118 EXTRA_LDFLAGS=          -Wl,--as-needed
119
120 # 5 is enough
121 DEBHELPER_VERSION:=     9
122 DSC_DEPS:=              debhelper (>= ${DEBHELPER_VERSION})
123 SUBST_VARS+=            DSC_DEPS
124
125 ifeq (${USE_LIBC},dietlibc)
126 DSC_DEPS:=              ${DSC_DEPS}, dietlibc-dev
127 USE_SHARED:=0
128 EXTRA_CFLAGS+=          -fno-stack-protector
129 endif
130
131 ifeq (${USE_LIBC},klibc)
132 DSC_DEPS:=              ${DSC_DEPS}, libklibc-dev
133 USE_FPU:=0
134 EXTRA_CFLAGS+=          -fno-stack-protector
135 ifeq (1,${USE_SHARED})
136 CC+=                    -shared
137 endif
138 endif
139
140 ifeq (${USE_LIBC},musl)
141 DSC_DEPS:=              ${DSC_DEPS}, musl-tools
142 EXTRA_CFLAGS+=          -Os
143 ifeq (0,${USE_SHARED})
144 EXTRA_LDFLAGS+=         -static
145 endif
146 endif
147
148 ifeq (${USE_LIBC},system)
149 ifeq (0,${USE_SHARED})
150 EXTRA_LDFLAGS+=         -static
151 endif
152 endif
153
154 ifneq (,$(findstring debug,${DEB_BUILD_OPTIONS}))
155 EXTRA_CFLAGS+=          -g3
156 endif
157
158 ifneq (,$(wildcard /usr/share/dpkg/buildflags.mk))
159 # dpkg-dev (>= 1.16.1~)
160 DEB_CFLAGS_MAINT_APPEND=${EXTRA_CFLAGS}
161 DEB_LDFLAGS_MAINT_APPEND=${EXTRA_LDFLAGS}
162 ifneq (${USE_LIBC},system)
163 DEB_CFLAGS_MAINT_STRIP=-O -O0 -O1 -O2 -O3 -Os -Ofast -Og -g -g1 -g2 -g3 -ggdb -gdwarf -gstabs
164 DEB_CFLAGS_MAINT_STRIP+=-specs=/usr/share/dpkg/no-pie-compile.specs
165 DEB_LDFLAGS_MAINT_STRIP+=-specs=/usr/share/dpkg/no-pie-link.specs
166 # do not use PIE with strange libcs
167 DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie
168 else
169 ifneq (,$(findstring static,${DEB_BUILD_OPTIONS}))
170 # cannot mix PIE with static linkage
171 DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie
172 else
173 DEB_BUILD_MAINT_OPTIONS=hardening=+all
174 endif
175 endif
176 include /usr/share/dpkg/buildflags.mk
177 else
178 # old-fashioned way to determine build flags
179 ifneq (${USE_LIBC},system)
180 CFLAGS=
181 else
182 CFLAGS=                 -O$(if $(findstring noopt,${DEB_BUILD_OPTIONS}),0,2) -g
183 endif
184 CFLAGS+=                ${EXTRA_CFLAGS}
185 LDFLAGS+=               ${EXTRA_LDFLAGS}
186 endif
187
188 ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
189 NUMJOBS=                $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
190 MAKEFLAGS+=             -j${NUMJOBS}
191 endif
192
193 CONFIGURE_ARGS=         --build=${DEB_BUILD_GNU_TYPE} \
194                         --host=${DEB_HOST_GNU_TYPE} \
195                         --prefix=/usr \
196                         --sysconfdir=/etc \
197                         --mandir=/usr/share/man \
198                         --disable-dependency-tracking
199
200 ifeq (0,${USE_PWNAM})
201 CONFIGURE_ARGS+=        --disable-getpwnam
202 endif
203
204 ifeq (0,${USE_FPU})
205 CONFIGURE_ARGS+=        --disable-fpu
206 endif
207
208 ifeq (0,${USE_MMU})
209 CONFIGURE_ARGS+=        --disable-fork
210 endif
211
212 ifeq (0,${USE_LIBS})
213 CONFIGURE_ARGS+=        --disable-search-libs \
214                         --disable-terminfo
215 else
216 DSC_DEPS:=              ${DSC_DEPS}, libtinfo-dev | libncurses-dev
217 endif
218
219 ifeq (1,${USE_SELINUX})
220 #DSC_DEPS:=             ${DSC_DEPS}, libselinux1-dev [!kfreebsd-i386 !kfreebsd-amd64 !hurd-i386]
221 DSC_DEPS:=              ${DSC_DEPS}, libselinux1-dev [linux-any]
222 endif
223
224 ifeq (,$(findstring noautotools,${DEB_BUILD_OPTIONS}))
225 DSC_DEPS:=              ${DSC_DEPS}, autotools-dev
226 endif
227
228 CONFIGURE_ARGS+=        --disable-termidx \
229                         --enable-sysconfjoesubdir=/jupp
230
231 debian/.configure_stamp: debian/.control_stamp
232         dh_testdir
233         -rm -f debian/.*_stamp
234         -rm -rf builddir
235         # apply autotools-dev if it exists
236         for x in config.guess config.sub; do \
237                 test -e /usr/share/misc/$$x || continue; \
238                 rm -f $$x; \
239                 cp /usr/share/misc/$$x .; \
240         done
241         mkdir builddir
242         cd builddir && exec env \
243             $(foreach i,CC CFLAGS CPPFLAGS LDFLAGS,$(call shellexport,$i)) \
244             sh ../configure ${CONFIGURE_ARGS}
245         @:>$@
246
247 debian/.build_stamp: debian/.configure_stamp
248         dh_testdir
249         cd builddir && exec ${MAKE}
250         ln -f builddir/joe builddir/jupp
251         ln -f builddir/joe.1 builddir/jupp.1
252         @:>$@
253
254 debian/.control_stamp:
255         @(if test -e debian/control.in; then exit 0; else \
256                 echo 'dh_testdir: "debian/control.in" not found.' \
257                     Are you sure you are in the correct directory\?; \
258                 exit 1; \
259         fi)
260         echo ${DEBHELPER_VERSION} >debian/compat
261         sed \
262             $(foreach v,${SUBST_VARS},-e 's\ 1@@'$(call shellescape,$v)'@@\ 1'$(call shellescape,$(strip ${$v}))'\ 1') \
263             <debian/control.in >debian/.gencontrol_stamp
264         : bail out if these differ: in those cases,
265         : retry the build, after regenerating debian/control
266         : with cp debian/.gencontrol_stamp debian/control
267         diff -Nu debian/control debian/.gencontrol_stamp
268         @:>$@
269
270 build: build-arch build-indep
271 build-arch: debian/.build_stamp
272 build-indep: debian/.configure_stamp
273
274 clean: debian/.control_stamp
275         dh_testdir
276         -rm -f debian/.*_stamp
277         -rm -rf builddir
278         # unapply autotools-dev if it exists
279         for x in config.guess config.sub; do \
280                 test -e /usr/share/misc/$$x || continue; \
281                 rm -f $$x; \
282         done
283         dh_clean
284
285 binary-indep: build-indep
286         dh_testdir
287         dh_testroot
288         if test -x "$$(which dh_prep)"; then dh_prep -i; else dh_clean -i -k; fi
289         dh_installchangelogs -i -Njupp
290         dh_install -i
291         dh_installmenu -i
292         # will be replaced by links to jupp
293         rm -rf debian/joe-jupp/usr/share/doc/joe-jupp
294         dh_link -i
295         dh_fixperms -i
296         dh_installdeb -i
297         dh_gencontrol -i
298         dh_md5sums -i
299         dh_builddeb -i
300
301 # no -Njupp because that's the only arch:any package
302 binary-arch: build-arch
303         dh_testdir
304         dh_testroot
305         if test -x "$$(which dh_prep)"; then dh_prep -a; else dh_clean -a -k; fi
306         dh_installchangelogs -pjupp -k NEWS
307         dh_installdocs -a
308         dh_install -a
309         dh_installmenu -a
310         dh_installman -a
311         dh_link -a
312         dh_strip -a
313         dh_compress -a
314         dh_fixperms -a
315         dh_installdeb -a
316         dh_shlibdeps -a
317         dh_gencontrol -a
318         dh_md5sums -a
319         dh_builddeb -a
320
321 binary: binary-arch binary-indep
322
323 .PHONY: binary binary-arch binary-indep build build-arch build-indep clean
324
325 ifneq (,$(strip ${___DISPLAY_MAKEVARS}))
326 $(foreach var,${___DISPLAY_MAKEVARS},$(info $(strip ${var})=${$(strip ${var})}))
327 $(error ___DISPLAY_MAKEVARS finished for ${___DISPLAY_MAKEVARS})
328 endif