Files
swift-mirror/test/Interop/Cxx/stdlib/use-libcxx-dependency-linux.swift
Egor Zhdan 059f0f97d1 [cxx-interop] Allow compiling with libc++ on Linux
This makes sure that Swift respects `-Xcc -stdlib=libc++` flags.

Clang already has existing logic to discover the system-wide libc++ installation on Linux. We rely on that logic here.

Importing a Swift module that was built with a different C++ stdlib is not supported and emits an error.

The Cxx module can be imported when compiling with any C++ stdlib. The synthesized conformances, e.g. to CxxRandomAccessCollection also work. However, CxxStdlib currently cannot be imported when compiling with libc++, since on Linux it refers to symbols from libstdc++ which have different mangled names in libc++.

rdar://118357548 / https://github.com/swiftlang/swift/issues/69825
2024-08-08 16:24:58 +01:00

35 lines
2.2 KiB
Swift

// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// Make sure we can build a Swift library with libc++ and then import it from a Swift executable.
// RUN: %target-build-swift %t/library.swift -emit-module -emit-library -cxx-interoperability-mode=default -Xcc -stdlib=libc++ -module-name MyLibrary -emit-module-path %t/artifacts/MyLibrary.swiftmodule -I %S/Inputs
// RUN: %target-build-swift %t/executable.swift -emit-executable -cxx-interoperability-mode=default -Xcc -stdlib=libc++ -module-name ImportsMyLibrary -I %t/artifacts -I %S/Inputs
// RUN: %empty-directory(%t/artifacts)
// Make sure Swift does not allow importing a library built with libc++ into an executable that uses libstdc++.
// RUN: %target-build-swift %t/library.swift -emit-module -emit-library -cxx-interoperability-mode=default -Xcc -stdlib=libc++ -module-name MyLibrary -emit-module-path %t/artifacts/MyLibrary.swiftmodule -I %S/Inputs
// RUN: not %target-build-swift %t/executable.swift -emit-executable -cxx-interoperability-mode=default -module-name ImportsMyLibrary -I %t/artifacts -I %S/Inputs 2>&1 | %FileCheck %s --check-prefix=CHECK-LIBCXX-DEPENDENCY
// CHECK-LIBCXX-DEPENDENCY: error: module 'MyLibrary' was built with libc++, but current compilation uses libstdc++
// RUN: %empty-directory(%t/artifacts)
// Make sure Swift does not allow importing a library built with libstdc++ into an executable that uses libc++.
// RUN: %target-build-swift %t/library.swift -emit-module -emit-library -cxx-interoperability-mode=default -module-name MyLibrary -emit-module-path %t/artifacts/MyLibrary.swiftmodule -I %S/Inputs
// RUN: not %target-build-swift %t/executable.swift -emit-executable -cxx-interoperability-mode=default -Xcc -stdlib=libc++ -module-name ImportsMyLibrary -I %t/artifacts -I %S/Inputs 2>&1 | %FileCheck %s --check-prefix=CHECK-LIBSTDCXX-DEPENDENCY
// CHECK-LIBSTDCXX-DEPENDENCY: error: module 'MyLibrary' was built with libstdc++, but current compilation uses libc++
// REQUIRES: OS=linux-gnu
// REQUIRES: system_wide_libcxx
//--- library.swift
import CxxStdlib
import StdString
public func getStdString() -> std.string { return std.string() }
//--- executable.swift
import MyLibrary
print("")