xref: /aosp_15_r20/external/tensorflow/tensorflow/tools/ci_build/builds/check_system_libs.py (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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')
29workspace0_path = os.path.join(tf_source_path, 'tensorflow', 'workspace0.bzl')
30workspace_glob = os.path.join(tf_source_path, 'tensorflow', 'workspace*.bzl')
31third_party_path = os.path.join(tf_source_path, 'third_party')
32third_party_glob = os.path.join(third_party_path, '*', 'workspace.bzl')
33
34if not os.path.isdir(tf_source_path):
35  raise ValueError('The path to the TensorFlow source must be passed as'
36                   ' the first argument')
37
38if not os.path.isfile(syslibs_configure_path):
39  raise ValueError('Could not find syslibs_configure.bzl at %s' %
40                   syslibs_configure_path)
41
42if not os.path.isfile(workspace0_path):
43  raise ValueError('Could not find workspace0.bzl at %s' % workspace0_path)
44
45def extract_valid_libs(filepath):
46  """Evaluate syslibs_configure.bzl, return the VALID_LIBS set from that file."""
47
48  # Stub only
49  def repository_rule(**kwargs):  # pylint: disable=unused-variable
50    del kwargs
51
52  # Populates VALID_LIBS
53  with open(filepath, 'r') as f:
54    f_globals = {'repository_rule': repository_rule}
55    f_locals = {}
56    exec(f.read(), f_globals, f_locals)  # pylint: disable=exec-used
57
58  return set(f_locals['VALID_LIBS'])
59
60
61def extract_system_builds(filepath):
62  """Extract the 'name' argument of all rules with a system_build_file argument."""
63  lib_names = []
64  system_build_files = []
65  current_name = None
66  with open(filepath, 'r') as f:
67    for line in f:
68      line = line.strip()
69      if line.startswith('name = '):
70        current_name = line[7:-1].strip('"')
71      elif line.startswith('system_build_file = '):
72        lib_names.append(current_name)
73        # Split at '=' to extract rhs, then extract value between quotes
74        system_build_spec = line.split('=')[-1].split('"')[1]
75        assert system_build_spec.startswith('//')
76        system_build_files.append(system_build_spec[2:].replace(':', os.sep))
77  return lib_names, system_build_files
78
79
80syslibs = extract_valid_libs(syslibs_configure_path)
81
82syslibs_from_workspace = set()
83system_build_files_from_workspace = []
84for current_path in glob.glob(workspace_glob) + glob.glob(third_party_glob):
85  cur_lib_names, build_files = extract_system_builds(current_path)
86  syslibs_from_workspace.update(cur_lib_names)
87  system_build_files_from_workspace.extend(build_files)
88
89missing_build_files = [
90    file for file in system_build_files_from_workspace
91    if not os.path.isfile(os.path.join(tf_source_path, file))
92]
93
94has_error = False
95
96if missing_build_files:
97  has_error = True
98  print('Missing system build files: ' + ', '.join(missing_build_files))
99
100if syslibs != syslibs_from_workspace:
101  has_error = True
102  # Libs present in workspace files but not in the allowlist
103  missing_syslibs = syslibs_from_workspace - syslibs
104  if missing_syslibs:
105    libs = ', '.join(sorted(missing_syslibs))
106    print('Libs missing from syslibs_configure: ' + libs)
107  # Libs present in the allow list but not in workspace files
108  additional_syslibs = syslibs - syslibs_from_workspace
109  if additional_syslibs:
110    libs = ', '.join(sorted(additional_syslibs))
111    print('Libs missing in workspace (or superfluous in syslibs_configure): ' +
112          libs)
113
114sys.exit(1 if has_error else 0)
115