Skip to content

Commit 3bc7bc1

Browse files
authored
Add swift.emit_symbol_graph feature (#838)
That emits the symbol graph for the Swift target to `bazel-bin`. Symbol graphs can then be fed to tools such as DocC or jazzy to generate documentation for a target. For example, for [Envoy Mobile](https://github.com/envoyproxy/envoy-mobile): ``` $ ./bazelw build //library/swift:ios_lib --config=ios $ "$(xcrun --find docc)" convert \ --index \ --fallback-display-name \ Envoy \ --fallback-bundle-identifier \ io.envoyproxy.EnvoyMobile \ --fallback-bundle-version \ 0.4.6 \ --output-dir \ Envoy.doccarchive \ --transform-for-static-hosting \ --additional-symbol-graph-dir \ bazel-bin/library/swift/ios_lib.symbolgraph ```
1 parent e52312c commit 3bc7bc1

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

swift/internal/compiling.bzl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ load(
3939
"SWIFT_FEATURE_EMIT_BC",
4040
"SWIFT_FEATURE_EMIT_C_MODULE",
4141
"SWIFT_FEATURE_EMIT_SWIFTINTERFACE",
42+
"SWIFT_FEATURE_EMIT_SYMBOL_GRAPH",
4243
"SWIFT_FEATURE_ENABLE_BATCH_MODE",
4344
"SWIFT_FEATURE_ENABLE_LIBRARY_EVOLUTION",
4445
"SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES",
@@ -703,6 +704,15 @@ def compile_action_configs(
703704
SWIFT_FEATURE_USE_PCH_OUTPUT_DIR,
704705
],
705706
),
707+
swift_toolchain_config.action_config(
708+
actions = [
709+
swift_action_names.COMPILE,
710+
],
711+
configurators = [_emit_symbol_graph_configurator],
712+
features = [
713+
SWIFT_FEATURE_EMIT_SYMBOL_GRAPH,
714+
],
715+
),
706716

707717
# When using C modules, disable the implicit search for module map files
708718
# because all of them, including system dependencies, will be provided
@@ -1608,6 +1618,21 @@ def _pch_output_dir_configurator(prerequisites, args):
16081618
paths.join(prerequisites.bin_dir.path, "_pch_output_dir"),
16091619
)
16101620

1621+
def _emit_symbol_graph_configurator(prerequisites, args):
1622+
"""Adds flags for `-emit-symbol-graph` configuration to the command line.
1623+
1624+
This is a directory to persist symbol graph files that can be used by
1625+
tools such as DocC or jazzy to generate documentation.
1626+
"""
1627+
args.add(
1628+
"-Xfrontend",
1629+
"-emit-symbol-graph",
1630+
)
1631+
args.add(
1632+
"-emit-symbol-graph-dir",
1633+
prerequisites.symbol_graph_directory.path,
1634+
)
1635+
16111636
def _global_index_store_configurator(prerequisites, args):
16121637
"""Adds flags for index-store generation to the command line."""
16131638
out_dir = prerequisites.indexstore_directory.dirname.split("/")[0]
@@ -1867,6 +1892,7 @@ def compile(
18671892
all_compile_outputs = compact([
18681893
compile_outputs.swiftinterface_file,
18691894
compile_outputs.indexstore_directory,
1895+
compile_outputs.symbol_graph_directory,
18701896
]) + compile_outputs.object_files
18711897
all_derived_outputs = compact([
18721898
# The `.swiftmodule` file is explicitly listed as the first output
@@ -1890,6 +1916,7 @@ def compile(
18901916
compile_outputs.swiftsourceinfo_file,
18911917
compile_outputs.generated_header_file,
18921918
compile_outputs.indexstore_directory,
1919+
compile_outputs.symbol_graph_directory,
18931920
]) + compile_outputs.object_files + other_outputs
18941921
all_derived_outputs = []
18951922

@@ -2073,6 +2100,7 @@ def compile(
20732100
other_compilation_outputs = struct(
20742101
ast_files = compile_outputs.ast_files,
20752102
indexstore = compile_outputs.indexstore_directory,
2103+
symbol_graph = compile_outputs.symbol_graph_directory,
20762104
)
20772105

20782106
return module_context, cc_compilation_outputs, other_compilation_outputs
@@ -2511,11 +2539,24 @@ def _declare_compile_outputs(
25112539
else:
25122540
indexstore_directory = None
25132541

2542+
emit_symbol_graph = is_feature_enabled(
2543+
feature_configuration = feature_configuration,
2544+
feature_name = SWIFT_FEATURE_EMIT_SYMBOL_GRAPH,
2545+
)
2546+
if (emit_symbol_graph):
2547+
symbol_graph_directory = derived_files.symbol_graph_directory(
2548+
actions = actions,
2549+
target_name = target_name,
2550+
)
2551+
else:
2552+
symbol_graph_directory = None
2553+
25142554
compile_outputs = struct(
25152555
ast_files = ast_files,
25162556
generated_header_file = generated_header,
25172557
generated_module_map_file = generated_module_map,
25182558
indexstore_directory = indexstore_directory,
2559+
symbol_graph_directory = symbol_graph_directory,
25192560
object_files = object_files,
25202561
output_file_map = output_file_map,
25212562
derived_files_output_file_map = derived_files_output_file_map,
@@ -2836,6 +2877,11 @@ def output_groups_from_other_compilation_outputs(*, other_compilation_outputs):
28362877
other_compilation_outputs.indexstore,
28372878
])
28382879

2880+
if other_compilation_outputs.symbol_graph:
2881+
output_groups["swift_symbol_graph"] = depset([
2882+
other_compilation_outputs.symbol_graph,
2883+
])
2884+
28392885
return output_groups
28402886

28412887
def swift_library_output_map(name):

swift/internal/derived_files.bzl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ def _indexstore_directory(actions, target_name):
6969
"""
7070
return actions.declare_directory("{}.indexstore".format(target_name))
7171

72+
def _symbol_graph_directory(actions, target_name):
73+
"""Declares a directory in which the compiler's symbol graph will be written.
74+
75+
Args:
76+
actions: The context's actions object.
77+
target_name: The name of the target being built.
78+
79+
Returns:
80+
The declared `File`.
81+
"""
82+
return actions.declare_directory("{}.symbolgraph".format(target_name))
83+
7284
def _intermediate_bc_file(actions, target_name, src):
7385
"""Declares a file for an intermediate llvm bc file during compilation.
7486
@@ -340,6 +352,7 @@ derived_files = struct(
340352
swiftinterface = _swiftinterface,
341353
swiftmodule = _swiftmodule,
342354
swiftsourceinfo = _swiftsourceinfo,
355+
symbol_graph_directory = _symbol_graph_directory,
343356
vfsoverlay = _vfsoverlay,
344357
whole_module_object_file = _whole_module_object_file,
345358
xctest_runner_script = _xctest_runner_script,

swift/internal/feature_names.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ SWIFT_FEATURE_CODEVIEW_DEBUG_INFO = "swift.codeview_debug_info"
105105
# https://docs.google.com/document/d/1cH2sTpgSnJZCkZtJl1aY-rzy4uGPcrI-6RrUpdATO2Q/
106106
SWIFT_FEATURE_INDEX_WHILE_BUILDING = "swift.index_while_building"
107107

108+
# If enabled, the compilation action for a target will produce a symbol graph.
109+
SWIFT_FEATURE_EMIT_SYMBOL_GRAPH = "swift.emit_symbol_graph"
110+
108111
# If enabled the compilation action will not produce indexes for system modules.
109112
SWIFT_FEATURE_DISABLE_SYSTEM_INDEX = "swift.disable_system_index"
110113

0 commit comments

Comments
 (0)