Line data Source code
1 : /* gm2spec.cc specific flags and argument handling within GNU Modula-2.
2 :
3 : Copyright (C) 2007-2026 Free Software Foundation, Inc.
4 : Contributed by Gaius Mulley <gaius@glam.ac.uk>.
5 :
6 : This file is part of GNU Modula-2.
7 :
8 : GNU Modula-2 is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3, or (at your option)
11 : any later version.
12 :
13 : GNU Modula-2 is distributed in the hope that it will be useful, but
14 : WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with GNU Modula-2; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 : #include "config.h"
23 : #define INCLUDE_STRING
24 : #define INCLUDE_VECTOR
25 : #include "system.h"
26 : #include "coretypes.h"
27 : #include "tm.h"
28 : #include "xregex.h"
29 : #include "obstack.h"
30 : #include "intl.h"
31 : #include "prefix.h"
32 : #include "opt-suggestions.h"
33 : #include "gcc.h"
34 : #include "opts.h"
35 : #include "vec.h"
36 :
37 : #include "m2/gm2config.h"
38 :
39 : #ifdef HAVE_DIRENT_H
40 : #include <dirent.h>
41 : #else
42 : #ifdef HAVE_SYS_NDIR_H
43 : #include <sys/ndir.h>
44 : #endif
45 : #ifdef HAVE_SYS_DIR_H
46 : #include <sys/dir.h>
47 : #endif
48 : #ifdef HAVE_NDIR_H
49 : #include <ndir.h>
50 : #endif
51 : #endif
52 :
53 : /* This bit is set if the arguments is a M2 source file. */
54 : #define M2SOURCE (1<<1)
55 : /* This bit is set if we saw a `-xfoo' language specification. */
56 : #define LANGSPEC (1<<2)
57 : /* This bit is set if they did `-lm' or `-lmath'. */
58 : #define MATHLIB (1<<3)
59 : /* This bit is set if they did `-lc'. */
60 : #define WITHLIBC (1<<4)
61 : /* Skip this option. */
62 : #define SKIPOPT (1<<5)
63 :
64 : #ifndef MATH_LIBRARY
65 : #define MATH_LIBRARY "m"
66 : #endif
67 : #ifndef MATH_LIBRARY_PROFILE
68 : #define MATH_LIBRARY_PROFILE MATH_LIBRARY
69 : #endif
70 :
71 : #ifndef LIBSTDCXX
72 : #define LIBSTDCXX "stdc++"
73 : #endif
74 : #ifndef LIBSTDCXX_PROFILE
75 : #define LIBSTDCXX_PROFILE LIBSTDCXX
76 : #endif
77 : #ifndef LIBSTDCXX_STATIC
78 : #define LIBSTDCXX_STATIC NULL
79 : #endif
80 :
81 : #ifndef LIBCXX
82 : #define LIBCXX "c++"
83 : #endif
84 : #ifndef LIBCXX_PROFILE
85 : #define LIBCXX_PROFILE LIBCXX
86 : #endif
87 : #ifndef LIBCXX_STATIC
88 : #define LIBCXX_STATIC NULL
89 : #endif
90 :
91 : #ifndef LIBCXXABI
92 : #define LIBCXXABI "c++abi"
93 : #endif
94 : #ifndef LIBCXXABI_PROFILE
95 : #define LIBCXXABI_PROFILE LIBCXXABI
96 : #endif
97 : #ifndef LIBCXXABI_STATIC
98 : #define LIBCXXABI_STATIC NULL
99 : #endif
100 :
101 : /* The values used here must match those of the stdlib_kind enumeration
102 : in c.opt. */
103 : enum stdcxxlib_kind
104 : {
105 : USE_LIBSTDCXX = 1,
106 : USE_LIBCXX = 2
107 : };
108 :
109 : #define DEFAULT_DIALECT "pim"
110 :
111 : #undef DEBUG_ARG
112 :
113 : typedef enum { iso, pim, min, logitech, pimcoroutine, maxlib } libs;
114 :
115 : /* These are the library names which are installed as part of gm2 and reflect
116 : -flibs=name. The -flibs= option provides the user with a short cut to add
117 : libraries without having to know the include and link path. */
118 :
119 : static const char *library_name[maxlib]
120 : = { "m2iso", "m2pim", "m2min", "m2log", "m2cor" };
121 :
122 : /* They match the installed archive name for example libm2iso.a,
123 : libm2pim.a, libm2min.a, libm2log.a and libm2cor.a. They also match a
124 : subdirectory name where the definition modules are kept. The driver
125 : checks the argument to -flibs= for an entry in library_name or
126 : alternatively the existance of the subdirectory (to allow for third
127 : party libraries to coexist). */
128 :
129 : static const char *library_abbrev[maxlib]
130 : = { "iso", "pim", "min", "log", "cor" };
131 :
132 : /* Users may specifiy -flibs=pim,iso etc which are mapped onto
133 : -flibs=m2pim,m2iso respectively. This provides a match between
134 : the dialect of Modula-2 and the library set. */
135 :
136 : static bool seen_scaffold_static = false;
137 : static bool seen_scaffold_dynamic = false;
138 : static bool seen_scaffold_main = false;
139 : static bool scaffold_static = false;
140 : static bool scaffold_dynamic = true; // Default uses -fscaffold-dynamic.
141 : static bool scaffold_main = false;
142 : static bool seen_gen_module_list = false;
143 : static bool seen_uselist = false;
144 : static bool uselist = false;
145 : static bool gen_module_list = true; // Default uses -fgen-module-list=-.
146 : static const char *gen_module_filename = "-";
147 : /* The original argument list and related info is copied here. */
148 : static unsigned int gm2_xargc;
149 : static const struct cl_decoded_option *gm2_x_decoded_options;
150 : static void append_arg (const struct cl_decoded_option *);
151 :
152 : /* The new argument list will be built here. */
153 : static unsigned int gm2_newargc;
154 : static struct cl_decoded_option *gm2_new_decoded_options;
155 : static const char *libraries = NULL; /* Abbreviated libraries. */
156 : static const char *m2_path_name = "";
157 :
158 468016 : typedef struct named_path_s {
159 : std::vector<const char*>path;
160 : const char *name;
161 : bool lib_root;
162 : } named_path;
163 :
164 : static std::vector<named_path>Ipaths;
165 :
166 : /* push_back_Ipath pushes a named path to the Ipaths global variable. */
167 :
168 : static void
169 154731 : push_back_Ipath (const char *arg)
170 : {
171 154731 : if (Ipaths.empty ())
172 : {
173 26347 : named_path np;
174 26347 : np.path.push_back (arg);
175 26347 : np.name = m2_path_name;
176 26347 : np.lib_root = false;
177 26347 : Ipaths.push_back (np);
178 26347 : }
179 : else
180 : {
181 128384 : if (strcmp (Ipaths.back ().name,
182 : m2_path_name) == 0)
183 42748 : Ipaths.back ().path.push_back (arg);
184 : else
185 : {
186 85636 : named_path np;
187 85636 : np.path.push_back (arg);
188 85636 : np.name = m2_path_name;
189 85636 : np.lib_root = false;
190 85636 : Ipaths.push_back (np);
191 85636 : }
192 : }
193 154731 : }
194 :
195 : /* push_back_lib_root pushes a lib_root onto the Ipaths vector.
196 : The ordering of the -fm2_add_lib_root=, -I and named paths
197 : must be preserved. */
198 :
199 : static void
200 45 : push_back_lib_root (const char *arg)
201 : {
202 45 : named_path np;
203 45 : np.name = arg;
204 45 : np.lib_root = true;
205 45 : Ipaths.push_back (np);
206 45 : }
207 :
208 : /* Return whether strings S1 and S2 are both NULL or both the same
209 : string. */
210 :
211 : static bool
212 703128 : strings_same (const char *s1, const char *s2)
213 : {
214 703128 : return s1 == s2 || (s1 != NULL && s2 != NULL && strcmp (s1, s2) == 0);
215 : }
216 :
217 : bool
218 138875 : options_same (const struct cl_decoded_option *opt1,
219 : const struct cl_decoded_option *opt2)
220 : {
221 138875 : return (opt1->opt_index == opt2->opt_index
222 117188 : && strings_same (opt1->arg, opt2->arg)
223 117188 : && strings_same (opt1->orig_option_with_args_text,
224 117188 : opt2->orig_option_with_args_text)
225 117188 : && strings_same (opt1->canonical_option[0],
226 117188 : opt2->canonical_option[0])
227 117188 : && strings_same (opt1->canonical_option[1],
228 117188 : opt2->canonical_option[1])
229 117188 : && strings_same (opt1->canonical_option[2],
230 117188 : opt2->canonical_option[2])
231 117188 : && strings_same (opt1->canonical_option[3],
232 117188 : opt2->canonical_option[3])
233 117188 : && (opt1->canonical_option_num_elements
234 117188 : == opt2->canonical_option_num_elements)
235 117188 : && opt1->value == opt2->value
236 256063 : && opt1->errors == opt2->errors);
237 : }
238 :
239 : /* Append another argument to the list being built. */
240 :
241 : static void
242 844025 : append_arg (const struct cl_decoded_option *arg)
243 : {
244 844025 : static unsigned int newargsize;
245 :
246 844025 : if (gm2_new_decoded_options == gm2_x_decoded_options
247 143535 : && gm2_newargc < gm2_xargc
248 982900 : && options_same (arg, &gm2_x_decoded_options[gm2_newargc]))
249 : {
250 117188 : ++gm2_newargc;
251 117188 : return; /* Nothing new here. */
252 : }
253 :
254 726837 : if (gm2_new_decoded_options == gm2_x_decoded_options)
255 : { /* Make new arglist. */
256 26347 : unsigned int i;
257 :
258 26347 : newargsize = (gm2_xargc << 2) + 20; /* This should handle all. */
259 26347 : gm2_new_decoded_options = XNEWVEC (struct cl_decoded_option, newargsize);
260 :
261 : /* Copy what has been done so far. */
262 143535 : for (i = 0; i < gm2_newargc; ++i)
263 117188 : gm2_new_decoded_options[i] = gm2_x_decoded_options[i];
264 : }
265 :
266 726837 : if (gm2_newargc == newargsize)
267 0 : fatal_error (input_location, "overflowed output argument list for %qs",
268 0 : arg->orig_option_with_args_text);
269 :
270 726837 : gm2_new_decoded_options[gm2_newargc++] = *arg;
271 : }
272 :
273 : /* Append an option described by OPT_INDEX, ARG and VALUE to the list
274 : being built. */
275 :
276 : static void
277 450416 : append_option (size_t opt_index, const char *arg, int value)
278 : {
279 450416 : struct cl_decoded_option decoded;
280 :
281 450416 : generate_option (opt_index, arg, value, CL_DRIVER, &decoded);
282 450416 : append_arg (&decoded);
283 450416 : }
284 :
285 : /* safe_strdup safely duplicates a string. */
286 :
287 : static char *
288 285323 : safe_strdup (const char *s)
289 : {
290 0 : if (s != NULL)
291 154776 : return xstrdup (s);
292 : return NULL;
293 : }
294 :
295 : /* add_default_libs adds the -l option which is derived from the
296 : libraries. */
297 :
298 : static int
299 4641 : add_default_libs (const char *libraries)
300 : {
301 4641 : const char *l = libraries;
302 4641 : const char *e;
303 4641 : char *libname;
304 4641 : unsigned int libcount = 0;
305 :
306 18564 : while ((l != NULL) && (l[0] != (char)0))
307 : {
308 18564 : e = index (l, ',');
309 18564 : if (e == NULL)
310 : {
311 4641 : libname = xstrdup (l);
312 4641 : l = NULL;
313 4641 : append_option (OPT_l, safe_strdup (libname), 1);
314 4641 : libcount++;
315 4641 : free (libname);
316 : }
317 : else
318 : {
319 13923 : libname = xstrndup (l, e - l);
320 13923 : l = e + 1;
321 13923 : append_option (OPT_l, safe_strdup (libname), 1);
322 13923 : libcount++;
323 13923 : free (libname);
324 : }
325 : }
326 4641 : return libcount;
327 : }
328 :
329 : /* add_word returns a new string which has the contents of lib
330 : appended to list. If list is NULL then lib is duplicated and
331 : returned otherwise the list is appended by "," and the contents of
332 : lib. */
333 :
334 : static const char *
335 21224 : add_word (const char *list, const char *lib)
336 : {
337 21224 : char *copy;
338 21224 : if (list == NULL)
339 5306 : return xstrdup (lib);
340 15918 : copy = (char *) xmalloc (strlen (list) + strlen (lib) + 1 + 1);
341 15918 : strcpy (copy, list);
342 15918 : strcat (copy, ",");
343 15918 : strcat (copy, lib);
344 15918 : return copy;
345 : }
346 :
347 : /* convert_abbreviation checks abbreviation against known library
348 : abbreviations. If an abbreviation is found it converts the element
349 : to the full library name, otherwise the user supplied name is added
350 : to the full_libraries list. A new string is returned. */
351 :
352 : static const char *
353 21224 : convert_abbreviation (const char *full_libraries, const char *abbreviation)
354 : {
355 127344 : for (int i = 0; i < maxlib; i++)
356 106120 : if (strcmp (abbreviation, library_abbrev[i]) == 0)
357 0 : return add_word (full_libraries, library_name[i]);
358 : /* Perhaps the user typed in the whole lib name rather than an abbrev. */
359 63672 : for (int i = 0; i < maxlib; i++)
360 63672 : if (strcmp (abbreviation, library_name[i]) == 0)
361 21224 : return add_word (full_libraries, abbreviation);
362 : /* Not found, probably a user typo. */
363 0 : error ("%qs is not a valid Modula-2 system library name or abbreviation",
364 : abbreviation);
365 0 : return full_libraries;
366 : }
367 :
368 : /* convert_abbreviations checks each element in the library list to
369 : see if an a known library abbreviation was used. If found it
370 : converts the element to the full library name, otherwise the
371 : element is copied into the list. A new string is returned. */
372 :
373 : static const char *
374 5306 : convert_abbreviations (const char *libraries)
375 : {
376 5306 : const char *start = libraries;
377 5306 : const char *end;
378 5306 : const char *full_libraries = NULL;
379 :
380 21224 : do
381 : {
382 21224 : end = index (start, ',');
383 21224 : if (end == NULL)
384 : {
385 5306 : full_libraries = convert_abbreviation (full_libraries, start);
386 5306 : start = NULL;
387 : }
388 : else
389 : {
390 15918 : full_libraries = convert_abbreviation (full_libraries,
391 15918 : xstrndup (start, end - start));
392 15918 : start = end + 1;
393 : }
394 : }
395 21224 : while ((start != NULL) && (start[0] != (char)0));
396 5306 : return full_libraries;
397 : }
398 :
399 : /* add_m2_I_path appends -fm2-pathname, -fm2-pathnameI and -fm2-add-lib-root
400 : options to the command line which are contructed in the saved Ipaths.
401 : The order of these options must be maintained. */
402 :
403 : static void
404 26347 : add_m2_I_path (void)
405 : {
406 138375 : for (auto np : Ipaths)
407 : {
408 112028 : if (np.lib_root)
409 90 : append_option (OPT_fm2_pathname_rootI_, safe_strdup (np.name), 1);
410 111983 : else if (strcmp (np.name, "") == 0)
411 4682 : append_option (OPT_fm2_pathname_, safe_strdup ("-"), 1);
412 : else
413 107301 : append_option (OPT_fm2_pathname_, safe_strdup (np.name), 1);
414 266759 : for (auto *s : np.path)
415 309462 : append_option (OPT_fm2_pathnameI, safe_strdup (s), 1);
416 112028 : }
417 26347 : Ipaths.clear();
418 26347 : }
419 :
420 :
421 : void
422 26347 : lang_specific_driver (struct cl_decoded_option **in_decoded_options,
423 : unsigned int *in_decoded_options_count,
424 : int *in_added_libraries)
425 : {
426 26347 : unsigned int argc = *in_decoded_options_count;
427 26347 : struct cl_decoded_option *decoded_options = *in_decoded_options;
428 26347 : unsigned int i;
429 :
430 : /* True if we saw a `-xfoo' language specification on the command
431 : line. This function will add a -xmodula-2 if the user has not
432 : already placed one onto the command line. */
433 26347 : bool seen_x_flag = false;
434 26347 : const char *language = NULL;
435 :
436 : /* If nonzero, the user gave us the `-p' or `-pg' flag. */
437 26347 : int saw_profile_flag = 0;
438 :
439 : /* What action to take for the c++ runtime library:
440 : -1 means we should not link it in.
441 : 0 means we should link it if it is needed.
442 : 1 means it is needed and should be linked in.
443 : 2 means it is needed but should be linked statically. */
444 26347 : int library = 0;
445 :
446 : /* Which c++ runtime library to link. */
447 26347 : stdcxxlib_kind which_library = USE_LIBSTDCXX;
448 :
449 26347 : const char *dialect = DEFAULT_DIALECT;
450 :
451 : /* An array used to flag each argument that needs a bit set for
452 : LANGSPEC, MATHLIB, or WITHLIBC. */
453 26347 : int *args;
454 :
455 : /* Have we seen -fmod=? */
456 26347 : char *module_extension = NULL;
457 :
458 : /* Should the driver perform a link? */
459 26347 : bool linking = true;
460 :
461 : /* Should the driver link the shared gm2 libs? */
462 26347 : bool shared_libgm2 = true;
463 :
464 : /* "-lm" or "-lmath" if it appears on the command line. */
465 26347 : const struct cl_decoded_option *saw_math = NULL;
466 :
467 : /* "-lc" if it appears on the command line. */
468 26347 : const struct cl_decoded_option *saw_libc = NULL;
469 :
470 : /* By default, we throw on the math library if we have one. */
471 26347 : int need_math = (MATH_LIBRARY[0] != '\0');
472 :
473 : /* 1 if we should add -lpthread to the command-line.
474 : FIXME: the default should be a configuration choice. */
475 26347 : int need_pthread = 1;
476 :
477 : /* True if we saw -static. */
478 26347 : int static_link = 0;
479 :
480 : /* True if we should add -shared-libgcc to the command-line. */
481 26347 : int shared_libgcc = 1;
482 :
483 : /* Have we seen the -v flag? */
484 26347 : bool verbose = false;
485 :
486 : /* Have we seen the -fm2-pathname flag? */
487 26347 : bool seen_pathname = false;
488 :
489 : /* The number of libraries added in. */
490 26347 : int added_libraries;
491 :
492 : /* True if we should add -fplugin=m2rte to the command-line. */
493 26347 : bool need_plugin = false;
494 :
495 : /* True if we should set up include paths and library paths. */
496 26347 : bool allow_libraries = true;
497 :
498 : #if defined(DEBUG_ARG)
499 : printf ("argc = %d\n", argc);
500 : fprintf (stderr, "Incoming:");
501 : for (i = 0; i < argc; i++)
502 : fprintf (stderr, " %s", decoded_options[i].orig_option_with_args_text);
503 : fprintf (stderr, "\n");
504 : #endif
505 :
506 : // add_spec_function ("m2I", add_m2_I_path);
507 26347 : gm2_xargc = argc;
508 26347 : gm2_x_decoded_options = decoded_options;
509 26347 : gm2_newargc = 0;
510 26347 : gm2_new_decoded_options = decoded_options;
511 26347 : added_libraries = *in_added_libraries;
512 26347 : args = XCNEWVEC (int, argc);
513 :
514 : /* First pass through arglist.
515 :
516 : If -nostdlib or a "turn-off-linking" option is anywhere in the
517 : command line, don't do any library-option processing (except
518 : relating to -x). */
519 :
520 699503 : for (i = 1; i < argc; i++)
521 : {
522 673156 : const char *arg = decoded_options[i].arg;
523 673156 : args[i] = 0;
524 : #if defined(DEBUG_ARG)
525 : printf ("1st pass: %s\n",
526 : decoded_options[i].orig_option_with_args_text);
527 : #endif
528 673156 : switch (decoded_options[i].opt_index)
529 : {
530 : case OPT_fiso:
531 673156 : dialect = "iso";
532 : break;
533 242 : case OPT_fpim2:
534 242 : dialect = "pim2";
535 242 : break;
536 51 : case OPT_fpim3:
537 51 : dialect = "pim3";
538 51 : break;
539 311 : case OPT_fpim4:
540 311 : dialect = "pim4";
541 311 : break;
542 15273 : case OPT_fpim:
543 15273 : dialect = "pim";
544 15273 : break;
545 23718 : case OPT_flibs_:
546 23718 : libraries = xstrdup (arg);
547 23718 : allow_libraries = decoded_options[i].value;
548 23718 : args[i] |= SKIPOPT; /* We will add the option if it is needed. */
549 23718 : break;
550 6 : case OPT_fmod_:
551 6 : module_extension = xstrdup (arg);
552 : #if defined(DEBUG_ARG)
553 : printf ("seen -fmod=%s\n", module_extension);
554 : #endif
555 6 : break;
556 0 : case OPT_fpthread:
557 0 : need_pthread = decoded_options[i].value;
558 0 : args[i] |= SKIPOPT; /* We will add the option if it is needed. */
559 0 : break;
560 666 : case OPT_fm2_plugin:
561 666 : need_plugin = decoded_options[i].value;
562 : #ifndef ENABLE_PLUGIN
563 : if (need_plugin)
564 : error ("plugin support is disabled; configure with "
565 : "%<--enable-plugin%>");
566 : #endif
567 666 : args[i] |= SKIPOPT; /* We will add the option if it is needed. */
568 666 : break;
569 83 : case OPT_fscaffold_dynamic:
570 83 : seen_scaffold_dynamic = true;
571 83 : scaffold_dynamic = decoded_options[i].value;
572 83 : args[i] |= SKIPOPT; /* We will add the option if it is needed. */
573 83 : break;
574 22 : case OPT_fscaffold_static:
575 22 : seen_scaffold_static = true;
576 22 : scaffold_static = decoded_options[i].value;
577 22 : args[i] |= SKIPOPT; /* We will add the option if it is needed. */
578 22 : break;
579 126 : case OPT_fscaffold_main:
580 126 : seen_scaffold_main = true;
581 126 : scaffold_main = decoded_options[i].value;
582 126 : args[i] |= SKIPOPT; /* We will add the option if it is needed. */
583 126 : break;
584 0 : case OPT_fgen_module_list_:
585 0 : seen_gen_module_list = true;
586 0 : gen_module_list = decoded_options[i].value;
587 0 : if (gen_module_list)
588 0 : gen_module_filename = decoded_options[i].arg;
589 : break;
590 0 : case OPT_fuse_list_:
591 0 : seen_uselist = true;
592 0 : uselist = decoded_options[i].value;
593 0 : break;
594 131185 : case OPT_fm2_pathname_:
595 131185 : seen_pathname = true;
596 131185 : args[i] |= SKIPOPT; /* We will add the option if it is needed. */
597 131185 : m2_path_name = decoded_options[i].arg;
598 131185 : break;
599 45 : case OPT_fm2_pathname_root_:
600 45 : args[i] |= SKIPOPT; /* We will add the option if it is needed. */
601 45 : push_back_lib_root (decoded_options[i].arg);
602 45 : break;
603 0 : case OPT__help:
604 0 : case OPT__help_:
605 : /* Let gcc.cc handle this, as it has a really
606 : cool facility for handling --help and --verbose --help. */
607 0 : *in_added_libraries = 0;
608 0 : return;
609 150049 : case OPT_I:
610 150049 : args[i] |= SKIPOPT; /* We will add the option if it is needed. */
611 150049 : push_back_Ipath (decoded_options[i].arg);
612 150049 : break;
613 0 : case OPT_nostdlib:
614 0 : case OPT_nostdlib__:
615 0 : case OPT_nodefaultlibs:
616 0 : library = -1;
617 0 : break;
618 :
619 2678 : case OPT_l:
620 2678 : if (strcmp (arg, MATH_LIBRARY) == 0)
621 : {
622 2678 : args[i] |= MATHLIB;
623 2678 : need_math = 0;
624 : }
625 0 : else if (strcmp (arg, "c") == 0)
626 0 : args[i] |= WITHLIBC;
627 : else
628 : /* Unrecognized libraries (e.g. -lfoo) may require libstdc++. */
629 0 : library = (library == 0) ? 1 : library;
630 : break;
631 :
632 0 : case OPT_pg:
633 0 : case OPT_p:
634 0 : saw_profile_flag++;
635 0 : break;
636 :
637 0 : case OPT_x:
638 0 : seen_x_flag = true;
639 0 : language = arg;
640 0 : break;
641 :
642 0 : case OPT_v:
643 0 : verbose = true;
644 0 : break;
645 :
646 0 : case OPT_Xlinker:
647 0 : case OPT_Wl_:
648 : /* Arguments that go directly to the linker might be .o files,
649 : or something, and so might cause libstdc++ to be needed. */
650 0 : if (library == 0)
651 673156 : library = 1;
652 : break;
653 :
654 14470 : case OPT_c:
655 14470 : case OPT_r:
656 14470 : case OPT_S:
657 14470 : case OPT_E:
658 14470 : case OPT_M:
659 14470 : case OPT_MM:
660 14470 : case OPT_fsyntax_only:
661 : /* Don't specify libraries if we won't link, since that would
662 : cause a warning. */
663 14470 : linking = false;
664 14470 : library = -1;
665 14470 : break;
666 :
667 : /* PCH makes no sense here, we do not catch -output-pch on purpose,
668 : that should flag an error. */
669 0 : case OPT_fpch_deps:
670 0 : case OPT_fpch_preprocess:
671 0 : case OPT_Winvalid_pch:
672 0 : args[i] |= SKIPOPT;
673 0 : break;
674 :
675 0 : case OPT_static:
676 0 : static_link = 1;
677 0 : break;
678 :
679 0 : case OPT_static_libgcc:
680 0 : shared_libgcc = 0;
681 0 : break;
682 :
683 0 : case OPT_static_libstdc__:
684 0 : library = library >= 0 ? 2 : library;
685 : #ifdef HAVE_LD_STATIC_DYNAMIC
686 : /* Remove -static-libstdc++ from the command only if target supports
687 : LD_STATIC_DYNAMIC. When not supported, it is left in so that a
688 : back-end target can use outfile substitution. */
689 0 : args[i] |= SKIPOPT;
690 : #endif
691 0 : break;
692 :
693 0 : case OPT_static_libgm2:
694 0 : shared_libgm2 = false;
695 : #ifdef HAVE_LD_STATIC_DYNAMIC
696 : /* Remove -static-libgm2 from the command only if target supports
697 : LD_STATIC_DYNAMIC. When not supported, it is left in so that a
698 : back-end target can use outfile substitution. */
699 0 : args[i] |= SKIPOPT;
700 : #endif
701 0 : break;
702 :
703 0 : case OPT_stdlib_:
704 0 : which_library = (stdcxxlib_kind) decoded_options[i].value;
705 0 : break;
706 :
707 31102 : case OPT_SPECIAL_input_file:
708 31102 : {
709 31102 : const char *source_file = decoded_options[i].orig_option_with_args_text;
710 : #if defined(DEBUG_ARG)
711 : printf ("seen OPT_SPECIAL_input_file: %s\n", source_file);
712 : #endif
713 31102 : if (source_file != NULL)
714 : {
715 : /* Record that this is a Modula-2 source file. */
716 31102 : const char *suffix = strrchr (source_file, '.');
717 : #if defined(DEBUG_ARG)
718 : printf ("ext = %s\n", suffix);
719 : #endif
720 31102 : if ((suffix != NULL)
721 31099 : && ((strcmp (suffix, ".mod") == 0)
722 13979 : || ((module_extension != NULL)
723 6 : && (strcmp (suffix, module_extension) == 0))))
724 : {
725 : #if defined(DEBUG_ARG)
726 : printf ("modula-2 source file detected: %s\n", source_file);
727 : #endif
728 17126 : args[i] |= M2SOURCE;
729 : }
730 : }
731 : }
732 : break;
733 :
734 : default:
735 : break;
736 : }
737 : }
738 26347 : if (language != NULL && (strcmp (language, "modula-2") != 0))
739 : return;
740 :
741 26347 : if (! seen_pathname)
742 : /* Not seen -fm2-pathname therefore make current working directory
743 : the first place to look for modules. */
744 4682 : push_back_Ipath (".");
745 :
746 : /* Override the default when the user specifies it. */
747 26347 : if (seen_scaffold_static && scaffold_static && !seen_scaffold_dynamic)
748 0 : scaffold_dynamic = false;
749 :
750 : /* If both options have been seen and both are true, that means the user
751 : tried to set both. */
752 26347 : if (seen_scaffold_dynamic && scaffold_dynamic
753 0 : && seen_scaffold_static && scaffold_static)
754 0 : error ("%qs and %qs cannot both be enabled",
755 : "-fscaffold-dynamic", "-fscaffold-static");
756 :
757 26347 : if (uselist && gen_module_list)
758 : {
759 0 : if (! seen_gen_module_list)
760 0 : gen_module_list = false;
761 0 : if (uselist && gen_module_list)
762 0 : error ("%qs and %qs cannot both be enabled",
763 : "-fgen-module-list=", "-fuse-list=");
764 : }
765 :
766 : /* There's no point adding -shared-libgcc if we don't have a shared
767 : libgcc. */
768 : #ifndef ENABLE_SHARED_LIBGCC
769 : shared_libgcc = 0;
770 : #endif
771 :
772 : /* Second pass through arglist, transforming arguments as appropriate. */
773 :
774 26347 : append_arg (&decoded_options[0]); /* Start with command name, of course. */
775 725850 : for (i = 1; i < argc; ++i)
776 : {
777 : #if defined(DEBUG_ARG)
778 : printf ("2nd pass: %s",
779 : decoded_options[i].orig_option_with_args_text);
780 : if ((args[i] & SKIPOPT) != 0)
781 : printf (" skipped");
782 : if ((args[i] & M2SOURCE) != 0)
783 : printf (" m2 source");
784 : printf ("\n");
785 : #endif
786 673156 : if ((args[i] & SKIPOPT) == 0)
787 : {
788 367262 : if ((args[i] & M2SOURCE) == 0)
789 : {
790 350136 : append_arg (&decoded_options[i]);
791 : /* Make sure -lstdc++ is before the math library, since libstdc++
792 : itself uses those math routines. */
793 350136 : if (!saw_math && (args[i] & MATHLIB) && library > 0)
794 : saw_math = &decoded_options[i];
795 :
796 350136 : if (!saw_libc && (args[i] & WITHLIBC) && library > 0)
797 : saw_libc = &decoded_options[i];
798 : }
799 : else
800 : {
801 17126 : if ((! seen_x_flag) && module_extension)
802 : {
803 : #if defined(DEBUG_ARG)
804 : printf (" adding: -x modula-2 ");
805 : #endif
806 6 : append_option (OPT_x, "modula-2", 1);
807 : }
808 17126 : append_arg (&decoded_options[i]);
809 : #if defined(DEBUG_ARG)
810 : printf (" adding: %s\n",
811 : decoded_options[i].orig_option_with_args_text);
812 : #endif
813 17126 : if ((! seen_x_flag) && module_extension)
814 : {
815 : #if defined(DEBUG_ARG)
816 : printf (" adding: -x none ");
817 : #endif
818 6 : append_option (OPT_x, "none", 1);
819 : }
820 : }
821 : }
822 : #if defined(DEBUG_ARG)
823 : else
824 : printf ("skipping: %s\n",
825 : decoded_options[i].orig_option_with_args_text);
826 : #endif
827 : }
828 :
829 26347 : add_m2_I_path ();
830 : /* We now add in extra arguments to facilitate a successful link.
831 : Note that the libraries are added to the end of the link here
832 : and also placed earlier into the link by lang-specs.h. Possibly
833 : this is needed because the m2pim,m2iso libraries are cross linked
834 : (--fixme-- combine all the m2 libraries into a single archive).
835 :
836 : We also add default scaffold linking options. */
837 :
838 : /* If we have not seen either uselist or gen_module_list and we need
839 : to link or compile a module list then we turn on -fgen_module_list=-
840 : as the default. */
841 26347 : if (!seen_uselist && !seen_gen_module_list
842 26347 : && (linking || scaffold_main))
843 11894 : append_option (OPT_fgen_module_list_, "-", 1);
844 :
845 : /* We checked that they were not both enabled above, if there was a set
846 : value (even iff that is 'off'), pass that to the FE. */
847 26347 : if (seen_scaffold_dynamic || scaffold_dynamic)
848 26347 : append_option (OPT_fscaffold_dynamic, NULL, scaffold_dynamic);
849 26347 : if (seen_scaffold_static)
850 22 : append_option (OPT_fscaffold_static, NULL, scaffold_static);
851 :
852 : /* If the user has set fscaffold-main specifically, use that. Otherwise, if
853 : we are linking then set it so that we generate the relevant code for the
854 : main module. */
855 26347 : if (seen_scaffold_main)
856 126 : append_option (OPT_fscaffold_main, NULL, scaffold_main);
857 26221 : else if (linking)
858 11798 : append_option (OPT_fscaffold_main, NULL, true);
859 :
860 26347 : if (allow_libraries)
861 : {
862 : /* If the libraries have not been specified by the user, select the
863 : appropriate libraries for the active dialect. */
864 5306 : if (libraries == NULL)
865 : {
866 5306 : if (strcmp (dialect, "iso") == 0)
867 310 : libraries = xstrdup ("m2iso,m2cor,m2pim,m2log");
868 : else
869 : /* Default to pim libraries otherwise. */
870 4996 : libraries = xstrdup ("m2cor,m2log,m2pim,m2iso");
871 : }
872 5306 : libraries = convert_abbreviations (libraries);
873 5306 : append_option (OPT_flibs_, xstrdup (libraries), 1);
874 : }
875 : else
876 21041 : append_option (OPT_flibs_, xstrdup ("-"), 0); /* no system libs. */
877 :
878 26347 : if (need_plugin)
879 302 : append_option (OPT_fplugin_, "m2rte", 1);
880 :
881 26347 : if (linking)
882 : {
883 11882 : if (allow_libraries)
884 : {
885 : #ifdef HAVE_LD_STATIC_DYNAMIC
886 4641 : if (!shared_libgm2)
887 0 : append_option (OPT_Wl_, LD_STATIC_OPTION, 1);
888 : #endif
889 4641 : added_libraries += add_default_libs (libraries);
890 : #ifdef HAVE_LD_STATIC_DYNAMIC
891 4641 : if (!shared_libgm2)
892 0 : append_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1);
893 : #endif
894 : }
895 :
896 : /* Add `-lstdc++' if we haven't already done so. */
897 : #ifdef HAVE_LD_STATIC_DYNAMIC
898 11882 : if (library > 1 && !static_link)
899 0 : append_option (OPT_Wl_, LD_STATIC_OPTION, 1);
900 : #endif
901 11882 : if (which_library == USE_LIBCXX)
902 : {
903 0 : append_option (OPT_l, saw_profile_flag ? LIBCXX_PROFILE : LIBCXX, 1);
904 0 : added_libraries++;
905 0 : if (LIBCXXABI != NULL)
906 : {
907 0 : append_option (OPT_l, saw_profile_flag ? LIBCXXABI_PROFILE
908 : : LIBCXXABI, 1);
909 0 : added_libraries++;
910 : }
911 : }
912 : else
913 : {
914 11882 : append_option (OPT_l, saw_profile_flag ? LIBSTDCXX_PROFILE
915 : : LIBSTDCXX, 1);
916 11882 : added_libraries++;
917 : }
918 : /* Add target-dependent static library, if necessary. */
919 11882 : if ((static_link || library > 1) && LIBSTDCXX_STATIC != NULL)
920 : {
921 : append_option (OPT_l, LIBSTDCXX_STATIC, 1);
922 : added_libraries++;
923 : }
924 : #ifdef HAVE_LD_STATIC_DYNAMIC
925 11882 : if (library > 1 && !static_link)
926 0 : append_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1);
927 : #endif
928 : }
929 26347 : if (need_math)
930 : {
931 23669 : append_option (OPT_l, saw_profile_flag ? MATH_LIBRARY_PROFILE :
932 : MATH_LIBRARY, 1);
933 23669 : added_libraries++;
934 : }
935 26347 : if (need_pthread)
936 : {
937 26347 : append_option (OPT_l, "pthread", 1);
938 26347 : added_libraries++;
939 : }
940 26347 : if (shared_libgcc && !static_link)
941 26347 : append_option (OPT_shared_libgcc, NULL, 1);
942 :
943 26347 : if (verbose && gm2_new_decoded_options != gm2_x_decoded_options)
944 : {
945 0 : fprintf (stderr, _("Driving:"));
946 0 : for (i = 0; i < gm2_newargc; i++)
947 0 : fprintf (stderr, " %s",
948 0 : gm2_new_decoded_options[i].orig_option_with_args_text);
949 0 : fprintf (stderr, "\n");
950 0 : fprintf (stderr, "new argc = %d, added_libraries = %d\n",
951 : gm2_newargc, added_libraries);
952 : }
953 :
954 26347 : *in_decoded_options_count = gm2_newargc;
955 26347 : *in_decoded_options = gm2_new_decoded_options;
956 26347 : *in_added_libraries = added_libraries;
957 : }
958 :
959 :
960 : /* Called before linking. Returns 0 on success and -1 on failure. */
961 : int
962 15343 : lang_specific_pre_link (void) /* Not used for M2. */
963 : {
964 15343 : return 0;
965 : }
966 :
967 : /* Number of extra output files that lang_specific_pre_link may generate. */
968 : int lang_specific_extra_outfiles = 0;
|