A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1 | # 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 | |
| 17 | from __future__ import absolute_import |
| 18 | from __future__ import division |
| 19 | from __future__ import print_function |
| 20 | |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 21 | import argparse |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 22 | import errno |
| 23 | import os |
| 24 | import platform |
| 25 | import re |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 26 | import subprocess |
| 27 | import sys |
| 28 | |
Andrew Selle | c9885ea | 2017-11-06 09:37:03 -0800 | [diff] [blame] | 29 | # pylint: disable=g-import-not-at-top |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 30 | try: |
| 31 | from shutil import which |
| 32 | except ImportError: |
| 33 | from distutils.spawn import find_executable as which |
Andrew Selle | c9885ea | 2017-11-06 09:37:03 -0800 | [diff] [blame] | 34 | # pylint: enable=g-import-not-at-top |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 35 | |
Dandelion Man? | 90e42f3 | 2017-12-15 18:15:07 -0800 | [diff] [blame] | 36 | _DEFAULT_CUDA_VERSION = '9.0' |
| 37 | _DEFAULT_CUDNN_VERSION = '7' |
Smit Hinsu | 63e6b9b | 2018-07-13 12:46:24 -0700 | [diff] [blame] | 38 | _DEFAULT_NCCL_VERSION = '2.2' |
Smit Hinsu | fe7d1d9 | 2018-07-14 13:16:58 -0700 | [diff] [blame] | 39 | _DEFAULT_CUDA_COMPUTE_CAPABILITIES = '3.5,7.0' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 40 | _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 Feng | b1d8c59 | 2017-11-22 13:42:21 -0800 | [diff] [blame] | 46 | _DEFAULT_TRISYCL_INCLUDE_DIR = '/usr/local/triSYCL/include' |
A. Unique TensorFlower | d340f47 | 2018-08-30 14:00:41 -0700 | [diff] [blame] | 47 | _SUPPORTED_ANDROID_NDK_VERSIONS = [10, 11, 12, 13, 14, 15, 16] |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 48 | |
| 49 | _DEFAULT_PROMPT_ASK_ATTEMPTS = 10 |
| 50 | |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 51 | _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 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 56 | if platform.machine() == 'ppc64le': |
| 57 | _DEFAULT_TENSORRT_PATH_LINUX = '/usr/lib/powerpc64le-linux-gnu/' |
| 58 | else: |
| 59 | _DEFAULT_TENSORRT_PATH_LINUX = '/usr/lib/%s-linux-gnu' % platform.machine() |
| 60 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 61 | |
| 62 | class UserInputError(Exception): |
| 63 | pass |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 64 | |
| 65 | |
| 66 | def is_windows(): |
| 67 | return platform.system() == 'Windows' |
| 68 | |
| 69 | |
| 70 | def is_linux(): |
| 71 | return platform.system() == 'Linux' |
| 72 | |
| 73 | |
| 74 | def is_macos(): |
| 75 | return platform.system() == 'Darwin' |
| 76 | |
| 77 | |
| 78 | def is_ppc64le(): |
| 79 | return platform.machine() == 'ppc64le' |
| 80 | |
| 81 | |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 82 | def is_cygwin(): |
| 83 | return platform.system().startswith('CYGWIN_NT') |
| 84 | |
| 85 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 86 | def get_input(question): |
| 87 | try: |
| 88 | try: |
| 89 | answer = raw_input(question) |
| 90 | except NameError: |
| 91 | answer = input(question) # pylint: disable=bad-builtin |
| 92 | except EOFError: |
| 93 | answer = '' |
| 94 | return answer |
| 95 | |
| 96 | |
| 97 | def symlink_force(target, link_name): |
| 98 | """Force symlink, equivalent of 'ln -sf'. |
| 99 | |
| 100 | Args: |
| 101 | target: items to link to. |
| 102 | link_name: name of the link. |
| 103 | """ |
| 104 | try: |
| 105 | os.symlink(target, link_name) |
| 106 | except OSError as e: |
| 107 | if e.errno == errno.EEXIST: |
| 108 | os.remove(link_name) |
| 109 | os.symlink(target, link_name) |
| 110 | else: |
| 111 | raise e |
| 112 | |
| 113 | |
| 114 | def sed_in_place(filename, old, new): |
| 115 | """Replace old string with new string in file. |
| 116 | |
| 117 | Args: |
| 118 | filename: string for filename. |
| 119 | old: string to replace. |
| 120 | new: new string to replace to. |
| 121 | """ |
| 122 | with open(filename, 'r') as f: |
| 123 | filedata = f.read() |
| 124 | newdata = filedata.replace(old, new) |
| 125 | with open(filename, 'w') as f: |
| 126 | f.write(newdata) |
| 127 | |
| 128 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 129 | def write_to_bazelrc(line): |
| 130 | with open(_TF_BAZELRC, 'a') as f: |
| 131 | f.write(line + '\n') |
| 132 | |
| 133 | |
| 134 | def write_action_env_to_bazelrc(var_name, var): |
| 135 | write_to_bazelrc('build --action_env %s="%s"' % (var_name, str(var))) |
| 136 | |
| 137 | |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 138 | def run_shell(cmd, allow_non_zero=False): |
| 139 | if allow_non_zero: |
| 140 | try: |
| 141 | output = subprocess.check_output(cmd) |
| 142 | except subprocess.CalledProcessError as e: |
| 143 | output = e.output |
| 144 | else: |
| 145 | output = subprocess.check_output(cmd) |
| 146 | return output.decode('UTF-8').strip() |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 147 | |
| 148 | |
| 149 | def cygpath(path): |
| 150 | """Convert path from posix to windows.""" |
Martin Wicke | d57572e | 2017-09-02 19:21:45 -0700 | [diff] [blame] | 151 | return os.path.abspath(path).replace('\\', '/') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 152 | |
| 153 | |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 154 | def get_python_path(environ_cp, python_bin_path): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 155 | """Get the python site package paths.""" |
| 156 | python_paths = [] |
| 157 | if environ_cp.get('PYTHONPATH'): |
| 158 | python_paths = environ_cp.get('PYTHONPATH').split(':') |
| 159 | try: |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 160 | library_paths = run_shell([ |
| 161 | python_bin_path, '-c', |
| 162 | 'import site; print("\\n".join(site.getsitepackages()))' |
| 163 | ]).split('\n') |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 164 | except subprocess.CalledProcessError: |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 165 | library_paths = [ |
| 166 | run_shell([ |
| 167 | python_bin_path, '-c', |
| 168 | 'from distutils.sysconfig import get_python_lib;' |
| 169 | 'print(get_python_lib())' |
| 170 | ]) |
| 171 | ] |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 172 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 173 | all_paths = set(python_paths + library_paths) |
| 174 | |
| 175 | paths = [] |
| 176 | for path in all_paths: |
| 177 | if os.path.isdir(path): |
| 178 | paths.append(path) |
| 179 | return paths |
| 180 | |
| 181 | |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 182 | def get_python_major_version(python_bin_path): |
| 183 | """Get the python major version.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 184 | return run_shell([python_bin_path, '-c', 'import sys; print(sys.version[0])']) |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 185 | |
| 186 | |
Gunhan Gulsoy | ed89a2b | 2017-09-19 18:36:26 -0700 | [diff] [blame] | 187 | def setup_python(environ_cp): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 188 | """Setup python related env variables.""" |
| 189 | # Get PYTHON_BIN_PATH, default is the current running python. |
| 190 | default_python_bin_path = sys.executable |
| 191 | ask_python_bin_path = ('Please specify the location of python. [Default is ' |
| 192 | '%s]: ') % default_python_bin_path |
| 193 | while True: |
| 194 | python_bin_path = get_from_env_or_user_or_default( |
| 195 | environ_cp, 'PYTHON_BIN_PATH', ask_python_bin_path, |
| 196 | default_python_bin_path) |
| 197 | # Check if the path is valid |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 198 | if os.path.isfile(python_bin_path) and os.access(python_bin_path, os.X_OK): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 199 | break |
| 200 | elif not os.path.exists(python_bin_path): |
| 201 | print('Invalid python path: %s cannot be found.' % python_bin_path) |
| 202 | else: |
| 203 | print('%s is not executable. Is it the python binary?' % python_bin_path) |
| 204 | environ_cp['PYTHON_BIN_PATH'] = '' |
| 205 | |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 206 | # Convert python path to Windows style before checking lib and version |
Martin Wicke | d57572e | 2017-09-02 19:21:45 -0700 | [diff] [blame] | 207 | if is_windows() or is_cygwin(): |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 208 | python_bin_path = cygpath(python_bin_path) |
| 209 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 210 | # Get PYTHON_LIB_PATH |
| 211 | python_lib_path = environ_cp.get('PYTHON_LIB_PATH') |
| 212 | if not python_lib_path: |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 213 | python_lib_paths = get_python_path(environ_cp, python_bin_path) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 214 | if environ_cp.get('USE_DEFAULT_PYTHON_LIB_PATH') == '1': |
Vijay Vasudevan | a1fba7f | 2017-07-28 10:58:56 -0700 | [diff] [blame] | 215 | python_lib_path = python_lib_paths[0] |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 216 | else: |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 217 | print('Found possible Python library paths:\n %s' % |
| 218 | '\n '.join(python_lib_paths)) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 219 | default_python_lib_path = python_lib_paths[0] |
| 220 | python_lib_path = get_input( |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 221 | 'Please input the desired Python library path to use. ' |
| 222 | 'Default is [%s]\n' % python_lib_paths[0]) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 223 | if not python_lib_path: |
| 224 | python_lib_path = default_python_lib_path |
Vijay Vasudevan | a1fba7f | 2017-07-28 10:58:56 -0700 | [diff] [blame] | 225 | environ_cp['PYTHON_LIB_PATH'] = python_lib_path |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 226 | |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 227 | python_major_version = get_python_major_version(python_bin_path) |
| 228 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 229 | # Convert python path to Windows style before writing into bazel.rc |
Martin Wicke | d57572e | 2017-09-02 19:21:45 -0700 | [diff] [blame] | 230 | if is_windows() or is_cygwin(): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 231 | python_lib_path = cygpath(python_lib_path) |
| 232 | |
| 233 | # Set-up env variables used by python_configure.bzl |
| 234 | write_action_env_to_bazelrc('PYTHON_BIN_PATH', python_bin_path) |
| 235 | write_action_env_to_bazelrc('PYTHON_LIB_PATH', python_lib_path) |
Gunhan Gulsoy | ed89a2b | 2017-09-19 18:36:26 -0700 | [diff] [blame] | 236 | write_to_bazelrc('build --python_path=\"%s"' % python_bin_path) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 237 | environ_cp['PYTHON_BIN_PATH'] = python_bin_path |
| 238 | |
| 239 | # Write tools/python_bin_path.sh |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 240 | with open( |
| 241 | os.path.join(_TF_WORKSPACE_ROOT, 'tools', 'python_bin_path.sh'), |
| 242 | 'w') as f: |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 243 | f.write('export PYTHON_BIN_PATH="%s"' % python_bin_path) |
| 244 | |
| 245 | |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 246 | def reset_tf_configure_bazelrc(workspace_path): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 247 | """Reset file that contains customized config settings.""" |
| 248 | open(_TF_BAZELRC, 'w').close() |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 249 | bazelrc_path = os.path.join(workspace_path, '.bazelrc') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 250 | |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 251 | data = [] |
| 252 | if os.path.exists(bazelrc_path): |
| 253 | with open(bazelrc_path, 'r') as f: |
| 254 | data = f.read().splitlines() |
| 255 | with open(bazelrc_path, 'w') as f: |
| 256 | for l in data: |
| 257 | if _TF_BAZELRC_FILENAME in l: |
| 258 | continue |
| 259 | f.write('%s\n' % l) |
| 260 | if is_windows(): |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 261 | tf_bazelrc_path = _TF_BAZELRC.replace('\\', '/') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 262 | else: |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 263 | tf_bazelrc_path = _TF_BAZELRC |
| 264 | f.write('import %s\n' % tf_bazelrc_path) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 265 | |
| 266 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 267 | def cleanup_makefile(): |
| 268 | """Delete any leftover BUILD files from the Makefile build. |
| 269 | |
| 270 | These files could interfere with Bazel parsing. |
| 271 | """ |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 272 | makefile_download_dir = os.path.join(_TF_WORKSPACE_ROOT, 'tensorflow', |
| 273 | 'contrib', 'makefile', 'downloads') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 274 | if os.path.isdir(makefile_download_dir): |
| 275 | for root, _, filenames in os.walk(makefile_download_dir): |
| 276 | for f in filenames: |
| 277 | if f.endswith('BUILD'): |
| 278 | os.remove(os.path.join(root, f)) |
| 279 | |
| 280 | |
| 281 | def get_var(environ_cp, |
| 282 | var_name, |
| 283 | query_item, |
| 284 | enabled_by_default, |
| 285 | question=None, |
| 286 | yes_reply=None, |
| 287 | no_reply=None): |
| 288 | """Get boolean input from user. |
| 289 | |
| 290 | If var_name is not set in env, ask user to enable query_item or not. If the |
| 291 | response is empty, use the default. |
| 292 | |
| 293 | Args: |
| 294 | environ_cp: copy of the os.environ. |
| 295 | var_name: string for name of environment variable, e.g. "TF_NEED_HDFS". |
| 296 | query_item: string for feature related to the variable, e.g. "Hadoop File |
| 297 | System". |
| 298 | enabled_by_default: boolean for default behavior. |
| 299 | question: optional string for how to ask for user input. |
Michael Case | d90054e | 2018-02-07 14:36:00 -0800 | [diff] [blame] | 300 | yes_reply: optional string for reply when feature is enabled. |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 301 | no_reply: optional string for reply when feature is disabled. |
| 302 | |
| 303 | Returns: |
| 304 | boolean value of the variable. |
Frank Chen | c4ef927 | 2018-01-10 11:36:52 -0800 | [diff] [blame] | 305 | |
| 306 | Raises: |
| 307 | UserInputError: if an environment variable is set, but it cannot be |
| 308 | interpreted as a boolean indicator, assume that the user has made a |
| 309 | scripting error, and will continue to provide invalid input. |
| 310 | Raise the error to avoid infinitely looping. |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 311 | """ |
| 312 | if not question: |
| 313 | question = 'Do you wish to build TensorFlow with %s support?' % query_item |
| 314 | if not yes_reply: |
| 315 | yes_reply = '%s support will be enabled for TensorFlow.' % query_item |
| 316 | if not no_reply: |
| 317 | no_reply = 'No %s' % yes_reply |
| 318 | |
| 319 | yes_reply += '\n' |
| 320 | no_reply += '\n' |
| 321 | |
| 322 | if enabled_by_default: |
| 323 | question += ' [Y/n]: ' |
| 324 | else: |
| 325 | question += ' [y/N]: ' |
| 326 | |
| 327 | var = environ_cp.get(var_name) |
Frank Chen | c4ef927 | 2018-01-10 11:36:52 -0800 | [diff] [blame] | 328 | if var is not None: |
| 329 | var_content = var.strip().lower() |
| 330 | true_strings = ('1', 't', 'true', 'y', 'yes') |
| 331 | false_strings = ('0', 'f', 'false', 'n', 'no') |
| 332 | if var_content in true_strings: |
| 333 | var = True |
| 334 | elif var_content in false_strings: |
| 335 | var = False |
| 336 | else: |
| 337 | raise UserInputError( |
| 338 | 'Environment variable %s must be set as a boolean indicator.\n' |
| 339 | 'The following are accepted as TRUE : %s.\n' |
| 340 | 'The following are accepted as FALSE: %s.\n' |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 341 | 'Current value is %s.' % (var_name, ', '.join(true_strings), |
| 342 | ', '.join(false_strings), var)) |
Frank Chen | c4ef927 | 2018-01-10 11:36:52 -0800 | [diff] [blame] | 343 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 344 | while var is None: |
| 345 | user_input_origin = get_input(question) |
| 346 | user_input = user_input_origin.strip().lower() |
| 347 | if user_input == 'y': |
| 348 | print(yes_reply) |
| 349 | var = True |
| 350 | elif user_input == 'n': |
| 351 | print(no_reply) |
| 352 | var = False |
| 353 | elif not user_input: |
| 354 | if enabled_by_default: |
| 355 | print(yes_reply) |
| 356 | var = True |
| 357 | else: |
| 358 | print(no_reply) |
| 359 | var = False |
| 360 | else: |
| 361 | print('Invalid selection: %s' % user_input_origin) |
| 362 | return var |
| 363 | |
| 364 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 365 | def set_build_var(environ_cp, |
| 366 | var_name, |
| 367 | query_item, |
| 368 | option_name, |
| 369 | enabled_by_default, |
| 370 | bazel_config_name=None): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 371 | """Set if query_item will be enabled for the build. |
| 372 | |
| 373 | Ask user if query_item will be enabled. Default is used if no input is given. |
| 374 | Set subprocess environment variable and write to .bazelrc if enabled. |
| 375 | |
| 376 | Args: |
| 377 | environ_cp: copy of the os.environ. |
| 378 | var_name: string for name of environment variable, e.g. "TF_NEED_HDFS". |
| 379 | query_item: string for feature related to the variable, e.g. "Hadoop File |
| 380 | System". |
| 381 | option_name: string for option to define in .bazelrc. |
| 382 | enabled_by_default: boolean for default behavior. |
Michael Case | 98850a5 | 2017-09-14 13:35:57 -0700 | [diff] [blame] | 383 | bazel_config_name: Name for Bazel --config argument to enable build feature. |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 384 | """ |
| 385 | |
| 386 | var = str(int(get_var(environ_cp, var_name, query_item, enabled_by_default))) |
| 387 | environ_cp[var_name] = var |
| 388 | if var == '1': |
| 389 | write_to_bazelrc('build --define %s=true' % option_name) |
Michael Case | 98850a5 | 2017-09-14 13:35:57 -0700 | [diff] [blame] | 390 | elif bazel_config_name is not None: |
| 391 | # TODO(mikecase): Migrate all users of configure.py to use --config Bazel |
| 392 | # options and not to set build configs through environment variables. |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 393 | write_to_bazelrc( |
| 394 | 'build:%s --define %s=true' % (bazel_config_name, option_name)) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 395 | |
| 396 | |
| 397 | def set_action_env_var(environ_cp, |
| 398 | var_name, |
| 399 | query_item, |
| 400 | enabled_by_default, |
| 401 | question=None, |
| 402 | yes_reply=None, |
| 403 | no_reply=None): |
| 404 | """Set boolean action_env variable. |
| 405 | |
| 406 | Ask user if query_item will be enabled. Default is used if no input is given. |
| 407 | Set environment variable and write to .bazelrc. |
| 408 | |
| 409 | Args: |
| 410 | environ_cp: copy of the os.environ. |
| 411 | var_name: string for name of environment variable, e.g. "TF_NEED_HDFS". |
| 412 | query_item: string for feature related to the variable, e.g. "Hadoop File |
| 413 | System". |
| 414 | enabled_by_default: boolean for default behavior. |
| 415 | question: optional string for how to ask for user input. |
Michael Case | d90054e | 2018-02-07 14:36:00 -0800 | [diff] [blame] | 416 | yes_reply: optional string for reply when feature is enabled. |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 417 | no_reply: optional string for reply when feature is disabled. |
| 418 | """ |
| 419 | var = int( |
| 420 | get_var(environ_cp, var_name, query_item, enabled_by_default, question, |
| 421 | yes_reply, no_reply)) |
| 422 | |
| 423 | write_action_env_to_bazelrc(var_name, var) |
| 424 | environ_cp[var_name] = str(var) |
| 425 | |
| 426 | |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 427 | def convert_version_to_int(version): |
| 428 | """Convert a version number to a integer that can be used to compare. |
| 429 | |
A. Unique TensorFlower | 28ce1d1 | 2017-08-15 12:08:29 -0700 | [diff] [blame] | 430 | Version strings of the form X.YZ and X.Y.Z-xxxxx are supported. The |
| 431 | 'xxxxx' part, for instance 'homebrew' on OS/X, is ignored. |
| 432 | |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 433 | Args: |
A. Unique TensorFlower | 28ce1d1 | 2017-08-15 12:08:29 -0700 | [diff] [blame] | 434 | version: a version to be converted |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 435 | |
| 436 | Returns: |
| 437 | An integer if converted successfully, otherwise return None. |
| 438 | """ |
A. Unique TensorFlower | 28ce1d1 | 2017-08-15 12:08:29 -0700 | [diff] [blame] | 439 | version = version.split('-')[0] |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 440 | version_segments = version.split('.') |
| 441 | for seg in version_segments: |
| 442 | if not seg.isdigit(): |
| 443 | return None |
| 444 | |
| 445 | version_str = ''.join(['%03d' % int(seg) for seg in version_segments]) |
| 446 | return int(version_str) |
| 447 | |
| 448 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 449 | def check_bazel_version(min_version): |
Yifei Feng | dce9a49 | 2018-02-22 14:24:57 -0800 | [diff] [blame] | 450 | """Check installed bazel version is at least min_version. |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 451 | |
| 452 | Args: |
| 453 | min_version: string for minimum bazel version. |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 454 | |
| 455 | Returns: |
| 456 | The bazel version detected. |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 457 | """ |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 458 | if which('bazel') is None: |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 459 | print('Cannot find bazel. Please install bazel.') |
| 460 | sys.exit(0) |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 461 | curr_version = run_shell( |
| 462 | ['bazel', '--batch', '--bazelrc=/dev/null', 'version']) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 463 | |
| 464 | for line in curr_version.split('\n'): |
| 465 | if 'Build label: ' in line: |
| 466 | curr_version = line.split('Build label: ')[1] |
| 467 | break |
| 468 | |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 469 | min_version_int = convert_version_to_int(min_version) |
| 470 | curr_version_int = convert_version_to_int(curr_version) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 471 | |
| 472 | # Check if current bazel version can be detected properly. |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 473 | if not curr_version_int: |
| 474 | print('WARNING: current bazel installation is not a release version.') |
| 475 | print('Make sure you are running at least bazel %s' % min_version) |
| 476 | return curr_version |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 477 | |
Michael Case | d94271a | 2017-08-22 17:26:52 -0700 | [diff] [blame] | 478 | print('You have bazel %s installed.' % curr_version) |
A. Unique TensorFlower | 28ce1d1 | 2017-08-15 12:08:29 -0700 | [diff] [blame] | 479 | |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 480 | if curr_version_int < min_version_int: |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 481 | print('Please upgrade your bazel installation to version %s or higher to ' |
| 482 | 'build TensorFlow!' % min_version) |
| 483 | sys.exit(0) |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 484 | return curr_version |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 485 | |
| 486 | |
| 487 | def set_cc_opt_flags(environ_cp): |
| 488 | """Set up architecture-dependent optimization flags. |
| 489 | |
| 490 | Also append CC optimization flags to bazel.rc.. |
| 491 | |
| 492 | Args: |
| 493 | environ_cp: copy of the os.environ. |
| 494 | """ |
| 495 | if is_ppc64le(): |
| 496 | # gcc on ppc64le does not support -march, use mcpu instead |
| 497 | default_cc_opt_flags = '-mcpu=native' |
A. Unique TensorFlower | 1bba94a | 2018-04-04 15:45:20 -0700 | [diff] [blame] | 498 | elif is_windows(): |
| 499 | default_cc_opt_flags = '/arch:AVX' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 500 | else: |
| 501 | default_cc_opt_flags = '-march=native' |
| 502 | question = ('Please specify optimization flags to use during compilation when' |
| 503 | ' bazel option "--config=opt" is specified [Default is %s]: ' |
| 504 | ) % default_cc_opt_flags |
| 505 | cc_opt_flags = get_from_env_or_user_or_default(environ_cp, 'CC_OPT_FLAGS', |
| 506 | question, default_cc_opt_flags) |
| 507 | for opt in cc_opt_flags.split(): |
Michael Case | 0017742 | 2017-11-10 13:14:03 -0800 | [diff] [blame] | 508 | write_to_bazelrc('build:opt --copt=%s' % opt) |
| 509 | # It should be safe on the same build host. |
A. Unique TensorFlower | 1bba94a | 2018-04-04 15:45:20 -0700 | [diff] [blame] | 510 | if not is_ppc64le() and not is_windows(): |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 511 | write_to_bazelrc('build:opt --host_copt=-march=native') |
Michael Case | bb3355d | 2017-11-09 08:46:31 -0800 | [diff] [blame] | 512 | write_to_bazelrc('build:opt --define with_default_optimizations=true') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 513 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 514 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 515 | def set_tf_cuda_clang(environ_cp): |
| 516 | """set TF_CUDA_CLANG action_env. |
| 517 | |
| 518 | Args: |
| 519 | environ_cp: copy of the os.environ. |
| 520 | """ |
| 521 | question = 'Do you want to use clang as CUDA compiler?' |
| 522 | yes_reply = 'Clang will be used as CUDA compiler.' |
| 523 | no_reply = 'nvcc will be used as CUDA compiler.' |
| 524 | set_action_env_var( |
| 525 | environ_cp, |
| 526 | 'TF_CUDA_CLANG', |
| 527 | None, |
| 528 | False, |
| 529 | question=question, |
| 530 | yes_reply=yes_reply, |
| 531 | no_reply=no_reply) |
| 532 | |
| 533 | |
A. Unique TensorFlower | fd29e95 | 2017-12-22 03:07:51 -0800 | [diff] [blame] | 534 | def set_tf_download_clang(environ_cp): |
| 535 | """Set TF_DOWNLOAD_CLANG action_env.""" |
Ilya Biryukov | 9e651e4 | 2018-03-22 05:33:42 -0700 | [diff] [blame] | 536 | question = 'Do you wish to download a fresh release of clang? (Experimental)' |
A. Unique TensorFlower | fd29e95 | 2017-12-22 03:07:51 -0800 | [diff] [blame] | 537 | yes_reply = 'Clang will be downloaded and used to compile tensorflow.' |
| 538 | no_reply = 'Clang will not be downloaded.' |
| 539 | set_action_env_var( |
| 540 | environ_cp, |
| 541 | 'TF_DOWNLOAD_CLANG', |
| 542 | None, |
| 543 | False, |
| 544 | question=question, |
| 545 | yes_reply=yes_reply, |
| 546 | no_reply=no_reply) |
| 547 | |
| 548 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 549 | def get_from_env_or_user_or_default(environ_cp, var_name, ask_for_var, |
| 550 | var_default): |
| 551 | """Get var_name either from env, or user or default. |
| 552 | |
| 553 | If var_name has been set as environment variable, use the preset value, else |
| 554 | ask for user input. If no input is provided, the default is used. |
| 555 | |
| 556 | Args: |
| 557 | environ_cp: copy of the os.environ. |
| 558 | var_name: string for name of environment variable, e.g. "TF_NEED_HDFS". |
| 559 | ask_for_var: string for how to ask for user input. |
| 560 | var_default: default value string. |
| 561 | |
| 562 | Returns: |
| 563 | string value for var_name |
| 564 | """ |
| 565 | var = environ_cp.get(var_name) |
| 566 | if not var: |
| 567 | var = get_input(ask_for_var) |
Michael Case | d94271a | 2017-08-22 17:26:52 -0700 | [diff] [blame] | 568 | print('\n') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 569 | if not var: |
| 570 | var = var_default |
| 571 | return var |
| 572 | |
| 573 | |
| 574 | def set_clang_cuda_compiler_path(environ_cp): |
| 575 | """Set CLANG_CUDA_COMPILER_PATH.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 576 | default_clang_path = which('clang') or '' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 577 | ask_clang_path = ('Please specify which clang should be used as device and ' |
| 578 | 'host compiler. [Default is %s]: ') % default_clang_path |
| 579 | |
| 580 | while True: |
| 581 | clang_cuda_compiler_path = get_from_env_or_user_or_default( |
| 582 | environ_cp, 'CLANG_CUDA_COMPILER_PATH', ask_clang_path, |
| 583 | default_clang_path) |
| 584 | if os.path.exists(clang_cuda_compiler_path): |
| 585 | break |
| 586 | |
| 587 | # Reset and retry |
| 588 | print('Invalid clang path: %s cannot be found.' % clang_cuda_compiler_path) |
| 589 | environ_cp['CLANG_CUDA_COMPILER_PATH'] = '' |
| 590 | |
| 591 | # Set CLANG_CUDA_COMPILER_PATH |
| 592 | environ_cp['CLANG_CUDA_COMPILER_PATH'] = clang_cuda_compiler_path |
| 593 | write_action_env_to_bazelrc('CLANG_CUDA_COMPILER_PATH', |
| 594 | clang_cuda_compiler_path) |
| 595 | |
| 596 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 597 | def prompt_loop_or_load_from_env(environ_cp, |
| 598 | var_name, |
| 599 | var_default, |
| 600 | ask_for_var, |
| 601 | check_success, |
| 602 | error_msg, |
| 603 | suppress_default_error=False, |
| 604 | n_ask_attempts=_DEFAULT_PROMPT_ASK_ATTEMPTS): |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 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 Chen | c4ef927 | 2018-01-10 11:36:52 -0800 | [diff] [blame] | 632 | 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 Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 635 | """ |
| 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): |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 643 | val = get_from_env_or_user_or_default(environ_cp, var_name, full_query, |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 644 | default) |
| 645 | if check_success(val): |
| 646 | break |
| 647 | if not suppress_default_error: |
| 648 | print(error_msg % val) |
| 649 | environ_cp[var_name] = '' |
| 650 | else: |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 651 | raise UserInputError( |
| 652 | 'Invalid %s setting was provided %d times in a row. ' |
| 653 | 'Assuming to be a scripting mistake.' % (var_name, n_ask_attempts)) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 654 | |
| 655 | environ_cp[var_name] = val |
| 656 | return val |
| 657 | |
| 658 | |
| 659 | def create_android_ndk_rule(environ_cp): |
| 660 | """Set ANDROID_NDK_HOME and write Android NDK WORKSPACE rule.""" |
| 661 | if is_windows() or is_cygwin(): |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 662 | default_ndk_path = cygpath( |
| 663 | '%s/Android/Sdk/ndk-bundle' % environ_cp['APPDATA']) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 664 | elif is_macos(): |
| 665 | default_ndk_path = '%s/library/Android/Sdk/ndk-bundle' % environ_cp['HOME'] |
| 666 | else: |
| 667 | default_ndk_path = '%s/Android/Sdk/ndk-bundle' % environ_cp['HOME'] |
| 668 | |
| 669 | def valid_ndk_path(path): |
| 670 | return (os.path.exists(path) and |
| 671 | os.path.exists(os.path.join(path, 'source.properties'))) |
| 672 | |
| 673 | android_ndk_home_path = prompt_loop_or_load_from_env( |
| 674 | environ_cp, |
| 675 | var_name='ANDROID_NDK_HOME', |
| 676 | var_default=default_ndk_path, |
| 677 | ask_for_var='Please specify the home path of the Android NDK to use.', |
| 678 | check_success=valid_ndk_path, |
| 679 | error_msg=('The path %s or its child file "source.properties" ' |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 680 | 'does not exist.')) |
Michael Case | 5105350 | 2018-06-05 17:47:19 -0700 | [diff] [blame] | 681 | write_action_env_to_bazelrc('ANDROID_NDK_HOME', android_ndk_home_path) |
| 682 | write_action_env_to_bazelrc('ANDROID_NDK_API_LEVEL', |
| 683 | check_ndk_level(android_ndk_home_path)) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 684 | |
| 685 | |
| 686 | def create_android_sdk_rule(environ_cp): |
| 687 | """Set Android variables and write Android SDK WORKSPACE rule.""" |
| 688 | if is_windows() or is_cygwin(): |
| 689 | default_sdk_path = cygpath('%s/Android/Sdk' % environ_cp['APPDATA']) |
| 690 | elif is_macos(): |
Shashi Shekhar | c0ff0cc | 2018-07-17 09:00:24 -0700 | [diff] [blame] | 691 | default_sdk_path = '%s/library/Android/Sdk' % environ_cp['HOME'] |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 692 | else: |
| 693 | default_sdk_path = '%s/Android/Sdk' % environ_cp['HOME'] |
| 694 | |
| 695 | def valid_sdk_path(path): |
| 696 | return (os.path.exists(path) and |
| 697 | os.path.exists(os.path.join(path, 'platforms')) and |
| 698 | os.path.exists(os.path.join(path, 'build-tools'))) |
| 699 | |
| 700 | android_sdk_home_path = prompt_loop_or_load_from_env( |
| 701 | environ_cp, |
| 702 | var_name='ANDROID_SDK_HOME', |
| 703 | var_default=default_sdk_path, |
| 704 | ask_for_var='Please specify the home path of the Android SDK to use.', |
| 705 | check_success=valid_sdk_path, |
| 706 | error_msg=('Either %s does not exist, or it does not contain the ' |
| 707 | 'subdirectories "platforms" and "build-tools".')) |
| 708 | |
| 709 | platforms = os.path.join(android_sdk_home_path, 'platforms') |
| 710 | api_levels = sorted(os.listdir(platforms)) |
| 711 | api_levels = [x.replace('android-', '') for x in api_levels] |
| 712 | |
| 713 | def valid_api_level(api_level): |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 714 | return os.path.exists( |
| 715 | os.path.join(android_sdk_home_path, 'platforms', |
| 716 | 'android-' + api_level)) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 717 | |
| 718 | android_api_level = prompt_loop_or_load_from_env( |
| 719 | environ_cp, |
| 720 | var_name='ANDROID_API_LEVEL', |
| 721 | var_default=api_levels[-1], |
| 722 | ask_for_var=('Please specify the Android SDK API level to use. ' |
| 723 | '[Available levels: %s]') % api_levels, |
| 724 | check_success=valid_api_level, |
| 725 | error_msg='Android-%s is not present in the SDK path.') |
| 726 | |
| 727 | build_tools = os.path.join(android_sdk_home_path, 'build-tools') |
| 728 | versions = sorted(os.listdir(build_tools)) |
| 729 | |
| 730 | def valid_build_tools(version): |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 731 | return os.path.exists( |
| 732 | os.path.join(android_sdk_home_path, 'build-tools', version)) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 733 | |
| 734 | android_build_tools_version = prompt_loop_or_load_from_env( |
| 735 | environ_cp, |
| 736 | var_name='ANDROID_BUILD_TOOLS_VERSION', |
| 737 | var_default=versions[-1], |
| 738 | ask_for_var=('Please specify an Android build tools version to use. ' |
| 739 | '[Available versions: %s]') % versions, |
| 740 | check_success=valid_build_tools, |
| 741 | error_msg=('The selected SDK does not have build-tools version %s ' |
| 742 | 'available.')) |
| 743 | |
Michael Case | 5105350 | 2018-06-05 17:47:19 -0700 | [diff] [blame] | 744 | write_action_env_to_bazelrc('ANDROID_BUILD_TOOLS_VERSION', |
| 745 | android_build_tools_version) |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 746 | write_action_env_to_bazelrc('ANDROID_SDK_API_LEVEL', android_api_level) |
| 747 | write_action_env_to_bazelrc('ANDROID_SDK_HOME', android_sdk_home_path) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 748 | |
| 749 | |
| 750 | def check_ndk_level(android_ndk_home_path): |
| 751 | """Check the revision number of an Android NDK path.""" |
| 752 | properties_path = '%s/source.properties' % android_ndk_home_path |
| 753 | if is_windows() or is_cygwin(): |
| 754 | properties_path = cygpath(properties_path) |
| 755 | with open(properties_path, 'r') as f: |
| 756 | filedata = f.read() |
| 757 | |
| 758 | revision = re.search(r'Pkg.Revision = (\d+)', filedata) |
| 759 | if revision: |
Michael Case | 5105350 | 2018-06-05 17:47:19 -0700 | [diff] [blame] | 760 | ndk_api_level = revision.group(1) |
| 761 | else: |
| 762 | raise Exception('Unable to parse NDK revision.') |
| 763 | if int(ndk_api_level) not in _SUPPORTED_ANDROID_NDK_VERSIONS: |
| 764 | print('WARNING: The API level of the NDK in %s is %s, which is not ' |
| 765 | 'supported by Bazel (officially supported versions: %s). Please use ' |
| 766 | 'another version. Compiling Android targets may result in confusing ' |
| 767 | 'errors.\n' % (android_ndk_home_path, ndk_api_level, |
| 768 | _SUPPORTED_ANDROID_NDK_VERSIONS)) |
| 769 | return ndk_api_level |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 770 | |
| 771 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 772 | def set_gcc_host_compiler_path(environ_cp): |
| 773 | """Set GCC_HOST_COMPILER_PATH.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 774 | default_gcc_host_compiler_path = which('gcc') or '' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 775 | cuda_bin_symlink = '%s/bin/gcc' % environ_cp.get('CUDA_TOOLKIT_PATH') |
| 776 | |
| 777 | if os.path.islink(cuda_bin_symlink): |
| 778 | # os.readlink is only available in linux |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 779 | default_gcc_host_compiler_path = os.path.realpath(cuda_bin_symlink) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 780 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 781 | gcc_host_compiler_path = prompt_loop_or_load_from_env( |
| 782 | environ_cp, |
| 783 | var_name='GCC_HOST_COMPILER_PATH', |
| 784 | var_default=default_gcc_host_compiler_path, |
| 785 | ask_for_var= |
| 786 | 'Please specify which gcc should be used by nvcc as the host compiler.', |
| 787 | check_success=os.path.exists, |
| 788 | error_msg='Invalid gcc path. %s cannot be found.', |
| 789 | ) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 790 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 791 | write_action_env_to_bazelrc('GCC_HOST_COMPILER_PATH', gcc_host_compiler_path) |
| 792 | |
| 793 | |
Ankur Taly | 0e6f39d | 2018-02-16 18:22:55 -0800 | [diff] [blame] | 794 | def reformat_version_sequence(version_str, sequence_count): |
| 795 | """Reformat the version string to have the given number of sequences. |
| 796 | |
| 797 | For example: |
| 798 | Given (7, 2) -> 7.0 |
| 799 | (7.0.1, 2) -> 7.0 |
| 800 | (5, 1) -> 5 |
| 801 | (5.0.3.2, 1) -> 5 |
| 802 | |
| 803 | Args: |
| 804 | version_str: String, the version string. |
| 805 | sequence_count: int, an integer. |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 806 | |
Ankur Taly | 0e6f39d | 2018-02-16 18:22:55 -0800 | [diff] [blame] | 807 | Returns: |
| 808 | string, reformatted version string. |
| 809 | """ |
| 810 | v = version_str.split('.') |
| 811 | if len(v) < sequence_count: |
| 812 | v = v + (['0'] * (sequence_count - len(v))) |
| 813 | |
| 814 | return '.'.join(v[:sequence_count]) |
| 815 | |
| 816 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 817 | def set_tf_cuda_version(environ_cp): |
| 818 | """Set CUDA_TOOLKIT_PATH and TF_CUDA_VERSION.""" |
| 819 | ask_cuda_version = ( |
A. Unique TensorFlower | b15500b | 2018-05-08 12:04:38 -0700 | [diff] [blame] | 820 | 'Please specify the CUDA SDK version you want to use. ' |
| 821 | '[Leave empty to default to CUDA %s]: ') % _DEFAULT_CUDA_VERSION |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 822 | |
Austin Anderson | f9a88f8 | 2017-12-13 11:49:40 -0800 | [diff] [blame] | 823 | for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 824 | # Configure the Cuda SDK version to use. |
| 825 | tf_cuda_version = get_from_env_or_user_or_default( |
| 826 | environ_cp, 'TF_CUDA_VERSION', ask_cuda_version, _DEFAULT_CUDA_VERSION) |
Ankur Taly | 0e6f39d | 2018-02-16 18:22:55 -0800 | [diff] [blame] | 827 | tf_cuda_version = reformat_version_sequence(str(tf_cuda_version), 2) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 828 | |
| 829 | # Find out where the CUDA toolkit is installed |
| 830 | default_cuda_path = _DEFAULT_CUDA_PATH |
Martin Wicke | d57572e | 2017-09-02 19:21:45 -0700 | [diff] [blame] | 831 | if is_windows() or is_cygwin(): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 832 | default_cuda_path = cygpath( |
| 833 | environ_cp.get('CUDA_PATH', _DEFAULT_CUDA_PATH_WIN)) |
| 834 | elif is_linux(): |
| 835 | # If the default doesn't exist, try an alternative default. |
| 836 | if (not os.path.exists(default_cuda_path) |
| 837 | ) and os.path.exists(_DEFAULT_CUDA_PATH_LINUX): |
| 838 | default_cuda_path = _DEFAULT_CUDA_PATH_LINUX |
| 839 | ask_cuda_path = ('Please specify the location where CUDA %s toolkit is' |
| 840 | ' installed. Refer to README.md for more details. ' |
| 841 | '[Default is %s]: ') % (tf_cuda_version, default_cuda_path) |
| 842 | cuda_toolkit_path = get_from_env_or_user_or_default( |
| 843 | environ_cp, 'CUDA_TOOLKIT_PATH', ask_cuda_path, default_cuda_path) |
A. Unique TensorFlower | 02f17fe | 2018-07-07 06:59:19 -0700 | [diff] [blame] | 844 | if is_windows() or is_cygwin(): |
| 845 | cuda_toolkit_path = cygpath(cuda_toolkit_path) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 846 | |
| 847 | if is_windows(): |
Niall Moran | b7d97e8 | 2018-08-09 00:29:49 +0100 | [diff] [blame] | 848 | cuda_rt_lib_paths = ['lib/x64/cudart.lib'] |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 849 | elif is_linux(): |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 850 | cuda_rt_lib_paths = [ |
| 851 | '%s/libcudart.so.%s' % (x, tf_cuda_version) for x in [ |
| 852 | 'lib64', |
| 853 | 'lib/powerpc64le-linux-gnu', |
| 854 | 'lib/x86_64-linux-gnu', |
| 855 | ] |
| 856 | ] |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 857 | elif is_macos(): |
Niall Moran | b7d97e8 | 2018-08-09 00:29:49 +0100 | [diff] [blame] | 858 | cuda_rt_lib_paths = ['lib/libcudart.%s.dylib' % tf_cuda_version] |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 859 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 860 | cuda_toolkit_paths_full = [ |
| 861 | os.path.join(cuda_toolkit_path, x) for x in cuda_rt_lib_paths |
| 862 | ] |
Niall Moran | b7d97e8 | 2018-08-09 00:29:49 +0100 | [diff] [blame] | 863 | if any([os.path.exists(x) for x in cuda_toolkit_paths_full]): |
Yifei Feng | 5198cb8 | 2018-08-17 13:53:06 -0700 | [diff] [blame] | 864 | break |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 865 | |
| 866 | # Reset and retry |
| 867 | print('Invalid path to CUDA %s toolkit. %s cannot be found' % |
hellcom | 9a13fc3 | 2018-09-12 10:58:24 +0300 | [diff] [blame] | 868 | (tf_cuda_version, cuda_toolkit_paths_full)) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 869 | environ_cp['TF_CUDA_VERSION'] = '' |
| 870 | environ_cp['CUDA_TOOLKIT_PATH'] = '' |
| 871 | |
Austin Anderson | f9a88f8 | 2017-12-13 11:49:40 -0800 | [diff] [blame] | 872 | else: |
| 873 | raise UserInputError('Invalid TF_CUDA_SETTING setting was provided %d ' |
| 874 | 'times in a row. Assuming to be a scripting mistake.' % |
| 875 | _DEFAULT_PROMPT_ASK_ATTEMPTS) |
| 876 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 877 | # Set CUDA_TOOLKIT_PATH and TF_CUDA_VERSION |
| 878 | environ_cp['CUDA_TOOLKIT_PATH'] = cuda_toolkit_path |
| 879 | write_action_env_to_bazelrc('CUDA_TOOLKIT_PATH', cuda_toolkit_path) |
| 880 | environ_cp['TF_CUDA_VERSION'] = tf_cuda_version |
| 881 | write_action_env_to_bazelrc('TF_CUDA_VERSION', tf_cuda_version) |
| 882 | |
| 883 | |
Yifei Feng | b1d8c59 | 2017-11-22 13:42:21 -0800 | [diff] [blame] | 884 | def set_tf_cudnn_version(environ_cp): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 885 | """Set CUDNN_INSTALL_PATH and TF_CUDNN_VERSION.""" |
| 886 | ask_cudnn_version = ( |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 887 | 'Please specify the cuDNN version you want to use. ' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 888 | '[Leave empty to default to cuDNN %s.0]: ') % _DEFAULT_CUDNN_VERSION |
| 889 | |
Austin Anderson | f9a88f8 | 2017-12-13 11:49:40 -0800 | [diff] [blame] | 890 | for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 891 | tf_cudnn_version = get_from_env_or_user_or_default( |
| 892 | environ_cp, 'TF_CUDNN_VERSION', ask_cudnn_version, |
| 893 | _DEFAULT_CUDNN_VERSION) |
Ankur Taly | 0e6f39d | 2018-02-16 18:22:55 -0800 | [diff] [blame] | 894 | tf_cudnn_version = reformat_version_sequence(str(tf_cudnn_version), 1) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 895 | |
| 896 | default_cudnn_path = environ_cp.get('CUDA_TOOLKIT_PATH') |
| 897 | ask_cudnn_path = (r'Please specify the location where cuDNN %s library is ' |
| 898 | 'installed. Refer to README.md for more details. [Default' |
A. Unique TensorFlower | 1b21235 | 2018-07-19 13:48:50 -0700 | [diff] [blame] | 899 | ' is %s]: ') % (tf_cudnn_version, default_cudnn_path) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 900 | cudnn_install_path = get_from_env_or_user_or_default( |
| 901 | environ_cp, 'CUDNN_INSTALL_PATH', ask_cudnn_path, default_cudnn_path) |
| 902 | |
| 903 | # Result returned from "read" will be used unexpanded. That make "~" |
| 904 | # unusable. Going through one more level of expansion to handle that. |
| 905 | cudnn_install_path = os.path.realpath( |
| 906 | os.path.expanduser(cudnn_install_path)) |
Martin Wicke | d57572e | 2017-09-02 19:21:45 -0700 | [diff] [blame] | 907 | if is_windows() or is_cygwin(): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 908 | cudnn_install_path = cygpath(cudnn_install_path) |
| 909 | |
| 910 | if is_windows(): |
| 911 | cuda_dnn_lib_path = 'lib/x64/cudnn.lib' |
| 912 | cuda_dnn_lib_alt_path = 'lib/x64/cudnn.lib' |
| 913 | elif is_linux(): |
| 914 | cuda_dnn_lib_path = 'lib64/libcudnn.so.%s' % tf_cudnn_version |
| 915 | cuda_dnn_lib_alt_path = 'libcudnn.so.%s' % tf_cudnn_version |
| 916 | elif is_macos(): |
| 917 | cuda_dnn_lib_path = 'lib/libcudnn.%s.dylib' % tf_cudnn_version |
| 918 | cuda_dnn_lib_alt_path = 'libcudnn.%s.dylib' % tf_cudnn_version |
| 919 | |
| 920 | cuda_dnn_lib_path_full = os.path.join(cudnn_install_path, cuda_dnn_lib_path) |
| 921 | cuda_dnn_lib_alt_path_full = os.path.join(cudnn_install_path, |
| 922 | cuda_dnn_lib_alt_path) |
| 923 | if os.path.exists(cuda_dnn_lib_path_full) or os.path.exists( |
| 924 | cuda_dnn_lib_alt_path_full): |
| 925 | break |
| 926 | |
| 927 | # Try another alternative for Linux |
| 928 | if is_linux(): |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 929 | ldconfig_bin = which('ldconfig') or '/sbin/ldconfig' |
| 930 | cudnn_path_from_ldconfig = run_shell([ldconfig_bin, '-p']) |
| 931 | cudnn_path_from_ldconfig = re.search('.*libcudnn.so .* => (.*)', |
A. Unique TensorFlower | e722358 | 2017-09-06 17:57:04 -0700 | [diff] [blame] | 932 | cudnn_path_from_ldconfig) |
| 933 | if cudnn_path_from_ldconfig: |
| 934 | cudnn_path_from_ldconfig = cudnn_path_from_ldconfig.group(1) |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 935 | if os.path.exists( |
| 936 | '%s.%s' % (cudnn_path_from_ldconfig, tf_cudnn_version)): |
A. Unique TensorFlower | e722358 | 2017-09-06 17:57:04 -0700 | [diff] [blame] | 937 | cudnn_install_path = os.path.dirname(cudnn_path_from_ldconfig) |
| 938 | break |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 939 | |
| 940 | # Reset and Retry |
| 941 | print( |
| 942 | 'Invalid path to cuDNN %s toolkit. None of the following files can be ' |
| 943 | 'found:' % tf_cudnn_version) |
| 944 | print(cuda_dnn_lib_path_full) |
| 945 | print(cuda_dnn_lib_alt_path_full) |
| 946 | if is_linux(): |
| 947 | print('%s.%s' % (cudnn_path_from_ldconfig, tf_cudnn_version)) |
| 948 | |
| 949 | environ_cp['TF_CUDNN_VERSION'] = '' |
Austin Anderson | f9a88f8 | 2017-12-13 11:49:40 -0800 | [diff] [blame] | 950 | else: |
| 951 | raise UserInputError('Invalid TF_CUDNN setting was provided %d ' |
| 952 | 'times in a row. Assuming to be a scripting mistake.' % |
| 953 | _DEFAULT_PROMPT_ASK_ATTEMPTS) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 954 | |
| 955 | # Set CUDNN_INSTALL_PATH and TF_CUDNN_VERSION |
| 956 | environ_cp['CUDNN_INSTALL_PATH'] = cudnn_install_path |
| 957 | write_action_env_to_bazelrc('CUDNN_INSTALL_PATH', cudnn_install_path) |
| 958 | environ_cp['TF_CUDNN_VERSION'] = tf_cudnn_version |
| 959 | write_action_env_to_bazelrc('TF_CUDNN_VERSION', tf_cudnn_version) |
| 960 | |
| 961 | |
Mingxing Tan | 1e7b0e4 | 2018-06-28 19:13:20 -0700 | [diff] [blame] | 962 | def is_cuda_compatible(lib, cuda_ver, cudnn_ver): |
| 963 | """Check compatibility between given library and cudnn/cudart libraries.""" |
| 964 | ldd_bin = which('ldd') or '/usr/bin/ldd' |
| 965 | ldd_out = run_shell([ldd_bin, lib], True) |
| 966 | ldd_out = ldd_out.split(os.linesep) |
| 967 | cudnn_pattern = re.compile('.*libcudnn.so\\.?(.*) =>.*$') |
| 968 | cuda_pattern = re.compile('.*libcudart.so\\.?(.*) =>.*$') |
| 969 | cudnn = None |
| 970 | cudart = None |
| 971 | cudnn_ok = True # assume no cudnn dependency by default |
| 972 | cuda_ok = True # assume no cuda dependency by default |
| 973 | for line in ldd_out: |
| 974 | if 'libcudnn.so' in line: |
| 975 | cudnn = cudnn_pattern.search(line) |
| 976 | cudnn_ok = False |
| 977 | elif 'libcudart.so' in line: |
| 978 | cudart = cuda_pattern.search(line) |
| 979 | cuda_ok = False |
| 980 | if cudnn and len(cudnn.group(1)): |
| 981 | cudnn = convert_version_to_int(cudnn.group(1)) |
| 982 | if cudart and len(cudart.group(1)): |
| 983 | cudart = convert_version_to_int(cudart.group(1)) |
| 984 | if cudnn is not None: |
| 985 | cudnn_ok = (cudnn == cudnn_ver) |
| 986 | if cudart is not None: |
| 987 | cuda_ok = (cudart == cuda_ver) |
| 988 | return cudnn_ok and cuda_ok |
| 989 | |
| 990 | |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 991 | def set_tf_tensorrt_install_path(environ_cp): |
| 992 | """Set TENSORRT_INSTALL_PATH and TF_TENSORRT_VERSION. |
| 993 | |
| 994 | Adapted from code contributed by Sami Kama (https://github.com/samikama). |
| 995 | |
| 996 | Args: |
| 997 | environ_cp: copy of the os.environ. |
| 998 | |
| 999 | Raises: |
| 1000 | ValueError: if this method was called under non-Linux platform. |
| 1001 | UserInputError: if user has provided invalid input multiple times. |
| 1002 | """ |
| 1003 | if not is_linux(): |
| 1004 | raise ValueError('Currently TensorRT is only supported on Linux platform.') |
| 1005 | |
| 1006 | # Ask user whether to add TensorRT support. |
Mingxing Tan | 1e7b0e4 | 2018-06-28 19:13:20 -0700 | [diff] [blame] | 1007 | if str(int(get_var(environ_cp, 'TF_NEED_TENSORRT', 'TensorRT', |
| 1008 | False))) != '1': |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1009 | return |
| 1010 | |
| 1011 | for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS): |
| 1012 | ask_tensorrt_path = (r'Please specify the location where TensorRT is ' |
| 1013 | 'installed. [Default is %s]:') % ( |
| 1014 | _DEFAULT_TENSORRT_PATH_LINUX) |
| 1015 | trt_install_path = get_from_env_or_user_or_default( |
| 1016 | environ_cp, 'TENSORRT_INSTALL_PATH', ask_tensorrt_path, |
| 1017 | _DEFAULT_TENSORRT_PATH_LINUX) |
| 1018 | |
| 1019 | # Result returned from "read" will be used unexpanded. That make "~" |
| 1020 | # unusable. Going through one more level of expansion to handle that. |
Mingxing Tan | 1e7b0e4 | 2018-06-28 19:13:20 -0700 | [diff] [blame] | 1021 | trt_install_path = os.path.realpath(os.path.expanduser(trt_install_path)) |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1022 | |
| 1023 | def find_libs(search_path): |
| 1024 | """Search for libnvinfer.so in "search_path".""" |
| 1025 | fl = set() |
| 1026 | if os.path.exists(search_path) and os.path.isdir(search_path): |
Mingxing Tan | 1e7b0e4 | 2018-06-28 19:13:20 -0700 | [diff] [blame] | 1027 | fl.update([ |
| 1028 | os.path.realpath(os.path.join(search_path, x)) |
| 1029 | for x in os.listdir(search_path) |
| 1030 | if 'libnvinfer.so' in x |
| 1031 | ]) |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1032 | return fl |
| 1033 | |
| 1034 | possible_files = find_libs(trt_install_path) |
| 1035 | possible_files.update(find_libs(os.path.join(trt_install_path, 'lib'))) |
| 1036 | possible_files.update(find_libs(os.path.join(trt_install_path, 'lib64'))) |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1037 | cuda_ver = convert_version_to_int(environ_cp['TF_CUDA_VERSION']) |
| 1038 | cudnn_ver = convert_version_to_int(environ_cp['TF_CUDNN_VERSION']) |
| 1039 | nvinfer_pattern = re.compile('.*libnvinfer.so.?(.*)$') |
| 1040 | highest_ver = [0, None, None] |
| 1041 | |
| 1042 | for lib_file in possible_files: |
Mingxing Tan | 1e7b0e4 | 2018-06-28 19:13:20 -0700 | [diff] [blame] | 1043 | if is_cuda_compatible(lib_file, cuda_ver, cudnn_ver): |
Jacques Pienaar | 2d0531d | 2018-03-21 12:07:51 -0700 | [diff] [blame] | 1044 | matches = nvinfer_pattern.search(lib_file) |
| 1045 | if len(matches.groups()) == 0: |
| 1046 | continue |
| 1047 | ver_str = matches.group(1) |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1048 | ver = convert_version_to_int(ver_str) if len(ver_str) else 0 |
| 1049 | if ver > highest_ver[0]: |
| 1050 | highest_ver = [ver, ver_str, lib_file] |
| 1051 | if highest_ver[1] is not None: |
| 1052 | trt_install_path = os.path.dirname(highest_ver[2]) |
| 1053 | tf_tensorrt_version = highest_ver[1] |
| 1054 | break |
| 1055 | |
| 1056 | # Try another alternative from ldconfig. |
| 1057 | ldconfig_bin = which('ldconfig') or '/sbin/ldconfig' |
| 1058 | ldconfig_output = run_shell([ldconfig_bin, '-p']) |
Mingxing Tan | 1e7b0e4 | 2018-06-28 19:13:20 -0700 | [diff] [blame] | 1059 | search_result = re.search('.*libnvinfer.so\\.?([0-9.]*).* => (.*)', |
| 1060 | ldconfig_output) |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1061 | if search_result: |
| 1062 | libnvinfer_path_from_ldconfig = search_result.group(2) |
| 1063 | if os.path.exists(libnvinfer_path_from_ldconfig): |
Mingxing Tan | 1e7b0e4 | 2018-06-28 19:13:20 -0700 | [diff] [blame] | 1064 | if is_cuda_compatible(libnvinfer_path_from_ldconfig, cuda_ver, |
| 1065 | cudnn_ver): |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1066 | trt_install_path = os.path.dirname(libnvinfer_path_from_ldconfig) |
| 1067 | tf_tensorrt_version = search_result.group(1) |
| 1068 | break |
| 1069 | |
| 1070 | # Reset and Retry |
Yifei Feng | dce9a49 | 2018-02-22 14:24:57 -0800 | [diff] [blame] | 1071 | if possible_files: |
| 1072 | print('TensorRT libraries found in one the following directories', |
| 1073 | 'are not compatible with selected cuda and cudnn installations') |
| 1074 | print(trt_install_path) |
| 1075 | print(os.path.join(trt_install_path, 'lib')) |
| 1076 | print(os.path.join(trt_install_path, 'lib64')) |
| 1077 | if search_result: |
| 1078 | print(libnvinfer_path_from_ldconfig) |
| 1079 | else: |
| 1080 | print( |
| 1081 | 'Invalid path to TensorRT. None of the following files can be found:') |
| 1082 | print(trt_install_path) |
| 1083 | print(os.path.join(trt_install_path, 'lib')) |
| 1084 | print(os.path.join(trt_install_path, 'lib64')) |
| 1085 | if search_result: |
| 1086 | print(libnvinfer_path_from_ldconfig) |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1087 | |
| 1088 | else: |
| 1089 | raise UserInputError('Invalid TF_TENSORRT setting was provided %d ' |
| 1090 | 'times in a row. Assuming to be a scripting mistake.' % |
| 1091 | _DEFAULT_PROMPT_ASK_ATTEMPTS) |
| 1092 | |
| 1093 | # Set TENSORRT_INSTALL_PATH and TF_TENSORRT_VERSION |
| 1094 | environ_cp['TENSORRT_INSTALL_PATH'] = trt_install_path |
| 1095 | write_action_env_to_bazelrc('TENSORRT_INSTALL_PATH', trt_install_path) |
| 1096 | environ_cp['TF_TENSORRT_VERSION'] = tf_tensorrt_version |
| 1097 | write_action_env_to_bazelrc('TF_TENSORRT_VERSION', tf_tensorrt_version) |
| 1098 | |
| 1099 | |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1100 | def set_tf_nccl_install_path(environ_cp): |
| 1101 | """Set NCCL_INSTALL_PATH and TF_NCCL_VERSION. |
| 1102 | |
| 1103 | Args: |
| 1104 | environ_cp: copy of the os.environ. |
| 1105 | |
| 1106 | Raises: |
| 1107 | ValueError: if this method was called under non-Linux platform. |
| 1108 | UserInputError: if user has provided invalid input multiple times. |
| 1109 | """ |
| 1110 | if not is_linux(): |
| 1111 | raise ValueError('Currently NCCL is only supported on Linux platforms.') |
| 1112 | |
| 1113 | ask_nccl_version = ( |
Smit Hinsu | 63e6b9b | 2018-07-13 12:46:24 -0700 | [diff] [blame] | 1114 | 'Please specify the NCCL version you want to use. If NCCL %s is not ' |
| 1115 | 'installed, then you can use version 1.3 that can be fetched ' |
| 1116 | 'automatically but it may have worse performance with multiple GPUs. ' |
| 1117 | '[Default is %s]: ') % (_DEFAULT_NCCL_VERSION, _DEFAULT_NCCL_VERSION) |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1118 | |
| 1119 | for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS): |
| 1120 | tf_nccl_version = get_from_env_or_user_or_default( |
| 1121 | environ_cp, 'TF_NCCL_VERSION', ask_nccl_version, _DEFAULT_NCCL_VERSION) |
| 1122 | tf_nccl_version = reformat_version_sequence(str(tf_nccl_version), 1) |
| 1123 | |
| 1124 | if tf_nccl_version == '1': |
| 1125 | break # No need to get install path, NCCL 1 is a GitHub repo. |
| 1126 | |
| 1127 | # TODO(csigg): Look with ldconfig first if we can find the library in paths |
| 1128 | # like /usr/lib/x86_64-linux-gnu and the header file in the corresponding |
| 1129 | # include directory. This is where the NCCL .deb packages install them. |
| 1130 | # Then ask the user if we should use that. Instead of a single |
| 1131 | # NCCL_INSTALL_PATH, pass separate NCCL_LIB_PATH and NCCL_HDR_PATH to |
| 1132 | # nccl_configure.bzl |
| 1133 | default_nccl_path = environ_cp.get('CUDA_TOOLKIT_PATH') |
| 1134 | ask_nccl_path = (r'Please specify the location where NCCL %s library is ' |
| 1135 | 'installed. Refer to README.md for more details. [Default ' |
| 1136 | 'is %s]:') % (tf_nccl_version, default_nccl_path) |
| 1137 | nccl_install_path = get_from_env_or_user_or_default( |
| 1138 | environ_cp, 'NCCL_INSTALL_PATH', ask_nccl_path, default_nccl_path) |
| 1139 | |
| 1140 | # Result returned from "read" will be used unexpanded. That make "~" |
| 1141 | # unusable. Going through one more level of expansion to handle that. |
| 1142 | nccl_install_path = os.path.realpath(os.path.expanduser(nccl_install_path)) |
| 1143 | if is_windows() or is_cygwin(): |
| 1144 | nccl_install_path = cygpath(nccl_install_path) |
| 1145 | |
| 1146 | if is_windows(): |
| 1147 | nccl_lib_path = 'lib/x64/nccl.lib' |
| 1148 | elif is_linux(): |
| 1149 | nccl_lib_path = 'lib/libnccl.so.%s' % tf_nccl_version |
| 1150 | elif is_macos(): |
| 1151 | nccl_lib_path = 'lib/libnccl.%s.dylib' % tf_nccl_version |
| 1152 | |
| 1153 | nccl_lib_path = os.path.join(nccl_install_path, nccl_lib_path) |
| 1154 | nccl_hdr_path = os.path.join(nccl_install_path, 'include/nccl.h') |
| 1155 | if os.path.exists(nccl_lib_path) and os.path.exists(nccl_hdr_path): |
| 1156 | # Set NCCL_INSTALL_PATH |
| 1157 | environ_cp['NCCL_INSTALL_PATH'] = nccl_install_path |
| 1158 | write_action_env_to_bazelrc('NCCL_INSTALL_PATH', nccl_install_path) |
| 1159 | break |
| 1160 | |
| 1161 | # Reset and Retry |
| 1162 | print('Invalid path to NCCL %s toolkit, %s or %s not found. Please use the ' |
| 1163 | 'O/S agnostic package of NCCL 2' % (tf_nccl_version, nccl_lib_path, |
| 1164 | nccl_hdr_path)) |
| 1165 | |
| 1166 | environ_cp['TF_NCCL_VERSION'] = '' |
| 1167 | else: |
| 1168 | raise UserInputError('Invalid TF_NCCL setting was provided %d ' |
| 1169 | 'times in a row. Assuming to be a scripting mistake.' % |
| 1170 | _DEFAULT_PROMPT_ASK_ATTEMPTS) |
| 1171 | |
| 1172 | # Set TF_NCCL_VERSION |
| 1173 | environ_cp['TF_NCCL_VERSION'] = tf_nccl_version |
| 1174 | write_action_env_to_bazelrc('TF_NCCL_VERSION', tf_nccl_version) |
| 1175 | |
| 1176 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1177 | def get_native_cuda_compute_capabilities(environ_cp): |
| 1178 | """Get native cuda compute capabilities. |
| 1179 | |
| 1180 | Args: |
| 1181 | environ_cp: copy of the os.environ. |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 1182 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1183 | Returns: |
| 1184 | string of native cuda compute capabilities, separated by comma. |
| 1185 | """ |
| 1186 | device_query_bin = os.path.join( |
| 1187 | environ_cp.get('CUDA_TOOLKIT_PATH'), 'extras/demo_suite/deviceQuery') |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 1188 | if os.path.isfile(device_query_bin) and os.access(device_query_bin, os.X_OK): |
| 1189 | try: |
| 1190 | output = run_shell(device_query_bin).split('\n') |
| 1191 | pattern = re.compile('[0-9]*\\.[0-9]*') |
| 1192 | output = [pattern.search(x) for x in output if 'Capability' in x] |
| 1193 | output = ','.join(x.group() for x in output if x is not None) |
| 1194 | except subprocess.CalledProcessError: |
| 1195 | output = '' |
| 1196 | else: |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1197 | output = '' |
| 1198 | return output |
| 1199 | |
| 1200 | |
| 1201 | def set_tf_cuda_compute_capabilities(environ_cp): |
| 1202 | """Set TF_CUDA_COMPUTE_CAPABILITIES.""" |
| 1203 | while True: |
| 1204 | native_cuda_compute_capabilities = get_native_cuda_compute_capabilities( |
| 1205 | environ_cp) |
| 1206 | if not native_cuda_compute_capabilities: |
| 1207 | default_cuda_compute_capabilities = _DEFAULT_CUDA_COMPUTE_CAPABILITIES |
| 1208 | else: |
| 1209 | default_cuda_compute_capabilities = native_cuda_compute_capabilities |
| 1210 | |
| 1211 | ask_cuda_compute_capabilities = ( |
| 1212 | 'Please specify a list of comma-separated ' |
| 1213 | 'Cuda compute capabilities you want to ' |
| 1214 | 'build with.\nYou can find the compute ' |
| 1215 | 'capability of your device at: ' |
| 1216 | 'https://developer.nvidia.com/cuda-gpus.\nPlease' |
| 1217 | ' note that each additional compute ' |
| 1218 | 'capability significantly increases your ' |
A. Unique TensorFlower | 1b21235 | 2018-07-19 13:48:50 -0700 | [diff] [blame] | 1219 | 'build time and binary size. [Default is: %s]: ' % |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1220 | default_cuda_compute_capabilities) |
| 1221 | tf_cuda_compute_capabilities = get_from_env_or_user_or_default( |
| 1222 | environ_cp, 'TF_CUDA_COMPUTE_CAPABILITIES', |
| 1223 | ask_cuda_compute_capabilities, default_cuda_compute_capabilities) |
| 1224 | # Check whether all capabilities from the input is valid |
| 1225 | all_valid = True |
Maciej | d0f5bc1 | 2018-04-30 22:30:58 -0500 | [diff] [blame] | 1226 | # Remove all whitespace characters before splitting the string |
Michael Case | 5105350 | 2018-06-05 17:47:19 -0700 | [diff] [blame] | 1227 | # that users may insert by accident, as this will result in error |
Maciej | d0f5bc1 | 2018-04-30 22:30:58 -0500 | [diff] [blame] | 1228 | tf_cuda_compute_capabilities = ''.join(tf_cuda_compute_capabilities.split()) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1229 | for compute_capability in tf_cuda_compute_capabilities.split(','): |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 1230 | m = re.match('[0-9]+.[0-9]+', compute_capability) |
| 1231 | if not m: |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1232 | print('Invalid compute capability: ' % compute_capability) |
| 1233 | all_valid = False |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 1234 | else: |
| 1235 | ver = int(m.group(0).split('.')[0]) |
| 1236 | if ver < 3: |
| 1237 | print('Only compute capabilities 3.0 or higher are supported.') |
| 1238 | all_valid = False |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1239 | |
| 1240 | if all_valid: |
| 1241 | break |
| 1242 | |
| 1243 | # Reset and Retry |
| 1244 | environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = '' |
| 1245 | |
| 1246 | # Set TF_CUDA_COMPUTE_CAPABILITIES |
| 1247 | environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = tf_cuda_compute_capabilities |
| 1248 | write_action_env_to_bazelrc('TF_CUDA_COMPUTE_CAPABILITIES', |
| 1249 | tf_cuda_compute_capabilities) |
| 1250 | |
| 1251 | |
| 1252 | def set_other_cuda_vars(environ_cp): |
| 1253 | """Set other CUDA related variables.""" |
A. Unique TensorFlower | ab39198 | 2018-07-11 04:52:49 -0700 | [diff] [blame] | 1254 | # If CUDA is enabled, always use GPU during build and test. |
| 1255 | if environ_cp.get('TF_CUDA_CLANG') == '1': |
| 1256 | write_to_bazelrc('build --config=cuda_clang') |
| 1257 | write_to_bazelrc('test --config=cuda_clang') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1258 | else: |
A. Unique TensorFlower | ab39198 | 2018-07-11 04:52:49 -0700 | [diff] [blame] | 1259 | write_to_bazelrc('build --config=cuda') |
| 1260 | write_to_bazelrc('test --config=cuda') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1261 | |
| 1262 | |
| 1263 | def set_host_cxx_compiler(environ_cp): |
| 1264 | """Set HOST_CXX_COMPILER.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 1265 | default_cxx_host_compiler = which('g++') or '' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1266 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1267 | host_cxx_compiler = prompt_loop_or_load_from_env( |
| 1268 | environ_cp, |
| 1269 | var_name='HOST_CXX_COMPILER', |
| 1270 | var_default=default_cxx_host_compiler, |
| 1271 | ask_for_var=('Please specify which C++ compiler should be used as the ' |
| 1272 | 'host C++ compiler.'), |
| 1273 | check_success=os.path.exists, |
| 1274 | error_msg='Invalid C++ compiler path. %s cannot be found.', |
| 1275 | ) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1276 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1277 | write_action_env_to_bazelrc('HOST_CXX_COMPILER', host_cxx_compiler) |
| 1278 | |
| 1279 | |
| 1280 | def set_host_c_compiler(environ_cp): |
| 1281 | """Set HOST_C_COMPILER.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 1282 | default_c_host_compiler = which('gcc') or '' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1283 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1284 | host_c_compiler = prompt_loop_or_load_from_env( |
| 1285 | environ_cp, |
| 1286 | var_name='HOST_C_COMPILER', |
| 1287 | var_default=default_c_host_compiler, |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 1288 | ask_for_var=('Please specify which C compiler should be used as the host ' |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1289 | 'C compiler.'), |
| 1290 | check_success=os.path.exists, |
| 1291 | error_msg='Invalid C compiler path. %s cannot be found.', |
| 1292 | ) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1293 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1294 | write_action_env_to_bazelrc('HOST_C_COMPILER', host_c_compiler) |
| 1295 | |
| 1296 | |
| 1297 | def set_computecpp_toolkit_path(environ_cp): |
| 1298 | """Set COMPUTECPP_TOOLKIT_PATH.""" |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1299 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1300 | def toolkit_exists(toolkit_path): |
| 1301 | """Check if a computecpp toolkit path is valid.""" |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1302 | if is_linux(): |
| 1303 | sycl_rt_lib_path = 'lib/libComputeCpp.so' |
| 1304 | else: |
| 1305 | sycl_rt_lib_path = '' |
| 1306 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 1307 | sycl_rt_lib_path_full = os.path.join(toolkit_path, sycl_rt_lib_path) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1308 | exists = os.path.exists(sycl_rt_lib_path_full) |
| 1309 | if not exists: |
| 1310 | print('Invalid SYCL %s library path. %s cannot be found' % |
| 1311 | (_TF_OPENCL_VERSION, sycl_rt_lib_path_full)) |
| 1312 | return exists |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1313 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1314 | computecpp_toolkit_path = prompt_loop_or_load_from_env( |
| 1315 | environ_cp, |
| 1316 | var_name='COMPUTECPP_TOOLKIT_PATH', |
| 1317 | var_default=_DEFAULT_COMPUTECPP_TOOLKIT_PATH, |
| 1318 | ask_for_var=( |
| 1319 | 'Please specify the location where ComputeCpp for SYCL %s is ' |
| 1320 | 'installed.' % _TF_OPENCL_VERSION), |
| 1321 | check_success=toolkit_exists, |
| 1322 | error_msg='Invalid SYCL compiler path. %s cannot be found.', |
| 1323 | suppress_default_error=True) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1324 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1325 | write_action_env_to_bazelrc('COMPUTECPP_TOOLKIT_PATH', |
| 1326 | computecpp_toolkit_path) |
| 1327 | |
Michael Case | d31531a | 2018-01-05 14:09:41 -0800 | [diff] [blame] | 1328 | |
Dandelion Man? | 90e42f3 | 2017-12-15 18:15:07 -0800 | [diff] [blame] | 1329 | def set_trisycl_include_dir(environ_cp): |
Michael Case | d31531a | 2018-01-05 14:09:41 -0800 | [diff] [blame] | 1330 | """Set TRISYCL_INCLUDE_DIR.""" |
Frank Chen | c4ef927 | 2018-01-10 11:36:52 -0800 | [diff] [blame] | 1331 | |
Dandelion Man? | 90e42f3 | 2017-12-15 18:15:07 -0800 | [diff] [blame] | 1332 | ask_trisycl_include_dir = ('Please specify the location of the triSYCL ' |
| 1333 | 'include directory. (Use --config=sycl_trisycl ' |
| 1334 | 'when building with Bazel) ' |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 1335 | '[Default is %s]: ') % ( |
| 1336 | _DEFAULT_TRISYCL_INCLUDE_DIR) |
Frank Chen | c4ef927 | 2018-01-10 11:36:52 -0800 | [diff] [blame] | 1337 | |
Dandelion Man? | 90e42f3 | 2017-12-15 18:15:07 -0800 | [diff] [blame] | 1338 | while True: |
| 1339 | trisycl_include_dir = get_from_env_or_user_or_default( |
Michael Case | d31531a | 2018-01-05 14:09:41 -0800 | [diff] [blame] | 1340 | environ_cp, 'TRISYCL_INCLUDE_DIR', ask_trisycl_include_dir, |
| 1341 | _DEFAULT_TRISYCL_INCLUDE_DIR) |
Dandelion Man? | 90e42f3 | 2017-12-15 18:15:07 -0800 | [diff] [blame] | 1342 | if os.path.exists(trisycl_include_dir): |
| 1343 | break |
| 1344 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 1345 | print('Invalid triSYCL include directory, %s cannot be found' % |
| 1346 | (trisycl_include_dir)) |
Dandelion Man? | 90e42f3 | 2017-12-15 18:15:07 -0800 | [diff] [blame] | 1347 | |
| 1348 | # Set TRISYCL_INCLUDE_DIR |
| 1349 | environ_cp['TRISYCL_INCLUDE_DIR'] = trisycl_include_dir |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 1350 | write_action_env_to_bazelrc('TRISYCL_INCLUDE_DIR', trisycl_include_dir) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1351 | |
Yifei Feng | b1d8c59 | 2017-11-22 13:42:21 -0800 | [diff] [blame] | 1352 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1353 | def set_mpi_home(environ_cp): |
| 1354 | """Set MPI_HOME.""" |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1355 | |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 1356 | default_mpi_home = which('mpirun') or which('mpiexec') or '' |
| 1357 | default_mpi_home = os.path.dirname(os.path.dirname(default_mpi_home)) |
| 1358 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1359 | def valid_mpi_path(mpi_home): |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 1360 | exists = ( |
| 1361 | os.path.exists(os.path.join(mpi_home, 'include')) and |
| 1362 | os.path.exists(os.path.join(mpi_home, 'lib'))) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1363 | if not exists: |
| 1364 | print('Invalid path to the MPI Toolkit. %s or %s cannot be found' % |
| 1365 | (os.path.join(mpi_home, 'include'), |
| 1366 | os.path.exists(os.path.join(mpi_home, 'lib')))) |
| 1367 | return exists |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1368 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1369 | _ = prompt_loop_or_load_from_env( |
| 1370 | environ_cp, |
| 1371 | var_name='MPI_HOME', |
| 1372 | var_default=default_mpi_home, |
| 1373 | ask_for_var='Please specify the MPI toolkit folder.', |
| 1374 | check_success=valid_mpi_path, |
| 1375 | error_msg='', |
| 1376 | suppress_default_error=True) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1377 | |
| 1378 | |
| 1379 | def set_other_mpi_vars(environ_cp): |
| 1380 | """Set other MPI related variables.""" |
| 1381 | # Link the MPI header files |
| 1382 | mpi_home = environ_cp.get('MPI_HOME') |
| 1383 | symlink_force('%s/include/mpi.h' % mpi_home, 'third_party/mpi/mpi.h') |
| 1384 | |
| 1385 | # Determine if we use OpenMPI or MVAPICH, these require different header files |
| 1386 | # to be included here to make bazel dependency checker happy |
| 1387 | if os.path.exists(os.path.join(mpi_home, 'include/mpi_portable_platform.h')): |
| 1388 | symlink_force( |
| 1389 | os.path.join(mpi_home, 'include/mpi_portable_platform.h'), |
| 1390 | 'third_party/mpi/mpi_portable_platform.h') |
| 1391 | # TODO(gunan): avoid editing files in configure |
| 1392 | sed_in_place('third_party/mpi/mpi.bzl', 'MPI_LIB_IS_OPENMPI=False', |
| 1393 | 'MPI_LIB_IS_OPENMPI=True') |
| 1394 | else: |
| 1395 | # MVAPICH / MPICH |
| 1396 | symlink_force( |
| 1397 | os.path.join(mpi_home, 'include/mpio.h'), 'third_party/mpi/mpio.h') |
| 1398 | symlink_force( |
| 1399 | os.path.join(mpi_home, 'include/mpicxx.h'), 'third_party/mpi/mpicxx.h') |
| 1400 | # TODO(gunan): avoid editing files in configure |
| 1401 | sed_in_place('third_party/mpi/mpi.bzl', 'MPI_LIB_IS_OPENMPI=True', |
| 1402 | 'MPI_LIB_IS_OPENMPI=False') |
| 1403 | |
| 1404 | if os.path.exists(os.path.join(mpi_home, 'lib/libmpi.so')): |
| 1405 | symlink_force( |
| 1406 | os.path.join(mpi_home, 'lib/libmpi.so'), 'third_party/mpi/libmpi.so') |
| 1407 | else: |
| 1408 | raise ValueError('Cannot find the MPI library file in %s/lib' % mpi_home) |
| 1409 | |
| 1410 | |
A. Unique TensorFlower | 061c359 | 2017-11-13 14:21:04 -0800 | [diff] [blame] | 1411 | def set_grpc_build_flags(): |
| 1412 | write_to_bazelrc('build --define grpc_no_ares=true') |
| 1413 | |
Michael Case | d31531a | 2018-01-05 14:09:41 -0800 | [diff] [blame] | 1414 | |
Yifei Feng | 5198cb8 | 2018-08-17 13:53:06 -0700 | [diff] [blame] | 1415 | def set_system_libs_flag(environ_cp): |
| 1416 | syslibs = environ_cp.get('TF_SYSTEM_LIBS', '') |
Yifei Feng | 5198cb8 | 2018-08-17 13:53:06 -0700 | [diff] [blame] | 1417 | if syslibs and syslibs != '': |
Jason Zaman | 5fc39bd | 2018-09-16 01:38:55 +0800 | [diff] [blame] | 1418 | if ',' in syslibs: |
| 1419 | syslibs = ','.join(sorted(syslibs.split(','))) |
| 1420 | else: |
| 1421 | syslibs = ','.join(sorted(syslibs.split())) |
Yifei Feng | 5198cb8 | 2018-08-17 13:53:06 -0700 | [diff] [blame] | 1422 | write_action_env_to_bazelrc('TF_SYSTEM_LIBS', syslibs) |
| 1423 | |
Jason Zaman | 5fc39bd | 2018-09-16 01:38:55 +0800 | [diff] [blame] | 1424 | if 'PREFIX' in environ_cp: |
| 1425 | write_to_bazelrc('build --define=PREFIX=%s' % environ_cp['PREFIX']) |
| 1426 | if 'LIBDIR' in environ_cp: |
| 1427 | write_to_bazelrc('build --define=LIBDIR=%s' % environ_cp['LIBDIR']) |
| 1428 | if 'INCLUDEDIR' in environ_cp: |
| 1429 | write_to_bazelrc('build --define=INCLUDEDIR=%s' % environ_cp['INCLUDEDIR']) |
| 1430 | |
Yifei Feng | 5198cb8 | 2018-08-17 13:53:06 -0700 | [diff] [blame] | 1431 | |
A. Unique TensorFlower | 1b21235 | 2018-07-19 13:48:50 -0700 | [diff] [blame] | 1432 | def set_windows_build_flags(environ_cp): |
| 1433 | """Set Windows specific build options.""" |
| 1434 | # The non-monolithic build is not supported yet |
| 1435 | write_to_bazelrc('build --config monolithic') |
| 1436 | # Suppress warning messages |
| 1437 | write_to_bazelrc('build --copt=-w --host_copt=-w') |
| 1438 | # Output more verbose information when something goes wrong |
| 1439 | write_to_bazelrc('build --verbose_failures') |
| 1440 | # The host and target platforms are the same in Windows build. So we don't |
| 1441 | # have to distinct them. This avoids building the same targets twice. |
| 1442 | write_to_bazelrc('build --distinct_host_configuration=false') |
| 1443 | # Enable short object file path to avoid long path issue on Windows. |
| 1444 | # TODO(pcloudy): Remove this flag when upgrading Bazel to 0.16.0 |
| 1445 | # Short object file path will be enabled by default. |
| 1446 | write_to_bazelrc('build --experimental_shortened_obj_file_path=true') |
| 1447 | |
| 1448 | if get_var( |
| 1449 | environ_cp, 'TF_OVERRIDE_EIGEN_STRONG_INLINE', 'Eigen strong inline', |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 1450 | True, ('Would you like to override eigen strong inline for some C++ ' |
| 1451 | 'compilation to reduce the compilation time?'), |
| 1452 | 'Eigen strong inline overridden.', 'Not overriding eigen strong inline, ' |
A. Unique TensorFlower | 1b21235 | 2018-07-19 13:48:50 -0700 | [diff] [blame] | 1453 | 'some compilations could take more than 20 mins.'): |
| 1454 | # Due to a known MSVC compiler issue |
| 1455 | # https://github.com/tensorflow/tensorflow/issues/10521 |
| 1456 | # Overriding eigen strong inline speeds up the compiling of |
| 1457 | # conv_grad_ops_3d.cc and conv_ops_3d.cc by 20 minutes, |
| 1458 | # but this also hurts the performance. Let users decide what they want. |
| 1459 | write_to_bazelrc('build --define=override_eigen_strong_inline=true') |
Dandelion Man? | 90e42f3 | 2017-12-15 18:15:07 -0800 | [diff] [blame] | 1460 | |
A. Unique TensorFlower | 061c359 | 2017-11-13 14:21:04 -0800 | [diff] [blame] | 1461 | |
Michael Case | d31531a | 2018-01-05 14:09:41 -0800 | [diff] [blame] | 1462 | def config_info_line(name, help_text): |
| 1463 | """Helper function to print formatted help text for Bazel config options.""" |
| 1464 | print('\t--config=%-12s\t# %s' % (name, help_text)) |
| 1465 | |
| 1466 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1467 | def main(): |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 1468 | parser = argparse.ArgumentParser() |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 1469 | parser.add_argument( |
| 1470 | '--workspace', |
| 1471 | type=str, |
| 1472 | default=_TF_WORKSPACE_ROOT, |
| 1473 | help='The absolute path to your active Bazel workspace.') |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 1474 | args = parser.parse_args() |
| 1475 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1476 | # Make a copy of os.environ to be clear when functions and getting and setting |
| 1477 | # environment variables. |
| 1478 | environ_cp = dict(os.environ) |
| 1479 | |
Yifei Feng | bb38411 | 2018-07-24 13:12:54 -0700 | [diff] [blame] | 1480 | check_bazel_version('0.15.0') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1481 | |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 1482 | reset_tf_configure_bazelrc(args.workspace) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1483 | cleanup_makefile() |
Gunhan Gulsoy | ed89a2b | 2017-09-19 18:36:26 -0700 | [diff] [blame] | 1484 | setup_python(environ_cp) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1485 | |
| 1486 | if is_windows(): |
Yong Tang | a7b7aa8 | 2018-07-02 07:41:42 -0700 | [diff] [blame] | 1487 | environ_cp['TF_NEED_AWS'] = '0' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1488 | environ_cp['TF_NEED_GCP'] = '0' |
| 1489 | environ_cp['TF_NEED_HDFS'] = '0' |
| 1490 | environ_cp['TF_NEED_JEMALLOC'] = '0' |
Michael Case | d90054e | 2018-02-07 14:36:00 -0800 | [diff] [blame] | 1491 | environ_cp['TF_NEED_KAFKA'] = '0' |
Yifei Feng | b1d8c59 | 2017-11-22 13:42:21 -0800 | [diff] [blame] | 1492 | environ_cp['TF_NEED_OPENCL_SYCL'] = '0' |
| 1493 | environ_cp['TF_NEED_COMPUTECPP'] = '0' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1494 | environ_cp['TF_NEED_OPENCL'] = '0' |
| 1495 | environ_cp['TF_CUDA_CLANG'] = '0' |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1496 | environ_cp['TF_NEED_TENSORRT'] = '0' |
Ilya Biryukov | 9e651e4 | 2018-03-22 05:33:42 -0700 | [diff] [blame] | 1497 | # TODO(ibiryukov): Investigate using clang as a cpu or cuda compiler on |
| 1498 | # Windows. |
| 1499 | environ_cp['TF_DOWNLOAD_CLANG'] = '0' |
A. Unique TensorFlower | 6e97fb3 | 2018-07-16 14:07:29 -0700 | [diff] [blame] | 1500 | environ_cp['TF_ENABLE_XLA'] = '0' |
| 1501 | environ_cp['TF_NEED_GDR'] = '0' |
| 1502 | environ_cp['TF_NEED_VERBS'] = '0' |
| 1503 | environ_cp['TF_NEED_MPI'] = '0' |
| 1504 | environ_cp['TF_SET_ANDROID_WORKSPACE'] = '0' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1505 | |
| 1506 | if is_macos(): |
| 1507 | environ_cp['TF_NEED_JEMALLOC'] = '0' |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1508 | environ_cp['TF_NEED_TENSORRT'] = '0' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1509 | |
Jon Triebenbach | 6896a74 | 2018-06-27 13:29:53 -0500 | [diff] [blame] | 1510 | # The numpy package on ppc64le uses OpenBLAS which has multi-threading |
| 1511 | # issues that lead to incorrect answers. Set OMP_NUM_THREADS=1 at |
| 1512 | # runtime to allow the Tensorflow testcases which compare numpy |
| 1513 | # results to Tensorflow results to succeed. |
| 1514 | if is_ppc64le(): |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 1515 | write_action_env_to_bazelrc('OMP_NUM_THREADS', 1) |
Jon Triebenbach | 6896a74 | 2018-06-27 13:29:53 -0500 | [diff] [blame] | 1516 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1517 | set_build_var(environ_cp, 'TF_NEED_JEMALLOC', 'jemalloc as malloc', |
| 1518 | 'with_jemalloc', True) |
| 1519 | set_build_var(environ_cp, 'TF_NEED_GCP', 'Google Cloud Platform', |
Benoit Steiner | 355e25e | 2017-10-24 19:47:46 -0700 | [diff] [blame] | 1520 | 'with_gcp_support', True, 'gcp') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1521 | set_build_var(environ_cp, 'TF_NEED_HDFS', 'Hadoop File System', |
Benoit Steiner | 355e25e | 2017-10-24 19:47:46 -0700 | [diff] [blame] | 1522 | 'with_hdfs_support', True, 'hdfs') |
Yong Tang | a7b7aa8 | 2018-07-02 07:41:42 -0700 | [diff] [blame] | 1523 | set_build_var(environ_cp, 'TF_NEED_AWS', 'Amazon AWS Platform', |
| 1524 | 'with_aws_support', True, 'aws') |
Michael Case | d90054e | 2018-02-07 14:36:00 -0800 | [diff] [blame] | 1525 | set_build_var(environ_cp, 'TF_NEED_KAFKA', 'Apache Kafka Platform', |
Jianwei Xie | 63dffd5 | 2018-03-29 10:50:46 -0700 | [diff] [blame] | 1526 | 'with_kafka_support', True, 'kafka') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1527 | set_build_var(environ_cp, 'TF_ENABLE_XLA', 'XLA JIT', 'with_xla_support', |
Michael Case | 98850a5 | 2017-09-14 13:35:57 -0700 | [diff] [blame] | 1528 | False, 'xla') |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 1529 | set_build_var(environ_cp, 'TF_NEED_GDR', 'GDR', 'with_gdr_support', False, |
| 1530 | 'gdr') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1531 | set_build_var(environ_cp, 'TF_NEED_VERBS', 'VERBS', 'with_verbs_support', |
Michael Case | 98850a5 | 2017-09-14 13:35:57 -0700 | [diff] [blame] | 1532 | False, 'verbs') |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 1533 | set_build_var(environ_cp, 'TF_NEED_NGRAPH', 'nGraph', 'with_ngraph_support', |
| 1534 | False, 'ngraph') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1535 | |
Yifei Feng | b1d8c59 | 2017-11-22 13:42:21 -0800 | [diff] [blame] | 1536 | set_action_env_var(environ_cp, 'TF_NEED_OPENCL_SYCL', 'OpenCL SYCL', False) |
| 1537 | if environ_cp.get('TF_NEED_OPENCL_SYCL') == '1': |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1538 | set_host_cxx_compiler(environ_cp) |
| 1539 | set_host_c_compiler(environ_cp) |
Yifei Feng | b1d8c59 | 2017-11-22 13:42:21 -0800 | [diff] [blame] | 1540 | set_action_env_var(environ_cp, 'TF_NEED_COMPUTECPP', 'ComputeCPP', True) |
| 1541 | if environ_cp.get('TF_NEED_COMPUTECPP') == '1': |
| 1542 | set_computecpp_toolkit_path(environ_cp) |
| 1543 | else: |
| 1544 | set_trisycl_include_dir(environ_cp) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1545 | |
| 1546 | set_action_env_var(environ_cp, 'TF_NEED_CUDA', 'CUDA', False) |
A. Unique TensorFlower | 24cbb2a | 2017-09-08 07:45:44 -0700 | [diff] [blame] | 1547 | if (environ_cp.get('TF_NEED_CUDA') == '1' and |
| 1548 | 'TF_CUDA_CONFIG_REPO' not in environ_cp): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1549 | set_tf_cuda_version(environ_cp) |
Yifei Feng | b1d8c59 | 2017-11-22 13:42:21 -0800 | [diff] [blame] | 1550 | set_tf_cudnn_version(environ_cp) |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1551 | if is_linux(): |
| 1552 | set_tf_tensorrt_install_path(environ_cp) |
Michael Case | 0073d13 | 2018-04-11 09:34:44 -0700 | [diff] [blame] | 1553 | set_tf_nccl_install_path(environ_cp) |
| 1554 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1555 | set_tf_cuda_compute_capabilities(environ_cp) |
Ankur Taly | 0e6f39d | 2018-02-16 18:22:55 -0800 | [diff] [blame] | 1556 | if 'LD_LIBRARY_PATH' in environ_cp and environ_cp.get( |
| 1557 | 'LD_LIBRARY_PATH') != '1': |
| 1558 | write_action_env_to_bazelrc('LD_LIBRARY_PATH', |
| 1559 | environ_cp.get('LD_LIBRARY_PATH')) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1560 | |
| 1561 | set_tf_cuda_clang(environ_cp) |
| 1562 | if environ_cp.get('TF_CUDA_CLANG') == '1': |
Ilya Biryukov | 9e651e4 | 2018-03-22 05:33:42 -0700 | [diff] [blame] | 1563 | # Ask whether we should download the clang toolchain. |
| 1564 | set_tf_download_clang(environ_cp) |
A. Unique TensorFlower | fd29e95 | 2017-12-22 03:07:51 -0800 | [diff] [blame] | 1565 | if environ_cp.get('TF_DOWNLOAD_CLANG') != '1': |
| 1566 | # Set up which clang we should use as the cuda / host compiler. |
| 1567 | set_clang_cuda_compiler_path(environ_cp) |
Ilya Biryukov | 1c3d02e | 2018-09-04 03:09:52 -0700 | [diff] [blame] | 1568 | else: |
| 1569 | # Use downloaded LLD for linking. |
| 1570 | write_to_bazelrc('build:cuda_clang --config=download_clang_use_lld') |
| 1571 | write_to_bazelrc('test:cuda_clang --config=download_clang_use_lld') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1572 | else: |
| 1573 | # Set up which gcc nvcc should use as the host compiler |
| 1574 | # No need to set this on Windows |
| 1575 | if not is_windows(): |
| 1576 | set_gcc_host_compiler_path(environ_cp) |
| 1577 | set_other_cuda_vars(environ_cp) |
Ilya Biryukov | 9e651e4 | 2018-03-22 05:33:42 -0700 | [diff] [blame] | 1578 | else: |
| 1579 | # CUDA not required. Ask whether we should download the clang toolchain and |
| 1580 | # use it for the CPU build. |
| 1581 | set_tf_download_clang(environ_cp) |
| 1582 | if environ_cp.get('TF_DOWNLOAD_CLANG') == '1': |
| 1583 | write_to_bazelrc('build --config=download_clang') |
| 1584 | write_to_bazelrc('test --config=download_clang') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1585 | |
| 1586 | set_build_var(environ_cp, 'TF_NEED_MPI', 'MPI', 'with_mpi_support', False) |
| 1587 | if environ_cp.get('TF_NEED_MPI') == '1': |
| 1588 | set_mpi_home(environ_cp) |
| 1589 | set_other_mpi_vars(environ_cp) |
| 1590 | |
A. Unique TensorFlower | 061c359 | 2017-11-13 14:21:04 -0800 | [diff] [blame] | 1591 | set_grpc_build_flags() |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1592 | set_cc_opt_flags(environ_cp) |
Yifei Feng | 5198cb8 | 2018-08-17 13:53:06 -0700 | [diff] [blame] | 1593 | set_system_libs_flag(environ_cp) |
A. Unique TensorFlower | 1b21235 | 2018-07-19 13:48:50 -0700 | [diff] [blame] | 1594 | if is_windows(): |
| 1595 | set_windows_build_flags(environ_cp) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1596 | |
Anna R | a9a1d5a | 2018-09-14 12:44:31 -0700 | [diff] [blame] | 1597 | # Add a config option to build TensorFlow 2.0 API. |
| 1598 | write_to_bazelrc('build:v2 --define=tf_api_version=2') |
| 1599 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 1600 | if get_var(environ_cp, 'TF_SET_ANDROID_WORKSPACE', 'android workspace', False, |
| 1601 | ('Would you like to interactively configure ./WORKSPACE for ' |
| 1602 | 'Android builds?'), 'Searching for NDK and SDK installations.', |
| 1603 | 'Not configuring the WORKSPACE for Android builds.'): |
Michael Case | 5105350 | 2018-06-05 17:47:19 -0700 | [diff] [blame] | 1604 | create_android_ndk_rule(environ_cp) |
| 1605 | create_android_sdk_rule(environ_cp) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1606 | |
A. Unique TensorFlower | 1b21235 | 2018-07-19 13:48:50 -0700 | [diff] [blame] | 1607 | # On Windows, we don't have MKL support and the build is always monolithic. |
| 1608 | # So no need to print the following message. |
| 1609 | # TODO(pcloudy): remove the following if check when they make sense on Windows |
| 1610 | if not is_windows(): |
| 1611 | print('Preconfigured Bazel build configs. You can use any of the below by ' |
| 1612 | 'adding "--config=<>" to your build command. See tools/bazel.rc for ' |
| 1613 | 'more details.') |
| 1614 | config_info_line('mkl', 'Build with MKL support.') |
| 1615 | config_info_line('monolithic', 'Config for mostly static monolithic build.') |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1616 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame^] | 1617 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1618 | if __name__ == '__main__': |
| 1619 | main() |