The swift_common
module provides API access to the behavior implemented
by the Swift build rules, so that other custom rules can invoke Swift
compilation and/or linking as part of their implementation.
swift_common.cc_feature_configuration(feature_configuration)
Returns the C++ feature configuration in a Swift feature configuration.
PARAMETERS
Name | Description | Default Value |
---|---|---|
feature_configuration | The Swift feature configuration, as returned from swift_common.configure_features . |
none |
RETURNS
A C++ FeatureConfiguration
value (see
cc_common.configure_features
for more information).
swift_common.compilation_attrs(additional_deps_aspects, requires_srcs)
Returns an attribute dictionary for rules that compile Swift code.
The returned dictionary contains the subset of attributes that are shared by
the swift_binary
, swift_library
, and swift_test
rules that deal with
inputs and options for compilation. Users who are authoring custom rules
that compile Swift code but not as a library can add this dictionary to
their own rule's attributes to give it a familiar API.
Do note, however, that it is the responsibility of the rule implementation
to retrieve the values of those attributes and pass them correctly to the
other swift_common
APIs.
There is a hierarchy to the attribute sets offered by the swift_common
API:
- If you only need access to the toolchain for its tools and libraries but
are not doing any compilation, use
toolchain_attrs
. - If you need to invoke compilation actions but are not making the
resulting object files into a static or shared library, use
compilation_attrs
. - If you want to provide a rule interface that is suitable as a drop-in
replacement for
swift_library
, uselibrary_rule_attrs
.
Each of the attribute functions in the list above also contains the attributes from the earlier items in the list.
PARAMETERS
RETURNS
A new attribute dictionary that can be added to the attributes of a
custom build rule to provide a similar interface to swift_binary
,
swift_library
, and swift_test
.
swift_common.compile(actions, feature_configuration, module_name, srcs, swift_toolchain, target_name, workspace_name, additional_inputs, bin_dir, copts, defines, deps, generated_header_name, genfiles_dir, private_deps)
Compiles a Swift module.
PARAMETERS
RETURNS
A struct
containing the following fields:
generated_header
: AFile
representing the Objective-C header that was generated for the compiled module. If no header was generated, this field will be None.generated_header_module_map
: AFile
representing the module map that was generated to correspond to the generated Objective-C header. If no module map was generated, this field will be None.indexstore
: AFile
representing the directory that contains the index store data generated by the compiler if index-while-building is enabled. May be None if no indexing was requested.linker_flags
: A list of strings representing additional flags that should be passed to the linker when linking these objects into a binary. If there are none, this field will always be an empty list, never None.linker_inputs
: A list ofFile
s representing additional input files (such as those referenced inlinker_flags
) that need to be available to the link action when linking these objects into a binary. If there are none, this field will always be an empty list, never None.object_files
: A list of.o
files that were produced by the compiler.precompiled_module
: AFile
representing the explicit module (.pcm
) of the Clang module for the generated header, orNone
if no explicit module was generated.swiftdoc
: The.swiftdoc
file that was produced by the compiler.swiftinterface
: The.swiftinterface
file that was produced by the compiler. If no interface file was produced (because the toolchain does not support them or it was not requested), this field will be None.swiftmodule
: The.swiftmodule
file that was produced by the compiler.
swift_common.configure_features(ctx, swift_toolchain, requested_features, unsupported_features)
Creates a feature configuration to be passed to Swift build APIs.
This function calls through to cc_common.configure_features
to configure
underlying C++ features as well, and nests the C++ feature configuration
inside the Swift one. Users who need to call C++ APIs that require a feature
configuration can extract it by calling
swift_common.cc_feature_configuration(feature_configuration)
.
PARAMETERS
RETURNS
An opaque value representing the feature configuration that can be
passed to other swift_common
functions. Note that the structure of
this value should otherwise not be relied on or inspected directly.
swift_common.create_clang_module(compilation_context, module_map, precompiled_module)
Creates a value representing a Clang module used as a Swift dependency.
PARAMETERS
RETURNS
A struct
containing the compilation_context
, module_map
, and
precompiled_module
fields provided as arguments.
swift_common.create_module(name, clang, is_system, swift)
Creates a value containing Clang/Swift module artifacts of a dependency.
At least one of the clang
and swift
arguments must not be None
. It is
valid for both to be present; this is the case for most Swift modules, which
provide both Swift module artifacts as well as a generated header/module map
for Objective-C targets to depend on.
PARAMETERS
RETURNS
A struct
containing the name
, clang
, is_system
, and swift
fields provided as arguments.
swift_common.create_swift_info(direct_swift_infos, modules, swift_infos)
Creates a new SwiftInfo
provider with the given values.
This function is recommended instead of directly creating a SwiftInfo
provider because it encodes reasonable defaults for fields that some rules
may not be interested in and ensures that the direct and transitive fields
are set consistently.
This function can also be used to do a simple merge of SwiftInfo
providers, by leaving the modules
argument unspecified. In that case, the
returned provider will not represent a true Swift module; it is merely a
"collector" for other dependencies.
PARAMETERS
RETURNS
A new SwiftInfo
provider with the given values.
swift_common.create_swift_interop_info(module_map, module_name, requested_features, swift_infos, unsupported_features)
Returns a provider that lets a target expose C/Objective-C APIs to Swift.
The provider returned by this function allows custom build rules written in
Starlark to be uninvolved with much of the low-level machinery involved in
making a Swift-compatible module. Such a target should propagate a CcInfo
provider whose compilation context contains the headers that it wants to
make into a module, and then also propagate the provider returned from this
function.
The simplest usage is for a custom rule to call
swift_common.create_swift_interop_info
passing it only the list of
SwiftInfo
providers from its dependencies; this tells
swift_clang_module_aspect
to derive the module name from the target label
and create a module map using the headers from the compilation context.
If the custom rule has reason to provide its own module name or module map,
then it can do so using the module_name
and module_map
arguments.
When a rule returns this provider, it must provide the full set of
SwiftInfo
providers from dependencies that will be merged with the one
that swift_clang_module_aspect
creates for the target itself; the aspect
will not do so automatically. This allows the rule to not only add extra
dependencies (such as support libraries from implicit attributes) but also
exclude dependencies if necessary.
PARAMETERS
RETURNS
A provider whose type/layout is an implementation detail and should not be relied upon.
swift_common.create_swift_module(swiftdoc, swiftmodule, defines, swiftinterface)
Creates a value representing a Swift module use as a Swift dependency.
PARAMETERS
RETURNS
A struct
containing the defines
, swiftdoc
, swiftmodule
, and
swiftinterface
fields provided as arguments.
swift_common.derive_module_name(args)
Returns a derived module name from the given build label.
For targets whose module name is not explicitly specified, the module name is computed using the following algorithm:
- The package and name components of the label are considered separately.
All interior sequences of non-identifier characters (anything other
than
a-z
,A-Z
,0-9
, and_
) are replaced by a single underscore (_
). Any leading or trailing non-identifier characters are dropped. - If the package component is non-empty after the above transformation, it is joined with the transformed name component using an underscore. Otherwise, the transformed name is used by itself.
- If this would result in a string that begins with a digit (
0-9
), an underscore is prepended to make it identifier-safe.
This mapping is intended to be fairly predictable, but not reversible.
PARAMETERS
Name | Description | Default Value |
---|---|---|
args | Either a single argument of type Label , or two arguments of type str where the first argument is the package name and the second argument is the target name. |
none |
RETURNS
The module name derived from the label.
swift_common.is_enabled(feature_configuration, feature_name)
Returns True
if the feature is enabled in the feature configuration.
This function handles both Swift-specific features and C++ features so that users do not have to manually extract the C++ configuration in order to check it.
PARAMETERS
Name | Description | Default Value |
---|---|---|
feature_configuration | The Swift feature configuration, as returned by swift_common.configure_features . |
none |
feature_name | The name of the feature to check. | none |
RETURNS
True
if the given feature is enabled in the feature configuration.
swift_common.library_rule_attrs(additional_deps_aspects, requires_srcs)
Returns an attribute dictionary for swift_library
-like rules.
The returned dictionary contains the same attributes that are defined by the
swift_library
rule (including the private _toolchain
attribute that
specifies the toolchain dependency). Users who are authoring custom rules
can use this dictionary verbatim or add other custom attributes to it in
order to make their rule a drop-in replacement for swift_library
(for
example, if writing a custom rule that does some preprocessing or generation
of sources and then compiles them).
Do note, however, that it is the responsibility of the rule implementation
to retrieve the values of those attributes and pass them correctly to the
other swift_common
APIs.
There is a hierarchy to the attribute sets offered by the swift_common
API:
- If you only need access to the toolchain for its tools and libraries but
are not doing any compilation, use
toolchain_attrs
. - If you need to invoke compilation actions but are not making the
resulting object files into a static or shared library, use
compilation_attrs
. - If you want to provide a rule interface that is suitable as a drop-in
replacement for
swift_library
, uselibrary_rule_attrs
.
Each of the attribute functions in the list above also contains the attributes from the earlier items in the list.
PARAMETERS
RETURNS
A new attribute dictionary that can be added to the attributes of a
custom build rule to provide the same interface as swift_library
.
swift_common.precompile_clang_module(actions, cc_compilation_context, feature_configuration, module_map_file, module_name, swift_toolchain, target_name, bin_dir, genfiles_dir, swift_info)
Precompiles an explicit Clang module that is compatible with Swift.
PARAMETERS
RETURNS
A File
representing the precompiled module (.pcm
) file, or None
if
the toolchain or target does not support precompiled modules.
swift_common.swift_runtime_linkopts(is_static, toolchain, is_test)
Returns the flags that should be passed when linking a Swift binary.
This function provides the appropriate linker arguments to callers who need
to link a binary using something other than swift_binary
(for example, an
application bundle containing a universal apple_binary
).
PARAMETERS
RETURNS
A list
of command line flags that should be passed when linking a
binary against the Swift runtime libraries.
swift_common.toolchain_attrs(toolchain_attr_name)
Returns an attribute dictionary for toolchain users.
The returned dictionary contains a key with the name specified by the
argument toolchain_attr_name
(which defaults to the value "_toolchain"
),
the value of which is a BUILD API attr.label
that references the default
Swift toolchain. Users who are authoring custom rules can add this
dictionary to the attributes of their own rule in order to depend on the
toolchain and access its SwiftToolchainInfo
provider to pass it to other
swift_common
functions.
There is a hierarchy to the attribute sets offered by the swift_common
API:
- If you only need access to the toolchain for its tools and libraries but
are not doing any compilation, use
toolchain_attrs
. - If you need to invoke compilation actions but are not making the
resulting object files into a static or shared library, use
compilation_attrs
. - If you want to provide a rule interface that is suitable as a drop-in
replacement for
swift_library
, uselibrary_rule_attrs
.
Each of the attribute functions in the list above also contains the attributes from the earlier items in the list.
PARAMETERS
RETURNS
A new attribute dictionary that can be added to the attributes of a custom build rule to provide access to the Swift toolchain.