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