blob: 27519b4aba013e53f688513b798de4238e545022 [file] [log] [blame]
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""configure script to get build parameters from user."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21import errno
22import os
23import platform
24import re
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070025import subprocess
26import sys
27
Andrew Sellec9885ea2017-11-06 09:37:03 -080028# pylint: disable=g-import-not-at-top
Jonathan Hseu008910f2017-08-25 14:01:05 -070029try:
30 from shutil import which
31except ImportError:
32 from distutils.spawn import find_executable as which
Andrew Sellec9885ea2017-11-06 09:37:03 -080033# pylint: enable=g-import-not-at-top
Jonathan Hseu008910f2017-08-25 14:01:05 -070034
Michael Casefe2c8d82017-10-02 13:54:34 -070035_TF_BAZELRC = os.path.join(os.path.dirname(os.path.abspath(__file__)),
36 '.tf_configure.bazelrc')
Austin Anderson6afface2017-12-05 11:59:17 -080037_TF_WORKSPACE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
38 'WORKSPACE')
Dandelion Man?90e42f32017-12-15 18:15:07 -080039_DEFAULT_CUDA_VERSION = '9.0'
40_DEFAULT_CUDNN_VERSION = '7'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070041_DEFAULT_CUDA_COMPUTE_CAPABILITIES = '3.5,5.2'
42_DEFAULT_CUDA_PATH = '/usr/local/cuda'
43_DEFAULT_CUDA_PATH_LINUX = '/opt/cuda'
44_DEFAULT_CUDA_PATH_WIN = ('C:/Program Files/NVIDIA GPU Computing '
45 'Toolkit/CUDA/v%s' % _DEFAULT_CUDA_VERSION)
Guangda Lai76f69382018-01-25 23:59:19 -080046_DEFAULT_TENSORRT_PATH_LINUX = '/usr/lib/x86_64-linux-gnu'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070047_TF_OPENCL_VERSION = '1.2'
48_DEFAULT_COMPUTECPP_TOOLKIT_PATH = '/usr/local/computecpp'
Yifei Fengb1d8c592017-11-22 13:42:21 -080049_DEFAULT_TRISYCL_INCLUDE_DIR = '/usr/local/triSYCL/include'
Austin Anderson6afface2017-12-05 11:59:17 -080050_SUPPORTED_ANDROID_NDK_VERSIONS = [10, 11, 12, 13, 14, 15]
51
52_DEFAULT_PROMPT_ASK_ATTEMPTS = 10
53
54
55class UserInputError(Exception):
56 pass
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070057
58
59def is_windows():
60 return platform.system() == 'Windows'
61
62
63def is_linux():
64 return platform.system() == 'Linux'
65
66
67def is_macos():
68 return platform.system() == 'Darwin'
69
70
71def is_ppc64le():
72 return platform.machine() == 'ppc64le'
73
74
Jonathan Hseu008910f2017-08-25 14:01:05 -070075def is_cygwin():
76 return platform.system().startswith('CYGWIN_NT')
77
78
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070079def get_input(question):
80 try:
81 try:
82 answer = raw_input(question)
83 except NameError:
84 answer = input(question) # pylint: disable=bad-builtin
85 except EOFError:
86 answer = ''
87 return answer
88
89
90def symlink_force(target, link_name):
91 """Force symlink, equivalent of 'ln -sf'.
92
93 Args:
94 target: items to link to.
95 link_name: name of the link.
96 """
97 try:
98 os.symlink(target, link_name)
99 except OSError as e:
100 if e.errno == errno.EEXIST:
101 os.remove(link_name)
102 os.symlink(target, link_name)
103 else:
104 raise e
105
106
107def sed_in_place(filename, old, new):
108 """Replace old string with new string in file.
109
110 Args:
111 filename: string for filename.
112 old: string to replace.
113 new: new string to replace to.
114 """
115 with open(filename, 'r') as f:
116 filedata = f.read()
117 newdata = filedata.replace(old, new)
118 with open(filename, 'w') as f:
119 f.write(newdata)
120
121
122def remove_line_with(filename, token):
123 """Remove lines that contain token from file.
124
125 Args:
126 filename: string for filename.
127 token: string token to check if to remove a line from file or not.
128 """
129 with open(filename, 'r') as f:
130 filedata = f.read()
131
132 with open(filename, 'w') as f:
133 for line in filedata.strip().split('\n'):
134 if token not in line:
135 f.write(line + '\n')
136
137
138def write_to_bazelrc(line):
139 with open(_TF_BAZELRC, 'a') as f:
140 f.write(line + '\n')
141
142
143def write_action_env_to_bazelrc(var_name, var):
144 write_to_bazelrc('build --action_env %s="%s"' % (var_name, str(var)))
145
146
Jonathan Hseu008910f2017-08-25 14:01:05 -0700147def run_shell(cmd, allow_non_zero=False):
148 if allow_non_zero:
149 try:
150 output = subprocess.check_output(cmd)
151 except subprocess.CalledProcessError as e:
152 output = e.output
153 else:
154 output = subprocess.check_output(cmd)
155 return output.decode('UTF-8').strip()
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700156
157
158def cygpath(path):
159 """Convert path from posix to windows."""
Martin Wicked57572e2017-09-02 19:21:45 -0700160 return os.path.abspath(path).replace('\\', '/')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700161
162
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700163def get_python_path(environ_cp, python_bin_path):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700164 """Get the python site package paths."""
165 python_paths = []
166 if environ_cp.get('PYTHONPATH'):
167 python_paths = environ_cp.get('PYTHONPATH').split(':')
168 try:
Jonathan Hseu008910f2017-08-25 14:01:05 -0700169 library_paths = run_shell(
170 [python_bin_path, '-c',
Austin Anderson6afface2017-12-05 11:59:17 -0800171 'import site; print("\\n".join(site.getsitepackages()))']).split('\n')
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700172 except subprocess.CalledProcessError:
Jonathan Hseu008910f2017-08-25 14:01:05 -0700173 library_paths = [run_shell(
174 [python_bin_path, '-c',
175 'from distutils.sysconfig import get_python_lib;'
176 'print(get_python_lib())'])]
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700177
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700178 all_paths = set(python_paths + library_paths)
179
180 paths = []
181 for path in all_paths:
182 if os.path.isdir(path):
183 paths.append(path)
184 return paths
185
186
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700187def get_python_major_version(python_bin_path):
188 """Get the python major version."""
Jonathan Hseu008910f2017-08-25 14:01:05 -0700189 return run_shell([python_bin_path, '-c', 'import sys; print(sys.version[0])'])
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700190
191
Gunhan Gulsoyed89a2b2017-09-19 18:36:26 -0700192def setup_python(environ_cp):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700193 """Setup python related env variables."""
194 # Get PYTHON_BIN_PATH, default is the current running python.
195 default_python_bin_path = sys.executable
196 ask_python_bin_path = ('Please specify the location of python. [Default is '
197 '%s]: ') % default_python_bin_path
198 while True:
199 python_bin_path = get_from_env_or_user_or_default(
200 environ_cp, 'PYTHON_BIN_PATH', ask_python_bin_path,
201 default_python_bin_path)
202 # Check if the path is valid
Jonathan Hseu008910f2017-08-25 14:01:05 -0700203 if os.path.isfile(python_bin_path) and os.access(
204 python_bin_path, os.X_OK):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700205 break
206 elif not os.path.exists(python_bin_path):
207 print('Invalid python path: %s cannot be found.' % python_bin_path)
208 else:
209 print('%s is not executable. Is it the python binary?' % python_bin_path)
210 environ_cp['PYTHON_BIN_PATH'] = ''
211
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700212 # Convert python path to Windows style before checking lib and version
Martin Wicked57572e2017-09-02 19:21:45 -0700213 if is_windows() or is_cygwin():
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700214 python_bin_path = cygpath(python_bin_path)
215
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700216 # Get PYTHON_LIB_PATH
217 python_lib_path = environ_cp.get('PYTHON_LIB_PATH')
218 if not python_lib_path:
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700219 python_lib_paths = get_python_path(environ_cp, python_bin_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700220 if environ_cp.get('USE_DEFAULT_PYTHON_LIB_PATH') == '1':
Vijay Vasudevana1fba7f2017-07-28 10:58:56 -0700221 python_lib_path = python_lib_paths[0]
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700222 else:
Jonathan Hseu008910f2017-08-25 14:01:05 -0700223 print('Found possible Python library paths:\n %s' %
224 '\n '.join(python_lib_paths))
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700225 default_python_lib_path = python_lib_paths[0]
226 python_lib_path = get_input(
Jonathan Hseu008910f2017-08-25 14:01:05 -0700227 'Please input the desired Python library path to use. '
228 'Default is [%s]\n' % python_lib_paths[0])
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700229 if not python_lib_path:
230 python_lib_path = default_python_lib_path
Vijay Vasudevana1fba7f2017-07-28 10:58:56 -0700231 environ_cp['PYTHON_LIB_PATH'] = python_lib_path
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700232
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700233 python_major_version = get_python_major_version(python_bin_path)
234
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700235 # Convert python path to Windows style before writing into bazel.rc
Martin Wicked57572e2017-09-02 19:21:45 -0700236 if is_windows() or is_cygwin():
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700237 python_lib_path = cygpath(python_lib_path)
238
239 # Set-up env variables used by python_configure.bzl
240 write_action_env_to_bazelrc('PYTHON_BIN_PATH', python_bin_path)
241 write_action_env_to_bazelrc('PYTHON_LIB_PATH', python_lib_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700242 write_to_bazelrc('build --force_python=py%s' % python_major_version)
243 write_to_bazelrc('build --host_force_python=py%s' % python_major_version)
Gunhan Gulsoyed89a2b2017-09-19 18:36:26 -0700244 write_to_bazelrc('build --python_path=\"%s"' % python_bin_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700245 environ_cp['PYTHON_BIN_PATH'] = python_bin_path
246
247 # Write tools/python_bin_path.sh
248 with open('tools/python_bin_path.sh', 'w') as f:
249 f.write('export PYTHON_BIN_PATH="%s"' % python_bin_path)
250
251
252def reset_tf_configure_bazelrc():
253 """Reset file that contains customized config settings."""
254 open(_TF_BAZELRC, 'w').close()
255
256 home = os.path.expanduser('~')
257 if not os.path.exists('.bazelrc'):
258 if os.path.exists(os.path.join(home, '.bazelrc')):
259 with open('.bazelrc', 'a') as f:
Shanqing Caie2e3a942017-09-25 19:35:53 -0700260 f.write('import %s/.bazelrc\n' % home.replace('\\', '/'))
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700261 else:
262 open('.bazelrc', 'w').close()
263
264 remove_line_with('.bazelrc', 'tf_configure')
265 with open('.bazelrc', 'a') as f:
266 f.write('import %workspace%/.tf_configure.bazelrc\n')
267
268
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700269def cleanup_makefile():
270 """Delete any leftover BUILD files from the Makefile build.
271
272 These files could interfere with Bazel parsing.
273 """
274 makefile_download_dir = 'tensorflow/contrib/makefile/downloads'
275 if os.path.isdir(makefile_download_dir):
276 for root, _, filenames in os.walk(makefile_download_dir):
277 for f in filenames:
278 if f.endswith('BUILD'):
279 os.remove(os.path.join(root, f))
280
281
282def get_var(environ_cp,
283 var_name,
284 query_item,
285 enabled_by_default,
286 question=None,
287 yes_reply=None,
288 no_reply=None):
289 """Get boolean input from user.
290
291 If var_name is not set in env, ask user to enable query_item or not. If the
292 response is empty, use the default.
293
294 Args:
295 environ_cp: copy of the os.environ.
296 var_name: string for name of environment variable, e.g. "TF_NEED_HDFS".
297 query_item: string for feature related to the variable, e.g. "Hadoop File
298 System".
299 enabled_by_default: boolean for default behavior.
300 question: optional string for how to ask for user input.
Michael Cased90054e2018-02-07 14:36:00 -0800301 yes_reply: optional string for reply when feature is enabled.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700302 no_reply: optional string for reply when feature is disabled.
303
304 Returns:
305 boolean value of the variable.
Frank Chenc4ef9272018-01-10 11:36:52 -0800306
307 Raises:
308 UserInputError: if an environment variable is set, but it cannot be
309 interpreted as a boolean indicator, assume that the user has made a
310 scripting error, and will continue to provide invalid input.
311 Raise the error to avoid infinitely looping.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700312 """
313 if not question:
314 question = 'Do you wish to build TensorFlow with %s support?' % query_item
315 if not yes_reply:
316 yes_reply = '%s support will be enabled for TensorFlow.' % query_item
317 if not no_reply:
318 no_reply = 'No %s' % yes_reply
319
320 yes_reply += '\n'
321 no_reply += '\n'
322
323 if enabled_by_default:
324 question += ' [Y/n]: '
325 else:
326 question += ' [y/N]: '
327
328 var = environ_cp.get(var_name)
Frank Chenc4ef9272018-01-10 11:36:52 -0800329 if var is not None:
330 var_content = var.strip().lower()
331 true_strings = ('1', 't', 'true', 'y', 'yes')
332 false_strings = ('0', 'f', 'false', 'n', 'no')
333 if var_content in true_strings:
334 var = True
335 elif var_content in false_strings:
336 var = False
337 else:
338 raise UserInputError(
339 'Environment variable %s must be set as a boolean indicator.\n'
340 'The following are accepted as TRUE : %s.\n'
341 'The following are accepted as FALSE: %s.\n'
342 'Current value is %s.' % (
343 var_name, ', '.join(true_strings), ', '.join(false_strings),
344 var))
345
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700346 while var is None:
347 user_input_origin = get_input(question)
348 user_input = user_input_origin.strip().lower()
349 if user_input == 'y':
350 print(yes_reply)
351 var = True
352 elif user_input == 'n':
353 print(no_reply)
354 var = False
355 elif not user_input:
356 if enabled_by_default:
357 print(yes_reply)
358 var = True
359 else:
360 print(no_reply)
361 var = False
362 else:
363 print('Invalid selection: %s' % user_input_origin)
364 return var
365
366
367def set_build_var(environ_cp, var_name, query_item, option_name,
Michael Case98850a52017-09-14 13:35:57 -0700368 enabled_by_default, bazel_config_name=None):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700369 """Set if query_item will be enabled for the build.
370
371 Ask user if query_item will be enabled. Default is used if no input is given.
372 Set subprocess environment variable and write to .bazelrc if enabled.
373
374 Args:
375 environ_cp: copy of the os.environ.
376 var_name: string for name of environment variable, e.g. "TF_NEED_HDFS".
377 query_item: string for feature related to the variable, e.g. "Hadoop File
378 System".
379 option_name: string for option to define in .bazelrc.
380 enabled_by_default: boolean for default behavior.
Michael Case98850a52017-09-14 13:35:57 -0700381 bazel_config_name: Name for Bazel --config argument to enable build feature.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700382 """
383
384 var = str(int(get_var(environ_cp, var_name, query_item, enabled_by_default)))
385 environ_cp[var_name] = var
386 if var == '1':
387 write_to_bazelrc('build --define %s=true' % option_name)
Michael Case98850a52017-09-14 13:35:57 -0700388 elif bazel_config_name is not None:
389 # TODO(mikecase): Migrate all users of configure.py to use --config Bazel
390 # options and not to set build configs through environment variables.
391 write_to_bazelrc('build:%s --define %s=true'
392 % (bazel_config_name, option_name))
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700393
394
395def set_action_env_var(environ_cp,
396 var_name,
397 query_item,
398 enabled_by_default,
399 question=None,
400 yes_reply=None,
401 no_reply=None):
402 """Set boolean action_env variable.
403
404 Ask user if query_item will be enabled. Default is used if no input is given.
405 Set environment variable and write to .bazelrc.
406
407 Args:
408 environ_cp: copy of the os.environ.
409 var_name: string for name of environment variable, e.g. "TF_NEED_HDFS".
410 query_item: string for feature related to the variable, e.g. "Hadoop File
411 System".
412 enabled_by_default: boolean for default behavior.
413 question: optional string for how to ask for user input.
Michael Cased90054e2018-02-07 14:36:00 -0800414 yes_reply: optional string for reply when feature is enabled.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700415 no_reply: optional string for reply when feature is disabled.
416 """
417 var = int(
418 get_var(environ_cp, var_name, query_item, enabled_by_default, question,
419 yes_reply, no_reply))
420
421 write_action_env_to_bazelrc(var_name, var)
422 environ_cp[var_name] = str(var)
423
424
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700425def convert_version_to_int(version):
426 """Convert a version number to a integer that can be used to compare.
427
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700428 Version strings of the form X.YZ and X.Y.Z-xxxxx are supported. The
429 'xxxxx' part, for instance 'homebrew' on OS/X, is ignored.
430
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700431 Args:
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700432 version: a version to be converted
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700433
434 Returns:
435 An integer if converted successfully, otherwise return None.
436 """
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700437 version = version.split('-')[0]
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700438 version_segments = version.split('.')
439 for seg in version_segments:
440 if not seg.isdigit():
441 return None
442
443 version_str = ''.join(['%03d' % int(seg) for seg in version_segments])
444 return int(version_str)
445
446
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700447def check_bazel_version(min_version):
448 """Check installed bezel version is at least min_version.
449
450 Args:
451 min_version: string for minimum bazel version.
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700452
453 Returns:
454 The bazel version detected.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700455 """
Jonathan Hseu008910f2017-08-25 14:01:05 -0700456 if which('bazel') is None:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700457 print('Cannot find bazel. Please install bazel.')
458 sys.exit(0)
Jonathan Hseu008910f2017-08-25 14:01:05 -0700459 curr_version = run_shell(['bazel', '--batch', 'version'])
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700460
461 for line in curr_version.split('\n'):
462 if 'Build label: ' in line:
463 curr_version = line.split('Build label: ')[1]
464 break
465
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700466 min_version_int = convert_version_to_int(min_version)
467 curr_version_int = convert_version_to_int(curr_version)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700468
469 # Check if current bazel version can be detected properly.
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700470 if not curr_version_int:
471 print('WARNING: current bazel installation is not a release version.')
472 print('Make sure you are running at least bazel %s' % min_version)
473 return curr_version
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700474
Michael Cased94271a2017-08-22 17:26:52 -0700475 print('You have bazel %s installed.' % curr_version)
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700476
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700477 if curr_version_int < min_version_int:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700478 print('Please upgrade your bazel installation to version %s or higher to '
479 'build TensorFlow!' % min_version)
480 sys.exit(0)
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700481 return curr_version
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700482
483
484def set_cc_opt_flags(environ_cp):
485 """Set up architecture-dependent optimization flags.
486
487 Also append CC optimization flags to bazel.rc..
488
489 Args:
490 environ_cp: copy of the os.environ.
491 """
492 if is_ppc64le():
493 # gcc on ppc64le does not support -march, use mcpu instead
494 default_cc_opt_flags = '-mcpu=native'
495 else:
496 default_cc_opt_flags = '-march=native'
497 question = ('Please specify optimization flags to use during compilation when'
498 ' bazel option "--config=opt" is specified [Default is %s]: '
499 ) % default_cc_opt_flags
500 cc_opt_flags = get_from_env_or_user_or_default(environ_cp, 'CC_OPT_FLAGS',
501 question, default_cc_opt_flags)
502 for opt in cc_opt_flags.split():
Michael Case00177422017-11-10 13:14:03 -0800503 write_to_bazelrc('build:opt --copt=%s' % opt)
504 # It should be safe on the same build host.
505 write_to_bazelrc('build:opt --host_copt=-march=native')
Michael Casebb3355d2017-11-09 08:46:31 -0800506 write_to_bazelrc('build:opt --define with_default_optimizations=true')
Michael Case00177422017-11-10 13:14:03 -0800507 # TODO(mikecase): Remove these default defines once we are able to get
508 # TF Lite targets building without them.
Andrew Selle0b154392017-11-10 10:35:35 -0800509 write_to_bazelrc('build --copt=-DGEMMLOWP_ALLOW_SLOW_SCALAR_FALLBACK')
510 write_to_bazelrc('build --host_copt=-DGEMMLOWP_ALLOW_SLOW_SCALAR_FALLBACK')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700511
512
513def set_tf_cuda_clang(environ_cp):
514 """set TF_CUDA_CLANG action_env.
515
516 Args:
517 environ_cp: copy of the os.environ.
518 """
519 question = 'Do you want to use clang as CUDA compiler?'
520 yes_reply = 'Clang will be used as CUDA compiler.'
521 no_reply = 'nvcc will be used as CUDA compiler.'
522 set_action_env_var(
523 environ_cp,
524 'TF_CUDA_CLANG',
525 None,
526 False,
527 question=question,
528 yes_reply=yes_reply,
529 no_reply=no_reply)
530
531
A. Unique TensorFlowerfd29e952017-12-22 03:07:51 -0800532def set_tf_download_clang(environ_cp):
533 """Set TF_DOWNLOAD_CLANG action_env."""
534 question = 'Do you want to download a fresh release of clang? (Experimental)'
535 yes_reply = 'Clang will be downloaded and used to compile tensorflow.'
536 no_reply = 'Clang will not be downloaded.'
537 set_action_env_var(
538 environ_cp,
539 'TF_DOWNLOAD_CLANG',
540 None,
541 False,
542 question=question,
543 yes_reply=yes_reply,
544 no_reply=no_reply)
545
546
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700547def get_from_env_or_user_or_default(environ_cp, var_name, ask_for_var,
548 var_default):
549 """Get var_name either from env, or user or default.
550
551 If var_name has been set as environment variable, use the preset value, else
552 ask for user input. If no input is provided, the default is used.
553
554 Args:
555 environ_cp: copy of the os.environ.
556 var_name: string for name of environment variable, e.g. "TF_NEED_HDFS".
557 ask_for_var: string for how to ask for user input.
558 var_default: default value string.
559
560 Returns:
561 string value for var_name
562 """
563 var = environ_cp.get(var_name)
564 if not var:
565 var = get_input(ask_for_var)
Michael Cased94271a2017-08-22 17:26:52 -0700566 print('\n')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700567 if not var:
568 var = var_default
569 return var
570
571
572def set_clang_cuda_compiler_path(environ_cp):
573 """Set CLANG_CUDA_COMPILER_PATH."""
Jonathan Hseu008910f2017-08-25 14:01:05 -0700574 default_clang_path = which('clang') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700575 ask_clang_path = ('Please specify which clang should be used as device and '
576 'host compiler. [Default is %s]: ') % default_clang_path
577
578 while True:
579 clang_cuda_compiler_path = get_from_env_or_user_or_default(
580 environ_cp, 'CLANG_CUDA_COMPILER_PATH', ask_clang_path,
581 default_clang_path)
582 if os.path.exists(clang_cuda_compiler_path):
583 break
584
585 # Reset and retry
586 print('Invalid clang path: %s cannot be found.' % clang_cuda_compiler_path)
587 environ_cp['CLANG_CUDA_COMPILER_PATH'] = ''
588
589 # Set CLANG_CUDA_COMPILER_PATH
590 environ_cp['CLANG_CUDA_COMPILER_PATH'] = clang_cuda_compiler_path
591 write_action_env_to_bazelrc('CLANG_CUDA_COMPILER_PATH',
592 clang_cuda_compiler_path)
593
594
Austin Anderson6afface2017-12-05 11:59:17 -0800595def prompt_loop_or_load_from_env(
596 environ_cp,
597 var_name,
598 var_default,
599 ask_for_var,
600 check_success,
601 error_msg,
602 suppress_default_error=False,
603 n_ask_attempts=_DEFAULT_PROMPT_ASK_ATTEMPTS
604):
605 """Loop over user prompts for an ENV param until receiving a valid response.
606
607 For the env param var_name, read from the environment or verify user input
608 until receiving valid input. When done, set var_name in the environ_cp to its
609 new value.
610
611 Args:
612 environ_cp: (Dict) copy of the os.environ.
613 var_name: (String) string for name of environment variable, e.g. "TF_MYVAR".
614 var_default: (String) default value string.
615 ask_for_var: (String) string for how to ask for user input.
616 check_success: (Function) function that takes one argument and returns a
617 boolean. Should return True if the value provided is considered valid. May
618 contain a complex error message if error_msg does not provide enough
619 information. In that case, set suppress_default_error to True.
620 error_msg: (String) String with one and only one '%s'. Formatted with each
621 invalid response upon check_success(input) failure.
622 suppress_default_error: (Bool) Suppress the above error message in favor of
623 one from the check_success function.
624 n_ask_attempts: (Integer) Number of times to query for valid input before
625 raising an error and quitting.
626
627 Returns:
628 [String] The value of var_name after querying for input.
629
630 Raises:
631 UserInputError: if a query has been attempted n_ask_attempts times without
Frank Chenc4ef9272018-01-10 11:36:52 -0800632 success, assume that the user has made a scripting error, and will
633 continue to provide invalid input. Raise the error to avoid infinitely
634 looping.
Austin Anderson6afface2017-12-05 11:59:17 -0800635 """
636 default = environ_cp.get(var_name) or var_default
637 full_query = '%s [Default is %s]: ' % (
638 ask_for_var,
639 default,
640 )
641
642 for _ in range(n_ask_attempts):
643 val = get_from_env_or_user_or_default(environ_cp,
644 var_name,
645 full_query,
646 default)
647 if check_success(val):
648 break
649 if not suppress_default_error:
650 print(error_msg % val)
651 environ_cp[var_name] = ''
652 else:
653 raise UserInputError('Invalid %s setting was provided %d times in a row. '
654 'Assuming to be a scripting mistake.' %
655 (var_name, n_ask_attempts))
656
657 environ_cp[var_name] = val
658 return val
659
660
661def create_android_ndk_rule(environ_cp):
662 """Set ANDROID_NDK_HOME and write Android NDK WORKSPACE rule."""
663 if is_windows() or is_cygwin():
664 default_ndk_path = cygpath('%s/Android/Sdk/ndk-bundle' %
665 environ_cp['APPDATA'])
666 elif is_macos():
667 default_ndk_path = '%s/library/Android/Sdk/ndk-bundle' % environ_cp['HOME']
668 else:
669 default_ndk_path = '%s/Android/Sdk/ndk-bundle' % environ_cp['HOME']
670
671 def valid_ndk_path(path):
672 return (os.path.exists(path) and
673 os.path.exists(os.path.join(path, 'source.properties')))
674
675 android_ndk_home_path = prompt_loop_or_load_from_env(
676 environ_cp,
677 var_name='ANDROID_NDK_HOME',
678 var_default=default_ndk_path,
679 ask_for_var='Please specify the home path of the Android NDK to use.',
680 check_success=valid_ndk_path,
681 error_msg=('The path %s or its child file "source.properties" '
682 'does not exist.')
683 )
684
685 write_android_ndk_workspace_rule(android_ndk_home_path)
686
687
688def create_android_sdk_rule(environ_cp):
689 """Set Android variables and write Android SDK WORKSPACE rule."""
690 if is_windows() or is_cygwin():
691 default_sdk_path = cygpath('%s/Android/Sdk' % environ_cp['APPDATA'])
692 elif is_macos():
693 default_sdk_path = '%s/library/Android/Sdk/ndk-bundle' % environ_cp['HOME']
694 else:
695 default_sdk_path = '%s/Android/Sdk' % environ_cp['HOME']
696
697 def valid_sdk_path(path):
698 return (os.path.exists(path) and
699 os.path.exists(os.path.join(path, 'platforms')) and
700 os.path.exists(os.path.join(path, 'build-tools')))
701
702 android_sdk_home_path = prompt_loop_or_load_from_env(
703 environ_cp,
704 var_name='ANDROID_SDK_HOME',
705 var_default=default_sdk_path,
706 ask_for_var='Please specify the home path of the Android SDK to use.',
707 check_success=valid_sdk_path,
708 error_msg=('Either %s does not exist, or it does not contain the '
709 'subdirectories "platforms" and "build-tools".'))
710
711 platforms = os.path.join(android_sdk_home_path, 'platforms')
712 api_levels = sorted(os.listdir(platforms))
713 api_levels = [x.replace('android-', '') for x in api_levels]
714
715 def valid_api_level(api_level):
716 return os.path.exists(os.path.join(android_sdk_home_path,
717 'platforms',
718 'android-' + api_level))
719
720 android_api_level = prompt_loop_or_load_from_env(
721 environ_cp,
722 var_name='ANDROID_API_LEVEL',
723 var_default=api_levels[-1],
724 ask_for_var=('Please specify the Android SDK API level to use. '
725 '[Available levels: %s]') % api_levels,
726 check_success=valid_api_level,
727 error_msg='Android-%s is not present in the SDK path.')
728
729 build_tools = os.path.join(android_sdk_home_path, 'build-tools')
730 versions = sorted(os.listdir(build_tools))
731
732 def valid_build_tools(version):
733 return os.path.exists(os.path.join(android_sdk_home_path,
734 'build-tools',
735 version))
736
737 android_build_tools_version = prompt_loop_or_load_from_env(
738 environ_cp,
739 var_name='ANDROID_BUILD_TOOLS_VERSION',
740 var_default=versions[-1],
741 ask_for_var=('Please specify an Android build tools version to use. '
742 '[Available versions: %s]') % versions,
743 check_success=valid_build_tools,
744 error_msg=('The selected SDK does not have build-tools version %s '
745 'available.'))
746
747 write_android_sdk_workspace_rule(android_sdk_home_path,
748 android_build_tools_version,
749 android_api_level)
750
751
752def write_android_sdk_workspace_rule(android_sdk_home_path,
753 android_build_tools_version,
754 android_api_level):
755 print('Writing android_sdk_workspace rule.\n')
756 with open(_TF_WORKSPACE, 'a') as f:
757 f.write("""
758android_sdk_repository(
759 name="androidsdk",
760 api_level=%s,
761 path="%s",
762 build_tools_version="%s")\n
763""" % (android_api_level, android_sdk_home_path, android_build_tools_version))
764
765
766def write_android_ndk_workspace_rule(android_ndk_home_path):
767 print('Writing android_ndk_workspace rule.')
768 ndk_api_level = check_ndk_level(android_ndk_home_path)
769 if int(ndk_api_level) not in _SUPPORTED_ANDROID_NDK_VERSIONS:
770 print('WARNING: The API level of the NDK in %s is %s, which is not '
771 'supported by Bazel (officially supported versions: %s). Please use '
772 'another version. Compiling Android targets may result in confusing '
773 'errors.\n' % (android_ndk_home_path, ndk_api_level,
774 _SUPPORTED_ANDROID_NDK_VERSIONS))
775 with open(_TF_WORKSPACE, 'a') as f:
776 f.write("""
777android_ndk_repository(
778 name="androidndk",
779 path="%s",
780 api_level=%s)\n
781""" % (android_ndk_home_path, ndk_api_level))
782
783
784def check_ndk_level(android_ndk_home_path):
785 """Check the revision number of an Android NDK path."""
786 properties_path = '%s/source.properties' % android_ndk_home_path
787 if is_windows() or is_cygwin():
788 properties_path = cygpath(properties_path)
789 with open(properties_path, 'r') as f:
790 filedata = f.read()
791
792 revision = re.search(r'Pkg.Revision = (\d+)', filedata)
793 if revision:
794 return revision.group(1)
795 return None
796
797
798def workspace_has_any_android_rule():
799 """Check the WORKSPACE for existing android_*_repository rules."""
800 with open(_TF_WORKSPACE, 'r') as f:
801 workspace = f.read()
802 has_any_rule = re.search(r'^android_[ns]dk_repository',
803 workspace,
804 re.MULTILINE)
805 return has_any_rule
806
807
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700808def set_gcc_host_compiler_path(environ_cp):
809 """Set GCC_HOST_COMPILER_PATH."""
Jonathan Hseu008910f2017-08-25 14:01:05 -0700810 default_gcc_host_compiler_path = which('gcc') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700811 cuda_bin_symlink = '%s/bin/gcc' % environ_cp.get('CUDA_TOOLKIT_PATH')
812
813 if os.path.islink(cuda_bin_symlink):
814 # os.readlink is only available in linux
Jonathan Hseu008910f2017-08-25 14:01:05 -0700815 default_gcc_host_compiler_path = os.path.realpath(cuda_bin_symlink)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700816
Austin Anderson6afface2017-12-05 11:59:17 -0800817 gcc_host_compiler_path = prompt_loop_or_load_from_env(
818 environ_cp,
819 var_name='GCC_HOST_COMPILER_PATH',
820 var_default=default_gcc_host_compiler_path,
821 ask_for_var=
822 'Please specify which gcc should be used by nvcc as the host compiler.',
823 check_success=os.path.exists,
824 error_msg='Invalid gcc path. %s cannot be found.',
825 )
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700826
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700827 write_action_env_to_bazelrc('GCC_HOST_COMPILER_PATH', gcc_host_compiler_path)
828
829
830def set_tf_cuda_version(environ_cp):
831 """Set CUDA_TOOLKIT_PATH and TF_CUDA_VERSION."""
832 ask_cuda_version = (
833 'Please specify the CUDA SDK version you want to use, '
834 'e.g. 7.0. [Leave empty to default to CUDA %s]: ') % _DEFAULT_CUDA_VERSION
835
Austin Andersonf9a88f82017-12-13 11:49:40 -0800836 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700837 # Configure the Cuda SDK version to use.
838 tf_cuda_version = get_from_env_or_user_or_default(
839 environ_cp, 'TF_CUDA_VERSION', ask_cuda_version, _DEFAULT_CUDA_VERSION)
840
841 # Find out where the CUDA toolkit is installed
842 default_cuda_path = _DEFAULT_CUDA_PATH
Martin Wicked57572e2017-09-02 19:21:45 -0700843 if is_windows() or is_cygwin():
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700844 default_cuda_path = cygpath(
845 environ_cp.get('CUDA_PATH', _DEFAULT_CUDA_PATH_WIN))
846 elif is_linux():
847 # If the default doesn't exist, try an alternative default.
848 if (not os.path.exists(default_cuda_path)
849 ) and os.path.exists(_DEFAULT_CUDA_PATH_LINUX):
850 default_cuda_path = _DEFAULT_CUDA_PATH_LINUX
851 ask_cuda_path = ('Please specify the location where CUDA %s toolkit is'
852 ' installed. Refer to README.md for more details. '
853 '[Default is %s]: ') % (tf_cuda_version, default_cuda_path)
854 cuda_toolkit_path = get_from_env_or_user_or_default(
855 environ_cp, 'CUDA_TOOLKIT_PATH', ask_cuda_path, default_cuda_path)
856
857 if is_windows():
858 cuda_rt_lib_path = 'lib/x64/cudart.lib'
859 elif is_linux():
860 cuda_rt_lib_path = 'lib64/libcudart.so.%s' % tf_cuda_version
861 elif is_macos():
862 cuda_rt_lib_path = 'lib/libcudart.%s.dylib' % tf_cuda_version
863
864 cuda_toolkit_path_full = os.path.join(cuda_toolkit_path, cuda_rt_lib_path)
865 if os.path.exists(cuda_toolkit_path_full):
866 break
867
868 # Reset and retry
869 print('Invalid path to CUDA %s toolkit. %s cannot be found' %
870 (tf_cuda_version, cuda_toolkit_path_full))
871 environ_cp['TF_CUDA_VERSION'] = ''
872 environ_cp['CUDA_TOOLKIT_PATH'] = ''
873
Austin Andersonf9a88f82017-12-13 11:49:40 -0800874 else:
875 raise UserInputError('Invalid TF_CUDA_SETTING setting was provided %d '
876 'times in a row. Assuming to be a scripting mistake.' %
877 _DEFAULT_PROMPT_ASK_ATTEMPTS)
878
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700879 # Set CUDA_TOOLKIT_PATH and TF_CUDA_VERSION
880 environ_cp['CUDA_TOOLKIT_PATH'] = cuda_toolkit_path
881 write_action_env_to_bazelrc('CUDA_TOOLKIT_PATH', cuda_toolkit_path)
882 environ_cp['TF_CUDA_VERSION'] = tf_cuda_version
883 write_action_env_to_bazelrc('TF_CUDA_VERSION', tf_cuda_version)
884
885
Yifei Fengb1d8c592017-11-22 13:42:21 -0800886def set_tf_cudnn_version(environ_cp):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700887 """Set CUDNN_INSTALL_PATH and TF_CUDNN_VERSION."""
888 ask_cudnn_version = (
Jonathan Hseu008910f2017-08-25 14:01:05 -0700889 'Please specify the cuDNN version you want to use. '
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700890 '[Leave empty to default to cuDNN %s.0]: ') % _DEFAULT_CUDNN_VERSION
891
Austin Andersonf9a88f82017-12-13 11:49:40 -0800892 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700893 tf_cudnn_version = get_from_env_or_user_or_default(
894 environ_cp, 'TF_CUDNN_VERSION', ask_cudnn_version,
895 _DEFAULT_CUDNN_VERSION)
896
897 default_cudnn_path = environ_cp.get('CUDA_TOOLKIT_PATH')
898 ask_cudnn_path = (r'Please specify the location where cuDNN %s library is '
899 'installed. Refer to README.md for more details. [Default'
900 ' is %s]:') % (tf_cudnn_version, default_cudnn_path)
901 cudnn_install_path = get_from_env_or_user_or_default(
902 environ_cp, 'CUDNN_INSTALL_PATH', ask_cudnn_path, default_cudnn_path)
903
904 # Result returned from "read" will be used unexpanded. That make "~"
905 # unusable. Going through one more level of expansion to handle that.
906 cudnn_install_path = os.path.realpath(
907 os.path.expanduser(cudnn_install_path))
Martin Wicked57572e2017-09-02 19:21:45 -0700908 if is_windows() or is_cygwin():
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700909 cudnn_install_path = cygpath(cudnn_install_path)
910
911 if is_windows():
912 cuda_dnn_lib_path = 'lib/x64/cudnn.lib'
913 cuda_dnn_lib_alt_path = 'lib/x64/cudnn.lib'
914 elif is_linux():
915 cuda_dnn_lib_path = 'lib64/libcudnn.so.%s' % tf_cudnn_version
916 cuda_dnn_lib_alt_path = 'libcudnn.so.%s' % tf_cudnn_version
917 elif is_macos():
918 cuda_dnn_lib_path = 'lib/libcudnn.%s.dylib' % tf_cudnn_version
919 cuda_dnn_lib_alt_path = 'libcudnn.%s.dylib' % tf_cudnn_version
920
921 cuda_dnn_lib_path_full = os.path.join(cudnn_install_path, cuda_dnn_lib_path)
922 cuda_dnn_lib_alt_path_full = os.path.join(cudnn_install_path,
923 cuda_dnn_lib_alt_path)
924 if os.path.exists(cuda_dnn_lib_path_full) or os.path.exists(
925 cuda_dnn_lib_alt_path_full):
926 break
927
928 # Try another alternative for Linux
929 if is_linux():
Jonathan Hseu008910f2017-08-25 14:01:05 -0700930 ldconfig_bin = which('ldconfig') or '/sbin/ldconfig'
931 cudnn_path_from_ldconfig = run_shell([ldconfig_bin, '-p'])
932 cudnn_path_from_ldconfig = re.search('.*libcudnn.so .* => (.*)',
A. Unique TensorFlowere7223582017-09-06 17:57:04 -0700933 cudnn_path_from_ldconfig)
934 if cudnn_path_from_ldconfig:
935 cudnn_path_from_ldconfig = cudnn_path_from_ldconfig.group(1)
936 if os.path.exists('%s.%s' % (cudnn_path_from_ldconfig,
937 tf_cudnn_version)):
938 cudnn_install_path = os.path.dirname(cudnn_path_from_ldconfig)
939 break
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700940
941 # Reset and Retry
942 print(
943 'Invalid path to cuDNN %s toolkit. None of the following files can be '
944 'found:' % tf_cudnn_version)
945 print(cuda_dnn_lib_path_full)
946 print(cuda_dnn_lib_alt_path_full)
947 if is_linux():
948 print('%s.%s' % (cudnn_path_from_ldconfig, tf_cudnn_version))
949
950 environ_cp['TF_CUDNN_VERSION'] = ''
Austin Andersonf9a88f82017-12-13 11:49:40 -0800951 else:
952 raise UserInputError('Invalid TF_CUDNN setting was provided %d '
953 'times in a row. Assuming to be a scripting mistake.' %
954 _DEFAULT_PROMPT_ASK_ATTEMPTS)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700955
956 # Set CUDNN_INSTALL_PATH and TF_CUDNN_VERSION
957 environ_cp['CUDNN_INSTALL_PATH'] = cudnn_install_path
958 write_action_env_to_bazelrc('CUDNN_INSTALL_PATH', cudnn_install_path)
959 environ_cp['TF_CUDNN_VERSION'] = tf_cudnn_version
960 write_action_env_to_bazelrc('TF_CUDNN_VERSION', tf_cudnn_version)
961
962
Guangda Lai76f69382018-01-25 23:59:19 -0800963def set_tf_tensorrt_install_path(environ_cp):
964 """Set TENSORRT_INSTALL_PATH and TF_TENSORRT_VERSION.
965
966 Adapted from code contributed by Sami Kama (https://github.com/samikama).
967
968 Args:
969 environ_cp: copy of the os.environ.
970
971 Raises:
972 ValueError: if this method was called under non-Linux platform.
973 UserInputError: if user has provided invalid input multiple times.
974 """
975 if not is_linux():
976 raise ValueError('Currently TensorRT is only supported on Linux platform.')
977
978 # Ask user whether to add TensorRT support.
979 if str(int(get_var(
980 environ_cp, 'TF_NEED_TENSORRT', 'TensorRT', False))) != '1':
981 return
982
983 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS):
984 ask_tensorrt_path = (r'Please specify the location where TensorRT is '
985 'installed. [Default is %s]:') % (
986 _DEFAULT_TENSORRT_PATH_LINUX)
987 trt_install_path = get_from_env_or_user_or_default(
988 environ_cp, 'TENSORRT_INSTALL_PATH', ask_tensorrt_path,
989 _DEFAULT_TENSORRT_PATH_LINUX)
990
991 # Result returned from "read" will be used unexpanded. That make "~"
992 # unusable. Going through one more level of expansion to handle that.
993 trt_install_path = os.path.realpath(
994 os.path.expanduser(trt_install_path))
995
996 def find_libs(search_path):
997 """Search for libnvinfer.so in "search_path"."""
998 fl = set()
999 if os.path.exists(search_path) and os.path.isdir(search_path):
1000 fl.update([os.path.realpath(os.path.join(search_path, x))
1001 for x in os.listdir(search_path) if 'libnvinfer.so' in x])
1002 return fl
1003
1004 possible_files = find_libs(trt_install_path)
1005 possible_files.update(find_libs(os.path.join(trt_install_path, 'lib')))
1006 possible_files.update(find_libs(os.path.join(trt_install_path, 'lib64')))
1007
1008 def is_compatible(tensorrt_lib, cuda_ver, cudnn_ver):
1009 """Check the compatibility between tensorrt and cudnn/cudart libraries."""
1010 ldd_bin = which('ldd') or '/usr/bin/ldd'
1011 ldd_out = run_shell([ldd_bin, tensorrt_lib]).split(os.linesep)
1012 cudnn_pattern = re.compile('.*libcudnn.so\\.?(.*) =>.*$')
1013 cuda_pattern = re.compile('.*libcudart.so\\.?(.*) =>.*$')
1014 cudnn = None
1015 cudart = None
1016 for line in ldd_out:
1017 if 'libcudnn.so' in line:
1018 cudnn = cudnn_pattern.search(line)
1019 elif 'libcudart.so' in line:
1020 cudart = cuda_pattern.search(line)
1021 if cudnn and len(cudnn.group(1)):
1022 cudnn = convert_version_to_int(cudnn.group(1))
1023 if cudart and len(cudart.group(1)):
1024 cudart = convert_version_to_int(cudart.group(1))
1025 return (cudnn == cudnn_ver) and (cudart == cuda_ver)
1026
1027 cuda_ver = convert_version_to_int(environ_cp['TF_CUDA_VERSION'])
1028 cudnn_ver = convert_version_to_int(environ_cp['TF_CUDNN_VERSION'])
1029 nvinfer_pattern = re.compile('.*libnvinfer.so.?(.*)$')
1030 highest_ver = [0, None, None]
1031
1032 for lib_file in possible_files:
1033 if is_compatible(lib_file, cuda_ver, cudnn_ver):
1034 ver_str = nvinfer_pattern.search(lib_file).group(1)
1035 ver = convert_version_to_int(ver_str) if len(ver_str) else 0
1036 if ver > highest_ver[0]:
1037 highest_ver = [ver, ver_str, lib_file]
1038 if highest_ver[1] is not None:
1039 trt_install_path = os.path.dirname(highest_ver[2])
1040 tf_tensorrt_version = highest_ver[1]
1041 break
1042
1043 # Try another alternative from ldconfig.
1044 ldconfig_bin = which('ldconfig') or '/sbin/ldconfig'
1045 ldconfig_output = run_shell([ldconfig_bin, '-p'])
1046 search_result = re.search(
1047 '.*libnvinfer.so\\.?([0-9.]*).* => (.*)', ldconfig_output)
1048 if search_result:
1049 libnvinfer_path_from_ldconfig = search_result.group(2)
1050 if os.path.exists(libnvinfer_path_from_ldconfig):
1051 if is_compatible(libnvinfer_path_from_ldconfig, cuda_ver, cudnn_ver):
1052 trt_install_path = os.path.dirname(libnvinfer_path_from_ldconfig)
1053 tf_tensorrt_version = search_result.group(1)
1054 break
1055
1056 # Reset and Retry
1057 print('Invalid path to TensorRT. None of the following files can be found:')
1058 print(trt_install_path)
1059 print(os.path.join(trt_install_path, 'lib'))
1060 print(os.path.join(trt_install_path, 'lib64'))
1061 if search_result:
1062 print(libnvinfer_path_from_ldconfig)
1063
1064 else:
1065 raise UserInputError('Invalid TF_TENSORRT setting was provided %d '
1066 'times in a row. Assuming to be a scripting mistake.' %
1067 _DEFAULT_PROMPT_ASK_ATTEMPTS)
1068
1069 # Set TENSORRT_INSTALL_PATH and TF_TENSORRT_VERSION
1070 environ_cp['TENSORRT_INSTALL_PATH'] = trt_install_path
1071 write_action_env_to_bazelrc('TENSORRT_INSTALL_PATH', trt_install_path)
1072 environ_cp['TF_TENSORRT_VERSION'] = tf_tensorrt_version
1073 write_action_env_to_bazelrc('TF_TENSORRT_VERSION', tf_tensorrt_version)
1074
1075
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001076def get_native_cuda_compute_capabilities(environ_cp):
1077 """Get native cuda compute capabilities.
1078
1079 Args:
1080 environ_cp: copy of the os.environ.
1081 Returns:
1082 string of native cuda compute capabilities, separated by comma.
1083 """
1084 device_query_bin = os.path.join(
1085 environ_cp.get('CUDA_TOOLKIT_PATH'), 'extras/demo_suite/deviceQuery')
Jonathan Hseu008910f2017-08-25 14:01:05 -07001086 if os.path.isfile(device_query_bin) and os.access(device_query_bin, os.X_OK):
1087 try:
1088 output = run_shell(device_query_bin).split('\n')
1089 pattern = re.compile('[0-9]*\\.[0-9]*')
1090 output = [pattern.search(x) for x in output if 'Capability' in x]
1091 output = ','.join(x.group() for x in output if x is not None)
1092 except subprocess.CalledProcessError:
1093 output = ''
1094 else:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001095 output = ''
1096 return output
1097
1098
1099def set_tf_cuda_compute_capabilities(environ_cp):
1100 """Set TF_CUDA_COMPUTE_CAPABILITIES."""
1101 while True:
1102 native_cuda_compute_capabilities = get_native_cuda_compute_capabilities(
1103 environ_cp)
1104 if not native_cuda_compute_capabilities:
1105 default_cuda_compute_capabilities = _DEFAULT_CUDA_COMPUTE_CAPABILITIES
1106 else:
1107 default_cuda_compute_capabilities = native_cuda_compute_capabilities
1108
1109 ask_cuda_compute_capabilities = (
1110 'Please specify a list of comma-separated '
1111 'Cuda compute capabilities you want to '
1112 'build with.\nYou can find the compute '
1113 'capability of your device at: '
1114 'https://developer.nvidia.com/cuda-gpus.\nPlease'
1115 ' note that each additional compute '
1116 'capability significantly increases your '
1117 'build time and binary size. [Default is: %s]' %
1118 default_cuda_compute_capabilities)
1119 tf_cuda_compute_capabilities = get_from_env_or_user_or_default(
1120 environ_cp, 'TF_CUDA_COMPUTE_CAPABILITIES',
1121 ask_cuda_compute_capabilities, default_cuda_compute_capabilities)
1122 # Check whether all capabilities from the input is valid
1123 all_valid = True
1124 for compute_capability in tf_cuda_compute_capabilities.split(','):
Andrew Harp6e3e7d12017-08-21 12:10:44 -07001125 m = re.match('[0-9]+.[0-9]+', compute_capability)
1126 if not m:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001127 print('Invalid compute capability: ' % compute_capability)
1128 all_valid = False
Andrew Harp6e3e7d12017-08-21 12:10:44 -07001129 else:
1130 ver = int(m.group(0).split('.')[0])
1131 if ver < 3:
1132 print('Only compute capabilities 3.0 or higher are supported.')
1133 all_valid = False
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001134
1135 if all_valid:
1136 break
1137
1138 # Reset and Retry
1139 environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = ''
1140
1141 # Set TF_CUDA_COMPUTE_CAPABILITIES
1142 environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = tf_cuda_compute_capabilities
1143 write_action_env_to_bazelrc('TF_CUDA_COMPUTE_CAPABILITIES',
1144 tf_cuda_compute_capabilities)
1145
1146
1147def set_other_cuda_vars(environ_cp):
1148 """Set other CUDA related variables."""
1149 if is_windows():
1150 # The following three variables are needed for MSVC toolchain configuration
1151 # in Bazel
1152 environ_cp['CUDA_PATH'] = environ_cp.get('CUDA_TOOLKIT_PATH')
1153 environ_cp['CUDA_COMPUTE_CAPABILITIES'] = environ_cp.get(
1154 'TF_CUDA_COMPUTE_CAPABILITIES')
1155 environ_cp['NO_WHOLE_ARCHIVE_OPTION'] = 1
1156 write_action_env_to_bazelrc('CUDA_PATH', environ_cp.get('CUDA_PATH'))
1157 write_action_env_to_bazelrc('CUDA_COMPUTE_CAPABILITIE',
1158 environ_cp.get('CUDA_COMPUTE_CAPABILITIE'))
1159 write_action_env_to_bazelrc('NO_WHOLE_ARCHIVE_OPTION',
1160 environ_cp.get('NO_WHOLE_ARCHIVE_OPTION'))
1161 write_to_bazelrc('build --config=win-cuda')
1162 write_to_bazelrc('test --config=win-cuda')
1163 else:
1164 # If CUDA is enabled, always use GPU during build and test.
1165 if environ_cp.get('TF_CUDA_CLANG') == '1':
1166 write_to_bazelrc('build --config=cuda_clang')
1167 write_to_bazelrc('test --config=cuda_clang')
1168 else:
1169 write_to_bazelrc('build --config=cuda')
1170 write_to_bazelrc('test --config=cuda')
1171
1172
1173def set_host_cxx_compiler(environ_cp):
1174 """Set HOST_CXX_COMPILER."""
Jonathan Hseu008910f2017-08-25 14:01:05 -07001175 default_cxx_host_compiler = which('g++') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001176
Austin Anderson6afface2017-12-05 11:59:17 -08001177 host_cxx_compiler = prompt_loop_or_load_from_env(
1178 environ_cp,
1179 var_name='HOST_CXX_COMPILER',
1180 var_default=default_cxx_host_compiler,
1181 ask_for_var=('Please specify which C++ compiler should be used as the '
1182 'host C++ compiler.'),
1183 check_success=os.path.exists,
1184 error_msg='Invalid C++ compiler path. %s cannot be found.',
1185 )
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001186
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001187 write_action_env_to_bazelrc('HOST_CXX_COMPILER', host_cxx_compiler)
1188
1189
1190def set_host_c_compiler(environ_cp):
1191 """Set HOST_C_COMPILER."""
Jonathan Hseu008910f2017-08-25 14:01:05 -07001192 default_c_host_compiler = which('gcc') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001193
Austin Anderson6afface2017-12-05 11:59:17 -08001194 host_c_compiler = prompt_loop_or_load_from_env(
1195 environ_cp,
1196 var_name='HOST_C_COMPILER',
1197 var_default=default_c_host_compiler,
1198 ask_for_var=('Please specify which C compiler should be used as the host'
1199 'C compiler.'),
1200 check_success=os.path.exists,
1201 error_msg='Invalid C compiler path. %s cannot be found.',
1202 )
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001203
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001204 write_action_env_to_bazelrc('HOST_C_COMPILER', host_c_compiler)
1205
1206
1207def set_computecpp_toolkit_path(environ_cp):
1208 """Set COMPUTECPP_TOOLKIT_PATH."""
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001209
Austin Anderson6afface2017-12-05 11:59:17 -08001210 def toolkit_exists(toolkit_path):
1211 """Check if a computecpp toolkit path is valid."""
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001212 if is_linux():
1213 sycl_rt_lib_path = 'lib/libComputeCpp.so'
1214 else:
1215 sycl_rt_lib_path = ''
1216
Austin Anderson6afface2017-12-05 11:59:17 -08001217 sycl_rt_lib_path_full = os.path.join(toolkit_path,
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001218 sycl_rt_lib_path)
Austin Anderson6afface2017-12-05 11:59:17 -08001219 exists = os.path.exists(sycl_rt_lib_path_full)
1220 if not exists:
1221 print('Invalid SYCL %s library path. %s cannot be found' %
1222 (_TF_OPENCL_VERSION, sycl_rt_lib_path_full))
1223 return exists
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001224
Austin Anderson6afface2017-12-05 11:59:17 -08001225 computecpp_toolkit_path = prompt_loop_or_load_from_env(
1226 environ_cp,
1227 var_name='COMPUTECPP_TOOLKIT_PATH',
1228 var_default=_DEFAULT_COMPUTECPP_TOOLKIT_PATH,
1229 ask_for_var=(
1230 'Please specify the location where ComputeCpp for SYCL %s is '
1231 'installed.' % _TF_OPENCL_VERSION),
1232 check_success=toolkit_exists,
1233 error_msg='Invalid SYCL compiler path. %s cannot be found.',
1234 suppress_default_error=True)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001235
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001236 write_action_env_to_bazelrc('COMPUTECPP_TOOLKIT_PATH',
1237 computecpp_toolkit_path)
1238
Michael Cased31531a2018-01-05 14:09:41 -08001239
Dandelion Man?90e42f32017-12-15 18:15:07 -08001240def set_trisycl_include_dir(environ_cp):
Michael Cased31531a2018-01-05 14:09:41 -08001241 """Set TRISYCL_INCLUDE_DIR."""
Frank Chenc4ef9272018-01-10 11:36:52 -08001242
Dandelion Man?90e42f32017-12-15 18:15:07 -08001243 ask_trisycl_include_dir = ('Please specify the location of the triSYCL '
1244 'include directory. (Use --config=sycl_trisycl '
1245 'when building with Bazel) '
1246 '[Default is %s]: '
Michael Cased31531a2018-01-05 14:09:41 -08001247 ) % (_DEFAULT_TRISYCL_INCLUDE_DIR)
Frank Chenc4ef9272018-01-10 11:36:52 -08001248
Dandelion Man?90e42f32017-12-15 18:15:07 -08001249 while True:
1250 trisycl_include_dir = get_from_env_or_user_or_default(
Michael Cased31531a2018-01-05 14:09:41 -08001251 environ_cp, 'TRISYCL_INCLUDE_DIR', ask_trisycl_include_dir,
1252 _DEFAULT_TRISYCL_INCLUDE_DIR)
Dandelion Man?90e42f32017-12-15 18:15:07 -08001253 if os.path.exists(trisycl_include_dir):
1254 break
1255
1256 print('Invalid triSYCL include directory, %s cannot be found'
1257 % (trisycl_include_dir))
1258
1259 # Set TRISYCL_INCLUDE_DIR
1260 environ_cp['TRISYCL_INCLUDE_DIR'] = trisycl_include_dir
1261 write_action_env_to_bazelrc('TRISYCL_INCLUDE_DIR',
1262 trisycl_include_dir)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001263
Yifei Fengb1d8c592017-11-22 13:42:21 -08001264
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001265def set_mpi_home(environ_cp):
1266 """Set MPI_HOME."""
Austin Anderson6afface2017-12-05 11:59:17 -08001267
Jonathan Hseu008910f2017-08-25 14:01:05 -07001268 default_mpi_home = which('mpirun') or which('mpiexec') or ''
1269 default_mpi_home = os.path.dirname(os.path.dirname(default_mpi_home))
1270
Austin Anderson6afface2017-12-05 11:59:17 -08001271 def valid_mpi_path(mpi_home):
1272 exists = (os.path.exists(os.path.join(mpi_home, 'include')) and
1273 os.path.exists(os.path.join(mpi_home, 'lib')))
1274 if not exists:
1275 print('Invalid path to the MPI Toolkit. %s or %s cannot be found' %
1276 (os.path.join(mpi_home, 'include'),
1277 os.path.exists(os.path.join(mpi_home, 'lib'))))
1278 return exists
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001279
Austin Anderson6afface2017-12-05 11:59:17 -08001280 _ = prompt_loop_or_load_from_env(
1281 environ_cp,
1282 var_name='MPI_HOME',
1283 var_default=default_mpi_home,
1284 ask_for_var='Please specify the MPI toolkit folder.',
1285 check_success=valid_mpi_path,
1286 error_msg='',
1287 suppress_default_error=True)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001288
1289
1290def set_other_mpi_vars(environ_cp):
1291 """Set other MPI related variables."""
1292 # Link the MPI header files
1293 mpi_home = environ_cp.get('MPI_HOME')
1294 symlink_force('%s/include/mpi.h' % mpi_home, 'third_party/mpi/mpi.h')
1295
1296 # Determine if we use OpenMPI or MVAPICH, these require different header files
1297 # to be included here to make bazel dependency checker happy
1298 if os.path.exists(os.path.join(mpi_home, 'include/mpi_portable_platform.h')):
1299 symlink_force(
1300 os.path.join(mpi_home, 'include/mpi_portable_platform.h'),
1301 'third_party/mpi/mpi_portable_platform.h')
1302 # TODO(gunan): avoid editing files in configure
1303 sed_in_place('third_party/mpi/mpi.bzl', 'MPI_LIB_IS_OPENMPI=False',
1304 'MPI_LIB_IS_OPENMPI=True')
1305 else:
1306 # MVAPICH / MPICH
1307 symlink_force(
1308 os.path.join(mpi_home, 'include/mpio.h'), 'third_party/mpi/mpio.h')
1309 symlink_force(
1310 os.path.join(mpi_home, 'include/mpicxx.h'), 'third_party/mpi/mpicxx.h')
1311 # TODO(gunan): avoid editing files in configure
1312 sed_in_place('third_party/mpi/mpi.bzl', 'MPI_LIB_IS_OPENMPI=True',
1313 'MPI_LIB_IS_OPENMPI=False')
1314
1315 if os.path.exists(os.path.join(mpi_home, 'lib/libmpi.so')):
1316 symlink_force(
1317 os.path.join(mpi_home, 'lib/libmpi.so'), 'third_party/mpi/libmpi.so')
1318 else:
1319 raise ValueError('Cannot find the MPI library file in %s/lib' % mpi_home)
1320
1321
A. Unique TensorFlower061c3592017-11-13 14:21:04 -08001322def set_grpc_build_flags():
1323 write_to_bazelrc('build --define grpc_no_ares=true')
1324
Michael Cased31531a2018-01-05 14:09:41 -08001325
Dandelion Man?90e42f32017-12-15 18:15:07 -08001326def set_windows_build_flags():
1327 if is_windows():
1328 # The non-monolithic build is not supported yet
1329 write_to_bazelrc('build --config monolithic')
1330 # Suppress warning messages
1331 write_to_bazelrc('build --copt=-w --host_copt=-w')
1332 # Output more verbose information when something goes wrong
1333 write_to_bazelrc('build --verbose_failures')
1334
A. Unique TensorFlower061c3592017-11-13 14:21:04 -08001335
Michael Cased31531a2018-01-05 14:09:41 -08001336def config_info_line(name, help_text):
1337 """Helper function to print formatted help text for Bazel config options."""
1338 print('\t--config=%-12s\t# %s' % (name, help_text))
1339
1340
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001341def main():
1342 # Make a copy of os.environ to be clear when functions and getting and setting
1343 # environment variables.
1344 environ_cp = dict(os.environ)
1345
Gunhan Gulsoyed89a2b2017-09-19 18:36:26 -07001346 check_bazel_version('0.5.4')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001347
1348 reset_tf_configure_bazelrc()
1349 cleanup_makefile()
Gunhan Gulsoyed89a2b2017-09-19 18:36:26 -07001350 setup_python(environ_cp)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001351
1352 if is_windows():
Benoit Steiner355e25e2017-10-24 19:47:46 -07001353 environ_cp['TF_NEED_S3'] = '0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001354 environ_cp['TF_NEED_GCP'] = '0'
1355 environ_cp['TF_NEED_HDFS'] = '0'
1356 environ_cp['TF_NEED_JEMALLOC'] = '0'
Michael Cased90054e2018-02-07 14:36:00 -08001357 environ_cp['TF_NEED_KAFKA'] = '0'
Yifei Fengb1d8c592017-11-22 13:42:21 -08001358 environ_cp['TF_NEED_OPENCL_SYCL'] = '0'
1359 environ_cp['TF_NEED_COMPUTECPP'] = '0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001360 environ_cp['TF_NEED_OPENCL'] = '0'
1361 environ_cp['TF_CUDA_CLANG'] = '0'
Guangda Lai76f69382018-01-25 23:59:19 -08001362 environ_cp['TF_NEED_TENSORRT'] = '0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001363
1364 if is_macos():
1365 environ_cp['TF_NEED_JEMALLOC'] = '0'
Guangda Lai76f69382018-01-25 23:59:19 -08001366 environ_cp['TF_NEED_TENSORRT'] = '0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001367
1368 set_build_var(environ_cp, 'TF_NEED_JEMALLOC', 'jemalloc as malloc',
1369 'with_jemalloc', True)
1370 set_build_var(environ_cp, 'TF_NEED_GCP', 'Google Cloud Platform',
Benoit Steiner355e25e2017-10-24 19:47:46 -07001371 'with_gcp_support', True, 'gcp')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001372 set_build_var(environ_cp, 'TF_NEED_HDFS', 'Hadoop File System',
Benoit Steiner355e25e2017-10-24 19:47:46 -07001373 'with_hdfs_support', True, 'hdfs')
Michael Case7e4e3362017-10-09 13:31:15 -07001374 set_build_var(environ_cp, 'TF_NEED_S3', 'Amazon S3 File System',
1375 'with_s3_support', True, 's3')
Michael Cased90054e2018-02-07 14:36:00 -08001376 set_build_var(environ_cp, 'TF_NEED_KAFKA', 'Apache Kafka Platform',
1377 'with_kafka_support', False, 'kafka')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001378 set_build_var(environ_cp, 'TF_ENABLE_XLA', 'XLA JIT', 'with_xla_support',
Michael Case98850a52017-09-14 13:35:57 -07001379 False, 'xla')
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -07001380 set_build_var(environ_cp, 'TF_NEED_GDR', 'GDR', 'with_gdr_support',
Michael Case98850a52017-09-14 13:35:57 -07001381 False, 'gdr')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001382 set_build_var(environ_cp, 'TF_NEED_VERBS', 'VERBS', 'with_verbs_support',
Michael Case98850a52017-09-14 13:35:57 -07001383 False, 'verbs')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001384
Yifei Fengb1d8c592017-11-22 13:42:21 -08001385 set_action_env_var(environ_cp, 'TF_NEED_OPENCL_SYCL', 'OpenCL SYCL', False)
1386 if environ_cp.get('TF_NEED_OPENCL_SYCL') == '1':
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001387 set_host_cxx_compiler(environ_cp)
1388 set_host_c_compiler(environ_cp)
Yifei Fengb1d8c592017-11-22 13:42:21 -08001389 set_action_env_var(environ_cp, 'TF_NEED_COMPUTECPP', 'ComputeCPP', True)
1390 if environ_cp.get('TF_NEED_COMPUTECPP') == '1':
1391 set_computecpp_toolkit_path(environ_cp)
1392 else:
1393 set_trisycl_include_dir(environ_cp)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001394
1395 set_action_env_var(environ_cp, 'TF_NEED_CUDA', 'CUDA', False)
A. Unique TensorFlower24cbb2a2017-09-08 07:45:44 -07001396 if (environ_cp.get('TF_NEED_CUDA') == '1' and
1397 'TF_CUDA_CONFIG_REPO' not in environ_cp):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001398 set_tf_cuda_version(environ_cp)
Yifei Fengb1d8c592017-11-22 13:42:21 -08001399 set_tf_cudnn_version(environ_cp)
Guangda Lai76f69382018-01-25 23:59:19 -08001400 if is_linux():
1401 set_tf_tensorrt_install_path(environ_cp)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001402 set_tf_cuda_compute_capabilities(environ_cp)
1403
1404 set_tf_cuda_clang(environ_cp)
1405 if environ_cp.get('TF_CUDA_CLANG') == '1':
A. Unique TensorFlowerfd29e952017-12-22 03:07:51 -08001406 if not is_windows():
1407 # Ask if we want to download clang release while building.
1408 set_tf_download_clang(environ_cp)
1409 else:
1410 # We use bazel's generated crosstool on Windows and there is no
1411 # way to provide downloaded toolchain for that yet.
1412 # TODO(ibiryukov): Investigate using clang as a cuda compiler on
1413 # Windows.
1414 environ_cp['TF_DOWNLOAD_CLANG'] = '0'
1415
1416 if environ_cp.get('TF_DOWNLOAD_CLANG') != '1':
1417 # Set up which clang we should use as the cuda / host compiler.
1418 set_clang_cuda_compiler_path(environ_cp)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001419 else:
1420 # Set up which gcc nvcc should use as the host compiler
1421 # No need to set this on Windows
1422 if not is_windows():
1423 set_gcc_host_compiler_path(environ_cp)
1424 set_other_cuda_vars(environ_cp)
1425
1426 set_build_var(environ_cp, 'TF_NEED_MPI', 'MPI', 'with_mpi_support', False)
1427 if environ_cp.get('TF_NEED_MPI') == '1':
1428 set_mpi_home(environ_cp)
1429 set_other_mpi_vars(environ_cp)
1430
A. Unique TensorFlower061c3592017-11-13 14:21:04 -08001431 set_grpc_build_flags()
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001432 set_cc_opt_flags(environ_cp)
Dandelion Man?90e42f32017-12-15 18:15:07 -08001433 set_windows_build_flags()
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001434
Austin Anderson6afface2017-12-05 11:59:17 -08001435 if workspace_has_any_android_rule():
1436 print('The WORKSPACE file has at least one of ["android_sdk_repository", '
1437 '"android_ndk_repository"] already set. Will not ask to help '
1438 'configure the WORKSPACE. Please delete the existing rules to '
1439 'activate the helper.\n')
1440 else:
1441 if get_var(
1442 environ_cp, 'TF_SET_ANDROID_WORKSPACE', 'android workspace',
1443 False,
1444 ('Would you like to interactively configure ./WORKSPACE for '
1445 'Android builds?'),
1446 'Searching for NDK and SDK installations.',
1447 'Not configuring the WORKSPACE for Android builds.'):
1448 create_android_ndk_rule(environ_cp)
1449 create_android_sdk_rule(environ_cp)
1450
Michael Cased31531a2018-01-05 14:09:41 -08001451 print('Preconfigured Bazel build configs. You can use any of the below by '
1452 'adding "--config=<>" to your build command. See tools/bazel.rc for '
1453 'more details.')
1454 config_info_line('mkl', 'Build with MKL support.')
1455 config_info_line('monolithic', 'Config for mostly static monolithic build.')
Guangda Lai76f69382018-01-25 23:59:19 -08001456 config_info_line('tensorrt', 'Build with TensorRT support.')
Austin Anderson6afface2017-12-05 11:59:17 -08001457
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001458if __name__ == '__main__':
1459 main()