From 5956327d8ef2f8e8b75987b0aba7f2e29342c80c Mon Sep 17 00:00:00 2001
From: Explorer09 <explorer09@gmail.com>
Date: Sat, 14 Jul 2018 11:30:30 +0800
Subject: [PATCH] build: config_for_build.h for cross, bootstrap config.

This should be enough to fix the main bug as reported in #314.
Other cross-compile limitations may still exist.

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
---
 configure.ac              |  3 ++
 src/.gitignore            |  1 +
 src/Makefile.am           |  2 +-
 src/config_for_build.h.in | 81 +++++++++++++++++++++++++++++++++++++++
 src/flexdef.h             |  6 ++-
 5 files changed, 91 insertions(+), 2 deletions(-)
 create mode 100644 src/config_for_build.h.in

diff --git a/configure.ac b/configure.ac
index 9c53590fd..df2efc0c3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -172,6 +172,9 @@ AC_CHECK_FUNCS([dup2 memset regcomp strcasecmp strchr strdup strtol], [],
 # reallocarray - OpenBSD function. We have replacement if not available.
 AC_CHECK_FUNCS([pow setlocale reallocarr reallocarray])
 
+AS_IF([test "$cross_compiling" = yes],
+  [AC_CONFIG_FILES([src/config_for_build.h])])
+
 AC_CONFIG_FILES(
 Makefile
 doc/Makefile
diff --git a/src/.gitignore b/src/.gitignore
index 7f35139c0..6a0f9cb02 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -3,6 +3,7 @@
 *.o
 config.h
 config.h.in
+config_for_build.h
 flex
 libfl.pc
 parse.c
diff --git a/src/Makefile.am b/src/Makefile.am
index e033cc8da..d031bf57e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -32,7 +32,7 @@ stage1flex_LINK = $(LIBTOOL) --tag=CC --mode=link $(CC_FOR_BUILD) \
 		  $(CFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) -o $@
 
 $(stage1flex_OBJECTS): CC=$(CC_FOR_BUILD)
-$(stage1flex_OBJECTS): CFLAGS=$(CFLAGS_FOR_BUILD)
+$(stage1flex_OBJECTS): CFLAGS=$(CFLAGS_FOR_BUILD) -DUSE_CONFIG_FOR_BUILD
 $(stage1flex_OBJECTS): CPP=$(CPP_FOR_BUILD)
 $(stage1flex_OBJECTS): CPPFLAGS=$(CPPFLAGS_FOR_BUILD)
 $(stage1flex_OBJECTS): LDFLAGS=$(LDFLAGS_FOR_BUILD)
diff --git a/src/config_for_build.h.in b/src/config_for_build.h.in
new file mode 100644
index 000000000..dde989994
--- /dev/null
+++ b/src/config_for_build.h.in
@@ -0,0 +1,81 @@
+/* config_for_build - minimal config header for bootstrapping flex */
+
+/*  This file is part of flex. */
+
+/*  Redistribution and use in source and binary forms, with or without */
+/*  modification, are permitted provided that the following conditions */
+/*  are met: */
+
+/*  1. Redistributions of source code must retain the above copyright */
+/*     notice, this list of conditions and the following disclaimer. */
+/*  2. Redistributions in binary form must reproduce the above copyright */
+/*     notice, this list of conditions and the following disclaimer in the */
+/*     documentation and/or other materials provided with the distribution. */
+
+/*  Neither the name of the University nor the names of its contributors */
+/*  may be used to endorse or promote products derived from this software */
+/*  without specific prior written permission. */
+
+/*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
+/*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
+/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
+/*  PURPOSE. */
+
+/* The "config_for_build.in" file is intended to be processed by "configure"
+ * in order to produce working PACKAGE, VERSION and M4 definitions.
+ * Add "config_for_build.in" to AC_CONFIG_FILES. Do not add it to
+ * AC_CONFIG_HEADERS as it would break our intentional #undef's and mess up
+ * with the #define statements.
+ */
+#define PACKAGE "@PACKAGE@"
+#define VERSION "@VERSION@"
+#define M4 "@M4@"
+
+#undef ENABLE_NLS
+/* Flex includes <locale.h> only if NLS is enabled, but we undef it anyway. */
+#undef HAVE_LOCALE_H
+
+#undef HAVE_NETINET_IN_H
+#ifdef __has_include
+# if __has_include(<netinet/in.h>)
+#  define HAVE_NETINET_IN_H 1
+# endif
+#endif
+
+#undef HAVE_REALLOCARRAY
+#undef HAVE_REALLOCARR
+
+/* Always use malloc/realloc wrappers. */
+#undef malloc
+#define malloc rpl_malloc
+#undef realloc
+#define realloc rpl_realloc
+
+/* The Bison-generated parser in flex code controls the use of alloca() through
+ * its own macro (YYSTACK_USE_ALLOCA) and not defines from Autoconf.
+ * alloca() is not used in other parts of flex code.
+ */
+#undef HAVE_ALLOCA_H
+#undef HAVE_ALLOCA
+#undef C_ALLOCA
+
+/* HAVE_INTTYPES_H is defined by Autoconf but not used in flex code. Flex has
+ * flexint.h that provides fallback definitions.
+ */
+/*
+#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
+# undef HAVE_INTTYPES_H
+#endif
+*/
+
+#undef HAVE_STDBOOL_H
+#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
+# undef HAVE__BOOL
+#endif
+
+/* Standard C89 stuff which "configure" provides fallback #define's for. */
+#ifndef __STDC__
+# undef HAVE_LIMITS_H
+# define const /* const */
+# define size_t unsigned int
+#endif
diff --git a/src/flexdef.h b/src/flexdef.h
index e47c0ef09..75b82fe72 100644
--- a/src/flexdef.h
+++ b/src/flexdef.h
@@ -35,8 +35,12 @@
 #ifndef FLEXDEF_H
 #define FLEXDEF_H 1
 
-#ifdef HAVE_CONFIG_H
+#ifndef USE_CONFIG_FOR_BUILD
+# ifdef HAVE_CONFIG_H
 #include <config.h>
+# endif
+#else
+#include <config_for_build.h>
 #endif
 
 #include <stdio.h>