]>
Commit | Line | Data |
---|---|---|
e46b55d0 | 1 | /* Additional functions for the GCC driver on Darwin native. |
aeee4812 | 2 | Copyright (C) 2006-2023 Free Software Foundation, Inc. |
e46b55d0 GK |
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 | |
2f83c7d6 | 9 | the Free Software Foundation; either version 3, or (at your option) |
e46b55d0 GK |
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 | |
2f83c7d6 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
e46b55d0 | 20 | |
e46b55d0 | 21 | #include "config.h" |
45c3fea9 | 22 | #include "libiberty.h" |
e46b55d0 GK |
23 | #include "system.h" |
24 | #include "coretypes.h" | |
25 | #include "tm.h" | |
60cf253a | 26 | #include "opts.h" |
2fe7f26c | 27 | #include "diagnostic-core.h" |
2be55a25 | 28 | |
351ac9c5 IS |
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; | |
96de87b9 | 46 | unsigned long major, minor = 0, tiny = 0; |
351ac9c5 IS |
47 | char *end; |
48 | const char *old_version = version_str; | |
49 | bool need_rewrite = false; | |
50 | ||
51 | version_len = strlen (version_str); | |
96de87b9 | 52 | if (version_len < 2) /* The minimum would be 11 */ |
351ac9c5 IS |
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); | |
351ac9c5 | 66 | |
f18cbc1e FXC |
67 | /* macOS 10, 11, and 12 are known. clang accepts up to 99. */ |
68 | if (major < 10 || major > 99) | |
351ac9c5 IS |
69 | return NULL; |
70 | ||
96de87b9 IS |
71 | /* Skip a separating period, if there's one. */ |
72 | version_str = end + ((*end == '.') ? 1 : 0); | |
73 | ||
11b96757 SJ |
74 | if (major > 10 && *end != '\0' && !ISDIGIT (version_str[0])) |
75 | /* For macOS 11+, we allow just the major number, but if the minor is | |
96de87b9 IS |
76 | there it must be numeric. */ |
77 | return NULL; | |
11b96757 | 78 | else if (major > 10 && *end == '\0') |
96de87b9 IS |
79 | /* We will rewrite 11 => 11.0.0. */ |
80 | need_rewrite = true; | |
81 | else if (major == 10 && (*end == '\0' || !ISDIGIT (version_str[0]))) | |
82 | /* Otherwise, minor version components must be present and numeric. */ | |
351ac9c5 IS |
83 | return NULL; |
84 | ||
85 | /* If we have one or more leading zeros on a component, then rewrite the | |
86 | version string. */ | |
96de87b9 | 87 | if (*end != '\0' && version_str[0] == '0' && version_str[1] != '\0' |
351ac9c5 IS |
88 | && version_str[1] != '.') |
89 | need_rewrite = true; | |
90 | ||
91 | minor = strtoul (version_str, &end, 10); | |
92 | version_str = end + ((*end == '.') ? 1 : 0); | |
93 | if (minor > 99) | |
94 | return NULL; | |
95 | ||
96 | /* If 'tiny' is present it must be numeric. */ | |
97 | if (*end != '\0' && !ISDIGIT (version_str[0])) | |
98 | return NULL; | |
99 | ||
100 | /* If we have one or more leading zeros on a component, then rewrite the | |
101 | version string. */ | |
102 | if (*end != '\0' && version_str[0] == '0' | |
103 | && version_str[1] != '\0') | |
104 | need_rewrite = true; | |
105 | ||
106 | tiny = strtoul (version_str, &end, 10); | |
107 | if (tiny > 99) | |
108 | return NULL; | |
109 | ||
110 | /* Version string must contain no more than three tokens. */ | |
111 | if (*end != '\0') | |
112 | return NULL; | |
113 | ||
114 | if (need_rewrite) | |
115 | { | |
116 | char *new_version; | |
556ab512 | 117 | asprintf (&new_version, "%2lu.%lu.%lu", major, minor, tiny); |
351ac9c5 IS |
118 | return new_version; |
119 | } | |
120 | ||
121 | return old_version; | |
122 | } | |
123 | ||
2be55a25 | 124 | #ifndef CROSS_DIRECTORY_STRUCTURE |
e46b55d0 GK |
125 | #include <sys/sysctl.h> |
126 | #include "xregex.h" | |
127 | ||
556ab512 IS |
128 | /* Determine the version of the running OS. |
129 | We only look at the first two components (ignoring the patch one) and | |
130 | report NN.MM.0 where NN is currently either 10 or 11 and MM is the OS | |
131 | minor release number. | |
132 | If we can't parse what the kernel gives us, warn the user, and do nothing. */ | |
133 | ||
9c250803 JC |
134 | static char * |
135 | darwin_find_version_from_kernel (void) | |
558c362a IS |
136 | { |
137 | char osversion[32]; | |
138 | size_t osversion_len = sizeof (osversion) - 1; | |
139 | static int osversion_name[2] = { CTL_KERN, KERN_OSRELEASE }; | |
140 | int major_vers; | |
558c362a | 141 | char * version_p; |
9c250803 | 142 | char * new_flag; |
558c362a | 143 | |
558c362a IS |
144 | if (sysctl (osversion_name, ARRAY_SIZE (osversion_name), osversion, |
145 | &osversion_len, NULL, 0) == -1) | |
146 | { | |
d67b22e7 | 147 | warning (0, "%<sysctl%> for %<kern.osversion%> failed: %m"); |
9c250803 | 148 | return NULL; |
558c362a IS |
149 | } |
150 | ||
151 | /* Try to parse the first two parts of the OS version number. Warn | |
152 | user and return if it doesn't make sense. */ | |
153 | if (! ISDIGIT (osversion[0])) | |
154 | goto parse_failed; | |
155 | major_vers = osversion[0] - '0'; | |
156 | version_p = osversion + 1; | |
157 | if (ISDIGIT (*version_p)) | |
158 | major_vers = major_vers * 10 + (*version_p++ - '0'); | |
558c362a IS |
159 | if (*version_p++ != '.') |
160 | goto parse_failed; | |
556ab512 | 161 | |
0e1d4b3b | 162 | /* Darwin20 sees a transition to macOS 11. In this, it seems that the |
add1adaa SW |
163 | mapping to macOS minor version and patch level is now always 0, 0 |
164 | (at least for macOS 11 and 12). */ | |
556ab512 | 165 | if (major_vers >= 20) |
0e1d4b3b | 166 | { |
add1adaa SW |
167 | /* Apple clang doesn't include the minor version or the patch level |
168 | in the object file, nor does it pass it to ld */ | |
169 | asprintf (&new_flag, "%d.00.00", major_vers - 9); | |
0e1d4b3b | 170 | } |
556ab512 | 171 | else if (major_vers - 4 <= 4) |
558c362a | 172 | /* On 10.4 and earlier, the old linker is used which does not |
b410cf1d IS |
173 | support three-component system versions. |
174 | FIXME: we should not assume this - a newer linker could be used. */ | |
9c250803 | 175 | asprintf (&new_flag, "10.%d", major_vers - 4); |
558c362a | 176 | else |
7d843469 SW |
177 | /* Although the newer linker supports three-component system |
178 | versions, there's no guarantee that the minor version component | |
179 | of the kernel and the system are the same. Apple's clang always | |
180 | uses 0 as the minor version: do the same. */ | |
181 | asprintf (&new_flag, "10.%d.0", major_vers - 4); | |
558c362a | 182 | |
9c250803 | 183 | return new_flag; |
558c362a IS |
184 | |
185 | parse_failed: | |
d67b22e7 | 186 | warning (0, "could not understand %<kern.osversion%> %q.*s", |
558c362a | 187 | (int) osversion_len, osversion); |
9c250803 | 188 | return NULL; |
558c362a | 189 | } |
558c362a IS |
190 | #endif |
191 | ||
e46b55d0 GK |
192 | /* When running on a Darwin system and using that system's headers and |
193 | libraries, default the -mmacosx-version-min flag to be the version | |
558c362a IS |
194 | of the system on which the compiler is running. |
195 | ||
196 | When building cross or native cross compilers, default to the OSX | |
197 | version of the target (as provided by the most specific target header | |
198 | included in tm.h). This may be overidden by setting the flag explicitly | |
199 | (or by the MACOSX_DEPLOYMENT_TARGET environment). */ | |
e46b55d0 | 200 | |
45c3fea9 IS |
201 | static const char * |
202 | darwin_default_min_version (void) | |
e46b55d0 | 203 | { |
45c3fea9 IS |
204 | /* Try to retrieve the deployment target from the environment. */ |
205 | const char *new_flag = getenv ("MACOSX_DEPLOYMENT_TARGET"); | |
e46b55d0 | 206 | |
45c3fea9 IS |
207 | /* Apparently, an empty string for MACOSX_DEPLOYMENT_TARGET means |
208 | "use the default". Or, possibly "use 10.1". We choose | |
209 | to ignore the environment variable, as if it was never set. */ | |
210 | if (new_flag == NULL || new_flag[0] == 0) | |
558c362a | 211 | #ifndef CROSS_DIRECTORY_STRUCTURE |
45c3fea9 IS |
212 | /* Try to find the version from the kernel, if we fail - we print a |
213 | message and give up. */ | |
214 | new_flag = darwin_find_version_from_kernel (); | |
558c362a | 215 | #else |
45c3fea9 IS |
216 | /* For cross-compilers, default to a minimum version determined by |
217 | the configuration. */ | |
218 | new_flag = DEF_MIN_OSX_VERSION; | |
558c362a | 219 | #endif /* CROSS_DIRECTORY_STRUCTURE */ |
e46b55d0 | 220 | |
45c3fea9 IS |
221 | if (new_flag != NULL) |
222 | { | |
351ac9c5 IS |
223 | const char *checked = validate_macosx_version_min (new_flag); |
224 | if (checked == NULL) | |
225 | { | |
d67b22e7 | 226 | warning (0, "could not understand version %qs", new_flag); |
351ac9c5 IS |
227 | return NULL; |
228 | } | |
229 | new_flag = xstrndup (checked, strlen (checked)); | |
45c3fea9 IS |
230 | } |
231 | return new_flag; | |
e46b55d0 GK |
232 | } |
233 | ||
24ec3cc9 IS |
234 | /* See if we can find the sysroot from the SDKROOT environment variable. */ |
235 | ||
236 | static const char * | |
237 | maybe_get_sysroot_from_sdkroot () | |
238 | { | |
239 | const char *maybe_sysroot = getenv ("SDKROOT"); | |
240 | ||
241 | /* We'll use the same rules as the clang driver, for compatibility. | |
242 | 1) The path must be absolute | |
243 | 2) Ignore "/", that is the default anyway and we do not want the | |
244 | sysroot semantics to be applied to it. | |
245 | 3) It must exist (actually, we'll check it's readable too). */ | |
246 | ||
247 | if (maybe_sysroot == NULL | |
248 | || *maybe_sysroot != '/' | |
249 | || strlen (maybe_sysroot) == 1 | |
250 | || access (maybe_sysroot, R_OK) == -1) | |
251 | return NULL; | |
252 | ||
253 | return xstrndup (maybe_sysroot, strlen (maybe_sysroot)); | |
254 | } | |
255 | ||
353cb291 IS |
256 | /* Handle the deduction of m32/m64 from -arch flags and the interactions |
257 | between them (i.e. try to warn a user who thinks that they have a driver | |
258 | that can produce multi-slice "FAT" outputs with more than one arch). | |
259 | Default the -mmacosx-version-min flag, which requires a system call on | |
260 | native hosts. */ | |
2be55a25 JM |
261 | |
262 | void | |
263 | darwin_driver_init (unsigned int *decoded_options_count, | |
264 | struct cl_decoded_option **decoded_options) | |
265 | { | |
266 | unsigned int i; | |
37341412 IS |
267 | bool seenX86 = false; |
268 | bool seenX86_64 = false; | |
269 | bool seenPPC = false; | |
270 | bool seenPPC64 = false; | |
271 | bool seenM32 = false; | |
272 | bool seenM64 = false; | |
273 | bool appendM32 = false; | |
274 | bool appendM64 = false; | |
45c3fea9 IS |
275 | const char *vers_string = NULL; |
276 | bool seen_version_min = false; | |
24ec3cc9 | 277 | bool seen_sysroot_p = false; |
d4943ce9 | 278 | bool noexport_p = true; |
2be55a25 JM |
279 | |
280 | for (i = 1; i < *decoded_options_count; i++) | |
281 | { | |
282 | if ((*decoded_options)[i].errors & CL_ERR_MISSING_ARG) | |
283 | continue; | |
37341412 | 284 | |
2be55a25 JM |
285 | switch ((*decoded_options)[i].opt_index) |
286 | { | |
bd837408 | 287 | case OPT_arch: |
37341412 IS |
288 | /* Support provision of a single -arch xxxx flag as a means of |
289 | specifying the sub-target/multi-lib. Translate this into -m32/64 | |
290 | as appropriate. */ | |
bd837408 | 291 | if (!strcmp ((*decoded_options)[i].arg, "i386")) |
37341412 | 292 | seenX86 = true; |
bd837408 | 293 | else if (!strcmp ((*decoded_options)[i].arg, "x86_64")) |
37341412 IS |
294 | seenX86_64 = true; |
295 | else if (!strcmp ((*decoded_options)[i].arg, "ppc")) | |
296 | seenPPC = true; | |
297 | else if (!strcmp ((*decoded_options)[i].arg, "ppc64")) | |
298 | seenPPC64 = true; | |
299 | else | |
d67b22e7 | 300 | error ("this compiler does not support %qs", |
37341412 IS |
301 | (*decoded_options)[i].arg); |
302 | /* Now we've examined it, drop the -arch arg. */ | |
303 | if (*decoded_options_count > i) { | |
304 | memmove (*decoded_options + i, | |
305 | *decoded_options + i + 1, | |
33f0ad50 | 306 | ((*decoded_options_count - i - 1) |
37341412 IS |
307 | * sizeof (struct cl_decoded_option))); |
308 | } | |
309 | --i; | |
310 | --*decoded_options_count; | |
311 | break; | |
312 | ||
313 | case OPT_m32: | |
314 | seenM32 = true; | |
315 | break; | |
316 | ||
317 | case OPT_m64: | |
318 | seenM64 = true; | |
bd837408 | 319 | break; |
bd837408 | 320 | |
45c3fea9 IS |
321 | case OPT_mmacosx_version_min_: |
322 | seen_version_min = true; | |
351ac9c5 IS |
323 | vers_string = |
324 | validate_macosx_version_min ((*decoded_options)[i].arg); | |
325 | if (vers_string == NULL) | |
7df45789 | 326 | warning (0, "%qs is not valid for %<-mmacosx-version-min%>", |
351ac9c5 IS |
327 | (*decoded_options)[i].arg); |
328 | else if (vers_string == (*decoded_options)[i].arg) | |
329 | vers_string = xstrndup ((*decoded_options)[i].arg, 32); | |
330 | /* Now we've examined it, and verified/re-written, put it to | |
331 | one side and append later. */ | |
332 | if (*decoded_options_count > i) { | |
333 | memmove (*decoded_options + i, | |
334 | *decoded_options + i + 1, | |
33f0ad50 | 335 | ((*decoded_options_count - i - 1) |
351ac9c5 IS |
336 | * sizeof (struct cl_decoded_option))); |
337 | } | |
338 | --i; | |
339 | --*decoded_options_count; | |
340 | break; | |
45c3fea9 | 341 | |
24ec3cc9 IS |
342 | case OPT__sysroot_: |
343 | case OPT_isysroot: | |
344 | seen_sysroot_p = true; | |
345 | break; | |
346 | ||
d4943ce9 IS |
347 | case OPT_Xlinker: |
348 | case OPT_Wl_: | |
349 | gcc_checking_assert ((*decoded_options)[i].arg); | |
350 | if (startswith ((*decoded_options)[i].arg, "-exported_symbol")) | |
351 | noexport_p = false; | |
352 | break; | |
353 | ||
2be55a25 JM |
354 | default: |
355 | break; | |
356 | } | |
357 | } | |
358 | ||
37341412 IS |
359 | /* Turn -arch xxxx into the appropriate -m32/-m64 flag. |
360 | If the User tried to specify multiple arch flags (which is possible with | |
361 | some Darwin compilers) warn that this mode is not supported by this | |
d67b22e7 IS |
362 | compiler. We take arch specifiers that agree with the default multilib |
363 | as the first choice and reject others. */ | |
37341412 IS |
364 | /* TODO: determine if these warnings would better be errors. */ |
365 | #if DARWIN_X86 | |
366 | if (seenPPC || seenPPC64) | |
d67b22e7 IS |
367 | warning (0, "this compiler does not support PowerPC" |
368 | " (%<-arch%> option ignored)"); | |
37341412 IS |
369 | if (seenX86) |
370 | { | |
371 | if (seenX86_64 || seenM64) | |
d67b22e7 IS |
372 | { |
373 | const char *op = (seenX86_64? "-arch x86_64": "-m64"); | |
374 | warning (0, "%qs conflicts with %<-arch i386%> (%qs ignored)", | |
375 | op, op); | |
376 | } | |
377 | if (! seenM32) /* Add -m32 if the User didn't. */ | |
37341412 IS |
378 | appendM32 = true; |
379 | } | |
380 | else if (seenX86_64) | |
381 | { | |
d67b22e7 IS |
382 | if (seenM32) |
383 | warning (0, "%<-m32%> conflicts with %<-arch x86_64%>" | |
384 | " (%<-m32%> ignored)"); | |
385 | if (! seenM64) /* Add -m64 if the User didn't. */ | |
37341412 IS |
386 | appendM64 = true; |
387 | } | |
388 | #elif DARWIN_PPC | |
389 | if (seenX86 || seenX86_64) | |
d67b22e7 IS |
390 | warning (0, "this compiler does not support x86" |
391 | " (%<-arch%> option ignored)"); | |
37341412 IS |
392 | if (seenPPC) |
393 | { | |
394 | if (seenPPC64 || seenM64) | |
d67b22e7 IS |
395 | { |
396 | const char *op = (seenPPC64? "-arch ppc64": "-m64"); | |
397 | warning (0, "%qs conflicts with %<-arch ppc%> (%qs ignored)", | |
398 | op, op); | |
399 | } | |
400 | if (! seenM32) /* Add -m32 if the User didn't. */ | |
37341412 IS |
401 | appendM32 = true; |
402 | } | |
403 | else if (seenPPC64) | |
404 | { | |
d67b22e7 IS |
405 | if (seenM32) |
406 | warning (0, "%<-m32%> conflicts with %<-arch ppc64%>" | |
407 | " (%<-m32%> ignored)"); | |
408 | if (! seenM64) /* Add -m64 if the User didn't. */ | |
37341412 IS |
409 | appendM64 = true; |
410 | } | |
411 | #endif | |
412 | ||
ff56eea2 IS |
413 | /* If there is nothing else on the command line, do not add sysroot etc. */ |
414 | if (*decoded_options_count <= 1) | |
415 | return; | |
416 | ||
37341412 IS |
417 | if (appendM32 || appendM64) |
418 | { | |
419 | ++*decoded_options_count; | |
420 | *decoded_options = XRESIZEVEC (struct cl_decoded_option, | |
421 | *decoded_options, | |
422 | *decoded_options_count); | |
423 | generate_option (appendM32 ? OPT_m32 : OPT_m64, NULL, 1, CL_DRIVER, | |
424 | &(*decoded_options)[*decoded_options_count - 1]); | |
425 | } | |
426 | ||
ff56eea2 | 427 | if (!seen_sysroot_p) |
24ec3cc9 IS |
428 | { |
429 | /* We will pick up an SDKROOT if we didn't specify a sysroot and treat | |
430 | it as overriding any configure-time --with-sysroot. */ | |
431 | const char *sdkroot = maybe_get_sysroot_from_sdkroot (); | |
432 | if (sdkroot) | |
433 | { | |
434 | ++*decoded_options_count; | |
435 | *decoded_options = XRESIZEVEC (struct cl_decoded_option, | |
436 | *decoded_options, | |
437 | *decoded_options_count); | |
438 | generate_option (OPT__sysroot_, sdkroot, 1, CL_DRIVER, | |
439 | &(*decoded_options)[*decoded_options_count - 1]); | |
440 | } | |
441 | } | |
442 | ||
45c3fea9 IS |
443 | /* We will need to know the OS X version we're trying to build for here |
444 | so that we can figure out the mechanism and source for the sysroot to | |
445 | be used. */ | |
ff56eea2 | 446 | if (!seen_version_min) |
351ac9c5 IS |
447 | /* Not set by the User, try to figure it out. */ |
448 | vers_string = darwin_default_min_version (); | |
449 | ||
450 | /* Create and push a cleaned up version, plus the major version for | |
451 | assemblers and other cases that need it. */ | |
b410cf1d IS |
452 | if (vers_string != NULL) |
453 | { | |
351ac9c5 IS |
454 | ++*decoded_options_count; |
455 | *decoded_options = XRESIZEVEC (struct cl_decoded_option, | |
456 | *decoded_options, | |
457 | *decoded_options_count); | |
458 | generate_option (OPT_mmacosx_version_min_, vers_string, 1, CL_DRIVER, | |
459 | &(*decoded_options)[*decoded_options_count - 1]); | |
460 | ||
b410cf1d | 461 | char *asm_major = NULL; |
ce005f35 | 462 | const char *first_period = strchr(vers_string, '.'); |
b410cf1d IS |
463 | if (first_period != NULL) |
464 | { | |
ce005f35 | 465 | const char *second_period = strchr(first_period+1, '.'); |
b410cf1d IS |
466 | if (second_period != NULL) |
467 | asm_major = xstrndup (vers_string, second_period-vers_string); | |
468 | else | |
469 | asm_major = xstrdup (vers_string); | |
470 | } | |
471 | /* Else we appear to have a weird macosx version with no major number. | |
472 | Punt on this for now. */ | |
473 | if (asm_major != NULL) | |
474 | { | |
475 | ++*decoded_options_count; | |
476 | *decoded_options = XRESIZEVEC (struct cl_decoded_option, | |
477 | *decoded_options, | |
478 | *decoded_options_count); | |
479 | generate_option (OPT_asm_macosx_version_min_, asm_major, 1, CL_DRIVER, | |
480 | &(*decoded_options)[*decoded_options_count - 1]); | |
481 | } | |
482 | } | |
d4943ce9 IS |
483 | |
484 | if (noexport_p) | |
485 | { | |
486 | ++*decoded_options_count; | |
487 | *decoded_options = XRESIZEVEC (struct cl_decoded_option, | |
488 | *decoded_options, | |
489 | *decoded_options_count); | |
490 | generate_option (OPT_nodefaultexport, NULL, 1, CL_DRIVER, | |
491 | &(*decoded_options)[*decoded_options_count - 1]); | |
492 | } | |
2be55a25 | 493 | } |