From c09d1b1fa83be41f6d0568c50cc287ae718de498 Mon Sep 17 00:00:00 2001
Message-ID: <c09d1b1fa83be41f6d0568c50cc287ae718de498.1728191717.git.sam@gentoo.org>
In-Reply-To: <9f08064fef75ce02df9b6133d240c0fa84df458e.1728191717.git.sam@gentoo.org>
References: <9f08064fef75ce02df9b6133d240c0fa84df458e.1728191717.git.sam@gentoo.org>
From: "Jose E. Marchesi" <jose.marchesi@oracle.com>
Date: Mon, 5 Jun 2023 23:44:03 +0200
Subject: [PATCH 03/10] Fix bison and flex build rules.

2023-06-05  Jose E. Marchesi  <jemarch@gnu.org>

	* configure.ac: Use gl_PROG_BISON instead of gl_BISON.
	* src/Makefile.am: Fix bison and flex rules.
	* src/rec-sex-ast.h: Include stddef.h for size_t.
	* src/rec-sex-lex.l: Use header-file option and fix includes.
	* .gitignore: Ignore /src/rec-sex-lex.h.
---
 ChangeLog         |  8 +++++
 configure.ac      |  2 +-
 src/Makefile.am   | 90 +++++++++++++++++++++++++++++++++++++++++++++--
 src/rec-sex-ast.h |  1 +
 src/rec-sex-lex.l |  5 ++-
 6 files changed, 103 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5527ba2..d7cba1a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2023-06-05  Jose E. Marchesi  <jemarch@gnu.org>
+
+	* configure.ac: Use gl_PROG_BISON instead of gl_BISON.
+	* src/Makefile.am: Fix bison and flex rules.
+	* src/rec-sex-ast.h: Include stddef.h for size_t.
+	* src/rec-sex-lex.l: Use header-file option and fix includes.
+	* .gitignore: Ignore /src/rec-sex-lex.h.
+
 2022-04-25  Jose E. Marchesi  <jemarch@gnu.org>
 
 	* src/rec-db.c (rec_db_set): Avoid segfault with databases with no
diff --git a/configure.ac b/configure.ac
index 57dfb6a..24495b4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -46,7 +46,7 @@ AC_ARG_VAR([LEX], [The flex implementation to use.])
 AC_ARG_VAR([LEXLIB], [Options for linking with the flex runtime library.])
 AC_ARG_VAR([LEX_OUTPUT_ROOT], [Base of the file name that the lexer generates.])
 
-gl_BISON
+gl_PROG_BISON([RECUTILS_BISON],[3.6])
 
 dnl System
 AC_CANONICAL_HOST
diff --git a/src/Makefile.am b/src/Makefile.am
index cb7cf7a..f1e8fa7 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -17,6 +17,11 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+BUILT_SOURCES=
+MOSTLYCLEANFILES=
+MAINTAINERCLEANFILES=
+EXTRA_DIST=
+
 lib_LTLIBRARIES = librec.la
 
 include_HEADERS = rec.h
@@ -52,9 +57,90 @@ else
    librec_la_SOURCES += rec-crypt-dummy.c
 endif
 
-BUILT_SOURCES = rec-sex-tab.h
-AM_YFLAGS = -d
 AM_LFLAGS = -d
+# The Automake generated .l.c rule is broken: When executed in a VPATH build,
+#   - The .c file gets generated in the build directory. But since it requires
+#     special tools to rebuild it, we need to distribute it in the tarballs,
+#     and by the GNU Coding Standards
+#     <https://www.gnu.org/prep/standards/html_node/Makefile-Basics.html>
+#     the file should be generated in the source directory.
+#   - The #line directives in the .c file refer to a nonexistent file once it
+#     has been moved from the build directory to the source directory. This
+#     leads to error if 'lcov' is used later.
+# Additionally, here we assume Flex and therefore don't need the ylwrap script.
+# Therefore we override this rule.
+# Since this is a rule that produces multiple files, we apply the idiom from
+# <https://lists.gnu.org/archive/html/bug-make/2020-09/msg00008.html>, so that
+# it works also in parallel 'make'.
+generate-rec-sex-lex:
+	$(AM_V_LEX)$(LEX) $(LFLAGS) $(AM_LFLAGS) -t $(srcdir)/rec-sex-lex.l > rec-sex-lex.c \
+	&& test ':' = '$(LEX)' || { \
+	  sed -e 's|".*/rec-sex-lex\.l"|"rec-sex-lex.l"|' \
+	      -e 's|"lex\.yy\.c"|"rec-sex-lex.c"|' \
+	      < rec-sex-lex.c > rec-sex-lex.c-tmp \
+	  && sed -e 's|".*/rec-sex-lex\.l"|"rec-sex-lex.l"|' \
+	         < rec-sex-lex.h > rec-sex-lex.h-tmp \
+	  && rm -f rec-sex-lex.c rec-sex-lex.h \
+	  && mv rec-sex-lex.c-tmp $(srcdir)/rec-sex-lex.c \
+	  && mv rec-sex-lex.h-tmp $(srcdir)/rec-sex-lex.h; \
+	}
+.PHONY: generate-rec-sex-lex
+# The above rule will generate files with time-stamp order
+# rec-sex-lex.l <= rec-sex-lex.c <= rec-sex-lex.h.
+rec-sex-lex.c: rec-sex-lex.l
+	@{ test -f $(srcdir)/rec-sex-lex.c && test ! $(srcdir)/rec-sex-lex.c -ot $(srcdir)/rec-sex-lex.l; } || $(MAKE) generate-rec-sex-lex
+rec-sex-lex.h: rec-sex-lex.c
+	@{ test -f $(srcdir)/rec-sex-lex.h && test ! $(srcdir)/rec-sex-lex.h -ot $(srcdir)/rec-sex-lex.c; } || $(MAKE) generate-rec-sex-lex
+BUILT_SOURCES += rec-sex-lex.c rec-sex-lex.h
+MOSTLYCLEANFILES += rec-sex-lex.c-tmp rec-sex-lex.h-tmp
+MAINTAINERCLEANFILES += $(srcdir)/rec-sex-lex.c $(srcdir)/rec-sex-lex.h
+EXTRA_DIST += rec-sex-lex.l rec-sex-lex.c rec-sex-lex.h
+
+AM_YFLAGS = -t --report=state
+# The Automake generated .y.c rule is broken: When executed in a VPATH build,
+#   - The .c file gets generated in the build directory. But since it requires
+#     special tools to rebuild it, we need to distribute it in the tarballs,
+#     and by the GNU Coding Standards
+#     <https://www.gnu.org/prep/standards/html_node/Makefile-Basics.html>
+#     the file should be generated in the source directory.
+#   - The #line directives in the .c file refer to a nonexistent file once it
+#     has been moved from the build directory to the source directory. This
+#     leads to error if 'lcov' is used later.
+# Additionally, here we assume GNU Bison and therefore don't need the ylwrap
+# script.
+# Therefore we override this rule.
+# Since this is a rule that produces multiple files, we apply the idiom from
+# <https://lists.gnu.org/archive/html/bug-make/2020-09/msg00008.html>, so that
+# it works also in parallel 'make'.
+generate-rec-sex-tab:
+	$(AM_V_YACC)$(RECUTILS_BISON) -d $(YFLAGS) $(AM_YFLAGS) $(srcdir)/rec-sex-tab.y \
+	&& test ':' = '$(POKE_BISON)' || { \
+	  sed -e 's|".*/rec-sex-tab\.y"|"rec-sex-tab.y"|' \
+	      -e 's|"rec-sex-tab\.tab\.c"|"rec-sex-tab.c"|' \
+	      -e 's|"rec-sex-tab\.tab\.h"|"rec-sex-tab.h"|' \
+	      < rec-sex-tab.tab.c > rec-sex-tab.c-tmp \
+	  && sed -e 's|".*/rec-sex-tab\.y"|"rec-sex-tab.y"|' \
+	         -e 's|"rec-sex-tab\.tab\.h"|"rec-sex-tab.h"|' \
+	         < rec-sex-tab.tab.h > rec-sex-tab.h-tmp \
+	  && rm -f rec-sex-tab.tab.c rec-sex-tab.tab.h \
+	  && mv rec-sex-tab.c-tmp $(srcdir)/rec-sex-tab.c \
+	  && mv rec-sex-tab.h-tmp $(srcdir)/rec-sex-tab.h; \
+	}
+.PHONY: generate-rec-sex-tab
+# The above rule will generate files with time-stamp order
+# rec-sex-tab.y <= rec-sex-tab.c <= rec-sex-tab.h.
+rec-sex-tab.c: rec-sex-tab.y
+	@{ test -f $(srcdir)/rec-sex-tab.c && test ! $(srcdir)/rec-sex-tab.c -ot $(srcdir)/rec-sex-tab.y; } || $(MAKE) generate-rec-sex-tab
+rec-sex-tab.h: rec-sex-tab.c
+	@{ test -f $(srcdir)/rec-sex-tab.h && test ! $(srcdir)/rec-sex-tab.h -ot $(srcdir)/rec-sex-tab.c; } || $(MAKE) generate-rec-sex-tab
+BUILT_SOURCES += rec-sex-tab.c rec-sex-tab.h
+MOSTLYCLEANFILES += \
+  rec-sex-tab.tab.c rec-sex-tab.tab.h \
+  rec-sex-tab.c-tmp rec-sex-tab.h-tmp \
+  rec-sex-tab.output
+MAINTAINERCLEANFILES += $(srcdir)/rec-sex-tab.c $(srcdir)/rec-sex-tab.h
+EXTRA_DIST += rec-sex-tab.y rec-sex-tab.c rec-sex-tab.h
+
 AM_CFLAGS = 
 if USE_COVERAGE
   AM_CFLAGS += -fprofile-arcs -ftest-coverage
diff --git a/src/rec-sex-ast.h b/src/rec-sex-ast.h
index e2ef0dc..b5c6a79 100644
--- a/src/rec-sex-ast.h
+++ b/src/rec-sex-ast.h
@@ -22,6 +22,7 @@
 
 #include <config.h>
 
+#include <stddef.h> /* For size_t */
 #include <stdbool.h>
 
 enum rec_sex_ast_node_type_e
diff --git a/src/rec-sex-lex.l b/src/rec-sex-lex.l
index 2d4a891..8eee8dd 100644
--- a/src/rec-sex-lex.l
+++ b/src/rec-sex-lex.l
@@ -25,13 +25,16 @@
 %option reentrant
 %option bison-bridge
 %option extra-type="void *"
+%option header-file="rec-sex-lex.h"
 
 %option nounput
 %option noinput
 %top {
    /* This code goes at the "top" of the generated file.  */
    #include <config.h>
-   #include "rec-sex-lex.h"
+   #include <rec-sex-ast.h>
+   #include <rec-sex-parser.h>
+   #include "rec-sex-tab.h"
 }
 
 %{
-- 
2.46.2