25
25
import subprocess
26
26
import sys
27
27
28
+ try :
29
+ from shutil import which
30
+ except ImportError :
31
+ from distutils .spawn import find_executable as which
32
+
28
33
_TF_BAZELRC = '.tf_configure.bazelrc'
29
34
_DEFAULT_CUDA_VERSION = '8.0'
30
35
_DEFAULT_CUDNN_VERSION = '6'
@@ -53,6 +58,10 @@ def is_ppc64le():
53
58
return platform .machine () == 'ppc64le'
54
59
55
60
61
+ def is_cygwin ():
62
+ return platform .system ().startswith ('CYGWIN_NT' )
63
+
64
+
56
65
def get_input (question ):
57
66
try :
58
67
try :
@@ -121,13 +130,20 @@ def write_action_env_to_bazelrc(var_name, var):
121
130
write_to_bazelrc ('build --action_env %s="%s"' % (var_name , str (var )))
122
131
123
132
124
- def run_shell (cmd ):
125
- return subprocess .check_output (cmd , shell = True ).decode ('UTF-8' ).strip ()
133
+ def run_shell (cmd , allow_non_zero = False ):
134
+ if allow_non_zero :
135
+ try :
136
+ output = subprocess .check_output (cmd )
137
+ except subprocess .CalledProcessError as e :
138
+ output = e .output
139
+ else :
140
+ output = subprocess .check_output (cmd )
141
+ return output .decode ('UTF-8' ).strip ()
126
142
127
143
128
144
def cygpath (path ):
129
145
"""Convert path from posix to windows."""
130
- return run_shell ('cygpath -m "%s"' % path )
146
+ return run_shell ([ 'cygpath' , '-m' , path ] )
131
147
132
148
133
149
def get_python_path (environ_cp , python_bin_path ):
@@ -136,16 +152,14 @@ def get_python_path(environ_cp, python_bin_path):
136
152
if environ_cp .get ('PYTHONPATH' ):
137
153
python_paths = environ_cp .get ('PYTHONPATH' ).split (':' )
138
154
try :
139
- check_input = [python_bin_path , '-c' ,
140
- 'import site; print("\\ n".join(site.getsitepackages()))' ]
141
- library_paths = subprocess .check_output (
142
- check_input ).decode ('UTF-8' ).strip ().split ("\n " )
155
+ library_paths = run_shell (
156
+ [python_bin_path , '-c' ,
157
+ 'import site; print("\\ n".join(site.getsitepackages()))' ]).split ("\n " )
143
158
except subprocess .CalledProcessError :
144
- check_input = [python_bin_path , '-c' ,
145
- 'from distutils.sysconfig import get_python_lib;' +
146
- 'print(get_python_lib())' ]
147
- library_paths = [subprocess .check_output (
148
- check_input ).decode ('UTF-8' ).strip ()]
159
+ library_paths = [run_shell (
160
+ [python_bin_path , '-c' ,
161
+ 'from distutils.sysconfig import get_python_lib;'
162
+ 'print(get_python_lib())' ])]
149
163
150
164
all_paths = set (python_paths + library_paths )
151
165
@@ -158,8 +172,7 @@ def get_python_path(environ_cp, python_bin_path):
158
172
159
173
def get_python_major_version (python_bin_path ):
160
174
"""Get the python major version."""
161
- check_input = [python_bin_path , '-c' , 'import sys; print(sys.version[0])' ]
162
- return subprocess .check_output (check_input ).decode ('UTF-8' ).strip ()
175
+ return run_shell ([python_bin_path , '-c' , 'import sys; print(sys.version[0])' ])
163
176
164
177
165
178
def setup_python (environ_cp , bazel_version ):
@@ -173,8 +186,8 @@ def setup_python(environ_cp, bazel_version):
173
186
environ_cp , 'PYTHON_BIN_PATH' , ask_python_bin_path ,
174
187
default_python_bin_path )
175
188
# Check if the path is valid
176
- if ( os .path .isfile (python_bin_path ) and os .access (
177
- python_bin_path , os .X_OK )) or ( os . path . isdir ( python_bin_path )) :
189
+ if os .path .isfile (python_bin_path ) and os .access (
190
+ python_bin_path , os .X_OK ):
178
191
break
179
192
elif not os .path .exists (python_bin_path ):
180
193
print ('Invalid python path: %s cannot be found.' % python_bin_path )
@@ -183,7 +196,7 @@ def setup_python(environ_cp, bazel_version):
183
196
environ_cp ['PYTHON_BIN_PATH' ] = ''
184
197
185
198
# Convert python path to Windows style before checking lib and version
186
- if is_windows ():
199
+ if is_cygwin ():
187
200
python_bin_path = cygpath (python_bin_path )
188
201
189
202
# Get PYTHON_LIB_PATH
@@ -193,20 +206,20 @@ def setup_python(environ_cp, bazel_version):
193
206
if environ_cp .get ('USE_DEFAULT_PYTHON_LIB_PATH' ) == '1' :
194
207
python_lib_path = python_lib_paths [0 ]
195
208
else :
196
- print ('Found possible Python library paths:\n %s' %
197
- '\n ' .join (python_lib_paths ))
209
+ print ('Found possible Python library paths:\n %s' %
210
+ '\n ' .join (python_lib_paths ))
198
211
default_python_lib_path = python_lib_paths [0 ]
199
212
python_lib_path = get_input (
200
- 'Please input the desired Python library path to use. Default is %s '
201
- % python_lib_paths [0 ])
213
+ 'Please input the desired Python library path to use. '
214
+ 'Default is [%s] \n ' % python_lib_paths [0 ])
202
215
if not python_lib_path :
203
216
python_lib_path = default_python_lib_path
204
217
environ_cp ['PYTHON_LIB_PATH' ] = python_lib_path
205
218
206
219
python_major_version = get_python_major_version (python_bin_path )
207
220
208
221
# Convert python path to Windows style before writing into bazel.rc
209
- if is_windows ():
222
+ if is_cygwin ():
210
223
python_lib_path = cygpath (python_lib_path )
211
224
212
225
# Set-up env variables used by python_configure.bzl
@@ -428,11 +441,10 @@ def check_bazel_version(min_version):
428
441
Returns:
429
442
The bazel version detected.
430
443
"""
431
- try :
432
- curr_version = run_shell ('bazel --batch version' )
433
- except subprocess .CalledProcessError :
444
+ if which ('bazel' ) is None :
434
445
print ('Cannot find bazel. Please install bazel.' )
435
446
sys .exit (0 )
447
+ curr_version = run_shell (['bazel' , '--batch' , 'version' ])
436
448
437
449
for line in curr_version .split ('\n ' ):
438
450
if 'Build label: ' in line :
@@ -524,7 +536,7 @@ def get_from_env_or_user_or_default(environ_cp, var_name, ask_for_var,
524
536
525
537
def set_clang_cuda_compiler_path (environ_cp ):
526
538
"""Set CLANG_CUDA_COMPILER_PATH."""
527
- default_clang_path = run_shell ( 'which clang || true' )
539
+ default_clang_path = which ( ' clang' ) or ''
528
540
ask_clang_path = ('Please specify which clang should be used as device and '
529
541
'host compiler. [Default is %s]: ' ) % default_clang_path
530
542
@@ -547,12 +559,12 @@ def set_clang_cuda_compiler_path(environ_cp):
547
559
548
560
def set_gcc_host_compiler_path (environ_cp ):
549
561
"""Set GCC_HOST_COMPILER_PATH."""
550
- default_gcc_host_compiler_path = run_shell ( 'which gcc || true' )
562
+ default_gcc_host_compiler_path = which ( ' gcc' ) or ''
551
563
cuda_bin_symlink = '%s/bin/gcc' % environ_cp .get ('CUDA_TOOLKIT_PATH' )
552
564
553
565
if os .path .islink (cuda_bin_symlink ):
554
566
# os.readlink is only available in linux
555
- default_gcc_host_compiler_path = run_shell ( 'readlink %s' % cuda_bin_symlink )
567
+ default_gcc_host_compiler_path = os . path . realpath ( cuda_bin_symlink )
556
568
557
569
ask_gcc_path = (
558
570
'Please specify which gcc should be used by nvcc as the '
@@ -587,7 +599,7 @@ def set_tf_cuda_version(environ_cp):
587
599
588
600
# Find out where the CUDA toolkit is installed
589
601
default_cuda_path = _DEFAULT_CUDA_PATH
590
- if is_windows ():
602
+ if is_cygwin ():
591
603
default_cuda_path = cygpath (
592
604
environ_cp .get ('CUDA_PATH' , _DEFAULT_CUDA_PATH_WIN ))
593
605
elif is_linux ():
@@ -628,7 +640,7 @@ def set_tf_cuda_version(environ_cp):
628
640
def set_tf_cunn_version (environ_cp ):
629
641
"""Set CUDNN_INSTALL_PATH and TF_CUDNN_VERSION."""
630
642
ask_cudnn_version = (
631
- '" Please specify the cuDNN version you want to use. '
643
+ 'Please specify the cuDNN version you want to use. '
632
644
'[Leave empty to default to cuDNN %s.0]: ' ) % _DEFAULT_CUDNN_VERSION
633
645
634
646
while True :
@@ -647,7 +659,7 @@ def set_tf_cunn_version(environ_cp):
647
659
# unusable. Going through one more level of expansion to handle that.
648
660
cudnn_install_path = os .path .realpath (
649
661
os .path .expanduser (cudnn_install_path ))
650
- if is_windows ():
662
+ if is_cygwin ():
651
663
cudnn_install_path = cygpath (cudnn_install_path )
652
664
653
665
if is_windows ():
@@ -669,12 +681,10 @@ def set_tf_cunn_version(environ_cp):
669
681
670
682
# Try another alternative for Linux
671
683
if is_linux ():
672
- if subprocess .call (['which' , 'ldconfig' ]):
673
- ldconfig_bin = '/sbin/ldconfig'
674
- else :
675
- ldconfig_bin = 'ldconfig'
676
- cudnn_path_from_ldconfig = run_shell (
677
- r'%s -p | sed -n "s/.*libcudnn.so .* => \(.*\)/\\1/p"' % ldconfig_bin )
684
+ ldconfig_bin = which ('ldconfig' ) or '/sbin/ldconfig'
685
+ cudnn_path_from_ldconfig = run_shell ([ldconfig_bin , '-p' ])
686
+ cudnn_path_from_ldconfig = re .search ('.*libcudnn.so .* => (.*)' ,
687
+ cudnn_path_from_ldconfig ).group (1 )
678
688
if os .path .exists ('%s.%s' % (cudnn_path_from_ldconfig , tf_cudnn_version )):
679
689
cudnn_install_path = os .path .dirname (cudnn_path_from_ldconfig )
680
690
break
@@ -707,11 +717,15 @@ def get_native_cuda_compute_capabilities(environ_cp):
707
717
"""
708
718
device_query_bin = os .path .join (
709
719
environ_cp .get ('CUDA_TOOLKIT_PATH' ), 'extras/demo_suite/deviceQuery' )
710
- cmd = (r'"%s" | grep "Capability" | grep -o "[0-9]*\.[0-9]*" | sed '
711
- '":a;{N;s/\\ n/,/};ba"' ) % device_query_bin
712
- try :
713
- output = run_shell (cmd )
714
- except subprocess .CalledProcessError :
720
+ if os .path .isfile (device_query_bin ) and os .access (device_query_bin , os .X_OK ):
721
+ try :
722
+ output = run_shell (device_query_bin ).split ('\n ' )
723
+ pattern = re .compile ('[0-9]*\\ .[0-9]*' )
724
+ output = [pattern .search (x ) for x in output if 'Capability' in x ]
725
+ output = ',' .join (x .group () for x in output if x is not None )
726
+ except subprocess .CalledProcessError :
727
+ output = ''
728
+ else :
715
729
output = ''
716
730
return output
717
731
@@ -792,7 +806,7 @@ def set_other_cuda_vars(environ_cp):
792
806
793
807
def set_host_cxx_compiler (environ_cp ):
794
808
"""Set HOST_CXX_COMPILER."""
795
- default_cxx_host_compiler = run_shell ( 'which g++ || true' )
809
+ default_cxx_host_compiler = which ( ' g++' ) or ''
796
810
ask_cxx_host_compiler = (
797
811
'Please specify which C++ compiler should be used as'
798
812
' the host C++ compiler. [Default is %s]: ' ) % default_cxx_host_compiler
@@ -815,7 +829,7 @@ def set_host_cxx_compiler(environ_cp):
815
829
816
830
def set_host_c_compiler (environ_cp ):
817
831
"""Set HOST_C_COMPILER."""
818
- default_c_host_compiler = run_shell ( 'which gcc || true' )
832
+ default_c_host_compiler = which ( ' gcc' ) or ''
819
833
ask_c_host_compiler = (
820
834
'Please specify which C compiler should be used as the'
821
835
' host C compiler. [Default is %s]: ' ) % default_c_host_compiler
@@ -869,9 +883,9 @@ def set_computecpp_toolkit_path(environ_cp):
869
883
870
884
def set_mpi_home (environ_cp ):
871
885
"""Set MPI_HOME."""
872
- cmd = ( 'dirname $(dirname $(which mpirun)) || dirname $(dirname $(which '
873
- 'mpiexec)) || true' )
874
- default_mpi_home = run_shell ( cmd )
886
+ default_mpi_home = which ( ' mpirun' ) or which ( 'mpiexec' ) or ' '
887
+ default_mpi_home = os . path . dirname ( os . path . dirname ( default_mpi_home ) )
888
+
875
889
ask_mpi_home = ('Please specify the MPI toolkit folder. [Default is %s]: '
876
890
) % default_mpi_home
877
891
while True :
0 commit comments