Skip to content

Commit 42d7c0d

Browse files
committed
feat(examples): make -cxx-interoperability-mode a feature
-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 780ee01 commit 42d7c0d

File tree

7 files changed

+115
-7
lines changed

7 files changed

+115
-7
lines changed

examples/xplatform/cxx_from_swift/BUILD

Lines changed: 20 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,30 @@ 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"],
3345
module_name = "main",
34-
deps = [":counter"],
46+
deps = [":swift_counter"],
47+
)
48+
49+
swift_extract_symbol_graph(
50+
name = "symbol_graph",
51+
targets = [":swift_counter"],
3552
)
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
@@ -399,3 +399,10 @@ SWIFT_FEATURE_THIN_LTO = "swift.thin_lto"
399399

400400
# Enable full LTO and update output-file-map correctly
401401
SWIFT_FEATURE_FULL_LTO = "swift.full_lto"
402+
403+
# Enables swift cpp interop. When enabled, this feature adds -cxx-interoperability-mode=default
404+
# along with the corresponding -std=c++<N> options. Use this when swift needs to imports a module
405+
# that has C++ headers.
406+
SWIFT_FEATURE_ENABLE_CPP17_INTEROP = "swift.enable_cpp17_interop"
407+
SWIFT_FEATURE_ENABLE_CPP20_INTEROP = "swift.enable_cpp20_interop"
408+
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
@@ -40,6 +40,9 @@ load(
4040
"SWIFT_FEATURE_CODEVIEW_DEBUG_INFO",
4141
"SWIFT_FEATURE_COVERAGE",
4242
"SWIFT_FEATURE_COVERAGE_PREFIX_MAP",
43+
"SWIFT_FEATURE_ENABLE_CPP17_INTEROP",
44+
"SWIFT_FEATURE_ENABLE_CPP20_INTEROP",
45+
"SWIFT_FEATURE_ENABLE_CPP23_INTEROP",
4346
"SWIFT_FEATURE_DBG",
4447
"SWIFT_FEATURE_DEBUG_PREFIX_MAP",
4548
"SWIFT_FEATURE_DECLARE_SWIFTSOURCEINFO",
@@ -1241,6 +1244,42 @@ def compile_action_configs(
12411244
SWIFT_FEATURE__SUPPORTS_V6,
12421245
],
12431246
),
1247+
ActionConfigInfo(
1248+
actions = [
1249+
SWIFT_ACTION_COMPILE,
1250+
],
1251+
configurators = [
1252+
add_arg("-cxx-interoperability-mode=default"),
1253+
add_arg("-Xcc", "-std=c++17"),
1254+
],
1255+
features = [
1256+
SWIFT_FEATURE_ENABLE_CPP17_INTEROP,
1257+
],
1258+
),
1259+
ActionConfigInfo(
1260+
actions = [
1261+
SWIFT_ACTION_COMPILE,
1262+
],
1263+
configurators = [
1264+
add_arg("-cxx-interoperability-mode=default"),
1265+
add_arg("-Xcc", "-std=c++20"),
1266+
],
1267+
features = [
1268+
SWIFT_FEATURE_ENABLE_CPP20_INTEROP,
1269+
],
1270+
),
1271+
ActionConfigInfo(
1272+
actions = [
1273+
SWIFT_ACTION_COMPILE,
1274+
],
1275+
configurators = [
1276+
add_arg("-cxx-interoperability-mode=default"),
1277+
add_arg("-Xcc", "-std=c++23"),
1278+
],
1279+
features = [
1280+
SWIFT_FEATURE_ENABLE_CPP23_INTEROP,
1281+
],
1282+
),
12441283
]
12451284

12461285
# 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)