toybox/scripts/genconfig.sh
<<
>>
Prefs
   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 configure
   9
  10probecc()
  11{
  12  ${CROSS_COMPILE}${CC} $CFLAGS -xc -o /dev/null $1 -
  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 <linux/sched.h>
  37    int x=CLONE_NEWNS|CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET;
  38
  39    int main(int argc, char *argv[]) { setns(0,0); return unshare(x); }
  40EOF
  41
  42  probesymbol TOYBOX_FIFREEZE -c << EOF
  43    #include <linux/fs.h>
  44    #ifndef FIFREEZE
  45    #error nope
  46    #endif
  47EOF
  48
  49  # Work around some uClibc limitations
  50  probesymbol TOYBOX_ICONV -c << EOF
  51    #include "iconv.h"
  52EOF
  53  probesymbol TOYBOX_FALLOCATE << EOF
  54    #include <fcntl.h>
  55
  56    int main(int argc, char *argv[]) { return posix_fallocate(0,0,0); }
  57EOF
  58  
  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
  72  # Android is missing shadow.h
  73  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
  79
  80  # Some commands are android-specific
  81  probesymbol TOYBOX_ON_ANDROID -c << EOF
  82    #ifndef __ANDROID__
  83    #error nope
  84    #endif
  85EOF
  86
  87  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
  93  # nommu support
  94  probesymbol TOYBOX_FORK << EOF
  95    #include <unistd.h>
  96    int main(int argc, char *argv[]) { return fork(); }
  97EOF
  98  echo -e '\tdepends on !TOYBOX_MUSL_NOMMU_IS_BROKEN'
  99
 100  probesymbol TOYBOX_PRLIMIT << EOF
 101    #include <sys/types.h>
 102    #include <sys/time.h>
 103    #include <sys/resource.h>
 104    int prlimit(pid_t pid, int resource, const struct rlimit *new_limit,
 105      struct rlimit *old_limit);
 106    int main(int argc, char *argv[]) { prlimit(0, 0, 0, 0); }
 107EOF
 108
 109  probesymbol TOYBOX_GETRANDOM << EOF
 110    #include <sys/random.h>
 111    int main(void) { char buf[100]; getrandom(buf, 100, 0); }
 112EOF
 113}
 114
 115genconfig()
 116{
 117  # Reverse sort puts posix first, examples last.
 118  for j in $(ls toys/*/README | sort -s -r)
 119  do
 120    DIR="$(dirname "$j")"
 121
 122    [ $(ls "$DIR" | wc -l) -lt 2 ] && continue
 123
 124    echo "menu \"$(head -n 1 $j)\""
 125    echo
 126
 127    # extract config stanzas from each source file, in alphabetical order
 128    for i in $(ls -1 $DIR/*.c)
 129    do
 130      # Grab the config block for Config.in
 131      echo "# $i"
 132      sed -n '/^\*\//q;/^config [A-Z]/,$p' $i || return 1
 133      echo
 134    done
 135
 136    echo endmenu
 137  done
 138}
 139
 140probeconfig > generated/Config.probed || rm generated/Config.probed
 141genconfig > generated/Config.in || rm generated/Config.in
 142
 143# Find names of commands that can be built standalone in these C files
 144toys()
 145{
 146  grep 'TOY(.*)' "$@" | grep -v TOYFLAG_NOFORK | grep -v "0))" | \
 147    sed -rn 's/([^:]*):.*(OLD|NEW)TOY\( *([a-zA-Z][^,]*) *,.*/\1:\3/p'
 148}
 149
 150WORKING=
 151PENDING=
 152toys toys/*/*.c | (
 153while IFS=":" read FILE NAME
 154do
 155  [ "$NAME" == help ] && continue
 156  [ "$NAME" == install ] && continue
 157  echo -e "$NAME: $FILE *.[ch] lib/*.[ch]\n\tscripts/single.sh $NAME\n"
 158  echo -e "test_$NAME:\n\tscripts/test.sh $NAME\n"
 159  [ "${FILE/pending//}" != "$FILE" ] &&
 160    PENDING="$PENDING $NAME" ||
 161    WORKING="$WORKING $NAME"
 162done &&
 163echo -e "clean::\n\trm -f $WORKING $PENDING" &&
 164echo -e "list:\n\t@echo $(echo $WORKING | tr ' ' '\n' | sort | xargs)" &&
 165echo -e "list_pending:\n\t@echo $(echo $PENDING | tr ' ' '\n' | sort | xargs)" &&
 166echo -e ".PHONY: $WORKING $PENDING" | sed 's/ \([^ ]\)/ test_\1/g'
 167) > .singlemake
 168