1 /* gospec.cc -- Specific flags and argument handling of the gcc Go front end.
2 Copyright (C) 2009-2022 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
26 /* This bit is set if we saw a `-xfoo' language specification. */
27 #define LANGSPEC (1<<1)
28 /* This bit is set if they did `-lm' or `-lmath'. */
29 #define MATHLIB (1<<2)
30 /* This bit is set if they did `-lpthread'. */
31 #define THREADLIB (1<<3)
32 /* This bit is set if they did `-lc'. */
33 #define WITHLIBC (1<<4)
34 /* Skip this option. */
35 #define SKIPOPT (1<<5)
38 #define MATH_LIBRARY "m"
40 #ifndef MATH_LIBRARY_PROFILE
41 #define MATH_LIBRARY_PROFILE MATH_LIBRARY
44 #define THREAD_LIBRARY "pthread"
45 #define THREAD_LIBRARY_PROFILE THREAD_LIBRARY
48 #define LIBGO_PROFILE LIBGO
49 #define LIBGOBEGIN "gobegin"
52 lang_specific_driver (struct cl_decoded_option
**in_decoded_options
,
53 unsigned int *in_decoded_options_count
,
54 int *in_added_libraries
)
58 /* If true, the user gave us the `-p' or `-pg' flag. */
59 bool saw_profile_flag
= false;
61 /* This is a tristate:
62 -1 means we should not link in libgo
63 0 means we should link in libgo if it is needed
64 1 means libgo is needed and should be linked in.
65 2 means libgo is needed and should be linked statically. */
68 /* The new argument list will be contained in this. */
69 struct cl_decoded_option
*new_decoded_options
;
71 /* "-lm" or "-lmath" if it appears on the command line. */
72 const struct cl_decoded_option
*saw_math
= 0;
74 /* "-lpthread" if it appears on the command line. */
75 const struct cl_decoded_option
*saw_thread
= 0;
77 /* "-lc" if it appears on the command line. */
78 const struct cl_decoded_option
*saw_libc
= 0;
80 /* An array used to flag each argument that needs a bit set for
81 LANGSPEC, MATHLIB, or WITHLIBC. */
84 /* Whether we need the thread library. */
87 /* By default, we throw on the math library if we have one. */
88 int need_math
= (MATH_LIBRARY
[0] != '\0');
90 /* True if we saw -static. */
93 /* True if we should add -shared-libgcc to the command-line. */
94 int shared_libgcc
= 1;
96 /* The total number of arguments with the new stuff. */
99 /* The argument list. */
100 struct cl_decoded_option
*decoded_options
;
102 /* The number of libraries added in. */
105 /* The total number of arguments with the new stuff. */
108 /* Supports split stack */
109 int supports_split_stack
= 0;
111 /* Whether the -o option was used. */
112 bool saw_opt_o
= false;
114 /* Whether the -c option was used. Also used for -E, -fsyntax-only,
115 in general anything which implies only compilation and not
117 bool saw_opt_c
= false;
119 /* Whether the -S option was used. */
120 bool saw_opt_S
= false;
122 #ifdef TARGET_CAN_SPLIT_STACK_64BIT
123 /* Whether the -m64 option is in force. */
124 bool is_m64
= TARGET_CAN_SPLIT_STACK_64BIT
;
127 /* The first input file with an extension of .go. */
128 const char *first_go_file
= NULL
;
130 /* Whether we saw any -g option. */
131 bool saw_opt_g
= false;
133 argc
= *in_decoded_options_count
;
134 decoded_options
= *in_decoded_options
;
135 added_libraries
= *in_added_libraries
;
137 args
= XCNEWVEC (int, argc
);
139 for (i
= 1; i
< argc
; i
++)
141 const char *arg
= decoded_options
[i
].arg
;
143 switch (decoded_options
[i
].opt_index
)
147 case OPT_nodefaultlibs
:
152 if (strcmp (arg
, MATH_LIBRARY
) == 0)
157 else if (strcmp (arg
, THREAD_LIBRARY
) == 0)
158 args
[i
] |= THREADLIB
;
159 else if (strcmp (arg
, "c") == 0)
162 /* Unrecognized libraries (e.g. -lfoo) may require libgo. */
163 library
= (library
== 0) ? 1 : library
;
166 #ifdef TARGET_CAN_SPLIT_STACK_64BIT
178 saw_profile_flag
= true;
182 if (library
== 0 && strcmp (arg
, "go") == 0)
188 /* Arguments that go directly to the linker might be .o files,
189 or something, and so might cause libgo to be needed. */
198 case OPT_fsyntax_only
:
199 /* Don't specify libraries if we won't link, since that would
230 case OPT_static_libgcc
:
234 case OPT_static_libgo
:
235 library
= library
>= 0 ? 2 : library
;
239 case OPT_SPECIAL_input_file
:
243 if (first_go_file
== NULL
)
248 if (len
> 3 && strcmp (arg
+ len
- 3, ".go") == 0)
256 /* There's no point adding -shared-libgcc if we don't have a shared
258 #ifndef ENABLE_SHARED_LIBGCC
262 /* Make sure to have room for the trailing NULL argument. */
263 num_args
= argc
+ need_math
+ shared_libgcc
+ (library
> 0) * 5 + 10;
264 new_decoded_options
= XNEWVEC (struct cl_decoded_option
, num_args
);
269 /* Copy the 0th argument, i.e., the name of the program itself. */
270 new_decoded_options
[j
++] = decoded_options
[i
++];
272 #ifdef TARGET_CAN_SPLIT_STACK
273 supports_split_stack
= 1;
276 #ifdef TARGET_CAN_SPLIT_STACK_64BIT
278 supports_split_stack
= 1;
281 /* If we are linking, pass -fsplit-stack if it is supported. */
282 if ((library
>= 0) && supports_split_stack
)
284 generate_option (OPT_fsplit_stack
, NULL
, 1, CL_DRIVER
,
285 &new_decoded_options
[j
]);
289 /* The go1 compiler is going to enable debug info by default. If we
290 don't see any -g options, force -g, so that we invoke the
291 assembler with the right debug option. */
294 generate_option (OPT_g
, "1", 0, CL_DRIVER
, &new_decoded_options
[j
]);
298 /* NOTE: We start at 1 now, not 0. */
301 new_decoded_options
[j
] = decoded_options
[i
];
303 /* Make sure -lgo is before the math library, since libgo itself
304 uses those math routines. */
305 if (!saw_math
&& (args
[i
] & MATHLIB
) && library
> 0)
308 saw_math
= &decoded_options
[i
];
311 if (!saw_thread
&& (args
[i
] & THREADLIB
) && library
> 0)
314 saw_thread
= &decoded_options
[i
];
317 if (!saw_libc
&& (args
[i
] & WITHLIBC
) && library
> 0)
320 saw_libc
= &decoded_options
[i
];
323 if ((args
[i
] & SKIPOPT
) != 0)
330 /* If we didn't see a -o option, add one. This is because we need
331 the driver to pass all .go files to go1. Without a -o option the
332 driver will invoke go1 separately for each input file. FIXME:
333 This should probably use some other interface to force the driver
334 to set combine_inputs. */
335 if (first_go_file
!= NULL
&& !saw_opt_o
)
337 if (saw_opt_c
|| saw_opt_S
)
344 base
= lbasename (first_go_file
);
345 baselen
= strlen (base
) - 3;
347 out
= XNEWVEC (char, alen
);
348 memcpy (out
, base
, baselen
);
349 /* The driver will convert .o to some other suffix (e.g.,
350 .obj) if appropriate. */
353 out
[baselen
+ 1] = 's';
355 out
[baselen
+ 1] = 'o';
356 out
[baselen
+ 2] = '\0';
357 generate_option (OPT_o
, out
, 1, CL_DRIVER
,
358 &new_decoded_options
[j
]);
361 generate_option (OPT_o
, "a.out", 1, CL_DRIVER
,
362 &new_decoded_options
[j
]);
366 /* Add `-lgo' if we haven't already done so. */
369 generate_option (OPT_l
, LIBGOBEGIN
, 1, CL_DRIVER
,
370 &new_decoded_options
[j
]);
374 #ifdef HAVE_LD_STATIC_DYNAMIC
375 if (library
> 1 && !static_link
)
377 generate_option (OPT_Wl_
, LD_STATIC_OPTION
, 1, CL_DRIVER
,
378 &new_decoded_options
[j
]);
383 generate_option (OPT_l
, saw_profile_flag
? LIBGO_PROFILE
: LIBGO
, 1,
384 CL_DRIVER
, &new_decoded_options
[j
]);
388 #ifdef HAVE_LD_STATIC_DYNAMIC
389 if (library
> 1 && !static_link
)
391 generate_option (OPT_Wl_
, LD_DYNAMIC_OPTION
, 1, CL_DRIVER
,
392 &new_decoded_options
[j
]);
397 /* When linking libgo statically we also need to link with the
399 if (library
> 1 || static_link
)
404 new_decoded_options
[j
++] = *saw_thread
;
405 else if (library
> 0 && need_thread
)
407 generate_option (OPT_l
,
409 ? THREAD_LIBRARY_PROFILE
411 1, CL_DRIVER
, &new_decoded_options
[j
]);
417 new_decoded_options
[j
++] = *saw_math
;
418 else if (library
> 0 && need_math
)
420 generate_option (OPT_l
,
421 saw_profile_flag
? MATH_LIBRARY_PROFILE
: MATH_LIBRARY
,
422 1, CL_DRIVER
, &new_decoded_options
[j
]);
428 new_decoded_options
[j
++] = *saw_libc
;
429 if (shared_libgcc
&& !static_link
)
430 generate_option (OPT_shared_libgcc
, NULL
, 1, CL_DRIVER
,
431 &new_decoded_options
[j
++]);
433 /* libgcc wraps pthread_create to support split stack, however, due to
434 relative ordering of -lpthread and -lgcc, we can't just mark
435 __real_pthread_create in libgcc as non-weak. But we need to link in
436 pthread_create from pthread if we are statically linking, so we work-
437 around by passing -u pthread_create to the linker. */
438 if (static_link
&& supports_split_stack
)
440 generate_option (OPT_Wl_
, "-u,pthread_create", 1, CL_DRIVER
,
441 &new_decoded_options
[j
]);
445 #if defined(TARGET_SOLARIS) && !defined(USE_GLD)
446 /* We use a common symbol for go$zerovalue. On Solaris, when not
447 using the GNU linker, the Solaris linker needs an option to not
448 warn about this. Everything works without this option, but you
449 get unsightly warnings at link time. */
450 generate_option (OPT_Wl_
, "-t", 1, CL_DRIVER
, &new_decoded_options
[j
]);
454 *in_decoded_options_count
= j
;
455 *in_decoded_options
= new_decoded_options
;
456 *in_added_libraries
= added_libraries
;
459 /* Called before linking. Returns 0 on success and -1 on failure. */
460 int lang_specific_pre_link (void) /* Not used for Go. */
465 /* Number of extra output files that lang_specific_pre_link may generate. */
466 int lang_specific_extra_outfiles
= 0; /* Not used for Go. */