Skip to content

Commit 58a3428

Browse files
laramielcopybara-github
authored andcommitted
Create a tag_release rule
bazel_to_cmake: The correct name is BuildSettingInfo, not BuildSettingProvider PiperOrigin-RevId: 805183129 Change-Id: I4207843a8042fd83c03a9782c7b2f639514cb460
1 parent 6cd8967 commit 58a3428

File tree

17 files changed

+163
-40
lines changed

17 files changed

+163
-40
lines changed

bazel/tag_release.bzl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2021 The TensorStore Authors
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+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
16+
17+
# TODO: Consider using the bazel --stamp flag:
18+
#
19+
# https://bazel.build/docs/user-manual#workspace-status-command
20+
# --workspace_status_command program would generate key value pairs,
21+
# which are stored in https://bazel.build/rules/lib/builtins/ctx#version_file
22+
#
23+
# Also consider exposing the version as a python __version__.py file.
24+
25+
_TEMPLATE = """
26+
// version is auto-generated by tensorstore build.
27+
28+
#ifndef TENSORSTORE_VERSION
29+
#define TENSORSTORE_VERSION "{version}"
30+
#endif // TENSORSTORE_VERSION
31+
32+
"""
33+
34+
def _tag_release_impl(ctx):
35+
ctx.actions.write(
36+
output = ctx.outputs.out,
37+
content = _TEMPLATE.format(version = ctx.attr.label[BuildSettingInfo].value),
38+
is_executable = False,
39+
)
40+
return [DefaultInfo(files = depset([ctx.outputs.out]))]
41+
42+
tag_release = rule(
43+
implementation = _tag_release_impl,
44+
output_to_genfiles = True,
45+
attrs = {
46+
"out": attr.output(mandatory = True, doc = "The output file to write the version to."),
47+
"label": attr.label(mandatory = True, doc = "The label to tag the release with."),
48+
},
49+
)

setup.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ def run(self):
240240
else:
241241
build_flags.append('--copt=-O3')
242242

243+
version_stamp = self.distribution.get_version()
244+
if version_stamp:
245+
build_flags.append(
246+
'--//tensorstore/internal/version=%s' % version_stamp
247+
)
248+
243249
build_command = (
244250
[sys.executable, '-u', bazelisk]
245251
+ startup_options

tensorstore/internal/curl/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ tensorstore_cc_library(
9999
hdrs = ["curl_wrappers.h"],
100100
deps = [
101101
"//tensorstore/internal:source_location",
102+
"//tensorstore/internal/version:version_h",
102103
"//tensorstore/util:status",
103104
"//tensorstore/util:str_cat",
104105
"@abseil-cpp//absl/base",

tensorstore/internal/curl/curl_wrappers.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "absl/strings/cord.h"
2727
#include <curl/curl.h>
2828
#include "tensorstore/internal/source_location.h"
29+
#include "tensorstore/internal/version/version.h"
2930
#include "tensorstore/util/status.h"
3031
#include "tensorstore/util/str_cat.h"
3132

@@ -69,8 +70,8 @@ void CurlSlistCleanup::operator()(curl_slist* s) { curl_slist_free_all(s); }
6970

7071
/// Returns the default CurlUserAgent.
7172
std::string GetCurlUserAgentSuffix() {
72-
static std::string agent =
73-
tensorstore::StrCat("tensorstore/0.1 ", curl_version());
73+
static std::string agent = tensorstore::StrCat(
74+
"tensorstore/", TENSORSTORE_VERSION, " ", curl_version());
7475
return agent;
7576
}
7677

tensorstore/internal/version/BUILD

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
2+
load("//bazel:tag_release.bzl", "tag_release")
3+
load("//bazel:tensorstore.bzl", "tensorstore_cc_library")
4+
5+
package(default_visibility = ["//tensorstore:internal_packages"])
6+
7+
licenses(["notice"])
8+
9+
string_flag(
10+
name = "version",
11+
build_setting_default = "0.1.dev",
12+
)
13+
14+
tag_release(
15+
name = "generate_version_h",
16+
out = "version.h",
17+
label = ":version",
18+
)
19+
20+
tensorstore_cc_library(
21+
name = "version_h",
22+
hdrs = ["version.h"],
23+
)

tools/cmake/bazel_to_cmake/bzl_library/bazel_skylib.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from ..cmake_target import CMakeTarget
2929
from ..evaluation import EvaluationState
3030
from ..starlark.bazel_target import TargetId
31-
from ..starlark.common_providers import BuildSettingProvider
31+
from ..starlark.common_providers import BuildSettingInfo
3232
from ..starlark.common_providers import ConditionProvider
3333
from ..starlark.common_providers import FilesProvider
3434
from ..starlark.invocation_context import InvocationContext
@@ -344,20 +344,9 @@ def _write_file_impl(
344344
@register_bzl_library("@bazel_skylib//rules:common_settings.bzl")
345345
class BazelSkylibCommonSettingsLibrary(ScopeCommon):
346346

347-
BuildSettingInfo = provider(
348-
doc="A singleton provider that contains the raw value of a build setting",
349-
fields={
350-
"value": (
351-
"The value of the build setting in the current configuration. "
352-
"This value may come from the command line or an upstream "
353-
"transition, or else it will be the build setting's default."
354-
),
355-
},
356-
)
357-
358347
@property
359348
def bazel_BuildSettingInfo(self):
360-
return self.BuildSettingInfo
349+
return BuildSettingInfo
361350

362351
def bazel_bool_flag(
363352
self,
@@ -409,7 +398,7 @@ def _bool_flag_impl(
409398
_context.access(CMakeBuilder).addtext(
410399
f"""option({cmake_name} "" {"ON" if default_value else "OFF"})\n"""
411400
)
412-
_context.add_analyzed_target(_target, TargetInfo(BuildSettingProvider(value)))
401+
_context.add_analyzed_target(_target, TargetInfo(BuildSettingInfo(value)))
413402

414403

415404
def _string_flag_impl(
@@ -433,4 +422,4 @@ def _string_flag_impl(
433422
_context.access(CMakeBuilder).addtext(
434423
f"""option({cmake_name} "" {quote_string(value)})\n"""
435424
)
436-
_context.add_analyzed_target(_target, TargetInfo(BuildSettingProvider(value)))
425+
_context.add_analyzed_target(_target, TargetInfo(BuildSettingInfo(value)))

tools/cmake/bazel_to_cmake/evaluation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
from .starlark.bazel_target import PackageId
9494
from .starlark.bazel_target import RepositoryId
9595
from .starlark.bazel_target import TargetId
96-
from .starlark.common_providers import BuildSettingProvider
96+
from .starlark.common_providers import BuildSettingInfo
9797
from .starlark.common_providers import ConditionProvider
9898
from .starlark.common_providers import FilesProvider
9999
from .starlark.exec import compile_and_exec
@@ -403,8 +403,8 @@ def evaluate_condition(self, target_id: TargetId) -> Any:
403403
target_info = self.get_target_info(target_id)
404404
if target_info.get(ConditionProvider) is not None:
405405
return target_info[ConditionProvider].value
406-
if target_info.get(BuildSettingProvider) is not None:
407-
return target_info[BuildSettingProvider].value
406+
if target_info.get(BuildSettingInfo) is not None:
407+
return target_info[BuildSettingInfo].value
408408

409409
print(f"Using {target_id.as_label()} as false condition.")
410410
return False

tools/cmake/bazel_to_cmake/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from .platforms import add_platform_constraints
3535
from .starlark.bazel_target import RepositoryId
3636
from .starlark.bazel_target import TargetId
37-
from .starlark.common_providers import BuildSettingProvider
37+
from .starlark.common_providers import BuildSettingInfo
3838
from .starlark.common_providers import ConditionProvider
3939
from .starlark.provider import TargetInfo
4040
from .util import get_matching_build_files
@@ -312,7 +312,7 @@ def run_main(args: argparse.Namespace):
312312
# * Build and configuration settings.
313313
def _persist_targetinfo(target: TargetId, info: TargetInfo):
314314
if (
315-
info.get(BuildSettingProvider) is not None
315+
info.get(BuildSettingInfo) is not None
316316
or info.get(ConditionProvider) is not None
317317
):
318318
workspace.set_persistent_target_info(target, info)

tools/cmake/bazel_to_cmake/native_rules_alias.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from .cmake_target import CMakeTargetPair
2727
from .evaluation import EvaluationState
2828
from .starlark.bazel_target import TargetId
29-
from .starlark.common_providers import BuildSettingProvider
29+
from .starlark.common_providers import BuildSettingInfo
3030
from .starlark.common_providers import ConditionProvider
3131
from .starlark.invocation_context import InvocationContext
3232
from .starlark.label import RelativeLabel
@@ -67,7 +67,7 @@ def _alias_impl(
6767

6868
# Special case any target info with conditions or build settings.
6969
if target_info.get(ConditionProvider) or target_info.get(
70-
BuildSettingProvider
70+
BuildSettingInfo
7171
):
7272
_context.add_analyzed_target(_target, target_info)
7373
return

tools/cmake/bazel_to_cmake/native_rules_config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from .evaluation import EvaluationState
2828
from .starlark import rule # pylint: disable=unused-import
2929
from .starlark.bazel_target import TargetId
30-
from .starlark.common_providers import BuildSettingProvider
30+
from .starlark.common_providers import BuildSettingInfo
3131
from .starlark.common_providers import ConditionProvider
3232
from .starlark.invocation_context import InvocationContext
3333
from .starlark.label import RelativeLabel
@@ -80,9 +80,9 @@ def evaluate() -> bool:
8080
for flag, value in flag_values.items():
8181
flag_target = _context.resolve_target_or_label(flag)
8282
flag_info = _context.get_target_info(flag_target)
83-
if not flag_info.get(BuildSettingProvider):
83+
if not flag_info.get(BuildSettingInfo):
8484
return False
85-
if str(flag_info[BuildSettingProvider].value) != value:
85+
if str(flag_info[BuildSettingInfo].value) != value:
8686
return False
8787
if constraint_values:
8888
for constraint in _context.resolve_target_or_label_list(

0 commit comments

Comments
 (0)