• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
6source scripts/portability.sh
7
8mkdir -p "$GENDIR"
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  # Probe for container support on target
28  probesymbol TOYBOX_CONTAINER << EOF
29    #include <stdio.h>
30    #include <sys/syscall.h>
31    #include <linux/sched.h>
32    int x=CLONE_NEWNS|CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET;
33
34    int main(int argc, char *argv[]){printf("%d", x+SYS_unshare+ SYS_setns);}
35EOF
36
37  probesymbol TOYBOX_FIFREEZE -c << EOF
38    #include <linux/fs.h>
39    #ifndef FIFREEZE
40    #error nope
41    #endif
42EOF
43
44  # Work around some uClibc limitations
45  probesymbol TOYBOX_ICONV -c << EOF
46    #include "iconv.h"
47EOF
48
49  # Android and some other platforms miss utmpx
50  probesymbol TOYBOX_UTMPX -c << EOF
51    #include <utmpx.h>
52    #ifndef BOOT_TIME
53    #error nope
54    #endif
55    int main(int argc, char *argv[]) {
56      struct utmpx *a; 
57      if (0 != (a = getutxent())) return 0;
58      return 1;
59    }
60EOF
61
62  # Android is missing shadow.h
63  probesymbol TOYBOX_SHADOW -c << EOF
64    #include <shadow.h>
65    int main(int argc, char *argv[]) {
66      struct spwd *a = getspnam("root"); return 0;
67    }
68EOF
69
70  # Some commands are android-specific
71  probesymbol TOYBOX_ON_ANDROID -c << EOF
72    #ifndef __ANDROID__
73    #error nope
74    #endif
75EOF
76
77  probesymbol TOYBOX_ANDROID_SCHEDPOLICY << EOF
78    #include <processgroup/sched_policy.h>
79
80    int main(int argc,char *argv[]) { get_sched_policy_name(0); }
81EOF
82
83  # nommu support
84  probesymbol TOYBOX_FORK << EOF
85    #include <unistd.h>
86    int main(int argc, char *argv[]) { return fork(); }
87EOF
88  echo -e '\tdepends on !TOYBOX_FORCE_NOMMU'
89
90  probesymbol TOYBOX_PRLIMIT << EOF
91    #include <sys/types.h>
92    #include <sys/time.h>
93    #include <sys/resource.h>
94    int prlimit(pid_t pid, int resource, const struct rlimit *new_limit,
95      struct rlimit *old_limit);
96    int main(int argc, char *argv[]) { prlimit(0, 0, 0, 0); }
97EOF
98
99  probesymbol TOYBOX_GETRANDOM << EOF
100    #include <sys/random.h>
101    int main(void) { char buf[100]; getrandom(buf, 100, 0); }
102EOF
103
104  # glibc requires #define GNU to get the wrapper for this Linux system call,
105  # so just use syscall().
106  probesymbol TOYBOX_COPYFILERANGE << EOF
107    #include <sys/syscall.h>
108    #include <unistd.h>
109    int main(void) { syscall(__NR_copy_file_range, 0, 0, 1, 0, 123, 0); }
110EOF
111  probesymbol TOYBOX_HASTIMERS << EOF
112    #include <signal.h>
113    #include <time.h>
114    int main(void) {void *x=0;timer_create(CLOCK_MONOTONIC,x,x);}
115EOF
116}
117
118genconfig()
119{
120  # Reverse sort puts posix first, examples last.
121  for j in $(ls toys/*/README | sort -s -r)
122  do
123    DIR="$(dirname "$j")"
124
125    [ $(ls "$DIR" | wc -l) -lt 2 ] && continue
126
127    echo "menu \"$(head -n 1 $j)\""
128    echo
129
130    # extract config stanzas from each source file, in alphabetical order
131    for i in $(ls -1 $DIR/*.c)
132    do
133      # Grab the config block for Config.in
134      echo "# $i"
135      $SED -n '/^\*\//q;/^config [A-Z]/,$p' $i || return 1
136      echo
137    done
138
139    echo endmenu
140  done
141}
142
143probeconfig > "$GENDIR"/Config.probed || rm "$GENDIR"/Config.probed
144genconfig > "$GENDIR"/Config.in || rm "$GENDIR"/Config.in
145
146# Find names of commands that can be built standalone in these C files
147toys()
148{
149  grep 'TOY(.*)' "$@" | grep -v TOYFLAG_NOFORK | grep -v "0))" | \
150    $SED -En 's/([^:]*):.*(OLD|NEW)TOY\( *([a-zA-Z][^,]*) *,.*/\1:\3/p'
151}
152
153WORKING=
154PENDING=
155toys toys/*/*.c | (
156while IFS=":" read FILE NAME
157do
158  [ "$NAME" == help ] && continue
159  [ "$NAME" == install ] && continue
160  [ "$NAME" == sh ] && FILE="toys/*/*.c"
161  echo -e "$NAME: $FILE *.[ch] lib/*.[ch]\n\tscripts/single.sh $NAME\n"
162  echo -e "test_$NAME:\n\tscripts/test.sh $NAME\n"
163  [ "${FILE/pending//}" != "$FILE" ] &&
164    PENDING="$PENDING $NAME" ||
165    WORKING="$WORKING $NAME"
166done &&
167echo -e "clean::\n\t@rm -f $WORKING $PENDING" &&
168echo -e "list:\n\t@echo $(echo $WORKING | tr ' ' '\n' | sort | xargs)" &&
169echo -e "list_pending:\n\t@echo $(echo $PENDING | tr ' ' '\n' | sort | xargs)" &&
170echo -e ".PHONY: $WORKING $PENDING" | $SED 's/ \([^ ]\)/ test_\1/g'
171) > .singlemake
172