Skip to content

Commit 24ec3cc

Browse files
committed
[Darwin] Pick up SDKROOT as the sysroot fallback.
For compatibility with xcrun and the behaviour of the clang driver, make use of the setting of the SDKROOT environment variable when it is available. This applies to both finding headers and libraries (i.e. it is also passed to ld64). Priority: 1. User's command-line specified --sysroot= or -isysroot. 2. The SDKROOT variable when set, and validated. 3. Any sysroot provided by --with-sysroot= configuration parameter. SDKROOT is checked thus: 1. Presence. 2. That it starts with "/" (i.e. 'absolute'). 3. That it is not "/" only (since that's the default). 4. That it is readable by the process executing the driver. This is pretty much the same rule set as used by the clang driver. NOTE: (3) might turn out to be overly restrictive in the case that we have configured with --with-sysroot= and then we want to run on a system with an installation of the headers/libraries in /. We can revisit this if that turns out to be an important use-case. So one can do: xcrun --sdk macosx /path/to/gcc .... and that provides the SDK path as the sysroot to GCC as expected. CAVEAT: An unfortunate effect of the fact that "gcc" (and "g++") are executables in the Xcode installation, which are found ahead of any such named in the $PATH: PATH=/path/to/gcc/install:$PATH xcrun --sdk macosx gcc .... does *not* work, instead that executes the clang from the xcode/commmand line tools installation. PATH=/path/to/gcc/install:$PATH xcrun --sdk macosx x64_64-apple-darwinXX-gcc ... does work as expected, however. gcc/ChangeLog: 2019-10-03 Iain Sandoe <[email protected]> PR target/87243 * config/darwin-driver.c (maybe_get_sysroot_from_sdkroot): New. (darwin_driver_init): Use the sysroot provided by SDKROOT when that is available and the user has not set one on the command line. From-SVN: r276530
1 parent cfcf355 commit 24ec3cc

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

gcc/ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2019-10-03 Iain Sandoe <[email protected]>
2+
3+
PR target/87243
4+
* config/darwin-driver.c (maybe_get_sysroot_from_sdkroot): New.
5+
(darwin_driver_init): Use the sysroot provided by SDKROOT when that
6+
is available and the user has not set one on the command line.
7+
18
2019-10-03 Dragan Mladjenovic <[email protected]>
29

310
PR target/91769

gcc/config/darwin-driver.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,28 @@ darwin_default_min_version (void)
210210
return new_flag;
211211
}
212212

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+
213235
/* Translate -filelist and -framework options in *DECODED_OPTIONS
214236
(size *DECODED_OPTIONS_COUNT) to use -Xlinker so that they are
215237
considered to be linker inputs in the case that no other inputs are
@@ -234,6 +256,7 @@ darwin_driver_init (unsigned int *decoded_options_count,
234256
bool appendM64 = false;
235257
const char *vers_string = NULL;
236258
bool seen_version_min = false;
259+
bool seen_sysroot_p = false;
237260

238261
for (i = 1; i < *decoded_options_count; i++)
239262
{
@@ -314,6 +337,11 @@ darwin_driver_init (unsigned int *decoded_options_count,
314337
--*decoded_options_count;
315338
break;
316339

340+
case OPT__sysroot_:
341+
case OPT_isysroot:
342+
seen_sysroot_p = true;
343+
break;
344+
317345
default:
318346
break;
319347
}
@@ -375,6 +403,22 @@ darwin_driver_init (unsigned int *decoded_options_count,
375403
&(*decoded_options)[*decoded_options_count - 1]);
376404
}
377405

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+
378422
/* We will need to know the OS X version we're trying to build for here
379423
so that we can figure out the mechanism and source for the sysroot to
380424
be used. */

0 commit comments

Comments
 (0)