blob: e195d6554ec84391a232ec798c431e16c1c856cd [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
Shanqing Cai71445712018-03-12 19:33:52 -070021import argparse
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070022import errno
23import os
24import platform
25import re
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070026import subprocess
27import sys
28
Andrew Sellec9885ea2017-11-06 09:37:03 -080029# pylint: disable=g-import-not-at-top
Jonathan Hseu008910f2017-08-25 14:01:05 -070030try:
31 from shutil import which
32except ImportError:
33 from distutils.spawn import find_executable as which
Andrew Sellec9885ea2017-11-06 09:37:03 -080034# pylint: enable=g-import-not-at-top
Jonathan Hseu008910f2017-08-25 14:01:05 -070035
Dandelion Man?90e42f32017-12-15 18:15:07 -080036_DEFAULT_CUDA_VERSION = '9.0'
37_DEFAULT_CUDNN_VERSION = '7'
Smit Hinsu63e6b9b2018-07-13 12:46:24 -070038_DEFAULT_NCCL_VERSION = '2.2'
Smit Hinsufe7d1d92018-07-14 13:16:58 -070039_DEFAULT_CUDA_COMPUTE_CAPABILITIES = '3.5,7.0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070040_DEFAULT_CUDA_PATH = '/usr/local/cuda'
41_DEFAULT_CUDA_PATH_LINUX = '/opt/cuda'
42_DEFAULT_CUDA_PATH_WIN = ('C:/Program Files/NVIDIA GPU Computing '
43 'Toolkit/CUDA/v%s' % _DEFAULT_CUDA_VERSION)
44_TF_OPENCL_VERSION = '1.2'
45_DEFAULT_COMPUTECPP_TOOLKIT_PATH = '/usr/local/computecpp'
Yifei Fengb1d8c592017-11-22 13:42:21 -080046_DEFAULT_TRISYCL_INCLUDE_DIR = '/usr/local/triSYCL/include'
A. Unique TensorFlowerd340f472018-08-30 14:00:41 -070047_SUPPORTED_ANDROID_NDK_VERSIONS = [10, 11, 12, 13, 14, 15, 16]
Austin Anderson6afface2017-12-05 11:59:17 -080048
49_DEFAULT_PROMPT_ASK_ATTEMPTS = 10
50
Shanqing Cai71445712018-03-12 19:33:52 -070051_TF_WORKSPACE_ROOT = os.path.abspath(os.path.dirname(__file__))
52_TF_BAZELRC_FILENAME = '.tf_configure.bazelrc'
53_TF_BAZELRC = os.path.join(_TF_WORKSPACE_ROOT, _TF_BAZELRC_FILENAME)
54_TF_WORKSPACE = os.path.join(_TF_WORKSPACE_ROOT, 'WORKSPACE')
55
Jason Furmanek7c234152018-09-26 04:44:12 +000056NCCL_LIB_PATHS = [
57 "lib64/",
58 "lib/powerpc64le-linux-gnu/",
59 "lib/x86_64-linux-gnu/",
60 ""
61]
Austin Anderson6afface2017-12-05 11:59:17 -080062
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -070063if platform.machine() == 'ppc64le':
64 _DEFAULT_TENSORRT_PATH_LINUX = '/usr/lib/powerpc64le-linux-gnu/'
65else:
66 _DEFAULT_TENSORRT_PATH_LINUX = '/usr/lib/%s-linux-gnu' % platform.machine()
67
Austin Anderson6afface2017-12-05 11:59:17 -080068
69class UserInputError(Exception):
70 pass
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070071
72
73def is_windows():
74 return platform.system() == 'Windows'
75
76
77def is_linux():
78 return platform.system() == 'Linux'
79
80
81def is_macos():
82 return platform.system() == 'Darwin'
83
84
85def is_ppc64le():
86 return platform.machine() == 'ppc64le'
87
88
Jonathan Hseu008910f2017-08-25 14:01:05 -070089def is_cygwin():
90 return platform.system().startswith('CYGWIN_NT')
91
92
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070093def get_input(question):
94 try:
95 try:
96 answer = raw_input(question)
97 except NameError:
98 answer = input(question) # pylint: disable=bad-builtin
99 except EOFError:
100 answer = ''
101 return answer
102
103
104def symlink_force(target, link_name):
105 """Force symlink, equivalent of 'ln -sf'.
106
107 Args:
108 target: items to link to.
109 link_name: name of the link.
110 """
111 try:
112 os.symlink(target, link_name)
113 except OSError as e:
114 if e.errno == errno.EEXIST:
115 os.remove(link_name)
116 os.symlink(target, link_name)
117 else:
118 raise e
119
120
121def sed_in_place(filename, old, new):
122 """Replace old string with new string in file.
123
124 Args:
125 filename: string for filename.
126 old: string to replace.
127 new: new string to replace to.
128 """
129 with open(filename, 'r') as f:
130 filedata = f.read()
131 newdata = filedata.replace(old, new)
132 with open(filename, 'w') as f:
133 f.write(newdata)
134
135
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700136def write_to_bazelrc(line):
137 with open(_TF_BAZELRC, 'a') as f:
138 f.write(line + '\n')
139
140
141def write_action_env_to_bazelrc(var_name, var):
142 write_to_bazelrc('build --action_env %s="%s"' % (var_name, str(var)))
143
144
Jonathan Hseu008910f2017-08-25 14:01:05 -0700145def run_shell(cmd, allow_non_zero=False):
146 if allow_non_zero:
147 try:
148 output = subprocess.check_output(cmd)
149 except subprocess.CalledProcessError as e:
150 output = e.output
151 else:
152 output = subprocess.check_output(cmd)
153 return output.decode('UTF-8').strip()
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700154
155
156def cygpath(path):
157 """Convert path from posix to windows."""
Martin Wicked57572e2017-09-02 19:21:45 -0700158 return os.path.abspath(path).replace('\\', '/')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700159
160
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700161def get_python_path(environ_cp, python_bin_path):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700162 """Get the python site package paths."""
163 python_paths = []
164 if environ_cp.get('PYTHONPATH'):
165 python_paths = environ_cp.get('PYTHONPATH').split(':')
166 try:
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700167 library_paths = run_shell([
168 python_bin_path, '-c',
169 'import site; print("\\n".join(site.getsitepackages()))'
170 ]).split('\n')
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700171 except subprocess.CalledProcessError:
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700172 library_paths = [
173 run_shell([
174 python_bin_path, '-c',
175 'from distutils.sysconfig import get_python_lib;'
176 'print(get_python_lib())'
177 ])
178 ]
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700179
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700180 all_paths = set(python_paths + library_paths)
181
182 paths = []
183 for path in all_paths:
184 if os.path.isdir(path):
185 paths.append(path)
186 return paths
187
188
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700189def get_python_major_version(python_bin_path):
190 """Get the python major version."""
Jonathan Hseu008910f2017-08-25 14:01:05 -0700191 return run_shell([python_bin_path, '-c', 'import sys; print(sys.version[0])'])
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700192
193
Gunhan Gulsoyed89a2b2017-09-19 18:36:26 -0700194def setup_python(environ_cp):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700195 """Setup python related env variables."""
196 # Get PYTHON_BIN_PATH, default is the current running python.
197 default_python_bin_path = sys.executable
198 ask_python_bin_path = ('Please specify the location of python. [Default is '
199 '%s]: ') % default_python_bin_path
200 while True:
201 python_bin_path = get_from_env_or_user_or_default(
202 environ_cp, 'PYTHON_BIN_PATH', ask_python_bin_path,
203 default_python_bin_path)
204 # Check if the path is valid
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700205 if os.path.isfile(python_bin_path) and os.access(python_bin_path, os.X_OK):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700206 break
207 elif not os.path.exists(python_bin_path):
208 print('Invalid python path: %s cannot be found.' % python_bin_path)
209 else:
210 print('%s is not executable. Is it the python binary?' % python_bin_path)
211 environ_cp['PYTHON_BIN_PATH'] = ''
212
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700213 # Convert python path to Windows style before checking lib and version
Martin Wicked57572e2017-09-02 19:21:45 -0700214 if is_windows() or is_cygwin():
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700215 python_bin_path = cygpath(python_bin_path)
216
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700217 # Get PYTHON_LIB_PATH
218 python_lib_path = environ_cp.get('PYTHON_LIB_PATH')
219 if not python_lib_path:
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700220 python_lib_paths = get_python_path(environ_cp, python_bin_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700221 if environ_cp.get('USE_DEFAULT_PYTHON_LIB_PATH') == '1':
Vijay Vasudevana1fba7f2017-07-28 10:58:56 -0700222 python_lib_path = python_lib_paths[0]
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700223 else:
Jonathan Hseu008910f2017-08-25 14:01:05 -0700224 print('Found possible Python library paths:\n %s' %
225 '\n '.join(python_lib_paths))
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700226 default_python_lib_path = python_lib_paths[0]
227 python_lib_path = get_input(
Jonathan Hseu008910f2017-08-25 14:01:05 -0700228 'Please input the desired Python library path to use. '
229 'Default is [%s]\n' % python_lib_paths[0])
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700230 if not python_lib_path:
231 python_lib_path = default_python_lib_path
Vijay Vasudevana1fba7f2017-07-28 10:58:56 -0700232 environ_cp['PYTHON_LIB_PATH'] = python_lib_path
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700233
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700234 python_major_version = get_python_major_version(python_bin_path)
235
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700236 # Convert python path to Windows style before writing into bazel.rc
Martin Wicked57572e2017-09-02 19:21:45 -0700237 if is_windows() or is_cygwin():
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700238 python_lib_path = cygpath(python_lib_path)
239
240 # Set-up env variables used by python_configure.bzl
241 write_action_env_to_bazelrc('PYTHON_BIN_PATH', python_bin_path)
242 write_action_env_to_bazelrc('PYTHON_LIB_PATH', python_lib_path)
Gunhan Gulsoyed89a2b2017-09-19 18:36:26 -0700243 write_to_bazelrc('build --python_path=\"%s"' % python_bin_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700244 environ_cp['PYTHON_BIN_PATH'] = python_bin_path
245
246 # Write tools/python_bin_path.sh
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700247 with open(
248 os.path.join(_TF_WORKSPACE_ROOT, 'tools', 'python_bin_path.sh'),
249 'w') as f:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700250 f.write('export PYTHON_BIN_PATH="%s"' % python_bin_path)
251
252
Shanqing Cai71445712018-03-12 19:33:52 -0700253def reset_tf_configure_bazelrc(workspace_path):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700254 """Reset file that contains customized config settings."""
255 open(_TF_BAZELRC, 'w').close()
Shanqing Cai71445712018-03-12 19:33:52 -0700256 bazelrc_path = os.path.join(workspace_path, '.bazelrc')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700257
Shanqing Cai71445712018-03-12 19:33:52 -0700258 data = []
259 if os.path.exists(bazelrc_path):
260 with open(bazelrc_path, 'r') as f:
261 data = f.read().splitlines()
262 with open(bazelrc_path, 'w') as f:
263 for l in data:
264 if _TF_BAZELRC_FILENAME in l:
265 continue
266 f.write('%s\n' % l)
267 if is_windows():
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700268 tf_bazelrc_path = _TF_BAZELRC.replace('\\', '/')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700269 else:
Shanqing Cai71445712018-03-12 19:33:52 -0700270 tf_bazelrc_path = _TF_BAZELRC
271 f.write('import %s\n' % tf_bazelrc_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700272
273
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700274def cleanup_makefile():
275 """Delete any leftover BUILD files from the Makefile build.
276
277 These files could interfere with Bazel parsing.
278 """
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700279 makefile_download_dir = os.path.join(_TF_WORKSPACE_ROOT, 'tensorflow',
280 'contrib', 'makefile', 'downloads')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700281 if os.path.isdir(makefile_download_dir):
282 for root, _, filenames in os.walk(makefile_download_dir):
283 for f in filenames:
284 if f.endswith('BUILD'):
285 os.remove(os.path.join(root, f))
286
287
288def get_var(environ_cp,
289 var_name,
290 query_item,
291 enabled_by_default,
292 question=None,
293 yes_reply=None,
294 no_reply=None):
295 """Get boolean input from user.
296
297 If var_name is not set in env, ask user to enable query_item or not. If the
298 response is empty, use the default.
299
300 Args:
301 environ_cp: copy of the os.environ.
302 var_name: string for name of environment variable, e.g. "TF_NEED_HDFS".
303 query_item: string for feature related to the variable, e.g. "Hadoop File
304 System".
305 enabled_by_default: boolean for default behavior.
306 question: optional string for how to ask for user input.
Michael Cased90054e2018-02-07 14:36:00 -0800307 yes_reply: optional string for reply when feature is enabled.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700308 no_reply: optional string for reply when feature is disabled.
309
310 Returns:
311 boolean value of the variable.
Frank Chenc4ef9272018-01-10 11:36:52 -0800312
313 Raises:
314 UserInputError: if an environment variable is set, but it cannot be
315 interpreted as a boolean indicator, assume that the user has made a
316 scripting error, and will continue to provide invalid input.
317 Raise the error to avoid infinitely looping.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700318 """
319 if not question:
320 question = 'Do you wish to build TensorFlow with %s support?' % query_item
321 if not yes_reply:
322 yes_reply = '%s support will be enabled for TensorFlow.' % query_item
323 if not no_reply:
324 no_reply = 'No %s' % yes_reply
325
326 yes_reply += '\n'
327 no_reply += '\n'
328
329 if enabled_by_default:
330 question += ' [Y/n]: '
331 else:
332 question += ' [y/N]: '
333
334 var = environ_cp.get(var_name)
Frank Chenc4ef9272018-01-10 11:36:52 -0800335 if var is not None:
336 var_content = var.strip().lower()
337 true_strings = ('1', 't', 'true', 'y', 'yes')
338 false_strings = ('0', 'f', 'false', 'n', 'no')
339 if var_content in true_strings:
340 var = True
341 elif var_content in false_strings:
342 var = False
343 else:
344 raise UserInputError(
345 'Environment variable %s must be set as a boolean indicator.\n'
346 'The following are accepted as TRUE : %s.\n'
347 'The following are accepted as FALSE: %s.\n'
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700348 'Current value is %s.' % (var_name, ', '.join(true_strings),
349 ', '.join(false_strings), var))
Frank Chenc4ef9272018-01-10 11:36:52 -0800350
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700351 while var is None:
352 user_input_origin = get_input(question)
353 user_input = user_input_origin.strip().lower()
354 if user_input == 'y':
355 print(yes_reply)
356 var = True
357 elif user_input == 'n':
358 print(no_reply)
359 var = False
360 elif not user_input:
361 if enabled_by_default:
362 print(yes_reply)
363 var = True
364 else:
365 print(no_reply)
366 var = False
367 else:
368 print('Invalid selection: %s' % user_input_origin)
369 return var
370
371
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700372def set_build_var(environ_cp,
373 var_name,
374 query_item,
375 option_name,
376 enabled_by_default,
377 bazel_config_name=None):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700378 """Set if query_item will be enabled for the build.
379
380 Ask user if query_item will be enabled. Default is used if no input is given.
381 Set subprocess environment variable and write to .bazelrc if enabled.
382
383 Args:
384 environ_cp: copy of the os.environ.
385 var_name: string for name of environment variable, e.g. "TF_NEED_HDFS".
386 query_item: string for feature related to the variable, e.g. "Hadoop File
387 System".
388 option_name: string for option to define in .bazelrc.
389 enabled_by_default: boolean for default behavior.
Michael Case98850a52017-09-14 13:35:57 -0700390 bazel_config_name: Name for Bazel --config argument to enable build feature.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700391 """
392
393 var = str(int(get_var(environ_cp, var_name, query_item, enabled_by_default)))
394 environ_cp[var_name] = var
395 if var == '1':
396 write_to_bazelrc('build --define %s=true' % option_name)
Michael Case98850a52017-09-14 13:35:57 -0700397 elif bazel_config_name is not None:
398 # TODO(mikecase): Migrate all users of configure.py to use --config Bazel
399 # options and not to set build configs through environment variables.
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700400 write_to_bazelrc(
401 'build:%s --define %s=true' % (bazel_config_name, option_name))
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700402
403
404def set_action_env_var(environ_cp,
405 var_name,
406 query_item,
407 enabled_by_default,
408 question=None,
409 yes_reply=None,
410 no_reply=None):
411 """Set boolean action_env variable.
412
413 Ask user if query_item will be enabled. Default is used if no input is given.
414 Set environment variable and write to .bazelrc.
415
416 Args:
417 environ_cp: copy of the os.environ.
418 var_name: string for name of environment variable, e.g. "TF_NEED_HDFS".
419 query_item: string for feature related to the variable, e.g. "Hadoop File
420 System".
421 enabled_by_default: boolean for default behavior.
422 question: optional string for how to ask for user input.
Michael Cased90054e2018-02-07 14:36:00 -0800423 yes_reply: optional string for reply when feature is enabled.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700424 no_reply: optional string for reply when feature is disabled.
425 """
426 var = int(
427 get_var(environ_cp, var_name, query_item, enabled_by_default, question,
428 yes_reply, no_reply))
429
430 write_action_env_to_bazelrc(var_name, var)
431 environ_cp[var_name] = str(var)
432
433
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700434def convert_version_to_int(version):
435 """Convert a version number to a integer that can be used to compare.
436
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700437 Version strings of the form X.YZ and X.Y.Z-xxxxx are supported. The
438 'xxxxx' part, for instance 'homebrew' on OS/X, is ignored.
439
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700440 Args:
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700441 version: a version to be converted
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700442
443 Returns:
444 An integer if converted successfully, otherwise return None.
445 """
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700446 version = version.split('-')[0]
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700447 version_segments = version.split('.')
448 for seg in version_segments:
449 if not seg.isdigit():
450 return None
451
452 version_str = ''.join(['%03d' % int(seg) for seg in version_segments])
453 return int(version_str)
454
455
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700456def check_bazel_version(min_version):
Yifei Fengdce9a492018-02-22 14:24:57 -0800457 """Check installed bazel version is at least min_version.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700458
459 Args:
460 min_version: string for minimum bazel version.
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700461
462 Returns:
463 The bazel version detected.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700464 """
Jonathan Hseu008910f2017-08-25 14:01:05 -0700465 if which('bazel') is None:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700466 print('Cannot find bazel. Please install bazel.')
467 sys.exit(0)
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700468 curr_version = run_shell(
469 ['bazel', '--batch', '--bazelrc=/dev/null', 'version'])
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700470
471 for line in curr_version.split('\n'):
472 if 'Build label: ' in line:
473 curr_version = line.split('Build label: ')[1]
474 break
475
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700476 min_version_int = convert_version_to_int(min_version)
477 curr_version_int = convert_version_to_int(curr_version)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700478
479 # Check if current bazel version can be detected properly.
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700480 if not curr_version_int:
481 print('WARNING: current bazel installation is not a release version.')
482 print('Make sure you are running at least bazel %s' % min_version)
483 return curr_version
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700484
Michael Cased94271a2017-08-22 17:26:52 -0700485 print('You have bazel %s installed.' % curr_version)
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700486
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700487 if curr_version_int < min_version_int:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700488 print('Please upgrade your bazel installation to version %s or higher to '
489 'build TensorFlow!' % min_version)
490 sys.exit(0)
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700491 return curr_version
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700492
493
494def set_cc_opt_flags(environ_cp):
495 """Set up architecture-dependent optimization flags.
496
497 Also append CC optimization flags to bazel.rc..
498
499 Args:
500 environ_cp: copy of the os.environ.
501 """
502 if is_ppc64le():
503 # gcc on ppc64le does not support -march, use mcpu instead
504 default_cc_opt_flags = '-mcpu=native'
A. Unique TensorFlower1bba94a2018-04-04 15:45:20 -0700505 elif is_windows():
506 default_cc_opt_flags = '/arch:AVX'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700507 else:
508 default_cc_opt_flags = '-march=native'
509 question = ('Please specify optimization flags to use during compilation when'
510 ' bazel option "--config=opt" is specified [Default is %s]: '
511 ) % default_cc_opt_flags
512 cc_opt_flags = get_from_env_or_user_or_default(environ_cp, 'CC_OPT_FLAGS',
513 question, default_cc_opt_flags)
514 for opt in cc_opt_flags.split():
Michael Case00177422017-11-10 13:14:03 -0800515 write_to_bazelrc('build:opt --copt=%s' % opt)
516 # It should be safe on the same build host.
A. Unique TensorFlower1bba94a2018-04-04 15:45:20 -0700517 if not is_ppc64le() and not is_windows():
Shanqing Cai71445712018-03-12 19:33:52 -0700518 write_to_bazelrc('build:opt --host_copt=-march=native')
Michael Casebb3355d2017-11-09 08:46:31 -0800519 write_to_bazelrc('build:opt --define with_default_optimizations=true')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700520
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700521
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700522def set_tf_cuda_clang(environ_cp):
523 """set TF_CUDA_CLANG action_env.
524
525 Args:
526 environ_cp: copy of the os.environ.
527 """
528 question = 'Do you want to use clang as CUDA compiler?'
529 yes_reply = 'Clang will be used as CUDA compiler.'
530 no_reply = 'nvcc will be used as CUDA compiler.'
531 set_action_env_var(
532 environ_cp,
533 'TF_CUDA_CLANG',
534 None,
535 False,
536 question=question,
537 yes_reply=yes_reply,
538 no_reply=no_reply)
539
540
A. Unique TensorFlowerfd29e952017-12-22 03:07:51 -0800541def set_tf_download_clang(environ_cp):
542 """Set TF_DOWNLOAD_CLANG action_env."""
Ilya Biryukov9e651e42018-03-22 05:33:42 -0700543 question = 'Do you wish to download a fresh release of clang? (Experimental)'
A. Unique TensorFlowerfd29e952017-12-22 03:07:51 -0800544 yes_reply = 'Clang will be downloaded and used to compile tensorflow.'
545 no_reply = 'Clang will not be downloaded.'
546 set_action_env_var(
547 environ_cp,
548 'TF_DOWNLOAD_CLANG',
549 None,
550 False,
551 question=question,
552 yes_reply=yes_reply,
553 no_reply=no_reply)
554
555
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700556def get_from_env_or_user_or_default(environ_cp, var_name, ask_for_var,
557 var_default):
558 """Get var_name either from env, or user or default.
559
560 If var_name has been set as environment variable, use the preset value, else
561 ask for user input. If no input is provided, the default is used.
562
563 Args:
564 environ_cp: copy of the os.environ.
565 var_name: string for name of environment variable, e.g. "TF_NEED_HDFS".
566 ask_for_var: string for how to ask for user input.
567 var_default: default value string.
568
569 Returns:
570 string value for var_name
571 """
572 var = environ_cp.get(var_name)
573 if not var:
574 var = get_input(ask_for_var)
Michael Cased94271a2017-08-22 17:26:52 -0700575 print('\n')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700576 if not var:
577 var = var_default
578 return var
579
580
581def set_clang_cuda_compiler_path(environ_cp):
582 """Set CLANG_CUDA_COMPILER_PATH."""
Jonathan Hseu008910f2017-08-25 14:01:05 -0700583 default_clang_path = which('clang') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700584 ask_clang_path = ('Please specify which clang should be used as device and '
585 'host compiler. [Default is %s]: ') % default_clang_path
586
587 while True:
588 clang_cuda_compiler_path = get_from_env_or_user_or_default(
589 environ_cp, 'CLANG_CUDA_COMPILER_PATH', ask_clang_path,
590 default_clang_path)
591 if os.path.exists(clang_cuda_compiler_path):
592 break
593
594 # Reset and retry
595 print('Invalid clang path: %s cannot be found.' % clang_cuda_compiler_path)
596 environ_cp['CLANG_CUDA_COMPILER_PATH'] = ''
597
598 # Set CLANG_CUDA_COMPILER_PATH
599 environ_cp['CLANG_CUDA_COMPILER_PATH'] = clang_cuda_compiler_path
600 write_action_env_to_bazelrc('CLANG_CUDA_COMPILER_PATH',
601 clang_cuda_compiler_path)
602
603
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700604def prompt_loop_or_load_from_env(environ_cp,
605 var_name,
606 var_default,
607 ask_for_var,
608 check_success,
609 error_msg,
610 suppress_default_error=False,
611 n_ask_attempts=_DEFAULT_PROMPT_ASK_ATTEMPTS):
Austin Anderson6afface2017-12-05 11:59:17 -0800612 """Loop over user prompts for an ENV param until receiving a valid response.
613
614 For the env param var_name, read from the environment or verify user input
615 until receiving valid input. When done, set var_name in the environ_cp to its
616 new value.
617
618 Args:
619 environ_cp: (Dict) copy of the os.environ.
620 var_name: (String) string for name of environment variable, e.g. "TF_MYVAR".
621 var_default: (String) default value string.
622 ask_for_var: (String) string for how to ask for user input.
623 check_success: (Function) function that takes one argument and returns a
624 boolean. Should return True if the value provided is considered valid. May
625 contain a complex error message if error_msg does not provide enough
626 information. In that case, set suppress_default_error to True.
627 error_msg: (String) String with one and only one '%s'. Formatted with each
628 invalid response upon check_success(input) failure.
629 suppress_default_error: (Bool) Suppress the above error message in favor of
630 one from the check_success function.
631 n_ask_attempts: (Integer) Number of times to query for valid input before
632 raising an error and quitting.
633
634 Returns:
635 [String] The value of var_name after querying for input.
636
637 Raises:
638 UserInputError: if a query has been attempted n_ask_attempts times without
Frank Chenc4ef9272018-01-10 11:36:52 -0800639 success, assume that the user has made a scripting error, and will
640 continue to provide invalid input. Raise the error to avoid infinitely
641 looping.
Austin Anderson6afface2017-12-05 11:59:17 -0800642 """
643 default = environ_cp.get(var_name) or var_default
644 full_query = '%s [Default is %s]: ' % (
645 ask_for_var,
646 default,
647 )
648
649 for _ in range(n_ask_attempts):
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700650 val = get_from_env_or_user_or_default(environ_cp, var_name, full_query,
Austin Anderson6afface2017-12-05 11:59:17 -0800651 default)
652 if check_success(val):
653 break
654 if not suppress_default_error:
655 print(error_msg % val)
656 environ_cp[var_name] = ''
657 else:
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700658 raise UserInputError(
659 'Invalid %s setting was provided %d times in a row. '
660 'Assuming to be a scripting mistake.' % (var_name, n_ask_attempts))
Austin Anderson6afface2017-12-05 11:59:17 -0800661
662 environ_cp[var_name] = val
663 return val
664
665
666def create_android_ndk_rule(environ_cp):
667 """Set ANDROID_NDK_HOME and write Android NDK WORKSPACE rule."""
668 if is_windows() or is_cygwin():
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700669 default_ndk_path = cygpath(
670 '%s/Android/Sdk/ndk-bundle' % environ_cp['APPDATA'])
Austin Anderson6afface2017-12-05 11:59:17 -0800671 elif is_macos():
672 default_ndk_path = '%s/library/Android/Sdk/ndk-bundle' % environ_cp['HOME']
673 else:
674 default_ndk_path = '%s/Android/Sdk/ndk-bundle' % environ_cp['HOME']
675
676 def valid_ndk_path(path):
677 return (os.path.exists(path) and
678 os.path.exists(os.path.join(path, 'source.properties')))
679
680 android_ndk_home_path = prompt_loop_or_load_from_env(
681 environ_cp,
682 var_name='ANDROID_NDK_HOME',
683 var_default=default_ndk_path,
684 ask_for_var='Please specify the home path of the Android NDK to use.',
685 check_success=valid_ndk_path,
686 error_msg=('The path %s or its child file "source.properties" '
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700687 'does not exist.'))
Michael Case51053502018-06-05 17:47:19 -0700688 write_action_env_to_bazelrc('ANDROID_NDK_HOME', android_ndk_home_path)
689 write_action_env_to_bazelrc('ANDROID_NDK_API_LEVEL',
690 check_ndk_level(android_ndk_home_path))
Austin Anderson6afface2017-12-05 11:59:17 -0800691
692
693def create_android_sdk_rule(environ_cp):
694 """Set Android variables and write Android SDK WORKSPACE rule."""
695 if is_windows() or is_cygwin():
696 default_sdk_path = cygpath('%s/Android/Sdk' % environ_cp['APPDATA'])
697 elif is_macos():
Shashi Shekharc0ff0cc2018-07-17 09:00:24 -0700698 default_sdk_path = '%s/library/Android/Sdk' % environ_cp['HOME']
Austin Anderson6afface2017-12-05 11:59:17 -0800699 else:
700 default_sdk_path = '%s/Android/Sdk' % environ_cp['HOME']
701
702 def valid_sdk_path(path):
703 return (os.path.exists(path) and
704 os.path.exists(os.path.join(path, 'platforms')) and
705 os.path.exists(os.path.join(path, 'build-tools')))
706
707 android_sdk_home_path = prompt_loop_or_load_from_env(
708 environ_cp,
709 var_name='ANDROID_SDK_HOME',
710 var_default=default_sdk_path,
711 ask_for_var='Please specify the home path of the Android SDK to use.',
712 check_success=valid_sdk_path,
713 error_msg=('Either %s does not exist, or it does not contain the '
714 'subdirectories "platforms" and "build-tools".'))
715
716 platforms = os.path.join(android_sdk_home_path, 'platforms')
717 api_levels = sorted(os.listdir(platforms))
718 api_levels = [x.replace('android-', '') for x in api_levels]
719
720 def valid_api_level(api_level):
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700721 return os.path.exists(
722 os.path.join(android_sdk_home_path, 'platforms',
723 'android-' + api_level))
Austin Anderson6afface2017-12-05 11:59:17 -0800724
725 android_api_level = prompt_loop_or_load_from_env(
726 environ_cp,
727 var_name='ANDROID_API_LEVEL',
728 var_default=api_levels[-1],
729 ask_for_var=('Please specify the Android SDK API level to use. '
730 '[Available levels: %s]') % api_levels,
731 check_success=valid_api_level,
732 error_msg='Android-%s is not present in the SDK path.')
733
734 build_tools = os.path.join(android_sdk_home_path, 'build-tools')
735 versions = sorted(os.listdir(build_tools))
736
737 def valid_build_tools(version):
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700738 return os.path.exists(
739 os.path.join(android_sdk_home_path, 'build-tools', version))
Austin Anderson6afface2017-12-05 11:59:17 -0800740
741 android_build_tools_version = prompt_loop_or_load_from_env(
742 environ_cp,
743 var_name='ANDROID_BUILD_TOOLS_VERSION',
744 var_default=versions[-1],
745 ask_for_var=('Please specify an Android build tools version to use. '
746 '[Available versions: %s]') % versions,
747 check_success=valid_build_tools,
748 error_msg=('The selected SDK does not have build-tools version %s '
749 'available.'))
750
Michael Case51053502018-06-05 17:47:19 -0700751 write_action_env_to_bazelrc('ANDROID_BUILD_TOOLS_VERSION',
752 android_build_tools_version)
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700753 write_action_env_to_bazelrc('ANDROID_SDK_API_LEVEL', android_api_level)
754 write_action_env_to_bazelrc('ANDROID_SDK_HOME', android_sdk_home_path)
Austin Anderson6afface2017-12-05 11:59:17 -0800755
756
757def check_ndk_level(android_ndk_home_path):
758 """Check the revision number of an Android NDK path."""
759 properties_path = '%s/source.properties' % android_ndk_home_path
760 if is_windows() or is_cygwin():
761 properties_path = cygpath(properties_path)
762 with open(properties_path, 'r') as f:
763 filedata = f.read()
764
765 revision = re.search(r'Pkg.Revision = (\d+)', filedata)
766 if revision:
Michael Case51053502018-06-05 17:47:19 -0700767 ndk_api_level = revision.group(1)
768 else:
769 raise Exception('Unable to parse NDK revision.')
770 if int(ndk_api_level) not in _SUPPORTED_ANDROID_NDK_VERSIONS:
771 print('WARNING: The API level of the NDK in %s is %s, which is not '
772 'supported by Bazel (officially supported versions: %s). Please use '
773 'another version. Compiling Android targets may result in confusing '
774 'errors.\n' % (android_ndk_home_path, ndk_api_level,
775 _SUPPORTED_ANDROID_NDK_VERSIONS))
776 return ndk_api_level
Austin Anderson6afface2017-12-05 11:59:17 -0800777
778
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700779def set_gcc_host_compiler_path(environ_cp):
780 """Set GCC_HOST_COMPILER_PATH."""
Jonathan Hseu008910f2017-08-25 14:01:05 -0700781 default_gcc_host_compiler_path = which('gcc') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700782 cuda_bin_symlink = '%s/bin/gcc' % environ_cp.get('CUDA_TOOLKIT_PATH')
783
784 if os.path.islink(cuda_bin_symlink):
785 # os.readlink is only available in linux
Jonathan Hseu008910f2017-08-25 14:01:05 -0700786 default_gcc_host_compiler_path = os.path.realpath(cuda_bin_symlink)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700787
Austin Anderson6afface2017-12-05 11:59:17 -0800788 gcc_host_compiler_path = prompt_loop_or_load_from_env(
789 environ_cp,
790 var_name='GCC_HOST_COMPILER_PATH',
791 var_default=default_gcc_host_compiler_path,
792 ask_for_var=
793 'Please specify which gcc should be used by nvcc as the host compiler.',
794 check_success=os.path.exists,
795 error_msg='Invalid gcc path. %s cannot be found.',
796 )
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700797
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700798 write_action_env_to_bazelrc('GCC_HOST_COMPILER_PATH', gcc_host_compiler_path)
799
800
Ankur Taly0e6f39d2018-02-16 18:22:55 -0800801def reformat_version_sequence(version_str, sequence_count):
802 """Reformat the version string to have the given number of sequences.
803
804 For example:
805 Given (7, 2) -> 7.0
806 (7.0.1, 2) -> 7.0
807 (5, 1) -> 5
808 (5.0.3.2, 1) -> 5
809
810 Args:
811 version_str: String, the version string.
812 sequence_count: int, an integer.
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700813
Ankur Taly0e6f39d2018-02-16 18:22:55 -0800814 Returns:
815 string, reformatted version string.
816 """
817 v = version_str.split('.')
818 if len(v) < sequence_count:
819 v = v + (['0'] * (sequence_count - len(v)))
820
821 return '.'.join(v[:sequence_count])
822
823
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700824def set_tf_cuda_version(environ_cp):
825 """Set CUDA_TOOLKIT_PATH and TF_CUDA_VERSION."""
826 ask_cuda_version = (
A. Unique TensorFlowerb15500b2018-05-08 12:04:38 -0700827 'Please specify the CUDA SDK version you want to use. '
828 '[Leave empty to default to CUDA %s]: ') % _DEFAULT_CUDA_VERSION
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700829
Austin Andersonf9a88f82017-12-13 11:49:40 -0800830 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700831 # Configure the Cuda SDK version to use.
832 tf_cuda_version = get_from_env_or_user_or_default(
833 environ_cp, 'TF_CUDA_VERSION', ask_cuda_version, _DEFAULT_CUDA_VERSION)
Ankur Taly0e6f39d2018-02-16 18:22:55 -0800834 tf_cuda_version = reformat_version_sequence(str(tf_cuda_version), 2)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700835
836 # Find out where the CUDA toolkit is installed
837 default_cuda_path = _DEFAULT_CUDA_PATH
Martin Wicked57572e2017-09-02 19:21:45 -0700838 if is_windows() or is_cygwin():
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700839 default_cuda_path = cygpath(
840 environ_cp.get('CUDA_PATH', _DEFAULT_CUDA_PATH_WIN))
841 elif is_linux():
842 # If the default doesn't exist, try an alternative default.
843 if (not os.path.exists(default_cuda_path)
844 ) and os.path.exists(_DEFAULT_CUDA_PATH_LINUX):
845 default_cuda_path = _DEFAULT_CUDA_PATH_LINUX
846 ask_cuda_path = ('Please specify the location where CUDA %s toolkit is'
847 ' installed. Refer to README.md for more details. '
848 '[Default is %s]: ') % (tf_cuda_version, default_cuda_path)
849 cuda_toolkit_path = get_from_env_or_user_or_default(
850 environ_cp, 'CUDA_TOOLKIT_PATH', ask_cuda_path, default_cuda_path)
A. Unique TensorFlower02f17fe2018-07-07 06:59:19 -0700851 if is_windows() or is_cygwin():
852 cuda_toolkit_path = cygpath(cuda_toolkit_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700853
854 if is_windows():
Niall Moranb7d97e82018-08-09 00:29:49 +0100855 cuda_rt_lib_paths = ['lib/x64/cudart.lib']
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700856 elif is_linux():
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700857 cuda_rt_lib_paths = [
858 '%s/libcudart.so.%s' % (x, tf_cuda_version) for x in [
859 'lib64',
860 'lib/powerpc64le-linux-gnu',
861 'lib/x86_64-linux-gnu',
862 ]
863 ]
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700864 elif is_macos():
Niall Moranb7d97e82018-08-09 00:29:49 +0100865 cuda_rt_lib_paths = ['lib/libcudart.%s.dylib' % tf_cuda_version]
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700866
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700867 cuda_toolkit_paths_full = [
868 os.path.join(cuda_toolkit_path, x) for x in cuda_rt_lib_paths
869 ]
Niall Moranb7d97e82018-08-09 00:29:49 +0100870 if any([os.path.exists(x) for x in cuda_toolkit_paths_full]):
Yifei Feng5198cb82018-08-17 13:53:06 -0700871 break
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700872
873 # Reset and retry
874 print('Invalid path to CUDA %s toolkit. %s cannot be found' %
hellcom9a13fc32018-09-12 10:58:24 +0300875 (tf_cuda_version, cuda_toolkit_paths_full))
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700876 environ_cp['TF_CUDA_VERSION'] = ''
877 environ_cp['CUDA_TOOLKIT_PATH'] = ''
878
Austin Andersonf9a88f82017-12-13 11:49:40 -0800879 else:
880 raise UserInputError('Invalid TF_CUDA_SETTING setting was provided %d '
881 'times in a row. Assuming to be a scripting mistake.' %
882 _DEFAULT_PROMPT_ASK_ATTEMPTS)
883
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700884 # Set CUDA_TOOLKIT_PATH and TF_CUDA_VERSION
885 environ_cp['CUDA_TOOLKIT_PATH'] = cuda_toolkit_path
886 write_action_env_to_bazelrc('CUDA_TOOLKIT_PATH', cuda_toolkit_path)
887 environ_cp['TF_CUDA_VERSION'] = tf_cuda_version
888 write_action_env_to_bazelrc('TF_CUDA_VERSION', tf_cuda_version)
889
890
Yifei Fengb1d8c592017-11-22 13:42:21 -0800891def set_tf_cudnn_version(environ_cp):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700892 """Set CUDNN_INSTALL_PATH and TF_CUDNN_VERSION."""
893 ask_cudnn_version = (
Jonathan Hseu008910f2017-08-25 14:01:05 -0700894 'Please specify the cuDNN version you want to use. '
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700895 '[Leave empty to default to cuDNN %s.0]: ') % _DEFAULT_CUDNN_VERSION
896
Austin Andersonf9a88f82017-12-13 11:49:40 -0800897 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700898 tf_cudnn_version = get_from_env_or_user_or_default(
899 environ_cp, 'TF_CUDNN_VERSION', ask_cudnn_version,
900 _DEFAULT_CUDNN_VERSION)
Ankur Taly0e6f39d2018-02-16 18:22:55 -0800901 tf_cudnn_version = reformat_version_sequence(str(tf_cudnn_version), 1)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700902
903 default_cudnn_path = environ_cp.get('CUDA_TOOLKIT_PATH')
904 ask_cudnn_path = (r'Please specify the location where cuDNN %s library is '
905 'installed. Refer to README.md for more details. [Default'
A. Unique TensorFlower1b212352018-07-19 13:48:50 -0700906 ' is %s]: ') % (tf_cudnn_version, default_cudnn_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700907 cudnn_install_path = get_from_env_or_user_or_default(
908 environ_cp, 'CUDNN_INSTALL_PATH', ask_cudnn_path, default_cudnn_path)
909
910 # Result returned from "read" will be used unexpanded. That make "~"
911 # unusable. Going through one more level of expansion to handle that.
912 cudnn_install_path = os.path.realpath(
913 os.path.expanduser(cudnn_install_path))
Martin Wicked57572e2017-09-02 19:21:45 -0700914 if is_windows() or is_cygwin():
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700915 cudnn_install_path = cygpath(cudnn_install_path)
916
917 if is_windows():
918 cuda_dnn_lib_path = 'lib/x64/cudnn.lib'
919 cuda_dnn_lib_alt_path = 'lib/x64/cudnn.lib'
920 elif is_linux():
921 cuda_dnn_lib_path = 'lib64/libcudnn.so.%s' % tf_cudnn_version
922 cuda_dnn_lib_alt_path = 'libcudnn.so.%s' % tf_cudnn_version
923 elif is_macos():
924 cuda_dnn_lib_path = 'lib/libcudnn.%s.dylib' % tf_cudnn_version
925 cuda_dnn_lib_alt_path = 'libcudnn.%s.dylib' % tf_cudnn_version
926
927 cuda_dnn_lib_path_full = os.path.join(cudnn_install_path, cuda_dnn_lib_path)
928 cuda_dnn_lib_alt_path_full = os.path.join(cudnn_install_path,
929 cuda_dnn_lib_alt_path)
930 if os.path.exists(cuda_dnn_lib_path_full) or os.path.exists(
931 cuda_dnn_lib_alt_path_full):
932 break
933
934 # Try another alternative for Linux
935 if is_linux():
Jonathan Hseu008910f2017-08-25 14:01:05 -0700936 ldconfig_bin = which('ldconfig') or '/sbin/ldconfig'
937 cudnn_path_from_ldconfig = run_shell([ldconfig_bin, '-p'])
938 cudnn_path_from_ldconfig = re.search('.*libcudnn.so .* => (.*)',
A. Unique TensorFlowere7223582017-09-06 17:57:04 -0700939 cudnn_path_from_ldconfig)
940 if cudnn_path_from_ldconfig:
941 cudnn_path_from_ldconfig = cudnn_path_from_ldconfig.group(1)
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700942 if os.path.exists(
943 '%s.%s' % (cudnn_path_from_ldconfig, tf_cudnn_version)):
A. Unique TensorFlowere7223582017-09-06 17:57:04 -0700944 cudnn_install_path = os.path.dirname(cudnn_path_from_ldconfig)
945 break
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700946
947 # Reset and Retry
948 print(
949 'Invalid path to cuDNN %s toolkit. None of the following files can be '
950 'found:' % tf_cudnn_version)
951 print(cuda_dnn_lib_path_full)
952 print(cuda_dnn_lib_alt_path_full)
953 if is_linux():
954 print('%s.%s' % (cudnn_path_from_ldconfig, tf_cudnn_version))
955
956 environ_cp['TF_CUDNN_VERSION'] = ''
Austin Andersonf9a88f82017-12-13 11:49:40 -0800957 else:
958 raise UserInputError('Invalid TF_CUDNN setting was provided %d '
959 'times in a row. Assuming to be a scripting mistake.' %
960 _DEFAULT_PROMPT_ASK_ATTEMPTS)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700961
962 # Set CUDNN_INSTALL_PATH and TF_CUDNN_VERSION
963 environ_cp['CUDNN_INSTALL_PATH'] = cudnn_install_path
964 write_action_env_to_bazelrc('CUDNN_INSTALL_PATH', cudnn_install_path)
965 environ_cp['TF_CUDNN_VERSION'] = tf_cudnn_version
966 write_action_env_to_bazelrc('TF_CUDNN_VERSION', tf_cudnn_version)
967
968
Mingxing Tan1e7b0e42018-06-28 19:13:20 -0700969def is_cuda_compatible(lib, cuda_ver, cudnn_ver):
970 """Check compatibility between given library and cudnn/cudart libraries."""
971 ldd_bin = which('ldd') or '/usr/bin/ldd'
972 ldd_out = run_shell([ldd_bin, lib], True)
973 ldd_out = ldd_out.split(os.linesep)
974 cudnn_pattern = re.compile('.*libcudnn.so\\.?(.*) =>.*$')
975 cuda_pattern = re.compile('.*libcudart.so\\.?(.*) =>.*$')
976 cudnn = None
977 cudart = None
978 cudnn_ok = True # assume no cudnn dependency by default
979 cuda_ok = True # assume no cuda dependency by default
980 for line in ldd_out:
981 if 'libcudnn.so' in line:
982 cudnn = cudnn_pattern.search(line)
983 cudnn_ok = False
984 elif 'libcudart.so' in line:
985 cudart = cuda_pattern.search(line)
986 cuda_ok = False
987 if cudnn and len(cudnn.group(1)):
988 cudnn = convert_version_to_int(cudnn.group(1))
989 if cudart and len(cudart.group(1)):
990 cudart = convert_version_to_int(cudart.group(1))
991 if cudnn is not None:
992 cudnn_ok = (cudnn == cudnn_ver)
993 if cudart is not None:
994 cuda_ok = (cudart == cuda_ver)
995 return cudnn_ok and cuda_ok
996
997
Guangda Lai76f69382018-01-25 23:59:19 -0800998def set_tf_tensorrt_install_path(environ_cp):
999 """Set TENSORRT_INSTALL_PATH and TF_TENSORRT_VERSION.
1000
1001 Adapted from code contributed by Sami Kama (https://github.com/samikama).
1002
1003 Args:
1004 environ_cp: copy of the os.environ.
1005
1006 Raises:
1007 ValueError: if this method was called under non-Linux platform.
1008 UserInputError: if user has provided invalid input multiple times.
1009 """
1010 if not is_linux():
1011 raise ValueError('Currently TensorRT is only supported on Linux platform.')
1012
1013 # Ask user whether to add TensorRT support.
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001014 if str(int(get_var(environ_cp, 'TF_NEED_TENSORRT', 'TensorRT',
1015 False))) != '1':
Guangda Lai76f69382018-01-25 23:59:19 -08001016 return
1017
1018 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS):
1019 ask_tensorrt_path = (r'Please specify the location where TensorRT is '
1020 'installed. [Default is %s]:') % (
1021 _DEFAULT_TENSORRT_PATH_LINUX)
1022 trt_install_path = get_from_env_or_user_or_default(
1023 environ_cp, 'TENSORRT_INSTALL_PATH', ask_tensorrt_path,
1024 _DEFAULT_TENSORRT_PATH_LINUX)
1025
1026 # Result returned from "read" will be used unexpanded. That make "~"
1027 # unusable. Going through one more level of expansion to handle that.
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001028 trt_install_path = os.path.realpath(os.path.expanduser(trt_install_path))
Guangda Lai76f69382018-01-25 23:59:19 -08001029
1030 def find_libs(search_path):
1031 """Search for libnvinfer.so in "search_path"."""
1032 fl = set()
1033 if os.path.exists(search_path) and os.path.isdir(search_path):
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001034 fl.update([
1035 os.path.realpath(os.path.join(search_path, x))
1036 for x in os.listdir(search_path)
1037 if 'libnvinfer.so' in x
1038 ])
Guangda Lai76f69382018-01-25 23:59:19 -08001039 return fl
1040
1041 possible_files = find_libs(trt_install_path)
1042 possible_files.update(find_libs(os.path.join(trt_install_path, 'lib')))
1043 possible_files.update(find_libs(os.path.join(trt_install_path, 'lib64')))
Guangda Lai76f69382018-01-25 23:59:19 -08001044 cuda_ver = convert_version_to_int(environ_cp['TF_CUDA_VERSION'])
1045 cudnn_ver = convert_version_to_int(environ_cp['TF_CUDNN_VERSION'])
1046 nvinfer_pattern = re.compile('.*libnvinfer.so.?(.*)$')
1047 highest_ver = [0, None, None]
1048
1049 for lib_file in possible_files:
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001050 if is_cuda_compatible(lib_file, cuda_ver, cudnn_ver):
Jacques Pienaar2d0531d2018-03-21 12:07:51 -07001051 matches = nvinfer_pattern.search(lib_file)
1052 if len(matches.groups()) == 0:
1053 continue
1054 ver_str = matches.group(1)
Guangda Lai76f69382018-01-25 23:59:19 -08001055 ver = convert_version_to_int(ver_str) if len(ver_str) else 0
1056 if ver > highest_ver[0]:
1057 highest_ver = [ver, ver_str, lib_file]
1058 if highest_ver[1] is not None:
1059 trt_install_path = os.path.dirname(highest_ver[2])
1060 tf_tensorrt_version = highest_ver[1]
1061 break
1062
1063 # Try another alternative from ldconfig.
1064 ldconfig_bin = which('ldconfig') or '/sbin/ldconfig'
1065 ldconfig_output = run_shell([ldconfig_bin, '-p'])
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001066 search_result = re.search('.*libnvinfer.so\\.?([0-9.]*).* => (.*)',
1067 ldconfig_output)
Guangda Lai76f69382018-01-25 23:59:19 -08001068 if search_result:
1069 libnvinfer_path_from_ldconfig = search_result.group(2)
1070 if os.path.exists(libnvinfer_path_from_ldconfig):
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001071 if is_cuda_compatible(libnvinfer_path_from_ldconfig, cuda_ver,
1072 cudnn_ver):
Guangda Lai76f69382018-01-25 23:59:19 -08001073 trt_install_path = os.path.dirname(libnvinfer_path_from_ldconfig)
1074 tf_tensorrt_version = search_result.group(1)
1075 break
1076
1077 # Reset and Retry
Yifei Fengdce9a492018-02-22 14:24:57 -08001078 if possible_files:
1079 print('TensorRT libraries found in one the following directories',
1080 'are not compatible with selected cuda and cudnn installations')
1081 print(trt_install_path)
1082 print(os.path.join(trt_install_path, 'lib'))
1083 print(os.path.join(trt_install_path, 'lib64'))
1084 if search_result:
1085 print(libnvinfer_path_from_ldconfig)
1086 else:
1087 print(
1088 'Invalid path to TensorRT. None of the following files can be found:')
1089 print(trt_install_path)
1090 print(os.path.join(trt_install_path, 'lib'))
1091 print(os.path.join(trt_install_path, 'lib64'))
1092 if search_result:
1093 print(libnvinfer_path_from_ldconfig)
Guangda Lai76f69382018-01-25 23:59:19 -08001094
1095 else:
1096 raise UserInputError('Invalid TF_TENSORRT setting was provided %d '
1097 'times in a row. Assuming to be a scripting mistake.' %
1098 _DEFAULT_PROMPT_ASK_ATTEMPTS)
1099
1100 # Set TENSORRT_INSTALL_PATH and TF_TENSORRT_VERSION
1101 environ_cp['TENSORRT_INSTALL_PATH'] = trt_install_path
1102 write_action_env_to_bazelrc('TENSORRT_INSTALL_PATH', trt_install_path)
1103 environ_cp['TF_TENSORRT_VERSION'] = tf_tensorrt_version
1104 write_action_env_to_bazelrc('TF_TENSORRT_VERSION', tf_tensorrt_version)
1105
1106
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001107def set_tf_nccl_install_path(environ_cp):
Jason Furmanek7c234152018-09-26 04:44:12 +00001108 """Set NCCL_INSTALL_PATH, NCCL_HDR_PATH and TF_NCCL_VERSION.
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001109
1110 Args:
1111 environ_cp: copy of the os.environ.
1112
1113 Raises:
1114 ValueError: if this method was called under non-Linux platform.
1115 UserInputError: if user has provided invalid input multiple times.
1116 """
1117 if not is_linux():
1118 raise ValueError('Currently NCCL is only supported on Linux platforms.')
1119
1120 ask_nccl_version = (
Smit Hinsu63e6b9b2018-07-13 12:46:24 -07001121 'Please specify the NCCL version you want to use. If NCCL %s is not '
1122 'installed, then you can use version 1.3 that can be fetched '
1123 'automatically but it may have worse performance with multiple GPUs. '
1124 '[Default is %s]: ') % (_DEFAULT_NCCL_VERSION, _DEFAULT_NCCL_VERSION)
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001125
1126 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS):
1127 tf_nccl_version = get_from_env_or_user_or_default(
1128 environ_cp, 'TF_NCCL_VERSION', ask_nccl_version, _DEFAULT_NCCL_VERSION)
1129 tf_nccl_version = reformat_version_sequence(str(tf_nccl_version), 1)
1130
1131 if tf_nccl_version == '1':
1132 break # No need to get install path, NCCL 1 is a GitHub repo.
1133
Jason Furmanek7c234152018-09-26 04:44:12 +00001134 # Look with ldconfig first if we can find the library in paths
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001135 # like /usr/lib/x86_64-linux-gnu and the header file in the corresponding
1136 # include directory. This is where the NCCL .deb packages install them.
Jason Furmanek7c234152018-09-26 04:44:12 +00001137
1138 # First check to see if NCCL is in the ldconfig.
1139 # If its found, use that location.
1140 if is_linux():
1141 ldconfig_bin = which('ldconfig') or '/sbin/ldconfig'
1142 nccl2_path_from_ldconfig = run_shell([ldconfig_bin, '-p'])
1143 nccl2_path_from_ldconfig = re.search('.*libnccl.so .* => (.*)',
1144 nccl2_path_from_ldconfig)
1145 if nccl2_path_from_ldconfig:
1146 nccl2_path_from_ldconfig = nccl2_path_from_ldconfig.group(1)
1147 if os.path.exists('%s.%s' % (nccl2_path_from_ldconfig, tf_nccl_version)):
1148 nccl_install_path = os.path.dirname(nccl2_path_from_ldconfig)
1149 print('NCCL libraries found in ' + nccl2_path_from_ldconfig)
1150
1151 # Check if this is the main system lib location
1152 if re.search('.*linux-gnu', nccl_install_path):
1153 trunc_nccl_install_path = "/usr"
1154 print("This looks like a system path.")
1155 else:
1156 trunc_nccl_install_path = nccl_install_path + "/.."
1157
1158 # Look for header
1159 nccl_hdr_path = trunc_nccl_install_path + "/include"
1160 print("Assuming NCCL header path is " + nccl_hdr_path)
1161 if os.path.exists(nccl_hdr_path + "/nccl.h"):
1162 # Set NCCL_INSTALL_PATH
1163 environ_cp['NCCL_INSTALL_PATH'] = nccl_install_path
1164 write_action_env_to_bazelrc('NCCL_INSTALL_PATH', nccl_install_path)
1165
1166 # Set NCCL_HDR_PATH
1167 environ_cp['NCCL_HDR_PATH'] = nccl_hdr_path
1168 write_action_env_to_bazelrc('NCCL_HDR_PATH', nccl_hdr_path)
1169 break
1170 else:
1171 print('The header for NCCL2 cannot be found. Please install the libnccl-dev package.')
1172 else:
1173 print('NCCL2 is listed by ldconfig but the library is not found. '
1174 'Your ldconfig is out of date. Please run sudo ldconfig.')
1175 else:
1176 # NCCL is not found in ldconfig. Ask the user for the location.
1177 default_nccl_path = environ_cp.get('CUDA_TOOLKIT_PATH')
1178 ask_nccl_path = (r'Please specify the location where NCCL %s library is '
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001179 'installed. Refer to README.md for more details. [Default '
1180 'is %s]:') % (tf_nccl_version, default_nccl_path)
Jason Furmanek7c234152018-09-26 04:44:12 +00001181 nccl_install_path = get_from_env_or_user_or_default(
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001182 environ_cp, 'NCCL_INSTALL_PATH', ask_nccl_path, default_nccl_path)
1183
Jason Furmanek7c234152018-09-26 04:44:12 +00001184 # Result returned from "read" will be used unexpanded. That make "~"
1185 # unusable. Going through one more level of expansion to handle that.
1186 nccl_install_path = os.path.realpath(os.path.expanduser(nccl_install_path))
1187 if is_windows() or is_cygwin():
1188 nccl_install_path = cygpath(nccl_install_path)
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001189
Jason Furmanek7c234152018-09-26 04:44:12 +00001190 if is_windows():
1191 nccl_lib_path = 'lib/x64/nccl.lib'
1192 elif is_linux():
1193 nccl_lib_filename = 'libnccl.so.%s' % tf_nccl_version
1194 nccl_lpath = '%s/lib/%s' % (nccl_install_path, nccl_lib_filename)
1195 if not os.path.exists(nccl_lpath):
1196 for relative_path in NCCL_LIB_PATHS:
1197 path = '%s/%s%s' % (nccl_install_path, relative_path, nccl_lib_filename)
1198 if os.path.exists(path):
1199 print("NCCL found at " + path)
1200 nccl_lib_path = path
1201 break
1202 else:
1203 nccl_lib_path = nccl_lpath
1204 elif is_macos():
1205 nccl_lib_path = 'lib/libnccl.%s.dylib' % tf_nccl_version
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001206
Jason Furmanek7c234152018-09-26 04:44:12 +00001207 nccl_lib_path = os.path.join(nccl_install_path, nccl_lib_path)
1208 nccl_hdr_path = os.path.join(os.path.dirname(nccl_lib_path), '../include/nccl.h')
1209 print("Assuming NCCL header path is "+nccl_hdr_path)
1210 if os.path.exists(nccl_lib_path) and os.path.exists(nccl_hdr_path):
1211 # Set NCCL_INSTALL_PATH
1212 environ_cp['NCCL_INSTALL_PATH'] = os.path.dirname(nccl_lib_path)
1213 write_action_env_to_bazelrc('NCCL_INSTALL_PATH', os.path.dirname(nccl_lib_path))
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001214
Jason Furmanek7c234152018-09-26 04:44:12 +00001215 # Set NCCL_HDR_PATH
1216 environ_cp['NCCL_HDR_PATH'] = os.path.dirname(nccl_hdr_path)
1217 write_action_env_to_bazelrc('NCCL_HDR_PATH', os.path.dirname(nccl_hdr_path))
1218 break
1219
1220 # Reset and Retry
1221 print('Invalid path to NCCL %s toolkit, %s or %s not found. Please use the '
1222 'O/S agnostic package of NCCL 2' % (tf_nccl_version, nccl_lib_path,
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001223 nccl_hdr_path))
1224
Jason Furmanek7c234152018-09-26 04:44:12 +00001225 environ_cp['TF_NCCL_VERSION'] = ''
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001226 else:
1227 raise UserInputError('Invalid TF_NCCL setting was provided %d '
1228 'times in a row. Assuming to be a scripting mistake.' %
1229 _DEFAULT_PROMPT_ASK_ATTEMPTS)
1230
1231 # Set TF_NCCL_VERSION
1232 environ_cp['TF_NCCL_VERSION'] = tf_nccl_version
1233 write_action_env_to_bazelrc('TF_NCCL_VERSION', tf_nccl_version)
1234
1235
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001236def get_native_cuda_compute_capabilities(environ_cp):
1237 """Get native cuda compute capabilities.
1238
1239 Args:
1240 environ_cp: copy of the os.environ.
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001241
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001242 Returns:
1243 string of native cuda compute capabilities, separated by comma.
1244 """
1245 device_query_bin = os.path.join(
1246 environ_cp.get('CUDA_TOOLKIT_PATH'), 'extras/demo_suite/deviceQuery')
Jonathan Hseu008910f2017-08-25 14:01:05 -07001247 if os.path.isfile(device_query_bin) and os.access(device_query_bin, os.X_OK):
1248 try:
1249 output = run_shell(device_query_bin).split('\n')
1250 pattern = re.compile('[0-9]*\\.[0-9]*')
1251 output = [pattern.search(x) for x in output if 'Capability' in x]
1252 output = ','.join(x.group() for x in output if x is not None)
1253 except subprocess.CalledProcessError:
1254 output = ''
1255 else:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001256 output = ''
1257 return output
1258
1259
1260def set_tf_cuda_compute_capabilities(environ_cp):
1261 """Set TF_CUDA_COMPUTE_CAPABILITIES."""
1262 while True:
1263 native_cuda_compute_capabilities = get_native_cuda_compute_capabilities(
1264 environ_cp)
1265 if not native_cuda_compute_capabilities:
1266 default_cuda_compute_capabilities = _DEFAULT_CUDA_COMPUTE_CAPABILITIES
1267 else:
1268 default_cuda_compute_capabilities = native_cuda_compute_capabilities
1269
1270 ask_cuda_compute_capabilities = (
1271 'Please specify a list of comma-separated '
1272 'Cuda compute capabilities you want to '
1273 'build with.\nYou can find the compute '
1274 'capability of your device at: '
1275 'https://developer.nvidia.com/cuda-gpus.\nPlease'
1276 ' note that each additional compute '
1277 'capability significantly increases your '
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001278 'build time and binary size. [Default is: %s]: ' %
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001279 default_cuda_compute_capabilities)
1280 tf_cuda_compute_capabilities = get_from_env_or_user_or_default(
1281 environ_cp, 'TF_CUDA_COMPUTE_CAPABILITIES',
1282 ask_cuda_compute_capabilities, default_cuda_compute_capabilities)
1283 # Check whether all capabilities from the input is valid
1284 all_valid = True
Maciejd0f5bc12018-04-30 22:30:58 -05001285 # Remove all whitespace characters before splitting the string
Michael Case51053502018-06-05 17:47:19 -07001286 # that users may insert by accident, as this will result in error
Maciejd0f5bc12018-04-30 22:30:58 -05001287 tf_cuda_compute_capabilities = ''.join(tf_cuda_compute_capabilities.split())
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001288 for compute_capability in tf_cuda_compute_capabilities.split(','):
Andrew Harp6e3e7d12017-08-21 12:10:44 -07001289 m = re.match('[0-9]+.[0-9]+', compute_capability)
1290 if not m:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001291 print('Invalid compute capability: ' % compute_capability)
1292 all_valid = False
Andrew Harp6e3e7d12017-08-21 12:10:44 -07001293 else:
1294 ver = int(m.group(0).split('.')[0])
1295 if ver < 3:
1296 print('Only compute capabilities 3.0 or higher are supported.')
1297 all_valid = False
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001298
1299 if all_valid:
1300 break
1301
1302 # Reset and Retry
1303 environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = ''
1304
1305 # Set TF_CUDA_COMPUTE_CAPABILITIES
1306 environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = tf_cuda_compute_capabilities
1307 write_action_env_to_bazelrc('TF_CUDA_COMPUTE_CAPABILITIES',
1308 tf_cuda_compute_capabilities)
1309
1310
1311def set_other_cuda_vars(environ_cp):
1312 """Set other CUDA related variables."""
A. Unique TensorFlowerab391982018-07-11 04:52:49 -07001313 # If CUDA is enabled, always use GPU during build and test.
1314 if environ_cp.get('TF_CUDA_CLANG') == '1':
1315 write_to_bazelrc('build --config=cuda_clang')
1316 write_to_bazelrc('test --config=cuda_clang')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001317 else:
A. Unique TensorFlowerab391982018-07-11 04:52:49 -07001318 write_to_bazelrc('build --config=cuda')
1319 write_to_bazelrc('test --config=cuda')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001320
1321
1322def set_host_cxx_compiler(environ_cp):
1323 """Set HOST_CXX_COMPILER."""
Jonathan Hseu008910f2017-08-25 14:01:05 -07001324 default_cxx_host_compiler = which('g++') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001325
Austin Anderson6afface2017-12-05 11:59:17 -08001326 host_cxx_compiler = prompt_loop_or_load_from_env(
1327 environ_cp,
1328 var_name='HOST_CXX_COMPILER',
1329 var_default=default_cxx_host_compiler,
1330 ask_for_var=('Please specify which C++ compiler should be used as the '
1331 'host C++ compiler.'),
1332 check_success=os.path.exists,
1333 error_msg='Invalid C++ compiler path. %s cannot be found.',
1334 )
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001335
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001336 write_action_env_to_bazelrc('HOST_CXX_COMPILER', host_cxx_compiler)
1337
1338
1339def set_host_c_compiler(environ_cp):
1340 """Set HOST_C_COMPILER."""
Jonathan Hseu008910f2017-08-25 14:01:05 -07001341 default_c_host_compiler = which('gcc') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001342
Austin Anderson6afface2017-12-05 11:59:17 -08001343 host_c_compiler = prompt_loop_or_load_from_env(
1344 environ_cp,
1345 var_name='HOST_C_COMPILER',
1346 var_default=default_c_host_compiler,
Shanqing Cai71445712018-03-12 19:33:52 -07001347 ask_for_var=('Please specify which C compiler should be used as the host '
Austin Anderson6afface2017-12-05 11:59:17 -08001348 'C compiler.'),
1349 check_success=os.path.exists,
1350 error_msg='Invalid C compiler path. %s cannot be found.',
1351 )
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001352
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001353 write_action_env_to_bazelrc('HOST_C_COMPILER', host_c_compiler)
1354
1355
1356def set_computecpp_toolkit_path(environ_cp):
1357 """Set COMPUTECPP_TOOLKIT_PATH."""
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001358
Austin Anderson6afface2017-12-05 11:59:17 -08001359 def toolkit_exists(toolkit_path):
1360 """Check if a computecpp toolkit path is valid."""
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001361 if is_linux():
1362 sycl_rt_lib_path = 'lib/libComputeCpp.so'
1363 else:
1364 sycl_rt_lib_path = ''
1365
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001366 sycl_rt_lib_path_full = os.path.join(toolkit_path, sycl_rt_lib_path)
Austin Anderson6afface2017-12-05 11:59:17 -08001367 exists = os.path.exists(sycl_rt_lib_path_full)
1368 if not exists:
1369 print('Invalid SYCL %s library path. %s cannot be found' %
1370 (_TF_OPENCL_VERSION, sycl_rt_lib_path_full))
1371 return exists
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001372
Austin Anderson6afface2017-12-05 11:59:17 -08001373 computecpp_toolkit_path = prompt_loop_or_load_from_env(
1374 environ_cp,
1375 var_name='COMPUTECPP_TOOLKIT_PATH',
1376 var_default=_DEFAULT_COMPUTECPP_TOOLKIT_PATH,
1377 ask_for_var=(
1378 'Please specify the location where ComputeCpp for SYCL %s is '
1379 'installed.' % _TF_OPENCL_VERSION),
1380 check_success=toolkit_exists,
1381 error_msg='Invalid SYCL compiler path. %s cannot be found.',
1382 suppress_default_error=True)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001383
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001384 write_action_env_to_bazelrc('COMPUTECPP_TOOLKIT_PATH',
1385 computecpp_toolkit_path)
1386
Michael Cased31531a2018-01-05 14:09:41 -08001387
Dandelion Man?90e42f32017-12-15 18:15:07 -08001388def set_trisycl_include_dir(environ_cp):
Michael Cased31531a2018-01-05 14:09:41 -08001389 """Set TRISYCL_INCLUDE_DIR."""
Frank Chenc4ef9272018-01-10 11:36:52 -08001390
Dandelion Man?90e42f32017-12-15 18:15:07 -08001391 ask_trisycl_include_dir = ('Please specify the location of the triSYCL '
1392 'include directory. (Use --config=sycl_trisycl '
1393 'when building with Bazel) '
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001394 '[Default is %s]: ') % (
1395 _DEFAULT_TRISYCL_INCLUDE_DIR)
Frank Chenc4ef9272018-01-10 11:36:52 -08001396
Dandelion Man?90e42f32017-12-15 18:15:07 -08001397 while True:
1398 trisycl_include_dir = get_from_env_or_user_or_default(
Michael Cased31531a2018-01-05 14:09:41 -08001399 environ_cp, 'TRISYCL_INCLUDE_DIR', ask_trisycl_include_dir,
1400 _DEFAULT_TRISYCL_INCLUDE_DIR)
Dandelion Man?90e42f32017-12-15 18:15:07 -08001401 if os.path.exists(trisycl_include_dir):
1402 break
1403
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001404 print('Invalid triSYCL include directory, %s cannot be found' %
1405 (trisycl_include_dir))
Dandelion Man?90e42f32017-12-15 18:15:07 -08001406
1407 # Set TRISYCL_INCLUDE_DIR
1408 environ_cp['TRISYCL_INCLUDE_DIR'] = trisycl_include_dir
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001409 write_action_env_to_bazelrc('TRISYCL_INCLUDE_DIR', trisycl_include_dir)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001410
Yifei Fengb1d8c592017-11-22 13:42:21 -08001411
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001412def set_mpi_home(environ_cp):
1413 """Set MPI_HOME."""
Austin Anderson6afface2017-12-05 11:59:17 -08001414
Jonathan Hseu008910f2017-08-25 14:01:05 -07001415 default_mpi_home = which('mpirun') or which('mpiexec') or ''
1416 default_mpi_home = os.path.dirname(os.path.dirname(default_mpi_home))
1417
Austin Anderson6afface2017-12-05 11:59:17 -08001418 def valid_mpi_path(mpi_home):
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001419 exists = (
1420 os.path.exists(os.path.join(mpi_home, 'include')) and
1421 os.path.exists(os.path.join(mpi_home, 'lib')))
Austin Anderson6afface2017-12-05 11:59:17 -08001422 if not exists:
1423 print('Invalid path to the MPI Toolkit. %s or %s cannot be found' %
1424 (os.path.join(mpi_home, 'include'),
1425 os.path.exists(os.path.join(mpi_home, 'lib'))))
1426 return exists
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001427
Austin Anderson6afface2017-12-05 11:59:17 -08001428 _ = prompt_loop_or_load_from_env(
1429 environ_cp,
1430 var_name='MPI_HOME',
1431 var_default=default_mpi_home,
1432 ask_for_var='Please specify the MPI toolkit folder.',
1433 check_success=valid_mpi_path,
1434 error_msg='',
1435 suppress_default_error=True)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001436
1437
1438def set_other_mpi_vars(environ_cp):
1439 """Set other MPI related variables."""
1440 # Link the MPI header files
1441 mpi_home = environ_cp.get('MPI_HOME')
1442 symlink_force('%s/include/mpi.h' % mpi_home, 'third_party/mpi/mpi.h')
1443
1444 # Determine if we use OpenMPI or MVAPICH, these require different header files
1445 # to be included here to make bazel dependency checker happy
1446 if os.path.exists(os.path.join(mpi_home, 'include/mpi_portable_platform.h')):
1447 symlink_force(
1448 os.path.join(mpi_home, 'include/mpi_portable_platform.h'),
1449 'third_party/mpi/mpi_portable_platform.h')
1450 # TODO(gunan): avoid editing files in configure
1451 sed_in_place('third_party/mpi/mpi.bzl', 'MPI_LIB_IS_OPENMPI=False',
1452 'MPI_LIB_IS_OPENMPI=True')
1453 else:
1454 # MVAPICH / MPICH
1455 symlink_force(
1456 os.path.join(mpi_home, 'include/mpio.h'), 'third_party/mpi/mpio.h')
1457 symlink_force(
1458 os.path.join(mpi_home, 'include/mpicxx.h'), 'third_party/mpi/mpicxx.h')
1459 # TODO(gunan): avoid editing files in configure
1460 sed_in_place('third_party/mpi/mpi.bzl', 'MPI_LIB_IS_OPENMPI=True',
1461 'MPI_LIB_IS_OPENMPI=False')
1462
1463 if os.path.exists(os.path.join(mpi_home, 'lib/libmpi.so')):
1464 symlink_force(
1465 os.path.join(mpi_home, 'lib/libmpi.so'), 'third_party/mpi/libmpi.so')
1466 else:
1467 raise ValueError('Cannot find the MPI library file in %s/lib' % mpi_home)
1468
1469
A. Unique TensorFlower061c3592017-11-13 14:21:04 -08001470def set_grpc_build_flags():
1471 write_to_bazelrc('build --define grpc_no_ares=true')
1472
Michael Cased31531a2018-01-05 14:09:41 -08001473
Yifei Feng5198cb82018-08-17 13:53:06 -07001474def set_system_libs_flag(environ_cp):
1475 syslibs = environ_cp.get('TF_SYSTEM_LIBS', '')
Yifei Feng5198cb82018-08-17 13:53:06 -07001476 if syslibs and syslibs != '':
Jason Furmanekd5967842018-09-26 05:19:10 +00001477 if ',' in syslibs:
1478 syslibs = ','.join(sorted(syslibs.split(',')))
1479 else:
1480 syslibs = ','.join(sorted(syslibs.split()))
Yifei Feng5198cb82018-08-17 13:53:06 -07001481 write_action_env_to_bazelrc('TF_SYSTEM_LIBS', syslibs)
1482
Jason Furmanekd5967842018-09-26 05:19:10 +00001483 if 'PREFIX' in environ_cp:
1484 write_to_bazelrc('build --define=PREFIX=%s' % environ_cp['PREFIX'])
1485 if 'LIBDIR' in environ_cp:
1486 write_to_bazelrc('build --define=LIBDIR=%s' % environ_cp['LIBDIR'])
1487 if 'INCLUDEDIR' in environ_cp:
Jason Furmanek1668d282018-09-26 05:22:04 +00001488 write_to_bazelrc('build --define=INCLUDEDIR=%s' % environ_cp['INCLUDEDIR'])
Jason Furmanekd5967842018-09-26 05:19:10 +00001489
Yifei Feng5198cb82018-08-17 13:53:06 -07001490
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001491def set_windows_build_flags(environ_cp):
1492 """Set Windows specific build options."""
1493 # The non-monolithic build is not supported yet
1494 write_to_bazelrc('build --config monolithic')
1495 # Suppress warning messages
1496 write_to_bazelrc('build --copt=-w --host_copt=-w')
1497 # Output more verbose information when something goes wrong
1498 write_to_bazelrc('build --verbose_failures')
1499 # The host and target platforms are the same in Windows build. So we don't
1500 # have to distinct them. This avoids building the same targets twice.
1501 write_to_bazelrc('build --distinct_host_configuration=false')
1502 # Enable short object file path to avoid long path issue on Windows.
1503 # TODO(pcloudy): Remove this flag when upgrading Bazel to 0.16.0
1504 # Short object file path will be enabled by default.
1505 write_to_bazelrc('build --experimental_shortened_obj_file_path=true')
1506
1507 if get_var(
1508 environ_cp, 'TF_OVERRIDE_EIGEN_STRONG_INLINE', 'Eigen strong inline',
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001509 True, ('Would you like to override eigen strong inline for some C++ '
1510 'compilation to reduce the compilation time?'),
1511 'Eigen strong inline overridden.', 'Not overriding eigen strong inline, '
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001512 'some compilations could take more than 20 mins.'):
1513 # Due to a known MSVC compiler issue
1514 # https://github.com/tensorflow/tensorflow/issues/10521
1515 # Overriding eigen strong inline speeds up the compiling of
1516 # conv_grad_ops_3d.cc and conv_ops_3d.cc by 20 minutes,
1517 # but this also hurts the performance. Let users decide what they want.
1518 write_to_bazelrc('build --define=override_eigen_strong_inline=true')
Dandelion Man?90e42f32017-12-15 18:15:07 -08001519
A. Unique TensorFlower061c3592017-11-13 14:21:04 -08001520
Michael Cased31531a2018-01-05 14:09:41 -08001521def config_info_line(name, help_text):
1522 """Helper function to print formatted help text for Bazel config options."""
1523 print('\t--config=%-12s\t# %s' % (name, help_text))
1524
1525
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001526def main():
Shanqing Cai71445712018-03-12 19:33:52 -07001527 parser = argparse.ArgumentParser()
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001528 parser.add_argument(
1529 '--workspace',
1530 type=str,
1531 default=_TF_WORKSPACE_ROOT,
1532 help='The absolute path to your active Bazel workspace.')
Shanqing Cai71445712018-03-12 19:33:52 -07001533 args = parser.parse_args()
1534
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001535 # Make a copy of os.environ to be clear when functions and getting and setting
1536 # environment variables.
1537 environ_cp = dict(os.environ)
1538
Yifei Fengbb384112018-07-24 13:12:54 -07001539 check_bazel_version('0.15.0')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001540
Shanqing Cai71445712018-03-12 19:33:52 -07001541 reset_tf_configure_bazelrc(args.workspace)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001542 cleanup_makefile()
Gunhan Gulsoyed89a2b2017-09-19 18:36:26 -07001543 setup_python(environ_cp)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001544
1545 if is_windows():
Yong Tanga7b7aa82018-07-02 07:41:42 -07001546 environ_cp['TF_NEED_AWS'] = '0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001547 environ_cp['TF_NEED_GCP'] = '0'
1548 environ_cp['TF_NEED_HDFS'] = '0'
1549 environ_cp['TF_NEED_JEMALLOC'] = '0'
Michael Cased90054e2018-02-07 14:36:00 -08001550 environ_cp['TF_NEED_KAFKA'] = '0'
Yifei Fengb1d8c592017-11-22 13:42:21 -08001551 environ_cp['TF_NEED_OPENCL_SYCL'] = '0'
1552 environ_cp['TF_NEED_COMPUTECPP'] = '0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001553 environ_cp['TF_NEED_OPENCL'] = '0'
1554 environ_cp['TF_CUDA_CLANG'] = '0'
Guangda Lai76f69382018-01-25 23:59:19 -08001555 environ_cp['TF_NEED_TENSORRT'] = '0'
Ilya Biryukov9e651e42018-03-22 05:33:42 -07001556 # TODO(ibiryukov): Investigate using clang as a cpu or cuda compiler on
1557 # Windows.
1558 environ_cp['TF_DOWNLOAD_CLANG'] = '0'
A. Unique TensorFlower6e97fb32018-07-16 14:07:29 -07001559 environ_cp['TF_ENABLE_XLA'] = '0'
1560 environ_cp['TF_NEED_GDR'] = '0'
1561 environ_cp['TF_NEED_VERBS'] = '0'
1562 environ_cp['TF_NEED_MPI'] = '0'
1563 environ_cp['TF_SET_ANDROID_WORKSPACE'] = '0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001564
1565 if is_macos():
1566 environ_cp['TF_NEED_JEMALLOC'] = '0'
Guangda Lai76f69382018-01-25 23:59:19 -08001567 environ_cp['TF_NEED_TENSORRT'] = '0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001568
Jon Triebenbach6896a742018-06-27 13:29:53 -05001569 # The numpy package on ppc64le uses OpenBLAS which has multi-threading
1570 # issues that lead to incorrect answers. Set OMP_NUM_THREADS=1 at
1571 # runtime to allow the Tensorflow testcases which compare numpy
1572 # results to Tensorflow results to succeed.
1573 if is_ppc64le():
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001574 write_action_env_to_bazelrc('OMP_NUM_THREADS', 1)
Jon Triebenbach6896a742018-06-27 13:29:53 -05001575
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001576 set_build_var(environ_cp, 'TF_NEED_JEMALLOC', 'jemalloc as malloc',
1577 'with_jemalloc', True)
1578 set_build_var(environ_cp, 'TF_NEED_GCP', 'Google Cloud Platform',
Benoit Steiner355e25e2017-10-24 19:47:46 -07001579 'with_gcp_support', True, 'gcp')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001580 set_build_var(environ_cp, 'TF_NEED_HDFS', 'Hadoop File System',
Benoit Steiner355e25e2017-10-24 19:47:46 -07001581 'with_hdfs_support', True, 'hdfs')
Yong Tanga7b7aa82018-07-02 07:41:42 -07001582 set_build_var(environ_cp, 'TF_NEED_AWS', 'Amazon AWS Platform',
1583 'with_aws_support', True, 'aws')
Michael Cased90054e2018-02-07 14:36:00 -08001584 set_build_var(environ_cp, 'TF_NEED_KAFKA', 'Apache Kafka Platform',
Jianwei Xie63dffd52018-03-29 10:50:46 -07001585 'with_kafka_support', True, 'kafka')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001586 set_build_var(environ_cp, 'TF_ENABLE_XLA', 'XLA JIT', 'with_xla_support',
Michael Case98850a52017-09-14 13:35:57 -07001587 False, 'xla')
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001588 set_build_var(environ_cp, 'TF_NEED_GDR', 'GDR', 'with_gdr_support', False,
1589 'gdr')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001590 set_build_var(environ_cp, 'TF_NEED_VERBS', 'VERBS', 'with_verbs_support',
Michael Case98850a52017-09-14 13:35:57 -07001591 False, 'verbs')
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001592 set_build_var(environ_cp, 'TF_NEED_NGRAPH', 'nGraph', 'with_ngraph_support',
1593 False, 'ngraph')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001594
Yifei Fengb1d8c592017-11-22 13:42:21 -08001595 set_action_env_var(environ_cp, 'TF_NEED_OPENCL_SYCL', 'OpenCL SYCL', False)
1596 if environ_cp.get('TF_NEED_OPENCL_SYCL') == '1':
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001597 set_host_cxx_compiler(environ_cp)
1598 set_host_c_compiler(environ_cp)
Yifei Fengb1d8c592017-11-22 13:42:21 -08001599 set_action_env_var(environ_cp, 'TF_NEED_COMPUTECPP', 'ComputeCPP', True)
1600 if environ_cp.get('TF_NEED_COMPUTECPP') == '1':
1601 set_computecpp_toolkit_path(environ_cp)
1602 else:
1603 set_trisycl_include_dir(environ_cp)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001604
1605 set_action_env_var(environ_cp, 'TF_NEED_CUDA', 'CUDA', False)
A. Unique TensorFlower24cbb2a2017-09-08 07:45:44 -07001606 if (environ_cp.get('TF_NEED_CUDA') == '1' and
1607 'TF_CUDA_CONFIG_REPO' not in environ_cp):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001608 set_tf_cuda_version(environ_cp)
Yifei Fengb1d8c592017-11-22 13:42:21 -08001609 set_tf_cudnn_version(environ_cp)
Guangda Lai76f69382018-01-25 23:59:19 -08001610 if is_linux():
1611 set_tf_tensorrt_install_path(environ_cp)
Michael Case0073d132018-04-11 09:34:44 -07001612 set_tf_nccl_install_path(environ_cp)
1613
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001614 set_tf_cuda_compute_capabilities(environ_cp)
Ankur Taly0e6f39d2018-02-16 18:22:55 -08001615 if 'LD_LIBRARY_PATH' in environ_cp and environ_cp.get(
1616 'LD_LIBRARY_PATH') != '1':
1617 write_action_env_to_bazelrc('LD_LIBRARY_PATH',
1618 environ_cp.get('LD_LIBRARY_PATH'))
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001619
1620 set_tf_cuda_clang(environ_cp)
1621 if environ_cp.get('TF_CUDA_CLANG') == '1':
Ilya Biryukov9e651e42018-03-22 05:33:42 -07001622 # Ask whether we should download the clang toolchain.
1623 set_tf_download_clang(environ_cp)
A. Unique TensorFlowerfd29e952017-12-22 03:07:51 -08001624 if environ_cp.get('TF_DOWNLOAD_CLANG') != '1':
1625 # Set up which clang we should use as the cuda / host compiler.
1626 set_clang_cuda_compiler_path(environ_cp)
Ilya Biryukov1c3d02e2018-09-04 03:09:52 -07001627 else:
1628 # Use downloaded LLD for linking.
1629 write_to_bazelrc('build:cuda_clang --config=download_clang_use_lld')
1630 write_to_bazelrc('test:cuda_clang --config=download_clang_use_lld')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001631 else:
1632 # Set up which gcc nvcc should use as the host compiler
1633 # No need to set this on Windows
1634 if not is_windows():
1635 set_gcc_host_compiler_path(environ_cp)
1636 set_other_cuda_vars(environ_cp)
Ilya Biryukov9e651e42018-03-22 05:33:42 -07001637 else:
1638 # CUDA not required. Ask whether we should download the clang toolchain and
1639 # use it for the CPU build.
1640 set_tf_download_clang(environ_cp)
1641 if environ_cp.get('TF_DOWNLOAD_CLANG') == '1':
1642 write_to_bazelrc('build --config=download_clang')
1643 write_to_bazelrc('test --config=download_clang')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001644
1645 set_build_var(environ_cp, 'TF_NEED_MPI', 'MPI', 'with_mpi_support', False)
1646 if environ_cp.get('TF_NEED_MPI') == '1':
1647 set_mpi_home(environ_cp)
1648 set_other_mpi_vars(environ_cp)
1649
A. Unique TensorFlower061c3592017-11-13 14:21:04 -08001650 set_grpc_build_flags()
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001651 set_cc_opt_flags(environ_cp)
Yifei Feng5198cb82018-08-17 13:53:06 -07001652 set_system_libs_flag(environ_cp)
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001653 if is_windows():
1654 set_windows_build_flags(environ_cp)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001655
Anna Ra9a1d5a2018-09-14 12:44:31 -07001656 # Add a config option to build TensorFlow 2.0 API.
1657 write_to_bazelrc('build:v2 --define=tf_api_version=2')
1658
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001659 if get_var(environ_cp, 'TF_SET_ANDROID_WORKSPACE', 'android workspace', False,
1660 ('Would you like to interactively configure ./WORKSPACE for '
1661 'Android builds?'), 'Searching for NDK and SDK installations.',
1662 'Not configuring the WORKSPACE for Android builds.'):
Michael Case51053502018-06-05 17:47:19 -07001663 create_android_ndk_rule(environ_cp)
1664 create_android_sdk_rule(environ_cp)
Austin Anderson6afface2017-12-05 11:59:17 -08001665
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001666 # On Windows, we don't have MKL support and the build is always monolithic.
1667 # So no need to print the following message.
1668 # TODO(pcloudy): remove the following if check when they make sense on Windows
1669 if not is_windows():
1670 print('Preconfigured Bazel build configs. You can use any of the below by '
1671 'adding "--config=<>" to your build command. See tools/bazel.rc for '
1672 'more details.')
1673 config_info_line('mkl', 'Build with MKL support.')
1674 config_info_line('monolithic', 'Config for mostly static monolithic build.')
Austin Anderson6afface2017-12-05 11:59:17 -08001675
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001676
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001677if __name__ == '__main__':
1678 main()