dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

autobuild (15128B)


      1 #!/usr/bin/env sh
      2 
      3 ##
      4 ## Installs package dependencies and builds the application.
      5 ##
      6 
      7 # Don't exit if some command fails.
      8 set +e
      9 # Disable file globbing.
     10 set -f
     11 
     12 # Boolean variables are true if non-empty and false otherwise.
     13 
     14 # Command to install packages.
     15 PKGCMD=
     16 # Args to pass to $PKGCMD.
     17 PKGARGS=
     18 # Required packages.
     19 PACKAGES=
     20 # Whether package installation requires root permissions.
     21 PKG_INSTALL_AS_ROOT=true
     22 # Whether to skip package installation altogether.
     23 PKG_INSTALL_SKIP=
     24 # Whether to force package installation, even if it does not seem
     25 # necessary.
     26 PKG_INSTALL_FORCE=
     27 # Only test if the OS is handled by this script.
     28 DRY_RUN=
     29 # If and where to install the program.
     30 INSTALL_DIR=
     31 # Whether we can install packages.
     32 OS_IS_HANDLED=true
     33 # Which OSs installer to use
     34 OS=
     35 
     36 ## +-----------------------------------------------------------+
     37 ## * Utility Functions
     38 ## +-----------------------------------------------------------+
     39 
     40 usage()
     41 {
     42     cat <<EOF
     43 usage:$(basename "$0") [--help|-n|-i DIR|[-d -D]|[--os OS]]
     44 
     45     -n       Don't do anything, but check if this OS is handled.
     46 
     47     -i DIR   Install the program in the given directory.
     48 
     49     -d       Force dependency installattion.
     50 
     51     -D       Skip dependency installattion.
     52 
     53     --os OS  Use the given OS's installer
     54 
     55     --help   Display this message.
     56 
     57 EOF
     58     exit "$1"
     59 }
     60 
     61 # Search for command $1 in PATH. Print its absolute filename.
     62 which()
     63 {
     64     if [ -z "$1" ]; then
     65         return 1
     66     fi
     67     command -v "$1"
     68 }
     69 
     70 # Quote $@ for the shell.
     71 quote()
     72 {
     73     quoted=
     74     for arg; do
     75         qarg=$(printf "%s" "$arg" | sed -e 's/[|&;<>()$\`"'\'' 	]/\\&/g')
     76         if [ -z "$quoted" ]; then
     77             quoted=$qarg
     78         else
     79             quoted="$quoted $qarg"
     80         fi
     81     done
     82     printf "%s" "$quoted"
     83 }
     84 
     85 # Attempt to exec $@ as root.
     86 exec_privileged() {
     87     if [ -z "$1" ]; then
     88         echo "internal error: command is empty"
     89         exit 2
     90     fi
     91     if [ -w / ]; then
     92         "$@"
     93     elif which sudo >/dev/null 2>&1; then
     94         sudo -- "$@"
     95         retval=$?
     96         sudo -k
     97         return $retval
     98     elif which su >/dev/null 2>&1; then
     99         su -c "$(quote "$@")"
    100     else
    101         echo "No such program: sudo or su"
    102         exit 1
    103     fi
    104 }
    105 
    106 # Test if $1 is in PATH or exit with a failure status.
    107 assert_program()
    108 {
    109     if ! which "$1" >/dev/null 2>&1; then
    110         echo "No such program: $1"
    111         exit 1
    112     fi
    113 }
    114 
    115 # Source filename $1 and echo variable $2.
    116 source_var()
    117 {
    118     if ! [ -f "$1" ] || ! [ -r "$1" ] || [ -z "$2" ]; then
    119         return 1
    120     fi
    121     # shellcheck source=/dev/null
    122     . "$1"
    123     eval "printf '%s\n' \$$2"
    124     return 0
    125 }
    126 
    127 exit_success()
    128 {
    129     echo "==========================="
    130     echo "   Build succeeded. :O)    "
    131     echo "==========================="
    132     exit 0
    133 }
    134 
    135 exit_fail()
    136 {
    137     echo "==========================="
    138     echo "     Build failed.  ;o(    "
    139     echo "==========================="
    140     if [ -z "$PKG_INSTALL_FORCE" ]; then
    141         echo "Note: maybe try the '-d' option."
    142     fi
    143     exit 1
    144 }
    145 
    146 # Return 0, if all required packages seem to be installed.
    147 have_packages_installed()
    148 {
    149     {
    150         which pkg-config || return 1
    151         if ! [ -f configure ];then
    152             which autoreconf || return 1
    153             which automake || return 1
    154         fi
    155         for lib in libpng glib-2.0 poppler poppler-glib zlib; do
    156             pkg-config --exists $lib || return 1
    157         done
    158         which make || return 1
    159         which gcc || which cc || return 1
    160         [ $? -eq 0 ] || return 1
    161         return 0
    162     } >/dev/null 2>&1
    163 }
    164 
    165 handle_options()
    166 {
    167     while [ $# -gt 0 ]; do
    168         case $1 in
    169             --help) usage 0;;
    170             -n) DRY_RUN=true;;
    171             -d) PKG_INSTALL_FORCE=true ;;
    172             -D) PKG_INSTALL_SKIP=true ;;
    173             -i)
    174                 shift
    175                 [ $# -gt 0 ] || usage 1
    176                 if [ "${1%%/}" != "${PWD%%/}" ]; then
    177                     INSTALL_DIR=$1
    178                 fi ;;
    179             --os)
    180                 shift
    181                 [ $# -gt 0 ] || usage 1
    182                 OS="$1"
    183                 ;;
    184             *) usage 1 ;;
    185         esac
    186         shift
    187     done
    188     if [ -n "$PKG_INSTALL_SKIP" ] && [ -n "$PKG_INSTALL_FORCE" ]; then
    189         usage 1
    190     fi
    191 }
    192 
    193 ## +-----------------------------------------------------------+
    194 ## * OS Functions
    195 ## +-----------------------------------------------------------+
    196 
    197 # Archlinux
    198 os_arch() {
    199     if ! [ -e "/etc/arch-release" ]; then
    200         return 1;
    201     fi
    202     PKGCMD=pacman
    203     PKGARGS="-S --needed"
    204     PACKAGES="base-devel libpng zlib poppler-glib"
    205     return 0;
    206 }
    207 
    208 # CentOS
    209 os_centos() {
    210     if ! [ -e "/etc/centos-release" ]; then
    211         return 1
    212     fi
    213     PKGCMD=yum
    214     if yum help install-n >/dev/null 2>&1; then
    215         PKGARGS=install-n
    216     else
    217         PKGARGS=install
    218     fi
    219     PACKAGES="autoconf
    220               automake
    221               gcc
    222               libpng-devel
    223               make
    224               pkgconfig
    225               poppler-devel
    226               poppler-glib-devel
    227               zlib-devel"
    228     return 0
    229 }
    230 
    231 # FreeBSD
    232 os_freebsd() {
    233     if ! which uname >/dev/null 2>&1 || [ "$(uname -s)" != "FreeBSD" ]; then
    234         return 1
    235     fi
    236     PKGCMD=pkg
    237     PKGARGS=install
    238     PACKAGES="autotools poppler-glib png pkgconf"
    239     return 0
    240 }
    241 
    242 # NetBSD
    243 os_netbsd() {
    244     if ! which uname >/dev/null 2>&1 || [ "$(uname -s)" != "NetBSD" ]; then
    245         return 1
    246     fi
    247     PKGCMD=pkgin
    248     PKGARGS=install
    249     PACKAGES="autoconf automake poppler-glib png pkgconf"
    250     return 0
    251 }
    252 
    253 # OpenBSD
    254 os_openbsd() {
    255     if ! which uname >/dev/null 2>&1 || [ "$(uname -s)" != "OpenBSD" ]; then
    256         return 1
    257     fi
    258     PKGCMD=pkg_add
    259     PKGARGS="-uU"
    260     PACKAGES="autoconf%2.69 automake%1.15 poppler poppler-utils png"
    261     export AUTOCONF_VERSION=2.69
    262     export AUTOMAKE_VERSION=1.15
    263     return 0
    264 }
    265 
    266 # Fedora
    267 os_fedora() {
    268     if ! [ -e "/etc/fedora-release" ]; then
    269         return 1
    270     fi
    271     PKGCMD=dnf
    272     PKGARGS=install
    273     PACKAGES="autoconf
    274               automake
    275               gcc
    276               libpng-devel
    277               make
    278               poppler-devel
    279               poppler-glib-devel
    280               zlib-devel"
    281     VERSION=$(source_var /etc/os-release VERSION_ID)
    282     if [ -n "$VERSION" ] && [ "$VERSION" -ge 26 ]; then
    283         PACKAGES="$PACKAGES pkgconf"
    284     else
    285         PACKAGES="$PACKAGES pkgconfig"
    286     fi
    287     return 0
    288 }
    289 
    290 # Debian/Ubuntu
    291 os_debian() {
    292     if ! [ -e "/etc/debian_version" ]; then
    293         return 1
    294     fi
    295     PACKAGES="autoconf
    296               automake
    297               gcc
    298               libpng-dev
    299               libpoppler-dev
    300               libpoppler-glib-dev
    301               libz-dev
    302               make
    303               pkg-config"
    304     PKGCMD=apt-get
    305     PKGARGS="install -y"
    306     return 0
    307 }
    308 
    309 # Msys2
    310 os_msys2() {
    311     if [ -z "$MSYSTEM" ] || ! [ -r "/etc/profile" ]; then
    312         return 1
    313     fi
    314     case $MSYSTEM in
    315         MINGW64)
    316             PACKAGES="base-devel
    317                       autoconf
    318                       automake
    319                       mingw-w64-x86_64-libpng
    320                       mingw-w64-x86_64-poppler
    321                       mingw-w64-x86_64-imagemagick
    322                       mingw-w64-x86_64-toolchain
    323                       mingw-w64-x86_64-openssl
    324                       mingw-w64-x86_64-zlib" ;;
    325         MINGW32)
    326             PACKAGES="base-devel
    327                       autoconf
    328                       automake
    329                       mingw-w64-i686-libpng
    330                       mingw-w64-i686-poppler
    331                       mingw-w64-i686-imagemagick
    332                       mingw-w64-i686-toolchain
    333                       mingw-w64-i686-openssl
    334                       mingw-w64-i686-zlib" ;;
    335         MSYS)
    336             case $(uname -m) in
    337                 x86_64)
    338                     MSYSTEM=MINGW64 ;;
    339                 *)
    340                     MSYSTEM=MINGW32 ;;
    341             esac
    342             export MSYSTEM
    343             # shellcheck source=/dev/null
    344             . /etc/profile
    345             eval "exec $(quote "$0" "$@")" ;;
    346         *)
    347             echo "Unrecognized MSYSTEM value: $MSYSTEM"
    348             exit 1 ;;
    349     esac
    350     PKGCMD=pacman
    351     PKGARGS="-S --needed"
    352     PKG_INSTALL_AS_ROOT=
    353     return 0
    354 }
    355 
    356 # MacOS
    357 os_macos() {
    358     if ! which uname >/dev/null 2>&1 || [ "$(uname -s)" != "Darwin" ]; then
    359         return 1
    360     elif which brew >/dev/null 2>&1; then
    361         PKGCMD=brew
    362         PKGARGS=install
    363         PACKAGES="pkg-config poppler autoconf automake"
    364         PKG_INSTALL_AS_ROOT=
    365         # brew installs libffi as keg-only, meaning we need to set
    366         # PKG_CONFIG_PATH manually so configure can find it
    367         export PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:$(brew --prefix libffi)/lib/pkgconfig/:$(brew --prefix zlib)/lib/pkgconfig/"
    368     elif which port >/dev/null 2>&1; then
    369         PKGCMD=port
    370         PKGARGS=install
    371         PACKAGES="pkgconfig poppler autoconf automake libpng"
    372     else
    373         return 1
    374     fi
    375     return 0
    376 }
    377 
    378 # NixOS
    379 os_nixos() {
    380     # Already in the nix-shell.
    381     if [ -n "$AUTOBUILD_NIX_SHELL" ]; then
    382         return 0
    383     fi
    384     if ! which nix-shell >/dev/null 2>&1; then
    385         return 1
    386     fi
    387     if [ -n "$DRY_RUN" ]; then
    388         return 0
    389     fi
    390     if ! nix-instantiate --eval -E "<nixpkgs>" &>/dev/null; then
    391       echo "File 'nixpkgs' was not found in the Nix path. Using NixOS/nixpkgs"
    392       NIX_PATH="nixpkgs=https://github.com/nixos/nixpkgs/archive/master.tar.gz:$NIX_PATH"
    393     fi
    394     command="AUTOBUILD_NIX_SHELL=true"
    395     command="$command;export AUTOBUILD_NIX_SHELL"
    396     command="$command;$(quote "$0" "$@")"
    397     exec nix-shell --pure --run "$command" \
    398          -p automake autoconf pkg-config libpng zlib poppler
    399 }
    400 
    401 # Gentoo
    402 os_gentoo() {
    403     if ! [ -e "/etc/gentoo-release" ]; then
    404         return 1
    405     fi
    406     PKGCMD=emerge
    407     PKGARGS=--noreplace
    408     PACKAGES="app-text/poppler
    409               dev-util/pkgconf
    410               media-libs/libpng
    411               sys-devel/autoconf
    412               sys-devel/automake
    413               sys-devel/gcc
    414               sys-devel/make
    415               sys-libs/glibc
    416               sys-libs/zlib"
    417     return 0
    418 }
    419 
    420 # Void
    421 os_void() {
    422     if [ -f "/etc/os-release" ]; then
    423         . /etc/os-release
    424         if [ "$ID" != "void" ]; then
    425           return 1
    426         fi
    427     else
    428       return 1
    429     fi
    430     PACKAGES="autoconf
    431               automake
    432               libpng-devel
    433               poppler-devel
    434               poppler-glib-devel
    435               zlib-devel
    436               make
    437               pkgconf
    438               cairo-devel"
    439     PKGCMD=xbps-install
    440     PKGARGS="-Sy"
    441     export PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:/usr/lib/pkgconfig"
    442     return 0
    443 }
    444 
    445 # openSUSE (TODO: add support for micro versions)
    446 os_opensuse() {
    447     if [ -f "/etc/os-release" ]; then
    448         . /etc/os-release
    449         if [ "$ID" != "opensuse-leap" ] && [ "$ID" != "opensuse-tumbleweed" ]; then
    450           return 1
    451         fi
    452     else
    453       return 1
    454     fi
    455     PACKAGES="make
    456               automake
    457               autoconf
    458               gcc
    459               libpng16-devel
    460               libpng16-compat-devel
    461               zlib-devel
    462               libpoppler-devel
    463               libpoppler-glib-devel
    464               glib2-devel
    465               pkgconf"
    466     PKGCMD=zypper
    467     PKGARGS="install"
    468     return 0
    469 }
    470 
    471 # Alpine Linux
    472 os_alpine() {
    473     if [ -f "/etc/os-release" ]; then
    474         . /etc/os-release
    475         if [ "$ID" != "alpine" ]; then
    476           return 1
    477         fi
    478     else
    479       return 1
    480     fi
    481     PACKAGES="autoconf
    482               automake
    483               libpng-dev
    484               poppler-dev
    485               glib-dev
    486               gcc
    487               build-base"
    488     PKGCMD=apk
    489     PKGARGS="add"
    490     return 0
    491 }
    492 
    493 # By Parameter --os
    494 os_argument() {
    495     [ -z "$OS" ] && return 1
    496     case $OS in
    497         macos)   os_macos   "$@";;
    498         freebsd) os_freebsd "$@";;
    499         arch)    os_arch    "$@";;
    500         centos)  os_centos  "$@";;
    501         netbsd)  os_netbsd  "$@";;
    502         openbsd) os_openbsd "$@";;
    503         fedora)  os_fedora  "$@";;
    504         debian)  os_debian  "$@";;
    505         gentoo)  os_gentoo  "$@";;
    506         msys2)   os_msys2   "$@";;
    507         nixos)   os_nixos   "$@";;
    508         void)    os_void    "$@";;
    509         opensuse) os_opensuse "$@";;
    510         alpine)  os_alpine  "$@";;
    511         *)       echo "Invalid --os argument: $OS"
    512                  exit 1
    513     esac || {
    514         echo "Unable to install on this system as $OS"
    515         exit 1
    516     }
    517 }
    518 
    519 ## +-----------------------------------------------------------+
    520 ## * Figure out where we are
    521 ## ** install deps and build the program
    522 ## +-----------------------------------------------------------+
    523 
    524 handle_options "$@"
    525 
    526 os_argument "$@" || \
    527 os_macos    "$@" || \
    528 os_freebsd  "$@" || \
    529 os_arch     "$@" || \
    530 os_centos   "$@" || \
    531 os_netbsd   "$@" || \
    532 os_openbsd  "$@" || \
    533 os_fedora   "$@" || \
    534 os_debian   "$@" || \
    535 os_gentoo   "$@" || \
    536 os_msys2    "$@" || \
    537 os_nixos    "$@" || \
    538 os_void     "$@" || \
    539 os_opensuse "$@" || \
    540 os_alpine   "$@" || \
    541 {
    542     OS_IS_HANDLED=
    543     if [ -z "$DRY_RUN" ]; then
    544         echo "Failed to recognize this system, trying to continue."
    545     fi
    546 }
    547 
    548 if [ -n "$DRY_RUN" ]; then
    549     [ -n "$OS_IS_HANDLED" ]
    550     exit $?
    551 fi
    552 
    553 if [ -n "$PKGCMD" ];then
    554     echo "---------------------------"
    555     echo "    Installing packages    "
    556     echo "---------------------------"
    557     if [ -n "$PKG_INSTALL_SKIP" ]; then
    558         echo "Skipping package installation (as requested)"
    559     elif [ -z "$PKG_INSTALL_FORCE" ] && have_packages_installed; then
    560         echo "Skipping package installation (already installed)"
    561     else
    562         assert_program "$PKGCMD"
    563         echo "$PKGCMD $PKGARGS $PACKAGES"
    564         if [ -n "$PKG_INSTALL_AS_ROOT" ]; then
    565             exec_privileged $PKGCMD $PKGARGS $PACKAGES
    566         else
    567             $PKGCMD $PKGARGS $PACKAGES
    568         fi
    569     fi
    570     echo
    571 fi
    572 
    573 # Try to be in the correct directory.
    574 if which dirname >/dev/null 2>&1; then
    575     cd "$(dirname "$0")" || {
    576         echo "Failed to change into the source directory"
    577         exit 1
    578     }
    579 fi
    580 
    581 echo "---------------------------"
    582 echo " Configuring and compiling "
    583 echo "---------------------------"
    584 
    585 # Create the configure script.
    586 if ! [ -f ./configure ]; then
    587     assert_program autoreconf
    588     echo "autoreconf -i"
    589     autoreconf -i
    590     [ -f ./configure ] || exit_fail
    591 fi
    592 
    593 # Build the program.
    594 if [ -n "$INSTALL_DIR" ]; then
    595     prefix=--bindir=$INSTALL_DIR
    596 fi
    597 
    598 echo "./configure -q $prefix && make clean && make -s"
    599 eval "./configure -q $(quote "$prefix") && make clean && make -s || exit_fail"
    600 echo
    601 if [ -n "$INSTALL_DIR" ]; then
    602     echo "---------------------------"
    603     echo "       Installing          "
    604     echo "---------------------------"
    605     echo make -s install
    606     if mkdir -p -- "$INSTALL_DIR" && [ -w "$INSTALL_DIR" ]; then
    607         make install || exit_fail
    608     else
    609         exec_privileged make install || exit_fail
    610     fi
    611     # Copy dynamic libraries on windows.
    612     if [ -f epdfinfo.exe ]; then
    613         assert_program awk
    614         assert_program ldd
    615         for dll in $(ldd epdfinfo.exe | awk '/\/mingw/ {print $3}'); do
    616             cp -u "$dll" "$INSTALL_DIR"
    617         done
    618     fi
    619     echo
    620 fi
    621 exit_success
    622 
    623 # Local Variables:
    624 # compile-command: "shellcheck autobuild"
    625 # End: