Skip to content

Commit 01c27aa

Browse files
bigelephant29copybara-github
authored andcommitted
PostMark: Implement the postmark attribute in the cc_binary rule.
This is not available in Bazel at the moment. PiperOrigin-RevId: 825562421 Change-Id: Ib77c80eca0f76c4177e5ed7741846e65dacf46f7
1 parent f3ef345 commit 01c27aa

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

cc/private/rules_impl/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ bzl_library(
4040
"cc_binary.bzl",
4141
"cc_import.bzl",
4242
"cc_library.bzl",
43+
"cc_postmark.bzl",
4344
"cc_shared_library.bzl",
4445
"cc_static_library.bzl",
4546
"cc_test.bzl",

cc/private/rules_impl/attrs.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
load("//cc/common:cc_info.bzl", "CcInfo")
1919
load("//cc/common:semantics.bzl", "semantics")
20+
load("//cc/private/rules_impl:cc_postmark.bzl", "postmark")
2021
load(":cc_shared_library.bzl", "dynamic_deps_attrs")
2122

2223
visibility("private")
@@ -293,7 +294,7 @@ Whether to encode build information into the binary. Possible values:
293294
""" + semantics.stamp_extra_docs
294295

295296
# buildifier: disable=attr-licenses
296-
cc_binary_attrs = common_attrs | {
297+
cc_binary_attrs = common_attrs | postmark.get_attrs() | {
297298
"deps": attr.label_list(
298299
allow_files = semantics.ALLOWED_FILES_IN_DEPS,
299300
allow_rules = semantics.ALLOWED_RULES_IN_DEPS + semantics.ALLOWED_RULES_WITH_WARNINGS_IN_DEPS,

cc/private/rules_impl/cc_binary.bzl

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ load("//cc/common:cc_info.bzl", "CcInfo")
2222
load("//cc/common:debug_package_info.bzl", "DebugPackageInfo")
2323
load("//cc/common:semantics.bzl", "semantics")
2424
load(":attrs.bzl", "cc_binary_attrs")
25+
load(":cc_postmark.bzl", "postmark")
2526
load(":cc_shared_library.bzl", "GraphNodeInfo", "add_unused_dynamic_deps", "build_exports_map_from_only_dynamic_deps", "build_link_once_static_libs_map", "dynamic_deps_initializer", "merge_cc_shared_library_infos", "separate_static_and_dynamic_link_libraries", "sort_linker_inputs", "throw_linked_but_not_exported_errors")
2627

2728
_CcLauncherInfo = cc_common.launcher_provider
@@ -307,7 +308,8 @@ def _create_transitive_linking_actions(
307308
additional_linkopts,
308309
additional_make_variable_substitutions,
309310
link_variables,
310-
additional_outputs):
311+
additional_outputs,
312+
stamp):
311313
cc_compilation_outputs_with_only_objects = cc_common.create_compilation_outputs(objects = None, pic_objects = None)
312314
deps_cc_info = CcInfo(linking_context = deps_cc_linking_context)
313315
libraries_for_current_cc_linking_context = []
@@ -381,7 +383,7 @@ def _create_transitive_linking_actions(
381383
feature_configuration = feature_configuration,
382384
cc_toolchain = cc_toolchain,
383385
compilation_outputs = cc_compilation_outputs_with_only_objects,
384-
stamp = cc_helper.is_stamping_enabled(ctx),
386+
stamp = int(stamp),
385387
additional_inputs = additional_linker_inputs,
386388
linking_contexts = [cc_linking_context],
387389
name = ctx.label.name,
@@ -633,6 +635,16 @@ def cc_binary_impl(ctx, additional_linkopts, force_linkstatic = False):
633635
if extra_link_time_libraries != None:
634636
linker_inputs_extra, runtime_libraries_extra = cc_common.build_extra_link_time_libraries(extra_libraries = extra_link_time_libraries, ctx = ctx, static_mode = linking_mode != linker_mode.LINKING_DYNAMIC, for_dynamic_library = _is_link_shared(ctx))
635637

638+
use_postmark = postmark.get_use_postmark(ctx)
639+
output_binary_for_linking = binary
640+
link_stamp = cc_helper.is_stamping_enabled(ctx)
641+
if use_postmark:
642+
output_binary_for_linking = ctx.actions.declare_file(
643+
"unstamped_" + binary.basename,
644+
sibling = binary,
645+
)
646+
link_stamp = False
647+
636648
cc_linking_outputs_binary, cc_launcher_info, deps_cc_linking_context = _create_transitive_linking_actions(
637649
ctx,
638650
cc_toolchain,
@@ -641,7 +653,7 @@ def cc_binary_impl(ctx, additional_linkopts, force_linkstatic = False):
641653
cc_compilation_outputs,
642654
additional_linker_inputs,
643655
cc_linking_outputs,
644-
binary,
656+
output_binary_for_linking,
645657
deps_cc_linking_context,
646658
linker_inputs_extra,
647659
link_compile_output_separately,
@@ -651,8 +663,17 @@ def cc_binary_impl(ctx, additional_linkopts, force_linkstatic = False):
651663
additional_make_variable_substitutions,
652664
link_variables,
653665
additional_linker_outputs,
666+
stamp = link_stamp,
654667
)
655668

669+
if use_postmark:
670+
postmark.add_action(
671+
ctx,
672+
cc_toolchain,
673+
binary,
674+
output_binary_for_linking,
675+
)
676+
656677
cc_linking_outputs_binary_library = cc_linking_outputs_binary.library_to_link
657678
libraries = []
658679
if _is_link_shared(ctx) and cc_linking_outputs_binary_library != None:
@@ -809,6 +830,10 @@ ALLOWED_SRC_FILES.extend(cc_helper.extensions.SHARED_LIBRARY)
809830
ALLOWED_SRC_FILES.extend(cc_helper.extensions.OBJECT_FILE)
810831
ALLOWED_SRC_FILES.extend(cc_helper.extensions.PIC_OBJECT_FILE)
811832

833+
def _cc_binary_initializer(**kwargs):
834+
kwargs = postmark.initializer(**kwargs)
835+
return dynamic_deps_initializer(**kwargs)
836+
812837
def _impl(ctx):
813838
binary_info, providers = cc_binary_impl(ctx, [])
814839

@@ -831,7 +856,7 @@ def _impl(ctx):
831856

832857
cc_binary = rule(
833858
implementation = _impl,
834-
initializer = dynamic_deps_initializer,
859+
initializer = _cc_binary_initializer,
835860
doc = """
836861
<p>It produces an executable binary.</p>
837862
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2025 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""PostMark is not implemented in Bazel."""
16+
17+
def _get_postmark_attrs():
18+
return {}
19+
20+
def _postmark_initializer(**kwargs):
21+
return kwargs
22+
23+
def _add_postmark_action(
24+
ctx, # @unused
25+
cc_toolchain, # @unused
26+
binary, # @unused
27+
output_binary_for_linking): # @unused
28+
pass
29+
30+
def _get_use_postmark(ctx): # @unused
31+
return False
32+
33+
postmark = struct(
34+
get_attrs = _get_postmark_attrs,
35+
add_action = _add_postmark_action,
36+
get_use_postmark = _get_use_postmark,
37+
initializer = _postmark_initializer,
38+
)

cc/private/rules_impl/cc_test.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ load("//cc/common:cc_info.bzl", "CcInfo")
2020
load("//cc/common:semantics.bzl", "semantics")
2121
load(":attrs.bzl", "cc_binary_attrs", "linkstatic_doc", "stamp_doc")
2222
load(":cc_binary.bzl", "cc_binary_impl")
23+
load(":cc_postmark.bzl", "postmark")
2324
load(":cc_shared_library.bzl", "dynamic_deps_initializer")
2425

2526
_CC_TEST_TOOLCHAIN_TYPE = "@bazel_tools//tools/cpp:test_runner_toolchain_type"
@@ -105,10 +106,14 @@ def cc_test_initializer(**kwargs):
105106
106107
Args:
107108
**kwargs: Arguments suitable for cc_test.
109+
110+
Returns:
111+
Arguments suitable for cc_test.
108112
"""
109113

110114
if "linkstatic" not in kwargs:
111115
kwargs["linkstatic"] = semantics.get_linkstatic_default_for_test()
116+
kwargs = postmark.initializer(**kwargs)
112117

113118
return dynamic_deps_initializer(**kwargs)
114119

0 commit comments

Comments
 (0)