• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15# ==============================================================================
16#
17# Checks that the options mentioned in syslibs_configure.bzl are consistent with
18# the valid options in workspace.bzl
19# Expects the tensorflow source folder as the first argument
20
21import glob
22import os
23import sys
24
25tf_source_path = sys.argv[1]
26
27syslibs_configure_path = os.path.join(tf_source_path, 'third_party',
28                                      'systemlibs', 'syslibs_configure.bzl')
29workspace_path = os.path.join(tf_source_path, 'tensorflow', 'workspace.bzl')
30third_party_path = os.path.join(tf_source_path, 'third_party')
31third_party_glob = os.path.join(third_party_path, '*', 'workspace.bzl')
32
33if not (os.path.isdir(tf_source_path) and os.path.isfile(syslibs_configure_path)
34        and os.path.isfile(workspace_path)):
35  raise ValueError('The path to the TensorFlow source must be passed as'
36                   ' the first argument')
37
38
39def extract_valid_libs(filepath):
40  """Evaluate syslibs_configure.bzl, return the VALID_LIBS set from that file."""
41
42  # Stub only
43  def repository_rule(**kwargs):  # pylint: disable=unused-variable
44    del kwargs
45
46  # Populates VALID_LIBS
47  with open(filepath, 'r') as f:
48    f_globals = {'repository_rule': repository_rule}
49    f_locals = {}
50    exec(f.read(), f_globals, f_locals)  # pylint: disable=exec-used
51
52  return set(f_locals['VALID_LIBS'])
53
54
55def extract_system_builds(filepath):
56  """Extract the 'name' argument of all rules with a system_build_file argument."""
57  lib_names = []
58  system_build_files = []
59  current_name = None
60  with open(filepath, 'r') as f:
61    for line in f:
62      line = line.strip()
63      if line.startswith('name = '):
64        current_name = line[7:-1].strip('"')
65      elif line.startswith('system_build_file = '):
66        lib_names.append(current_name)
67        # Split at '=' to extract rhs, then extract value between quotes
68        system_build_spec = line.split('=')[-1].split('"')[1]
69        assert system_build_spec.startswith('//')
70        system_build_files.append(system_build_spec[2:].replace(':', os.sep))
71  return lib_names, system_build_files
72
73
74syslibs = extract_valid_libs(syslibs_configure_path)
75
76syslibs_from_workspace = set()
77system_build_files_from_workspace = []
78for current_path in [workspace_path] + glob.glob(third_party_glob):
79  cur_lib_names, build_files = extract_system_builds(current_path)
80  syslibs_from_workspace.update(cur_lib_names)
81  system_build_files_from_workspace.extend(build_files)
82
83missing_build_files = [
84    file for file in system_build_files_from_workspace
85    if not os.path.isfile(os.path.join(tf_source_path, file))
86]
87
88has_error = False
89
90if missing_build_files:
91  has_error = True
92  print('Missing system build files: ' + ', '.join(missing_build_files))
93
94if syslibs != syslibs_from_workspace:
95  has_error = True
96  # Libs present in workspace files but not in the allowlist
97  missing_syslibs = syslibs_from_workspace - syslibs
98  if missing_syslibs:
99    libs = ', '.join(sorted(missing_syslibs))
100    print('Libs missing from syslibs_configure: ' + libs)
101  # Libs present in the allow list but not in workspace files
102  additional_syslibs = syslibs - syslibs_from_workspace
103  if additional_syslibs:
104    libs = ', '.join(sorted(additional_syslibs))
105    print('Libs missing in workspace (or superfluous in syslibs_configure): ' +
106          libs)
107
108sys.exit(1 if has_error else 0)
109