Consolidate CLI-arg processing. Provide routines from Charmonizer itself for parsing command line arguments, rather than handling that task in each individual charmonizer.c.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/8b291d2f Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/8b291d2f Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/8b291d2f Branch: refs/heads/chaz_compiler_flags Commit: 8b291d2fc442ed0293cccb9e421dd59014724d25 Parents: b2f9eb4 Author: Marvin Humphrey <[email protected]> Authored: Thu Dec 6 18:16:58 2012 -0800 Committer: Marvin Humphrey <[email protected]> Committed: Mon Dec 10 18:33:53 2012 -0800 ---------------------------------------------------------------------- charmonizer/charmonize.c | 21 ++--- charmonizer/src/Charmonizer/Probe.c | 111 +++++++++++++++++++++-- charmonizer/src/Charmonizer/Probe.h | 41 +++++++-- clownfish/compiler/common/charmonizer.main | 65 ++------------ clownfish/runtime/common/charmonizer.main | 92 ++----------------- common/charmonizer.main | 92 ++----------------- 6 files changed, 172 insertions(+), 250 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/8b291d2f/charmonizer/charmonize.c ---------------------------------------------------------------------- diff --git a/charmonizer/charmonize.c b/charmonizer/charmonize.c index 1c656af..8c32c5d 100644 --- a/charmonizer/charmonize.c +++ b/charmonizer/charmonize.c @@ -110,19 +110,14 @@ S_parse_arguments(int argc, char **argv, struct CLIArgs *args) { } int main(int argc, char **argv) { - struct CLIArgs args; - memset(&args, 0, sizeof(struct CLIArgs)); - - S_parse_arguments(argc, argv, &args); - chaz_Probe_init(args.cc_command, args.cc_flags); - if (args.enable_c) { - chaz_ConfWriterC_enable(); - } - if (args.enable_perl) { - chaz_ConfWriterPerl_enable(); - } - if (args.enable_ruby) { - chaz_ConfWriterRuby_enable(); + /* Initialize. */ + { + struct chaz_CLIArgs args; + int result = chaz_Probe_parse_cli_args(argc, argv, &args); + if (!result) { + chaz_Probe_die_usage(); + } + chaz_Probe_init(&args); } /* Run probe modules. */ http://git-wip-us.apache.org/repos/asf/lucy/blob/8b291d2f/charmonizer/src/Charmonizer/Probe.c ---------------------------------------------------------------------- diff --git a/charmonizer/src/Charmonizer/Probe.c b/charmonizer/src/Charmonizer/Probe.c index 18176f7..dc99875 100644 --- a/charmonizer/src/Charmonizer/Probe.c +++ b/charmonizer/src/Charmonizer/Probe.c @@ -20,24 +20,123 @@ #include "Charmonizer/Probe.h" #include "Charmonizer/Core/HeaderChecker.h" #include "Charmonizer/Core/ConfWriter.h" +#include "Charmonizer/Core/ConfWriterC.h" +#include "Charmonizer/Core/ConfWriterPerl.h" +#include "Charmonizer/Core/ConfWriterRuby.h" #include "Charmonizer/Core/Util.h" #include "Charmonizer/Core/Compiler.h" #include "Charmonizer/Core/OperatingSystem.h" +int +chaz_Probe_parse_cli_args(int argc, const char *argv[], + struct chaz_CLIArgs *args) { + int i; + int output_enabled = 0; + + /* Zero out args struct. */ + memset(args, 0, sizeof(struct chaz_CLIArgs)); + + /* Parse most args. */ + for (i = 1; i < argc; i++) { + const char *arg = argv[i]; + if (strcmp(arg, "--") == 0) { + /* From here on out, everything will be a compiler flag. */ + i++; + break; + } + if (strcmp(arg, "--enable-c") == 0) { + args->charmony_h = 1; + output_enabled = 1; + } + else if (strcmp(arg, "--enable-perl") == 0) { + args->charmony_pm = 1; + output_enabled = 1; + } + else if (strcmp(arg, "--enable-ruby") == 0) { + args->charmony_rb = 1; + output_enabled = 1; + } + else if (memcmp(arg, "--cc=", 5) == 0) { + if (strlen(arg) > CHAZ_PROBE_MAX_CC_LEN - 5) { + fprintf(stderr, "Exceeded max length for compiler command"); + exit(1); + } + strcpy(args->cc, arg + 5); + } + } /* preserve value of i */ + + /* Accumulate compiler flags. */ + for (; i < argc; i++) { + const char *arg = argv[i]; + size_t new_len = strlen(arg) + strlen(args->ccflags) + 2; + if (new_len >= CHAZ_PROBE_MAX_FLAGS_LEN) { + fprintf(stderr, "Exceeded max length for compiler flags"); + exit(1); + } + strcat(args->ccflags, " "); + strcat(args->ccflags, arg); + } + + /* Process CHARM_VERBOSITY environment variable. */ + { + const char *verbosity_env = getenv("CHARM_VERBOSITY"); + if (verbosity_env && strlen(verbosity_env)) { + args->verbosity = strtol(verbosity_env, NULL, 10); + } + } + + /* Validate. */ + if (!strlen(args->cc) || !output_enabled) { + return false; + } + + return true; +} + void -chaz_Probe_init(const char *cc_command, const char *cc_flags) { - /* Proces CHARM_VERBOSITY environment variable. */ - const char *verbosity_env = getenv("CHARM_VERBOSITY"); - if (verbosity_env && strlen(verbosity_env)) { - chaz_Util_verbosity = strtol(verbosity_env, NULL, 10); +chaz_Probe_die_usage(void) { + fprintf(stderr, + "Usage: ./charmonize --cc=CC_COMMAND [--enable-c] " + "[--enable-perl] [--enable-ruby] -- CC_FLAGS\n"); + exit(1); +} + +void +chaz_Probe_init(struct chaz_CLIArgs *args) { + int output_enabled = 0; + + { + /* Process CHARM_VERBOSITY environment variable. */ + const char *verbosity_env = getenv("CHARM_VERBOSITY"); + if (verbosity_env && strlen(verbosity_env)) { + chaz_Util_verbosity = strtol(verbosity_env, NULL, 10); + } } /* Dispatch other initializers. */ chaz_OS_init(); - chaz_CC_init(cc_command, cc_flags); + chaz_CC_init(args->cc, args->ccflags); chaz_ConfWriter_init(); chaz_HeadCheck_init(); + /* Enable output. */ + if (args->charmony_h) { + chaz_ConfWriterC_enable(); + output_enabled = true; + } + if (args->charmony_pm) { + chaz_ConfWriterPerl_enable(); + output_enabled = true; + } + if (args->charmony_rb) { + chaz_ConfWriterRuby_enable(); + output_enabled = true; + } + if (!output_enabled) { + fprintf(stderr, "No output formats enabled\n"); + exit(1); + } + if (chaz_Util_verbosity) { printf("Initialization complete.\n"); } } http://git-wip-us.apache.org/repos/asf/lucy/blob/8b291d2f/charmonizer/src/Charmonizer/Probe.h ---------------------------------------------------------------------- diff --git a/charmonizer/src/Charmonizer/Probe.h b/charmonizer/src/Charmonizer/Probe.h index ee80130..6214ba5 100644 --- a/charmonizer/src/Charmonizer/Probe.h +++ b/charmonizer/src/Charmonizer/Probe.h @@ -24,8 +24,40 @@ extern "C" { #include <stddef.h> #include <stdio.h> -/* Set up the Charmonizer environment. This should be called before anything - * else. +#define CHAZ_PROBE_MAX_CC_LEN 100 +#define CHAZ_PROBE_MAX_FLAGS_LEN 2000 + +struct chaz_CLIArgs { + char cc[CHAZ_PROBE_MAX_CC_LEN + 1]; + char ccflags[CHAZ_PROBE_MAX_FLAGS_LEN + 1]; + int charmony_h; + int charmony_pm; + int charmony_rb; + int verbosity; +}; + +/* Parse command line arguments, initializing and filling in the supplied + * `args` struct. + * + * APP_NAME --cc=CC_COMMAND + * [--enable-c] + * [--enable-perl] + * [--enable-ruby] + * [-- [CC_FLAGS]] + * + * @return true if argument parsing proceeds without incident, false if + * unexpected arguments are encountered or values are missing or invalid. + */ +int +chaz_Probe_parse_cli_args(int argc, const char *argv[], + struct chaz_CLIArgs *args); + +/* Exit after printing usage instructions to stderr. + */ +void +chaz_Probe_die_usage(void); + +/* Set up the Charmonizer environment. * * If the environment variable CHARM_VERBOSITY has been set, it will be * processed at this time: @@ -33,12 +65,9 @@ extern "C" { * 0 - silent * 1 - normal * 2 - debugging - * - * @param cc_command the string used to invoke the C compiler via system() - * @param cc_flags flags which will be passed on to the C compiler */ void -chaz_Probe_init(const char *cc_command, const char *cc_flags); +chaz_Probe_init(struct chaz_CLIArgs *args); /* Clean up the Charmonizer environment -- deleting tempfiles, etc. This * should be called only after everything else finishes. http://git-wip-us.apache.org/repos/asf/lucy/blob/8b291d2f/clownfish/compiler/common/charmonizer.main ---------------------------------------------------------------------- diff --git a/clownfish/compiler/common/charmonizer.main b/clownfish/compiler/common/charmonizer.main index d9902ea..40e4a2f 100644 --- a/clownfish/compiler/common/charmonizer.main +++ b/clownfish/compiler/common/charmonizer.main @@ -23,66 +23,17 @@ #include "Charmonizer/Probe.h" #include "Charmonizer/Probe/Integers.h" -#define MAX_CC_LEN 128 -#define MAX_FLAGS_LEN 2048 - -struct CLIArgs { - char cc_command[MAX_CC_LEN + 1]; - char cc_flags[MAX_FLAGS_LEN + 1]; -}; - -/* Parse command line arguments. */ -static void -S_parse_arguments(int argc, char **argv, struct CLIArgs *args) { - int i; - - /* Parse most args. */ - for (i = 1; i < argc; i++) { - char *arg = argv[i]; - if (strcmp(arg, "--") == 0) { - /* From here on out, everything will be a compiler flag. */ - i++; - break; - } - if (memcmp(arg, "--cc=", 5) == 0) { - if (strlen(arg) > MAX_CC_LEN - 5) { - fprintf(stderr, "Exceeded max length for compiler command"); - exit(1); - } - strcpy(args->cc_command, arg + 5); - } - } - - /* Accumulate compiler flags. */ - for (; i < argc; i++) { - char *arg = argv[i]; - if (strlen(arg) + strlen(args->cc_flags) + 2 >= MAX_FLAGS_LEN) { - fprintf(stderr, "Exceeded max length for compiler flags"); - exit(1); +int main(int argc, const char **argv) { + /* Initialize. */ + { + struct chaz_CLIArgs args; + int result = chaz_Probe_parse_cli_args(argc, argv, &args); + if (!result) { + chaz_Probe_die_usage(); } - strcat(args->cc_flags, " "); - strcat(args->cc_flags, arg); + chaz_Probe_init(&args); } - /* Validate. */ - if (!args->cc_command - || !strlen(args->cc_command) - ) { - fprintf(stderr, - "Usage: ./charmonizer --cc=CC_COMMAND -- CC_FLAGS\n"); - exit(1); - } - -} - -int main(int argc, char **argv) { - struct CLIArgs args; - memset(&args, 0, sizeof(struct CLIArgs)); - - S_parse_arguments(argc, argv, &args); - chaz_Probe_init(args.cc_command, args.cc_flags); - chaz_ConfWriterC_enable(); - /* Run probe modules. */ chaz_Integers_run(); http://git-wip-us.apache.org/repos/asf/lucy/blob/8b291d2f/clownfish/runtime/common/charmonizer.main ---------------------------------------------------------------------- diff --git a/clownfish/runtime/common/charmonizer.main b/clownfish/runtime/common/charmonizer.main index 477b451..de9ba04 100644 --- a/clownfish/runtime/common/charmonizer.main +++ b/clownfish/runtime/common/charmonizer.main @@ -38,91 +38,15 @@ #include "Charmonizer/Core/ConfWriterPerl.h" #include "Charmonizer/Core/ConfWriterRuby.h" -#define MAX_CC_LEN 128 -#define MAX_FLAGS_LEN 2048 - -struct CLIArgs { - char cc_command[MAX_CC_LEN + 1]; - char cc_flags[MAX_FLAGS_LEN + 1]; - int enable_c; - int enable_perl; - int enable_ruby; -}; - -/* Parse command line arguments. */ -static void -S_parse_arguments(int argc, char **argv, struct CLIArgs *args) { - int i; - int output_enabled = 0; - - /* Parse most args. */ - for (i = 1; i < argc; i++) { - char *arg = argv[i]; - if (strcmp(arg, "--") == 0) { - /* From here on out, everything will be a compiler flag. */ - i++; - break; - } - if (strcmp(arg, "--enable-c") == 0) { - args->enable_c = 1; - output_enabled = 1; - } - else if (strcmp(arg, "--enable-perl") == 0) { - args->enable_perl = 1; - output_enabled = 1; - } - else if (strcmp(arg, "--enable-ruby") == 0) { - args->enable_ruby = 1; - output_enabled = 1; - } - else if (memcmp(arg, "--cc=", 5) == 0) { - if (strlen(arg) > MAX_CC_LEN - 5) { - fprintf(stderr, "Exceeded max length for compiler command"); - exit(1); - } - strcpy(args->cc_command, arg + 5); - } - } - - /* Accumulate compiler flags. */ - for (; i < argc; i++) { - char *arg = argv[i]; - if (strlen(arg) + strlen(args->cc_flags) + 2 >= MAX_FLAGS_LEN) { - fprintf(stderr, "Exceeded max length for compiler flags"); - exit(1); +int main(int argc, const char **argv) { + /* Initialize. */ + { + struct chaz_CLIArgs args; + int result = chaz_Probe_parse_cli_args(argc, argv, &args); + if (!result) { + chaz_Probe_die_usage(); } - strcat(args->cc_flags, " "); - strcat(args->cc_flags, arg); - - } - - /* Validate. */ - if (!args->cc_command - || !strlen(args->cc_command) - || !output_enabled - ) { - fprintf(stderr, - "Usage: ./charmonize --cc=CC_COMMAND [--enable-c] " - "[--enable-perl] [--enable-ruby] -- CC_FLAGS\n"); - exit(1); - } - -} - -int main(int argc, char **argv) { - struct CLIArgs args; - memset(&args, 0, sizeof(struct CLIArgs)); - - S_parse_arguments(argc, argv, &args); - chaz_Probe_init(args.cc_command, args.cc_flags); - if (args.enable_c) { - chaz_ConfWriterC_enable(); - } - if (args.enable_perl) { - chaz_ConfWriterPerl_enable(); - } - if (args.enable_ruby) { - chaz_ConfWriterRuby_enable(); + chaz_Probe_init(&args); } /* Run probe modules. */ http://git-wip-us.apache.org/repos/asf/lucy/blob/8b291d2f/common/charmonizer.main ---------------------------------------------------------------------- diff --git a/common/charmonizer.main b/common/charmonizer.main index b9463ac..2ba57eb 100644 --- a/common/charmonizer.main +++ b/common/charmonizer.main @@ -38,91 +38,15 @@ #include "Charmonizer/Core/ConfWriterPerl.h" #include "Charmonizer/Core/ConfWriterRuby.h" -#define MAX_CC_LEN 128 -#define MAX_FLAGS_LEN 2048 - -struct CLIArgs { - char cc_command[MAX_CC_LEN + 1]; - char cc_flags[MAX_FLAGS_LEN + 1]; - int enable_c; - int enable_perl; - int enable_ruby; -}; - -/* Parse command line arguments. */ -static void -S_parse_arguments(int argc, char **argv, struct CLIArgs *args) { - int i; - int output_enabled = 0; - - /* Parse most args. */ - for (i = 1; i < argc; i++) { - char *arg = argv[i]; - if (strcmp(arg, "--") == 0) { - /* From here on out, everything will be a compiler flag. */ - i++; - break; - } - if (strcmp(arg, "--enable-c") == 0) { - args->enable_c = 1; - output_enabled = 1; - } - else if (strcmp(arg, "--enable-perl") == 0) { - args->enable_perl = 1; - output_enabled = 1; - } - else if (strcmp(arg, "--enable-ruby") == 0) { - args->enable_ruby = 1; - output_enabled = 1; - } - else if (memcmp(arg, "--cc=", 5) == 0) { - if (strlen(arg) > MAX_CC_LEN - 5) { - fprintf(stderr, "Exceeded max length for compiler command"); - exit(1); - } - strcpy(args->cc_command, arg + 5); - } - } - - /* Accumulate compiler flags. */ - for (; i < argc; i++) { - char *arg = argv[i]; - if (strlen(arg) + strlen(args->cc_flags) + 2 >= MAX_FLAGS_LEN) { - fprintf(stderr, "Exceeded max length for compiler flags"); - exit(1); +int main(int argc, const char **argv) { + /* Initialize. */ + { + struct chaz_CLIArgs args; + int result = chaz_Probe_parse_cli_args(argc, argv, &args); + if (!result) { + chaz_Probe_die_usage(); } - strcat(args->cc_flags, " "); - strcat(args->cc_flags, arg); - - } - - /* Validate. */ - if (!args->cc_command - || !strlen(args->cc_command) - || !output_enabled - ) { - fprintf(stderr, - "Usage: ./charmonize --cc=CC_COMMAND [--enable-c] " - "[--enable-perl] [--enable-ruby] -- CC_FLAGS\n"); - exit(1); - } - -} - -int main(int argc, char **argv) { - struct CLIArgs args; - memset(&args, 0, sizeof(struct CLIArgs)); - - S_parse_arguments(argc, argv, &args); - chaz_Probe_init(args.cc_command, args.cc_flags); - if (args.enable_c) { - chaz_ConfWriterC_enable(); - } - if (args.enable_perl) { - chaz_ConfWriterPerl_enable(); - } - if (args.enable_ruby) { - chaz_ConfWriterRuby_enable(); + chaz_Probe_init(&args); } /* Run probe modules. */
