darwin-driver.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* Additional functions for the GCC driver on Darwin native.
  2. Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. Contributed by Apple Computer Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "gcc.h"
  21. #include "opts.h"
  22. #ifndef CROSS_DIRECTORY_STRUCTURE
  23. #include <sys/sysctl.h>
  24. #include "xregex.h"
  25. static char *
  26. darwin_find_version_from_kernel (void)
  27. {
  28. char osversion[32];
  29. size_t osversion_len = sizeof (osversion) - 1;
  30. static int osversion_name[2] = { CTL_KERN, KERN_OSRELEASE };
  31. int major_vers;
  32. char minor_vers[6];
  33. char * version_p;
  34. char * version_pend;
  35. char * new_flag;
  36. /* Determine the version of the running OS. If we can't, warn user,
  37. and do nothing. */
  38. if (sysctl (osversion_name, ARRAY_SIZE (osversion_name), osversion,
  39. &osversion_len, NULL, 0) == -1)
  40. {
  41. warning (0, "sysctl for kern.osversion failed: %m");
  42. return NULL;
  43. }
  44. /* Try to parse the first two parts of the OS version number. Warn
  45. user and return if it doesn't make sense. */
  46. if (! ISDIGIT (osversion[0]))
  47. goto parse_failed;
  48. major_vers = osversion[0] - '0';
  49. version_p = osversion + 1;
  50. if (ISDIGIT (*version_p))
  51. major_vers = major_vers * 10 + (*version_p++ - '0');
  52. if (*version_p++ != '.')
  53. goto parse_failed;
  54. version_pend = strchr(version_p, '.');
  55. if (!version_pend)
  56. goto parse_failed;
  57. if (! ISDIGIT (*version_p))
  58. goto parse_failed;
  59. strncpy(minor_vers, version_p, version_pend - version_p);
  60. minor_vers[version_pend - version_p] = '\0';
  61. /* The major kernel version number is 4 plus the second OS version
  62. component. */
  63. if (major_vers - 4 <= 4)
  64. /* On 10.4 and earlier, the old linker is used which does not
  65. support three-component system versions. */
  66. asprintf (&new_flag, "10.%d", major_vers - 4);
  67. else
  68. asprintf (&new_flag, "10.%d.%s", major_vers - 4, minor_vers);
  69. return new_flag;
  70. parse_failed:
  71. warning (0, "couldn%'t understand kern.osversion %q.*s",
  72. (int) osversion_len, osversion);
  73. return NULL;
  74. }
  75. #endif
  76. /* When running on a Darwin system and using that system's headers and
  77. libraries, default the -mmacosx-version-min flag to be the version
  78. of the system on which the compiler is running.
  79. When building cross or native cross compilers, default to the OSX
  80. version of the target (as provided by the most specific target header
  81. included in tm.h). This may be overidden by setting the flag explicitly
  82. (or by the MACOSX_DEPLOYMENT_TARGET environment). */
  83. static void
  84. darwin_default_min_version (unsigned int *decoded_options_count,
  85. struct cl_decoded_option **decoded_options)
  86. {
  87. const unsigned int argc = *decoded_options_count;
  88. struct cl_decoded_option *const argv = *decoded_options;
  89. unsigned int i;
  90. const char *new_flag;
  91. /* If the command-line is empty, just return. */
  92. if (argc <= 1)
  93. return;
  94. /* Don't do this if the user specified -mmacosx-version-min= or
  95. -mno-macosx-version-min. */
  96. for (i = 1; i < argc; i++)
  97. if (argv[i].opt_index == OPT_mmacosx_version_min_)
  98. return;
  99. /* Retrieve the deployment target from the environment and insert
  100. it as a flag. */
  101. {
  102. const char * macosx_deployment_target;
  103. macosx_deployment_target = getenv ("MACOSX_DEPLOYMENT_TARGET");
  104. if (macosx_deployment_target
  105. /* Apparently, an empty string for MACOSX_DEPLOYMENT_TARGET means
  106. "use the default". Or, possibly "use 10.1". We choose
  107. to ignore the environment variable, as if it was never set. */
  108. && macosx_deployment_target[0])
  109. {
  110. ++*decoded_options_count;
  111. *decoded_options = XNEWVEC (struct cl_decoded_option,
  112. *decoded_options_count);
  113. (*decoded_options)[0] = argv[0];
  114. generate_option (OPT_mmacosx_version_min_, macosx_deployment_target,
  115. 1, CL_DRIVER, &(*decoded_options)[1]);
  116. memcpy (*decoded_options + 2, argv + 1,
  117. (argc - 1) * sizeof (struct cl_decoded_option));
  118. return;
  119. }
  120. }
  121. #ifndef CROSS_DIRECTORY_STRUCTURE
  122. /* Try to find the version from the kernel, if we fail - we print a message
  123. and give up. */
  124. new_flag = darwin_find_version_from_kernel ();
  125. if (!new_flag)
  126. return;
  127. #else
  128. /* For cross-compilers, default to the target OS version. */
  129. new_flag = DEF_MIN_OSX_VERSION;
  130. #endif /* CROSS_DIRECTORY_STRUCTURE */
  131. /* Add the new flag. */
  132. ++*decoded_options_count;
  133. *decoded_options = XNEWVEC (struct cl_decoded_option,
  134. *decoded_options_count);
  135. (*decoded_options)[0] = argv[0];
  136. generate_option (OPT_mmacosx_version_min_, new_flag,
  137. 1, CL_DRIVER, &(*decoded_options)[1]);
  138. memcpy (*decoded_options + 2, argv + 1,
  139. (argc - 1) * sizeof (struct cl_decoded_option));
  140. return;
  141. }
  142. /* Translate -filelist and -framework options in *DECODED_OPTIONS
  143. (size *DECODED_OPTIONS_COUNT) to use -Xlinker so that they are
  144. considered to be linker inputs in the case that no other inputs are
  145. specified. Handling these options in DRIVER_SELF_SPECS does not
  146. suffice because specs are too late to add linker inputs, and
  147. handling them in LINK_SPEC does not suffice because the linker will
  148. not be called if there are no other inputs. When native, also
  149. default the -mmacosx-version-min flag. */
  150. void
  151. darwin_driver_init (unsigned int *decoded_options_count,
  152. struct cl_decoded_option **decoded_options)
  153. {
  154. unsigned int i;
  155. for (i = 1; i < *decoded_options_count; i++)
  156. {
  157. if ((*decoded_options)[i].errors & CL_ERR_MISSING_ARG)
  158. continue;
  159. switch ((*decoded_options)[i].opt_index)
  160. {
  161. #if DARWIN_X86
  162. case OPT_arch:
  163. if (!strcmp ((*decoded_options)[i].arg, "i386"))
  164. generate_option (OPT_m32, NULL, 1, CL_DRIVER, &(*decoded_options)[i]);
  165. else if (!strcmp ((*decoded_options)[i].arg, "x86_64"))
  166. generate_option (OPT_m64, NULL, 1, CL_DRIVER, &(*decoded_options)[i]);
  167. break;
  168. #endif
  169. case OPT_filelist:
  170. case OPT_framework:
  171. ++*decoded_options_count;
  172. *decoded_options = XRESIZEVEC (struct cl_decoded_option,
  173. *decoded_options,
  174. *decoded_options_count);
  175. memmove (*decoded_options + i + 2,
  176. *decoded_options + i + 1,
  177. ((*decoded_options_count - i - 2)
  178. * sizeof (struct cl_decoded_option)));
  179. generate_option (OPT_Xlinker, (*decoded_options)[i].arg, 1,
  180. CL_DRIVER, &(*decoded_options)[i + 1]);
  181. generate_option (OPT_Xlinker,
  182. (*decoded_options)[i].canonical_option[0], 1,
  183. CL_DRIVER, &(*decoded_options)[i]);
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. darwin_default_min_version (decoded_options_count, decoded_options);
  190. }