Skip to content

Commit f5bb643

Browse files
Add two repository rules:
- @local_execution_config_platform: local platform to allow selecting locally executed tools on - @local_execution_config_python: python configured for execution on the local machine during otherwise remote builds Mark rules that are required to run locally to require our local platform. This allows pyth... PiperOrigin-RevId: 307771596 Change-Id: If1f0013ec88a35d507b2b622894208aab2416fe5
1 parent e103364 commit f5bb643

12 files changed

Lines changed: 91 additions & 4 deletions

File tree

WORKSPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ load("//tensorflow:workspace.bzl", "tf_repositories")
1818
# Please add all new TensorFlow dependencies in workspace.bzl.
1919
tf_repositories()
2020

21+
register_execution_platforms("@local_execution_config_platform//:platform")
22+
register_toolchains("@local_execution_config_python//:py_toolchain")
2123
register_toolchains("@local_config_python//:py_toolchain")
2224

2325
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_repositories")

tensorflow/opensource_only.files

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ tensorflow/third_party/toolchains/remote_config/containers.bzl
263263
tensorflow/third_party/toolchains/remote_config/rbe_config.bzl
264264
tensorflow/third_party/wrapt.BUILD
265265
tensorflow/third_party/zlib.BUILD
266+
tensorflow/tools/build_info/BUILD
266267
tensorflow/tools/ci_build/release/common.sh
267268
tensorflow/tools/ci_build/release/common_win.bat
268269
tensorflow/tools/ci_build/release/macos/cpu_libtensorflow/build.sh
@@ -393,6 +394,7 @@ tensorflow/tools/def_file_filter/BUILD
393394
tensorflow/tools/def_file_filter/BUILD.tpl
394395
tensorflow/tools/def_file_filter/def_file_filter.py.tpl
395396
tensorflow/tools/def_file_filter/def_file_filter_configure.bzl
397+
tensorflow/tools/git/BUILD
396398
tensorflow/tools/lib_package/BUILD
397399
tensorflow/tools/lib_package/LibTensorFlowTest.java
398400
tensorflow/tools/lib_package/README.md

tensorflow/python/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ load("//tensorflow:tensorflow.bzl", "tf_external_workspace_visible")
2626

2727
# buildifier: disable=same-origin-load
2828
load("//tensorflow:tensorflow.bzl", "tf_pybind_cc_library_wrapper")
29+
30+
# buildifier: disable=same-origin-load
31+
load("//tensorflow:tensorflow.bzl", "tf_local_platform_constraint")
2932
load("//tensorflow/core/platform:build_config.bzl", "pyx_library", "tf_additional_all_protos", "tf_additional_lib_deps", "tf_proto_library", "tf_proto_library_py", "tf_protos_grappler") # @unused
3033
load("//tensorflow/core/platform:build_config_root.bzl", "if_static", "tf_additional_plugin_deps", "tf_additional_xla_deps_py")
3134
load("//tensorflow/python:build_defs.bzl", "tf_gen_op_wrapper_private_py")
@@ -233,9 +236,12 @@ py_library(
233236
],
234237
)
235238

239+
# TODO(gunan): Investigate making this action hermetic so we do not need
240+
# to run it locally.
236241
tf_py_build_info_genrule(
237242
name = "py_build_info_gen",
238243
out = "platform/build_info.py",
244+
exec_compatible_with = tf_local_platform_constraint(),
239245
)
240246

241247
py_library(

tensorflow/tensorflow.bzl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2519,7 +2519,12 @@ def tf_genrule_cmd_append_to_srcs(to_append):
25192519
return ("cat $(SRCS) > $(@) && " + "echo >> $(@) && " + "echo " + to_append +
25202520
" >> $(@)")
25212521

2522+
def tf_local_platform_constraint():
2523+
return ["@local_execution_config_platform//:platform_constraint"]
2524+
25222525
def tf_version_info_genrule(name, out):
2526+
# TODO(gunan): Investigate making this action hermetic so we do not need
2527+
# to run it locally.
25232528
native.genrule(
25242529
name = name,
25252530
srcs = [
@@ -2532,9 +2537,10 @@ def tf_version_info_genrule(name, out):
25322537
"$(location //tensorflow/tools/git:gen_git_source) --generate $(SRCS) \"$@\" --git_tag_override=$${GIT_TAG_OVERRIDE:-}",
25332538
local = 1,
25342539
exec_tools = [clean_dep("//tensorflow/tools/git:gen_git_source")],
2540+
exec_compatible_with = tf_local_platform_constraint(),
25352541
)
25362542

2537-
def tf_py_build_info_genrule(name, out, **kwargs):
2543+
def tf_py_build_info_genrule(name, out, exec_compatible_with, **kwargs):
25382544
native.genrule(
25392545
name = name,
25402546
outs = [out],

tensorflow/tools/build_info/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package(
99
py_binary(
1010
name = "gen_build_info",
1111
srcs = ["gen_build_info.py"],
12+
exec_compatible_with = ["@local_execution_config_platform//:platform_constraint"],
1213
python_version = "PY3",
1314
srcs_version = "PY2AND3",
1415
deps = [

tensorflow/tools/git/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package(
1111
py_binary(
1212
name = "gen_git_source",
1313
srcs = ["gen_git_source.py"],
14+
exec_compatible_with = ["@local_execution_config_platform//:platform_constraint"],
1415
python_version = "PY3",
1516
srcs_version = "PY2AND3",
1617
)

third_party/py/BUILD.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ toolchain(
2828
name = "py_toolchain",
2929
toolchain = ":py_runtime_pair",
3030
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
31+
target_compatible_with = [%{PLATFORM_CONSTRAINT}],
32+
exec_compatible_with = [%{PLATFORM_CONSTRAINT}],
3133
)
3234

3335
# To build Python C/C++ extension on Windows, we need to link to python import library pythonXY.lib

third_party/py/python_configure.bzl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,15 @@ def _create_local_python_repository(repository_ctx):
240240
"numpy_include",
241241
)
242242

243+
platform_constraint = ""
244+
if repository_ctx.attr.platform_constraint:
245+
platform_constraint = "\"%s\"" % repository_ctx.attr.platform_constraint
243246
repository_ctx.template("BUILD", build_tpl, {
244247
"%{PYTHON_BIN_PATH}": python_bin,
245248
"%{PYTHON_INCLUDE_GENRULE}": python_include_rule,
246249
"%{PYTHON_IMPORT_LIB_GENRULE}": python_import_lib_genrule,
247250
"%{NUMPY_INCLUDE_GENRULE}": numpy_include_rule,
251+
"%{PLATFORM_CONSTRAINT}": platform_constraint,
248252
})
249253

250254
def _create_remote_python_repository(repository_ctx, remote_config_repo):
@@ -268,18 +272,31 @@ _ENVIRONS = [
268272
PYTHON_LIB_PATH,
269273
]
270274

275+
local_python_configure = repository_rule(
276+
implementation = _create_local_python_repository,
277+
environ = _ENVIRONS,
278+
attrs = {
279+
"environ": attr.string_dict(),
280+
"platform_constraint": attr.string(),
281+
},
282+
)
283+
271284
remote_python_configure = repository_rule(
272285
implementation = _create_local_python_repository,
273286
environ = _ENVIRONS,
274287
remotable = True,
275288
attrs = {
276289
"environ": attr.string_dict(),
290+
"platform_constraint": attr.string(),
277291
},
278292
)
279293

280294
python_configure = repository_rule(
281295
implementation = _python_autoconf_impl,
282296
environ = _ENVIRONS + [TF_PYTHON_CONFIG_REPO],
297+
attrs = {
298+
"platform_constraint": attr.string(),
299+
},
283300
)
284301
"""Detects and configures the local Python.
285302
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
# Each platform creates a constraint @<platform>//:platform_constraint that
2+
# is listed in its constraint_values; rule that want to select a specific
3+
# platform to run on can put @<platform>//:platform_constraing into their
4+
# exec_compatible_with attribute.
5+
# Toolchains can similarly be marked with target_compatible_with or
6+
# exec_compatible_with to bind them to this platform.
7+
constraint_setting(
8+
name = "platform_setting"
9+
)
10+
11+
constraint_value(
12+
name = "platform_constraint",
13+
constraint_setting = ":platform_setting",
14+
visibility = ["//visibility:public"],
15+
)
16+
117
platform(
218
name = "platform",
319
constraint_values = [
420
"@bazel_tools//platforms:x86_64",
521
"@bazel_tools//platforms:%{platform}",
22+
":platform_constraint",
623
],
724
exec_properties = %{exec_properties},
825
)

third_party/remote_config/remote_platform_configure.bzl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
def _remote_platform_configure_impl(repository_ctx):
44
platform = repository_ctx.attr.platform
5+
if platform == "local":
6+
os = repository_ctx.os.name.lower()
7+
if os.startswith("windows"):
8+
platform = "windows"
9+
elif os.startswith("mac os"):
10+
platform = "osx"
11+
else:
12+
platform = "linux"
513
exec_properties = repository_ctx.attr.platform_exec_properties
614

715
serialized_exec_properties = "{"
@@ -22,6 +30,6 @@ remote_platform_configure = repository_rule(
2230
implementation = _remote_platform_configure_impl,
2331
attrs = {
2432
"platform_exec_properties": attr.string_dict(mandatory = True),
25-
"platform": attr.string(default = "linux", values = ["linux", "windows"]),
33+
"platform": attr.string(default = "linux", values = ["linux", "windows", "local"]),
2634
},
2735
)

0 commit comments

Comments
 (0)