apple / swift-atomics

Low-level atomic operations for Swift
Apache License 2.0
1.06k stars 50 forks source link

Cannot build when C++ interoperability is used #88

Closed arguiot closed 1 year ago

arguiot commented 1 year ago

Issue description

When attempting to build the apple/swift-atomics library with C++ interoperability enabled, several errors are encountered in the _AtomicsShims.h header file. These errors prevent the library from being built successfully.

Steps to reproduce

  1. Use the apple/swift-atomics package in a target using SwiftPM.
  2. Configure the build with C++ interoperability enabled.
  3. Attempt to build the library.

Expected behavior

The library should build successfully with C++ interoperability enabled. The library should correctly detect that C++ inter is enabled instead of blindly assuming that interoperability will be C/Objective-C

Actual behavior

Several errors are encountered in the _AtomicsShims.h header file, preventing the library from being built successfully.

Error messages

swift-atomics/Sources/_AtomicsShims/include/_AtomicsShims.h:227:1: Cannot initialize a member subobject of type '_Atomic(_sa_dword)' with an rvalue of type '_Atomic(_sa_dword)'

swift-atomics/Sources/_AtomicsShims/include/_AtomicsShims.h:227:1: No viable conversion from returned value of type '_Atomic(_sa_dword)' to function return type '_sa_dword'

Could not build Objective-C module '_AtomicsShims'

Environment

lorentey commented 1 year ago

Configure the build with C++ interoperability enabled.

How are you doing this?

arguiot commented 1 year ago

In Xcode 15, just go to « Build Settings » and search for interoperability. Just switch to C++/Objective C++.

arguiot commented 1 year ago

Another way is to go in Package.swift and use .Cxx for interoperability.

lorentey commented 1 year ago

In other words, for an Xcode project that depends on this package, set the SWIFT_OBJC_INTEROP_MODE build setting to objcxx. If the dependent project is a package, add .interoperabilityMode(.Cxx) to the target's swiftSettings argument.

These are new in the current prerelease Swift 5.9 builds.

lorentey commented 1 year ago

Given that this can only affect Swift 5.9+, the option I like the most is to push the package forward and replace C atomics with native builtins on such toolchains. (Which are now accessible to packages thanks to an "experimental" language feature.)

lorentey commented 1 year ago

Thanks for reporting this! It is now resolved on the main branch of this repository. If you find that the problem is still reproducible on the current main branch, please reopen this issue.

To verify the fix, you'll need to temporarily update to a commit-based (or branch-based) dependency spec for swift-atomics.

The problem and its solution are both specific to Swift 5.9, so we'll need to wait until 5.9 ships before tagging a new swift-atomics release that includes the fix.