diff mbox

gengtype improvements for plugins. patch 1/N

Message ID 1283016347.3067.43.camel@glinka
State New
Headers show

Commit Message

Basile Starynkevitch Aug. 28, 2010, 5:25 p.m. UTC
On Sat, 2010-08-28 at 19:06 +0200, Ralf Wildenhues wrote:
> Hello Basile,
> 
> I cannot approve nor reject your patch, but regarding the build system
> changes:
> build tools need to be compiled with $(COMPILER_FOR_BUILD) and with
> $(BUILD_COMPILERFLAGS) $(BUILD_CPPFLAGS), otherwise cross compilation
> will fail. 

Thanks for the feedback. Attached is the corrected patch
patch01bis-r163612-gengtype-progarg.diff

Below is the corrected gcc/ChangeLog entry.
2010-08-28  Jeremie Salvucci  <jeremie.salvucci@free.fr>,
	    Basile Starynkevitch  <basile@starynkevitch.net>

	* gengtype.c: Added #include of getopt.h & version.h. Moved the
	include of gengtype.h

	(lang_bitmap, struct outf, outf_p, output_file)
	(inputlist, output_files, header_file, plugin_output_filename)
	(srcdir, srcdir_len, do_dump): definitions moved to gengtype.c,
	and variables declared here.
	(read_state_filename, write_state_filename, do_debug): New
	declarations.

	(gengtype_long_options): Added new static variable.

	(print_usage): Added new function.
	(print_version): Added new function.
	(parse_program_options): Added new function.

	(main): use parse_program_options, cleaned up a bit. Added stubs
	for read & write of state.

	* gengtype.h: Moved many things from gengtype.c to here.
	(outf, outf_p, output_files, header_file, srcdir, srcdir_len)
	(do_dump): Moved variables from gengtype.c

	(read_state_filename, write_state_filename, do_debug): Added variables.
	(dbgprintf): Added conditional macro.

	* Makefile.in (build/version.o): Added target, use BUILD_CPPFLAGS
	& $(COMPILER_FOR_BUILD)

	(build/gengtype): Link with it.
	(s-gtype): Run gengtype with new program arguments.


Ok for trunk?

PS. If anyone have concrete advice for sequence of patches I would be
grateful. Perhaps interdiff is enough for the second patch.
diff mbox

Patch

Index: gcc/gengtype.c
===================================================================
--- gcc/gengtype.c	(revision 163612)
+++ gcc/gengtype.c	(working copy)
@@ -20,10 +20,12 @@ 
 
 #include "bconfig.h"
 #include "system.h"
-#include "gengtype.h"
 #include "errors.h"	/* for fatal */
+#include "getopt.h"
 #include "double-int.h"
 #include "hashtab.h"
+#include "version.h"    /* for version_string & pkgversion_string */
+#include "gengtype.h"
 
 /* Data types, macros, etc. used only in this file.  */
 
@@ -39,8 +41,6 @@  enum typekind {
   TYPE_PARAM_STRUCT
 };
 
-typedef unsigned lang_bitmap;
-
 /* A way to pass data through to the output end.  */
 struct options
 {
@@ -120,16 +120,6 @@  struct type
   || (x)->kind == TYPE_STRUCT 			\
   || (x)->kind == TYPE_LANG_STRUCT)
 
-/* Structure representing an output file.  */
-struct outf
-{
-  struct outf *next;
-  const char *name;
-  size_t buflength;
-  size_t bufused;
-  char *buf;
-};
-typedef struct outf * outf_p;
 
 /* An output file, suitable for definitions, that can see declarations
    made in INPUT_FILE and is linked into every language that uses
@@ -142,26 +132,37 @@  const char *get_output_file_name (const char *);
 static void oprintf (outf_p o, const char *S, ...)
      ATTRIBUTE_PRINTF_2;
 
-/* The list of output files.  */
-static outf_p output_files;
 
+/* Rhe name of the file containing the list of input files. */
+static char* inputlist = 0;
+
 /* The plugin input files and their number; in that case only
    a single file is produced.  */
 static char** plugin_files;
 static size_t nb_plugin_files;
-/* the generated plugin output name & file */
+
+/* The generated plugin output file & name.  */
 static outf_p plugin_output;
+static char* plugin_output_filename;
 
-/* The output header file that is included into pretty much every
-   source file.  */
-static outf_p header_file;
+/* Our output files and output header file.  */
+outf_p output_files;
+outf_p header_file;
 
-/* Source directory.  */
-static const char *srcdir;
+/* Our source directory & its name.  */
+const char *srcdir;
+size_t srcdir_len;
 
-/* Length of srcdir name.  */
-static size_t srcdir_len = 0;
+/* Variables used for reading and writing the state. Not yet
+   implemented feature! */
+char *read_state_filename;
+char *write_state_filename;
 
+
+/* Variables to help debugging. */
+int do_dump;
+int do_debug;
+
 static outf_p create_file (const char *, const char *);
 
 static const char * get_file_basename (const char *);
@@ -4209,32 +4210,140 @@  dump_everything (void)
 }
 
 
+/* Option specification for getopt_long.  */
+static const struct option gengtype_long_options[] = 
+{
+    { "help",      no_argument, NULL, 'h' },
+    { "version",   no_argument, NULL, 'v' },
+    { "dump",      no_argument, NULL, 'd' },
+    { "debug",     no_argument, NULL, 'D' },
+    { "plugin",    required_argument, NULL, 'P' },
+    { "srcdir",    required_argument, NULL, 'S' },
+    { "inputs",    required_argument, NULL, 'I' },
+    { "read-state",    required_argument, NULL, 'r' },
+    { "write-state",    required_argument, NULL, 'w' },
+    { NULL,        no_argument, NULL, 0   },
+};
+
+
+static void
+print_usage (void)
+{
+    printf ("Usage: %s\n", progname);
+    printf ("\t -h | --help  \t# Give this help.\n");
+    printf ("\t -D | --debug  \t# Lots of debug output to debug gengtype itself.\n");
+    printf ("\t -V | --version  \t# Give version information.\n");
+    printf ("\t -d | --dump  \t# Dump state for debugging.\n");
+    printf ("\t -P | --plugin <output-file>  \t#  Generate for a plugin.\n");
+    printf ("\t -S | --srcdir <GCC-directory>  \t#  Specify the GCC source directory.\n");
+    printf ("\t -I | --inputs <input-list>  \t#  Specify the file with source files list.\n");
+    printf ("\t -w | --write-state <state-file> \t# Write a state file (for plugin use).\n");
+    printf ("\t -r | --read-state <state-file> \t# Read a state file.\n");
+}
+
+static void
+print_version (void)
+{
+    printf ("%s %s%s\n", progname, pkgversion_string, version_string);
+    printf ("Report bugs: %s\n", bug_report_url);
+}
+
+/* Parse the program options using getopt_long... */
+static void
+parse_program_options (int argc, char**argv)
+{
+    int opt = -1;
+    while ((opt = getopt_long (argc, argv, "hVdP:S:I:w:r:D",
+			       gengtype_long_options, NULL)) >= 0)
+    {
+	switch (opt)
+	{
+	case 'h': /* --help */
+	    print_usage ();
+	    break;
+	case 'V': /* --version */
+	    print_version ();
+	    break;
+	case 'd': /* --dump */
+	    do_dump = 1;
+	    break;
+	case 'D': /* --debug */
+	    do_debug = 1;
+	    break;
+	case 'P': /* --plugin */
+	    if (optarg)
+		plugin_output_filename = optarg;
+	    else
+		fatal ("missing plugin output file name");
+	    break;
+	case 'S': /* --srcdir */
+	    if (optarg)
+		srcdir = optarg;
+	    else
+		fatal ("missing source directory");
+	    srcdir_len = strlen (srcdir);
+	    break;
+	case 'I': /* --inputs */
+	    if (optarg)
+		inputlist = optarg;
+	    else
+		fatal ("missing input list");
+	    break;
+	case 'r': /* --read-state */
+	    if (optarg)
+		read_state_filename = optarg;
+	    else
+		fatal ("missing read state file");
+	    dbgprintf ("read state %s\n", optarg);
+	    break;
+	case 'w': /* --write-state */
+	    dbgprintf ("write state %s\n", optarg);
+	    if (optarg)
+		write_state_filename = optarg;
+	    else
+		fatal ("missing write state file");
+	    break;
+	default:
+	    fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
+	    print_usage ();
+	    fatal ("unexpected flag");
+	}
+    };
+    if (plugin_output_filename)
+    {
+	/* In plugin mode we require some input files. */
+	int i = 0;
+	if (optind >= argc)
+	    fatal("no source files given in plugin mode");
+	nb_plugin_files = argc - optind;
+	for  (i = 0; i < (int) nb_plugin_files; i++)
+	{
+	    char *name = argv[i + optind];
+	    plugin_files[i] = name;
+	}
+    }
+}
+
 int
 main (int argc, char **argv)
 {
   size_t i;
   static struct fileloc pos = { this_file, 0 };
-  char* inputlist = 0;
-  int do_dump = 0;
   outf_p output_header;
-  char* plugin_output_filename = NULL;
   /* fatal uses this */
   progname = "gengtype";
 
-  if (argc >= 2 && !strcmp (argv[1], "-d"))
-    {
-      do_dump = 1;
-      argv = &argv[1];
-      argc--;
-    }
+  parse_program_options (argc, argv);
 
-  if (argc >= 6 && !strcmp (argv[1], "-P"))
+  if (read_state_filename) 
+    fatal ("read state of %s not implemented yet", read_state_filename);
+
+  if (write_state_filename)
+    fatal ("write state of %s not implemented yet", write_state_filename);
+
+  if (plugin_output_filename)
     {
-      plugin_output_filename = argv[2];
       plugin_output = create_file ("GCC", plugin_output_filename);
-      srcdir = argv[3];
-      inputlist = argv[4];
-      nb_plugin_files = argc - 5;
       plugin_files = XCNEWVEC (char *, nb_plugin_files);
       for (i = 0; i < nb_plugin_files; i++)
       {
@@ -4246,17 +4355,8 @@  main (int argc, char **argv)
         strcpy (plugin_files[i], name);
       }
     }
-  else if (argc == 3)
-    {
-      srcdir = argv[1];
-      inputlist = argv[2];
-    }
-  else
-    fatal ("usage: gengtype [-d] [-P pluginout.h] srcdir input-list "
-           "[file1 file2 ... fileN]");
 
-  srcdir_len = strlen (srcdir);
-
+  dbgprintf ("inputlist %s", inputlist);
   read_input_list (inputlist);
   if (hit_error)
     return 1;
Index: gcc/gengtype.h
===================================================================
--- gcc/gengtype.h	(revision 163612)
+++ gcc/gengtype.h	(working copy)
@@ -20,6 +20,10 @@  along with GCC; see the file COPYING3.  If not see
 #ifndef GCC_GENGTYPE_H
 #define GCC_GENGTYPE_H
 
+/* Sets of accepted source languages like C, C++, Ada ... are
+   represented by a bitmap.  */
+typedef unsigned lang_bitmap;
+
 /* A file position, mostly for error messages.
    The FILE element may be compared using pointer equality.  */
 struct fileloc {
@@ -37,6 +41,35 @@  typedef struct options *options_p;
 extern int lexer_toplevel_done;
 extern struct fileloc lexer_line;
 
+/* Structure representing an output file.  */
+struct outf
+{
+    struct outf *next;
+    const char *name;
+    size_t buflength;
+    size_t bufused;
+    char *buf;
+};
+typedef struct outf * outf_p;
+
+/* The list of output files.  */
+extern outf_p output_files;
+
+/* The output header file that is included into pretty much every
+   source file.  */
+extern outf_p header_file;
+
+/* Source directory.  */
+extern const char *srcdir;
+
+/* Length of srcdir name.  */
+extern size_t srcdir_len;
+
+/* Variable used for reading and writing the state. */
+extern char *read_state_filename;
+extern char *write_state_filename;
+
+
 /* Print an error message.  */
 extern void error_at_line
   (const struct fileloc *pos, const char *msg, ...) ATTRIBUTE_PRINTF_2;
@@ -110,4 +143,19 @@  enum {
      a meaningful value to be printed.  */
   FIRST_TOKEN_WITH_VALUE = PARAM_IS
 };
-#endif
+
+
+/* For debugging purposes of gengtype itself!  */
+extern int do_dump;
+extern int do_debug;
+
+#if ENABLE_CHECKING
+#define dbgprintf(Fmt,...) do {if(do_debug)				\
+	    fprintf(stderr, "%s:%d: " Fmt "\n",				\
+		    lbasename(__FILE__),__LINE__, ##__VA_ARGS__);} while(0)
+#else
+#define dbgprintf(Fmt,...) do {/*nodebugprintf*/} while(0)
+#endif /*ENABLE_CHECKING*/
+
+#endif /*GCC_GENGTYPE_H*/
+
Index: gcc/Makefile.in
===================================================================
--- gcc/Makefile.in	(revision 163612)
+++ gcc/Makefile.in	(working copy)
@@ -3804,7 +3804,7 @@  s-gtyp-input: Makefile
 
 s-gtype: build/gengtype$(build_exeext) $(filter-out [%], $(GTFILES)) \
 	 gtyp-input.list
-	$(RUN_GEN) build/gengtype$(build_exeext) $(srcdir) gtyp-input.list
+	$(RUN_GEN) build/gengtype$(build_exeext) -S $(srcdir) -I gtyp-input.list
 	$(STAMP) s-gtype
 
 generated_files = config.h tm.h $(TM_P_H) $(TM_H) multilib.h \
@@ -3892,6 +3892,21 @@  build/gengtype-parse.o : gengtype-parse.c gengtype
   $(SYSTEM_H)
 build/gengtype.o : gengtype.c $(BCONFIG_H) $(SYSTEM_H) gengtype.h 	\
   rtl.def insn-notes.def errors.h double-int.h $(HASHTAB_H)
+
+## The build/version.o is needed at least for gengtype.
+ifdef REVISION_c
+build/version.o: version.c version.h $(REVISION) $(DATESTAMP) $(BASEVER) $(DEVPHASE)
+else
+build/version.o: version.c version.h $(DATESTAMP) $(BASEVER) $(DEVPHASE)
+endif
+
+## The build/version.o is compiled by the $(COMPILER_FOR_BUILD) so
+build/version.o: BUILD_CPPFLAGS +=  \
+	-DBASEVER=$(BASEVER_s) -DDATESTAMP=$(DATESTAMP_s) \
+	-DREVISION=$(REVISION_s) \
+	-DDEVPHASE=$(DEVPHASE_s) -DPKGVERSION=$(PKGVERSION_s) \
+	-DBUGURL=$(BUGURL_s) -c $(srcdir)/version.c $(OUTPUT_OPTION)
+
 build/genmddeps.o: genmddeps.c $(BCONFIG_H) $(SYSTEM_H) coretypes.h	\
   errors.h $(READ_MD_H)
 build/genmodes.o : genmodes.c $(BCONFIG_H) $(SYSTEM_H) errors.h		\
@@ -3935,7 +3950,9 @@  $(genprog:%=build/gen%$(build_exeext)): $(BUILD_ER
 build/genautomata$(build_exeext) : BUILD_LIBS += -lm
 
 # These programs are not linked with the MD reader.
-build/gengtype$(build_exeext) : build/gengtype-lex.o build/gengtype-parse.o
+build/gengtype$(build_exeext) : build/gengtype-lex.o build/gengtype-parse.o \
+               build/version.o
+
 build/genhooks$(build_exeext) : $(BUILD_ERRORS)
 
 # Generated source files for gengtype.