sfeed-box.sh - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       sfeed-box.sh (3909B)
       ---
            1 #!/bin/sh
            2 
            3 CC="${CC:-cc}"
            4 AR="${AR:-ar}"
            5 RANLIB="${RANLIB:-ranlib}"
            6 CFLAGS="-Os -flto -Wall" # optimize for size and enable link-time optimizations.
            7 # use minicurses (no linking with (n)curses)
            8 CPPFLAGS="-D_DEFAULT_SOURCE -D_XOPEN_SOURCE=700 -D_BSD_SOURCE -DSFEED_MINICURSES"
            9 LDFLAGS="-s -flto -Wl,--gc-sections" # aggressive strip and link-time optimizations.
           10 
           11 # hyperaggressive optimizations
           12 #CC="musl-gcc"
           13 #CFLAGS="-Os -flto -Wall -ffunction-sections -fdata-sections -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-stack-protector -fomit-frame-pointer -fmerge-all-constants -fvisibility=hidden"
           14 #LDFLAGS="-s -flto -Wl,--gc-sections,--strip-all,--as-needed,--hash-style=gnu,--build-id=none"
           15 
           16 # uncomment for static-linked.
           17 #STATIC="-static"
           18 
           19 src="sfeed.c sfeed_atom.c sfeed_curses.c sfeed_frames.c sfeed_gopher.c sfeed_html.c sfeed_json.c sfeed_mbox.c sfeed_opml_import.c sfeed_plain.c sfeed_twtxt.c sfeed_web.c sfeed_xmlenc.c"
           20 obj="sfeed.o sfeed_atom.o sfeed_curses.o sfeed_frames.o sfeed_gopher.o sfeed_html.o sfeed_json.o sfeed_mbox.o sfeed_opml_import.o sfeed_plain.o sfeed_twtxt.o sfeed_web.o sfeed_xmlenc.o"
           21 libsrc="util.c xml.c"
           22 libobj="util.o xml.o"
           23 # uncomment compatsrc for some systems (non-BSD).
           24 compatsrc="strlcpy.c strlcat.c"
           25 compatobj="strlcat.o strlcpy.o"
           26 
           27 # compile source from stdin to object file.
           28 compile() {
           29         echo "compile: $1"
           30         ${CC} ${STATIC} ${CFLAGS} ${CPPFLAGS} -x c -c - -o "$1"
           31 }
           32 
           33 box() {
           34 cat <<!
           35 #include <stdio.h>
           36 #include <string.h>
           37 
           38 int sfeed_atom_main(int argc, char *argv[]);
           39 int sfeed_curses_main(int argc, char *argv[]);
           40 int sfeed_frames_main(int argc, char *argv[]);
           41 int sfeed_gopher_main(int argc, char *argv[]);
           42 int sfeed_html_main(int argc, char *argv[]);
           43 int sfeed_json_main(int argc, char *argv[]);
           44 int sfeed_mbox_main(int argc, char *argv[]);
           45 int sfeed_opml_import_main(void);
           46 int sfeed_plain_main(int argc, char *argv[]);
           47 int sfeed_twtxt_main(int argc, char *argv[]);
           48 int sfeed_web_main(int argc, char *argv[]);
           49 int sfeed_xmlenc_main(void);
           50 
           51 int sfeed_main(int argc, char *argv[]);
           52 
           53 int
           54 main(int argc, char *argv[])
           55 {
           56         const char *s = ((s = strrchr(argv[0], '/'))) ? s + 1 : argv[0];
           57 
           58         if (!strcmp(s, "sfeed_atom")) return sfeed_atom_main(argc, argv);
           59         else if (!strcmp(s, "sfeed_curses")) return sfeed_curses_main(argc, argv);
           60         else if (!strcmp(s, "sfeed_frames")) return sfeed_frames_main(argc, argv);
           61         else if (!strcmp(s, "sfeed_gopher")) return sfeed_gopher_main(argc, argv);
           62         else if (!strcmp(s, "sfeed_html")) return sfeed_html_main(argc, argv);
           63         else if (!strcmp(s, "sfeed_json")) return sfeed_json_main(argc, argv);
           64         else if (!strcmp(s, "sfeed_mbox")) return sfeed_mbox_main(argc, argv);
           65         else if (!strcmp(s, "sfeed_opml_import")) return sfeed_opml_import_main();
           66         else if (!strcmp(s, "sfeed_plain")) return sfeed_plain_main(argc, argv);
           67         else if (!strcmp(s, "sfeed_twtxt")) return sfeed_twtxt_main(argc, argv);
           68         else if (!strcmp(s, "sfeed_web")) return sfeed_web_main(argc, argv);
           69         else if (!strcmp(s, "sfeed_xmlenc")) return sfeed_xmlenc_main();
           70         return sfeed_main(argc, argv);
           71 }
           72 !
           73 }
           74 
           75 do_build() {
           76         # lib
           77         for f in ${libsrc} ${compatsrc}; do
           78                 compile "${f%.c}.o" < "$f"
           79         done
           80         for f in util xml; do
           81                 ${AR} -rc "lib${f}.a" "${f}.o"
           82                 ${RANLIB} "lib${f}.a"
           83         done
           84 
           85         # replace main with program_main
           86         for f in $src; do
           87                 sed "s/^main(/$(echo "${f%.c}" | sed s/-/_/g)_&/" < $f | compile "${f%.c}.o"
           88         done
           89 
           90         box | compile "sfeed-box.o"
           91 }
           92 
           93 do_link() {
           94         echo "link sfeed-box"
           95         $CC $STATIC $CFLAGS $LDFLAGS -o \
           96                 sfeed-box \
           97                 ${obj} ${libobj} ${compatobj} \
           98                 sfeed-box.o
           99 #        strip sfeed-box
          100 }
          101 
          102 do_install() {
          103         cp -f sfeed-box sfeed
          104 
          105         # symlink, except use "sfeed" as "sfeed-box" itself.
          106         for f in $src; do
          107                 [ "$f" = "sfeed.c" ] && continue
          108 
          109                 ln -sf sfeed "${f%.c}" # symlink
          110                 #ln -f sfeed "${f%.c}" # hardlink
          111         done
          112 }
          113 
          114 do_list() {
          115         size sfeed-box
          116         ls -l sfeed-box
          117 }
          118 
          119 if [ "$1" != "" ]; then
          120         "do_$1"
          121 else
          122         do_build
          123         do_link
          124         do_install
          125         do_list
          126 fi