Update Android.bp & soong/regen.sh

* Selects our Clang and glibc sysroot instead of using the system
* Updates musl logic to use checked in sysroot
* Autogenerate source lists

Change-Id: I7b8649ce6dc0882d060169704ca81a5c71da600d
diff --git a/Android.bp b/Android.bp
index 954f0d2..246870d 100644
--- a/Android.bp
+++ b/Android.bp
@@ -14,8 +14,14 @@
 
 cc_binary_host {
     name: "m4",
+    defaults: [
+        "m4_darwin_defaults",
+        "m4_glibc_defaults",
+        "m4_musl_defaults",
+    ],
 
     cflags: ["-Wno-unused-parameter"],
+    c_std: "c17",
     stl: "none",
 
     local_include_dirs: [
@@ -23,158 +29,24 @@
         "src",
     ],
 
-    srcs: [
-        "src/m4.c",
-        "src/builtin.c",
-        "src/debug.c",
-        "src/eval.c",
-        "src/format.c",
-        "src/freeze.c",
-        "src/input.c",
-        "src/macro.c",
-        "src/output.c",
-        "src/path.c",
-        "src/symtab.c",
-
-        "lib/gl_avltree_oset.c",
-        "lib/binary-io.c",
-        "lib/c-ctype.c",
-        "lib/c-stack.c",
-        "lib/c-strcasecmp.c",
-        "lib/c-strncasecmp.c",
-        "lib/clean-temp.c",
-        "lib/cloexec.c",
-        "lib/close-stream.c",
-        "lib/closein.c",
-        "lib/closeout.c",
-        "lib/dirname.c",
-        "lib/basename.c",
-        "lib/dirname-lgpl.c",
-        "lib/basename-lgpl.c",
-        "lib/stripslash.c",
-        "lib/execute.c",
-        "lib/exitfail.c",
-        "lib/fatal-signal.c",
-        "lib/fd-hook.c",
-        "lib/fd-safer-flag.c",
-        "lib/dup-safer-flag.c",
-        "lib/filenamecat.c",
-        "lib/filenamecat-lgpl.c",
-        "lib/fopen-safer.c",
-        "lib/freading.c",
-        "lib/getprogname.c",
-        "lib/hard-locale.c",
-        "lib/gl_linkedhash_list.c",
-        "lib/gl_list.c",
-        "lib/localcharset.c",
-        "lib/glthread/lock.c",
-        "lib/malloca.c",
-        "lib/math.c",
-        "lib/memchr2.c",
-        "lib/gl_oset.c",
-        "lib/pipe2.c",
-        "lib/pipe2-safer.c",
-        "lib/printf-frexp.c",
-        "lib/printf-frexpl.c",
-        "lib/progname.c",
-        "lib/quotearg.c",
-        "lib/sig-handler.c",
-        "lib/spawn-pipe.c",
-        "lib/mkstemp-safer.c",
-        "lib/tempname.c",
-        "lib/glthread/threadlib.c",
-        "lib/glthread/tls.c",
-        "lib/tmpdir.c",
-        "lib/unistd.c",
-        "lib/dup-safer.c",
-        "lib/fd-safer.c",
-        "lib/pipe-safer.c",
-        "lib/verror.c",
-        "lib/version-etc.c",
-        "lib/version-etc-fsf.c",
-        "lib/wait-process.c",
-        "lib/wctype-h.c",
-        "lib/xmalloc.c",
-        "lib/xalloc-die.c",
-        "lib/gl_xlist.c",
-        "lib/xmalloca.c",
-        "lib/gl_xoset.c",
-        "lib/xprintf.c",
-        "lib/xsize.c",
-        "lib/xstrndup.c",
-        "lib/xvasprintf.c",
-        "lib/xasprintf.c",
-
-        "lib/asnprintf.c",
-        "lib/fclose.c",
-        "lib/fflush.c",
-        "lib/fpurge.c",
-        "lib/fseek.c",
-        "lib/obstack.c",
-        "lib/printf-args.c",
-        "lib/printf-parse.c",
-        "lib/vasnprintf.c",
-    ],
-
     target: {
         glibc: {
             local_include_dirs: ["soong/linux_glibc"],
-            srcs: [
-                "lib/fcntl.c",
-                "lib/freadahead.c",
-                "lib/fseeko.c",
-                "lib/mbrtowc.c",
-            ],
             // TODO(b/131098715): remove
             static_libs: ["libcompiler_rt"],
         },
         musl: {
             local_include_dirs: ["soong/linux_musl"],
-            srcs: [
-                "lib/canonicalize-lgpl.c",
-                "lib/error.c",
-                "lib/fcntl.c",
-                "lib/getopt1.c",
-                "lib/getopt.c",
-                "lib/isnanf.c",
-                "lib/isnanl.c",
-                "lib/mbrtowc.c",
-                "lib/rawmemchr.c",
-                "lib/regex.c",
-                "lib/spawn_faction_addclose.c",
-                "lib/spawn_faction_adddup2.c",
-                "lib/spawn_faction_addopen.c",
+            cflags: [
+                "-Wno-error=unused-const-variable",
+                "-DMUSL_LIBC",
             ],
             // TODO(b/131098715): remove
             static_libs: ["libcompiler_rt"],
         },
         darwin: {
             local_include_dirs: ["soong/darwin"],
-            srcs: [
-                "lib/asprintf.c",
-                "lib/canonicalize-lgpl.c",
-                "lib/error.c",
-                "lib/fpending.c",
-                "lib/freadahead.c",
-                "lib/fseeko.c",
-                "lib/getopt.c",
-                "lib/getopt1.c",
-                "lib/isnanf.c",
-                "lib/isnanl.c",
-                "lib/lstat.c",
-                "lib/open.c",
-                "lib/rawmemchr.c",
-                "lib/readlink.c",
-                "lib/regex.c",
-                "lib/rename.c",
-                "lib/secure_getenv.c",
-                "lib/stat.c",
-                "lib/strchrnul.c",
-                "lib/strerror.c",
-                "lib/strerror-override.c",
-                "lib/strstr.c",
-                "lib/vasprintf.c",
-            ],
+            host_ldlibs: ["-framework CoreFoundation"],
         },
     },
 }
diff --git a/soong/regen.sh b/soong/regen.sh
index cf325fe..1089fd9 100755
--- a/soong/regen.sh
+++ b/soong/regen.sh
@@ -18,29 +18,83 @@
 
 cd `dirname ${BASH_SOURCE[0]}`
 
+ANDROID_BUILD_TOP=$(cd ../../..; pwd)
+clang_version=$(cd $ANDROID_BUILD_TOP; build/soong/scripts/get_clang_version.py)
+
+UNAME=$(uname | tr 'A-Z' 'a-z')
 DIR=linux_glibc
-if [ `uname` == "Darwin" ]; then
+TARGET=glibc
+if [ $UNAME == "darwin" ]; then
   DIR=darwin
-fi
-
-configure_args=""
-
-if [ "$1" = "--musl_sysroot" ]; then
+  TARGET=darwin
+elif [ "$1" = "--musl" ]; then
   DIR=linux_musl
-  sysroot=$2
-  configure_args="CC=${sysroot}/bin/musl-clang CXX=${sysroot}/bin/musl-clang LDFLAGS=--rtlib=compiler-rt"
+  TARGET=musl
 fi
 
-mkdir -p $DIR
+if [ $UNAME == "linux" ]; then
+  CC="$ANDROID_BUILD_TOP/prebuilts/clang/host/linux-x86/$clang_version/bin/clang"
+fi
+CFLAGS=""
+LDLFAGS=""
+config_opts=()
+
+if [ $DIR = "linux_glibc" ]; then
+  glibc="${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8"
+  CFLAGS="--sysroot=${glibc}/sysroot"
+  LDFLAGS="--sysroot=${glibc}/sysroot -B${glibc}/lib/gcc/x86_64-linux/4.8.3 -L${glibc}lib/gcc/x86_64-linux/4.8.3 -L${glibc}/x86_64-linux/lib64 --rtlib=compiler-rt"
+elif [ $DIR = "linux_musl" ]; then
+  sysroot="${ANDROID_BUILD_TOP}/prebuilts/build-tools/sysroots/x86_64-linux-musl"
+  CFLAGS="--sysroot=${sysroot} -target x86_64-linux-musl"
+  LDFLAGS="--rtlib=compiler-rt -fuse-ld=lld"
+elif [ $DIR = "darwin" ]; then
+  # vfork is deprecated on Mac, so always fallback to fork
+  config_opts+=("ac_cv_func_vfork=no")
+  # we'd need to link to libiconv
+  config_opts+=("am_cv_func_iconv=no")
+fi
+
+rm -rf $DIR
+mkdir -p $DIR $DIR/sys
 cd $DIR
 
 # Generate headers
 rm -rf tmp
 mkdir tmp
 cd tmp
-../../../configure ${configure_args}
+../../../configure CC=$CC CXX=$CC CFLAGS="${CFLAGS}" LDFLAGS="${LDFLAGS}" "${config_opts[@]}"
+
+# Don't try to use various tools to update the source tree:
+#   aclocal/automake/etc
+touch ../../../aclocal.m4
+touch ../../../configure
+touch ../../../Makefile.in
+#   autoheader
+touch ../../../lib/config.hin
+#   doc/...
+touch ../../../doc/stamp-vti
+touch ../../../doc/m4.info
+touch ../../../doc/m4.1
+
 make
+
+cat >../Android.bp <<END
+// Autogenerated by android_regen.sh
+
+cc_defaults {
+    name: "m4_${TARGET}_defaults",
+    target: {
+        $TARGET: {
+            srcs: [
+                $(find lib src -name '*.o' | sed -E "s/(.*)\.o/\"\1\.c\",/" | LC_ALL=C sort)
+            ],
+        },
+    },
+}
+END
+${ANDROID_BUILD_TOP}/prebuilts/build-tools/$UNAME-x86/bin/bpfmt -w ../Android.bp
+
 cd lib
-find . -name '*.h' | xargs -IX cp X ../../
+find . -name '*.h' | xargs -IX cp X ../../X
 cd ../..
 rm -rf tmp