Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[staging-next] swift: bootstrap using system stdlib #211908

Merged
merged 2 commits into from
Jan 28, 2023

Conversation

stephank
Copy link
Contributor

@stephank stephank commented Jan 21, 2023

Description of changes

After the merge of #189977, there were new failures on staging.

db57f8c is a straight revert of #207123. As discussed there, we determined the change did not have any effect, and in fact broke the Swift build, because there are now 2 copies of xpc in the search path.

927b2ce is a little meatier.

There are multiple bootstrapping modes for the Swift compiler. The official upstream Linux builds use -DBOOTSTRAPPING_MODE=BOOTSTRAPPING, which bootstraps compiler + stdlib, while the official Darwin builds use -DBOOTSTRAPPING_MODE=BOOTSTRAPPING-WITH-HOSTLIBS, which bootstraps compiler only. I had BOOTSTRAPPING working on Darwin before, but it myseriously broke.

The failure with BOOTSTRAPPING was that both the bootstrap libswiftCore.dylib and the system /usr/lib/swift/libswiftCore.dylib were being loaded at run-time, causing lots of warnings about duplicate ObjC classes, and eventually a segfault in the compiler itself. The system copy is not something we can easily eliminate; it's part of the stable macOS ABI, much like a system framework.

I've not been able to determine an exact cause why this started failing now, but regardless, we already had to patch the Swift build to make BOOTSTRAPPING work. It seems like it may not be supported on Darwin, so this change makes our build match upstream by using BOOTSTRAPPING-WITH-HOSTLIBS, which is probably for the better.

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.05 Release Notes (or backporting 22.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

@ofborg ofborg bot requested review from trepetti, Trundle, dtzWill and dduan January 21, 2023 12:10
@stephank stephank changed the base branch from staging to staging-next January 21, 2023 21:07
@stephank stephank changed the title swift: bootstrap using system stdlib [staging-next] swift: bootstrap using system stdlib Jan 21, 2023
@stephank
Copy link
Contributor Author

A staging-next PR was opened (#211923) so I'm retargeting that.

Copy link
Member

@SuperSandro2000 SuperSandro2000 left a comment

Choose a reason for hiding this comment

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

only styling from me, macos sdk is out of my knowledge


installPhase = ''
mkdir -p $out/lib/swift
cp -r \
Copy link
Member

Choose a reason for hiding this comment

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

symlinking does not work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is modelled after libs in apple_sdk.nix. I think the idea is that we try to avoid users having to download the full SDK.

Comment on lines 413 to 423
${lib.optionalString stdenv.isDarwin ''
# Add appleSwiftCore to the search paths. We can't simply add it to
# buildInputs, because it is potentially an older stdlib than the one we're
# building. We have to remove it again after the main Swift build, or later
# build steps may fail. (Specific case: Concurrency backdeploy uses the
# Sendable protocol, which appears to not be present in the macOS 11 SDK.)
OLD_NIX_SWIFTFLAGS_COMPILE="$NIX_SWIFTFLAGS_COMPILE"
OLD_NIX_LDFLAGS="$NIX_LDFLAGS"
export NIX_SWIFTFLAGS_COMPILE+=" -I ${appleSwiftCore}/lib/swift"
export NIX_LDFLAGS+=" -L ${appleSwiftCore}/lib/swift"
''}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
${lib.optionalString stdenv.isDarwin ''
# Add appleSwiftCore to the search paths. We can't simply add it to
# buildInputs, because it is potentially an older stdlib than the one we're
# building. We have to remove it again after the main Swift build, or later
# build steps may fail. (Specific case: Concurrency backdeploy uses the
# Sendable protocol, which appears to not be present in the macOS 11 SDK.)
OLD_NIX_SWIFTFLAGS_COMPILE="$NIX_SWIFTFLAGS_COMPILE"
OLD_NIX_LDFLAGS="$NIX_LDFLAGS"
export NIX_SWIFTFLAGS_COMPILE+=" -I ${appleSwiftCore}/lib/swift"
export NIX_LDFLAGS+=" -L ${appleSwiftCore}/lib/swift"
''}
'' + lib.optionalString stdenv.isDarwin ''
# Add appleSwiftCore to the search paths. We can't simply add it to
# buildInputs, because it is potentially an older stdlib than the one we're
# building. We have to remove it again after the main Swift build, or later
# build steps may fail. (Specific case: Concurrency backdeploy uses the
# Sendable protocol, which appears to not be present in the macOS 11 SDK.)
OLD_NIX_SWIFTFLAGS_COMPILE="$NIX_SWIFTFLAGS_COMPILE"
OLD_NIX_LDFLAGS="$NIX_LDFLAGS"
export NIX_SWIFTFLAGS_COMPILE+=" -I ${appleSwiftCore}/lib/swift"
export NIX_LDFLAGS+=" -L ${appleSwiftCore}/lib/swift"
'' + ''

# - Experimental features are OFF by default in CMake, but some are
# required to build the stdlib.
# - SWIFT_STDLIB_ENABLE_OBJC_INTEROP is set explicitely because its check
# is buggy. (Uses SWIFT_HOST_VARIANT_SDK before initialized.)
# Fixed in: https://github.com/apple/swift/commit/84083afef1de5931904d5c815d53856cdb3fb232
cmakeFlags="
-GNinja
-DBOOTSTRAPPING_MODE=BOOTSTRAPPING
-DBOOTSTRAPPING_MODE=${if stdenv.isDarwin then "BOOTSTRAPPING-WITH-HOSTLIBS" else "BOOTSTRAPPING"}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
-DBOOTSTRAPPING_MODE=${if stdenv.isDarwin then "BOOTSTRAPPING-WITH-HOSTLIBS" else "BOOTSTRAPPING"}
-DBOOTSTRAPPING_MODE=BOOTSTRAPPING${lib.optionalStrin stdenv.isDarwin "-WITH-HOSTLIBS"}

Comment on lines 450 to 455
${lib.optionalString stdenv.isDarwin ''
# Restore search paths to remove appleSwiftCore.
export NIX_SWIFTFLAGS_COMPILE="$OLD_NIX_SWIFTFLAGS_COMPILE"
export NIX_LDFLAGS="$OLD_NIX_LDFLAGS"
''}

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
${lib.optionalString stdenv.isDarwin ''
# Restore search paths to remove appleSwiftCore.
export NIX_SWIFTFLAGS_COMPILE="$OLD_NIX_SWIFTFLAGS_COMPILE"
export NIX_LDFLAGS="$OLD_NIX_LDFLAGS"
''}
'' + lib.optionalString stdenv.isDarwin ''
# Restore search paths to remove appleSwiftCore.
export NIX_SWIFTFLAGS_COMPILE="$OLD_NIX_SWIFTFLAGS_COMPILE"
export NIX_LDFLAGS="$OLD_NIX_LDFLAGS"
'' + ''

This reverts commit 511f21d.

In apple_sdk_11_0, the xpc package contains only headers that are
already part of libsystem, so this change did nothing.

For Swift and `-fmodules`, this actually caused an error, because
there was now a duplicate module in the search path.
`BOOTSTRAPPING-WITH-HOSTLIBS` is also what official builds use on
Darwin. It's unclear why `BOOTSTRAPPING` was working before, but it is
probably not supported on Darwin to begin with. It was now causing
crashes because of mixing of two copies of stdlib at run-time.
@vcunat vcunat added the 6.topic: darwin Running or building packages on Darwin label Jan 22, 2023
@domenkozar domenkozar merged commit cfb80ea into NixOS:staging-next Jan 28, 2023
@stephank stephank deleted the fix/swift-darwin branch January 29, 2023 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: darwin Running or building packages on Darwin 10.rebuild-darwin: 501+ 10.rebuild-darwin: 2501-5000 10.rebuild-linux: 11-100 11.by: package-maintainer This PR was created by the maintainer of the package it changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants