gospec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* gospec.c -- Specific flags and argument handling of the gcc Go front end.
  2. Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "gcc.h"
  20. #include "opts.h"
  21. /* This bit is set if we saw a `-xfoo' language specification. */
  22. #define LANGSPEC (1<<1)
  23. /* This bit is set if they did `-lm' or `-lmath'. */
  24. #define MATHLIB (1<<2)
  25. /* This bit is set if they did `-lpthread'. */
  26. #define THREADLIB (1<<3)
  27. /* This bit is set if they did `-lc'. */
  28. #define WITHLIBC (1<<4)
  29. /* Skip this option. */
  30. #define SKIPOPT (1<<5)
  31. #ifndef MATH_LIBRARY
  32. #define MATH_LIBRARY "m"
  33. #endif
  34. #ifndef MATH_LIBRARY_PROFILE
  35. #define MATH_LIBRARY_PROFILE MATH_LIBRARY
  36. #endif
  37. #define THREAD_LIBRARY "pthread"
  38. #define THREAD_LIBRARY_PROFILE THREAD_LIBRARY
  39. #define LIBGO "go"
  40. #define LIBGO_PROFILE LIBGO
  41. #define LIBGOBEGIN "gobegin"
  42. void
  43. lang_specific_driver (struct cl_decoded_option **in_decoded_options,
  44. unsigned int *in_decoded_options_count,
  45. int *in_added_libraries)
  46. {
  47. unsigned int i, j;
  48. /* If true, the user gave us the `-p' or `-pg' flag. */
  49. bool saw_profile_flag = false;
  50. /* This is a tristate:
  51. -1 means we should not link in libgo
  52. 0 means we should link in libgo if it is needed
  53. 1 means libgo is needed and should be linked in.
  54. 2 means libgo is needed and should be linked statically. */
  55. int library = 0;
  56. /* The new argument list will be contained in this. */
  57. struct cl_decoded_option *new_decoded_options;
  58. /* "-lm" or "-lmath" if it appears on the command line. */
  59. const struct cl_decoded_option *saw_math = 0;
  60. /* "-lpthread" if it appears on the command line. */
  61. const struct cl_decoded_option *saw_thread = 0;
  62. /* "-lc" if it appears on the command line. */
  63. const struct cl_decoded_option *saw_libc = 0;
  64. /* An array used to flag each argument that needs a bit set for
  65. LANGSPEC, MATHLIB, or WITHLIBC. */
  66. int *args;
  67. /* Whether we need the thread library. */
  68. int need_thread = 0;
  69. /* By default, we throw on the math library if we have one. */
  70. int need_math = (MATH_LIBRARY[0] != '\0');
  71. /* True if we saw -static. */
  72. int static_link = 0;
  73. /* True if we should add -shared-libgcc to the command-line. */
  74. int shared_libgcc = 1;
  75. /* The total number of arguments with the new stuff. */
  76. unsigned int argc;
  77. /* The argument list. */
  78. struct cl_decoded_option *decoded_options;
  79. /* The number of libraries added in. */
  80. int added_libraries;
  81. /* The total number of arguments with the new stuff. */
  82. int num_args = 1;
  83. /* Whether the -o option was used. */
  84. bool saw_opt_o = false;
  85. /* Whether the -c option was used. Also used for -E, -fsyntax-only,
  86. in general anything which implies only compilation and not
  87. linking. */
  88. bool saw_opt_c = false;
  89. /* Whether the -S option was used. */
  90. bool saw_opt_S = false;
  91. /* The first input file with an extension of .go. */
  92. const char *first_go_file = NULL;
  93. argc = *in_decoded_options_count;
  94. decoded_options = *in_decoded_options;
  95. added_libraries = *in_added_libraries;
  96. args = XCNEWVEC (int, argc);
  97. for (i = 1; i < argc; i++)
  98. {
  99. const char *arg = decoded_options[i].arg;
  100. switch (decoded_options[i].opt_index)
  101. {
  102. case OPT_nostdlib:
  103. case OPT_nodefaultlibs:
  104. library = -1;
  105. break;
  106. case OPT_l:
  107. if (strcmp (arg, MATH_LIBRARY) == 0)
  108. {
  109. args[i] |= MATHLIB;
  110. need_math = 0;
  111. }
  112. else if (strcmp (arg, THREAD_LIBRARY) == 0)
  113. args[i] |= THREADLIB;
  114. else if (strcmp (arg, "c") == 0)
  115. args[i] |= WITHLIBC;
  116. else
  117. /* Unrecognized libraries (e.g. -lfoo) may require libgo. */
  118. library = (library == 0) ? 1 : library;
  119. break;
  120. case OPT_pg:
  121. case OPT_p:
  122. saw_profile_flag = true;
  123. break;
  124. case OPT_x:
  125. if (library == 0 && strcmp (arg, "go") == 0)
  126. library = 1;
  127. break;
  128. case OPT_Xlinker:
  129. case OPT_Wl_:
  130. /* Arguments that go directly to the linker might be .o files,
  131. or something, and so might cause libgo to be needed. */
  132. if (library == 0)
  133. library = 1;
  134. break;
  135. case OPT_c:
  136. case OPT_E:
  137. case OPT_M:
  138. case OPT_MM:
  139. case OPT_fsyntax_only:
  140. /* Don't specify libraries if we won't link, since that would
  141. cause a warning. */
  142. saw_opt_c = true;
  143. library = -1;
  144. break;
  145. case OPT_S:
  146. saw_opt_S = true;
  147. library = -1;
  148. break;
  149. case OPT_o:
  150. saw_opt_o = true;
  151. break;
  152. case OPT_static:
  153. static_link = 1;
  154. break;
  155. case OPT_static_libgcc:
  156. shared_libgcc = 0;
  157. break;
  158. case OPT_static_libgo:
  159. library = library >= 0 ? 2 : library;
  160. args[i] |= SKIPOPT;
  161. break;
  162. case OPT_SPECIAL_input_file:
  163. if (library == 0)
  164. library = 1;
  165. if (first_go_file == NULL)
  166. {
  167. int len;
  168. len = strlen (arg);
  169. if (len > 3 && strcmp (arg + len - 3, ".go") == 0)
  170. first_go_file = arg;
  171. }
  172. break;
  173. }
  174. }
  175. /* There's no point adding -shared-libgcc if we don't have a shared
  176. libgcc. */
  177. #ifndef ENABLE_SHARED_LIBGCC
  178. shared_libgcc = 0;
  179. #endif
  180. /* Make sure to have room for the trailing NULL argument. */
  181. num_args = argc + need_math + shared_libgcc + (library > 0) * 5 + 10;
  182. new_decoded_options = XNEWVEC (struct cl_decoded_option, num_args);
  183. i = 0;
  184. j = 0;
  185. /* Copy the 0th argument, i.e., the name of the program itself. */
  186. new_decoded_options[j++] = decoded_options[i++];
  187. /* If we are linking, pass -fsplit-stack if it is supported. */
  188. #ifdef TARGET_CAN_SPLIT_STACK
  189. if (library >= 0)
  190. {
  191. generate_option (OPT_fsplit_stack, NULL, 1, CL_DRIVER,
  192. &new_decoded_options[j]);
  193. j++;
  194. }
  195. #endif
  196. /* NOTE: We start at 1 now, not 0. */
  197. while (i < argc)
  198. {
  199. new_decoded_options[j] = decoded_options[i];
  200. /* Make sure -lgo is before the math library, since libgo itself
  201. uses those math routines. */
  202. if (!saw_math && (args[i] & MATHLIB) && library > 0)
  203. {
  204. --j;
  205. saw_math = &decoded_options[i];
  206. }
  207. if (!saw_thread && (args[i] & THREADLIB) && library > 0)
  208. {
  209. --j;
  210. saw_thread = &decoded_options[i];
  211. }
  212. if (!saw_libc && (args[i] & WITHLIBC) && library > 0)
  213. {
  214. --j;
  215. saw_libc = &decoded_options[i];
  216. }
  217. if ((args[i] & SKIPOPT) != 0)
  218. --j;
  219. i++;
  220. j++;
  221. }
  222. /* If we didn't see a -o option, add one. This is because we need
  223. the driver to pass all .go files to go1. Without a -o option the
  224. driver will invoke go1 separately for each input file. FIXME:
  225. This should probably use some other interface to force the driver
  226. to set combine_inputs. */
  227. if (first_go_file != NULL && !saw_opt_o)
  228. {
  229. if (saw_opt_c || saw_opt_S)
  230. {
  231. const char *base;
  232. int baselen;
  233. int alen;
  234. char *out;
  235. base = lbasename (first_go_file);
  236. baselen = strlen (base) - 3;
  237. alen = baselen + 3;
  238. out = XNEWVEC (char, alen);
  239. memcpy (out, base, baselen);
  240. /* The driver will convert .o to some other suffix (e.g.,
  241. .obj) if appropriate. */
  242. out[baselen] = '.';
  243. if (saw_opt_S)
  244. out[baselen + 1] = 's';
  245. else
  246. out[baselen + 1] = 'o';
  247. out[baselen + 2] = '\0';
  248. generate_option (OPT_o, out, 1, CL_DRIVER,
  249. &new_decoded_options[j]);
  250. }
  251. else
  252. generate_option (OPT_o, "a.out", 1, CL_DRIVER,
  253. &new_decoded_options[j]);
  254. j++;
  255. }
  256. /* Add `-lgo' if we haven't already done so. */
  257. if (library > 0)
  258. {
  259. generate_option (OPT_l, LIBGOBEGIN, 1, CL_DRIVER,
  260. &new_decoded_options[j]);
  261. added_libraries++;
  262. j++;
  263. #ifdef HAVE_LD_STATIC_DYNAMIC
  264. if (library > 1 && !static_link)
  265. {
  266. generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
  267. &new_decoded_options[j]);
  268. j++;
  269. }
  270. #endif
  271. generate_option (OPT_l, saw_profile_flag ? LIBGO_PROFILE : LIBGO, 1,
  272. CL_DRIVER, &new_decoded_options[j]);
  273. added_libraries++;
  274. j++;
  275. #ifdef HAVE_LD_STATIC_DYNAMIC
  276. if (library > 1 && !static_link)
  277. {
  278. generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
  279. &new_decoded_options[j]);
  280. j++;
  281. }
  282. #endif
  283. /* When linking libgo statically we also need to link with the
  284. pthread library. */
  285. if (library > 1 || static_link)
  286. need_thread = 1;
  287. }
  288. if (saw_thread)
  289. new_decoded_options[j++] = *saw_thread;
  290. else if (library > 0 && need_thread)
  291. {
  292. generate_option (OPT_l,
  293. (saw_profile_flag
  294. ? THREAD_LIBRARY_PROFILE
  295. : THREAD_LIBRARY),
  296. 1, CL_DRIVER, &new_decoded_options[j]);
  297. added_libraries++;
  298. j++;
  299. }
  300. if (saw_math)
  301. new_decoded_options[j++] = *saw_math;
  302. else if (library > 0 && need_math)
  303. {
  304. generate_option (OPT_l,
  305. saw_profile_flag ? MATH_LIBRARY_PROFILE : MATH_LIBRARY,
  306. 1, CL_DRIVER, &new_decoded_options[j]);
  307. added_libraries++;
  308. j++;
  309. }
  310. if (saw_libc)
  311. new_decoded_options[j++] = *saw_libc;
  312. if (shared_libgcc && !static_link)
  313. generate_option (OPT_shared_libgcc, NULL, 1, CL_DRIVER,
  314. &new_decoded_options[j++]);
  315. #ifdef TARGET_CAN_SPLIT_STACK
  316. /* libgcc wraps pthread_create to support split stack, however, due to
  317. relative ordering of -lpthread and -lgcc, we can't just mark
  318. __real_pthread_create in libgcc as non-weak. But we need to link in
  319. pthread_create from pthread if we are statically linking, so we work-
  320. around by passing -u pthread_create to to the linker. */
  321. if (static_link)
  322. {
  323. generate_option (OPT_Wl_, "-u,pthread_create", 1, CL_DRIVER,
  324. &new_decoded_options[j]);
  325. j++;
  326. }
  327. #endif
  328. #if defined(TARGET_SOLARIS) && !defined(USE_GLD)
  329. /* We use a common symbol for go$zerovalue. On Solaris, when not
  330. using the GNU linker, the Solaris linker needs an option to not
  331. warn about this. Everything works without this option, but you
  332. get unsightly warnings at link time. */
  333. generate_option (OPT_Wl_, "-t", 1, CL_DRIVER, &new_decoded_options[j]);
  334. j++;
  335. #endif
  336. *in_decoded_options_count = j;
  337. *in_decoded_options = new_decoded_options;
  338. *in_added_libraries = added_libraries;
  339. }
  340. /* Called before linking. Returns 0 on success and -1 on failure. */
  341. int lang_specific_pre_link (void) /* Not used for Go. */
  342. {
  343. return 0;
  344. }
  345. /* Number of extra output files that lang_specific_pre_link may generate. */
  346. int lang_specific_extra_outfiles = 0; /* Not used for Go. */