Skip to content

Darwin: Implement sysroot flag#2077

Open
etcwilde wants to merge 1 commit into
swiftlang:mainfrom
etcwilde:ewilde/darwin-sysroot
Open

Darwin: Implement sysroot flag#2077
etcwilde wants to merge 1 commit into
swiftlang:mainfrom
etcwilde:ewilde/darwin-sysroot

Conversation

@etcwilde
Copy link
Copy Markdown
Member

@etcwilde etcwilde commented Feb 5, 2026

The sysroot flag is designed to control where the compiler looks for the C runtimes and headers that one would normally find in an SDK for a system. Apple SDKs contain both the Swift runtimes and the other platform libraries, so under normal circumstances, this flag isn't needed.

It is useful, however, for consistency and testing purposes. As we're bringing up the split runtime/compiler build on the main repository, having the ability to tell the compiler under test to use the just-built Swift runtimes, but also where to find the existing system libraries (libsystem.tbd, e.g.) is incredibly useful.

If the sysroot flag is set, it take precedence for the clang importer and clang-linker.
If the sysroot flag is left unset but an sdk flag is set, the compiler should implicitly fall back on the sdk flag, which is the current behavior.
Finally, if neither are set, it falls back on whatever default the frontend uses, and no special flags are forwarded.

@etcwilde
Copy link
Copy Markdown
Member Author

etcwilde commented Feb 5, 2026

@swift-ci please test

Comment on lines +224 to +227
if let sysroot = parsedOptions.getLastArgument(.sysroot)?.asSingle {
commandLine.appendFlag("--sysroot")
try commandLine.appendPath(VirtualPath(path: sysroot))
} else if let sdkPath = targetInfo.sdkPath?.path {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be better expressed as:

if let sysroot = parsedOptions.getLastArgument(.sysroot)?.asSingle ?? targetInfo.sdkPath?.path {
  commandLine.appendFlag("--sysroot")
  try commandLine.appendPath(VirtualPath(path: sysroot))
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the sdkPath is a VirtualPath.Handle, not a string. I don't think we can merge these like this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to refactor constructing the virtual path for the sysroot though?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the next PR, refactors this to store both as TextualVirtualPath objects in the driver so that we can merge these clauses: etcwilde@fe2bd92

"-target", "x86_64-apple-macosx10.14",
"-sdk", sdk.pathString,
"-sysroot", customSysroot.pathString,
"foo.swift"], env: envVars)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to prefer test.swift or something other than "foo" given its unfortunate history.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like I missed something while I was out. When was foo disallowed, and why?

@etcwilde etcwilde force-pushed the ewilde/darwin-sysroot branch from 535db50 to c1ce976 Compare February 5, 2026 23:30
@etcwilde
Copy link
Copy Markdown
Member Author

etcwilde commented Feb 6, 2026

The test failure is:

<big wall of text> does not contain [SwiftDriver.Job.ArgTemplate.flag("-fmodule-file=SAL=C:\\Users\\ContainerAdministrator\\AppData\\Local\\clang\\ModuleCache\\SAL-C0NRIAP58RED50SQXNKMXXKN.pcm")]

in one of the explicit module build tests. I don't believe that this change is related to that failure, but it looks like things haven't been testing or merging cleanly for a while here so I can't really make out what I should expect.

@cachemeifyoucan
Copy link
Copy Markdown
Contributor

You need to run swift-ci test and that is the only blocking PR testing.

That failure is fixed in #2074 and I just merged. There might still be other issue.

@compnerd
Copy link
Copy Markdown
Member

compnerd commented Feb 6, 2026

@swift-ci please test

@etcwilde
Copy link
Copy Markdown
Member Author

etcwilde commented Feb 6, 2026

@swift-ci please test Windows

The sysroot flag is designed to control where the compiler looks for the
C runtimes and headers that one would normally find in an SDK for a
system. Apple SDKs contain both the Swift runtimes and the other
platform libraries, so under normal circumstances, this flag isn't
needed.

It is useful, however, for consistency and testing purposes. As we're
bringing up the split runtime/compiler build on the main repository,
having the ability to tell the compiler under test to use the just-built
Swift runtimes, but also where to find the existing system libraries
(libsystem.tbd, e.g.) is incredibly useful.

If the sysroot flag is set, it take precedence for the clang importer
and clang-linker.
If the sysroot flag is left unset but an sdk flag is set, the compiler
should implicitly fall back on the sdk flag, which is the current
behavior.
Finally, if neither are set, it falls back on whatever default the
frontend uses, and no special flags are forwarded.
@etcwilde etcwilde force-pushed the ewilde/darwin-sysroot branch from c1ce976 to 0e7acd4 Compare February 12, 2026 17:34
@etcwilde
Copy link
Copy Markdown
Member Author

@swift-ci please test

@etcwilde
Copy link
Copy Markdown
Member Author

@swift-ci please test Windows

@finagolfin
Copy link
Copy Markdown
Member

Apple SDKs contain both the Swift runtimes and the other platform libraries, so under normal circumstances, this flag isn't needed.

It is useful, however, for consistency and testing purposes.

Not just for that, I took a look at the macOS CI after we discussed this new flag on the Build and Packaging workgroup call a couple weeks ago and I see that the -sdk and -resource-dir flags are used extensively there to bring up the Swift stdlib on macOS, just like it would be for iOS and so on. That doesn't even include all the compiler validation suite tests that use the -sdk flag to find the same Darwin C/C++ headers and libraries that you would like to use this new -sysroot flag for instead.

If the plan is to move that Darwin build to this -sysroot flag and a -sdk flag with changed behavior, you really should run this pull through a stdlib build and the compiler validation suite using this new -sysroot flag instead. Otherwise, you won't even know of holes that remain in this -sdk/-sysroot implementation until way down the line, similar to the holes I have listed for non-Darwin platforms in swiftlang/swift#79657.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants