Skip to content
Open
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
24 changes: 21 additions & 3 deletions examples/xplatform/cxx_from_swift/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("//swift:swift_binary.bzl", "swift_binary")
load("//swift:swift_extract_symbol_graph.bzl", "swift_extract_symbol_graph")
load("//swift:swift_interop_hint.bzl", "swift_interop_hint")
load("//swift:swift_library.bzl", "swift_library")

licenses(["notice"])

Expand All @@ -21,15 +23,31 @@ swift_interop_hint(
module_name = "CxxCounter",
)

# 2. The Swift binary then depends on the `cc_library`. This causes a
# 2. The Swift library then depends on the `cc_library`. This causes a
# Swift-compatible module map to be created for the `cc_library` so that the
# Swift code can import it.
swift_library(
name = "swift_counter",
srcs = ["Counter.swift"],
features = ["swift.enable_cpp20_interop"],
module_name = "Counter",
deps = [":counter"],
)

# 3. The Swift binary then depends on the `cc_library`. This causes a
# Swift-compatible module map to be created for the `cc_library` so that the
# Swift code can import it. Be sure to enable C++ Interoperability in the Swift
# compiler using the `-cxx-interoperability-mode` build flag.
# https://www.swift.org/documentation/cxx-interop/project-build-setup/#mixing-swift-and-c-using-other-build-systems
swift_binary(
name = "cxx_from_swift",
srcs = ["main.swift"],
copts = ["-cxx-interoperability-mode=default"],
features = ["swift.enable_cpp20_interop"],
module_name = "main",
deps = [":counter"],
deps = [":swift_counter"],
)

swift_extract_symbol_graph(
name = "symbol_graph",
targets = [":swift_counter"],
)
11 changes: 11 additions & 0 deletions examples/xplatform/cxx_from_swift/Counter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Import the C++ interface.
@_exported import CxxCounter

/// Wraps the C++ interface in a Swift interface.
extension swiftexample.Counter {

mutating public func Decrement() {
count_ = count_ - 1
}

}
2 changes: 0 additions & 2 deletions examples/xplatform/cxx_from_swift/counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class Counter {
public:
int Get() const;
void Increment();

private:
int count_ = 0;
};

Expand Down
2 changes: 1 addition & 1 deletion examples/xplatform/cxx_from_swift/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// Import the C++ interface.
import CxxCounter
import Counter

var counter = swiftexample.Counter()
for _ in 1...10 {
Expand Down
7 changes: 7 additions & 0 deletions swift/internal/feature_names.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,10 @@ SWIFT_FEATURE_THIN_LTO = "swift.thin_lto"

# Enable full LTO and update output-file-map correctly
SWIFT_FEATURE_FULL_LTO = "swift.full_lto"

# Enables swift cpp interop. When enabled, this feature adds -cxx-interoperability-mode=default
# along with the corresponding -std=c++<N> options. Use this when swift needs to imports a module
# that has C++ headers.
SWIFT_FEATURE_ENABLE_CPP17_INTEROP = "swift.enable_cpp17_interop"
SWIFT_FEATURE_ENABLE_CPP20_INTEROP = "swift.enable_cpp20_interop"
SWIFT_FEATURE_ENABLE_CPP23_INTEROP = "swift.enable_cpp23_interop"
39 changes: 39 additions & 0 deletions swift/toolchains/config/compile_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ load(
"SWIFT_FEATURE_EMIT_SWIFTINTERFACE",
"SWIFT_FEATURE_ENABLE_BARE_SLASH_REGEX",
"SWIFT_FEATURE_ENABLE_BATCH_MODE",
"SWIFT_FEATURE_ENABLE_CPP17_INTEROP",
"SWIFT_FEATURE_ENABLE_CPP20_INTEROP",
"SWIFT_FEATURE_ENABLE_CPP23_INTEROP",
"SWIFT_FEATURE_ENABLE_LIBRARY_EVOLUTION",
"SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES",
"SWIFT_FEATURE_ENABLE_TESTING",
Expand Down Expand Up @@ -1241,6 +1244,42 @@ def compile_action_configs(
SWIFT_FEATURE__SUPPORTS_V6,
],
),
ActionConfigInfo(
actions = [
SWIFT_ACTION_COMPILE,
],
configurators = [
add_arg("-cxx-interoperability-mode=default"),
add_arg("-Xcc", "-std=c++17"),
],
features = [
SWIFT_FEATURE_ENABLE_CPP17_INTEROP,
],
),
ActionConfigInfo(
actions = [
SWIFT_ACTION_COMPILE,
],
configurators = [
add_arg("-cxx-interoperability-mode=default"),
add_arg("-Xcc", "-std=c++20"),
],
features = [
SWIFT_FEATURE_ENABLE_CPP20_INTEROP,
],
),
ActionConfigInfo(
actions = [
SWIFT_ACTION_COMPILE,
],
configurators = [
add_arg("-cxx-interoperability-mode=default"),
add_arg("-Xcc", "-std=c++23"),
],
features = [
SWIFT_FEATURE_ENABLE_CPP23_INTEROP,
],
),
]

# NOTE: The positions of these action configs in the list are important,
Expand Down
38 changes: 37 additions & 1 deletion swift/toolchains/config/symbol_graph_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ load(
"//swift/internal:action_names.bzl",
"SWIFT_ACTION_SYMBOL_GRAPH_EXTRACT",
)
load(":action_config.bzl", "ActionConfigInfo")
load(
"//swift/internal:feature_names.bzl",
"SWIFT_FEATURE_ENABLE_CPP17_INTEROP",
"SWIFT_FEATURE_ENABLE_CPP20_INTEROP",
"SWIFT_FEATURE_ENABLE_CPP23_INTEROP",
)
load(":action_config.bzl", "ActionConfigInfo", "add_arg")

def symbol_graph_action_configs():
"""Returns the list of action configs needed to extract symbol graphs.
Expand All @@ -45,6 +51,36 @@ def symbol_graph_action_configs():
_symbol_graph_emit_extension_block_symbols_configurator,
],
),
ActionConfigInfo(
actions = [SWIFT_ACTION_SYMBOL_GRAPH_EXTRACT],
configurators = [
add_arg("-cxx-interoperability-mode=default"),
add_arg("-Xcc", "-std=c++17"),
],
features = [
SWIFT_FEATURE_ENABLE_CPP17_INTEROP,
],
),
ActionConfigInfo(
actions = [SWIFT_ACTION_SYMBOL_GRAPH_EXTRACT],
configurators = [
add_arg("-cxx-interoperability-mode=default"),
add_arg("-Xcc", "-std=c++20"),
],
features = [
SWIFT_FEATURE_ENABLE_CPP20_INTEROP,
],
),
ActionConfigInfo(
actions = [SWIFT_ACTION_SYMBOL_GRAPH_EXTRACT],
configurators = [
add_arg("-cxx-interoperability-mode=default"),
add_arg("-Xcc", "-std=c++23"),
],
features = [
SWIFT_FEATURE_ENABLE_CPP23_INTEROP,
],
),
]

def _symbol_graph_minimum_access_level_configurator(prerequisites, args):
Expand Down