Skip to content

Commit

Permalink
introduce a new flag --experimental_cpp20_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
PikachuHyA committed May 27, 2024
1 parent f1f50e6 commit c25e786
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -880,10 +880,24 @@ public boolean experimentalCcImplementationDepsForStarlark(StarlarkThread thread
return experimentalCcImplementationDeps();
}

@StarlarkMethod(
name = "experimental_cpp20_modules",
documented = false,
useStarlarkThread = true)
public boolean experimentalCpp20ModulesForStarlark(StarlarkThread thread)
throws EvalException {
CcModule.checkPrivateStarlarkificationAllowlist(thread);
return experimentalCpp20Modules();
}

public boolean experimentalCcImplementationDeps() {
return cppOptions.experimentalCcImplementationDeps;
}

public boolean experimentalCpp20Modules() {
return cppOptions.experimentalCpp20Modules;
}

public boolean getExperimentalCppCompileResourcesEstimation() {
return cppOptions.experimentalCppCompileResourcesEstimation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,19 @@ public Label getMemProfProfileLabel() {
help = "If enabled, cc_library targets can use attribute `implementation_deps`.")
public boolean experimentalCcImplementationDeps;

@Option(
name = "experimental_cpp20_modules",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
effectTags = {
OptionEffectTag.LOADING_AND_ANALYSIS,
OptionEffectTag.EXECUTION,
OptionEffectTag.CHANGES_INPUTS
},
metadataTags = {OptionMetadataTag.EXPERIMENTAL},
help = "If enabled, cc_binary and cc_library targets can use attribute `module_interfaces`.")
public boolean experimentalCpp20Modules;

@Option(
name = "experimental_link_static_libraries_once",
defaultValue = "true",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ bazel_fragments["CppOptions"] = fragment(
"//command_line_option:target libcTop label",
"//command_line_option:experimental_link_static_libraries_once",
"//command_line_option:experimental_cc_implementation_deps",
"//command_line_option:experimental_cpp20_modules",
"//command_line_option:start_end_lib",
"//command_line_option:experimental_inmemory_dotd_files",
"//command_line_option:incompatible_disable_legacy_cc_provider",
Expand Down
2 changes: 2 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/attrs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ C++ Standard has no restriction about module interface file extension
<li>MSVC use ixx </li>
</ul>
</p>
<p>For now usage is guarded by the flag
<code>--experimental_cpp20_modules</code>.</p>
""",
),
"data": attr.label_list(
Expand Down
3 changes: 2 additions & 1 deletion src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ def cc_binary_impl(ctx, additional_linkopts, force_linkstatic = False):
unsupported_features = disabled_features,
)

cc_helper.check_cpp20_module(ctx, feature_configuration)
semantics.check_can_module_interfaces(ctx)
cc_helper.check_cpp20_modules(ctx, feature_configuration)

all_deps = ctx.attr.deps + semantics.get_cc_runtimes(ctx, _is_link_shared(ctx))
compilation_context_deps = [dep[CcInfo].compilation_context for dep in all_deps if CcInfo in dep]
Expand Down
7 changes: 4 additions & 3 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,6 @@ def _map_to_list(m):
result.append((k, v))
return result


# "srcs" attribute is a LABEL_LIST in cc_rules, which might also contain files.
def _calculate_artifact_label_map(srcs, attr):
artifact_label_map = {}
Expand All @@ -971,6 +970,7 @@ def _calculate_artifact_label_map(srcs, attr):
attr = attr,
)
return artifact_label_map

# Returns a list of (Artifact, Label) tuples. Each tuple represents an input source
# file and the label of the rule that generates it (or the label of the source file itself if it
# is an input file).
Expand Down Expand Up @@ -1216,7 +1216,8 @@ def _should_use_pic(ctx, cc_toolchain, feature_configuration):
cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "prefer_pic_for_opt_binaries")
)
)
def _check_cpp20_module(ctx, feature_configuration):

def _check_cpp20_modules(ctx, feature_configuration):
if len(ctx.files.module_interfaces) > 0 and not cc_common.is_enabled(
feature_configuration = feature_configuration,
feature_name = "cpp20_modules",
Expand Down Expand Up @@ -1290,5 +1291,5 @@ cc_helper = struct(
package_source_root = _package_source_root,
tokenize = _tokenize,
should_use_pic = _should_use_pic,
check_cpp20_module = _check_cpp20_module,
check_cpp20_modules = _check_cpp20_modules,
)
3 changes: 2 additions & 1 deletion src/main/starlark/builtins_bzl/common/cc/cc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def _cc_library_impl(ctx):
unsupported_features = ctx.disabled_features,
)

cc_helper.check_cpp20_module(ctx, feature_configuration)
semantics.check_can_module_interfaces(ctx)
cc_helper.check_cpp20_modules(ctx, feature_configuration)

precompiled_files = cc_helper.build_precompiled_files(ctx = ctx)

Expand Down
6 changes: 6 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/semantics.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ def _check_can_use_implementation_deps(ctx):
if (not experimental_cc_implementation_deps and ctx.attr.implementation_deps):
fail("requires --experimental_cc_implementation_deps", attr = "implementation_deps")

def _check_can_module_interfaces(ctx):
experimental_cpp20_modules = ctx.fragments.cpp.experimental_cpp20_modules()
if (not experimental_cpp20_modules and ctx.attr.module_interfaces):
fail("requires --experimental_cpp20_modules", attr = "module_interfaces")

_WINDOWS_PLATFORM = Label("@platforms//os:windows") # Resolve the label within builtins context

def _get_linkstatic_default_for_test():
Expand Down Expand Up @@ -161,6 +166,7 @@ semantics = struct(
get_grep_includes = _get_grep_includes,
get_implementation_deps_allowed_attr = _get_implementation_deps_allowed_attr,
check_can_use_implementation_deps = _check_can_use_implementation_deps,
check_can_module_interfaces = _check_can_module_interfaces,
get_linkstatic_default_for_test = _get_linkstatic_default_for_test,
get_runtimes_toolchain = _get_runtimes_toolchain,
get_test_malloc_attr = _get_test_malloc_attr,
Expand Down

0 comments on commit c25e786

Please sign in to comment.