Skip to content

Commit 671baf0

Browse files
Michael Casegunan
Michael Case
authored andcommitted
Make configure script runnable from external workspace. (tensorflow#17172)
To run from external workspace, you should now be able to invoke script like the following. This will generate some TensorFlow specfic bazel options and import them into your project's .bazelrc. $(bazel info output_base)/external/org_tensorflow/configure.py --workspace=$(PWD)
1 parent 3d68260 commit 671baf0

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

configure

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ if [ -z "$PYTHON_BIN_PATH" ]; then
88
fi
99

1010
# Set all env variables
11-
"$PYTHON_BIN_PATH" configure.py
11+
CONFIGURE_DIR=$(dirname "$0")
12+
"$PYTHON_BIN_PATH" "${CONFIGURE_DIR}/configure.py" "$@"
1213

1314
echo "Configuration finished"
1415

configure.py

+30-35
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from __future__ import division
1919
from __future__ import print_function
2020

21+
import argparse
2122
import errno
2223
import os
2324
import platform
@@ -32,10 +33,6 @@
3233
from distutils.spawn import find_executable as which
3334
# pylint: enable=g-import-not-at-top
3435

35-
_TF_BAZELRC = os.path.join(os.path.dirname(os.path.abspath(__file__)),
36-
'.tf_configure.bazelrc')
37-
_TF_WORKSPACE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
38-
'WORKSPACE')
3936
_DEFAULT_CUDA_VERSION = '9.0'
4037
_DEFAULT_CUDNN_VERSION = '7'
4138
_DEFAULT_CUDA_COMPUTE_CAPABILITIES = '3.5,5.2'
@@ -51,6 +48,11 @@
5148

5249
_DEFAULT_PROMPT_ASK_ATTEMPTS = 10
5350

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+
5456

5557
class UserInputError(Exception):
5658
pass
@@ -119,22 +121,6 @@ def sed_in_place(filename, old, new):
119121
f.write(newdata)
120122

121123

122-
def remove_line_with(filename, token):
123-
"""Remove lines that contain token from file.
124-
125-
Args:
126-
filename: string for filename.
127-
token: string token to check if to remove a line from file or not.
128-
"""
129-
with open(filename, 'r') as f:
130-
filedata = f.read()
131-
132-
with open(filename, 'w') as f:
133-
for line in filedata.strip().split('\n'):
134-
if token not in line:
135-
f.write(line + '\n')
136-
137-
138124
def write_to_bazelrc(line):
139125
with open(_TF_BAZELRC, 'a') as f:
140126
f.write(line + '\n')
@@ -245,33 +231,35 @@ def setup_python(environ_cp):
245231
environ_cp['PYTHON_BIN_PATH'] = python_bin_path
246232

247233
# Write tools/python_bin_path.sh
248-
with open('tools/python_bin_path.sh', 'w') as f:
234+
with open(os.path.join(
235+
_TF_WORKSPACE_ROOT, 'tools', 'python_bin_path.sh'), 'w') as f:
249236
f.write('export PYTHON_BIN_PATH="%s"' % python_bin_path)
250237

251238

252-
def reset_tf_configure_bazelrc():
239+
def reset_tf_configure_bazelrc(workspace_path):
253240
"""Reset file that contains customized config settings."""
254241
open(_TF_BAZELRC, 'w').close()
242+
bazelrc_path = os.path.join(workspace_path, '.bazelrc')
255243

256-
home = os.path.expanduser('~')
257-
if not os.path.exists('.bazelrc'):
258-
if os.path.exists(os.path.join(home, '.bazelrc')):
259-
with open('.bazelrc', 'a') as f:
260-
f.write('import %s/.bazelrc\n' % home.replace('\\', '/'))
261-
else:
262-
open('.bazelrc', 'w').close()
263-
264-
remove_line_with('.bazelrc', 'tf_configure')
265-
with open('.bazelrc', 'a') as f:
266-
f.write('import %workspace%/.tf_configure.bazelrc\n')
244+
data = []
245+
if os.path.exists(bazelrc_path):
246+
with open(bazelrc_path, 'r') as f:
247+
data = f.read().splitlines()
248+
with open(bazelrc_path, 'w') as f:
249+
for l in data:
250+
if _TF_BAZELRC_FILENAME in l:
251+
continue
252+
f.write('%s\n' % l)
253+
f.write('import %s\n' % _TF_BAZELRC)
267254

268255

269256
def cleanup_makefile():
270257
"""Delete any leftover BUILD files from the Makefile build.
271258
272259
These files could interfere with Bazel parsing.
273260
"""
274-
makefile_download_dir = 'tensorflow/contrib/makefile/downloads'
261+
makefile_download_dir = os.path.join(
262+
_TF_WORKSPACE_ROOT, 'tensorflow', 'contrib', 'makefile', 'downloads')
275263
if os.path.isdir(makefile_download_dir):
276264
for root, _, filenames in os.walk(makefile_download_dir):
277265
for f in filenames:
@@ -1373,13 +1361,20 @@ def config_info_line(name, help_text):
13731361

13741362

13751363
def main():
1364+
parser = argparse.ArgumentParser()
1365+
parser.add_argument("--workspace",
1366+
type=str,
1367+
default=_TF_WORKSPACE_ROOT,
1368+
help="The absolute path to your active Bazel workspace.")
1369+
args = parser.parse_args()
1370+
13761371
# Make a copy of os.environ to be clear when functions and getting and setting
13771372
# environment variables.
13781373
environ_cp = dict(os.environ)
13791374

13801375
check_bazel_version('0.5.4')
13811376

1382-
reset_tf_configure_bazelrc()
1377+
reset_tf_configure_bazelrc(args.workspace)
13831378
cleanup_makefile()
13841379
setup_python(environ_cp)
13851380

0 commit comments

Comments
 (0)