Commit Graph

41 Commits

Author SHA1 Message Date
Egor Zhdan
10ab242b64 [cxx-interop] Do not install binary .swiftmodule files for the overlays
This fixes a deserialization failure in the compiler that occurred while loading the CxxStdlib overlay module:
```
Cross-reference to module 'Swift'
... Optional
... some
... with type <τ_0_0 where τ_0_0 : ~Copyable, τ_0_0 : ~Escapable> (Optional<τ_0_0>.Type) -> (τ_0_0) -> Optional<τ_0_0>
```

This was happening because the overlays were built against a different version of the Swift stdlib than is being used. The compiler is able to rebuild the Cxx and CxxStdlib modules from their textual interfaces. Let's use that feature unconditionally in production toolchains to avoid this kind of binary incompatibilities.

rdar://150416863
(cherry picked from commit 16b280862a)
2025-05-12 19:30:15 +01:00
Doug Gregor
06c0efbce6 [stdlib] Reinstate AllowUnsafeAttribute experimental feature on libraries
We need this so that older compilers can handle the .swiftinterface
files we generate. It's unnecessary for newer compilers and can be
removed later.

Fixes rdar://148529962.

(cherry picked from commit 0100104ff7)
2025-04-04 15:18:25 -07:00
Doug Gregor
89d0277a35 Enable strict memory safety in the Cxx/CxxStdlib modules 2025-02-26 14:28:30 -08:00
Allan Shortlidge
eb75c0cd23 CMake: Bring back -enable-experimental-feature Span.
Reenable it temporarily (until after 6.2 is released) for compatibility with
older compilers.
2025-02-16 07:15:44 -08:00
Allan Shortlidge
a0ac6c74e3 Tests: Stop referring to the Span feature since it has been removed. 2025-02-15 17:23:14 -08:00
Gabor Horvath
b6cc118f71 [cxx-interop] Explicit conversions between Swift and C++ spans
A first step towards creating safe overloads for C++ APIs using span
(rdar://139074571).

Note that we need to mark span as owned because it the libc++
implementation was mistakenly recognized as owned and might now rely on
span methods like `data` being renamed as `__dataUnsafe`. We will change
it under a new interop version. But for the time being, we want
consistent behavior across stdlib versions.
2024-12-13 16:01:02 +00:00
Egor Zhdan
22141b1118 [cxx-interop] Lower iOS deployment target for Cxx and CxxStdlib
The iOS/watchOS/tvOS deployment targets for Cxx and CxxStdlib binaries got unintentionally bumped in b87b263.

This reverts the deployment targets to the older versions.

rdar://140823785 / resolves https://github.com/swiftlang/swift/issues/77909
2024-12-03 16:39:59 +00:00
Egor Zhdan
703e479135 [cxx-interop] Make Cxx and CxxStdlib libraries resilient
This enables library evolution for the two libraries that form the Swift overlay for the C++ standard library.

rdar://129169673
2024-11-12 17:22:03 +00:00
Egor Zhdan
b0a3bb10f0 [cxx-interop] Build Cxx and CxxStdlib modules for macCatalyst
This enables building the C++ stdlib overlays (`Cxx.swiftmodule` and `CxxStdlib.swiftmodule`) for the Mac Catalyst target.

rdar://126938813
2024-07-05 19:35:29 +01:00
susmonteiro
e86099c26d [cxx-interop] Implements constructor for std::span from UnsafeBufferPointer
Tests: init span from UnsafeBufferPointer, for loop, map, filter, init Array from span, span of strings
2024-07-01 16:07:28 +01:00
Egor Zhdan
e144f607d7 [cxx-interop] Migrate to overlay the new compiler flag
`-enable-experimental-cxx-interop` is deprecated, the new flag for enabling C++ interop is `-cxx-interoperability-mode=enabled`.

NFC intended.
2023-11-28 20:30:12 +00:00
Egor Zhdan
00cdb66d01 [cxx-interop] Lower macOS deployment target version for the Cxx module
The Cxx module does not rely on any modern OS features. It can be deployed to older macOS versions.

rdar://117699474
2023-10-30 19:22:02 +00:00
Egor Zhdan
f0be52accd [cxx-interop] Add conversion to Bool for types that define operator bool()
C++ `operator bool()` is currently imported into Swift as `__convertToBool()`, which shouldn't be used by clients directly.

This adds a new protocol into the C++ stdlib overlay: `CxxConvertibleToBool`, along with an intitializer for `Swift.Bool` taking an instance of `CxxConvertibleToBool`.

rdar://115074954
2023-09-18 14:54:45 +01:00
Saleem Abdulrasool
ba9174df18 Cxx: build Cxx module statically on all platforms
This adjusts Cxx to be built statically on all platforms including
Windows.  The static library support is sufficient to support this
module linking statically on Windows.
2023-08-28 09:27:34 -07:00
Saleem Abdulrasool
dff40e9399 Cxx: mark as fragile
This library is still fragile as C++ Interop is in preview. Mark it as
fragile to match cxxStdlib.
2023-08-25 11:31:36 -07:00
Egor Zhdan
4acf9c8cf4 [cxx-interop] Add CxxVector protocol
This makes it possible to initialize `std::vector` from a Swift Sequence. This also conforms C++ vectors to `ExpressibleByArrayLiteral`, making it possible, for instance, to pass a Swift array to a C++ function that takes a vector of strings as a parameter.

rdar://104826995
2023-08-07 17:12:12 +01:00
Egor Zhdan
b5866040ae [cxx-interop] Fix Linux build with SWIFT_ENABLE_EXPERIMENTAL_CXX_INTEROP=NO
This fixes the Swift LSAN CI job Linux (https://ci.swift.org/job/oss-swift-RA-lsan-linux-ubuntu-18_04/) that started failing after https://github.com/apple/swift/pull/65398.

rdar://108674065
2023-05-02 17:35:29 +01:00
Daniel Rodríguez Troitiño
539fc69e1e build: Fixes for building Cxx module with/without stdlib. (#65172)
With the changes introduced in #65055, the Cxx module will build even
when not building the stdlib as long as
`SWIFT_BUILD_STDLIB_EXTRA_TOOLCHAIN_CONTENT` was set. With the further changes
in #65122 the previous flag is not necessary, and only
`SWIFT_BUILD_STDLIB_CXX_MODULE` (which defaults to `TRUE`) is necessary.

However, `stdlib/public/Cxx` did rely on `StdlibOptions.cmake` to be
include before it, or the functions that build the target libraries will
find a half configured state. `StdlibOptions.cmake` are included in
`stdlib/toolchain`, in `stdlib/` and `StandaloneOverlay.cmake`. Before #65122
it was working just because `stdlib/toolchain` was added just before.

The second changes adds dependencies on the legacy layouts and the clang
headers. The legacy layouts are a dependency only added to the libraries of
the stdlib core (IS_STDLIB_CORE flag). Since enabling that flag also enables
a bunch other stuff, and I am not sure that Cxx should be classified as "core"
anyway, I preferred to add the dependency. The clang headers are another
dependency of the swiftCore, which otherwise causes problems to not find
the compiler headers when building Cxx. This is necessary for builds
that use a previously built compiler to compile the stdlib.
2023-04-14 08:44:54 -07:00
Egor Zhdan
14f32312bf [cxx-interop] Do not add a dependency on clang to CxxStdlib
Cxx & CxxStdlib modules are Swift-only, they do not require invoking clang directly.

When building with `SWIFT_INCLUDE_TOOLS=NO`, Clang is not available as a CMake target (see `swift_common_standalone_build_config`).

rdar://107780733
2023-04-11 16:45:39 +01:00
Egor Zhdan
51c9539655 [cxx-interop] Build CxxStdlib module when SWIFT_BUILD_SDK_OVERLAY=NO
The C++ stdlib overlay binaries now a part of the toolchain, not the OS SDKs. This makes sure that these binaries are built when building the toolchain separately from the stdlib.

rdar://106002094
2023-03-09 19:30:47 +00:00
Egor Zhdan
ac72084854 Merge pull request #63683 from apple/egorzhdan/cxx-optional
[cxx-interop] Add `CxxOptional` protocol for `std::optional` ergonomics
2023-03-06 10:43:05 +00:00
Egor Zhdan
a12986ade4 [cxx-interop] Add CxxOptional protocol for std::optional ergonomics
This adds a protocol to the C++ standard library overlay which will improve the ergonomics of `std::optional` when used from Swift code.

As of now, the overlay adds an initializer of `Swift.Optional` that takes an instance of `CxxOptional` as a parameter.
2023-03-03 19:41:30 +01:00
Egor Zhdan
75d0e999ea [cxx-interop] Build swiftCxx overlay when SWIFT_BUILD_SDK_OVERLAY=NO
The C++ stdlib overlay binaries (`libswiftCxx` and `libswiftCxxStdlib`) are now a part of the toolchain, not the OS SDKs. This makes sure that `libswiftCxx` is built when building the toolchain separately from the stdlib.

rdar://105799283
2023-02-25 00:23:43 +00:00
Egor Zhdan
71cc1bb6f1 [cxx-interop] NFC: move unsafe iterator types to a separate file
Since the unsafe iterator types are now used throughout the overlay, not just by `CxxSequence` and `CxxRandomAccessCollection`, let's move them to a separate file.
2023-02-10 11:57:57 +00:00
Egor Zhdan
919eea7045 [cxx-interop] Add CxxDictionary protocol for std::map ergonomics
This adds a protocol to the C++ standard library overlay which will improve the ergonomics of `std::map` and `std::unordered_map` when used from Swift code.

As of now, `CxxDictionary` adds a subscript with an optional return type that mimics the subscript of `Swift.Dictionary`.

Similar to https://github.com/apple/swift/pull/63244.
2023-02-09 14:31:06 +00:00
Egor Zhdan
6366878308 [cxx-interop] Add CxxSet protocol for std::set ergonomics
This adds a protocol to the C++ standard library overlay which will improve the ergonomics of `std::set`, `std::unordered_set` and `std::multiset` when used from Swift code.

As of now, `CxxSet` adds a `contains` function to C++ sets.

C++ stdlib set types are automatically conformed to `CxxSet`: `std::set`, `unordered_set`, `std::multiset`. Custom user types are not conformed to `CxxSet` automatically: while a custom type might have an interface similar to `std::set`, the semantics might differ, and adding a conformance would cause confusion.
2023-01-26 19:35:30 +00:00
Egor Zhdan
151c205a29 [cxx-interop] Move C++ standard library overlay to the toolchain
The `swiftCxx` and `swiftstd` modules are currently a part of the SDK, but they should be a part of the toolchain instead.

rdar://103082278
2022-12-12 11:18:30 +00:00
Egor Zhdan
c2c3ea7e07 [cxx-interop] Make Cxx Swift library static
Instead of a dynamic `swiftCxx.dylib` library, let's build a static library to simplify backdeployment and reduce potential compatibility difficulties in the future.

This also adds `NO_LINK_NAME` option to `add_swift_target_library` to prevent the CMake scripts from passing `-module-link-name` to swiftc when building a given module. This fixes linker errors, which would otherwise occur due to the force-load symbol name (`_swift_FORCE_LOAD_$xyz`) being emitted for the libraries that are now static (`swiftCxx`, `swiftstd`).
2022-12-07 12:37:25 +00:00
Egor Zhdan
3e1a6dcea4 [cxx-interop] Add CxxConvertibleToCollection protocol
Since we don't automatically conform C++ non-random-access collections to `Swift.Sequence` anymore for performance reasons, we need an alternative way to access the elements of a C++ sequence from Swift.

This allows explicitly converting a C++ sequence to a Swift Array/Set.
2022-11-24 14:04:21 +00:00
Egor Zhdan
bc16131166 [cxx-interop] Add CxxRandomAccessCollection protocol
This helps to bridge C++ random access collections, such as `std::vector` and `std::string`, to Swift by conforming them to `Swift.RandomAccessCollection`
2022-10-14 17:39:37 +01:00
zoecarver
3805828ad0 [cxx-interop] Add CxxShim library; move __swift_interopStaticCast into it. 2022-07-05 15:52:51 -07:00
Egor Zhdan
82a90b90aa [cxx-interop] Split C++ stdlib overlay into two modules
This allows projects that don't want to pull in the entire C++ standard library to use stdlib-independent C++ interop utilities like `CxxSequence`.
This also makes the utilities available on platforms where we don't currently have the `std` overlay available, e.g. Windows.
2022-06-28 15:37:20 +01:00
Egor Zhdan
6754c3cf82 [cxx-interop] Add CxxSequence protocol to the stdlib overlay
This change adds basic helper protocols and structs that are going to be used for making C++ sequences and collection safe and Swifty by adding conformances to `Swift.Sequence`, `Swift.Collection`, etc.

This is not meant to be a final design.
2022-06-27 20:40:12 +01:00
Egor Zhdan
0ced5ec070 [cxx-interop] Re-enable C++ stdlib overlay on Linux 2022-06-14 23:28:37 +01:00
Alex Hoppen
95481a54bc Revert "Merge pull request #58983 from apple/egorzhdan/cxx-overlay-linux"
This reverts commit 9e69e4dd76, reversing
changes made to 59bb4cb11a.
2022-06-08 08:35:04 +02:00
Egor Zhdan
bcc07cc4eb [cxx-interop] Enable C++ stdlib overlay on Linux 2022-06-07 16:38:28 +01:00
Egor Zhdan
ff8464fd2a [cxx-interop] Add basic C++ stdlib overlay
This will allow us to add Swift conformances to C++ standard library types, and improve usability of the C++ standard library from Swift.
2022-05-16 13:30:35 +01:00
Egor Zhdan
0def240c14 [cxx-interop] Copy libstdc++ resources again if the sources changed
Previously if you changed `libstdcxx.h` or `libstdcxx.modulemap` locally and ran an incremental build, the artifacts weren't updated.
2022-05-13 13:38:03 +01:00
Egor Zhdan
67a28c78d5 [cxx-interop][SwiftCompilerSources] Enable libstdc++ on platforms that use it
Since libstdc++ doesn't come with a Clang modulemap, Swift provides its own modulemap for libstdc++. This change makes the modulemap available while building SwiftCompilerSources.
2022-05-03 19:48:14 +01:00
Butta
a839a69484 [android] Don't generate a libstdcxx.modulemap
Android uses libc++, which already has its own module map.
2022-04-28 23:51:20 +05:30
Egor Zhdan
a3e914364a [cxx-interop] Import libstdc++ as a Clang module
This change adds a module map for libstdc++, which allows it to be properly imported into Swift as a module. The module map is installed into `usr/lib/swift/linux/{arch}` similarly to `glibc.modulemap`, and is passed to Clang as `-fmodule-map-file`.

That means it is now possible to import std directly from Swift on Linux, when C++ interop is enabled.

The module map currently declares a single `std` module without splitting the headers into submodules. This is going to change in the near future.

rdar://87654514
2022-04-08 14:10:04 +01:00