@@ -106,6 +106,27 @@ def install(self, host_target):
106
106
107
107
108
108
# SWIFT_ENABLE_TENSORFLOW
109
+ def _get_tensorflow_library (host ):
110
+ if host .startswith ('macosx' ):
111
+ return ('libtensorflow.2.1.0.dylib' , 'libtensorflow.dylib' )
112
+
113
+ if host .startswith ('linux' ):
114
+ return ('libtensorflow.so.2.1.0' , 'libtensorflow.so' )
115
+
116
+ raise RuntimeError ('unknown host target {}' .format (host ))
117
+
118
+ def _silenced (op ):
119
+ def inner (* args , ** kwargs ):
120
+ try :
121
+ return op (* args , ** kwargs )
122
+ except OSError :
123
+ pass
124
+ return inner
125
+
126
+ def _symlink (dest , src ):
127
+ _silenced (os .unlink )(src )
128
+ os .symlink (dest , src )
129
+
109
130
class TensorFlow (product .Product ):
110
131
@classmethod
111
132
def product_source_name (cls ):
@@ -121,29 +142,14 @@ def is_build_script_impl_product(cls):
121
142
def should_build (self , host_target ):
122
143
return self .args .build_tensorflow_swift_apis
123
144
124
- def _get_tensorflow_library (self , host ):
125
- if host .startswith ('macosx' ):
126
- return ('libtensorflow.2.1.0.dylib' , 'libtensorflow.dylib' )
127
-
128
- if host .startswith ('linux' ):
129
- return ('libtensorflow.so.2.1.0' , 'libtensorflow.so' )
130
-
131
- raise RuntimeError ('unknown host target {}' .format (host ))
132
-
133
- def _symlink (self , dest , src ):
134
- try :
135
- os .unlink (src )
136
- except OSError :
137
- pass
138
- os .symlink (dest , src )
139
-
140
145
def build (self , host_target ):
141
146
with shell .pushd (self .source_dir ):
142
147
# Run the TensorFlow configure script: `yes "" | ./configure`.
143
148
# NOTE: consider rewriting `subprocess` API usages using `shell`
144
149
# APIs.
145
150
yes_process = subprocess .Popen (['yes' , '' ], stdout = subprocess .PIPE )
146
- subprocess .check_call (['./configure' ], stdin = yes_process .stdout )
151
+ subprocess .check_call ([os .path .join (self .source_dir , 'configure' )],
152
+ stdin = yes_process .stdout )
147
153
yes_process .terminate ()
148
154
149
155
# Build TensorFlow via bazel.
@@ -160,18 +166,13 @@ def build(self, host_target):
160
166
# Create a symlink to the standard unsuffixed library name:
161
167
# "libtensorflow.{dylib,so}".
162
168
(suffixed_lib_name , unsuffixed_lib_name ) = \
163
- self . _get_tensorflow_library (host_target )
169
+ _get_tensorflow_library (host_target )
164
170
165
171
# NOTE: ignore the race condition here ....
166
- try :
167
- os .unlink (os .path .join (self .source_dir , 'bazel-bin' , 'tensorflow' ,
168
- unsuffixed_lib_name ))
169
- except OSError :
170
- pass
171
- os .symlink (os .path .join (self .source_dir , 'bazel-bin' , 'tensorflow' ,
172
- suffixed_lib_name ),
173
- os .path .join (self .source_dir , 'bazel-bin' , 'tensorflow' ,
174
- unsuffixed_lib_name ))
172
+ _symlink (os .path .join (self .source_dir , 'bazel-bin' , 'tensorflow' ,
173
+ suffixed_lib_name ),
174
+ os .path .join (self .source_dir , 'bazel-bin' , 'tensorflow' ,
175
+ unsuffixed_lib_name ))
175
176
176
177
def should_test (self , host_target ):
177
178
return False
@@ -184,7 +185,7 @@ def should_install(self, host_target):
184
185
185
186
def install (self , host_target ):
186
187
(suffixed_lib_name , unsuffixed_lib_name ) = \
187
- self . _get_tensorflow_library (host_target )
188
+ _get_tensorflow_library (host_target )
188
189
189
190
subdir = None
190
191
if host_target .startswith ('macsox' ):
@@ -195,19 +196,21 @@ def install(self, host_target):
195
196
if not subdir :
196
197
raise RuntimeError ('unknown host target {}' .format (host_target ))
197
198
198
- try :
199
- os .unlink (os .path .join (self .install_toolchain_path (),
200
- 'usr' , 'lib' , 'swift' ,
201
- subdir , suffixed_lib_name ))
202
- os .makedirs (os .path .join (self .install_toolchain_path (),
203
- 'usr' , 'lib' , 'swift' , subdir ))
204
- except OSError :
205
- pass
199
+ _silenced (os .unlink )(os .path .join (self .install_toolchain_path (),
200
+ 'usr' , 'lib' , 'swift' , subdir ,
201
+ suffixed_lib_name ))
202
+ _silenced (os .makedirs )(os .path .join (self .install_toolchain_path (),
203
+ 'usr' , 'lib' , 'swift' , subdir ))
206
204
shutil .copy (os .path .join (self .source_dir , 'bazel-bin' , 'tensorflow' ,
207
205
suffixed_lib_name ),
208
206
os .path .join (self .install_toolchain_path (),
209
207
'usr' , 'lib' , 'swift' ,
210
208
subdir , suffixed_lib_name ))
209
+ # Add write permissions to `libtensorflow.{dylib,so}`. This is required
210
+ # for symbol stripping and code signing.
211
+ os .chmod (os .path .join (self .install_toolchain_path (),
212
+ 'usr' , 'lib' , 'swift' , subdir , suffixed_lib_name ),
213
+ 0o755 )
211
214
212
215
if host_target .startswith ('linux' ):
213
216
versions = (
@@ -218,27 +221,22 @@ def install(self, host_target):
218
221
)
219
222
220
223
for (index , value ) in enumerate (versions [:- 1 ]):
221
- self . _symlink (value ,
222
- os .path .join (self .install_toolchain_path (),
223
- 'usr' , 'lib' , 'swift' , subdir ,
224
- versions [index + 1 ]))
224
+ _symlink (value ,
225
+ os .path .join (self .install_toolchain_path (),
226
+ 'usr' , 'lib' , 'swift' , subdir ,
227
+ versions [index + 1 ]))
225
228
else :
226
- self ._symlink (suffixed_lib_name ,
227
- os .path .join (self .install_toolchain_path (),
228
- 'usr' , 'lib' , 'swift' , subdir ,
229
- unsuffixed_lib_name ))
230
-
231
- try :
232
- shutil .rmtree (os .path .join (self .install_toolchain_path (),
233
- 'usr' , 'lib' , 'swift' , 'tensorflow' ))
234
- except OSError :
235
- pass
236
- try :
237
- os .makedirs (os .path .join (self .install_toolchain_path (),
238
- 'usr' , 'lib' , 'swift' , 'tensorflow' , 'c' ,
239
- 'eager' ))
240
- except OSError :
241
- pass
229
+ _symlink (suffixed_lib_name ,
230
+ os .path .join (self .install_toolchain_path (),
231
+ 'usr' , 'lib' , 'swift' , subdir ,
232
+ unsuffixed_lib_name ))
233
+
234
+ _silenced (shutil .rmtree )(os .path .join (self .install_toolchain_path (),
235
+ 'usr' , 'lib' , 'swift' ,
236
+ 'tensorflow' ))
237
+ _silenced (os .makedirs )(os .path .join (self .install_toolchain_path (),
238
+ 'usr' , 'lib' , 'swift' , 'tensorflow' ,
239
+ 'c' , 'eager' ))
242
240
for header in (
243
241
'c_api.h' ,
244
242
'c_api_experimental.h' ,
0 commit comments