History

better command line parsing - added -r option

bellard committed 24 years ago

Commit 6dd6a021 parent d2115bf1 file · +74 −32Browse files

Unified · Split

tcc.c+74−32
@@ -7213,16 +7213,18 @@ void help(void) · 7212 unmodified lines
72137213 " -llib link with dynamic or static library 'lib'\n"
72147214 " -shared generate a shared library\n"
72157215 " -static static linking\n"
7216+ " -r relocatable output\n"
72167217 );
72177218 }
72187219
72197220 int main(int argc, char **argv)
72207221 {
72217222 char *r, *outfile;
7222− int optind, output_type, multiple_files, i;
7223+ int optind, output_type, multiple_files, i, reloc_output;
72237224 TCCState *s;
7224− char **libraries;
7225− int nb_libraries;
7225+ char **files;
7226+ int nb_files, nb_libraries, nb_objfiles;
7227+ char objfilename[1024];
72267228
72277229 s = tcc_new();
72287230 output_type = TCC_OUTPUT_MEMORY;
@@ -7230,23 +7232,37 @@ int main(int argc, char **argv) · 1 unmodified line
72307232 optind = 1;
72317233 outfile = NULL;
72327234 multiple_files = 0;
7233− libraries = NULL;
7235+ files = NULL;
7236+ nb_files = 0;
72347237 nb_libraries = 0;
7238+ reloc_output = 0;
72357239 while (1) {
72367240 if (optind >= argc) {
7237− show_help:
7238− help();
7239− return 1;
7241+ if (nb_files == 0)
7242+ goto show_help;
7243+ else
7244+ break;
72407245 }
7241− r = argv[optind];
7242− if (r[0] != '-')
7243− break;
7244− optind++;
7245− if (r[1] == '-') {
7246− /* '--' enables multiple files input */
7246+ r = argv[optind++];
7247+ if (r[0] != '-') {
7248+ /* add a new file */
7249+ dynarray_add((void ***)&files, &nb_files, r);
7250+ if (!multiple_files) {
7251+ optind--;
7252+ /* argv[0] will be this file */
7253+ break;
7254+ }
7255+ } else if (r[1] == '-') {
7256+ /* '--' enables multiple files input and also ends several file input */
7257+ if (multiple_files) {
7258+ optind--; /* argv[0] will be '--' */
7259+ break;
7260+ }
72477261 multiple_files = 1;
72487262 } else if (r[1] == 'h' || r[1] == '?') {
7249− goto show_help;
7263+ show_help:
7264+ help();
7265+ return 1;
72507266 } else if (r[1] == 'I') {
72517267 if (tcc_add_include_path(s, r + 2) < 0)
72527268 error("too many include paths");
@@ -7267,7 +7283,8 @@ int main(int argc, char **argv) · 14 unmodified lines
72677283 /* set tcc utilities path (mainly for tcc development) */
72687284 tcc_lib_path = r + 2;
72697285 } else if (r[1] == 'l') {
7270− dynarray_add((void ***)&libraries, &nb_libraries, r + 2);
7286+ dynarray_add((void ***)&files, &nb_files, r);
7287+ nb_libraries++;
72717288 } else if (!strcmp(r + 1, "bench")) {
72727289 do_bench = 1;
72737290 } else
@@ -7291,36 +7308,61 @@ int main(int argc, char **argv) · 17 unmodified lines
72917308 goto show_help;
72927309 multiple_files = 1;
72937310 outfile = argv[optind++];
7311+ } else if (r[1] == 'r') {
7312+ /* generate a .o merging several output files */
7313+ reloc_output = 1;
7314+ output_type = TCC_OUTPUT_OBJ;
72947315 } else {
72957316 error("invalid option -- '%s'", r);
72967317 }
72977318 }
72987319
7320+ nb_objfiles = nb_files - nb_libraries;
7321+
72997322 /* if outfile provided without other options, we output an
73007323 executable */
73017324 if (outfile && output_type == TCC_OUTPUT_MEMORY)
73027325 output_type = TCC_OUTPUT_EXE;
7303−
7304− tcc_set_output_type(s, output_type);
73057326
7306− tcc_add_file(s, argv[optind]);
7307− if (multiple_files) {
7308− while ((optind + 1) < argc) {
7309− optind++;
7310− r = argv[optind];
7311− if (r[0] == '-') {
7312− if (r[1] != '-')
7313− error("'--' expected");
7314− break;
7315− }
7316− tcc_add_file(s, r);
7327+ /* check -c consistency : only single file handled. XXX: checks file type */
7328+ if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
7329+ /* accepts only a single input file */
7330+ if (nb_objfiles != 1)
7331+ error("cannot specify multiple files with -c");
7332+ if (nb_libraries != 0)
7333+ error("cannot specify libraries with -c");
7334+ }
7335+
7336+ /* compute default outfile name */
7337+ if (output_type != TCC_OUTPUT_MEMORY && !outfile) {
7338+ if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
7339+ char *ext;
7340+ /* add .o extension */
7341+ pstrcpy(objfilename, sizeof(objfilename) - 1, files[0]);
7342+ ext = strrchr(objfilename, '.');
7343+ if (!ext)
7344+ goto default_outfile;
7345+ strcpy(ext + 1, "o");
7346+ } else {
7347+ default_outfile:
7348+ pstrcpy(objfilename, sizeof(objfilename), "a.out");
73177349 }
7350+ outfile = objfilename;
73187351 }
73197352
7320− /* add specified libraries */
7321− for(i = 0; i < nb_libraries;i++) {
7322− if (tcc_add_library(s, libraries[i]) < 0)
7323− error("cannot find -l%s", libraries[i]);
7353+ tcc_set_output_type(s, output_type);
7354+
7355+ /* compile or add each files or library */
7356+ for(i = 0;i < nb_files; i++) {
7357+ const char *filename;
7358+
7359+ filename = files[i];
7360+ if (filename[0] == '-') {
7361+ if (tcc_add_library(s, filename + 2) < 0)
7362+ error("cannot find %s", filename);
7363+ } else {
7364+ tcc_add_file(s, filename);
7365+ }
73247366 }
73257367
73267368 if (do_bench) {