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 | |
| 21 | import errno |
| 22 | import os |
| 23 | import platform |
| 24 | import re |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 25 | import subprocess |
| 26 | import sys |
| 27 | |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 28 | try: |
| 29 | from shutil import which |
| 30 | except ImportError: |
| 31 | from distutils.spawn import find_executable as which |
| 32 | |
Michael Case | fe2c8d8 | 2017-10-02 13:54:34 -0700 | [diff] [blame^] | 33 | _TF_BAZELRC = os.path.join(os.path.dirname(os.path.abspath(__file__)), |
| 34 | '.tf_configure.bazelrc') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 35 | _DEFAULT_CUDA_VERSION = '8.0' |
| 36 | _DEFAULT_CUDNN_VERSION = '6' |
| 37 | _DEFAULT_CUDA_COMPUTE_CAPABILITIES = '3.5,5.2' |
| 38 | _DEFAULT_CUDA_PATH = '/usr/local/cuda' |
| 39 | _DEFAULT_CUDA_PATH_LINUX = '/opt/cuda' |
| 40 | _DEFAULT_CUDA_PATH_WIN = ('C:/Program Files/NVIDIA GPU Computing ' |
| 41 | 'Toolkit/CUDA/v%s' % _DEFAULT_CUDA_VERSION) |
| 42 | _TF_OPENCL_VERSION = '1.2' |
| 43 | _DEFAULT_COMPUTECPP_TOOLKIT_PATH = '/usr/local/computecpp' |
| 44 | |
| 45 | |
| 46 | def is_windows(): |
| 47 | return platform.system() == 'Windows' |
| 48 | |
| 49 | |
| 50 | def is_linux(): |
| 51 | return platform.system() == 'Linux' |
| 52 | |
| 53 | |
| 54 | def is_macos(): |
| 55 | return platform.system() == 'Darwin' |
| 56 | |
| 57 | |
| 58 | def is_ppc64le(): |
| 59 | return platform.machine() == 'ppc64le' |
| 60 | |
| 61 | |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 62 | def is_cygwin(): |
| 63 | return platform.system().startswith('CYGWIN_NT') |
| 64 | |
| 65 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 66 | def get_input(question): |
| 67 | try: |
| 68 | try: |
| 69 | answer = raw_input(question) |
| 70 | except NameError: |
| 71 | answer = input(question) # pylint: disable=bad-builtin |
| 72 | except EOFError: |
| 73 | answer = '' |
| 74 | return answer |
| 75 | |
| 76 | |
| 77 | def symlink_force(target, link_name): |
| 78 | """Force symlink, equivalent of 'ln -sf'. |
| 79 | |
| 80 | Args: |
| 81 | target: items to link to. |
| 82 | link_name: name of the link. |
| 83 | """ |
| 84 | try: |
| 85 | os.symlink(target, link_name) |
| 86 | except OSError as e: |
| 87 | if e.errno == errno.EEXIST: |
| 88 | os.remove(link_name) |
| 89 | os.symlink(target, link_name) |
| 90 | else: |
| 91 | raise e |
| 92 | |
| 93 | |
| 94 | def sed_in_place(filename, old, new): |
| 95 | """Replace old string with new string in file. |
| 96 | |
| 97 | Args: |
| 98 | filename: string for filename. |
| 99 | old: string to replace. |
| 100 | new: new string to replace to. |
| 101 | """ |
| 102 | with open(filename, 'r') as f: |
| 103 | filedata = f.read() |
| 104 | newdata = filedata.replace(old, new) |
| 105 | with open(filename, 'w') as f: |
| 106 | f.write(newdata) |
| 107 | |
| 108 | |
| 109 | def remove_line_with(filename, token): |
| 110 | """Remove lines that contain token from file. |
| 111 | |
| 112 | Args: |
| 113 | filename: string for filename. |
| 114 | token: string token to check if to remove a line from file or not. |
| 115 | """ |
| 116 | with open(filename, 'r') as f: |
| 117 | filedata = f.read() |
| 118 | |
| 119 | with open(filename, 'w') as f: |
| 120 | for line in filedata.strip().split('\n'): |
| 121 | if token not in line: |
| 122 | f.write(line + '\n') |
| 123 | |
| 124 | |
| 125 | def write_to_bazelrc(line): |
| 126 | with open(_TF_BAZELRC, 'a') as f: |
| 127 | f.write(line + '\n') |
| 128 | |
| 129 | |
| 130 | def write_action_env_to_bazelrc(var_name, var): |
| 131 | write_to_bazelrc('build --action_env %s="%s"' % (var_name, str(var))) |
| 132 | |
| 133 | |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 134 | def run_shell(cmd, allow_non_zero=False): |
| 135 | if allow_non_zero: |
| 136 | try: |
| 137 | output = subprocess.check_output(cmd) |
| 138 | except subprocess.CalledProcessError as e: |
| 139 | output = e.output |
| 140 | else: |
| 141 | output = subprocess.check_output(cmd) |
| 142 | return output.decode('UTF-8').strip() |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 143 | |
| 144 | |
| 145 | def cygpath(path): |
| 146 | """Convert path from posix to windows.""" |
Martin Wicke | d57572e | 2017-09-02 19:21:45 -0700 | [diff] [blame] | 147 | return os.path.abspath(path).replace('\\', '/') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 148 | |
| 149 | |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 150 | def get_python_path(environ_cp, python_bin_path): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 151 | """Get the python site package paths.""" |
| 152 | python_paths = [] |
| 153 | if environ_cp.get('PYTHONPATH'): |
| 154 | python_paths = environ_cp.get('PYTHONPATH').split(':') |
| 155 | try: |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 156 | library_paths = run_shell( |
| 157 | [python_bin_path, '-c', |
| 158 | 'import site; print("\\n".join(site.getsitepackages()))']).split("\n") |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 159 | except subprocess.CalledProcessError: |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 160 | library_paths = [run_shell( |
| 161 | [python_bin_path, '-c', |
| 162 | 'from distutils.sysconfig import get_python_lib;' |
| 163 | 'print(get_python_lib())'])] |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 164 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 165 | all_paths = set(python_paths + library_paths) |
| 166 | |
| 167 | paths = [] |
| 168 | for path in all_paths: |
| 169 | if os.path.isdir(path): |
| 170 | paths.append(path) |
| 171 | return paths |
| 172 | |
| 173 | |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 174 | def get_python_major_version(python_bin_path): |
| 175 | """Get the python major version.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 176 | 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] | 177 | |
| 178 | |
Gunhan Gulsoy | ed89a2b | 2017-09-19 18:36:26 -0700 | [diff] [blame] | 179 | def setup_python(environ_cp): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 180 | """Setup python related env variables.""" |
| 181 | # Get PYTHON_BIN_PATH, default is the current running python. |
| 182 | default_python_bin_path = sys.executable |
| 183 | ask_python_bin_path = ('Please specify the location of python. [Default is ' |
| 184 | '%s]: ') % default_python_bin_path |
| 185 | while True: |
| 186 | python_bin_path = get_from_env_or_user_or_default( |
| 187 | environ_cp, 'PYTHON_BIN_PATH', ask_python_bin_path, |
| 188 | default_python_bin_path) |
| 189 | # Check if the path is valid |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 190 | if os.path.isfile(python_bin_path) and os.access( |
| 191 | python_bin_path, os.X_OK): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 192 | break |
| 193 | elif not os.path.exists(python_bin_path): |
| 194 | print('Invalid python path: %s cannot be found.' % python_bin_path) |
| 195 | else: |
| 196 | print('%s is not executable. Is it the python binary?' % python_bin_path) |
| 197 | environ_cp['PYTHON_BIN_PATH'] = '' |
| 198 | |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 199 | # Convert python path to Windows style before checking lib and version |
Martin Wicke | d57572e | 2017-09-02 19:21:45 -0700 | [diff] [blame] | 200 | if is_windows() or is_cygwin(): |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 201 | python_bin_path = cygpath(python_bin_path) |
| 202 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 203 | # Get PYTHON_LIB_PATH |
| 204 | python_lib_path = environ_cp.get('PYTHON_LIB_PATH') |
| 205 | if not python_lib_path: |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 206 | python_lib_paths = get_python_path(environ_cp, python_bin_path) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 207 | if environ_cp.get('USE_DEFAULT_PYTHON_LIB_PATH') == '1': |
Vijay Vasudevan | a1fba7f | 2017-07-28 10:58:56 -0700 | [diff] [blame] | 208 | python_lib_path = python_lib_paths[0] |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 209 | else: |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 210 | print('Found possible Python library paths:\n %s' % |
| 211 | '\n '.join(python_lib_paths)) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 212 | default_python_lib_path = python_lib_paths[0] |
| 213 | python_lib_path = get_input( |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 214 | 'Please input the desired Python library path to use. ' |
| 215 | 'Default is [%s]\n' % python_lib_paths[0]) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 216 | if not python_lib_path: |
| 217 | python_lib_path = default_python_lib_path |
Vijay Vasudevan | a1fba7f | 2017-07-28 10:58:56 -0700 | [diff] [blame] | 218 | environ_cp['PYTHON_LIB_PATH'] = python_lib_path |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 219 | |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 220 | python_major_version = get_python_major_version(python_bin_path) |
| 221 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 222 | # Convert python path to Windows style before writing into bazel.rc |
Martin Wicke | d57572e | 2017-09-02 19:21:45 -0700 | [diff] [blame] | 223 | if is_windows() or is_cygwin(): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 224 | python_lib_path = cygpath(python_lib_path) |
| 225 | |
| 226 | # Set-up env variables used by python_configure.bzl |
| 227 | write_action_env_to_bazelrc('PYTHON_BIN_PATH', python_bin_path) |
| 228 | write_action_env_to_bazelrc('PYTHON_LIB_PATH', python_lib_path) |
| 229 | write_to_bazelrc('build --define PYTHON_BIN_PATH="%s"' % python_bin_path) |
| 230 | write_to_bazelrc('build --define PYTHON_LIB_PATH="%s"' % python_lib_path) |
| 231 | write_to_bazelrc('build --force_python=py%s' % python_major_version) |
| 232 | write_to_bazelrc('build --host_force_python=py%s' % python_major_version) |
Gunhan Gulsoy | ed89a2b | 2017-09-19 18:36:26 -0700 | [diff] [blame] | 233 | write_to_bazelrc('build --python_path=\"%s"' % python_bin_path) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 234 | write_to_bazelrc('test --force_python=py%s' % python_major_version) |
| 235 | write_to_bazelrc('test --host_force_python=py%s' % python_major_version) |
| 236 | write_to_bazelrc('test --define PYTHON_BIN_PATH="%s"' % python_bin_path) |
| 237 | write_to_bazelrc('test --define PYTHON_LIB_PATH="%s"' % python_lib_path) |
| 238 | write_to_bazelrc('run --define PYTHON_BIN_PATH="%s"' % python_bin_path) |
| 239 | write_to_bazelrc('run --define PYTHON_LIB_PATH="%s"' % python_lib_path) |
| 240 | environ_cp['PYTHON_BIN_PATH'] = python_bin_path |
| 241 | |
| 242 | # Write tools/python_bin_path.sh |
| 243 | with open('tools/python_bin_path.sh', 'w') as f: |
| 244 | f.write('export PYTHON_BIN_PATH="%s"' % python_bin_path) |
| 245 | |
| 246 | |
| 247 | def reset_tf_configure_bazelrc(): |
| 248 | """Reset file that contains customized config settings.""" |
| 249 | open(_TF_BAZELRC, 'w').close() |
| 250 | |
| 251 | home = os.path.expanduser('~') |
| 252 | if not os.path.exists('.bazelrc'): |
| 253 | if os.path.exists(os.path.join(home, '.bazelrc')): |
| 254 | with open('.bazelrc', 'a') as f: |
Shanqing Cai | e2e3a94 | 2017-09-25 19:35:53 -0700 | [diff] [blame] | 255 | f.write('import %s/.bazelrc\n' % home.replace('\\', '/')) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 256 | else: |
| 257 | open('.bazelrc', 'w').close() |
| 258 | |
| 259 | remove_line_with('.bazelrc', 'tf_configure') |
| 260 | with open('.bazelrc', 'a') as f: |
| 261 | f.write('import %workspace%/.tf_configure.bazelrc\n') |
| 262 | |
| 263 | |
| 264 | def run_gen_git_source(environ_cp): |
| 265 | """Run the gen_git_source to create links. |
| 266 | |
| 267 | The links are for bazel to track dependencies for git hash propagation. |
| 268 | |
| 269 | Args: |
| 270 | environ_cp: copy of the os.environ. |
| 271 | """ |
Vijay Vasudevan | a1fba7f | 2017-07-28 10:58:56 -0700 | [diff] [blame] | 272 | cmd = '"%s" tensorflow/tools/git/gen_git_source.py --configure %s' % ( |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 273 | environ_cp.get('PYTHON_BIN_PATH'), os.getcwd()) |
| 274 | os.system(cmd) |
| 275 | |
| 276 | |
| 277 | def cleanup_makefile(): |
| 278 | """Delete any leftover BUILD files from the Makefile build. |
| 279 | |
| 280 | These files could interfere with Bazel parsing. |
| 281 | """ |
| 282 | makefile_download_dir = 'tensorflow/contrib/makefile/downloads' |
| 283 | if os.path.isdir(makefile_download_dir): |
| 284 | for root, _, filenames in os.walk(makefile_download_dir): |
| 285 | for f in filenames: |
| 286 | if f.endswith('BUILD'): |
| 287 | os.remove(os.path.join(root, f)) |
| 288 | |
| 289 | |
| 290 | def get_var(environ_cp, |
| 291 | var_name, |
| 292 | query_item, |
| 293 | enabled_by_default, |
| 294 | question=None, |
| 295 | yes_reply=None, |
| 296 | no_reply=None): |
| 297 | """Get boolean input from user. |
| 298 | |
| 299 | If var_name is not set in env, ask user to enable query_item or not. If the |
| 300 | response is empty, use the default. |
| 301 | |
| 302 | Args: |
| 303 | environ_cp: copy of the os.environ. |
| 304 | var_name: string for name of environment variable, e.g. "TF_NEED_HDFS". |
| 305 | query_item: string for feature related to the variable, e.g. "Hadoop File |
| 306 | System". |
| 307 | enabled_by_default: boolean for default behavior. |
| 308 | question: optional string for how to ask for user input. |
| 309 | yes_reply: optionanl string for reply when feature is enabled. |
| 310 | no_reply: optional string for reply when feature is disabled. |
| 311 | |
| 312 | Returns: |
| 313 | boolean value of the variable. |
| 314 | """ |
| 315 | if not question: |
| 316 | question = 'Do you wish to build TensorFlow with %s support?' % query_item |
| 317 | if not yes_reply: |
| 318 | yes_reply = '%s support will be enabled for TensorFlow.' % query_item |
| 319 | if not no_reply: |
| 320 | no_reply = 'No %s' % yes_reply |
| 321 | |
| 322 | yes_reply += '\n' |
| 323 | no_reply += '\n' |
| 324 | |
| 325 | if enabled_by_default: |
| 326 | question += ' [Y/n]: ' |
| 327 | else: |
| 328 | question += ' [y/N]: ' |
| 329 | |
| 330 | var = environ_cp.get(var_name) |
| 331 | while var is None: |
| 332 | user_input_origin = get_input(question) |
| 333 | user_input = user_input_origin.strip().lower() |
| 334 | if user_input == 'y': |
| 335 | print(yes_reply) |
| 336 | var = True |
| 337 | elif user_input == 'n': |
| 338 | print(no_reply) |
| 339 | var = False |
| 340 | elif not user_input: |
| 341 | if enabled_by_default: |
| 342 | print(yes_reply) |
| 343 | var = True |
| 344 | else: |
| 345 | print(no_reply) |
| 346 | var = False |
| 347 | else: |
| 348 | print('Invalid selection: %s' % user_input_origin) |
| 349 | return var |
| 350 | |
| 351 | |
| 352 | def set_build_var(environ_cp, var_name, query_item, option_name, |
Michael Case | 98850a5 | 2017-09-14 13:35:57 -0700 | [diff] [blame] | 353 | enabled_by_default, bazel_config_name=None): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 354 | """Set if query_item will be enabled for the build. |
| 355 | |
| 356 | Ask user if query_item will be enabled. Default is used if no input is given. |
| 357 | Set subprocess environment variable and write to .bazelrc if enabled. |
| 358 | |
| 359 | Args: |
| 360 | environ_cp: copy of the os.environ. |
| 361 | var_name: string for name of environment variable, e.g. "TF_NEED_HDFS". |
| 362 | query_item: string for feature related to the variable, e.g. "Hadoop File |
| 363 | System". |
| 364 | option_name: string for option to define in .bazelrc. |
| 365 | enabled_by_default: boolean for default behavior. |
Michael Case | 98850a5 | 2017-09-14 13:35:57 -0700 | [diff] [blame] | 366 | 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] | 367 | """ |
| 368 | |
| 369 | var = str(int(get_var(environ_cp, var_name, query_item, enabled_by_default))) |
| 370 | environ_cp[var_name] = var |
| 371 | if var == '1': |
| 372 | write_to_bazelrc('build --define %s=true' % option_name) |
Michael Case | 98850a5 | 2017-09-14 13:35:57 -0700 | [diff] [blame] | 373 | elif bazel_config_name is not None: |
| 374 | # TODO(mikecase): Migrate all users of configure.py to use --config Bazel |
| 375 | # options and not to set build configs through environment variables. |
| 376 | write_to_bazelrc('build:%s --define %s=true' |
| 377 | % (bazel_config_name, option_name)) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 378 | |
| 379 | |
| 380 | def set_action_env_var(environ_cp, |
| 381 | var_name, |
| 382 | query_item, |
| 383 | enabled_by_default, |
| 384 | question=None, |
| 385 | yes_reply=None, |
| 386 | no_reply=None): |
| 387 | """Set boolean action_env variable. |
| 388 | |
| 389 | Ask user if query_item will be enabled. Default is used if no input is given. |
| 390 | Set environment variable and write to .bazelrc. |
| 391 | |
| 392 | Args: |
| 393 | environ_cp: copy of the os.environ. |
| 394 | var_name: string for name of environment variable, e.g. "TF_NEED_HDFS". |
| 395 | query_item: string for feature related to the variable, e.g. "Hadoop File |
| 396 | System". |
| 397 | enabled_by_default: boolean for default behavior. |
| 398 | question: optional string for how to ask for user input. |
| 399 | yes_reply: optionanl string for reply when feature is enabled. |
| 400 | no_reply: optional string for reply when feature is disabled. |
| 401 | """ |
| 402 | var = int( |
| 403 | get_var(environ_cp, var_name, query_item, enabled_by_default, question, |
| 404 | yes_reply, no_reply)) |
| 405 | |
| 406 | write_action_env_to_bazelrc(var_name, var) |
| 407 | environ_cp[var_name] = str(var) |
| 408 | |
| 409 | |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 410 | def convert_version_to_int(version): |
| 411 | """Convert a version number to a integer that can be used to compare. |
| 412 | |
A. Unique TensorFlower | 28ce1d1 | 2017-08-15 12:08:29 -0700 | [diff] [blame] | 413 | Version strings of the form X.YZ and X.Y.Z-xxxxx are supported. The |
| 414 | 'xxxxx' part, for instance 'homebrew' on OS/X, is ignored. |
| 415 | |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 416 | Args: |
A. Unique TensorFlower | 28ce1d1 | 2017-08-15 12:08:29 -0700 | [diff] [blame] | 417 | version: a version to be converted |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 418 | |
| 419 | Returns: |
| 420 | An integer if converted successfully, otherwise return None. |
| 421 | """ |
A. Unique TensorFlower | 28ce1d1 | 2017-08-15 12:08:29 -0700 | [diff] [blame] | 422 | version = version.split('-')[0] |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 423 | version_segments = version.split('.') |
| 424 | for seg in version_segments: |
| 425 | if not seg.isdigit(): |
| 426 | return None |
| 427 | |
| 428 | version_str = ''.join(['%03d' % int(seg) for seg in version_segments]) |
| 429 | return int(version_str) |
| 430 | |
| 431 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 432 | def check_bazel_version(min_version): |
| 433 | """Check installed bezel version is at least min_version. |
| 434 | |
| 435 | Args: |
| 436 | min_version: string for minimum bazel version. |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 437 | |
| 438 | Returns: |
| 439 | The bazel version detected. |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 440 | """ |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 441 | if which('bazel') is None: |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 442 | print('Cannot find bazel. Please install bazel.') |
| 443 | sys.exit(0) |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 444 | curr_version = run_shell(['bazel', '--batch', 'version']) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 445 | |
| 446 | for line in curr_version.split('\n'): |
| 447 | if 'Build label: ' in line: |
| 448 | curr_version = line.split('Build label: ')[1] |
| 449 | break |
| 450 | |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 451 | min_version_int = convert_version_to_int(min_version) |
| 452 | curr_version_int = convert_version_to_int(curr_version) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 453 | |
| 454 | # Check if current bazel version can be detected properly. |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 455 | if not curr_version_int: |
| 456 | print('WARNING: current bazel installation is not a release version.') |
| 457 | print('Make sure you are running at least bazel %s' % min_version) |
| 458 | return curr_version |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 459 | |
Michael Case | d94271a | 2017-08-22 17:26:52 -0700 | [diff] [blame] | 460 | print('You have bazel %s installed.' % curr_version) |
A. Unique TensorFlower | 28ce1d1 | 2017-08-15 12:08:29 -0700 | [diff] [blame] | 461 | |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 462 | if curr_version_int < min_version_int: |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 463 | print('Please upgrade your bazel installation to version %s or higher to ' |
| 464 | 'build TensorFlow!' % min_version) |
| 465 | sys.exit(0) |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 466 | return curr_version |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 467 | |
| 468 | |
| 469 | def set_cc_opt_flags(environ_cp): |
| 470 | """Set up architecture-dependent optimization flags. |
| 471 | |
| 472 | Also append CC optimization flags to bazel.rc.. |
| 473 | |
| 474 | Args: |
| 475 | environ_cp: copy of the os.environ. |
| 476 | """ |
| 477 | if is_ppc64le(): |
| 478 | # gcc on ppc64le does not support -march, use mcpu instead |
| 479 | default_cc_opt_flags = '-mcpu=native' |
| 480 | else: |
| 481 | default_cc_opt_flags = '-march=native' |
| 482 | question = ('Please specify optimization flags to use during compilation when' |
| 483 | ' bazel option "--config=opt" is specified [Default is %s]: ' |
| 484 | ) % default_cc_opt_flags |
| 485 | cc_opt_flags = get_from_env_or_user_or_default(environ_cp, 'CC_OPT_FLAGS', |
| 486 | question, default_cc_opt_flags) |
| 487 | for opt in cc_opt_flags.split(): |
| 488 | write_to_bazelrc('build:opt --cxxopt=%s --copt=%s' % (opt, opt)) |
| 489 | |
| 490 | |
| 491 | def set_tf_cuda_clang(environ_cp): |
| 492 | """set TF_CUDA_CLANG action_env. |
| 493 | |
| 494 | Args: |
| 495 | environ_cp: copy of the os.environ. |
| 496 | """ |
| 497 | question = 'Do you want to use clang as CUDA compiler?' |
| 498 | yes_reply = 'Clang will be used as CUDA compiler.' |
| 499 | no_reply = 'nvcc will be used as CUDA compiler.' |
| 500 | set_action_env_var( |
| 501 | environ_cp, |
| 502 | 'TF_CUDA_CLANG', |
| 503 | None, |
| 504 | False, |
| 505 | question=question, |
| 506 | yes_reply=yes_reply, |
| 507 | no_reply=no_reply) |
| 508 | |
| 509 | |
| 510 | def get_from_env_or_user_or_default(environ_cp, var_name, ask_for_var, |
| 511 | var_default): |
| 512 | """Get var_name either from env, or user or default. |
| 513 | |
| 514 | If var_name has been set as environment variable, use the preset value, else |
| 515 | ask for user input. If no input is provided, the default is used. |
| 516 | |
| 517 | Args: |
| 518 | environ_cp: copy of the os.environ. |
| 519 | var_name: string for name of environment variable, e.g. "TF_NEED_HDFS". |
| 520 | ask_for_var: string for how to ask for user input. |
| 521 | var_default: default value string. |
| 522 | |
| 523 | Returns: |
| 524 | string value for var_name |
| 525 | """ |
| 526 | var = environ_cp.get(var_name) |
| 527 | if not var: |
| 528 | var = get_input(ask_for_var) |
Michael Case | d94271a | 2017-08-22 17:26:52 -0700 | [diff] [blame] | 529 | print('\n') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 530 | if not var: |
| 531 | var = var_default |
| 532 | return var |
| 533 | |
| 534 | |
| 535 | def set_clang_cuda_compiler_path(environ_cp): |
| 536 | """Set CLANG_CUDA_COMPILER_PATH.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 537 | default_clang_path = which('clang') or '' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 538 | ask_clang_path = ('Please specify which clang should be used as device and ' |
| 539 | 'host compiler. [Default is %s]: ') % default_clang_path |
| 540 | |
| 541 | while True: |
| 542 | clang_cuda_compiler_path = get_from_env_or_user_or_default( |
| 543 | environ_cp, 'CLANG_CUDA_COMPILER_PATH', ask_clang_path, |
| 544 | default_clang_path) |
| 545 | if os.path.exists(clang_cuda_compiler_path): |
| 546 | break |
| 547 | |
| 548 | # Reset and retry |
| 549 | print('Invalid clang path: %s cannot be found.' % clang_cuda_compiler_path) |
| 550 | environ_cp['CLANG_CUDA_COMPILER_PATH'] = '' |
| 551 | |
| 552 | # Set CLANG_CUDA_COMPILER_PATH |
| 553 | environ_cp['CLANG_CUDA_COMPILER_PATH'] = clang_cuda_compiler_path |
| 554 | write_action_env_to_bazelrc('CLANG_CUDA_COMPILER_PATH', |
| 555 | clang_cuda_compiler_path) |
| 556 | |
| 557 | |
| 558 | def set_gcc_host_compiler_path(environ_cp): |
| 559 | """Set GCC_HOST_COMPILER_PATH.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 560 | default_gcc_host_compiler_path = which('gcc') or '' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 561 | cuda_bin_symlink = '%s/bin/gcc' % environ_cp.get('CUDA_TOOLKIT_PATH') |
| 562 | |
| 563 | if os.path.islink(cuda_bin_symlink): |
| 564 | # os.readlink is only available in linux |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 565 | default_gcc_host_compiler_path = os.path.realpath(cuda_bin_symlink) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 566 | |
| 567 | ask_gcc_path = ( |
| 568 | 'Please specify which gcc should be used by nvcc as the ' |
| 569 | 'host compiler. [Default is %s]: ') % default_gcc_host_compiler_path |
| 570 | while True: |
| 571 | gcc_host_compiler_path = get_from_env_or_user_or_default( |
| 572 | environ_cp, 'GCC_HOST_COMPILER_PATH', ask_gcc_path, |
| 573 | default_gcc_host_compiler_path) |
| 574 | |
| 575 | if os.path.exists(gcc_host_compiler_path): |
| 576 | break |
| 577 | |
| 578 | # Reset and retry |
| 579 | print('Invalid gcc path. %s cannot be found' % gcc_host_compiler_path) |
| 580 | environ_cp['GCC_HOST_COMPILER_PATH'] = '' |
| 581 | |
| 582 | # Set GCC_HOST_COMPILER_PATH |
| 583 | environ_cp['GCC_HOST_COMPILER_PATH'] = gcc_host_compiler_path |
| 584 | write_action_env_to_bazelrc('GCC_HOST_COMPILER_PATH', gcc_host_compiler_path) |
| 585 | |
| 586 | |
| 587 | def set_tf_cuda_version(environ_cp): |
| 588 | """Set CUDA_TOOLKIT_PATH and TF_CUDA_VERSION.""" |
| 589 | ask_cuda_version = ( |
| 590 | 'Please specify the CUDA SDK version you want to use, ' |
| 591 | 'e.g. 7.0. [Leave empty to default to CUDA %s]: ') % _DEFAULT_CUDA_VERSION |
| 592 | |
| 593 | while True: |
| 594 | # Configure the Cuda SDK version to use. |
| 595 | tf_cuda_version = get_from_env_or_user_or_default( |
| 596 | environ_cp, 'TF_CUDA_VERSION', ask_cuda_version, _DEFAULT_CUDA_VERSION) |
| 597 | |
| 598 | # Find out where the CUDA toolkit is installed |
| 599 | default_cuda_path = _DEFAULT_CUDA_PATH |
Martin Wicke | d57572e | 2017-09-02 19:21:45 -0700 | [diff] [blame] | 600 | if is_windows() or is_cygwin(): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 601 | default_cuda_path = cygpath( |
| 602 | environ_cp.get('CUDA_PATH', _DEFAULT_CUDA_PATH_WIN)) |
| 603 | elif is_linux(): |
| 604 | # If the default doesn't exist, try an alternative default. |
| 605 | if (not os.path.exists(default_cuda_path) |
| 606 | ) and os.path.exists(_DEFAULT_CUDA_PATH_LINUX): |
| 607 | default_cuda_path = _DEFAULT_CUDA_PATH_LINUX |
| 608 | ask_cuda_path = ('Please specify the location where CUDA %s toolkit is' |
| 609 | ' installed. Refer to README.md for more details. ' |
| 610 | '[Default is %s]: ') % (tf_cuda_version, default_cuda_path) |
| 611 | cuda_toolkit_path = get_from_env_or_user_or_default( |
| 612 | environ_cp, 'CUDA_TOOLKIT_PATH', ask_cuda_path, default_cuda_path) |
| 613 | |
| 614 | if is_windows(): |
| 615 | cuda_rt_lib_path = 'lib/x64/cudart.lib' |
| 616 | elif is_linux(): |
| 617 | cuda_rt_lib_path = 'lib64/libcudart.so.%s' % tf_cuda_version |
| 618 | elif is_macos(): |
| 619 | cuda_rt_lib_path = 'lib/libcudart.%s.dylib' % tf_cuda_version |
| 620 | |
| 621 | cuda_toolkit_path_full = os.path.join(cuda_toolkit_path, cuda_rt_lib_path) |
| 622 | if os.path.exists(cuda_toolkit_path_full): |
| 623 | break |
| 624 | |
| 625 | # Reset and retry |
| 626 | print('Invalid path to CUDA %s toolkit. %s cannot be found' % |
| 627 | (tf_cuda_version, cuda_toolkit_path_full)) |
| 628 | environ_cp['TF_CUDA_VERSION'] = '' |
| 629 | environ_cp['CUDA_TOOLKIT_PATH'] = '' |
| 630 | |
| 631 | # Set CUDA_TOOLKIT_PATH and TF_CUDA_VERSION |
| 632 | environ_cp['CUDA_TOOLKIT_PATH'] = cuda_toolkit_path |
| 633 | write_action_env_to_bazelrc('CUDA_TOOLKIT_PATH', cuda_toolkit_path) |
| 634 | environ_cp['TF_CUDA_VERSION'] = tf_cuda_version |
| 635 | write_action_env_to_bazelrc('TF_CUDA_VERSION', tf_cuda_version) |
| 636 | |
| 637 | |
| 638 | def set_tf_cunn_version(environ_cp): |
| 639 | """Set CUDNN_INSTALL_PATH and TF_CUDNN_VERSION.""" |
| 640 | ask_cudnn_version = ( |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 641 | 'Please specify the cuDNN version you want to use. ' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 642 | '[Leave empty to default to cuDNN %s.0]: ') % _DEFAULT_CUDNN_VERSION |
| 643 | |
| 644 | while True: |
| 645 | tf_cudnn_version = get_from_env_or_user_or_default( |
| 646 | environ_cp, 'TF_CUDNN_VERSION', ask_cudnn_version, |
| 647 | _DEFAULT_CUDNN_VERSION) |
| 648 | |
| 649 | default_cudnn_path = environ_cp.get('CUDA_TOOLKIT_PATH') |
| 650 | ask_cudnn_path = (r'Please specify the location where cuDNN %s library is ' |
| 651 | 'installed. Refer to README.md for more details. [Default' |
| 652 | ' is %s]:') % (tf_cudnn_version, default_cudnn_path) |
| 653 | cudnn_install_path = get_from_env_or_user_or_default( |
| 654 | environ_cp, 'CUDNN_INSTALL_PATH', ask_cudnn_path, default_cudnn_path) |
| 655 | |
| 656 | # Result returned from "read" will be used unexpanded. That make "~" |
| 657 | # unusable. Going through one more level of expansion to handle that. |
| 658 | cudnn_install_path = os.path.realpath( |
| 659 | os.path.expanduser(cudnn_install_path)) |
Martin Wicke | d57572e | 2017-09-02 19:21:45 -0700 | [diff] [blame] | 660 | if is_windows() or is_cygwin(): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 661 | cudnn_install_path = cygpath(cudnn_install_path) |
| 662 | |
| 663 | if is_windows(): |
| 664 | cuda_dnn_lib_path = 'lib/x64/cudnn.lib' |
| 665 | cuda_dnn_lib_alt_path = 'lib/x64/cudnn.lib' |
| 666 | elif is_linux(): |
| 667 | cuda_dnn_lib_path = 'lib64/libcudnn.so.%s' % tf_cudnn_version |
| 668 | cuda_dnn_lib_alt_path = 'libcudnn.so.%s' % tf_cudnn_version |
| 669 | elif is_macos(): |
| 670 | cuda_dnn_lib_path = 'lib/libcudnn.%s.dylib' % tf_cudnn_version |
| 671 | cuda_dnn_lib_alt_path = 'libcudnn.%s.dylib' % tf_cudnn_version |
| 672 | |
| 673 | cuda_dnn_lib_path_full = os.path.join(cudnn_install_path, cuda_dnn_lib_path) |
| 674 | cuda_dnn_lib_alt_path_full = os.path.join(cudnn_install_path, |
| 675 | cuda_dnn_lib_alt_path) |
| 676 | if os.path.exists(cuda_dnn_lib_path_full) or os.path.exists( |
| 677 | cuda_dnn_lib_alt_path_full): |
| 678 | break |
| 679 | |
| 680 | # Try another alternative for Linux |
| 681 | if is_linux(): |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 682 | ldconfig_bin = which('ldconfig') or '/sbin/ldconfig' |
| 683 | cudnn_path_from_ldconfig = run_shell([ldconfig_bin, '-p']) |
| 684 | cudnn_path_from_ldconfig = re.search('.*libcudnn.so .* => (.*)', |
A. Unique TensorFlower | e722358 | 2017-09-06 17:57:04 -0700 | [diff] [blame] | 685 | cudnn_path_from_ldconfig) |
| 686 | if cudnn_path_from_ldconfig: |
| 687 | cudnn_path_from_ldconfig = cudnn_path_from_ldconfig.group(1) |
| 688 | if os.path.exists('%s.%s' % (cudnn_path_from_ldconfig, |
| 689 | tf_cudnn_version)): |
| 690 | cudnn_install_path = os.path.dirname(cudnn_path_from_ldconfig) |
| 691 | break |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 692 | |
| 693 | # Reset and Retry |
| 694 | print( |
| 695 | 'Invalid path to cuDNN %s toolkit. None of the following files can be ' |
| 696 | 'found:' % tf_cudnn_version) |
| 697 | print(cuda_dnn_lib_path_full) |
| 698 | print(cuda_dnn_lib_alt_path_full) |
| 699 | if is_linux(): |
| 700 | print('%s.%s' % (cudnn_path_from_ldconfig, tf_cudnn_version)) |
| 701 | |
| 702 | environ_cp['TF_CUDNN_VERSION'] = '' |
| 703 | |
| 704 | # Set CUDNN_INSTALL_PATH and TF_CUDNN_VERSION |
| 705 | environ_cp['CUDNN_INSTALL_PATH'] = cudnn_install_path |
| 706 | write_action_env_to_bazelrc('CUDNN_INSTALL_PATH', cudnn_install_path) |
| 707 | environ_cp['TF_CUDNN_VERSION'] = tf_cudnn_version |
| 708 | write_action_env_to_bazelrc('TF_CUDNN_VERSION', tf_cudnn_version) |
| 709 | |
| 710 | |
| 711 | def get_native_cuda_compute_capabilities(environ_cp): |
| 712 | """Get native cuda compute capabilities. |
| 713 | |
| 714 | Args: |
| 715 | environ_cp: copy of the os.environ. |
| 716 | Returns: |
| 717 | string of native cuda compute capabilities, separated by comma. |
| 718 | """ |
| 719 | device_query_bin = os.path.join( |
| 720 | environ_cp.get('CUDA_TOOLKIT_PATH'), 'extras/demo_suite/deviceQuery') |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 721 | if os.path.isfile(device_query_bin) and os.access(device_query_bin, os.X_OK): |
| 722 | try: |
| 723 | output = run_shell(device_query_bin).split('\n') |
| 724 | pattern = re.compile('[0-9]*\\.[0-9]*') |
| 725 | output = [pattern.search(x) for x in output if 'Capability' in x] |
| 726 | output = ','.join(x.group() for x in output if x is not None) |
| 727 | except subprocess.CalledProcessError: |
| 728 | output = '' |
| 729 | else: |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 730 | output = '' |
| 731 | return output |
| 732 | |
| 733 | |
| 734 | def set_tf_cuda_compute_capabilities(environ_cp): |
| 735 | """Set TF_CUDA_COMPUTE_CAPABILITIES.""" |
| 736 | while True: |
| 737 | native_cuda_compute_capabilities = get_native_cuda_compute_capabilities( |
| 738 | environ_cp) |
| 739 | if not native_cuda_compute_capabilities: |
| 740 | default_cuda_compute_capabilities = _DEFAULT_CUDA_COMPUTE_CAPABILITIES |
| 741 | else: |
| 742 | default_cuda_compute_capabilities = native_cuda_compute_capabilities |
| 743 | |
| 744 | ask_cuda_compute_capabilities = ( |
| 745 | 'Please specify a list of comma-separated ' |
| 746 | 'Cuda compute capabilities you want to ' |
| 747 | 'build with.\nYou can find the compute ' |
| 748 | 'capability of your device at: ' |
| 749 | 'https://developer.nvidia.com/cuda-gpus.\nPlease' |
| 750 | ' note that each additional compute ' |
| 751 | 'capability significantly increases your ' |
| 752 | 'build time and binary size. [Default is: %s]' % |
| 753 | default_cuda_compute_capabilities) |
| 754 | tf_cuda_compute_capabilities = get_from_env_or_user_or_default( |
| 755 | environ_cp, 'TF_CUDA_COMPUTE_CAPABILITIES', |
| 756 | ask_cuda_compute_capabilities, default_cuda_compute_capabilities) |
| 757 | # Check whether all capabilities from the input is valid |
| 758 | all_valid = True |
| 759 | for compute_capability in tf_cuda_compute_capabilities.split(','): |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 760 | m = re.match('[0-9]+.[0-9]+', compute_capability) |
| 761 | if not m: |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 762 | print('Invalid compute capability: ' % compute_capability) |
| 763 | all_valid = False |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 764 | else: |
| 765 | ver = int(m.group(0).split('.')[0]) |
| 766 | if ver < 3: |
| 767 | print('Only compute capabilities 3.0 or higher are supported.') |
| 768 | all_valid = False |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 769 | |
| 770 | if all_valid: |
| 771 | break |
| 772 | |
| 773 | # Reset and Retry |
| 774 | environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = '' |
| 775 | |
| 776 | # Set TF_CUDA_COMPUTE_CAPABILITIES |
| 777 | environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = tf_cuda_compute_capabilities |
| 778 | write_action_env_to_bazelrc('TF_CUDA_COMPUTE_CAPABILITIES', |
| 779 | tf_cuda_compute_capabilities) |
| 780 | |
| 781 | |
| 782 | def set_other_cuda_vars(environ_cp): |
| 783 | """Set other CUDA related variables.""" |
| 784 | if is_windows(): |
| 785 | # The following three variables are needed for MSVC toolchain configuration |
| 786 | # in Bazel |
| 787 | environ_cp['CUDA_PATH'] = environ_cp.get('CUDA_TOOLKIT_PATH') |
| 788 | environ_cp['CUDA_COMPUTE_CAPABILITIES'] = environ_cp.get( |
| 789 | 'TF_CUDA_COMPUTE_CAPABILITIES') |
| 790 | environ_cp['NO_WHOLE_ARCHIVE_OPTION'] = 1 |
| 791 | write_action_env_to_bazelrc('CUDA_PATH', environ_cp.get('CUDA_PATH')) |
| 792 | write_action_env_to_bazelrc('CUDA_COMPUTE_CAPABILITIE', |
| 793 | environ_cp.get('CUDA_COMPUTE_CAPABILITIE')) |
| 794 | write_action_env_to_bazelrc('NO_WHOLE_ARCHIVE_OPTION', |
| 795 | environ_cp.get('NO_WHOLE_ARCHIVE_OPTION')) |
| 796 | write_to_bazelrc('build --config=win-cuda') |
| 797 | write_to_bazelrc('test --config=win-cuda') |
| 798 | else: |
| 799 | # If CUDA is enabled, always use GPU during build and test. |
| 800 | if environ_cp.get('TF_CUDA_CLANG') == '1': |
| 801 | write_to_bazelrc('build --config=cuda_clang') |
| 802 | write_to_bazelrc('test --config=cuda_clang') |
| 803 | else: |
| 804 | write_to_bazelrc('build --config=cuda') |
| 805 | write_to_bazelrc('test --config=cuda') |
| 806 | |
| 807 | |
| 808 | def set_host_cxx_compiler(environ_cp): |
| 809 | """Set HOST_CXX_COMPILER.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 810 | default_cxx_host_compiler = which('g++') or '' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 811 | ask_cxx_host_compiler = ( |
| 812 | 'Please specify which C++ compiler should be used as' |
| 813 | ' the host C++ compiler. [Default is %s]: ') % default_cxx_host_compiler |
| 814 | |
| 815 | while True: |
| 816 | host_cxx_compiler = get_from_env_or_user_or_default( |
| 817 | environ_cp, 'HOST_CXX_COMPILER', ask_cxx_host_compiler, |
| 818 | default_cxx_host_compiler) |
| 819 | if os.path.exists(host_cxx_compiler): |
| 820 | break |
| 821 | |
| 822 | # Reset and retry |
| 823 | print('Invalid C++ compiler path. %s cannot be found' % host_cxx_compiler) |
| 824 | environ_cp['HOST_CXX_COMPILER'] = '' |
| 825 | |
| 826 | # Set HOST_CXX_COMPILER |
| 827 | environ_cp['HOST_CXX_COMPILER'] = host_cxx_compiler |
| 828 | write_action_env_to_bazelrc('HOST_CXX_COMPILER', host_cxx_compiler) |
| 829 | |
| 830 | |
| 831 | def set_host_c_compiler(environ_cp): |
| 832 | """Set HOST_C_COMPILER.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 833 | default_c_host_compiler = which('gcc') or '' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 834 | ask_c_host_compiler = ( |
| 835 | 'Please specify which C compiler should be used as the' |
| 836 | ' host C compiler. [Default is %s]: ') % default_c_host_compiler |
| 837 | |
| 838 | while True: |
| 839 | host_c_compiler = get_from_env_or_user_or_default( |
| 840 | environ_cp, 'HOST_C_COMPILER', ask_c_host_compiler, |
| 841 | default_c_host_compiler) |
| 842 | if os.path.exists(host_c_compiler): |
| 843 | break |
| 844 | |
| 845 | # Reset and retry |
| 846 | print('Invalid C compiler path. %s cannot be found' % host_c_compiler) |
| 847 | environ_cp['HOST_C_COMPILER'] = '' |
| 848 | |
| 849 | # Set HOST_C_COMPILER |
| 850 | environ_cp['HOST_C_COMPILER'] = host_c_compiler |
| 851 | write_action_env_to_bazelrc('HOST_C_COMPILER', host_c_compiler) |
| 852 | |
| 853 | |
| 854 | def set_computecpp_toolkit_path(environ_cp): |
| 855 | """Set COMPUTECPP_TOOLKIT_PATH.""" |
| 856 | ask_computecpp_toolkit_path = ('Please specify the location where ComputeCpp ' |
| 857 | 'for SYCL %s is installed. [Default is %s]: ' |
| 858 | ) % (_TF_OPENCL_VERSION, |
| 859 | _DEFAULT_COMPUTECPP_TOOLKIT_PATH) |
| 860 | |
| 861 | while True: |
| 862 | computecpp_toolkit_path = get_from_env_or_user_or_default( |
| 863 | environ_cp, 'COMPUTECPP_TOOLKIT_PATH', ask_computecpp_toolkit_path, |
| 864 | _DEFAULT_COMPUTECPP_TOOLKIT_PATH) |
| 865 | if is_linux(): |
| 866 | sycl_rt_lib_path = 'lib/libComputeCpp.so' |
| 867 | else: |
| 868 | sycl_rt_lib_path = '' |
| 869 | |
| 870 | sycl_rt_lib_path_full = os.path.join(computecpp_toolkit_path, |
| 871 | sycl_rt_lib_path) |
| 872 | if os.path.exists(sycl_rt_lib_path_full): |
| 873 | break |
| 874 | |
| 875 | print('Invalid SYCL %s library path. %s cannot be found' % |
| 876 | (_TF_OPENCL_VERSION, sycl_rt_lib_path_full)) |
| 877 | environ_cp['COMPUTECPP_TOOLKIT_PATH'] = '' |
| 878 | |
| 879 | # Set COMPUTECPP_TOOLKIT_PATH |
| 880 | environ_cp['COMPUTECPP_TOOLKIT_PATH'] = computecpp_toolkit_path |
| 881 | write_action_env_to_bazelrc('COMPUTECPP_TOOLKIT_PATH', |
| 882 | computecpp_toolkit_path) |
| 883 | |
| 884 | |
| 885 | def set_mpi_home(environ_cp): |
| 886 | """Set MPI_HOME.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 887 | default_mpi_home = which('mpirun') or which('mpiexec') or '' |
| 888 | default_mpi_home = os.path.dirname(os.path.dirname(default_mpi_home)) |
| 889 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 890 | ask_mpi_home = ('Please specify the MPI toolkit folder. [Default is %s]: ' |
| 891 | ) % default_mpi_home |
| 892 | while True: |
| 893 | mpi_home = get_from_env_or_user_or_default(environ_cp, 'MPI_HOME', |
| 894 | ask_mpi_home, default_mpi_home) |
| 895 | |
| 896 | if os.path.exists(os.path.join(mpi_home, 'include')) and os.path.exists( |
| 897 | os.path.join(mpi_home, 'lib')): |
| 898 | break |
| 899 | |
| 900 | print('Invalid path to the MPI Toolkit. %s or %s cannot be found' % |
| 901 | (os.path.join(mpi_home, 'include'), |
| 902 | os.path.exists(os.path.join(mpi_home, 'lib')))) |
| 903 | environ_cp['MPI_HOME'] = '' |
| 904 | |
| 905 | # Set MPI_HOME |
| 906 | environ_cp['MPI_HOME'] = str(mpi_home) |
| 907 | |
| 908 | |
| 909 | def set_other_mpi_vars(environ_cp): |
| 910 | """Set other MPI related variables.""" |
| 911 | # Link the MPI header files |
| 912 | mpi_home = environ_cp.get('MPI_HOME') |
| 913 | symlink_force('%s/include/mpi.h' % mpi_home, 'third_party/mpi/mpi.h') |
| 914 | |
| 915 | # Determine if we use OpenMPI or MVAPICH, these require different header files |
| 916 | # to be included here to make bazel dependency checker happy |
| 917 | if os.path.exists(os.path.join(mpi_home, 'include/mpi_portable_platform.h')): |
| 918 | symlink_force( |
| 919 | os.path.join(mpi_home, 'include/mpi_portable_platform.h'), |
| 920 | 'third_party/mpi/mpi_portable_platform.h') |
| 921 | # TODO(gunan): avoid editing files in configure |
| 922 | sed_in_place('third_party/mpi/mpi.bzl', 'MPI_LIB_IS_OPENMPI=False', |
| 923 | 'MPI_LIB_IS_OPENMPI=True') |
| 924 | else: |
| 925 | # MVAPICH / MPICH |
| 926 | symlink_force( |
| 927 | os.path.join(mpi_home, 'include/mpio.h'), 'third_party/mpi/mpio.h') |
| 928 | symlink_force( |
| 929 | os.path.join(mpi_home, 'include/mpicxx.h'), 'third_party/mpi/mpicxx.h') |
| 930 | # TODO(gunan): avoid editing files in configure |
| 931 | sed_in_place('third_party/mpi/mpi.bzl', 'MPI_LIB_IS_OPENMPI=True', |
| 932 | 'MPI_LIB_IS_OPENMPI=False') |
| 933 | |
| 934 | if os.path.exists(os.path.join(mpi_home, 'lib/libmpi.so')): |
| 935 | symlink_force( |
| 936 | os.path.join(mpi_home, 'lib/libmpi.so'), 'third_party/mpi/libmpi.so') |
| 937 | else: |
| 938 | raise ValueError('Cannot find the MPI library file in %s/lib' % mpi_home) |
| 939 | |
| 940 | |
| 941 | def set_mkl(): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 942 | write_to_bazelrc('build:mkl --define using_mkl=true') |
| 943 | write_to_bazelrc('build:mkl -c opt') |
| 944 | write_to_bazelrc('build:mkl --copt="-DEIGEN_USE_VML"') |
| 945 | print( |
| 946 | 'Add "--config=mkl" to your bazel command to build with MKL ' |
| 947 | 'support.\nPlease note that MKL on MacOS or windows is still not ' |
| 948 | 'supported.\nIf you would like to use a local MKL instead of ' |
| 949 | 'downloading, please set the environment variable \"TF_MKL_ROOT\" every ' |
| 950 | 'time before build.') |
| 951 | |
| 952 | |
Allen Lavoie | 5c7f9e3 | 2017-09-21 11:29:45 -0700 | [diff] [blame] | 953 | def set_monolithic(): |
| 954 | # Add --config=monolithic to your bazel command to use a mostly-static |
| 955 | # build and disable modular op registration support (this will revert to |
| 956 | # loading TensorFlow with RTLD_GLOBAL in Python). By default (without |
| 957 | # --config=monolithic), TensorFlow will build with a dependence on |
| 958 | # //tensorflow:libtensorflow_framework.so. |
| 959 | write_to_bazelrc('build:monolithic --define framework_shared_object=false') |
| 960 | # For projects which use TensorFlow as part of a Bazel build process, putting |
| 961 | # nothing in a bazelrc will default to a monolithic build. The following line |
| 962 | # opts in to modular op registration support by default: |
| 963 | write_to_bazelrc('build --define framework_shared_object=true') |
| 964 | |
| 965 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 966 | def main(): |
| 967 | # Make a copy of os.environ to be clear when functions and getting and setting |
| 968 | # environment variables. |
| 969 | environ_cp = dict(os.environ) |
| 970 | |
Gunhan Gulsoy | ed89a2b | 2017-09-19 18:36:26 -0700 | [diff] [blame] | 971 | check_bazel_version('0.5.4') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 972 | |
| 973 | reset_tf_configure_bazelrc() |
| 974 | cleanup_makefile() |
Gunhan Gulsoy | ed89a2b | 2017-09-19 18:36:26 -0700 | [diff] [blame] | 975 | setup_python(environ_cp) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 976 | run_gen_git_source(environ_cp) |
| 977 | |
| 978 | if is_windows(): |
| 979 | environ_cp['TF_NEED_GCP'] = '0' |
| 980 | environ_cp['TF_NEED_HDFS'] = '0' |
| 981 | environ_cp['TF_NEED_JEMALLOC'] = '0' |
| 982 | environ_cp['TF_NEED_OPENCL'] = '0' |
| 983 | environ_cp['TF_CUDA_CLANG'] = '0' |
| 984 | |
| 985 | if is_macos(): |
| 986 | environ_cp['TF_NEED_JEMALLOC'] = '0' |
| 987 | |
| 988 | set_build_var(environ_cp, 'TF_NEED_JEMALLOC', 'jemalloc as malloc', |
| 989 | 'with_jemalloc', True) |
| 990 | set_build_var(environ_cp, 'TF_NEED_GCP', 'Google Cloud Platform', |
Michael Case | 98850a5 | 2017-09-14 13:35:57 -0700 | [diff] [blame] | 991 | 'with_gcp_support', False, 'gcp') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 992 | set_build_var(environ_cp, 'TF_NEED_HDFS', 'Hadoop File System', |
Michael Case | 98850a5 | 2017-09-14 13:35:57 -0700 | [diff] [blame] | 993 | 'with_hdfs_support', False, 'hdfs') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 994 | 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] | 995 | False, 'xla') |
A. Unique TensorFlower | 28ce1d1 | 2017-08-15 12:08:29 -0700 | [diff] [blame] | 996 | set_build_var(environ_cp, 'TF_NEED_GDR', 'GDR', 'with_gdr_support', |
Michael Case | 98850a5 | 2017-09-14 13:35:57 -0700 | [diff] [blame] | 997 | False, 'gdr') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 998 | set_build_var(environ_cp, 'TF_NEED_VERBS', 'VERBS', 'with_verbs_support', |
Michael Case | 98850a5 | 2017-09-14 13:35:57 -0700 | [diff] [blame] | 999 | False, 'verbs') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1000 | |
| 1001 | set_action_env_var(environ_cp, 'TF_NEED_OPENCL', 'OpenCL', False) |
| 1002 | if environ_cp.get('TF_NEED_OPENCL') == '1': |
| 1003 | set_host_cxx_compiler(environ_cp) |
| 1004 | set_host_c_compiler(environ_cp) |
| 1005 | set_computecpp_toolkit_path(environ_cp) |
| 1006 | |
| 1007 | set_action_env_var(environ_cp, 'TF_NEED_CUDA', 'CUDA', False) |
A. Unique TensorFlower | 24cbb2a | 2017-09-08 07:45:44 -0700 | [diff] [blame] | 1008 | if (environ_cp.get('TF_NEED_CUDA') == '1' and |
| 1009 | 'TF_CUDA_CONFIG_REPO' not in environ_cp): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1010 | set_tf_cuda_version(environ_cp) |
| 1011 | set_tf_cunn_version(environ_cp) |
| 1012 | set_tf_cuda_compute_capabilities(environ_cp) |
| 1013 | |
| 1014 | set_tf_cuda_clang(environ_cp) |
| 1015 | if environ_cp.get('TF_CUDA_CLANG') == '1': |
| 1016 | # Set up which clang we should use as the cuda / host compiler. |
| 1017 | set_clang_cuda_compiler_path(environ_cp) |
| 1018 | else: |
| 1019 | # Set up which gcc nvcc should use as the host compiler |
| 1020 | # No need to set this on Windows |
| 1021 | if not is_windows(): |
| 1022 | set_gcc_host_compiler_path(environ_cp) |
| 1023 | set_other_cuda_vars(environ_cp) |
| 1024 | |
| 1025 | set_build_var(environ_cp, 'TF_NEED_MPI', 'MPI', 'with_mpi_support', False) |
| 1026 | if environ_cp.get('TF_NEED_MPI') == '1': |
| 1027 | set_mpi_home(environ_cp) |
| 1028 | set_other_mpi_vars(environ_cp) |
| 1029 | |
| 1030 | set_cc_opt_flags(environ_cp) |
| 1031 | set_mkl() |
Allen Lavoie | 5c7f9e3 | 2017-09-21 11:29:45 -0700 | [diff] [blame] | 1032 | set_monolithic() |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1033 | |
| 1034 | |
| 1035 | if __name__ == '__main__': |
| 1036 | main() |