blob: c16974d72c5c6194793469bafe381e7d11afbf0a [file] [log] [blame]
Rob Landley28964802008-01-19 17:08:39 -06001#!/bin/bash
2
Rob Landley7c04f012008-01-20 19:00:16 -06003# 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
Rob Landley28964802008-01-19 17:08:39 -06006mkdir -p generated
7
Rob Landley2bd3a5d2012-02-13 08:44:32 -06008source configure
9
Rob Landleya913d922015-05-09 17:07:22 -050010probecc()
11{
12 ${CROSS_COMPILE}${CC} $CFLAGS -xc -o /dev/null $1 -
13}
14
Rob Landley76e1cb32014-04-16 07:49:32 -050015# 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{
Rob Landleya913d922015-05-09 17:07:22 -050019 probecc $2 2>/dev/null && DEFAULT=y || DEFAULT=n
Rob Landley76e1cb32014-04-16 07:49:32 -050020 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
Rob Landley1b7ad012012-02-23 21:03:18 -060025probeconfig()
Rob Landley28964802008-01-19 17:08:39 -060026{
Rob Landleya913d922015-05-09 17:07:22 -050027 > 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
Rob Landley27f57792012-02-03 23:16:28 -060034 # Probe for container support on target
Rob Landley76e1cb32014-04-16 07:49:32 -050035 probesymbol TOYBOX_CONTAINER << EOF
Georgi Chorbadzhiyski2c27fcf2012-03-04 01:24:06 -060036 #include <linux/sched.h>
Rob Landley27f57792012-02-03 23:16:28 -060037 int x=CLONE_NEWNS|CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET;
Rob Landleyf9fdd3c2012-03-19 20:15:08 -050038
Rob Landleyc99cbd12016-09-28 14:22:34 -050039 int main(int argc, char *argv[]) { setns(0,0); return unshare(x); }
Rob Landley27f57792012-02-03 23:16:28 -060040EOF
Rob Landley76e1cb32014-04-16 07:49:32 -050041
42 probesymbol TOYBOX_FIFREEZE -c << EOF
43 #include <linux/fs.h>
44 #ifndef FIFREEZE
45 #error nope
46 #endif
47EOF
Rob Landley5fe77cf2014-04-23 08:38:29 -050048
Rob Landley434cefb2014-06-28 20:16:11 -050049 # Work around some uClibc limitations
Rob Landley5fe77cf2014-04-23 08:38:29 -050050 probesymbol TOYBOX_ICONV -c << EOF
51 #include "iconv.h"
52EOF
Rob Landley434cefb2014-06-28 20:16:11 -050053 probesymbol TOYBOX_FALLOCATE << EOF
54 #include <fcntl.h>
55
56 int main(int argc, char *argv[]) { return posix_fallocate(0,0,0); }
57EOF
Isaac Dunham46ddf0e2014-11-19 16:38:46 -060058
59 # Android and some other platforms miss utmpx
60 probesymbol TOYBOX_UTMPX -c << EOF
61 #include <utmpx.h>
62 #ifndef BOOT_TIME
63 #error nope
64 #endif
65 int main(int argc, char *argv[]) {
66 struct utmpx *a;
67 if (0 != (a = getutxent())) return 0;
68 return 1;
69 }
70EOF
71
Elliott Hughesc2415d12015-01-16 13:49:23 -060072 # Android is missing shadow.h
Isaac Dunham46ddf0e2014-11-19 16:38:46 -060073 probesymbol TOYBOX_SHADOW -c << EOF
74 #include <shadow.h>
75 int main(int argc, char *argv[]) {
76 struct spwd *a = getspnam("root"); return 0;
77 }
78EOF
Rob Landley5b493dc2015-04-19 21:50:51 -050079
80 # Some commands are android-specific
81 probesymbol TOYBOX_ON_ANDROID -c << EOF
82 #ifndef __ANDROID__
83 #error nope
84 #endif
85EOF
Rob Landley99332732015-07-03 15:17:25 -050086
Rob Landleybe3e3182017-04-30 02:46:36 -050087 probesymbol TOYBOX_ANDROID_SCHEDPOLICY << EOF
88 #include <cutils/sched_policy.h>
89
90 int main(int argc,char *argv[]) { get_sched_policy_name(0); }
91EOF
92
Rob Landley99332732015-07-03 15:17:25 -050093 # nommu support
94 probesymbol TOYBOX_FORK << EOF
95 #include <unistd.h>
96 int main(int argc, char *argv[]) { return fork(); }
97EOF
Rob Landley8c588d82015-08-29 22:12:56 -050098 echo -e '\tdepends on !TOYBOX_MUSL_NOMMU_IS_BROKEN'
Rob Landley323819c2016-03-01 23:50:23 -060099
100 probesymbol TOYBOX_PRLIMIT << EOF
101 #include <sys/time.h>
102 #include <sys/resource.h>
103
104 int main(int argc, char *argv[]) { prlimit(0, 0, 0, 0); }
105EOF
Rob Landley1b7ad012012-02-23 21:03:18 -0600106}
Rob Landleyf9fdd3c2012-03-19 20:15:08 -0500107
Rob Landley1b7ad012012-02-23 21:03:18 -0600108genconfig()
109{
Rob Landleyf9070f32014-04-23 17:23:09 -0500110 # Reverse sort puts posix first, examples last.
Elliott Hughes0ca829c2016-05-25 14:43:56 -0700111 for j in $(ls toys/*/README | sort -s -r)
Rob Landley28964802008-01-19 17:08:39 -0600112 do
Rob Landleyb1c002a2012-12-10 21:08:42 -0600113 DIR="$(dirname "$j")"
114
115 [ $(ls "$DIR" | wc -l) -lt 2 ] && continue
116
Rob Landleyaa777fe2012-12-08 21:10:10 -0600117 echo "menu \"$(head -n 1 $j)\""
Rob Landley28964802008-01-19 17:08:39 -0600118 echo
Rob Landley3a9241a2012-08-25 14:25:22 -0500119
Rob Landley3a9241a2012-08-25 14:25:22 -0500120 # extract config stanzas from each source file, in alphabetical order
Rob Landleyaa777fe2012-12-08 21:10:10 -0600121 for i in $(ls -1 $DIR/*.c)
Rob Landley3a9241a2012-08-25 14:25:22 -0500122 do
123 # Grab the config block for Config.in
124 echo "# $i"
125 sed -n '/^\*\//q;/^config [A-Z]/,$p' $i || return 1
126 echo
127 done
128
129 echo endmenu
Rob Landley28964802008-01-19 17:08:39 -0600130 done
131}
132
Rob Landley1b7ad012012-02-23 21:03:18 -0600133probeconfig > generated/Config.probed || rm generated/Config.probed
134genconfig > generated/Config.in || rm generated/Config.in
Rob Landley712e43e2016-02-07 18:15:53 -0600135
136# Find names of commands that can be built standalone in these C files
137toys()
138{
Rob Landley05b75292016-02-14 14:04:21 -0600139 grep 'TOY(.*)' "$@" | grep -v TOYFLAG_NOFORK | grep -v "0))" | \
Rob Landley712e43e2016-02-07 18:15:53 -0600140 sed -rn 's/([^:]*):.*(OLD|NEW)TOY\( *([a-zA-Z][^,]*) *,.*/\1:\3/p'
141}
142
143WORKING=
144PENDING=
145toys toys/*/*.c | (
146while IFS=":" read FILE NAME
147do
148 [ "$NAME" == help ] && continue
149 [ "$NAME" == install ] && continue
Rob Landley9b14cb6a2016-02-07 19:09:39 -0600150 echo -e "$NAME: $FILE *.[ch] lib/*.[ch]\n\tscripts/single.sh $NAME\n"
Rob Landley5ec9f522016-02-19 17:56:01 -0600151 echo -e "test_$NAME:\n\tscripts/test.sh $NAME\n"
Rob Landley712e43e2016-02-07 18:15:53 -0600152 [ "${FILE/pending//}" != "$FILE" ] &&
153 PENDING="$PENDING $NAME" ||
154 WORKING="$WORKING $NAME"
Rob Landleya8d0d132016-03-23 03:25:37 -0500155done &&
156echo -e "clean::\n\trm -f $WORKING $PENDING" &&
Rob Landleya5995142016-04-20 00:47:05 -0500157echo -e "list:\n\t@echo $(echo $WORKING | tr ' ' '\n' | sort | xargs)" &&
Rob Landleya8d0d132016-03-23 03:25:37 -0500158echo -e "list_pending:\n\t@echo $(echo $PENDING | tr ' ' '\n' | sort | xargs)" &&
159echo -e ".PHONY: $WORKING $PENDING" | sed 's/ \([^ ]\)/ test_\1/g'
160) > .singlemake