]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/darwin-driver.c
Update copyright years.
[thirdparty/gcc.git] / gcc / config / darwin-driver.c
1 /* Additional functions for the GCC driver on Darwin native.
2 Copyright (C) 2006-2020 Free Software Foundation, Inc.
3 Contributed by Apple Computer Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "libiberty.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "opts.h"
27 #include "diagnostic-core.h"
28
29 /* Validate a version string (either given on the command line or, perhaps
30 as MACOSX_DEPLOYMENT_TARGET).
31
32 The specs %version-compare() function doesn't accept leading '0' on
33 numbers so strip them out. Do sanity checking here too.
34
35 Return:
36 * original string means it was OK and we didn't want to change it.
37 * new string means it was OK but we rewrote it to avoid possible format
38 problems.
39 * NULL means we didn't like what we saw.
40 */
41
42 static const char *
43 validate_macosx_version_min (const char *version_str)
44 {
45 size_t version_len;
46 unsigned long major, minor, tiny = 0;
47 char *end;
48 const char *old_version = version_str;
49 bool need_rewrite = false;
50
51 version_len = strlen (version_str);
52 if (version_len < 4) /* The minimum would be 10.x */
53 return NULL;
54
55 /* Version string must consist of digits and periods only. */
56 if (strspn (version_str, "0123456789.") != version_len)
57 return NULL;
58
59 if (!ISDIGIT (version_str[0]) || !ISDIGIT (version_str[version_len - 1]))
60 return NULL;
61
62 if (version_str[0] == '0')
63 need_rewrite = true;
64
65 major = strtoul (version_str, &end, 10);
66 version_str = end + ((*end == '.') ? 1 : 0);
67
68 if (major != 10) /* So far .. all MacOS 10 ... */
69 return NULL;
70
71 /* Version string components must be present and numeric. */
72 if (!ISDIGIT (version_str[0]))
73 return NULL;
74
75 /* If we have one or more leading zeros on a component, then rewrite the
76 version string. */
77 if (version_str[0] == '0' && version_str[1] != '\0'
78 && version_str[1] != '.')
79 need_rewrite = true;
80
81 minor = strtoul (version_str, &end, 10);
82 version_str = end + ((*end == '.') ? 1 : 0);
83 if (minor > 99)
84 return NULL;
85
86 /* If 'tiny' is present it must be numeric. */
87 if (*end != '\0' && !ISDIGIT (version_str[0]))
88 return NULL;
89
90 /* If we have one or more leading zeros on a component, then rewrite the
91 version string. */
92 if (*end != '\0' && version_str[0] == '0'
93 && version_str[1] != '\0')
94 need_rewrite = true;
95
96 tiny = strtoul (version_str, &end, 10);
97 if (tiny > 99)
98 return NULL;
99
100 /* Version string must contain no more than three tokens. */
101 if (*end != '\0')
102 return NULL;
103
104 if (need_rewrite)
105 {
106 char *new_version;
107 asprintf (&new_version, "10.%lu.%lu", minor, tiny);
108 return new_version;
109 }
110
111 return old_version;
112 }
113
114 #ifndef CROSS_DIRECTORY_STRUCTURE
115 #include <sys/sysctl.h>
116 #include "xregex.h"
117
118 static char *
119 darwin_find_version_from_kernel (void)
120 {
121 char osversion[32];
122 size_t osversion_len = sizeof (osversion) - 1;
123 static int osversion_name[2] = { CTL_KERN, KERN_OSRELEASE };
124 int major_vers;
125 char * version_p;
126 char * new_flag;
127
128 /* Determine the version of the running OS. If we can't, warn user,
129 and do nothing. */
130 if (sysctl (osversion_name, ARRAY_SIZE (osversion_name), osversion,
131 &osversion_len, NULL, 0) == -1)
132 {
133 warning (0, "sysctl for kern.osversion failed: %m");
134 return NULL;
135 }
136
137 /* Try to parse the first two parts of the OS version number. Warn
138 user and return if it doesn't make sense. */
139 if (! ISDIGIT (osversion[0]))
140 goto parse_failed;
141 major_vers = osversion[0] - '0';
142 version_p = osversion + 1;
143 if (ISDIGIT (*version_p))
144 major_vers = major_vers * 10 + (*version_p++ - '0');
145 if (*version_p++ != '.')
146 goto parse_failed;
147
148 /* The major kernel version number is 4 plus the second OS version
149 component. */
150 if (major_vers - 4 <= 4)
151 /* On 10.4 and earlier, the old linker is used which does not
152 support three-component system versions.
153 FIXME: we should not assume this - a newer linker could be used. */
154 asprintf (&new_flag, "10.%d", major_vers - 4);
155 else
156 /* Although the newer linker supports three-component system
157 versions, there's no guarantee that the minor version component
158 of the kernel and the system are the same. Apple's clang always
159 uses 0 as the minor version: do the same. */
160 asprintf (&new_flag, "10.%d.0", major_vers - 4);
161
162 return new_flag;
163
164 parse_failed:
165 warning (0, "couldn%'t understand kern.osversion %q.*s",
166 (int) osversion_len, osversion);
167 return NULL;
168 }
169 #endif
170
171 /* When running on a Darwin system and using that system's headers and
172 libraries, default the -mmacosx-version-min flag to be the version
173 of the system on which the compiler is running.
174
175 When building cross or native cross compilers, default to the OSX
176 version of the target (as provided by the most specific target header
177 included in tm.h). This may be overidden by setting the flag explicitly
178 (or by the MACOSX_DEPLOYMENT_TARGET environment). */
179
180 static const char *
181 darwin_default_min_version (void)
182 {
183 /* Try to retrieve the deployment target from the environment. */
184 const char *new_flag = getenv ("MACOSX_DEPLOYMENT_TARGET");
185
186 /* Apparently, an empty string for MACOSX_DEPLOYMENT_TARGET means
187 "use the default". Or, possibly "use 10.1". We choose
188 to ignore the environment variable, as if it was never set. */
189 if (new_flag == NULL || new_flag[0] == 0)
190 #ifndef CROSS_DIRECTORY_STRUCTURE
191 /* Try to find the version from the kernel, if we fail - we print a
192 message and give up. */
193 new_flag = darwin_find_version_from_kernel ();
194 #else
195 /* For cross-compilers, default to a minimum version determined by
196 the configuration. */
197 new_flag = DEF_MIN_OSX_VERSION;
198 #endif /* CROSS_DIRECTORY_STRUCTURE */
199
200 if (new_flag != NULL)
201 {
202 const char *checked = validate_macosx_version_min (new_flag);
203 if (checked == NULL)
204 {
205 warning (0, "couldn%'t understand version %s", new_flag);
206 return NULL;
207 }
208 new_flag = xstrndup (checked, strlen (checked));
209 }
210 return new_flag;
211 }
212
213 /* See if we can find the sysroot from the SDKROOT environment variable. */
214
215 static const char *
216 maybe_get_sysroot_from_sdkroot ()
217 {
218 const char *maybe_sysroot = getenv ("SDKROOT");
219
220 /* We'll use the same rules as the clang driver, for compatibility.
221 1) The path must be absolute
222 2) Ignore "/", that is the default anyway and we do not want the
223 sysroot semantics to be applied to it.
224 3) It must exist (actually, we'll check it's readable too). */
225
226 if (maybe_sysroot == NULL
227 || *maybe_sysroot != '/'
228 || strlen (maybe_sysroot) == 1
229 || access (maybe_sysroot, R_OK) == -1)
230 return NULL;
231
232 return xstrndup (maybe_sysroot, strlen (maybe_sysroot));
233 }
234
235 /* Translate -filelist and -framework options in *DECODED_OPTIONS
236 (size *DECODED_OPTIONS_COUNT) to use -Xlinker so that they are
237 considered to be linker inputs in the case that no other inputs are
238 specified. Handling these options in DRIVER_SELF_SPECS does not
239 suffice because specs are too late to add linker inputs, and
240 handling them in LINK_SPEC does not suffice because the linker will
241 not be called if there are no other inputs. When native, also
242 default the -mmacosx-version-min flag. */
243
244 void
245 darwin_driver_init (unsigned int *decoded_options_count,
246 struct cl_decoded_option **decoded_options)
247 {
248 unsigned int i;
249 bool seenX86 = false;
250 bool seenX86_64 = false;
251 bool seenPPC = false;
252 bool seenPPC64 = false;
253 bool seenM32 = false;
254 bool seenM64 = false;
255 bool appendM32 = false;
256 bool appendM64 = false;
257 const char *vers_string = NULL;
258 bool seen_version_min = false;
259 bool seen_sysroot_p = false;
260
261 for (i = 1; i < *decoded_options_count; i++)
262 {
263 if ((*decoded_options)[i].errors & CL_ERR_MISSING_ARG)
264 continue;
265
266 switch ((*decoded_options)[i].opt_index)
267 {
268 case OPT_arch:
269 /* Support provision of a single -arch xxxx flag as a means of
270 specifying the sub-target/multi-lib. Translate this into -m32/64
271 as appropriate. */
272 if (!strcmp ((*decoded_options)[i].arg, "i386"))
273 seenX86 = true;
274 else if (!strcmp ((*decoded_options)[i].arg, "x86_64"))
275 seenX86_64 = true;
276 else if (!strcmp ((*decoded_options)[i].arg, "ppc"))
277 seenPPC = true;
278 else if (!strcmp ((*decoded_options)[i].arg, "ppc64"))
279 seenPPC64 = true;
280 else
281 error ("this compiler does not support %s",
282 (*decoded_options)[i].arg);
283 /* Now we've examined it, drop the -arch arg. */
284 if (*decoded_options_count > i) {
285 memmove (*decoded_options + i,
286 *decoded_options + i + 1,
287 ((*decoded_options_count - i - 1)
288 * sizeof (struct cl_decoded_option)));
289 }
290 --i;
291 --*decoded_options_count;
292 break;
293
294 case OPT_m32:
295 seenM32 = true;
296 break;
297
298 case OPT_m64:
299 seenM64 = true;
300 break;
301
302 case OPT_filelist:
303 case OPT_framework:
304 ++*decoded_options_count;
305 *decoded_options = XRESIZEVEC (struct cl_decoded_option,
306 *decoded_options,
307 *decoded_options_count);
308 memmove (*decoded_options + i + 2,
309 *decoded_options + i + 1,
310 ((*decoded_options_count - i - 2)
311 * sizeof (struct cl_decoded_option)));
312 generate_option (OPT_Xlinker, (*decoded_options)[i].arg, 1,
313 CL_DRIVER, &(*decoded_options)[i + 1]);
314 generate_option (OPT_Xlinker,
315 (*decoded_options)[i].canonical_option[0], 1,
316 CL_DRIVER, &(*decoded_options)[i]);
317 break;
318
319 case OPT_mmacosx_version_min_:
320 seen_version_min = true;
321 vers_string =
322 validate_macosx_version_min ((*decoded_options)[i].arg);
323 if (vers_string == NULL)
324 warning (0, "%qs is not valid for %<mmacosx-version-min%>",
325 (*decoded_options)[i].arg);
326 else if (vers_string == (*decoded_options)[i].arg)
327 vers_string = xstrndup ((*decoded_options)[i].arg, 32);
328 /* Now we've examined it, and verified/re-written, put it to
329 one side and append later. */
330 if (*decoded_options_count > i) {
331 memmove (*decoded_options + i,
332 *decoded_options + i + 1,
333 ((*decoded_options_count - i - 1)
334 * sizeof (struct cl_decoded_option)));
335 }
336 --i;
337 --*decoded_options_count;
338 break;
339
340 case OPT__sysroot_:
341 case OPT_isysroot:
342 seen_sysroot_p = true;
343 break;
344
345 default:
346 break;
347 }
348 }
349
350 /* Turn -arch xxxx into the appropriate -m32/-m64 flag.
351 If the User tried to specify multiple arch flags (which is possible with
352 some Darwin compilers) warn that this mode is not supported by this
353 compiler (and ignore the arch flags, which means that the default multi-
354 lib will be generated). */
355 /* TODO: determine if these warnings would better be errors. */
356 #if DARWIN_X86
357 if (seenPPC || seenPPC64)
358 warning (0, "this compiler does not support PowerPC (arch flags ignored)");
359 if (seenX86)
360 {
361 if (seenX86_64 || seenM64)
362 warning (0, "%s conflicts with i386 (arch flags ignored)",
363 (seenX86_64? "x86_64": "m64"));
364 else if (! seenM32) /* Add -m32 if the User didn't. */
365 appendM32 = true;
366 }
367 else if (seenX86_64)
368 {
369 if (seenX86 || seenM32)
370 warning (0, "%s conflicts with x86_64 (arch flags ignored)",
371 (seenX86? "i386": "m32"));
372 else if (! seenM64) /* Add -m64 if the User didn't. */
373 appendM64 = true;
374 }
375 #elif DARWIN_PPC
376 if (seenX86 || seenX86_64)
377 warning (0, "this compiler does not support X86 (arch flags ignored)");
378 if (seenPPC)
379 {
380 if (seenPPC64 || seenM64)
381 warning (0, "%s conflicts with ppc (arch flags ignored)",
382 (seenPPC64? "ppc64": "m64"));
383 else if (! seenM32) /* Add -m32 if the User didn't. */
384 appendM32 = true;
385 }
386 else if (seenPPC64)
387 {
388 if (seenPPC || seenM32)
389 warning (0, "%s conflicts with ppc64 (arch flags ignored)",
390 (seenPPC? "ppc": "m32"));
391 else if (! seenM64) /* Add -m64 if the User didn't. */
392 appendM64 = true;
393 }
394 #endif
395
396 if (appendM32 || appendM64)
397 {
398 ++*decoded_options_count;
399 *decoded_options = XRESIZEVEC (struct cl_decoded_option,
400 *decoded_options,
401 *decoded_options_count);
402 generate_option (appendM32 ? OPT_m32 : OPT_m64, NULL, 1, CL_DRIVER,
403 &(*decoded_options)[*decoded_options_count - 1]);
404 }
405
406 if (! seen_sysroot_p)
407 {
408 /* We will pick up an SDKROOT if we didn't specify a sysroot and treat
409 it as overriding any configure-time --with-sysroot. */
410 const char *sdkroot = maybe_get_sysroot_from_sdkroot ();
411 if (sdkroot)
412 {
413 ++*decoded_options_count;
414 *decoded_options = XRESIZEVEC (struct cl_decoded_option,
415 *decoded_options,
416 *decoded_options_count);
417 generate_option (OPT__sysroot_, sdkroot, 1, CL_DRIVER,
418 &(*decoded_options)[*decoded_options_count - 1]);
419 }
420 }
421
422 /* We will need to know the OS X version we're trying to build for here
423 so that we can figure out the mechanism and source for the sysroot to
424 be used. */
425 if (! seen_version_min && *decoded_options_count > 1)
426 /* Not set by the User, try to figure it out. */
427 vers_string = darwin_default_min_version ();
428
429 /* Create and push a cleaned up version, plus the major version for
430 assemblers and other cases that need it. */
431 if (vers_string != NULL)
432 {
433 ++*decoded_options_count;
434 *decoded_options = XRESIZEVEC (struct cl_decoded_option,
435 *decoded_options,
436 *decoded_options_count);
437 generate_option (OPT_mmacosx_version_min_, vers_string, 1, CL_DRIVER,
438 &(*decoded_options)[*decoded_options_count - 1]);
439
440 char *asm_major = NULL;
441 const char *first_period = strchr(vers_string, '.');
442 if (first_period != NULL)
443 {
444 const char *second_period = strchr(first_period+1, '.');
445 if (second_period != NULL)
446 asm_major = xstrndup (vers_string, second_period-vers_string);
447 else
448 asm_major = xstrdup (vers_string);
449 }
450 /* Else we appear to have a weird macosx version with no major number.
451 Punt on this for now. */
452 if (asm_major != NULL)
453 {
454 ++*decoded_options_count;
455 *decoded_options = XRESIZEVEC (struct cl_decoded_option,
456 *decoded_options,
457 *decoded_options_count);
458 generate_option (OPT_asm_macosx_version_min_, asm_major, 1, CL_DRIVER,
459 &(*decoded_options)[*decoded_options_count - 1]);
460 }
461 }
462 }