00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "config.h"
00027 #include "system.h"
00028 #include "gcc.h"
00029
00030
00031 #define SPEC_FILE "libgcj.spec"
00032
00033
00034 #define LANGSPEC (1<<1)
00035
00036 #define PARAM_ARG (1<<2)
00037
00038 #define JAVA_FILE_ARG (1<<3)
00039
00040 #define CLASS_FILE_ARG (1<<4)
00041
00042 #define ZIP_FILE_ARG (1<<5)
00043
00044 #define INDIRECT_FILE_ARG (1<<6)
00045
00046 #define RESOURCE_FILE_ARG (1<<7)
00047
00048 static char *find_spec_file PARAMS ((const char *));
00049 static int verify_class_name PARAMS ((const char *));
00050
00051 static const char *main_class_name = NULL;
00052 int lang_specific_extra_outfiles = 0;
00053
00054
00055 int shared_libgcc = 1;
00056
00057 static const char jvgenmain_spec[] =
00058 "jvgenmain %{D*} %b %{!pipe:%u.i} |\n\
00059 cc1 %{!pipe:%U.i} %1 \
00060 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
00061 %{g*} %{O*} \
00062 %{v:-version} %{pg:-p} %{p}\
00063 %{<fbounds-check} %{<fno-bounds-check}\
00064 %{<fassume-compiled} %{<fno-assume-compiled}\
00065 %{<fcompile-resource*}\
00066 %{<femit-class-file} %{<femit-class-files} %{<fencoding*}\
00067 %{<fuse-boehm-gc} %{<fhash-synchronization} %{<fjni}\
00068 %{<findirect-dispatch} \
00069 %{<fno-store-check} %{<foutput-class-dir}\
00070 %{<fclasspath*} %{<fCLASSPATH*} %{<fbootclasspath*}\
00071 %{<fextdirs*}\
00072 %{<fuse-divide-subroutine} %{<fno-use-divide-subroutine}\
00073 %{<fcheck-references} %{<fno-check-references}\
00074 %{<ffilelist-file}\
00075 %{f*} -fdollars-in-identifiers\
00076 %{aux-info*}\
00077 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
00078 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
00079 %{!S:as %a %Y -o %d%w%u%O %{!pipe:%g.s} %A\n }";
00080
00081
00082
00083 static char *
00084 find_spec_file (dir)
00085 const char *dir;
00086 {
00087 char *spec;
00088 int x;
00089 struct stat sb;
00090
00091 spec = (char *) xmalloc (strlen (dir) + sizeof (SPEC_FILE)
00092 + sizeof ("-specs=") + 4);
00093 strcpy (spec, "-specs=");
00094 x = strlen (spec);
00095 strcat (spec, dir);
00096 strcat (spec, "/");
00097 strcat (spec, SPEC_FILE);
00098 if (! stat (spec + x, &sb))
00099 return spec;
00100 free (spec);
00101 return NULL;
00102 }
00103
00104
00105 #define JAVA_START_CHAR_P(c) (c < 128 && (ISIDST (c) || c == '$'))
00106 #define JAVA_PART_CHAR_P(c) (c < 128 \
00107 && (ISIDNUM (c) \
00108 || c == '$' \
00109 || (c >= 0x00 && c <= 0x08) \
00110 || (c >= 0x0e && c <= 0x1b) \
00111 || c == 0x7f))
00112
00113
00114
00115 static int
00116 verify_class_name (name)
00117 const char *name;
00118 {
00119
00120
00121 while (*name)
00122 {
00123 int ch = *name++;
00124 if (ch < 0 || ! JAVA_START_CHAR_P (ch))
00125 return 0;
00126 while (*name)
00127 {
00128 ch = *name++;
00129 if (ch < 0)
00130 return 0;
00131
00132
00133 if (ch == '.')
00134 break;
00135 if (! JAVA_PART_CHAR_P (ch))
00136 return 0;
00137 }
00138 }
00139
00140 return 1;
00141 }
00142
00143 void
00144 lang_specific_driver (in_argc, in_argv, in_added_libraries)
00145 int *in_argc;
00146 const char *const **in_argv;
00147 int *in_added_libraries;
00148 {
00149 int i, j;
00150
00151
00152 int saw_verbose_flag = 0;
00153
00154 int saw_save_temps = 0;
00155
00156
00157
00158 int library = 1;
00159
00160
00161
00162 int combine_inputs = 0;
00163
00164
00165 int last_input_index;
00166
00167
00168 int java_files_count = 0;
00169 int class_files_count = 0;
00170
00171 int zip_files_count = 0;
00172
00173 int indirect_files_count = 0;
00174
00175
00176 char *filelist_filename = 0;
00177
00178 FILE *filelist_file = 0;
00179
00180
00181
00182 int added = 2;
00183
00184
00185
00186 const char *quote = NULL;
00187
00188
00189 const char **arglist;
00190
00191
00192
00193
00194 int saw_speclang = 0;
00195
00196 #if 0
00197
00198 const char *saw_math ATTRIBUTE_UNUSED = 0;
00199
00200
00201 const char *saw_libc ATTRIBUTE_UNUSED = 0;
00202
00203
00204 const char *saw_gc ATTRIBUTE_UNUSED = 0;
00205
00206
00207 const char *saw_threadlib ATTRIBUTE_UNUSED = 0;
00208
00209
00210 int saw_libgcj ATTRIBUTE_UNUSED = 0;
00211 #endif
00212
00213
00214 int saw_resource = 0;
00215 int saw_C = 0;
00216 int saw_o = 0;
00217
00218
00219 int saw_O = 0;
00220 int saw_g = 0;
00221
00222
00223 int saw_D = 0;
00224
00225
00226
00227 int *args;
00228
00229
00230 int argc;
00231
00232
00233 const char *const *argv;
00234
00235
00236 int added_libraries;
00237
00238
00239 int num_args = 1;
00240
00241
00242 int will_link = 1;
00243
00244
00245 int want_spec_file = 1;
00246
00247
00248 char *spec_file = NULL;
00249
00250 argc = *in_argc;
00251 argv = *in_argv;
00252 added_libraries = *in_added_libraries;
00253
00254 args = (int *) xcalloc (argc, sizeof (int));
00255
00256 for (i = 1; i < argc; i++)
00257 {
00258
00259 if (quote)
00260 {
00261 quote = NULL;
00262 args[i] |= PARAM_ARG;
00263 continue;
00264 }
00265
00266
00267
00268 if (argv[i][0] == '\0' || argv[i][1] == '\0')
00269 continue;
00270
00271 if (argv[i][0] == '-')
00272 {
00273 if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
00274 || strcmp (argv[i], "-nodefaultlibs") == 0))
00275 {
00276 library = 0;
00277 }
00278 else if (strncmp (argv[i], "-fmain=", 7) == 0)
00279 {
00280 main_class_name = argv[i] + 7;
00281 added--;
00282 }
00283 else if (strcmp (argv[i], "-fhelp") == 0)
00284 want_spec_file = 0;
00285 else if (strcmp (argv[i], "-v") == 0)
00286 {
00287 saw_verbose_flag = 1;
00288 if (argc == 2)
00289 {
00290
00291
00292 library = 0;
00293 }
00294 }
00295 else if (strncmp (argv[i], "-x", 2) == 0)
00296 saw_speclang = 1;
00297 else if (strcmp (argv[i], "-C") == 0)
00298 {
00299 saw_C = 1;
00300 want_spec_file = 0;
00301 if (library != 0)
00302 added -= 2;
00303 library = 0;
00304 will_link = 0;
00305 }
00306 else if (strncmp (argv[i], "-fcompile-resource=", 19) == 0)
00307 {
00308 saw_resource = 1;
00309 want_spec_file = 0;
00310 if (library != 0)
00311 --added;
00312 library = 0;
00313 will_link = 0;
00314 }
00315 else if (argv[i][1] == 'D')
00316 saw_D = 1;
00317 else if (argv[i][1] == 'g')
00318 saw_g = 1;
00319 else if (argv[i][1] == 'O')
00320 saw_O = 1;
00321 else if ((argv[i][2] == '\0'
00322 && (char *)strchr ("bBVDUoeTuIYmLiA", argv[i][1]) != NULL)
00323 || strcmp (argv[i], "-Tdata") == 0
00324 || strcmp (argv[i], "-MT") == 0
00325 || strcmp (argv[i], "-MF") == 0)
00326 {
00327 if (strcmp (argv[i], "-o") == 0)
00328 saw_o = 1;
00329 quote = argv[i];
00330 }
00331 else if (strcmp(argv[i], "-classpath") == 0
00332 || strcmp(argv[i], "-bootclasspath") == 0
00333 || strcmp(argv[i], "-CLASSPATH") == 0)
00334 {
00335 quote = argv[i];
00336 added -= 1;
00337 }
00338 else if (library != 0
00339 && ((argv[i][2] == '\0'
00340 && (char *) strchr ("cSEM", argv[i][1]) != NULL)
00341 || strcmp (argv[i], "-MM") == 0))
00342 {
00343
00344
00345 library = 0;
00346 added -= 2;
00347
00348
00349 will_link = 0;
00350 }
00351 else if (strcmp (argv[i], "-d") == 0)
00352 {
00353
00354 quote = argv[i];
00355 added -= 1;
00356 }
00357 else if (strcmp (argv[i], "-fsyntax-only") == 0
00358 || strcmp (argv[i], "--syntax-only") == 0)
00359 {
00360 want_spec_file = 0;
00361 library = 0;
00362 will_link = 0;
00363 continue;
00364 }
00365 else if (strcmp (argv[i], "-save-temps") == 0)
00366 saw_save_temps = 1;
00367 else if (strcmp (argv[i], "-static-libgcc") == 0
00368 || strcmp (argv[i], "-static") == 0)
00369 shared_libgcc = 0;
00370 else
00371
00372 continue;
00373 }
00374 else
00375 {
00376 int len;
00377
00378 if (saw_speclang)
00379 {
00380 saw_speclang = 0;
00381 continue;
00382 }
00383
00384 if (saw_resource)
00385 {
00386 args[i] |= RESOURCE_FILE_ARG;
00387 last_input_index = i;
00388 added += 2;
00389 }
00390
00391 if (argv[i][0] == '@')
00392 {
00393 args[i] |= INDIRECT_FILE_ARG;
00394 indirect_files_count++;
00395 added += 2;
00396 }
00397
00398 len = strlen (argv[i]);
00399 if (len > 5 && strcmp (argv[i] + len - 5, ".java") == 0)
00400 {
00401 args[i] |= JAVA_FILE_ARG;
00402 java_files_count++;
00403 last_input_index = i;
00404 }
00405 if (len > 6 && strcmp (argv[i] + len - 6, ".class") == 0)
00406 {
00407 args[i] |= CLASS_FILE_ARG;
00408 class_files_count++;
00409 last_input_index = i;
00410 }
00411 if (len > 4
00412 && (strcmp (argv[i] + len - 4, ".zip") == 0
00413 || strcmp (argv[i] + len - 4, ".jar") == 0))
00414 {
00415 args[i] |= ZIP_FILE_ARG;
00416 zip_files_count++;
00417 last_input_index = i;
00418 }
00419 }
00420 }
00421
00422 if (quote)
00423 fatal ("argument to `%s' missing\n", quote);
00424
00425 if (saw_D && ! main_class_name)
00426 fatal ("can't specify `-D' without `--main'\n");
00427
00428 if (main_class_name && ! verify_class_name (main_class_name))
00429 fatal ("`%s' is not a valid class name", main_class_name);
00430
00431 num_args = argc + added;
00432 if (saw_resource)
00433 {
00434 if (! saw_o)
00435 fatal ("--resource requires -o");
00436 }
00437 if (saw_C)
00438 {
00439 num_args += 3;
00440 if (class_files_count + zip_files_count > 0)
00441 {
00442 error ("warning: already-compiled .class files ignored with -C");
00443 num_args -= class_files_count + zip_files_count;
00444 class_files_count = 0;
00445 zip_files_count = 0;
00446 }
00447 num_args += 2;
00448 if (saw_o)
00449 fatal ("cannot specify both -C and -o");
00450 }
00451 if ((saw_o && java_files_count + class_files_count + zip_files_count > 1)
00452 || (saw_C && java_files_count > 1)
00453 || (indirect_files_count > 0
00454 && java_files_count + class_files_count + zip_files_count > 0))
00455 combine_inputs = 1;
00456
00457 if (combine_inputs)
00458 {
00459 filelist_filename = make_temp_file ("jx");
00460 if (filelist_filename == NULL)
00461 fatal ("cannot create temporary file");
00462 record_temp_file (filelist_filename, ! saw_save_temps, 0);
00463 filelist_file = fopen (filelist_filename, "w");
00464 if (filelist_file == NULL)
00465 pfatal_with_name (filelist_filename);
00466 num_args -= java_files_count + class_files_count + zip_files_count;
00467 num_args += 2;
00468 }
00469
00470 #if 0
00471 if (! added && ! library && main_class_name == NULL && ! saw_C)
00472 {
00473 free (args);
00474 return;
00475 }
00476 #endif
00477
00478 if (main_class_name)
00479 {
00480 lang_specific_extra_outfiles++;
00481 }
00482 if (saw_g + saw_O == 0)
00483 num_args++;
00484 num_args++;
00485
00486 if (combine_inputs || indirect_files_count > 0)
00487 num_args += 1;
00488 if (combine_inputs && indirect_files_count > 0)
00489 fatal("using both @FILE with multiple files not implemented");
00490
00491
00492
00493 #ifndef ENABLE_SHARED_LIBGCC
00494 shared_libgcc = 0;
00495 #endif
00496
00497 num_args += shared_libgcc;
00498
00499 arglist = (const char **) xmalloc ((num_args + 1) * sizeof (char *));
00500 j = 0;
00501
00502 for (i = 0; i < argc; i++, j++)
00503 {
00504 arglist[j] = argv[i];
00505
00506 if ((args[i] & PARAM_ARG) || i == 0)
00507 continue;
00508
00509 if ((args[i] & RESOURCE_FILE_ARG) != 0)
00510 {
00511 arglist[j++] = "-xjava";
00512 arglist[j++] = argv[i];
00513 arglist[j] = "-xnone";
00514 }
00515
00516 if (strcmp (argv[i], "-classpath") == 0
00517 || strcmp (argv[i], "-bootclasspath") == 0
00518 || strcmp (argv[i], "-CLASSPATH") == 0)
00519 {
00520 arglist[j] = concat ("-f", argv[i]+1, "=", argv[i+1], NULL);
00521 i++;
00522 continue;
00523 }
00524
00525 if (strcmp (argv[i], "-d") == 0)
00526 {
00527 arglist[j] = concat ("-foutput-class-dir=", argv[i + 1], NULL);
00528 ++i;
00529 continue;
00530 }
00531
00532 if (spec_file == NULL && strncmp (argv[i], "-L", 2) == 0)
00533 spec_file = find_spec_file (argv[i] + 2);
00534
00535 if (strncmp (argv[i], "-fmain=", 7) == 0)
00536 {
00537 if (! will_link)
00538 fatal ("cannot specify `main' class when not linking");
00539 --j;
00540 continue;
00541 }
00542
00543 if ((args[i] & INDIRECT_FILE_ARG) != 0)
00544 {
00545 arglist[j++] = "-xjava";
00546 arglist[j++] = argv[i]+1;
00547 arglist[j] = "-xnone";
00548 }
00549
00550 if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
00551 {
00552 --j;
00553 continue;
00554 }
00555
00556 if (combine_inputs
00557 && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
00558 {
00559 fputs (argv[i], filelist_file);
00560 fputc ('\n', filelist_file);
00561 --j;
00562 continue;
00563 }
00564 }
00565
00566 if (combine_inputs || indirect_files_count > 0)
00567 arglist[j++] = "-ffilelist-file";
00568
00569 if (combine_inputs)
00570 {
00571 if (fclose (filelist_file))
00572 pfatal_with_name (filelist_filename);
00573 arglist[j++] = "-xjava";
00574 arglist[j++] = filelist_filename;
00575 }
00576
00577
00578 if (saw_g + saw_O == 0)
00579 arglist[j++] = "-g1";
00580
00581
00582
00583
00584 if (want_spec_file)
00585 arglist[j++] = spec_file == NULL ? "-specs=libgcj.spec" : spec_file;
00586
00587 if (saw_C)
00588 {
00589 arglist[j++] = "-fsyntax-only";
00590 arglist[j++] = "-femit-class-files";
00591 arglist[j++] = "-S";
00592 arglist[j++] = "-o";
00593 arglist[j++] = "NONE";
00594 }
00595
00596 if (shared_libgcc)
00597 arglist[j++] = "-shared-libgcc";
00598
00599 arglist[j] = NULL;
00600
00601 *in_argc = j;
00602 *in_argv = arglist;
00603 *in_added_libraries = added_libraries;
00604 }
00605
00606 int
00607 lang_specific_pre_link ()
00608 {
00609 int err;
00610 if (main_class_name == NULL)
00611 return 0;
00612
00613
00614
00615
00616
00617
00618
00619 set_input (concat (main_class_name, "main.c", NULL));
00620 err = do_spec (jvgenmain_spec);
00621 if (err == 0)
00622 {
00623
00624
00625
00626
00627 int i = n_infiles;
00628 const char *generated = outfiles[i];
00629 while (--i >= 0)
00630 outfiles[i + 1] = outfiles[i];
00631 outfiles[0] = generated;
00632 }
00633 return err;
00634 }