Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 72 additions & 4 deletions cc/private/cc_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,91 @@ _ModuleMapInfo = provider(
"ModuleMapInfo",
fields = {
"file": "The module map file.",
"name": "The name of the module.",
"name": "The name of the module or None if the module is named after the label of the target that generated the module map file (see get_module_map_name).",
},
)

def create_module_map(*, file, name):
def create_module_map(*, file, name = None):
"""
Creates a module map struct.

Args:
file: The module map file.
name: The name of the module.
name: The name of the module. If None (the default), the module is
named after the label of the target that generated the module map
file. Prefer the default: it avoids retaining a name string per
module map as the name is derived on demand from `file.owner`.
Returns:
A module map struct.
"""
check_private_api()
return _ModuleMapInfo(file = file, name = name)

def module_map_name_for_label(label):
"""
Returns the module name for a module map named after the given label.

The name is the label's unambiguous canonical form, except that
main-repository labels do not carry the leading double-at prefix (see
get_module_map_name).

Args:
label: The Label the module is named after.
Returns:
The name of the module as a string.
"""
label_string = str(label)

# buildifier: disable=canonical-repository
if label_string.startswith("@@//"):
return label_string[2:]
return label_string

def get_module_map_name(module_map):
"""
Returns the name of the module described by a module map struct.

Module maps generated for a target are named after the target's label,
rendered in its unambiguous canonical form, except that main-repository
labels do not carry the leading double-at prefix (for example "//pkg:lib"
for a main-repository target). Names of targets in external repositories
are thus valid label strings, while main-repository names match the
traditional module name format.

Args:
module_map: The module map struct.
Returns:
The name of the module as a string.
"""
if module_map.name != None:
return module_map.name
return module_map_name_for_label(module_map.file.owner)

def get_module_map_label(module_map):
"""
Returns the label a module map struct is named after.

This is only valid for module maps whose name is (derived from) a label,
which is the case for all module maps generated for a target, including
separate module maps. It is not valid for module maps with special names
such as the crosstool module map.

Args:
module_map: The module map struct.
Returns:
The Label the module's name refers to.
"""
if module_map.name == None:
return module_map.file.owner

# Module map names of main-repository targets lack the leading "@@" (see
# get_module_map_name), which has to be restored for parsing to ensure
# that the name is not resolved relative to this file's repository.
name = module_map.name

# buildifier: disable=canonical-repository
return Label(name if name.startswith("@") else "@@" + name)

def create_separate_module_map(module_map):
"""
Creates a separate module map struct.
Expand All @@ -178,7 +246,7 @@ def create_separate_module_map(module_map):
Returns:
A module map struct.
"""
return _ModuleMapInfo(file = module_map.file, name = module_map.name + ".sep")
return _ModuleMapInfo(file = module_map.file, name = get_module_map_name(module_map) + ".sep")

def create_linking_context(
*,
Expand Down
25 changes: 15 additions & 10 deletions cc/private/compile/cc_compilation_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ load(
"repository_exec_path",
)
load("//cc/common:semantics.bzl", "STRIP_INCLUDE_PREFIX_APPLIES_TO_TEXTUAL_HEADERS", "USE_EXEC_ROOT_FOR_VIRTUAL_INCLUDES_SYMLINKS")
load("//cc/private:cc_info.bzl", "create_compilation_context", "create_module_map")
load("//cc/private:cc_info.bzl", "create_compilation_context", "create_module_map", "get_module_map_name", "module_map_name_for_label")
load("//cc/private:cc_internal.bzl", _cc_internal = "cc_internal")

_VIRTUAL_INCLUDES_DIR = "_virtual_includes"
Expand Down Expand Up @@ -254,7 +254,8 @@ _ModuleMapInfo = provider(
def _module_map_struct_to_module_map_content(parameters, tree_expander):
lines = []
module_map = parameters.module_map
lines.append("module \"%s\" {" % module_map.name)
module_name = get_module_map_name(module_map)
lines.append("module \"%s\" {" % module_name)
lines.append(" export *")

def expanded(artifacts):
Expand Down Expand Up @@ -326,10 +327,10 @@ def _module_map_struct_to_module_map_content(parameters, tree_expander):

dependency_module_maps = parameters.dependency_module_maps.to_list()
for dep in dependency_module_maps:
lines.append(" use \"" + dep.name + "\"")
lines.append(" use \"" + get_module_map_name(dep) + "\"")

if parameters.separate_module_headers:
separate_name = module_map.name + ".sep"
separate_name = module_name + ".sep"
lines.append(" use \"" + separate_name + "\"")
lines.append("}")
lines.append("module \"" + separate_name + "\" {")
Expand All @@ -343,14 +344,14 @@ def _module_map_struct_to_module_map_content(parameters, tree_expander):
added_paths.add(header.path)

for dep in dependency_module_maps:
lines.append(" use \"" + dep.name + "\"")
lines.append(" use \"" + get_module_map_name(dep) + "\"")

lines.append("}")

if parameters.extern_dependencies:
for dep in dependency_module_maps:
lines.append(
"extern module \"" + dep.name + "\" \"" +
"extern module \"" + get_module_map_name(dep) + "\" \"" +
parameters.leading_periods + dep.file.path + "\"",
)

Expand Down Expand Up @@ -565,10 +566,14 @@ def _init_cc_compilation_context(
header_module = None
if _enabled(feature_configuration, "module_maps"):
if not module_map:
module_map = create_module_map(
file = actions.declare_file(label.name + ".cppmap"),
name = label.workspace_name + "//" + label.package + ":" + label.name,
)
file = actions.declare_file(label.name + ".cppmap")

# If compile() is called with a name that differs from the
# declaring target's name (e.g. by an aspect), the module name
# cannot be derived from the file's owner and has to be stored
# explicitly.
name = None if file.owner == label else module_map_name_for_label(label)
module_map = create_module_map(file = file, name = name)

# There are different modes for module compilation:
# 1. We create the module map and compile the module so that libraries depending on us can
Expand Down
44 changes: 14 additions & 30 deletions cc/private/compile/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ load(
"create_cc_compilation_context_with_cpp20_modules",
"create_compilation_context_with_extra_header_tokens",
"create_separate_module_map",
"get_module_map_label",
"get_module_map_name",
)
load("//cc/private:cc_internal.bzl", _cc_internal = "cc_internal")
load("//cc/private/compile:cc_compilation_helper.bzl", "cc_compilation_helper", "dotd_files_enabled", "serialized_diagnostics_file_enabled")
Expand Down Expand Up @@ -543,13 +545,10 @@ def _create_scan_deps_action(
),
)
specific_compile_build_variables = get_specific_compile_build_variables(
feature_configuration,
use_pic = use_pic,
source_file = source_artifact,
output_file = ddi_file,
dotd_file = dotd_file,
cpp_module_map = cc_compilation_context._module_map,
direct_module_maps = cc_compilation_context._direct_module_maps,
user_compile_flags = get_copts(
language = language,
cpp_configuration = cpp_configuration,
Expand Down Expand Up @@ -846,7 +845,6 @@ def _create_cc_compile_actions_with_cpp20_module_helper(
common_compile_variables = common_compile_build_variables,
fdo_build_variables = fdo_build_variables,
output_category = artifact_category.CLIF_OUTPUT_PROTO if cpp_source.type == CPP_SOURCE_TYPE_CLIF_INPUT_PROTO else artifact_category.OBJECT_FILE,
cpp_module_map = cc_compilation_context._module_map,
add_object = True,
enable_coverage = is_code_coverage_enabled,
generate_dwo = should_create_per_object_debug_info(feature_configuration, cpp_configuration),
Expand Down Expand Up @@ -994,7 +992,6 @@ def _create_cc_compile_actions_with_cpp20_module_helper(
common_compile_variables = common_compile_build_variables,
fdo_build_variables = fdo_build_variables,
output_category = artifact_category.CLIF_OUTPUT_PROTO if cpp_source.type == CPP_SOURCE_TYPE_CLIF_INPUT_PROTO else artifact_category.OBJECT_FILE,
cpp_module_map = cc_compilation_context._module_map,
add_object = True,
enable_coverage = is_code_coverage_enabled,
generate_dwo = should_create_per_object_debug_info(feature_configuration, cpp_configuration),
Expand Down Expand Up @@ -1165,7 +1162,7 @@ def _create_cc_compile_actions(

if _should_provide_header_modules(feature_configuration, private_headers, public_headers):
cpp_module_map = cc_compilation_context._module_map
module_map_label = Label(cpp_module_map.name)
module_map_label = get_module_map_label(cpp_module_map)
modules = _create_module_action(
action_construction_context = action_construction_context,
cc_compilation_context = cc_compilation_context,
Expand Down Expand Up @@ -1211,6 +1208,7 @@ def _create_cc_compile_actions(
fdo_build_variables = fdo_build_variables,
outputs = outputs,
cpp_module_map = separate_cpp_module_map,
module_name = get_module_map_name(separate_cpp_module_map),
language = language,
additional_compilation_inputs = [],
additional_include_scanning_roots = [],
Expand Down Expand Up @@ -1277,7 +1275,6 @@ def _create_cc_compile_actions(
common_compile_variables = common_compile_build_variables,
fdo_build_variables = fdo_build_variables,
output_category = (artifact_category.CLIF_OUTPUT_PROTO if source_type == CPP_SOURCE_TYPE_CLIF_INPUT_PROTO else artifact_category.OBJECT_FILE),
cpp_module_map = cc_compilation_context._module_map,
add_object = True,
enable_coverage = is_code_coverage_enabled,
generate_dwo = should_create_per_object_debug_info(feature_configuration, cpp_configuration),
Expand Down Expand Up @@ -1364,14 +1361,11 @@ def _create_cc_compile_actions(
),
) if serialized_diagnostics_file_enabled(feature_configuration) else None
specific_compile_build_variables = get_specific_compile_build_variables(
feature_configuration,
use_pic = generate_pic_action,
source_file = source_file,
output_file = output_file,
dotd_file = dotd_file,
diagnostics_file = diagnostics_file,
cpp_module_map = cc_compilation_context._module_map,
direct_module_maps = cc_compilation_context._direct_module_maps,
user_compile_flags = get_copts(
language = language,
cpp_configuration = cpp_configuration,
Expand Down Expand Up @@ -1429,7 +1423,6 @@ def _create_pic_nopic_compile_source_actions(
common_compile_variables,
fdo_build_variables,
output_category,
cpp_module_map,
add_object,
enable_coverage,
generate_dwo,
Expand All @@ -1441,7 +1434,8 @@ def _create_pic_nopic_compile_source_actions(
generate_pic_action,
generate_no_pic_action,
enable_dotd_files,
progress_message_prefix):
progress_message_prefix,
module_name = None):
results = []
if generate_pic_action:
pic_object = _create_compile_source_action(
Expand All @@ -1463,7 +1457,7 @@ def _create_pic_nopic_compile_source_actions(
common_compile_variables = common_compile_variables,
fdo_build_variables = fdo_build_variables,
output_category = output_category,
cpp_module_map = cpp_module_map,
module_name = module_name,
add_object = add_object,
enable_coverage = enable_coverage,
generate_dwo = generate_dwo,
Expand Down Expand Up @@ -1500,7 +1494,7 @@ def _create_pic_nopic_compile_source_actions(
common_compile_variables = common_compile_variables,
fdo_build_variables = fdo_build_variables,
output_category = output_category,
cpp_module_map = cpp_module_map,
module_name = module_name,
add_object = add_object,
enable_coverage = enable_coverage,
generate_dwo = generate_dwo,
Expand Down Expand Up @@ -1538,7 +1532,6 @@ def _create_compile_source_action(
common_compile_variables,
fdo_build_variables,
output_category,
cpp_module_map,
add_object,
enable_coverage,
generate_dwo,
Expand All @@ -1549,6 +1542,7 @@ def _create_compile_source_action(
additional_include_scanning_roots,
use_pic,
enable_dotd_files,
module_name = None,
additional_build_variables = {},
action_name = None,
additional_outputs = [],
Expand Down Expand Up @@ -1651,9 +1645,7 @@ def _create_compile_source_action(
dotd_file = dotd_file,
diagnostics_file = diagnostics_file,
use_pic = use_pic,
cpp_module_map = cpp_module_map,
feature_configuration = feature_configuration,
direct_module_maps = cc_compilation_context._direct_module_maps,
module_name = module_name,
fdo_build_variables = fdo_build_variables,
additional_build_variables = additional_build_variables,
)
Expand Down Expand Up @@ -1864,9 +1856,6 @@ def _create_temps_action(
dotd_file = preprocess_dotd_file,
diagnostics_file = preprocess_diagnostics_file,
use_pic = use_pic,
cpp_module_map = cc_compilation_context._module_map,
direct_module_maps = cc_compilation_context._direct_module_maps,
feature_configuration = feature_configuration,
fdo_build_variables = fdo_build_variables,
additional_build_variables = {"output_preprocess_file": preprocess_object_file.path},
)
Expand All @@ -1883,9 +1872,6 @@ def _create_temps_action(
dotd_file = assembly_dotd_file,
diagnostics_file = assembly_diagnostics_file,
use_pic = use_pic,
cpp_module_map = cc_compilation_context._module_map,
direct_module_maps = cc_compilation_context._direct_module_maps,
feature_configuration = feature_configuration,
fdo_build_variables = fdo_build_variables,
additional_build_variables = {"output_assembly_file": assembly_object_file.path},
)
Expand Down Expand Up @@ -2048,9 +2034,6 @@ def _create_module_codegen_action(
dotd_file = dotd_file,
diagnostics_file = diagnostics_file,
use_pic = use_pic,
cpp_module_map = cc_compilation_context._module_map,
feature_configuration = feature_configuration,
direct_module_maps = cc_compilation_context._direct_module_maps,
fdo_build_variables = fdo_build_variables,
additional_build_variables = {},
)
Expand Down Expand Up @@ -2121,8 +2104,9 @@ def _create_module_action(
additional_compilation_inputs,
additional_include_scanning_roots,
outputs,
progress_message_prefix):
module_map_label = Label(cpp_module_map.name)
progress_message_prefix,
module_name = None):
module_map_label = get_module_map_label(cpp_module_map)
return _create_pic_nopic_compile_source_actions(
action_construction_context = action_construction_context,
cc_compilation_context = cc_compilation_context,
Expand All @@ -2146,7 +2130,7 @@ def _create_module_action(
source_artifact = cpp_module_map.file,
language = language,
output_category = artifact_category.CPP_MODULE,
cpp_module_map = cpp_module_map,
module_name = module_name,
add_object = False,
enable_coverage = False,
generate_dwo = False,
Expand Down
3 changes: 0 additions & 3 deletions cc/private/compile/compile_action_templates.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,9 @@ def _create_compile_action_template(
use_pic = use_pic,
)
specific_compile_build_variables = get_specific_compile_build_variables(
feature_configuration,
use_pic = use_pic,
source_file = source_dir,
output_file = output_dir,
cpp_module_map = cc_compilation_context._module_map,
direct_module_maps = cc_compilation_context._direct_module_maps,
user_compile_flags = all_copts,
)
dotd_tree_artifact = _maybe_declare_dotd_tree_artifact(
Expand Down
Loading