Skip to content

Commit

Permalink
Add index-while-building support (back) to the Swift build rules
Browse files Browse the repository at this point in the history
When the `swift.index_while_building` feature is set, targets will emit an indexstore directory to the "indexstore" output group.

Unlike the original implementation, this one is focused on the remote indexing use case, so we don't support the old behavior meant for local builds of scanning `--swiftcopt` for a manually-specified path that would be the destination for all index data. Users who want to do that should just pass _all_ of the indexing flags to `--swiftcopt` instead of using this feature.

PiperOrigin-RevId: 459613898
(cherry picked from commit 8150427)

---

Cherry-pick notes: we never removed this. This is mainly just moving things around to make future cherry-picks easier. Though, we did take the `--swiftcopt` change by removing `_is_index_store_path_overridden` (which is a breaking change).

Signed-off-by: Brentley Jones <github@brentleyjones.com>
  • Loading branch information
allevato authored and brentleyjones committed Jun 21, 2024
1 parent 620fc29 commit 359a3c1
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 109 deletions.
43 changes: 27 additions & 16 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,28 +122,39 @@ Compiles a Swift module.

**RETURNS**

A tuple containing three elements:

1. A Swift module context (as returned by `swift_common.create_module`)
that contains the Swift (and potentially C/Objective-C) compilation
prerequisites of the compiled module. This should typically be
propagated by a `SwiftInfo` provider of the calling rule, and the
`CcCompilationContext` inside the Clang module substructure should
be propagated by the `CcInfo` provider of the calling rule.
2. A `CcCompilationOutputs` object (as returned by
`cc_common.create_compilation_outputs`) that contains the compiled
object files.
3. A struct containing:
A `struct` with the following fields:

* `module_context`: A Swift module context (as returned by
`swift_common.create_module`) that contains the Swift (and
potentially C/Objective-C) compilation prerequisites of the compiled
module. This should typically be propagated by a `SwiftInfo`
provider of the calling rule, and the `CcCompilationContext` inside
the Clang module substructure should be propagated by the `CcInfo`
provider of the calling rule.

* `compilation_outputs`: A `CcCompilationOutputs` object (as returned
by `cc_common.create_compilation_outputs`) that contains the
compiled object files.

* `supplemental_outputs`: A `struct` representing supplemental,
optional outputs. Its fields are:

* `ast_files`: A list of `File`s output from the `DUMP_AST`
action.
* `indexstore`: A `File` representing the directory that contains
the index store data generated by the compiler if the
`"swift.index_while_building"` feature is enabled, otherwise
this will be `None`.

* `const_values_files`: A list of `File`s that contains JSON
representations of constant values extracted from the source
files, if requested via a direct dependency.

* `indexstore_directory`: A directory-type `File` that represents
the indexstore output files created when the feature
`swift.index_while_building` is enabled.

* `macro_expansion_directory`: A directory-type `File` that
represents the location where macro expansion files were written
(only in debug/fastbuild and only when the toolchain supports
macros).


<a id="swift_common.compile_module_interface"></a>

Expand Down
8 changes: 6 additions & 2 deletions proto/swift_proto_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def compile_swift_protos_for_target(

# Compile the generated Swift source files as a module:
include_dev_srch_paths = include_developer_search_paths(attr)
module_context, cc_compilation_outputs, supplemental_outputs = swift_common.compile(
compile_result = swift_common.compile(
actions = ctx.actions,
cc_infos = get_providers(compiler_deps, CcInfo),
copts = ["-parse-as-library"],
Expand All @@ -283,11 +283,15 @@ def compile_swift_protos_for_target(
workspace_name = ctx.workspace_name,
)

module_context = compile_result.module_context
compilation_outputs = compile_result.compilation_outputs
supplemental_outputs = compile_result.supplemental_outputs

# Create the linking context from the compilation outputs:
linking_context, linking_output = (
swift_common.create_linking_context_from_compilation_outputs(
actions = ctx.actions,
compilation_outputs = cc_compilation_outputs,
compilation_outputs = compilation_outputs,
feature_configuration = feature_configuration,
include_dev_srch_paths = include_dev_srch_paths,
label = target_label,
Expand Down
61 changes: 37 additions & 24 deletions swift/internal/compiling.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -439,27 +439,38 @@ def compile(
outputs.
Returns:
A tuple containing three elements:
1. A Swift module context (as returned by `swift_common.create_module`)
that contains the Swift (and potentially C/Objective-C) compilation
prerequisites of the compiled module. This should typically be
propagated by a `SwiftInfo` provider of the calling rule, and the
`CcCompilationContext` inside the Clang module substructure should
be propagated by the `CcInfo` provider of the calling rule.
2. A `CcCompilationOutputs` object (as returned by
`cc_common.create_compilation_outputs`) that contains the compiled
object files.
3. A struct containing:
A `struct` with the following fields:
* `module_context`: A Swift module context (as returned by
`swift_common.create_module`) that contains the Swift (and
potentially C/Objective-C) compilation prerequisites of the compiled
module. This should typically be propagated by a `SwiftInfo`
provider of the calling rule, and the `CcCompilationContext` inside
the Clang module substructure should be propagated by the `CcInfo`
provider of the calling rule.
* `compilation_outputs`: A `CcCompilationOutputs` object (as returned
by `cc_common.create_compilation_outputs`) that contains the
compiled object files.
* `supplemental_outputs`: A `struct` representing supplemental,
optional outputs. Its fields are:
* `ast_files`: A list of `File`s output from the `DUMP_AST`
action.
* `indexstore`: A `File` representing the directory that contains
the index store data generated by the compiler if the
`"swift.index_while_building"` feature is enabled, otherwise
this will be `None`.
* `const_values_files`: A list of `File`s that contains JSON
representations of constant values extracted from the source
files, if requested via a direct dependency.
* `indexstore_directory`: A directory-type `File` that represents
the indexstore output files created when the feature
`swift.index_while_building` is enabled.
* `macro_expansion_directory`: A directory-type `File` that
represents the location where macro expansion files were written
(only in debug/fastbuild and only when the toolchain supports
macros).
"""

# Collect the `SwiftInfo` providers that represent the dependencies of the
Expand Down Expand Up @@ -815,20 +826,22 @@ to use swift_common.compile(include_dev_srch_paths = ...) instead.\
),
)

cc_compilation_outputs = cc_common.create_compilation_outputs(
compilation_outputs = cc_common.create_compilation_outputs(
objects = depset(compile_outputs.object_files),
pic_objects = depset(compile_outputs.object_files),
)

other_compilation_outputs = struct(
ast_files = compile_outputs.ast_files,
indexstore = compile_outputs.indexstore_directory,
macro_expansion_directory = compile_outputs.macro_expansion_directory,
const_values_files = compile_outputs.const_values_files,
return struct(
module_context = module_context,
compilation_outputs = compilation_outputs,
supplemental_outputs = struct(
ast_files = compile_outputs.ast_files,
const_values_files = compile_outputs.const_values_files,
indexstore_directory = compile_outputs.indexstore_directory,
macro_expansion_directory = compile_outputs.macro_expansion_directory,
),
)

return module_context, cc_compilation_outputs, other_compilation_outputs

def precompile_clang_module(
*,
actions,
Expand Down
20 changes: 10 additions & 10 deletions swift/internal/feature_names.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ SWIFT_FEATURE_FILE_PREFIX_MAP = "swift.file_prefix_map"
# graph.
SWIFT_FEATURE_EMIT_C_MODULE = "swift.emit_c_module"

# If enabled, the compilation action for a target will produce an index store.
# https://docs.google.com/document/d/1cH2sTpgSnJZCkZtJl1aY-rzy4uGPcrI-6RrUpdATO2Q/
SWIFT_FEATURE_INDEX_WHILE_BUILDING = "swift.index_while_building"

# If enabled the compilation action will not produce indexes for system modules.
SWIFT_FEATURE_DISABLE_SYSTEM_INDEX = "swift.disable_system_index"

# Index while building - using a global index store cache
SWIFT_FEATURE_USE_GLOBAL_INDEX_STORE = "swift.use_global_index_store"

# If enabled, when compiling an explicit C or Objectve-C module, every header
# included by the module being compiled must belong to one of the modules listed
# in its dependencies. This is ignored for system modules.
Expand Down Expand Up @@ -107,16 +117,6 @@ SWIFT_FEATURE_FULL_DEBUG_INFO = "swift.full_debug_info"
# Use CodeView debug information, which enables generation of PDBs for debugging.
SWIFT_FEATURE_CODEVIEW_DEBUG_INFO = "swift.codeview_debug_info"

# If enabled, the compilation action for a target will produce an index store.
# https://docs.google.com/document/d/1cH2sTpgSnJZCkZtJl1aY-rzy4uGPcrI-6RrUpdATO2Q/
SWIFT_FEATURE_INDEX_WHILE_BUILDING = "swift.index_while_building"

# If enabled the compilation action will not produce indexes for system modules.
SWIFT_FEATURE_DISABLE_SYSTEM_INDEX = "swift.disable_system_index"

# Index while building - using a global index store cache
SWIFT_FEATURE_USE_GLOBAL_INDEX_STORE = "swift.use_global_index_store"

# If enabled, compilation actions and module map generation will assume that the
# header paths in module maps are relative to the current working directory
# (i.e., the workspace root); if disabled, header paths in module maps are
Expand Down
4 changes: 2 additions & 2 deletions swift/internal/output_groups.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def supplemental_compilation_output_groups(*supplemental_outputs):
ast_files.extend(outputs.ast_files)
if outputs.const_values_files:
const_values_files.extend(outputs.const_values_files)
if outputs.indexstore:
indexstore_files.append(outputs.indexstore)
if outputs.indexstore_directory:
indexstore_files.append(outputs.indexstore_directory)
if outputs.macro_expansion_directory:
macro_expansions_files.append(outputs.macro_expansion_directory)

Expand Down
21 changes: 10 additions & 11 deletions swift/swift_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ def _swift_binary_impl(ctx):
)

srcs = ctx.files.srcs
output_groups = {}
module_contexts = []
additional_linking_contexts = []
all_supplemental_outputs = []

# If the binary has sources, compile those first and collect the outputs to
# be passed to the linker.
Expand All @@ -82,7 +82,7 @@ def _swift_binary_impl(ctx):

include_dev_srch_paths = include_developer_search_paths(ctx.attr)

module_context, cc_compilation_outputs, supplemental_outputs = swift_common.compile(
compile_result = swift_common.compile(
actions = ctx.actions,
additional_inputs = ctx.files.swiftc_inputs,
cc_infos = get_providers(ctx.attr.deps, CcInfo),
Expand All @@ -104,16 +104,21 @@ def _swift_binary_impl(ctx):
target_name = ctx.label.name,
workspace_name = ctx.workspace_name,
)
module_context = compile_result.module_context
module_contexts.append(module_context)
all_supplemental_outputs.append(supplemental_outputs)
compilation_outputs = compile_result.compilation_outputs
supplemental_outputs = compile_result.supplemental_outputs
output_groups = supplemental_compilation_output_groups(
supplemental_outputs,
)

# Unlike `upstream`, we create a linking_context in order to support
# `autolink-extract`. See `a1395155c6a27d76aab5e1a93455259a0ac10b2f` and
# `93219a3b21390f212f5fd013e8db3654fd09814c`.
linking_context, _ = swift_common.create_linking_context_from_compilation_outputs(
actions = ctx.actions,
alwayslink = True,
compilation_outputs = cc_compilation_outputs,
compilation_outputs = compilation_outputs,
feature_configuration = feature_configuration,
include_dev_srch_paths = include_dev_srch_paths,
label = ctx.label,
Expand All @@ -126,8 +131,6 @@ def _swift_binary_impl(ctx):
swift_toolchain = swift_toolchain,
)
additional_linking_contexts.append(linking_context)
else:
cc_compilation_outputs = cc_common.create_compilation_outputs()

additional_linking_contexts.append(malloc_linking_context(ctx))

Expand Down Expand Up @@ -172,11 +175,7 @@ def _swift_binary_impl(ctx):
files = ctx.files.data,
),
),
OutputGroupInfo(
**supplemental_compilation_output_groups(
*all_supplemental_outputs
)
),
OutputGroupInfo(**output_groups),
swift_common.create_swift_info(
modules = [
swift_common.create_module(
Expand Down
16 changes: 9 additions & 7 deletions swift/swift_compiler_plugin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _swift_compiler_plugin_impl(ctx):
module_name = derive_swift_module_name(ctx.label)
entry_point_function_name = "{}_main".format(module_name)

module_context, cc_compilation_outputs, supplemental_outputs = swift_common.compile(
compile_result = swift_common.compile(
actions = ctx.actions,
additional_inputs = ctx.files.swiftc_inputs,
cc_infos = get_providers(deps, CcInfo),
Expand Down Expand Up @@ -103,9 +103,9 @@ def _swift_compiler_plugin_impl(ctx):
target_name = ctx.label.name,
workspace_name = ctx.workspace_name,
)
output_groups = supplemental_compilation_output_groups(
supplemental_outputs,
)
module_context = compile_result.module_context
compilation_outputs = compile_result.compilation_outputs
supplemental_outputs = compile_result.supplemental_outputs

cc_feature_configuration = swift_common.cc_feature_configuration(
feature_configuration = feature_configuration,
Expand All @@ -116,7 +116,7 @@ def _swift_compiler_plugin_impl(ctx):
additional_inputs = ctx.files.swiftc_inputs,
additional_linking_contexts = [malloc_linking_context(ctx)],
cc_feature_configuration = cc_feature_configuration,
compilation_outputs = cc_compilation_outputs,
compilation_outputs = compilation_outputs,
deps = deps,
name = ctx.label.name,
output_type = "executable",
Expand All @@ -141,7 +141,7 @@ def _swift_compiler_plugin_impl(ctx):
actions = ctx.actions,
additional_inputs = ctx.files.swiftc_inputs,
alwayslink = True,
compilation_outputs = cc_compilation_outputs,
compilation_outputs = compilation_outputs,
feature_configuration = feature_configuration,
label = ctx.label,
include_dev_srch_paths = ctx.attr.testonly,
Expand All @@ -166,7 +166,9 @@ def _swift_compiler_plugin_impl(ctx):
files = ctx.files.data,
),
),
OutputGroupInfo(**output_groups),
OutputGroupInfo(
**supplemental_compilation_output_groups(supplemental_outputs)
),
SwiftCompilerPluginInfo(
cc_info = CcInfo(
compilation_context = module_context.clang.compilation_context,
Expand Down
14 changes: 9 additions & 5 deletions swift/swift_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def _swift_library_impl(ctx):

include_dev_srch_paths = include_developer_search_paths(ctx.attr)

module_context, cc_compilation_outputs, supplemental_outputs = swift_common.compile(
compile_result = swift_common.compile(
actions = ctx.actions,
additional_inputs = additional_inputs,
cc_infos = get_providers(ctx.attr.deps, CcInfo),
Expand All @@ -204,12 +204,16 @@ def _swift_library_impl(ctx):
workspace_name = ctx.workspace_name,
)

module_context = compile_result.module_context
compilation_outputs = compile_result.compilation_outputs
supplemental_outputs = compile_result.supplemental_outputs

linking_context, linking_output = (
swift_common.create_linking_context_from_compilation_outputs(
actions = ctx.actions,
additional_inputs = additional_inputs,
alwayslink = ctx.attr.alwayslink,
compilation_outputs = cc_compilation_outputs,
compilation_outputs = compilation_outputs,
feature_configuration = feature_configuration,
include_dev_srch_paths = include_dev_srch_paths,
label = ctx.label,
Expand Down Expand Up @@ -255,9 +259,6 @@ def _swift_library_impl(ctx):
files = ctx.files.data,
),
),
OutputGroupInfo(**supplemental_compilation_output_groups(
supplemental_outputs,
)),
CcInfo(
compilation_context = module_context.clang.compilation_context,
linking_context = linking_context,
Expand All @@ -274,6 +275,9 @@ def _swift_library_impl(ctx):
# not propagate.
swift_infos = swift_infos,
),
OutputGroupInfo(
**supplemental_compilation_output_groups(supplemental_outputs)
),
]

# Propagate an `apple_common.Objc` provider with linking info about the
Expand Down
Loading

0 comments on commit 359a3c1

Please sign in to comment.