1 1.1 mrg /* Specific flags and argument handling of the Fortran front-end. 2 1.1 mrg Copyright (C) 1997-2022 Free Software Foundation, Inc. 3 1.1 mrg 4 1.1 mrg This file is part of GCC. 5 1.1 mrg 6 1.1 mrg GNU CC is free software; you can redistribute it and/or modify 7 1.1 mrg it under the terms of the GNU General Public License as published by 8 1.1 mrg the Free Software Foundation; either version 3, or (at your option) 9 1.1 mrg any later version. 10 1.1 mrg 11 1.1 mrg GNU CC is distributed in the hope that it will be useful, 12 1.1 mrg but WITHOUT ANY WARRANTY; without even the implied warranty of 13 1.1 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 1.1 mrg GNU General Public License for more details. 15 1.1 mrg 16 1.1 mrg You should have received a copy of the GNU General Public License 17 1.1 mrg along with GCC; see the file COPYING3. If not see 18 1.1 mrg <http://www.gnu.org/licenses/>. */ 19 1.1 mrg 20 1.1 mrg /* This file is copied more or less verbatim from g77. */ 21 1.1 mrg /* This file contains a filter for the main `gcc' driver, which is 22 1.1 mrg replicated for the `gfortran' driver by adding this filter. The purpose 23 1.1 mrg of this filter is to be basically identical to gcc (in that 24 1.1 mrg it faithfully passes all of the original arguments to gcc) but, 25 1.1 mrg unless explicitly overridden by the user in certain ways, ensure 26 1.1 mrg that the needs of the language supported by this wrapper are met. 27 1.1 mrg 28 1.1 mrg For GNU Fortran 95(gfortran), we do the following to the argument list 29 1.1 mrg before passing it to `gcc': 30 1.1 mrg 31 1.1 mrg 1. Make sure `-lgfortran -lm' is at the end of the list. 32 1.1 mrg 33 1.1 mrg 2. Make sure each time `-lgfortran' or `-lm' is seen, it forms 34 1.1 mrg part of the series `-lgfortran -lm'. 35 1.1 mrg 36 1.1 mrg #1 and #2 are not done if `-nostdlib' or any option that disables 37 1.1 mrg the linking phase is present, or if `-xfoo' is in effect. Note that 38 1.1 mrg a lack of source files or -l options disables linking. 39 1.1 mrg 40 1.1 mrg This program was originally made out of gcc/cp/g++spec.cc, but the 41 1.1 mrg way it builds the new argument list was rewritten so it is much 42 1.1 mrg easier to maintain, improve the way it decides to add or not add 43 1.1 mrg extra arguments, etc. And several improvements were made in the 44 1.1 mrg handling of arguments, primarily to make it more consistent with 45 1.1 mrg `gcc' itself. */ 46 1.1 mrg 47 1.1 mrg #include "config.h" 48 1.1 mrg #include "system.h" 49 1.1 mrg #include "coretypes.h" 50 1.1 mrg #include "opt-suggestions.h" 51 1.1 mrg #include "gcc.h" 52 1.1 mrg #include "opts.h" 53 1.1 mrg 54 1.1 mrg #include "tm.h" 55 1.1 mrg #include "intl.h" 56 1.1 mrg 57 1.1 mrg #ifndef MATH_LIBRARY 58 1.1 mrg #define MATH_LIBRARY "m" 59 1.1 mrg #endif 60 1.1 mrg 61 1.1 mrg #ifndef FORTRAN_LIBRARY 62 1.1 mrg #define FORTRAN_LIBRARY "gfortran" 63 1.1 mrg #endif 64 1.1 mrg 65 1.1 mrg /* Name of the spec file. */ 66 1.1 mrg #define SPEC_FILE "libgfortran.spec" 67 1.1 mrg 68 1.1 mrg /* The original argument list and related info is copied here. */ 69 1.1 mrg static unsigned int g77_xargc; 70 1.1 mrg static const struct cl_decoded_option *g77_x_decoded_options; 71 1.1 mrg static void append_arg (const struct cl_decoded_option *); 72 1.1 mrg 73 1.1 mrg /* The new argument list will be built here. */ 74 1.1 mrg static unsigned int g77_newargc; 75 1.1 mrg static struct cl_decoded_option *g77_new_decoded_options; 76 1.1 mrg 77 1.1 mrg /* This will be NULL if we encounter a situation where we should not 78 1.1 mrg link in the fortran libraries. */ 79 1.1 mrg static const char *library = NULL; 80 1.1 mrg 81 1.1 mrg 82 1.1 mrg /* Return whether strings S1 and S2 are both NULL or both the same 83 1.1 mrg string. */ 84 1.1 mrg 85 1.1 mrg static bool 86 1.1 mrg strings_same (const char *s1, const char *s2) 87 1.1 mrg { 88 1.1 mrg return s1 == s2 || (s1 != NULL && s2 != NULL && strcmp (s1, s2) == 0); 89 1.1 mrg } 90 1.1 mrg 91 1.1 mrg /* Return whether decoded option structures OPT1 and OPT2 are the 92 1.1 mrg same. */ 93 1.1 mrg 94 1.1 mrg static bool 95 1.1 mrg options_same (const struct cl_decoded_option *opt1, 96 1.1 mrg const struct cl_decoded_option *opt2) 97 1.1 mrg { 98 1.1 mrg return (opt1->opt_index == opt2->opt_index 99 1.1 mrg && strings_same (opt1->arg, opt2->arg) 100 1.1 mrg && strings_same (opt1->orig_option_with_args_text, 101 1.1 mrg opt2->orig_option_with_args_text) 102 1.1 mrg && strings_same (opt1->canonical_option[0], 103 1.1 mrg opt2->canonical_option[0]) 104 1.1 mrg && strings_same (opt1->canonical_option[1], 105 1.1 mrg opt2->canonical_option[1]) 106 1.1 mrg && strings_same (opt1->canonical_option[2], 107 1.1 mrg opt2->canonical_option[2]) 108 1.1 mrg && strings_same (opt1->canonical_option[3], 109 1.1 mrg opt2->canonical_option[3]) 110 1.1 mrg && (opt1->canonical_option_num_elements 111 1.1 mrg == opt2->canonical_option_num_elements) 112 1.1 mrg && opt1->value == opt2->value 113 1.1 mrg && opt1->errors == opt2->errors); 114 1.1 mrg } 115 1.1 mrg 116 1.1 mrg /* Append another argument to the list being built. As long as it is 117 1.1 mrg identical to the corresponding arg in the original list, just increment 118 1.1 mrg the new arg count. Otherwise allocate a new list, etc. */ 119 1.1 mrg 120 1.1 mrg static void 121 1.1 mrg append_arg (const struct cl_decoded_option *arg) 122 1.1 mrg { 123 1.1 mrg static unsigned int newargsize; 124 1.1 mrg 125 1.1 mrg if (g77_new_decoded_options == g77_x_decoded_options 126 1.1 mrg && g77_newargc < g77_xargc 127 1.1 mrg && options_same (arg, &g77_x_decoded_options[g77_newargc])) 128 1.1 mrg { 129 1.1 mrg ++g77_newargc; 130 1.1 mrg return; /* Nothing new here. */ 131 1.1 mrg } 132 1.1 mrg 133 1.1 mrg if (g77_new_decoded_options == g77_x_decoded_options) 134 1.1 mrg { /* Make new arglist. */ 135 1.1 mrg unsigned int i; 136 1.1 mrg 137 1.1 mrg newargsize = (g77_xargc << 2) + 20; /* This should handle all. */ 138 1.1 mrg g77_new_decoded_options = XNEWVEC (struct cl_decoded_option, newargsize); 139 1.1 mrg 140 1.1 mrg /* Copy what has been done so far. */ 141 1.1 mrg for (i = 0; i < g77_newargc; ++i) 142 1.1 mrg g77_new_decoded_options[i] = g77_x_decoded_options[i]; 143 1.1 mrg } 144 1.1 mrg 145 1.1 mrg if (g77_newargc == newargsize) 146 1.1 mrg fatal_error (input_location, "overflowed output argument list for %qs", 147 1.1 mrg arg->orig_option_with_args_text); 148 1.1 mrg 149 1.1 mrg g77_new_decoded_options[g77_newargc++] = *arg; 150 1.1 mrg } 151 1.1 mrg 152 1.1 mrg /* Append an option described by OPT_INDEX, ARG and VALUE to the list 153 1.1 mrg being built. */ 154 1.1 mrg static void 155 1.1 mrg append_option (size_t opt_index, const char *arg, int value) 156 1.1 mrg { 157 1.1 mrg struct cl_decoded_option decoded; 158 1.1 mrg 159 1.1 mrg generate_option (opt_index, arg, value, CL_DRIVER, &decoded); 160 1.1 mrg append_arg (&decoded); 161 1.1 mrg } 162 1.1 mrg 163 1.1 mrg /* Append a libgfortran argument to the list being built. If 164 1.1 mrg FORCE_STATIC, ensure the library is linked statically. */ 165 1.1 mrg 166 1.1 mrg static void 167 1.1 mrg add_arg_libgfortran (bool force_static ATTRIBUTE_UNUSED) 168 1.1 mrg { 169 1.1 mrg #ifdef HAVE_LD_STATIC_DYNAMIC 170 1.1 mrg if (force_static) 171 1.1 mrg append_option (OPT_Wl_, LD_STATIC_OPTION, 1); 172 1.1 mrg #endif 173 1.1 mrg append_option (OPT_l, FORTRAN_LIBRARY, 1); 174 1.1 mrg #ifdef HAVE_LD_STATIC_DYNAMIC 175 1.1 mrg if (force_static) 176 1.1 mrg append_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1); 177 1.1 mrg #endif 178 1.1 mrg } 179 1.1 mrg 180 1.1 mrg void 181 1.1 mrg lang_specific_driver (struct cl_decoded_option **in_decoded_options, 182 1.1 mrg unsigned int *in_decoded_options_count, 183 1.1 mrg int *in_added_libraries ATTRIBUTE_UNUSED) 184 1.1 mrg { 185 1.1 mrg unsigned int argc = *in_decoded_options_count; 186 1.1 mrg struct cl_decoded_option *decoded_options = *in_decoded_options; 187 1.1 mrg unsigned int i; 188 1.1 mrg int verbose = 0; 189 1.1 mrg 190 1.1 mrg /* 0 => -xnone in effect. 191 1.1 mrg 1 => -xfoo in effect. */ 192 1.1 mrg int saw_speclang = 0; 193 1.1 mrg 194 1.1 mrg /* 0 => initial/reset state 195 1.1 mrg 1 => last arg was -l<library> 196 1.1 mrg 2 => last two args were -l<library> -lm. */ 197 1.1 mrg int saw_library = 0; 198 1.1 mrg 199 1.1 mrg /* By default, we throw on the math library if we have one. */ 200 1.1 mrg int need_math = (MATH_LIBRARY[0] != '\0'); 201 1.1 mrg 202 1.1 mrg /* Whether we should link a static libgfortran. */ 203 1.1 mrg int static_lib = 0; 204 1.1 mrg 205 1.1 mrg /* Whether we need to link statically. */ 206 1.1 mrg int static_linking = 0; 207 1.1 mrg 208 1.1 mrg /* The number of input and output files in the incoming arg list. */ 209 1.1 mrg int n_infiles = 0; 210 1.1 mrg int n_outfiles = 0; 211 1.1 mrg 212 1.1 mrg library = FORTRAN_LIBRARY; 213 1.1 mrg 214 1.1 mrg #if 0 215 1.1 mrg fprintf (stderr, "Incoming:"); 216 1.1 mrg for (i = 0; i < argc; i++) 217 1.1 mrg fprintf (stderr, " %s", decoded_options[i].orig_option_with_args_text); 218 1.1 mrg fprintf (stderr, "\n"); 219 1.1 mrg #endif 220 1.1 mrg 221 1.1 mrg g77_xargc = argc; 222 1.1 mrg g77_x_decoded_options = decoded_options; 223 1.1 mrg g77_newargc = 0; 224 1.1 mrg g77_new_decoded_options = decoded_options; 225 1.1 mrg 226 1.1 mrg /* First pass through arglist. 227 1.1 mrg 228 1.1 mrg If -nostdlib or a "turn-off-linking" option is anywhere in the 229 1.1 mrg command line, don't do any library-option processing (except 230 1.1 mrg relating to -x). */ 231 1.1 mrg 232 1.1 mrg for (i = 1; i < argc; ++i) 233 1.1 mrg { 234 1.1 mrg if (decoded_options[i].errors & CL_ERR_MISSING_ARG) 235 1.1 mrg continue; 236 1.1 mrg 237 1.1 mrg switch (decoded_options[i].opt_index) 238 1.1 mrg { 239 1.1 mrg case OPT_SPECIAL_input_file: 240 1.1 mrg ++n_infiles; 241 1.1 mrg continue; 242 1.1 mrg 243 1.1 mrg case OPT_nostdlib: 244 1.1 mrg case OPT_nodefaultlibs: 245 1.1 mrg case OPT_c: 246 1.1 mrg case OPT_r: 247 1.1 mrg case OPT_S: 248 1.1 mrg case OPT_fsyntax_only: 249 1.1 mrg case OPT_E: 250 1.1 mrg /* These options disable linking entirely or linking of the 251 1.1 mrg standard libraries. */ 252 1.1 mrg library = 0; 253 1.1 mrg break; 254 1.1 mrg 255 1.1 mrg case OPT_static_libgfortran: 256 1.1 mrg #ifdef HAVE_LD_STATIC_DYNAMIC 257 1.1 mrg static_lib = 1; 258 1.1 mrg #endif 259 1.1 mrg break; 260 1.1 mrg 261 1.1 mrg case OPT_static: 262 1.1 mrg #ifdef HAVE_LD_STATIC_DYNAMIC 263 1.1 mrg static_linking = 1; 264 1.1 mrg #endif 265 1.1 mrg break; 266 1.1 mrg 267 1.1 mrg case OPT_l: 268 1.1 mrg ++n_infiles; 269 1.1 mrg break; 270 1.1 mrg 271 1.1 mrg case OPT_o: 272 1.1 mrg ++n_outfiles; 273 1.1 mrg break; 274 1.1 mrg 275 1.1 mrg case OPT_v: 276 1.1 mrg verbose = 1; 277 1.1 mrg break; 278 1.1 mrg 279 1.1 mrg case OPT__version: 280 1.1 mrg printf ("GNU Fortran %s%s\n", pkgversion_string, version_string); 281 1.1 mrg printf ("Copyright %s 2022 Free Software Foundation, Inc.\n", 282 1.1 mrg _("(C)")); 283 1.1 mrg fputs (_("This is free software; see the source for copying conditions. There is NO\n\ 284 1.1 mrg warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"), 285 1.1 mrg stdout); 286 1.1 mrg exit (0); 287 1.1 mrg break; 288 1.1 mrg 289 1.1 mrg case OPT__help: 290 1.1 mrg /* Let gcc.cc handle this, as it has a really 291 1.1 mrg cool facility for handling --help and --verbose --help. */ 292 1.1 mrg return; 293 1.1 mrg 294 1.1 mrg default: 295 1.1 mrg break; 296 1.1 mrg } 297 1.1 mrg } 298 1.1 mrg 299 1.1 mrg if ((n_outfiles != 0) && (n_infiles == 0)) 300 1.1 mrg fatal_error (input_location, 301 1.1 mrg "no input files; unwilling to write output files"); 302 1.1 mrg 303 1.1 mrg /* If there are no input files, no need for the library. */ 304 1.1 mrg if (n_infiles == 0) 305 1.1 mrg library = 0; 306 1.1 mrg 307 1.1 mrg /* Second pass through arglist, transforming arguments as appropriate. */ 308 1.1 mrg 309 1.1 mrg append_arg (&decoded_options[0]); /* Start with command name, of course. */ 310 1.1 mrg 311 1.1 mrg for (i = 1; i < argc; ++i) 312 1.1 mrg { 313 1.1 mrg if (decoded_options[i].errors & CL_ERR_MISSING_ARG) 314 1.1 mrg { 315 1.1 mrg append_arg (&decoded_options[i]); 316 1.1 mrg continue; 317 1.1 mrg } 318 1.1 mrg 319 1.1 mrg if (decoded_options[i].opt_index == OPT_SPECIAL_input_file 320 1.1 mrg && decoded_options[i].arg[0] == '\0') 321 1.1 mrg { 322 1.1 mrg /* Interesting. Just append as is. */ 323 1.1 mrg append_arg (&decoded_options[i]); 324 1.1 mrg continue; 325 1.1 mrg } 326 1.1 mrg 327 1.1 mrg if (decoded_options[i].opt_index != OPT_l 328 1.1 mrg && (decoded_options[i].opt_index != OPT_SPECIAL_input_file 329 1.1 mrg || strcmp (decoded_options[i].arg, "-") == 0)) 330 1.1 mrg { 331 1.1 mrg /* Not a filename or library. */ 332 1.1 mrg 333 1.1 mrg if (saw_library == 1 && need_math) /* -l<library>. */ 334 1.1 mrg append_option (OPT_l, MATH_LIBRARY, 1); 335 1.1 mrg 336 1.1 mrg saw_library = 0; 337 1.1 mrg 338 1.1 mrg if (decoded_options[i].opt_index == OPT_SPECIAL_input_file) 339 1.1 mrg { 340 1.1 mrg append_arg (&decoded_options[i]); /* "-" == Standard input. */ 341 1.1 mrg continue; 342 1.1 mrg } 343 1.1 mrg 344 1.1 mrg if (decoded_options[i].opt_index == OPT_x) 345 1.1 mrg { 346 1.1 mrg /* Track input language. */ 347 1.1 mrg const char *lang = decoded_options[i].arg; 348 1.1 mrg 349 1.1 mrg saw_speclang = (strcmp (lang, "none") != 0); 350 1.1 mrg } 351 1.1 mrg 352 1.1 mrg append_arg (&decoded_options[i]); 353 1.1 mrg 354 1.1 mrg continue; 355 1.1 mrg } 356 1.1 mrg 357 1.1 mrg /* A filename/library, not an option. */ 358 1.1 mrg 359 1.1 mrg if (saw_speclang) 360 1.1 mrg saw_library = 0; /* -xfoo currently active. */ 361 1.1 mrg else 362 1.1 mrg { /* -lfoo or filename. */ 363 1.1 mrg if (decoded_options[i].opt_index == OPT_l 364 1.1 mrg && strcmp (decoded_options[i].arg, MATH_LIBRARY) == 0) 365 1.1 mrg { 366 1.1 mrg if (saw_library == 1) 367 1.1 mrg saw_library = 2; /* -l<library> -lm. */ 368 1.1 mrg else 369 1.1 mrg add_arg_libgfortran (static_lib && !static_linking); 370 1.1 mrg } 371 1.1 mrg else if (decoded_options[i].opt_index == OPT_l 372 1.1 mrg && strcmp (decoded_options[i].arg, FORTRAN_LIBRARY) == 0) 373 1.1 mrg { 374 1.1 mrg saw_library = 1; /* -l<library>. */ 375 1.1 mrg add_arg_libgfortran (static_lib && !static_linking); 376 1.1 mrg continue; 377 1.1 mrg } 378 1.1 mrg else 379 1.1 mrg { /* Other library, or filename. */ 380 1.1 mrg if (saw_library == 1 && need_math) 381 1.1 mrg append_option (OPT_l, MATH_LIBRARY, 1); 382 1.1 mrg saw_library = 0; 383 1.1 mrg } 384 1.1 mrg } 385 1.1 mrg append_arg (&decoded_options[i]); 386 1.1 mrg } 387 1.1 mrg 388 1.1 mrg /* Append `-lgfortran -lm' as necessary. */ 389 1.1 mrg 390 1.1 mrg if (library) 391 1.1 mrg { /* Doing a link and no -nostdlib. */ 392 1.1 mrg if (saw_speclang) 393 1.1 mrg append_option (OPT_x, "none", 1); 394 1.1 mrg 395 1.1 mrg switch (saw_library) 396 1.1 mrg { 397 1.1 mrg case 0: 398 1.1 mrg add_arg_libgfortran (static_lib && !static_linking); 399 1.1 mrg /* Fall through. */ 400 1.1 mrg 401 1.1 mrg case 1: 402 1.1 mrg if (need_math) 403 1.1 mrg append_option (OPT_l, MATH_LIBRARY, 1); 404 1.1 mrg default: 405 1.1 mrg break; 406 1.1 mrg } 407 1.1 mrg } 408 1.1 mrg 409 1.1 mrg #ifdef ENABLE_SHARED_LIBGCC 410 1.1 mrg if (library) 411 1.1 mrg { 412 1.1 mrg unsigned int i; 413 1.1 mrg 414 1.1 mrg for (i = 1; i < g77_newargc; i++) 415 1.1 mrg if (g77_new_decoded_options[i].opt_index == OPT_static_libgcc 416 1.1 mrg || g77_new_decoded_options[i].opt_index == OPT_static) 417 1.1 mrg break; 418 1.1 mrg 419 1.1 mrg if (i == g77_newargc) 420 1.1 mrg append_option (OPT_shared_libgcc, NULL, 1); 421 1.1 mrg } 422 1.1 mrg 423 1.1 mrg #endif 424 1.1 mrg 425 1.1 mrg if (verbose && g77_new_decoded_options != g77_x_decoded_options) 426 1.1 mrg { 427 1.1 mrg fprintf (stderr, _("Driving:")); 428 1.1 mrg for (i = 0; i < g77_newargc; i++) 429 1.1 mrg fprintf (stderr, " %s", 430 1.1 mrg g77_new_decoded_options[i].orig_option_with_args_text); 431 1.1 mrg fprintf (stderr, "\n"); 432 1.1 mrg } 433 1.1 mrg 434 1.1 mrg *in_decoded_options_count = g77_newargc; 435 1.1 mrg *in_decoded_options = g77_new_decoded_options; 436 1.1 mrg } 437 1.1 mrg 438 1.1 mrg 439 1.1 mrg /* Called before linking. Returns 0 on success and -1 on failure. */ 440 1.1 mrg int 441 1.1 mrg lang_specific_pre_link (void) 442 1.1 mrg { 443 1.1 mrg if (library) 444 1.1 mrg do_spec ("%:include(libgfortran.spec)"); 445 1.1 mrg 446 1.1 mrg return 0; 447 1.1 mrg } 448 1.1 mrg 449 1.1 mrg /* Number of extra output files that lang_specific_pre_link may generate. */ 450 1.1 mrg int lang_specific_extra_outfiles = 0; /* Not used for F77. */ 451