Skip to content

Commit 25ea7ca

Browse files
authored
feat(examples): make -cxx-interoperability-mode a feature (#1567)
-cxx-interoperability-mode currently needs to be passed as copts to targets who depend on C++ code, as seen in examples/xplatform/cxx_from_swift. This works in simple cases, but it currently fails if the `swift_symbol_graph_aspect` is applied on this target, as copts are not propagated to this action (rightfully, as not all copts will work). So in order to make it work, we're turning this flag into a feature, and we add corresponding action configs to both the compile action and to the symbol_graph_extract action. We're also enhancing the cxx_from_swift test by testing for this case. We're doing so by doing the following changes: * we're adding a swift_library() between the swift_binary() and the cc_library() targets * we're adding a new swift_extract_symbol_graph() target that depends on this new library This test will fail if -cxx-interoperability-mode=default is not passed to swift-symbolgraph-extract.
1 parent c6a6534 commit 25ea7ca

File tree

7 files changed

+116
-7
lines changed

7 files changed

+116
-7
lines changed

examples/xplatform/cxx_from_swift/BUILD

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
load("@rules_cc//cc:cc_library.bzl", "cc_library")
22
load("//swift:swift_binary.bzl", "swift_binary")
3+
load("//swift:swift_extract_symbol_graph.bzl", "swift_extract_symbol_graph")
34
load("//swift:swift_interop_hint.bzl", "swift_interop_hint")
5+
load("//swift:swift_library.bzl", "swift_library")
46

57
licenses(["notice"])
68

@@ -21,15 +23,31 @@ swift_interop_hint(
2123
module_name = "CxxCounter",
2224
)
2325

24-
# 2. The Swift binary then depends on the `cc_library`. This causes a
26+
# 2. The Swift library then depends on the `cc_library`. This causes a
27+
# Swift-compatible module map to be created for the `cc_library` so that the
28+
# Swift code can import it.
29+
swift_library(
30+
name = "swift_counter",
31+
srcs = ["Counter.swift"],
32+
features = ["swift.enable_cpp20_interop"],
33+
module_name = "Counter",
34+
deps = [":counter"],
35+
)
36+
37+
# 3. The Swift binary then depends on the `cc_library`. This causes a
2538
# Swift-compatible module map to be created for the `cc_library` so that the
2639
# Swift code can import it. Be sure to enable C++ Interoperability in the Swift
2740
# compiler using the `-cxx-interoperability-mode` build flag.
2841
# https://www.swift.org/documentation/cxx-interop/project-build-setup/#mixing-swift-and-c-using-other-build-systems
2942
swift_binary(
3043
name = "cxx_from_swift",
3144
srcs = ["main.swift"],
32-
copts = ["-cxx-interoperability-mode=default"],
45+
features = ["swift.enable_cpp20_interop"],
3346
module_name = "main",
34-
deps = [":counter"],
47+
deps = [":swift_counter"],
48+
)
49+
50+
swift_extract_symbol_graph(
51+
name = "symbol_graph",
52+
targets = [":swift_counter"],
3553
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Import the C++ interface.
2+
@_exported import CxxCounter
3+
4+
/// Wraps the C++ interface in a Swift interface.
5+
extension swiftexample.Counter {
6+
7+
mutating public func Decrement() {
8+
count_ = count_ - 1
9+
}
10+
11+
}

examples/xplatform/cxx_from_swift/counter.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class Counter {
1818
public:
1919
int Get() const;
2020
void Increment();
21-
22-
private:
2321
int count_ = 0;
2422
};
2523

examples/xplatform/cxx_from_swift/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
// Import the C++ interface.
16-
import CxxCounter
16+
import Counter
1717

1818
var counter = swiftexample.Counter()
1919
for _ in 1...10 {

swift/internal/feature_names.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,10 @@ SWIFT_FEATURE_THIN_LTO = "swift.thin_lto"
404404

405405
# Enable full LTO and update output-file-map correctly
406406
SWIFT_FEATURE_FULL_LTO = "swift.full_lto"
407+
408+
# Enables swift cpp interop. When enabled, this feature adds -cxx-interoperability-mode=default
409+
# along with the corresponding -std=c++<N> options. Use this when swift needs to imports a module
410+
# that has C++ headers.
411+
SWIFT_FEATURE_ENABLE_CPP17_INTEROP = "swift.enable_cpp17_interop"
412+
SWIFT_FEATURE_ENABLE_CPP20_INTEROP = "swift.enable_cpp20_interop"
413+
SWIFT_FEATURE_ENABLE_CPP23_INTEROP = "swift.enable_cpp23_interop"

swift/toolchains/config/compile_config.bzl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ load(
5454
"SWIFT_FEATURE_EMIT_SWIFTINTERFACE",
5555
"SWIFT_FEATURE_ENABLE_BARE_SLASH_REGEX",
5656
"SWIFT_FEATURE_ENABLE_BATCH_MODE",
57+
"SWIFT_FEATURE_ENABLE_CPP17_INTEROP",
58+
"SWIFT_FEATURE_ENABLE_CPP20_INTEROP",
59+
"SWIFT_FEATURE_ENABLE_CPP23_INTEROP",
5760
"SWIFT_FEATURE_ENABLE_LIBRARY_EVOLUTION",
5861
"SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES",
5962
"SWIFT_FEATURE_ENABLE_TESTING",
@@ -1298,6 +1301,42 @@ def compile_action_configs(
12981301
SWIFT_FEATURE__SUPPORTS_V6,
12991302
],
13001303
),
1304+
ActionConfigInfo(
1305+
actions = [
1306+
SWIFT_ACTION_COMPILE,
1307+
],
1308+
configurators = [
1309+
add_arg("-cxx-interoperability-mode=default"),
1310+
add_arg("-Xcc", "-std=c++17"),
1311+
],
1312+
features = [
1313+
SWIFT_FEATURE_ENABLE_CPP17_INTEROP,
1314+
],
1315+
),
1316+
ActionConfigInfo(
1317+
actions = [
1318+
SWIFT_ACTION_COMPILE,
1319+
],
1320+
configurators = [
1321+
add_arg("-cxx-interoperability-mode=default"),
1322+
add_arg("-Xcc", "-std=c++20"),
1323+
],
1324+
features = [
1325+
SWIFT_FEATURE_ENABLE_CPP20_INTEROP,
1326+
],
1327+
),
1328+
ActionConfigInfo(
1329+
actions = [
1330+
SWIFT_ACTION_COMPILE,
1331+
],
1332+
configurators = [
1333+
add_arg("-cxx-interoperability-mode=default"),
1334+
add_arg("-Xcc", "-std=c++23"),
1335+
],
1336+
features = [
1337+
SWIFT_FEATURE_ENABLE_CPP23_INTEROP,
1338+
],
1339+
),
13011340
]
13021341

13031342
# NOTE: The positions of these action configs in the list are important,

swift/toolchains/config/symbol_graph_config.bzl

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ load(
1818
"//swift/internal:action_names.bzl",
1919
"SWIFT_ACTION_SYMBOL_GRAPH_EXTRACT",
2020
)
21-
load(":action_config.bzl", "ActionConfigInfo")
21+
load(
22+
"//swift/internal:feature_names.bzl",
23+
"SWIFT_FEATURE_ENABLE_CPP17_INTEROP",
24+
"SWIFT_FEATURE_ENABLE_CPP20_INTEROP",
25+
"SWIFT_FEATURE_ENABLE_CPP23_INTEROP",
26+
)
27+
load(":action_config.bzl", "ActionConfigInfo", "add_arg")
2228

2329
def symbol_graph_action_configs():
2430
"""Returns the list of action configs needed to extract symbol graphs.
@@ -45,6 +51,36 @@ def symbol_graph_action_configs():
4551
_symbol_graph_emit_extension_block_symbols_configurator,
4652
],
4753
),
54+
ActionConfigInfo(
55+
actions = [SWIFT_ACTION_SYMBOL_GRAPH_EXTRACT],
56+
configurators = [
57+
add_arg("-cxx-interoperability-mode=default"),
58+
add_arg("-Xcc", "-std=c++17"),
59+
],
60+
features = [
61+
SWIFT_FEATURE_ENABLE_CPP17_INTEROP,
62+
],
63+
),
64+
ActionConfigInfo(
65+
actions = [SWIFT_ACTION_SYMBOL_GRAPH_EXTRACT],
66+
configurators = [
67+
add_arg("-cxx-interoperability-mode=default"),
68+
add_arg("-Xcc", "-std=c++20"),
69+
],
70+
features = [
71+
SWIFT_FEATURE_ENABLE_CPP20_INTEROP,
72+
],
73+
),
74+
ActionConfigInfo(
75+
actions = [SWIFT_ACTION_SYMBOL_GRAPH_EXTRACT],
76+
configurators = [
77+
add_arg("-cxx-interoperability-mode=default"),
78+
add_arg("-Xcc", "-std=c++23"),
79+
],
80+
features = [
81+
SWIFT_FEATURE_ENABLE_CPP23_INTEROP,
82+
],
83+
),
4884
]
4985

5086
def _symbol_graph_minimum_access_level_configurator(prerequisites, args):

0 commit comments

Comments
 (0)