Skip to content

[cxx-interop] Add basic C++ stdlib overlay #58452

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

Merged
merged 2 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ def enable_experimental_concurrency :
Flag<["-"], "enable-experimental-concurrency">,
HelpText<"Enable experimental concurrency model">;

def enable_experimental_cxx_interop :
Flag<["-"], "enable-experimental-cxx-interop">,
HelpText<"Enable C++ interop code generation and config directives">;

def enable_lexical_borrow_scopes :
Joined<["-"], "enable-lexical-borrow-scopes=">,
HelpText<"Whether to emit lexical borrow scopes (default: true)">,
Expand Down Expand Up @@ -825,11 +829,6 @@ def emit_sorted_sil : Flag<["-"], "emit-sorted-sil">,
def emit_syntax : Flag<["-"], "emit-syntax">,
HelpText<"Parse input file(s) and emit the Syntax tree(s) as JSON">, ModeOpt;

def enable_experimental_cxx_interop :
Flag<["-"], "enable-experimental-cxx-interop">,
HelpText<"Enable C++ interop code generation and config directives">,
Flags<[FrontendOption, HelpHidden]>;

def enable_cxx_interop :
Flag<["-"], "enable-cxx-interop">,
HelpText<"Alias for -enable-experimental-cxx-interop">,
Expand Down
21 changes: 21 additions & 0 deletions stdlib/public/Cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,24 @@ endforeach()
add_custom_target(libstdcxx-modulemap DEPENDS ${libstdcxx_modulemap_target_list})
set_property(TARGET libstdcxx-modulemap PROPERTY FOLDER "Miscellaneous")
add_dependencies(sdk-overlay libstdcxx-modulemap)


#
# C++ Standard Library Overlay.
#
add_swift_target_library(swiftstd ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
std.swift
String.swift

SWIFT_MODULE_DEPENDS_OSX Darwin
SWIFT_MODULE_DEPENDS_IOS Darwin
SWIFT_MODULE_DEPENDS_TVOS Darwin
SWIFT_MODULE_DEPENDS_WATCHOS Darwin

SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
-Xfrontend -enable-experimental-cxx-interop
-Xfrontend -module-interface-preserve-types-as-written

LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" -lc++
TARGET_SDKS ALL_APPLE_PLATFORMS # TODO: support other platforms as well
INSTALL_IN_COMPONENT sdk-overlay)
26 changes: 26 additions & 0 deletions stdlib/public/Cxx/String.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

extension std.string {
public init(_ string: String) {
self.init()
for char in string.utf8 {
self.push_back(value_type(char))
}
}
}

extension String {
public init(cxxString: std.string) {
self.init(cString: cxxString.c_str())
}
}
13 changes: 13 additions & 0 deletions stdlib/public/Cxx/std.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

@_exported import std // Clang module
21 changes: 21 additions & 0 deletions test/Interop/Cxx/stdlib/overlay/string.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-cxx-interop)
//
// REQUIRES: executable_test
// REQUIRES: OS=macosx

import StdlibUnittest
import std

var StdStringOverlayTestSuite = TestSuite("std::string overlay")

StdStringOverlayTestSuite.test("std::string <=> Swift.String") {
let cxx1 = std.string()
let swift1 = String(cxxString: cxx1)
expectEqual(swift1, "")

let cxx2 = std.string("something123")
let swift2 = String(cxxString: cxx2)
expectEqual(swift2, "something123")
}

runAllTests()
8 changes: 6 additions & 2 deletions validation-test/ParseableInterface/verify_all_overlays.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@

# swift -build-module-from-parseable-interface
output_path = os.path.join(output_dir, module_name + ".swiftmodule")
compiler_args = ["-o", output_path, "-module-name", module_name,
interface_file]
if module_name == "std":
compiler_args += ["-enable-experimental-cxx-interop"]

status = subprocess.call(compiler_invocation +
["-o", output_path, "-module-name", module_name,
interface_file])
compiler_args)
if status != 0:
print("# " + target_os + ": " + module_name)