File: autobuild

package info (click to toggle)
emacs-pdf-tools 1.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,792 kB
  • sloc: ansic: 11,326; lisp: 10,795; sh: 539; makefile: 167
file content (625 lines) | stat: -rwxr-xr-x 15,128 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#!/usr/bin/env sh

##
## Installs package dependencies and builds the application.
##

# Don't exit if some command fails.
set +e
# Disable file globbing.
set -f

# Boolean variables are true if non-empty and false otherwise.

# Command to install packages.
PKGCMD=
# Args to pass to $PKGCMD.
PKGARGS=
# Required packages.
PACKAGES=
# Whether package installation requires root permissions.
PKG_INSTALL_AS_ROOT=true
# Whether to skip package installation altogether.
PKG_INSTALL_SKIP=
# Whether to force package installation, even if it does not seem
# necessary.
PKG_INSTALL_FORCE=
# Only test if the OS is handled by this script.
DRY_RUN=
# If and where to install the program.
INSTALL_DIR=
# Whether we can install packages.
OS_IS_HANDLED=true
# Which OSs installer to use
OS=

## +-----------------------------------------------------------+
## * Utility Functions
## +-----------------------------------------------------------+

usage()
{
    cat <<EOF
usage:$(basename "$0") [--help|-n|-i DIR|[-d -D]|[--os OS]]

    -n       Don't do anything, but check if this OS is handled.

    -i DIR   Install the program in the given directory.

    -d       Force dependency installattion.

    -D       Skip dependency installattion.

    --os OS  Use the given OS's installer

    --help   Display this message.

EOF
    exit "$1"
}

# Search for command $1 in PATH. Print its absolute filename.
which()
{
    if [ -z "$1" ]; then
        return 1
    fi
    command -v "$1"
}

# Quote $@ for the shell.
quote()
{
    quoted=
    for arg; do
        qarg=$(printf "%s" "$arg" | sed -e 's/[|&;<>()$\`"'\'' 	]/\\&/g')
        if [ -z "$quoted" ]; then
            quoted=$qarg
        else
            quoted="$quoted $qarg"
        fi
    done
    printf "%s" "$quoted"
}

# Attempt to exec $@ as root.
exec_privileged() {
    if [ -z "$1" ]; then
        echo "internal error: command is empty"
        exit 2
    fi
    if [ -w / ]; then
        "$@"
    elif which sudo >/dev/null 2>&1; then
        sudo -- "$@"
        retval=$?
        sudo -k
        return $retval
    elif which su >/dev/null 2>&1; then
        su -c "$(quote "$@")"
    else
        echo "No such program: sudo or su"
        exit 1
    fi
}

# Test if $1 is in PATH or exit with a failure status.
assert_program()
{
    if ! which "$1" >/dev/null 2>&1; then
        echo "No such program: $1"
        exit 1
    fi
}

# Source filename $1 and echo variable $2.
source_var()
{
    if ! [ -f "$1" ] || ! [ -r "$1" ] || [ -z "$2" ]; then
        return 1
    fi
    # shellcheck source=/dev/null
    . "$1"
    eval "printf '%s\n' \$$2"
    return 0
}

exit_success()
{
    echo "==========================="
    echo "   Build succeeded. :O)    "
    echo "==========================="
    exit 0
}

exit_fail()
{
    echo "==========================="
    echo "     Build failed.  ;o(    "
    echo "==========================="
    if [ -z "$PKG_INSTALL_FORCE" ]; then
        echo "Note: maybe try the '-d' option."
    fi
    exit 1
}

# Return 0, if all required packages seem to be installed.
have_packages_installed()
{
    {
        which pkg-config || return 1
        if ! [ -f configure ];then
            which autoreconf || return 1
            which automake || return 1
        fi
        for lib in libpng glib-2.0 poppler poppler-glib zlib; do
            pkg-config --exists $lib || return 1
        done
        which make || return 1
        which gcc || which cc || return 1
        [ $? -eq 0 ] || return 1
        return 0
    } >/dev/null 2>&1
}

handle_options()
{
    while [ $# -gt 0 ]; do
        case $1 in
            --help) usage 0;;
            -n) DRY_RUN=true;;
            -d) PKG_INSTALL_FORCE=true ;;
            -D) PKG_INSTALL_SKIP=true ;;
            -i)
                shift
                [ $# -gt 0 ] || usage 1
                if [ "${1%%/}" != "${PWD%%/}" ]; then
                    INSTALL_DIR=$1
                fi ;;
            --os)
                shift
                [ $# -gt 0 ] || usage 1
                OS="$1"
                ;;
            *) usage 1 ;;
        esac
        shift
    done
    if [ -n "$PKG_INSTALL_SKIP" ] && [ -n "$PKG_INSTALL_FORCE" ]; then
        usage 1
    fi
}

## +-----------------------------------------------------------+
## * OS Functions
## +-----------------------------------------------------------+

# Archlinux
os_arch() {
    if ! [ -e "/etc/arch-release" ]; then
        return 1;
    fi
    PKGCMD=pacman
    PKGARGS="-S --needed"
    PACKAGES="base-devel libpng zlib poppler-glib"
    return 0;
}

# CentOS
os_centos() {
    if ! [ -e "/etc/centos-release" ]; then
        return 1
    fi
    PKGCMD=yum
    if yum help install-n >/dev/null 2>&1; then
        PKGARGS=install-n
    else
        PKGARGS=install
    fi
    PACKAGES="autoconf
              automake
              gcc
              libpng-devel
              make
              pkgconfig
              poppler-devel
              poppler-glib-devel
              zlib-devel"
    return 0
}

# FreeBSD
os_freebsd() {
    if ! which uname >/dev/null 2>&1 || [ "$(uname -s)" != "FreeBSD" ]; then
        return 1
    fi
    PKGCMD=pkg
    PKGARGS=install
    PACKAGES="autotools poppler-glib png pkgconf"
    return 0
}

# NetBSD
os_netbsd() {
    if ! which uname >/dev/null 2>&1 || [ "$(uname -s)" != "NetBSD" ]; then
        return 1
    fi
    PKGCMD=pkgin
    PKGARGS=install
    PACKAGES="autoconf automake poppler-glib png pkgconf"
    return 0
}

# OpenBSD
os_openbsd() {
    if ! which uname >/dev/null 2>&1 || [ "$(uname -s)" != "OpenBSD" ]; then
        return 1
    fi
    PKGCMD=pkg_add
    PKGARGS="-uU"
    PACKAGES="autoconf%2.69 automake%1.15 poppler poppler-utils png"
    export AUTOCONF_VERSION=2.69
    export AUTOMAKE_VERSION=1.15
    return 0
}

# Fedora
os_fedora() {
    if ! [ -e "/etc/fedora-release" ]; then
        return 1
    fi
    PKGCMD=dnf
    PKGARGS=install
    PACKAGES="autoconf
              automake
              gcc
              libpng-devel
              make
              poppler-devel
              poppler-glib-devel
              zlib-devel"
    VERSION=$(source_var /etc/os-release VERSION_ID)
    if [ -n "$VERSION" ] && [ "$VERSION" -ge 26 ]; then
        PACKAGES="$PACKAGES pkgconf"
    else
        PACKAGES="$PACKAGES pkgconfig"
    fi
    return 0
}

# Debian/Ubuntu
os_debian() {
    if ! [ -e "/etc/debian_version" ]; then
        return 1
    fi
    PACKAGES="autoconf
              automake
              gcc
              libpng-dev
              libpoppler-dev
              libpoppler-glib-dev
              libz-dev
              make
              pkg-config"
    PKGCMD=apt-get
    PKGARGS="install -y"
    return 0
}

# Msys2
os_msys2() {
    if [ -z "$MSYSTEM" ] || ! [ -r "/etc/profile" ]; then
        return 1
    fi
    case $MSYSTEM in
        MINGW64)
            PACKAGES="base-devel
                      autoconf
                      automake
                      mingw-w64-x86_64-libpng
                      mingw-w64-x86_64-poppler
                      mingw-w64-x86_64-imagemagick
                      mingw-w64-x86_64-toolchain
                      mingw-w64-x86_64-openssl
                      mingw-w64-x86_64-zlib" ;;
        MINGW32)
            PACKAGES="base-devel
                      autoconf
                      automake
                      mingw-w64-i686-libpng
                      mingw-w64-i686-poppler
                      mingw-w64-i686-imagemagick
                      mingw-w64-i686-toolchain
                      mingw-w64-i686-openssl
                      mingw-w64-i686-zlib" ;;
        MSYS)
            case $(uname -m) in
                x86_64)
                    MSYSTEM=MINGW64 ;;
                *)
                    MSYSTEM=MINGW32 ;;
            esac
            export MSYSTEM
            # shellcheck source=/dev/null
            . /etc/profile
            eval "exec $(quote "$0" "$@")" ;;
        *)
            echo "Unrecognized MSYSTEM value: $MSYSTEM"
            exit 1 ;;
    esac
    PKGCMD=pacman
    PKGARGS="-S --needed"
    PKG_INSTALL_AS_ROOT=
    return 0
}

# MacOS
os_macos() {
    if ! which uname >/dev/null 2>&1 || [ "$(uname -s)" != "Darwin" ]; then
        return 1
    elif which brew >/dev/null 2>&1; then
        PKGCMD=brew
        PKGARGS=install
        PACKAGES="pkg-config poppler autoconf automake"
        PKG_INSTALL_AS_ROOT=
        # brew installs libffi as keg-only, meaning we need to set
        # PKG_CONFIG_PATH manually so configure can find it
        export PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:$(brew --prefix libffi)/lib/pkgconfig/:$(brew --prefix zlib)/lib/pkgconfig/"
    elif which port >/dev/null 2>&1; then
        PKGCMD=port
        PKGARGS=install
        PACKAGES="pkgconfig poppler autoconf automake libpng"
    else
        return 1
    fi
    return 0
}

# NixOS
os_nixos() {
    # Already in the nix-shell.
    if [ -n "$AUTOBUILD_NIX_SHELL" ]; then
        return 0
    fi
    if ! which nix-shell >/dev/null 2>&1; then
        return 1
    fi
    if [ -n "$DRY_RUN" ]; then
        return 0
    fi
    if ! nix-instantiate --eval -E "<nixpkgs>" &>/dev/null; then
      echo "File 'nixpkgs' was not found in the Nix path. Using NixOS/nixpkgs"
      NIX_PATH="nixpkgs=https://github.com/nixos/nixpkgs/archive/master.tar.gz:$NIX_PATH"
    fi
    command="AUTOBUILD_NIX_SHELL=true"
    command="$command;export AUTOBUILD_NIX_SHELL"
    command="$command;$(quote "$0" "$@")"
    exec nix-shell --pure --run "$command" \
         -p automake autoconf pkg-config libpng zlib poppler
}

# Gentoo
os_gentoo() {
    if ! [ -e "/etc/gentoo-release" ]; then
        return 1
    fi
    PKGCMD=emerge
    PKGARGS=--noreplace
    PACKAGES="app-text/poppler
              dev-util/pkgconf
              media-libs/libpng
              sys-devel/autoconf
              sys-devel/automake
              sys-devel/gcc
              sys-devel/make
              sys-libs/glibc
              sys-libs/zlib"
    return 0
}

# Void
os_void() {
    if [ -f "/etc/os-release" ]; then
        . /etc/os-release
        if [ "$ID" != "void" ]; then
          return 1
        fi
    else
      return 1
    fi
    PACKAGES="autoconf
              automake
              libpng-devel
              poppler-devel
              poppler-glib-devel
              zlib-devel
              make
              pkgconf
              cairo-devel"
    PKGCMD=xbps-install
    PKGARGS="-Sy"
    export PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:/usr/lib/pkgconfig"
    return 0
}

# openSUSE (TODO: add support for micro versions)
os_opensuse() {
    if [ -f "/etc/os-release" ]; then
        . /etc/os-release
        if [ "$ID" != "opensuse-leap" ] && [ "$ID" != "opensuse-tumbleweed" ]; then
          return 1
        fi
    else
      return 1
    fi
    PACKAGES="make
              automake
              autoconf
              gcc
              libpng16-devel
              libpng16-compat-devel
              zlib-devel
              libpoppler-devel
              libpoppler-glib-devel
              glib2-devel
              pkgconf"
    PKGCMD=zypper
    PKGARGS="install"
    return 0
}

# Alpine Linux
os_alpine() {
    if [ -f "/etc/os-release" ]; then
        . /etc/os-release
        if [ "$ID" != "alpine" ]; then
          return 1
        fi
    else
      return 1
    fi
    PACKAGES="autoconf
              automake
              libpng-dev
              poppler-dev
              glib-dev
              gcc
              build-base"
    PKGCMD=apk
    PKGARGS="add"
    return 0
}

# By Parameter --os
os_argument() {
    [ -z "$OS" ] && return 1
    case $OS in
        macos)   os_macos   "$@";;
        freebsd) os_freebsd "$@";;
        arch)    os_arch    "$@";;
        centos)  os_centos  "$@";;
        netbsd)  os_netbsd  "$@";;
        openbsd) os_openbsd "$@";;
        fedora)  os_fedora  "$@";;
        debian)  os_debian  "$@";;
        gentoo)  os_gentoo  "$@";;
        msys2)   os_msys2   "$@";;
        nixos)   os_nixos   "$@";;
        void)    os_void    "$@";;
        opensuse) os_opensuse "$@";;
        alpine)  os_alpine  "$@";;
        *)       echo "Invalid --os argument: $OS"
                 exit 1
    esac || {
        echo "Unable to install on this system as $OS"
        exit 1
    }
}

## +-----------------------------------------------------------+
## * Figure out where we are
## ** install deps and build the program
## +-----------------------------------------------------------+

handle_options "$@"

os_argument "$@" || \
os_macos    "$@" || \
os_freebsd  "$@" || \
os_arch     "$@" || \
os_centos   "$@" || \
os_netbsd   "$@" || \
os_openbsd  "$@" || \
os_fedora   "$@" || \
os_debian   "$@" || \
os_gentoo   "$@" || \
os_msys2    "$@" || \
os_nixos    "$@" || \
os_void     "$@" || \
os_opensuse "$@" || \
os_alpine   "$@" || \
{
    OS_IS_HANDLED=
    if [ -z "$DRY_RUN" ]; then
        echo "Failed to recognize this system, trying to continue."
    fi
}

if [ -n "$DRY_RUN" ]; then
    [ -n "$OS_IS_HANDLED" ]
    exit $?
fi

if [ -n "$PKGCMD" ];then
    echo "---------------------------"
    echo "    Installing packages    "
    echo "---------------------------"
    if [ -n "$PKG_INSTALL_SKIP" ]; then
        echo "Skipping package installation (as requested)"
    elif [ -z "$PKG_INSTALL_FORCE" ] && have_packages_installed; then
        echo "Skipping package installation (already installed)"
    else
        assert_program "$PKGCMD"
        echo "$PKGCMD $PKGARGS $PACKAGES"
        if [ -n "$PKG_INSTALL_AS_ROOT" ]; then
            exec_privileged $PKGCMD $PKGARGS $PACKAGES
        else
            $PKGCMD $PKGARGS $PACKAGES
        fi
    fi
    echo
fi

# Try to be in the correct directory.
if which dirname >/dev/null 2>&1; then
    cd "$(dirname "$0")" || {
        echo "Failed to change into the source directory"
        exit 1
    }
fi

echo "---------------------------"
echo " Configuring and compiling "
echo "---------------------------"

# Create the configure script.
if ! [ -f ./configure ]; then
    assert_program autoreconf
    echo "autoreconf -i"
    autoreconf -i
    [ -f ./configure ] || exit_fail
fi

# Build the program.
if [ -n "$INSTALL_DIR" ]; then
    prefix=--bindir=$INSTALL_DIR
fi

echo "./configure -q $prefix && make clean && make -s"
eval "./configure -q $(quote "$prefix") && make clean && make -s || exit_fail"
echo
if [ -n "$INSTALL_DIR" ]; then
    echo "---------------------------"
    echo "       Installing          "
    echo "---------------------------"
    echo make -s install
    if mkdir -p -- "$INSTALL_DIR" && [ -w "$INSTALL_DIR" ]; then
        make install || exit_fail
    else
        exec_privileged make install || exit_fail
    fi
    # Copy dynamic libraries on windows.
    if [ -f epdfinfo.exe ]; then
        assert_program awk
        assert_program ldd
        for dll in $(ldd epdfinfo.exe | awk '/\/mingw/ {print $3}'); do
            cp -u "$dll" "$INSTALL_DIR"
        done
    fi
    echo
fi
exit_success

# Local Variables:
# compile-command: "shellcheck autobuild"
# End: