1#!/bin/bash 2 3# This has to be a separate file from scripts/make.sh so it can be called 4# before menuconfig. (It's called again from scripts/make.sh just to be sure.) 5 6mkdir -p generated 7 8source scripts/portability.sh 9 10probecc() 11{ 12 ${CROSS_COMPILE}${CC} $CFLAGS $LDFLAGS -xc -o /dev/null - "$@" 13} 14 15# Probe for a single config symbol with a "compiles or not" test. 16# Symbol name is first argument, flags second, feed C file to stdin 17probesymbol() 18{ 19 probecc "${@:2}" 2>/dev/null && DEFAULT=y || DEFAULT=n 20 rm a.out 2>/dev/null 21 echo -e "config $1\n\tbool" || exit 1 22 echo -e "\tdefault $DEFAULT\n" || exit 1 23} 24 25probeconfig() 26{ 27 > generated/cflags 28 # llvm produces its own really stupid warnings about things that aren't wrong, 29 # and although you can turn the warning off, gcc reacts badly to command line 30 # arguments it doesn't understand. So probe. 31 [ -z "$(probecc -Wno-string-plus-int <<< \#warn warn 2>&1 | grep string-plus-int)" ] && 32 echo -Wno-string-plus-int >> generated/cflags 33 34 # Probe for container support on target 35 probesymbol TOYBOX_CONTAINER << EOF 36 #include <stdio.h> 37 #include <sys/syscall.h> 38 #include <linux/sched.h> 39 int x=CLONE_NEWNS|CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET; 40 41 int main(int argc, char *argv[]){printf("%d", x+SYS_unshare+ SYS_setns);} 42EOF 43 44 probesymbol TOYBOX_FIFREEZE -c << EOF 45 #include <linux/fs.h> 46 #ifndef FIFREEZE 47 #error nope 48 #endif 49EOF 50 51 # Work around some uClibc limitations 52 probesymbol TOYBOX_ICONV -c << EOF 53 #include "iconv.h" 54EOF 55 56 # Android and some other platforms miss utmpx 57 probesymbol TOYBOX_UTMPX -c << EOF 58 #include <utmpx.h> 59 #ifndef BOOT_TIME 60 #error nope 61 #endif 62 int main(int argc, char *argv[]) { 63 struct utmpx *a; 64 if (0 != (a = getutxent())) return 0; 65 return 1; 66 } 67EOF 68 69 # Android is missing shadow.h 70 probesymbol TOYBOX_SHADOW -c << EOF 71 #include <shadow.h> 72 int main(int argc, char *argv[]) { 73 struct spwd *a = getspnam("root"); return 0; 74 } 75EOF 76 77 # Some commands are android-specific 78 probesymbol TOYBOX_ON_ANDROID -c << EOF 79 #ifndef __ANDROID__ 80 #error nope 81 #endif 82EOF 83 84 probesymbol TOYBOX_ANDROID_SCHEDPOLICY << EOF 85 #include <processgroup/sched_policy.h> 86 87 int main(int argc,char *argv[]) { get_sched_policy_name(0); } 88EOF 89 90 # nommu support 91 probesymbol TOYBOX_FORK << EOF 92 #include <unistd.h> 93 int main(int argc, char *argv[]) { return fork(); } 94EOF 95 echo -e '\tdepends on !TOYBOX_FORCE_NOMMU' 96 97 probesymbol TOYBOX_PRLIMIT << EOF 98 #include <sys/types.h> 99 #include <sys/time.h> 100 #include <sys/resource.h> 101 int prlimit(pid_t pid, int resource, const struct rlimit *new_limit, 102 struct rlimit *old_limit); 103 int main(int argc, char *argv[]) { prlimit(0, 0, 0, 0); } 104EOF 105 106 probesymbol TOYBOX_GETRANDOM << EOF 107 #include <sys/random.h> 108 int main(void) { char buf[100]; getrandom(buf, 100, 0); } 109EOF 110 111 probesymbol TOYBOX_COPYFILERANGE << EOF 112 #include <sys/syscall.h> 113 #include <unistd.h> 114 int main(void) { copyfilerange(0, 0, 1, 0, 123, 0); } 115EOF 116 probesymbol TOYBOX_HASTIMERS << EOF 117 #include <signal.h> 118 #include <time.h> 119 int main(void) {void *x=0;timer_create(CLOCK_MONOTONIC,x,x);} 120EOF 121} 122 123genconfig() 124{ 125 # Reverse sort puts posix first, examples last. 126 for j in $(ls toys/*/README | sort -s -r) 127 do 128 DIR="$(dirname "$j")" 129 130 [ $(ls "$DIR" | wc -l) -lt 2 ] && continue 131 132 echo "menu \"$(head -n 1 $j)\"" 133 echo 134 135 # extract config stanzas from each source file, in alphabetical order 136 for i in $(ls -1 $DIR/*.c) 137 do 138 # Grab the config block for Config.in 139 echo "# $i" 140 $SED -n '/^\*\//q;/^config [A-Z]/,$p' $i || return 1 141 echo 142 done 143 144 echo endmenu 145 done 146} 147 148probeconfig > generated/Config.probed || rm generated/Config.probed 149genconfig > generated/Config.in || rm generated/Config.in 150 151# Find names of commands that can be built standalone in these C files 152toys() 153{ 154 grep 'TOY(.*)' "$@" | grep -v TOYFLAG_NOFORK | grep -v "0))" | \ 155 $SED -En 's/([^:]*):.*(OLD|NEW)TOY\( *([a-zA-Z][^,]*) *,.*/\1:\3/p' 156} 157 158WORKING= 159PENDING= 160toys toys/*/*.c | ( 161while IFS=":" read FILE NAME 162do 163 [ "$NAME" == help ] && continue 164 [ "$NAME" == install ] && continue 165 [ "$NAME" == sh ] && FILE="toys/*/*.c" 166 echo -e "$NAME: $FILE *.[ch] lib/*.[ch]\n\tscripts/single.sh $NAME\n" 167 echo -e "test_$NAME:\n\tscripts/test.sh $NAME\n" 168 [ "${FILE/pending//}" != "$FILE" ] && 169 PENDING="$PENDING $NAME" || 170 WORKING="$WORKING $NAME" 171done && 172echo -e "clean::\n\t@rm -f $WORKING $PENDING" && 173echo -e "list:\n\t@echo $(echo $WORKING | tr ' ' '\n' | sort | xargs)" && 174echo -e "list_pending:\n\t@echo $(echo $PENDING | tr ' ' '\n' | sort | xargs)" && 175echo -e ".PHONY: $WORKING $PENDING" | $SED 's/ \([^ ]\)/ test_\1/g' 176) > .singlemake 177