aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/tools/build_info/gen_build_info.py
blob: 690214abfb155c083ef197e583afbafe9a3381f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/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, key_value_list):
  """Writes a Python that describes the build.

  Args:
    filename: filename to write to.
    build_config: A string that represents the config used in this build (e.g.
      "cuda").
    key_value_list: A list of "key=value" strings that will be added to the
      module as additional fields.

  Raises:
    ValueError: If `key_value_list` includes the key "is_cuda_build", which
      would clash with one of the default fields.
  """
  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"

  key_value_pair_stmts = []
  if key_value_list:
    for arg in key_value_list:
      key, value = arg.split("=")
      if key == "is_cuda_build":
        raise ValueError("The key \"is_cuda_build\" cannot be passed as one of "
                         "the --key_value arguments.")
      key_value_pair_stmts.append("%s = %r" % (key, value))
  key_value_pair_content = "\n".join(key_value_pair_stmts)

  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

%s
""" % (module_docstring, build_config_bool, key_value_pair_content)
  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")

parser.add_argument("--key_value", type=str, nargs="*",
                    help="List of key=value pairs.")

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, args.key_value)
else:
  raise RuntimeError("--raw_generate and --build_config must be used")