Skip to content

Commit 3d6b6ac

Browse files
fmeumcopybara-github
authored andcommitted
Do not retain a name string for generated module maps
Copybara Import from #789 BEGIN_PUBLIC Do not retain a name string for generated module maps (#789) Module maps generated for a target are named after the target's label. Since the generated module map file is declared by that very target, the name can be derived on demand from the file's owner label, which the `File` object already retains. The `name` field of the module map struct becomes optional, with `None` meaning "named after the label of the target that generated the module map file", and two central helpers (`get_module_map_name`, `get_module_map_label`) derive the string form and the `Label` form where needed. This fixes the round-trip bug originally addressed in #633, but avoids the memory regression that caused it to be rolled back in b81b6e4 (because retaining `Label` objects increased memory usage overall). Deriving the name on demand retains nothing extra as the owner label is already retained by the `File` and all name materializations are transient only (such as in `map_each` callbacks). Names are now the unambiguous canonical label string, except that main repository labels drop the leading `@@` (`//pkg:lib`), keeping the traditional module name format. Special module maps (the crosstool module map, ObjC internal module maps) keep their explicitly provided names. Closes #789 END_PUBLIC Startblock: * // Put other blockers before this line to avoid churning. * has lgtm * is approved * and then * all comments are resolved * and then *[] allows unknown commit COPYBARA_INTEGRATE_REVIEW=#789 from fmeum:module-map-label 6884865 PiperOrigin-RevId: 964972527 Change-Id: If97a7c4491d670e825b621e80541a064ffe8769f
1 parent dcc3ad3 commit 3d6b6ac

5 files changed

Lines changed: 117 additions & 59 deletions

File tree

‎cc/private/cc_info.bzl‎

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,91 @@ _ModuleMapInfo = provider(
152152
"ModuleMapInfo",
153153
fields = {
154154
"file": "The module map file.",
155-
"name": "The name of the module.",
155+
"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).",
156156
},
157157
)
158158

159-
def create_module_map(*, file, name):
159+
def create_module_map(*, file, name = None):
160160
"""
161161
Creates a module map struct.
162162
163163
Args:
164164
file: The module map file.
165-
name: The name of the module.
165+
name: The name of the module. If None (the default), the module is
166+
named after the label of the target that generated the module map
167+
file. Prefer the default: it avoids retaining a name string per
168+
module map as the name is derived on demand from `file.owner`.
166169
Returns:
167170
A module map struct.
168171
"""
169172
check_private_api()
170173
return _ModuleMapInfo(file = file, name = name)
171174

175+
def module_map_name_for_label(label):
176+
"""
177+
Returns the module name for a module map named after the given label.
178+
179+
The name is the label's unambiguous canonical form, except that
180+
main-repository labels do not carry the leading double-at prefix (see
181+
get_module_map_name).
182+
183+
Args:
184+
label: The Label the module is named after.
185+
Returns:
186+
The name of the module as a string.
187+
"""
188+
label_string = str(label)
189+
190+
# buildifier: disable=canonical-repository
191+
if label_string.startswith("@@//"):
192+
return label_string[2:]
193+
return label_string
194+
195+
def get_module_map_name(module_map):
196+
"""
197+
Returns the name of the module described by a module map struct.
198+
199+
Module maps generated for a target are named after the target's label,
200+
rendered in its unambiguous canonical form, except that main-repository
201+
labels do not carry the leading double-at prefix (for example "//pkg:lib"
202+
for a main-repository target). Names of targets in external repositories
203+
are thus valid label strings, while main-repository names match the
204+
traditional module name format.
205+
206+
Args:
207+
module_map: The module map struct.
208+
Returns:
209+
The name of the module as a string.
210+
"""
211+
if module_map.name != None:
212+
return module_map.name
213+
return module_map_name_for_label(module_map.file.owner)
214+
215+
def get_module_map_label(module_map):
216+
"""
217+
Returns the label a module map struct is named after.
218+
219+
This is only valid for module maps whose name is (derived from) a label,
220+
which is the case for all module maps generated for a target, including
221+
separate module maps. It is not valid for module maps with special names
222+
such as the crosstool module map.
223+
224+
Args:
225+
module_map: The module map struct.
226+
Returns:
227+
The Label the module's name refers to.
228+
"""
229+
if module_map.name == None:
230+
return module_map.file.owner
231+
232+
# Module map names of main-repository targets lack the leading "@@" (see
233+
# get_module_map_name), which has to be restored for parsing to ensure
234+
# that the name is not resolved relative to this file's repository.
235+
name = module_map.name
236+
237+
# buildifier: disable=canonical-repository
238+
return Label(name if name.startswith("@") else "@@" + name)
239+
172240
def create_separate_module_map(module_map):
173241
"""
174242
Creates a separate module map struct.
@@ -178,7 +246,7 @@ def create_separate_module_map(module_map):
178246
Returns:
179247
A module map struct.
180248
"""
181-
return _ModuleMapInfo(file = module_map.file, name = module_map.name + ".sep")
249+
return _ModuleMapInfo(file = module_map.file, name = get_module_map_name(module_map) + ".sep")
182250

183251
def create_linking_context(
184252
*,

‎cc/private/compile/cc_compilation_helper.bzl‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ load(
2121
"repository_exec_path",
2222
)
2323
load("//cc/common:semantics.bzl", "STRIP_INCLUDE_PREFIX_APPLIES_TO_TEXTUAL_HEADERS", "USE_EXEC_ROOT_FOR_VIRTUAL_INCLUDES_SYMLINKS")
24-
load("//cc/private:cc_info.bzl", "create_compilation_context", "create_module_map")
24+
load("//cc/private:cc_info.bzl", "create_compilation_context", "create_module_map", "get_module_map_name", "module_map_name_for_label")
2525
load("//cc/private:cc_internal.bzl", _cc_internal = "cc_internal")
2626

2727
_VIRTUAL_INCLUDES_DIR = "_virtual_includes"
@@ -254,7 +254,8 @@ _ModuleMapInfo = provider(
254254
def _module_map_struct_to_module_map_content(parameters, tree_expander):
255255
lines = []
256256
module_map = parameters.module_map
257-
lines.append("module \"%s\" {" % module_map.name)
257+
module_name = get_module_map_name(module_map)
258+
lines.append("module \"%s\" {" % module_name)
258259
lines.append(" export *")
259260

260261
def expanded(artifacts):
@@ -326,10 +327,10 @@ def _module_map_struct_to_module_map_content(parameters, tree_expander):
326327

327328
dependency_module_maps = parameters.dependency_module_maps.to_list()
328329
for dep in dependency_module_maps:
329-
lines.append(" use \"" + dep.name + "\"")
330+
lines.append(" use \"" + get_module_map_name(dep) + "\"")
330331

331332
if parameters.separate_module_headers:
332-
separate_name = module_map.name + ".sep"
333+
separate_name = module_name + ".sep"
333334
lines.append(" use \"" + separate_name + "\"")
334335
lines.append("}")
335336
lines.append("module \"" + separate_name + "\" {")
@@ -343,14 +344,14 @@ def _module_map_struct_to_module_map_content(parameters, tree_expander):
343344
added_paths.add(header.path)
344345

345346
for dep in dependency_module_maps:
346-
lines.append(" use \"" + dep.name + "\"")
347+
lines.append(" use \"" + get_module_map_name(dep) + "\"")
347348

348349
lines.append("}")
349350

350351
if parameters.extern_dependencies:
351352
for dep in dependency_module_maps:
352353
lines.append(
353-
"extern module \"" + dep.name + "\" \"" +
354+
"extern module \"" + get_module_map_name(dep) + "\" \"" +
354355
parameters.leading_periods + dep.file.path + "\"",
355356
)
356357

@@ -566,10 +567,14 @@ def _init_cc_compilation_context(
566567
header_module = None
567568
if _enabled(feature_configuration, "module_maps"):
568569
if not module_map:
569-
module_map = create_module_map(
570-
file = actions.declare_file(label.name + ".cppmap"),
571-
name = label.workspace_name + "//" + label.package + ":" + label.name,
572-
)
570+
file = actions.declare_file(label.name + ".cppmap")
571+
572+
# If compile() is called with a name that differs from the
573+
# declaring target's name (e.g. by an aspect), the module name
574+
# cannot be derived from the file's owner and has to be stored
575+
# explicitly.
576+
name = None if file.owner == label else module_map_name_for_label(label)
577+
module_map = create_module_map(file = file, name = name)
573578

574579
# There are different modes for module compilation:
575580
# 1. We create the module map and compile the module so that libraries depending on us can

‎cc/private/compile/compile.bzl‎

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ load(
3636
"create_cc_compilation_context_with_cpp20_modules",
3737
"create_compilation_context_with_extra_header_tokens",
3838
"create_separate_module_map",
39+
"get_module_map_label",
40+
"get_module_map_name",
3941
)
4042
load("//cc/private:cc_internal.bzl", _cc_internal = "cc_internal")
4143
load("//cc/private/compile:cc_compilation_helper.bzl", "cc_compilation_helper", "dotd_files_enabled", "serialized_diagnostics_file_enabled")
@@ -545,13 +547,10 @@ def _create_scan_deps_action(
545547
),
546548
)
547549
specific_compile_build_variables = get_specific_compile_build_variables(
548-
feature_configuration,
549550
use_pic = use_pic,
550551
source_file = source_artifact,
551552
output_file = ddi_file,
552553
dotd_file = dotd_file,
553-
cpp_module_map = cc_compilation_context._module_map,
554-
direct_module_maps = cc_compilation_context._direct_module_maps,
555554
user_compile_flags = get_copts(
556555
language = language,
557556
cpp_configuration = cpp_configuration,
@@ -848,7 +847,6 @@ def _create_cc_compile_actions_with_cpp20_module_helper(
848847
common_compile_variables = common_compile_build_variables,
849848
fdo_build_variables = fdo_build_variables,
850849
output_category = artifact_category.CLIF_OUTPUT_PROTO if cpp_source.type == CPP_SOURCE_TYPE_CLIF_INPUT_PROTO else artifact_category.OBJECT_FILE,
851-
cpp_module_map = cc_compilation_context._module_map,
852850
add_object = True,
853851
enable_coverage = is_code_coverage_enabled,
854852
generate_dwo = should_create_per_object_debug_info(feature_configuration, cpp_configuration),
@@ -996,7 +994,6 @@ def _create_cc_compile_actions_with_cpp20_module_helper(
996994
common_compile_variables = common_compile_build_variables,
997995
fdo_build_variables = fdo_build_variables,
998996
output_category = artifact_category.CLIF_OUTPUT_PROTO if cpp_source.type == CPP_SOURCE_TYPE_CLIF_INPUT_PROTO else artifact_category.OBJECT_FILE,
999-
cpp_module_map = cc_compilation_context._module_map,
1000997
add_object = True,
1001998
enable_coverage = is_code_coverage_enabled,
1002999
generate_dwo = should_create_per_object_debug_info(feature_configuration, cpp_configuration),
@@ -1167,7 +1164,7 @@ def _create_cc_compile_actions(
11671164

11681165
if _should_provide_header_modules(feature_configuration, private_headers, public_headers):
11691166
cpp_module_map = cc_compilation_context._module_map
1170-
module_map_label = Label(cpp_module_map.name)
1167+
module_map_label = get_module_map_label(cpp_module_map)
11711168
modules = _create_module_action(
11721169
action_construction_context = action_construction_context,
11731170
cc_compilation_context = cc_compilation_context,
@@ -1213,6 +1210,7 @@ def _create_cc_compile_actions(
12131210
fdo_build_variables = fdo_build_variables,
12141211
outputs = outputs,
12151212
cpp_module_map = separate_cpp_module_map,
1213+
module_name = get_module_map_name(separate_cpp_module_map),
12161214
language = language,
12171215
additional_compilation_inputs = [],
12181216
additional_include_scanning_roots = [],
@@ -1279,7 +1277,6 @@ def _create_cc_compile_actions(
12791277
common_compile_variables = common_compile_build_variables,
12801278
fdo_build_variables = fdo_build_variables,
12811279
output_category = (artifact_category.CLIF_OUTPUT_PROTO if source_type == CPP_SOURCE_TYPE_CLIF_INPUT_PROTO else artifact_category.OBJECT_FILE),
1282-
cpp_module_map = cc_compilation_context._module_map,
12831280
add_object = True,
12841281
enable_coverage = is_code_coverage_enabled,
12851282
generate_dwo = should_create_per_object_debug_info(feature_configuration, cpp_configuration),
@@ -1366,14 +1363,11 @@ def _create_cc_compile_actions(
13661363
),
13671364
) if serialized_diagnostics_file_enabled(feature_configuration) else None
13681365
specific_compile_build_variables = get_specific_compile_build_variables(
1369-
feature_configuration,
13701366
use_pic = generate_pic_action,
13711367
source_file = source_file,
13721368
output_file = output_file,
13731369
dotd_file = dotd_file,
13741370
diagnostics_file = diagnostics_file,
1375-
cpp_module_map = cc_compilation_context._module_map,
1376-
direct_module_maps = cc_compilation_context._direct_module_maps,
13771371
user_compile_flags = get_copts(
13781372
language = language,
13791373
cpp_configuration = cpp_configuration,
@@ -1431,7 +1425,6 @@ def _create_pic_nopic_compile_source_actions(
14311425
common_compile_variables,
14321426
fdo_build_variables,
14331427
output_category,
1434-
cpp_module_map,
14351428
add_object,
14361429
enable_coverage,
14371430
generate_dwo,
@@ -1443,7 +1436,8 @@ def _create_pic_nopic_compile_source_actions(
14431436
generate_pic_action,
14441437
generate_no_pic_action,
14451438
enable_dotd_files,
1446-
progress_message_prefix):
1439+
progress_message_prefix,
1440+
module_name = None):
14471441
results = []
14481442
if generate_pic_action:
14491443
pic_object = _create_compile_source_action(
@@ -1465,7 +1459,7 @@ def _create_pic_nopic_compile_source_actions(
14651459
common_compile_variables = common_compile_variables,
14661460
fdo_build_variables = fdo_build_variables,
14671461
output_category = output_category,
1468-
cpp_module_map = cpp_module_map,
1462+
module_name = module_name,
14691463
add_object = add_object,
14701464
enable_coverage = enable_coverage,
14711465
generate_dwo = generate_dwo,
@@ -1502,7 +1496,7 @@ def _create_pic_nopic_compile_source_actions(
15021496
common_compile_variables = common_compile_variables,
15031497
fdo_build_variables = fdo_build_variables,
15041498
output_category = output_category,
1505-
cpp_module_map = cpp_module_map,
1499+
module_name = module_name,
15061500
add_object = add_object,
15071501
enable_coverage = enable_coverage,
15081502
generate_dwo = generate_dwo,
@@ -1540,7 +1534,6 @@ def _create_compile_source_action(
15401534
common_compile_variables,
15411535
fdo_build_variables,
15421536
output_category,
1543-
cpp_module_map,
15441537
add_object,
15451538
enable_coverage,
15461539
generate_dwo,
@@ -1551,6 +1544,7 @@ def _create_compile_source_action(
15511544
additional_include_scanning_roots,
15521545
use_pic,
15531546
enable_dotd_files,
1547+
module_name = None,
15541548
additional_build_variables = {},
15551549
action_name = None,
15561550
additional_outputs = [],
@@ -1661,9 +1655,7 @@ def _create_compile_source_action(
16611655
dotd_file = dotd_file,
16621656
diagnostics_file = diagnostics_file,
16631657
use_pic = use_pic,
1664-
cpp_module_map = cpp_module_map,
1665-
feature_configuration = feature_configuration,
1666-
direct_module_maps = cc_compilation_context._direct_module_maps,
1658+
module_name = module_name,
16671659
fdo_build_variables = fdo_build_variables,
16681660
additional_build_variables = additional_build_variables,
16691661
)
@@ -1885,9 +1877,6 @@ def _create_temps_action(
18851877
dotd_file = preprocess_dotd_file,
18861878
diagnostics_file = preprocess_diagnostics_file,
18871879
use_pic = use_pic,
1888-
cpp_module_map = cc_compilation_context._module_map,
1889-
direct_module_maps = cc_compilation_context._direct_module_maps,
1890-
feature_configuration = feature_configuration,
18911880
fdo_build_variables = fdo_build_variables,
18921881
additional_build_variables = {"output_preprocess_file": preprocess_object_file.path},
18931882
)
@@ -1904,9 +1893,6 @@ def _create_temps_action(
19041893
dotd_file = assembly_dotd_file,
19051894
diagnostics_file = assembly_diagnostics_file,
19061895
use_pic = use_pic,
1907-
cpp_module_map = cc_compilation_context._module_map,
1908-
direct_module_maps = cc_compilation_context._direct_module_maps,
1909-
feature_configuration = feature_configuration,
19101896
fdo_build_variables = fdo_build_variables,
19111897
additional_build_variables = {"output_assembly_file": assembly_object_file.path},
19121898
)
@@ -2069,9 +2055,6 @@ def _create_module_codegen_action(
20692055
dotd_file = dotd_file,
20702056
diagnostics_file = diagnostics_file,
20712057
use_pic = use_pic,
2072-
cpp_module_map = cc_compilation_context._module_map,
2073-
feature_configuration = feature_configuration,
2074-
direct_module_maps = cc_compilation_context._direct_module_maps,
20752058
fdo_build_variables = fdo_build_variables,
20762059
additional_build_variables = {},
20772060
)
@@ -2142,8 +2125,9 @@ def _create_module_action(
21422125
additional_compilation_inputs,
21432126
additional_include_scanning_roots,
21442127
outputs,
2145-
progress_message_prefix):
2146-
module_map_label = Label(cpp_module_map.name)
2128+
progress_message_prefix,
2129+
module_name = None):
2130+
module_map_label = get_module_map_label(cpp_module_map)
21472131
return _create_pic_nopic_compile_source_actions(
21482132
action_construction_context = action_construction_context,
21492133
cc_compilation_context = cc_compilation_context,
@@ -2167,7 +2151,7 @@ def _create_module_action(
21672151
source_artifact = cpp_module_map.file,
21682152
language = language,
21692153
output_category = artifact_category.CPP_MODULE,
2170-
cpp_module_map = cpp_module_map,
2154+
module_name = module_name,
21712155
add_object = False,
21722156
enable_coverage = False,
21732157
generate_dwo = False,

‎cc/private/compile/compile_action_templates.bzl‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,9 @@ def _create_compile_action_template(
170170
use_pic = use_pic,
171171
)
172172
specific_compile_build_variables = get_specific_compile_build_variables(
173-
feature_configuration,
174173
use_pic = use_pic,
175174
source_file = source_dir,
176175
output_file = output_dir,
177-
cpp_module_map = cc_compilation_context._module_map,
178-
direct_module_maps = cc_compilation_context._direct_module_maps,
179176
user_compile_flags = all_copts,
180177
)
181178
dotd_tree_artifact = _maybe_declare_dotd_tree_artifact(

0 commit comments

Comments
 (0)