| 1 | #! /bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # |
| 3 | # A simple script that can be used to see if an environment was built |
| 4 | # successfully. Note that passing these test doesn't guarantee the environment |
| 5 | # will work, but failing them means it certainly won't. Note that this script |
| 6 | # may need to be executed with sudo if the current user doesn't have chroot |
| 7 | # permissions |
| 8 | # |
| 9 | # This program always exits with an error code of 2 so that it can be |
| 10 | # distinguished from a sudo error (with an exit code of 1) |
| 11 | # |
| 12 | # Copyright (C) 2018 Joshua Watt |
| 13 | # |
| 14 | # This program is free software; you can redistribute it and/or modify |
| 15 | # it under the terms of the GNU General Public License as published by |
| 16 | # the Free Software Foundation; either version 2 of the License, or |
| 17 | # (at your option) any later version. |
| 18 | # |
| 19 | # This program is distributed in the hope that it will be useful, |
| 20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | # GNU General Public License for more details. |
| 23 | # |
| 24 | # You should have received a copy of the GNU General Public License along |
| 25 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 26 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 27 | |
| 28 | # Exit on any unexpected failure |
| 29 | set -e |
| 30 | |
| 31 | TEST_DIR= |
| 32 | QUIET=false |
| 33 | REALPATH=$(whichscip-shell system . . which. realpath 2> /dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.) |
| 34 | |
| 35 | # Cleanup the temp directory on exit |
| 36 | cleanupscip-shell shell shell-project 0.0.0 cleanup().() { |
| 37 | if [scip-shell system . . `[`. -n "$TEST_DIR" ]; then |
| 38 | rmscip-shell system . . rm. -rf "$TEST_DIR" |
| 39 | fi |
| 40 | } |
| 41 | trap cleanup EXIT |
| 42 | |
| 43 | print_infoscip-shell shell shell-project 0.0.0 print_info().() { |
| 44 | if ! $QUIET; then |
| 45 | echoscip-shell system . . echo. "$@" |
| 46 | fi |
| 47 | } |
| 48 | |
| 49 | usagescip-shell shell shell-project 0.0.0 usage().() { |
| 50 | echoscip-shell system . . echo. "Usage: $(basenamescip-shell system . . basename. $0) [-h] TOOLCHAIN" |
| 51 | echoscip-shell system . . echo. " -h --help Show Help" |
| 52 | echoscip-shell system . . echo. " -q --quiet Only print errors" |
| 53 | echoscip-shell system . . echo. " TOOLCHAIN Toolchain archive to test" |
| 54 | echoscip-shell system . . echo. "" |
| 55 | echoscip-shell system . . echo. "Tests a toolchain environment to see if it is correctly constructed" |
| 56 | } |
| 57 | |
| 58 | OPTIONS=`getoptscip-shell system . . getopt. -o hqf --long help,quiet -n $(basenamescip-shell system . . basename. $0) -- "$@"` |
| 59 | eval set -- "$OPTIONS" |
| 60 | |
| 61 | while truescip-shell system . . true.; do |
| 62 | case "$1" in |
| 63 | -h|--help) |
| 64 | usagescip-shell shell shell-project 0.0.0 usage(). |
| 65 | exit 0 |
| 66 | ;; |
| 67 | -q|--quiet) |
| 68 | QUIET=true |
| 69 | shift |
| 70 | ;; |
| 71 | --) |
| 72 | shift |
| 73 | break |
| 74 | ;; |
| 75 | *) |
| 76 | echoscip-shell system . . echo. "Unknown option '$1'" |
| 77 | exit 2 |
| 78 | ;; |
| 79 | esac |
| 80 | done |
| 81 | |
| 82 | if [scip-shell system . . `[`. -z "$1" ]; then |
| 83 | echoscip-shell system . . echo. "Toolchain argument is required" |
| 84 | usagescip-shell shell shell-project 0.0.0 usage(). |
| 85 | exit 2 |
| 86 | fi |
| 87 | TEST_DIR=$(mktempscip-shell system . . mktemp. -d) |
| 88 | |
| 89 | if [scip-shell system . . `[`. -z "$REALPATH" ]; then |
| 90 | echoscip-shell system . . echo. "WARNING: realpath not found, symlink tests will be disabled" |
| 91 | fi |
| 92 | |
| 93 | # Extract the toolchain |
| 94 | tarscip-shell system . . tar. -xf "$1" -C "$TEST_DIR" |
| 95 | |
| 96 | # Determine the compiler |
| 97 | if [scip-shell system . . `[`. -e $TEST_DIR/usr/bin/clang ]; then |
| 98 | print_infoscip-shell shell shell-project 0.0.0 print_info(). "Compiler is clang" |
| 99 | IS_CLANG=true |
| 100 | else |
| 101 | print_infoscip-shell shell shell-project 0.0.0 print_info(). "Compiler is gcc" |
| 102 | IS_CLANG=false |
| 103 | fi |
| 104 | |
| 105 | check_programscip-shell shell shell-project 0.0.0 check_program().() { |
| 106 | local prog="$1" |
| 107 | shift |
| 108 | |
| 109 | cd $TEST_DIR |
| 110 | print_infoscip-shell shell shell-project 0.0.0 print_info(). "Checking $prog..." |
| 111 | if [scip-shell system . . `[`. ! -x "${TEST_DIR}${prog}" ]; then |
| 112 | echoscip-shell system . . echo. "$prog is missing or not executable" |
| 113 | exit 2 |
| 114 | fi |
| 115 | if [scip-shell system . . `[`. -n "$REALPATH" ]; then |
| 116 | local target="$($REALPATH "${TEST_DIR}${prog}")" |
| 117 | case $target in |
| 118 | "$($REALPATH "${TEST_DIR}")"/*) |
| 119 | ;; |
| 120 | *) |
| 121 | echoscip-shell system . . echo. "$prog is a symbolic link that points to '$target' outside the environment" |
| 122 | exit 2 |
| 123 | ;; |
| 124 | esac |
| 125 | fi |
| 126 | if ! chrootscip-shell system . . chroot. . $prog $@ < /dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 127 | echoscip-shell system . . echo. "$prog failed to execute" |
| 128 | exit 2 |
| 129 | fi |
| 130 | print_infoscip-shell shell shell-project 0.0.0 print_info(). "OK" |
| 131 | } |
| 132 | |
| 133 | check_programscip-shell shell shell-project 0.0.0 check_program(). /bin/truescip-shell filesystem . . `/bin/true`. |
| 134 | if $IS_CLANG; then |
| 135 | check_programscip-shell shell shell-project 0.0.0 check_program(). /usr/bin/clangscip-shell filesystem . . `/usr/bin/clang`. -xc -c -o test.o - |
| 136 | check_programscip-shell shell shell-project 0.0.0 check_program(). /usr/bin/asscip-shell filesystem . . `/usr/bin/as`. |
| 137 | # NOTE: The compilerwrapper programs /usr/bin/gcc and /usr/bin/g++ are not |
| 138 | # tested because they interfer with the automated testing when the |
| 139 | # address sanitizer is enabled |
| 140 | else |
| 141 | ARGS="-fpreprocessed" |
| 142 | check_programscip-shell shell shell-project 0.0.0 check_program(). /usr/bin/gccscip-shell filesystem . . `/usr/bin/gcc`. $ARGS -xc -c -o test.o - |
| 143 | check_programscip-shell shell shell-project 0.0.0 check_program(). /usr/bin/g++scip-shell filesystem . . `/usr/bin/g++`. $ARGS -xc++ -c -o test.o - |
| 144 | check_programscip-shell shell shell-project 0.0.0 check_program(). /usr/bin/cc1scip-shell filesystem . . `/usr/bin/cc1`. $ARGS -o test.o -quiet |
| 145 | check_programscip-shell shell shell-project 0.0.0 check_program(). /usr/bin/cc1plusscip-shell filesystem . . `/usr/bin/cc1plus`. $ARGS -o test.o -quiet |
| 146 | check_programscip-shell shell shell-project 0.0.0 check_program(). /usr/bin/asscip-shell filesystem . . `/usr/bin/as`. |
| 147 | fi |