From patchwork Wed Jun 16 13:11:34 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Basile Starynkevitch X-Patchwork-Id: 55883 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 9F376B7D5C for ; Wed, 16 Jun 2010 23:11:55 +1000 (EST) Received: (qmail 13514 invoked by alias); 16 Jun 2010 13:11:53 -0000 Received: (qmail 13505 invoked by uid 22791); 16 Jun 2010 13:11:52 -0000 X-SWARE-Spam-Status: No, hits=-1.7 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, TW_CP, TW_RG X-Spam-Check-By: sourceware.org Received: from smtp-103-wednesday.noc.nerim.net (HELO mallaury.nerim.net) (62.4.17.103) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 16 Jun 2010 13:11:37 +0000 Received: from hector.lesours (ours.starynkevitch.net [213.41.244.95]) by mallaury.nerim.net (Postfix) with ESMTP id BBABAA1065 for ; Wed, 16 Jun 2010 15:11:33 +0200 (CEST) Received: from basile18 by hector.lesours with local (Exim 4.71) (envelope-from ) id 1OOsP0-0001bU-IM for gcc-patches@gcc.gnu.org; Wed, 16 Jun 2010 15:11:34 +0200 Date: Wed, 16 Jun 2010 15:11:34 +0200 From: Basile Starynkevitch To: gcc-patches@gcc.gnu.org Subject: [Trunk] patch for GNU-friendly gengtype Message-ID: <20100616131134.GA5966@hector.lesours> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Hello All We need to improve gengtype, in particular so that it will (later) be installed -eg as gcc-gengtype- and be usable in plugin mode without requiring the entire GCC build or source tree. See http://gcc.gnu.org/ml/gcc/2009-06/msg00416.html and more. A first step is to make gengtype more GNU friendly, that is accept --help & --version program arguments, and change its program invocation conventions. The attached patch to trunk revison 160833 seems to work (on x86-64/Linux). gcc/Changelog entry: 2010-06-16 Basile Starynkevitch * Makefile.in (gengtype.o): Pass version information strings as -DGENGTYPE_* preprocessor flags. * gengtype.c: include getopt.h. (do_dump): Added static variable moved from main. (inputlist): Ditto. (plugin_output_filename): Ditto. (gengtype_long_options): New variable. (print_usage): Added function. (bug_report_url, version_string, pkgversion_string): Added variable. (print_version): Added function. (parse_program_options): Added new function. (main): Changed program invocation convention. Call parse_program_options. Removed old argument parsing. ############## I was not able to add a dependency to version.o for build/gengtype.o (this caused circularities in make), so I pasted code from version.c to gengtype.c Comments are welcome. Bootstrapped on x86-64/Linux (Ubuntu/Lucid). Ok for trunk? Cheers Index: gcc/gengtype.c =================================================================== --- gcc/gengtype.c (revision 160833) +++ gcc/gengtype.c (working copy) @@ -21,9 +21,11 @@ #include "bconfig.h" #include "system.h" #include "gengtype.h" +#include "getopt.h" #include "errors.h" /* for fatal */ #include "double-int.h" #include "hashtab.h" +#include "version.h" /* for version_string & pkgversion_string */ /* Data types, macros, etc. used only in this file. */ @@ -145,10 +147,12 @@ /* The list of output files. */ static outf_p output_files; -/* The plugin input files and their number; in that case only - a single file is produced. */ +/* The plugin input files and their number; in that case only a single + file is produced. Each element of plugin_files should have an all + zero lang_bitmap. */ static char** plugin_files; static size_t nb_plugin_files; + /* the generated plugin output name & file */ static outf_p plugin_output; @@ -162,6 +166,10 @@ /* Length of srcdir name. */ static size_t srcdir_len = 0; +static int do_dump = 0; +static char* inputlist = NULL; +static char* plugin_output_filename = NULL; + static outf_p create_file (const char *, const char *); static const char * get_file_basename (const char *); @@ -4158,54 +4166,129 @@ } +/* 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' }, + { "plugin", required_argument, NULL, 'P' }, + { "srcdir", required_argument, NULL, 'S' }, + { "inputs", required_argument, NULL, 'I' }, + { NULL, no_argument, NULL, 0 }, +}; + + +static void +print_usage (void) +{ + fprintf (stderr, "Usage: %s\n", progname); + fprintf (stderr, "\t -h | --help \t# Give this help.\n"); + fprintf (stderr, "\t -v | --version \t# Give version information.\n"); + fprintf (stderr, "\t -d | --dump \t# Dump state for debugging.\n"); + fprintf (stderr, "\t -P | --plugin \t# Generate for a plugin.\n"); + fprintf (stderr, "\t -S | --srcdir \t# Specify the GCC source directory.\n"); + fprintf (stderr, "\t -I | --inputs \t# Specify the file with source files list.\n"); +} + + +/* This is the location of the online document giving instructions for + reporting bugs. If you distribute a modified version of GCC, + please configure with --with-bugurl pointing to a document giving + instructions for reporting bugs to you, not us. (You are of course + welcome to forward us bugs reported to you, if you determine that + they are not bugs in your modifications.) */ +const char bug_report_url[] = GENGTYPE_BUGURL; +/* The complete version string, assembled from several pieces. + GENGTYPE_BASEVER, GENGTYPE_DATESTAMP, GENGTYPE_DEVPHASE, and + GENGTYPE_REVISION are defined by the Makefile. */ + +const char version_string[] = GENGTYPE_BASEVER GENGTYPE_DATESTAMP GENGTYPE_DEVPHASE GENGTYPE_REVISION; +const char pkgversion_string[] = GENGTYPE_PKGVERSION; + +static void +print_version (void) +{ + /* Since we cannot depend upon version.o, we have to duplicate + version information here. */ + fprintf (stderr, "%s %s%s\n", progname, pkgversion_string, version_string); + fprintf (stderr, "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:", + 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 '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; + 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++) + { + /* Place an all zero lang_bitmap before the plugin file + name. */ + char *name = argv[i + optind]; + int len = strlen(name) + 1 + sizeof (lang_bitmap); + plugin_files[i] = XCNEWVEC (char, len) + sizeof (lang_bitmap); + strcpy (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"; + progname = (argc>0)?argv[0]:"gengtype"; + parse_program_options (argc, argv); - if (argc >= 2 && !strcmp (argv[1], "-d")) - { - do_dump = 1; - argv = &argv[1]; - argc--; - } - - if (argc >= 6 && !strcmp (argv[1], "-P")) - { - 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++) - { - /* Place an all zero lang_bitmap before the plugin file - name. */ - char *name = argv[i + 5]; - int len = strlen(name) + 1 + sizeof (lang_bitmap); - plugin_files[i] = XCNEWVEC (char, len) + sizeof (lang_bitmap); - 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); - read_input_list (inputlist); if (hit_error) return 1; Index: gcc/Makefile.in =================================================================== --- gcc/Makefile.in (revision 160833) +++ gcc/Makefile.in (working copy) @@ -3745,7 +3745,7 @@ 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 \ @@ -3831,8 +3831,17 @@ build/gengtype-lex.o : gengtype-lex.c gengtype.h $(BCONFIG_H) $(SYSTEM_H) build/gengtype-parse.o : gengtype-parse.c gengtype.h $(BCONFIG_H) \ $(SYSTEM_H) + +# gengtype cannot depend of version.o, otherwise a circular make +# dependency appears. build/gengtype.o : gengtype.c $(BCONFIG_H) $(SYSTEM_H) gengtype.h \ - rtl.def insn-notes.def errors.h double-int.h $(HASHTAB_H) + rtl.def insn-notes.def errors.h double-int.h $(HASHTAB_H) \ + $(REVISION) $(DATESTAMP) $(BASEVER) + $(COMPILER) $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) \ + -DGENGTYPE_BASEVER=$(BASEVER_s) -DGENGTYPE_DATESTAMP=$(DATESTAMP_s) \ + -DGENGTYPE_REVISION=$(REVISION_s) \ + -DGENGTYPE_DEVPHASE=$(DEVPHASE_s) -DGENGTYPE_PKGVERSION=$(PKGVERSION_s) \ + -DGENGTYPE_BUGURL=$(BUGURL_s) -c $(srcdir)/gengtype.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 \ @@ -3875,7 +3884,7 @@ 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 # Generated source files for gengtype. gengtype-lex.c : gengtype-lex.l