diff --git a/tensorflow/BUILD b/tensorflow/BUILD index 5cd92d2ff47..cb77259a917 100644 --- a/tensorflow/BUILD +++ b/tensorflow/BUILD @@ -413,6 +413,7 @@ filegroup( "//tensorflow/tools/api/golden:all_files", "//tensorflow/tools/api/lib:all_files", "//tensorflow/tools/api/tests:all_files", + "//tensorflow/tools/build_info:all_files", "//tensorflow/tools/common:all_files", "//tensorflow/tools/compatibility:all_files", "//tensorflow/tools/dist_test/server:all_files", diff --git a/tensorflow/contrib/cmake/tf_python.cmake b/tensorflow/contrib/cmake/tf_python.cmake index ce353023174..7d63f5c39ef 100755 --- a/tensorflow/contrib/cmake/tf_python.cmake +++ b/tensorflow/contrib/cmake/tf_python.cmake @@ -624,6 +624,16 @@ add_python_module("tensorflow/contrib/reduce_slice_ops/python") add_python_module("tensorflow/contrib/reduce_slice_ops/python/kernel_tests") add_python_module("tensorflow/contrib/reduce_slice_ops/python/ops") +# Generate the tensorflow.python.platform.build_info module. +set(BUILD_INFO_PY "${CMAKE_CURRENT_BINARY_DIR}/tf_python/tensorflow/python/platform/build_info.py") +if(tensorflow_ENABLE_GPU) + set(BUILD_CONFIG_STRING "cuda") +else(tensorflow_ENABLE_GPU) + set(BUILD_CONFIG_STRING "cpu") +endif(tensorflow_ENABLE_GPU) +add_custom_command(TARGET tf_python_copy_scripts_to_destination PRE_BUILD + COMMAND ${PYTHON_EXECUTABLE} ${tensorflow_source_dir}/tensorflow/tools/build_info/gen_build_info.py --build_config ${BUILD_CONFIG_STRING} --raw_generate ${BUILD_INFO_PY}) + ######################################################## # tf_python_op_gen_main library @@ -1173,3 +1183,4 @@ else() WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tf_python) endif(${tensorflow_ENABLE_GPU}) endif(${tensorflow_TF_NIGHTLY}) + diff --git a/tensorflow/contrib/cmake/tf_tests.cmake b/tensorflow/contrib/cmake/tf_tests.cmake index 9dff8881559..d836428d9e8 100644 --- a/tensorflow/contrib/cmake/tf_tests.cmake +++ b/tensorflow/contrib/cmake/tf_tests.cmake @@ -145,6 +145,7 @@ if (tensorflow_BUILD_PYTHON_TESTS) "${tensorflow_source_dir}/tensorflow/contrib/estimator/python/estimator/*_test.py" "${tensorflow_source_dir}/tensorflow/python/kernel_tests/*.py" "${tensorflow_source_dir}/tensorflow/python/meta_graph_transform/*_test.py" + "${tensorflow_source_dir}/tensorflow/python/platform/build_info_test.py" "${tensorflow_source_dir}/tensorflow/python/profiler/*_test.py" "${tensorflow_source_dir}/tensorflow/python/profiler/internal/*_test.py" "${tensorflow_source_dir}/tensorflow/python/saved_model/*_test.py" diff --git a/tensorflow/python/BUILD b/tensorflow/python/BUILD index 092435a40f1..7ea5cedd8a4 100644 --- a/tensorflow/python/BUILD +++ b/tensorflow/python/BUILD @@ -19,6 +19,7 @@ load("//tensorflow:tensorflow.bzl", "tf_gen_op_wrapper_py") load("//tensorflow:tensorflow.bzl", "py_test") load("//tensorflow:tensorflow.bzl", "tf_py_test") load("//tensorflow:tensorflow.bzl", "py_tests") +load("//tensorflow:tensorflow.bzl", "tf_py_build_info_genrule") load("//tensorflow:tensorflow.bzl", "tf_py_wrap_cc") load("//tensorflow:tensorflow.bzl", "cuda_py_test") load("//tensorflow:tensorflow.bzl", "cuda_py_tests") @@ -100,15 +101,19 @@ py_library( ]), ) +tf_py_build_info_genrule() + py_library( name = "platform", srcs = glob( - ["platform/*.py"], + [ + "platform/*.py", + ], exclude = [ "**/*test.py", "**/benchmark.py", # In platform_benchmark. ], - ), + ) + ["platform/build_info.py"], srcs_version = "PY2AND3", deps = [ ":lib", @@ -806,6 +811,22 @@ py_test( ], ) +py_test( + name = "build_info_test", + size = "small", + srcs = [ + "platform/build_info.py", + "platform/build_info_test.py", + ], + main = "platform/build_info_test.py", + srcs_version = "PY2AND3", + tags = ["notap"], + deps = [ + ":client_testlib", + ":platform", + ], +) + py_test( name = "proto_test", size = "small", diff --git a/tensorflow/python/platform/build_info_test.py b/tensorflow/python/platform/build_info_test.py new file mode 100644 index 00000000000..581f3c19f6e --- /dev/null +++ b/tensorflow/python/platform/build_info_test.py @@ -0,0 +1,32 @@ +# Copyright 2015 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Test for the generated build_info script.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +from tensorflow.python.platform import build_info +from tensorflow.python.platform import test + + +class BuildInfoTest(test.TestCase): + + def testBuildInfo(self): + self.assertEqual(build_info.is_cuda_build, test.is_built_with_cuda()) + + +if __name__ == '__main__': + test.main() diff --git a/tensorflow/tensorflow.bzl b/tensorflow/tensorflow.bzl index e3af7faeb83..5c156e7ee26 100644 --- a/tensorflow/tensorflow.bzl +++ b/tensorflow/tensorflow.bzl @@ -1294,6 +1294,16 @@ def tf_version_info_genrule(): tools=[clean_dep("//tensorflow/tools/git:gen_git_source.py")],) +def tf_py_build_info_genrule(): + native.genrule( + name="py_build_info_gen", + outs=["platform/build_info.py"], + cmd= + "$(location //tensorflow/tools/build_info:gen_build_info.py) --raw_generate \"$@\" --build_config " + if_cuda("cuda", "cpu"), + local=1, + tools=[clean_dep("//tensorflow/tools/build_info:gen_build_info.py")],) + + def cc_library_with_android_deps(deps, android_deps=[], common_deps=[], diff --git a/tensorflow/tools/build_info/BUILD b/tensorflow/tools/build_info/BUILD new file mode 100644 index 00000000000..cdc47076ced --- /dev/null +++ b/tensorflow/tools/build_info/BUILD @@ -0,0 +1,26 @@ +# Description: +# Contains script to generate tensorflow/python/platform/build_info.py +package(default_visibility = ["//tensorflow:internal"]) + +licenses(["notice"]) # Apache 2.0 + +exports_files( + glob(["gen/*"]) + [ + "gen_build_info.py", + ], +) + +# ----------------------------------------------------------------------------- +# Google-internal targets. These must be at the end for syncrepo. + +filegroup( + name = "all_files", + srcs = glob( + ["**/*"], + exclude = [ + "**/METADATA", + "**/OWNERS", + ], + ), + visibility = ["//tensorflow:__subpackages__"], +) diff --git a/tensorflow/tools/build_info/gen_build_info.py b/tensorflow/tools/build_info/gen_build_info.py new file mode 100755 index 00000000000..f59cdb0e1e2 --- /dev/null +++ b/tensorflow/tools/build_info/gen_build_info.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Generates a Python module containing information about the build.""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import argparse + + +def write_build_info(filename, build_config): + """Writes a Python that describes the build. + + Args: + filename: filename to write to. + build_config: A string containinggit_version: the result of a git describe. + """ + module_docstring = "\"\"\"Generates a Python module containing information " + module_docstring += "about the build.\"\"\"" + if build_config == "cuda": + build_config_bool = "True" + else: + build_config_bool = "False" + + contents = """ +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +%s +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +is_cuda_build = %s +""" % (module_docstring, build_config_bool) + open(filename, "w").write(contents) + + +parser = argparse.ArgumentParser( + description="""Build info injection into the PIP package.""") + +parser.add_argument( + "--build_config", + type=str, + help="Either 'cuda' for GPU builds or 'cpu' for CPU builds.") + +parser.add_argument("--raw_generate", type=str, help="Generate build_info.py") + +args = parser.parse_args() + +if args.raw_generate is not None and args.build_config is not None: + write_build_info(args.raw_generate, args.build_config) +else: + raise RuntimeError("--raw_generate and --build_config must be used")