autobuild (15323B)
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 # We need to set PKG_CONFIG_PATH manually so configure can 366 # find these dependencies. I don't know why (one possible 367 # explanation has to do with 'keg-only' installs). If you do, 368 # please update: 369 # https://github.com/vedang/pdf-tools/issues/270 370 export PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:$(brew --prefix libffi)/lib/pkgconfig/:$(brew --prefix zlib)/lib/pkgconfig/:$(brew --prefix glib)/lib/pkgconfig/" 371 elif which port >/dev/null 2>&1; then 372 PKGCMD=port 373 PKGARGS=install 374 PACKAGES="pkgconfig poppler autoconf automake libpng" 375 else 376 return 1 377 fi 378 return 0 379 } 380 381 # NixOS 382 os_nixos() { 383 # Already in the nix-shell. 384 if [ -n "$AUTOBUILD_NIX_SHELL" ]; then 385 return 0 386 fi 387 if ! which nix-shell >/dev/null 2>&1; then 388 return 1 389 fi 390 if [ -n "$DRY_RUN" ]; then 391 return 0 392 fi 393 if ! nix-instantiate --eval -E "<nixpkgs>" &>/dev/null; then 394 echo "File 'nixpkgs' was not found in the Nix path. Using NixOS/nixpkgs" 395 NIX_PATH="nixpkgs=https://github.com/nixos/nixpkgs/archive/master.tar.gz:$NIX_PATH" 396 fi 397 command="AUTOBUILD_NIX_SHELL=true" 398 command="$command;export AUTOBUILD_NIX_SHELL" 399 command="$command;$(quote "$0" "$@")" 400 exec nix-shell --pure --run "$command" \ 401 -p automake autoconf pkg-config libpng zlib poppler 402 } 403 404 # Gentoo 405 os_gentoo() { 406 if ! [ -e "/etc/gentoo-release" ]; then 407 return 1 408 fi 409 PKGCMD=emerge 410 PKGARGS=--noreplace 411 PACKAGES="app-text/poppler 412 dev-util/pkgconf 413 media-libs/libpng 414 dev-build/autoconf 415 dev-build/automake 416 sys-devel/gcc 417 dev-build/make 418 sys-libs/glibc 419 sys-libs/zlib" 420 return 0 421 } 422 423 # Void 424 os_void() { 425 if [ -f "/etc/os-release" ]; then 426 . /etc/os-release 427 if [ "$ID" != "void" ]; then 428 return 1 429 fi 430 else 431 return 1 432 fi 433 PACKAGES="autoconf 434 automake 435 libpng-devel 436 poppler-devel 437 poppler-glib-devel 438 zlib-devel 439 make 440 pkgconf 441 cairo-devel" 442 PKGCMD=xbps-install 443 PKGARGS="-Sy" 444 export PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:/usr/lib/pkgconfig" 445 return 0 446 } 447 448 # openSUSE (TODO: add support for micro versions) 449 os_opensuse() { 450 if [ -f "/etc/os-release" ]; then 451 . /etc/os-release 452 if [ "$ID" != "opensuse-leap" ] && [ "$ID" != "opensuse-tumbleweed" ]; then 453 return 1 454 fi 455 else 456 return 1 457 fi 458 PACKAGES="make 459 automake 460 autoconf 461 gcc 462 libpng16-devel 463 libpng16-compat-devel 464 zlib-devel 465 libpoppler-devel 466 libpoppler-glib-devel 467 glib2-devel 468 pkgconf" 469 PKGCMD=zypper 470 PKGARGS="install" 471 return 0 472 } 473 474 # Alpine Linux 475 os_alpine() { 476 if [ -f "/etc/os-release" ]; then 477 . /etc/os-release 478 if [ "$ID" != "alpine" ]; then 479 return 1 480 fi 481 else 482 return 1 483 fi 484 PACKAGES="autoconf 485 automake 486 libpng-dev 487 poppler-dev 488 glib-dev 489 gcc 490 build-base" 491 PKGCMD=apk 492 PKGARGS="add" 493 return 0 494 } 495 496 # By Parameter --os 497 os_argument() { 498 [ -z "$OS" ] && return 1 499 case $OS in 500 macos) os_macos "$@";; 501 freebsd) os_freebsd "$@";; 502 arch) os_arch "$@";; 503 centos) os_centos "$@";; 504 netbsd) os_netbsd "$@";; 505 openbsd) os_openbsd "$@";; 506 fedora) os_fedora "$@";; 507 debian) os_debian "$@";; 508 gentoo) os_gentoo "$@";; 509 msys2) os_msys2 "$@";; 510 nixos) os_nixos "$@";; 511 void) os_void "$@";; 512 opensuse) os_opensuse "$@";; 513 alpine) os_alpine "$@";; 514 *) echo "Invalid --os argument: $OS" 515 exit 1 516 esac || { 517 echo "Unable to install on this system as $OS" 518 exit 1 519 } 520 } 521 522 ## +-----------------------------------------------------------+ 523 ## * Figure out where we are 524 ## ** install deps and build the program 525 ## +-----------------------------------------------------------+ 526 527 handle_options "$@" 528 529 os_argument "$@" || \ 530 os_macos "$@" || \ 531 os_freebsd "$@" || \ 532 os_arch "$@" || \ 533 os_centos "$@" || \ 534 os_netbsd "$@" || \ 535 os_openbsd "$@" || \ 536 os_fedora "$@" || \ 537 os_debian "$@" || \ 538 os_gentoo "$@" || \ 539 os_msys2 "$@" || \ 540 os_nixos "$@" || \ 541 os_void "$@" || \ 542 os_opensuse "$@" || \ 543 os_alpine "$@" || \ 544 { 545 OS_IS_HANDLED= 546 if [ -z "$DRY_RUN" ]; then 547 echo "Failed to recognize this system, trying to continue." 548 fi 549 } 550 551 if [ -n "$DRY_RUN" ]; then 552 [ -n "$OS_IS_HANDLED" ] 553 exit $? 554 fi 555 556 if [ -n "$PKGCMD" ];then 557 echo "---------------------------" 558 echo " Installing packages " 559 echo "---------------------------" 560 if [ -n "$PKG_INSTALL_SKIP" ]; then 561 echo "Skipping package installation (as requested)" 562 elif [ -z "$PKG_INSTALL_FORCE" ] && have_packages_installed; then 563 echo "Skipping package installation (already installed)" 564 else 565 assert_program "$PKGCMD" 566 echo "$PKGCMD $PKGARGS $PACKAGES" 567 if [ -n "$PKG_INSTALL_AS_ROOT" ]; then 568 exec_privileged $PKGCMD $PKGARGS $PACKAGES 569 else 570 $PKGCMD $PKGARGS $PACKAGES 571 fi 572 fi 573 echo 574 fi 575 576 # Try to be in the correct directory. 577 if which dirname >/dev/null 2>&1; then 578 cd "$(dirname "$0")" || { 579 echo "Failed to change into the source directory" 580 exit 1 581 } 582 fi 583 584 echo "---------------------------" 585 echo " Configuring and compiling " 586 echo "---------------------------" 587 588 # Create the configure script. 589 if ! [ -f ./configure ]; then 590 assert_program autoreconf 591 echo "autoreconf -i" 592 autoreconf -i 593 [ -f ./configure ] || exit_fail 594 fi 595 596 # Build the program. 597 if [ -n "$INSTALL_DIR" ]; then 598 prefix=--bindir=$INSTALL_DIR 599 fi 600 601 echo "./configure -q $prefix && make clean && make -s" 602 eval "./configure -q $(quote "$prefix") && make clean && make -s || exit_fail" 603 echo 604 if [ -n "$INSTALL_DIR" ]; then 605 echo "---------------------------" 606 echo " Installing " 607 echo "---------------------------" 608 echo make -s install 609 if mkdir -p -- "$INSTALL_DIR" && [ -w "$INSTALL_DIR" ]; then 610 make install || exit_fail 611 else 612 exec_privileged make install || exit_fail 613 fi 614 # Copy dynamic libraries on windows. 615 if [ -f epdfinfo.exe ]; then 616 assert_program awk 617 assert_program ldd 618 for dll in $(ldd epdfinfo.exe | awk '/\/mingw/ {print $3}'); do 619 cp -u "$dll" "$INSTALL_DIR" 620 done 621 fi 622 echo 623 fi 624 exit_success 625 626 # Local Variables: 627 # compile-command: "shellcheck autobuild" 628 # End: