]> granicus.if.org Git - sudo/blob - mkpkg
Add SPDX-License-Identifier to files.
[sudo] / mkpkg
1 #!/bin/sh
2 #
3 # SPDX-License-Identifier: ISC
4 #
5 # Copyright (c) 2010-2018 Todd C. Miller <Todd.Miller@sudo.ws>
6 #
7 # Permission to use, copy, modify, and distribute this software for any
8 # purpose with or without fee is hereby granted, provided that the above
9 # copyright notice and this permission notice appear in all copies.
10 #
11 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #
19 # Build a binary package using polypkg
20 # Usage: mkpkg [--debug] [--flavor flavor] [--platform platform] [--osversion ver]
21 #
22
23 # Make sure IFS is set to space, tab, newline in that order.
24 space=' '
25 tab='   '
26 nl='
27 '
28 IFS="   $nl"
29
30 # Parse arguments
31 usage="usage: mkpkg [--debug] [--flavor flavor] [--platform platform] [--osversion ver]"
32 debug=0
33 flavor=vanilla
34 crossbuild=false
35 while test $# -gt 0; do
36     case "$1" in
37         --debug)
38             set -x
39             debug=1
40             PPFLAGS="--debug${PPFLAGS+$space}${PPFLAGS}"
41             ;;
42         --flavor=?*)
43             flavor=`echo "$1" | sed -n 's/^--flavor=\(.*\)/\1/p'`
44             PPVARS="${PPVARS}${PPVARS+$space}flavor=$flavor"
45             ;;
46         --flavor)
47             if [ $# -lt 2 ]; then
48                 echo "$usage" 1>&2
49                 exit 1
50             fi
51             flavor="$2"
52             PPVARS="${PPVARS}${PPVARS+$space}flavor=$flavor"
53             shift
54             ;;
55         --platform=?*)
56             arg=`echo "$1" | sed -n 's/^--platform=\(.*\)/\1/p'`
57             PPFLAGS="${PPFLAGS}${PPFLAGS+$space}--platform $arg"
58             ;;
59         --platform)
60             if [ $# -lt 2 ]; then
61                 echo "$usage" 1>&2
62                 exit 1
63             fi
64             PPFLAGS="${PPFLAGS}${PPFLAGS+$space}--platform $2"
65             shift
66             ;;
67         --osversion=?*)
68             arg=`echo "$1" | sed -n 's/^--osversion=\(.*\)/\1/p'`
69             osversion="$arg"
70             ;;
71         --osversion)
72             if [ $# -lt 2 ]; then
73                 echo "$usage" 1>&2
74                 exit 1
75             fi
76             osversion="$2"
77             shift
78             ;;
79         --build|--host)
80             crossbuild=true
81             configure_opts="${configure_opts}${configure_opts+$tab}$1"
82             ;;
83         *)
84             # Pass unknown options to configure
85             configure_opts="${configure_opts}${configure_opts+$tab}$1"
86             ;;
87     esac
88     shift
89 done
90
91 top_srcdir=`dirname $0`
92
93 : ${osversion="`$top_srcdir/pp --probe`"}
94 test -n "$osversion" || exit 1
95 osrelease=`echo "$osversion" | sed -e 's/^[^0-9]*//' -e 's/-.*$//'`
96 : ${MAKE=make}
97
98 # If using GNU make, set number of jobs
99 if ${MAKE} --version 2>&1 | grep GNU >/dev/null; then
100     NJOBS=0
101     case "`uname`" in
102         Darwin)
103             # macOS
104             NJOBS=`sysctl -n hw.ncpu`
105             ;;
106         Linux)
107             if [ -r /proc/cpuinfo ]; then
108                 for c in `sed -n 's/^cpu cores[  ]*: *//p' /proc/cpuinfo`; do
109                     NJOBS=`expr $NJOBS + $c`
110                 done
111             fi
112             ;;
113         SunOS)
114             # Solaris
115             if [ -x /usr/sbin/psrinfo ]; then
116                 NJOBS=`/usr/sbin/psrinfo | wc -l`
117             fi
118             ;;
119         HP-UX)
120             NJOBS=`sar -Mu 1 1 | awk 'END {print NR-5}'`
121             ;;
122         AIX)
123             NJOBS=`bindprocessor -q | awk '{print NF-4}'`
124             ;;
125     esac
126     if [ $NJOBS -gt 1 ]; then
127         make_opts="-j$NJOBS"
128     fi
129 fi
130
131 # Choose compiler options by osversion if not cross-compiling.
132 if [ "$crossbuild" = "false" ]; then
133     case "$osversion" in
134         macos*)
135             # Use clang on macOS if present
136             if [ -z "$CC" -a -x /usr/bin/clang ]; then
137                 CC=/usr/bin/clang; export CC
138             fi
139             ;;
140         sol[0-9]*)
141             # Use the Sun Studio C compiler on Solaris if possible
142             if [ -z "$CC" -a -x /usr/bin/cc ]; then
143                 CC=/usr/bin/cc; export CC
144                 if [ -z "$CFLAGS" ]; then
145                     CFLAGS=-O; export CFLAGS
146                 fi
147             fi
148             ;;
149     esac
150 fi
151
152 # Give configure a hint that we are building a package.
153 # Some libc functions are only available on certain OS revisions.
154 configure_opts="${configure_opts}${configure_opts+$tab}--enable-package-build"
155
156 # Choose configure options by osversion.
157 # We use the same configure options as vendor packages when possible.
158 case "$osversion" in
159     centos*|rhel*|f[0-9]*)
160         case "$osversion" in
161             centos*|rhel*)
162                 osmajor=`sed -n -e 's/^.*release \([0-9]*\)[^0-9].*$/\1/p' /etc/redhat-release`
163                 if [ $osmajor -ge 4 ]; then
164                     # RHEL 4 and up support SELinux
165                     with_selinux=true
166                     if [ $osmajor -ge 5 ]; then
167                         # RHEL 5 and up has audit support and uses a
168                         # separate PAM config file for "sudo -i".
169                         with_linux_audit=true
170                         with_pam_login=true
171                         if [ $osmajor -ge 6 ]; then
172                             # RHEL 6 and above builds sudo with SSSD support
173                             with_sssd=true
174                             # RHEL 6 and above use /etc/sudo-ldap.conf
175                             with_sudo_ldap_conf=true
176                         fi
177                     fi
178                 fi
179                 ;;
180             f[0-9]*)
181                 # XXX - investigate which features were in which fedora version
182                 with_selinux=true
183                 with_linux_audit=true
184                 with_pam_login=true
185                 with_sssd=true
186                 ;;
187         esac
188
189         if [ X"$with_selinux" = X"true" ]; then
190             configure_opts="${configure_opts}${configure_opts+$tab}--with-selinux"
191         fi
192         if [ X"$with_linux_audit" = X"true" ]; then
193             configure_opts="${configure_opts}${configure_opts+$tab}--with-linux-audit"
194             PPVARS="${PPVARS}${PPVARS+$space}linux_audit=1.4.0"
195         fi
196         if [ X"$with_pam_login" = X"true" ]; then
197             configure_opts="${configure_opts}${configure_opts+$tab}--with-pam-login"
198         fi
199         if [ X"$with_sssd" = X"true" ]; then
200             configure_opts="${configure_opts}${configure_opts+$tab}--with-sssd"
201             if test "`getconf LONG_BIT`" = "64"; then
202                 # SSSD backend needs to know where to find the sssd lib
203                 configure_opts="${configure_opts}${configure_opts+$tab}--with-sssd-lib=/usr/lib64"
204             fi
205         fi
206         if [ X"$with_sudo_ldap_conf" = X"true" ]; then
207             configure_opts="${configure_opts}${configure_opts+$tab}--with-ldap-conf-file=/etc/sudo-ldap.conf"
208         fi
209         # Note, must indent with tabs, not spaces due to IFS trickery
210         configure_opts="--prefix=/usr
211                 --with-logging=syslog
212                 --with-logfac=authpriv
213                 --with-pam
214                 --enable-zlib=system
215                 --with-editor=/bin/vi
216                 --with-env-editor
217                 --with-ignore-dot
218                 --with-tty-tickets
219                 --with-ldap
220                 --with-passprompt=[sudo] password for %p: 
221                 --with-sendmail=/usr/sbin/sendmail
222                 $configure_opts"
223         ;;
224     sles*)
225         if [ $osrelease -ge 10 ]; then
226             # SLES 11 and higher has SELinux
227             if [ $osrelease -ge 11 ]; then
228                 configure_opts="${configure_opts}${configure_opts+$tab}--with-selinux"
229             fi
230         fi
231         # SuSE doesn't have /usr/libexec
232         libexec=lib
233         case "$osversion" in
234             *64*)       gcc -v 2>&1 | grep "with-cpu=[^ ]*32" >/dev/null || libexec=lib64
235                         ;;
236         esac
237         # Note, must indent with tabs, not spaces due to IFS trickery
238         # XXX - SuSE uses secure path but only for env_reset
239         configure_opts="--prefix=/usr
240                 --libexecdir=/usr/$libexec
241                 --with-logging=syslog
242                 --with-logfac=auth
243                 --with-all-insults
244                 --with-ignore-dot
245                 --with-tty-tickets
246                 --enable-shell-sets-home
247                 --with-sudoers-mode=0440
248                 --with-pam
249                 --enable-zlib=system
250                 --with-ldap
251                 --with-env-editor
252                 --with-passprompt=%p\'s password: 
253                 --with-sendmail=/usr/sbin/sendmail
254                 $configure_opts"
255
256         make_opts="${make_opts}${make_opts+ }"'docdir=$(datarootdir)/doc/packages/$(PACKAGE_TARNAME)'
257         ;;
258     deb*|ubu*)
259         # Man pages should be compressed in .deb files
260         export MANCOMPRESS='gzip -9'
261         export MANCOMPRESSEXT='.gz'
262         # If Ubuntu, add --enable-admin-flag
263         case "$osversion" in
264             ubu*)
265                 configure_opts="${configure_opts}${configure_opts+$tab}--enable-admin-flag${tab}--without-lecture"
266                 ;;
267         esac
268         # Newer Debian uses arch-specific lib dirs
269         MULTIARCH=`dpkg-architecture -qDEB_HOST_MULTIARCH 2>/dev/null`
270         # Note, must indent with tabs, not spaces due to IFS trickery
271         if test "$flavor" = "ldap"; then
272             configure_opts="${configure_opts}${configure_opts+$tab}--with-ldap
273                 --with-ldap-conf-file=/etc/sudo-ldap.conf"
274         else
275             configure_opts="${configure_opts}${configure_opts+$tab}--with-sssd"
276             if test -n "$MULTIARCH"; then
277                 # SSSD backend needs to know where to find the sssd lib
278                 configure_opts="${configure_opts}${configure_opts+$tab}--with-sssd-lib=/usr/lib/$MULTIARCH"
279             fi
280         fi
281         configure_opts="--prefix=/usr
282                 --with-all-insults
283                 --with-pam
284                 --enable-zlib=system
285                 --with-fqdn
286                 --with-logging=syslog
287                 --with-logfac=authpriv
288                 --with-env-editor
289                 --with-editor=/usr/bin/editor
290                 --with-timeout=15
291                 --with-password-timeout=0
292                 --with-passprompt=[sudo] password for %p: 
293                 --disable-root-mailer
294                 --with-sendmail=/usr/sbin/sendmail
295                 --mandir=/usr/share/man
296                 --libexecdir=/usr/lib
297                 --with-selinux
298                 --with-linux-audit
299                 $configure_opts"
300         # Use correct libaudit dependency
301         for f in /lib/${MULTIARCH}${MULTIARCH:+/}libaudit.so.[0-9]* /lib/libaudit.so.[0-9]*; do
302             if test -f "$f"; then
303                 linux_audit=`dpkg-query -S "$f" 2>/dev/null | sed -n 's/:.*//p'`
304                 test -n "$linux_audit" && break;
305             fi
306         done
307         if [ -z "linux_audit" ]; then
308             echo "unable to determine package for libaudit" 1>&2
309             exit 1
310         fi
311         PPVARS="${PPVARS}${PPVARS+$space}linux_audit=$linux_audit"
312         ;;
313     macos*)
314         case "$osversion" in
315             macos10[0-6]-i386|macos10[0-6]-x86_64)
316                 # Build intel universal binaries for 10.6 and below
317                 ARCH_FLAGS="-arch i386 -arch x86_64"
318                 ;;
319         esac
320         if test "${osversion}" != "`$top_srcdir/pp --probe`"; then
321             sdkvers=`echo "${osversion}" | sed 's/^macos\([0-9][0-9]\)\([0-9]*\)-.*$/\1.\2/'`
322             # Newer Xcode puts /Developer under the app Contents dir.
323             SDK_DIR="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs"
324             if test -d "${SDK_DIR}/MacOSX${sdkvers}.sdk"; then
325                 SDK_DIR="${SDK_DIR}/MacOSX${sdkvers}.sdk"
326             elif test -d "/Developer/SDKs/MacOSX${sdkvers}.sdk"; then
327                 SDK_DIR="/Developer/SDKs/MacOSX${sdkvers}.sdk"
328             fi
329             SDK_FLAGS="-isysroot ${SDK_DIR} -mmacosx-version-min=${sdkvers}"
330         fi
331         export CFLAGS="-O2 -g $ARCH_FLAGS $SDK_FLAGS"
332         export LDFLAGS="$ARCH_FLAGS $SDK_FLAGS"
333         # Note, must indent with tabs, not spaces due to IFS trickery
334         configure_opts="--with-pam
335                 --with-bsm-audit
336                 --without-tty-tickets
337                 --enable-zlib=system
338                 --with-ldap
339                 --with-insults=disabled
340                 --with-logging=syslog
341                 --with-logfac=authpriv
342                 --with-editor=/usr/bin/vim
343                 --with-env-editor
344                 $configure_opts"
345         ;;
346     aix*)
347         # Use -gxcoff with gcc instead of -g for dbx-style debugging symbols.
348         if test -z "$CC" && gcc -v >/dev/null 2>&1; then
349             CFLAGS=-gxcoff; export CFLAGS
350         fi
351         # Note, must indent with tabs, not spaces due to IFS trickery
352         # Note: we include our own zlib instead of relying on the
353         #       AIX freeware version being installed.
354         configure_opts="
355                 --prefix=/opt/freeware
356                 --mandir=/opt/freeware/man
357                 --with-insults=disabled
358                 --with-logging=syslog
359                 --with-logfac=auth
360                 --with-editor=/usr/bin/vi
361                 --with-env-editor
362                 --enable-zlib=builtin
363                 --disable-nls
364                 --with-sendmail=/usr/sbin/sendmail
365                 $configure_opts"
366         PPVARS="${PPVARS}${PPVARS+$space}aix_freeware=true"
367         ;;
368     *)
369         # For Solaris, add project support and use let configure choose zlib.
370         # For all others, use the builtin zlib and disable NLS support.
371         case "$osversion" in
372             sol*)
373                 configure_opts="${configure_opts}${configure_opts+$tab}--with-project"
374
375                 if [ $osrelease -ge 11 ]; then
376                     configure_opts="${configure_opts}${configure_opts+$tab}--with-bsm-audit"
377                 fi
378                 ;;
379             *)
380                 configure_opts="${configure_opts}${configure_opts+$tab}--enable-zlib=builtin${tab}--disable-nls"
381                 ;;
382         esac
383         if test "$flavor" = "ldap"; then
384             configure_opts="${configure_opts}${configure_opts+$tab}--with-ldap"
385         fi
386         # Note, must indent with tabs, not spaces due to IFS trickery
387         configure_opts="
388                 --with-insults=disabled
389                 --with-logging=syslog
390                 --with-logfac=auth
391                 --with-editor=/usr/bin/vim:/usr/bin/vi:/bin/vi
392                 --with-env-editor
393                 $configure_opts"
394         ;;
395 esac
396
397 # The postinstall script will create tmpfiles.d/sudo.conf for us
398 configure_opts="${configure_opts}${configure_opts+$tab}--disable-tmpfiles.d"
399
400 # Remove spaces from IFS when setting $@ so that passprompt may include them
401 OIFS="$IFS"
402 IFS="   $nl"
403 set -- $configure_opts $extra_opts
404 IFS="$OIFS"
405 if [ -r Makefile ]; then
406     ${MAKE} $make_opts distclean
407 fi
408 $top_srcdir/configure "$@" || exit 1
409 ${MAKE} $make_opts && ${MAKE} $make_opts PPFLAGS="$PPFLAGS" PPVARS="$PPVARS" package
410 test $debug -eq 0 && rm -rf destdir