Skip to content

Commit ed17a85

Browse files
authored
Merge pull request #58452 from apple/egorzhdan/cxx-stdlib-overlay
[cxx-interop] Add basic C++ stdlib overlay
2 parents 7534c06 + 74d22e7 commit ed17a85

File tree

6 files changed

+91
-7
lines changed

6 files changed

+91
-7
lines changed

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ def enable_experimental_concurrency :
278278
Flag<["-"], "enable-experimental-concurrency">,
279279
HelpText<"Enable experimental concurrency model">;
280280

281+
def enable_experimental_cxx_interop :
282+
Flag<["-"], "enable-experimental-cxx-interop">,
283+
HelpText<"Enable C++ interop code generation and config directives">;
284+
281285
def enable_lexical_borrow_scopes :
282286
Joined<["-"], "enable-lexical-borrow-scopes=">,
283287
HelpText<"Whether to emit lexical borrow scopes (default: true)">,
@@ -825,11 +829,6 @@ def emit_sorted_sil : Flag<["-"], "emit-sorted-sil">,
825829
def emit_syntax : Flag<["-"], "emit-syntax">,
826830
HelpText<"Parse input file(s) and emit the Syntax tree(s) as JSON">, ModeOpt;
827831

828-
def enable_experimental_cxx_interop :
829-
Flag<["-"], "enable-experimental-cxx-interop">,
830-
HelpText<"Enable C++ interop code generation and config directives">,
831-
Flags<[FrontendOption, HelpHidden]>;
832-
833832
def enable_cxx_interop :
834833
Flag<["-"], "enable-cxx-interop">,
835834
HelpText<"Alias for -enable-experimental-cxx-interop">,

stdlib/public/Cxx/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,24 @@ endforeach()
122122
add_custom_target(libstdcxx-modulemap DEPENDS ${libstdcxx_modulemap_target_list})
123123
set_property(TARGET libstdcxx-modulemap PROPERTY FOLDER "Miscellaneous")
124124
add_dependencies(sdk-overlay libstdcxx-modulemap)
125+
126+
127+
#
128+
# C++ Standard Library Overlay.
129+
#
130+
add_swift_target_library(swiftstd ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
131+
std.swift
132+
String.swift
133+
134+
SWIFT_MODULE_DEPENDS_OSX Darwin
135+
SWIFT_MODULE_DEPENDS_IOS Darwin
136+
SWIFT_MODULE_DEPENDS_TVOS Darwin
137+
SWIFT_MODULE_DEPENDS_WATCHOS Darwin
138+
139+
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
140+
-Xfrontend -enable-experimental-cxx-interop
141+
-Xfrontend -module-interface-preserve-types-as-written
142+
143+
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" -lc++
144+
TARGET_SDKS ALL_APPLE_PLATFORMS # TODO: support other platforms as well
145+
INSTALL_IN_COMPONENT sdk-overlay)

stdlib/public/Cxx/String.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
extension std.string {
14+
public init(_ string: String) {
15+
self.init()
16+
for char in string.utf8 {
17+
self.push_back(value_type(char))
18+
}
19+
}
20+
}
21+
22+
extension String {
23+
public init(cxxString: std.string) {
24+
self.init(cString: cxxString.c_str())
25+
}
26+
}

stdlib/public/Cxx/std.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@_exported import std // Clang module
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-cxx-interop)
2+
//
3+
// REQUIRES: executable_test
4+
// REQUIRES: OS=macosx
5+
6+
import StdlibUnittest
7+
import std
8+
9+
var StdStringOverlayTestSuite = TestSuite("std::string overlay")
10+
11+
StdStringOverlayTestSuite.test("std::string <=> Swift.String") {
12+
let cxx1 = std.string()
13+
let swift1 = String(cxxString: cxx1)
14+
expectEqual(swift1, "")
15+
16+
let cxx2 = std.string("something123")
17+
let swift2 = String(cxxString: cxx2)
18+
expectEqual(swift2, "something123")
19+
}
20+
21+
runAllTests()

validation-test/ParseableInterface/verify_all_overlays.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@
5151

5252
# swift -build-module-from-parseable-interface
5353
output_path = os.path.join(output_dir, module_name + ".swiftmodule")
54+
compiler_args = ["-o", output_path, "-module-name", module_name,
55+
interface_file]
56+
if module_name == "std":
57+
compiler_args += ["-enable-experimental-cxx-interop"]
58+
5459
status = subprocess.call(compiler_invocation +
55-
["-o", output_path, "-module-name", module_name,
56-
interface_file])
60+
compiler_args)
5761
if status != 0:
5862
print("# " + target_os + ": " + module_name)

0 commit comments

Comments
 (0)