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.
5 This file is part of GCC.
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)
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.
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/>. */
22 #include "libiberty.h"
24 #include "coretypes.h"
27 #include "diagnostic-core.h"
29 /* Validate a version string (either given on the command line or, perhaps
30 as MACOSX_DEPLOYMENT_TARGET).
32 The specs %version-compare() function doesn't accept leading '0' on
33 numbers so strip them out. Do sanity checking here too.
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
39 * NULL means we didn't like what we saw.
43 validate_macosx_version_min (const char *version_str
)
46 unsigned long major
, minor
, tiny
= 0;
48 const char *old_version
= version_str
;
49 bool need_rewrite
= false;
51 version_len
= strlen (version_str
);
52 if (version_len
< 4) /* The minimum would be 10.x */
55 /* Version string must consist of digits and periods only. */
56 if (strspn (version_str
, "0123456789.") != version_len
)
59 if (!ISDIGIT (version_str
[0]) || !ISDIGIT (version_str
[version_len
- 1]))
62 if (version_str
[0] == '0')
65 major
= strtoul (version_str
, &end
, 10);
66 version_str
= end
+ ((*end
== '.') ? 1 : 0);
68 if (major
!= 10) /* So far .. all MacOS 10 ... */
71 /* Version string components must be present and numeric. */
72 if (!ISDIGIT (version_str
[0]))
75 /* If we have one or more leading zeros on a component, then rewrite the
77 if (version_str
[0] == '0' && version_str
[1] != '\0'
78 && version_str
[1] != '.')
81 minor
= strtoul (version_str
, &end
, 10);
82 version_str
= end
+ ((*end
== '.') ? 1 : 0);
86 /* If 'tiny' is present it must be numeric. */
87 if (*end
!= '\0' && !ISDIGIT (version_str
[0]))
90 /* If we have one or more leading zeros on a component, then rewrite the
92 if (*end
!= '\0' && version_str
[0] == '0'
93 && version_str
[1] != '\0')
96 tiny
= strtoul (version_str
, &end
, 10);
100 /* Version string must contain no more than three tokens. */
107 asprintf (&new_version
, "10.%lu.%lu", minor
, tiny
);
114 #ifndef CROSS_DIRECTORY_STRUCTURE
115 #include <sys/sysctl.h>
119 darwin_find_version_from_kernel (void)
122 size_t osversion_len
= sizeof (osversion
) - 1;
123 static int osversion_name
[2] = { CTL_KERN
, KERN_OSRELEASE
};
128 /* Determine the version of the running OS. If we can't, warn user,
130 if (sysctl (osversion_name
, ARRAY_SIZE (osversion_name
), osversion
,
131 &osversion_len
, NULL
, 0) == -1)
133 warning (0, "sysctl for kern.osversion failed: %m");
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]))
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
++ != '.')
148 /* The major kernel version number is 4 plus the second OS version
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);
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);
165 warning (0, "couldn%'t understand kern.osversion %q.*s",
166 (int) osversion_len
, osversion
);
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.
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). */
181 darwin_default_min_version (void)
183 /* Try to retrieve the deployment target from the environment. */
184 const char *new_flag
= getenv ("MACOSX_DEPLOYMENT_TARGET");
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 ();
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 */
200 if (new_flag
!= NULL
)
202 const char *checked
= validate_macosx_version_min (new_flag
);
205 warning (0, "couldn%'t understand version %s", new_flag
);
208 new_flag
= xstrndup (checked
, strlen (checked
));
213 /* See if we can find the sysroot from the SDKROOT environment variable. */
216 maybe_get_sysroot_from_sdkroot ()
218 const char *maybe_sysroot
= getenv ("SDKROOT");
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). */
226 if (maybe_sysroot
== NULL
227 || *maybe_sysroot
!= '/'
228 || strlen (maybe_sysroot
) == 1
229 || access (maybe_sysroot
, R_OK
) == -1)
232 return xstrndup (maybe_sysroot
, strlen (maybe_sysroot
));
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. */
245 darwin_driver_init (unsigned int *decoded_options_count
,
246 struct cl_decoded_option
**decoded_options
)
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;
261 for (i
= 1; i
< *decoded_options_count
; i
++)
263 if ((*decoded_options
)[i
].errors
& CL_ERR_MISSING_ARG
)
266 switch ((*decoded_options
)[i
].opt_index
)
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
272 if (!strcmp ((*decoded_options
)[i
].arg
, "i386"))
274 else if (!strcmp ((*decoded_options
)[i
].arg
, "x86_64"))
276 else if (!strcmp ((*decoded_options
)[i
].arg
, "ppc"))
278 else if (!strcmp ((*decoded_options
)[i
].arg
, "ppc64"))
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
)));
291 --*decoded_options_count
;
304 ++*decoded_options_count
;
305 *decoded_options
= XRESIZEVEC (struct cl_decoded_option
,
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
]);
319 case OPT_mmacosx_version_min_
:
320 seen_version_min
= true;
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
)));
337 --*decoded_options_count
;
342 seen_sysroot_p
= true;
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. */
357 if (seenPPC
|| seenPPC64
)
358 warning (0, "this compiler does not support PowerPC (arch flags ignored)");
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. */
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. */
376 if (seenX86
|| seenX86_64
)
377 warning (0, "this compiler does not support X86 (arch flags ignored)");
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. */
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. */
396 if (appendM32
|| appendM64
)
398 ++*decoded_options_count
;
399 *decoded_options
= XRESIZEVEC (struct cl_decoded_option
,
401 *decoded_options_count
);
402 generate_option (appendM32
? OPT_m32
: OPT_m64
, NULL
, 1, CL_DRIVER
,
403 &(*decoded_options
)[*decoded_options_count
- 1]);
406 if (! seen_sysroot_p
)
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 ();
413 ++*decoded_options_count
;
414 *decoded_options
= XRESIZEVEC (struct cl_decoded_option
,
416 *decoded_options_count
);
417 generate_option (OPT__sysroot_
, sdkroot
, 1, CL_DRIVER
,
418 &(*decoded_options
)[*decoded_options_count
- 1]);
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
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 ();
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
)
433 ++*decoded_options_count
;
434 *decoded_options
= XRESIZEVEC (struct cl_decoded_option
,
436 *decoded_options_count
);
437 generate_option (OPT_mmacosx_version_min_
, vers_string
, 1, CL_DRIVER
,
438 &(*decoded_options
)[*decoded_options_count
- 1]);
440 char *asm_major
= NULL
;
441 const char *first_period
= strchr(vers_string
, '.');
442 if (first_period
!= NULL
)
444 const char *second_period
= strchr(first_period
+1, '.');
445 if (second_period
!= NULL
)
446 asm_major
= xstrndup (vers_string
, second_period
-vers_string
);
448 asm_major
= xstrdup (vers_string
);
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
)
454 ++*decoded_options_count
;
455 *decoded_options
= XRESIZEVEC (struct cl_decoded_option
,
457 *decoded_options_count
);
458 generate_option (OPT_asm_macosx_version_min_
, asm_major
, 1, CL_DRIVER
,
459 &(*decoded_options
)[*decoded_options_count
- 1]);