Skip to content

Commit ea3475b

Browse files
committed
tensorflow: streamline the installation rules a bit
- Use a decorator to silence the failure of the file system operation. - Ensure that we set the correct permissions on the installed library. - Use an absolute path to `configure` for tensorflow as this fails to build on some machines otherwise.
1 parent b023e03 commit ea3475b

File tree

1 file changed

+54
-56
lines changed
  • utils/swift_build_support/swift_build_support/products

1 file changed

+54
-56
lines changed

utils/swift_build_support/swift_build_support/products/tensorflow.py

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,27 @@ def install(self, host_target):
106106

107107

108108
# 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+
109130
class TensorFlow(product.Product):
110131
@classmethod
111132
def product_source_name(cls):
@@ -121,29 +142,14 @@ def is_build_script_impl_product(cls):
121142
def should_build(self, host_target):
122143
return self.args.build_tensorflow_swift_apis
123144

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-
140145
def build(self, host_target):
141146
with shell.pushd(self.source_dir):
142147
# Run the TensorFlow configure script: `yes "" | ./configure`.
143148
# NOTE: consider rewriting `subprocess` API usages using `shell`
144149
# APIs.
145150
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)
147153
yes_process.terminate()
148154

149155
# Build TensorFlow via bazel.
@@ -160,18 +166,13 @@ def build(self, host_target):
160166
# Create a symlink to the standard unsuffixed library name:
161167
# "libtensorflow.{dylib,so}".
162168
(suffixed_lib_name, unsuffixed_lib_name) = \
163-
self._get_tensorflow_library(host_target)
169+
_get_tensorflow_library(host_target)
164170

165171
# 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))
175176

176177
def should_test(self, host_target):
177178
return False
@@ -184,7 +185,7 @@ def should_install(self, host_target):
184185

185186
def install(self, host_target):
186187
(suffixed_lib_name, unsuffixed_lib_name) = \
187-
self._get_tensorflow_library(host_target)
188+
_get_tensorflow_library(host_target)
188189

189190
subdir = None
190191
if host_target.startswith('macsox'):
@@ -195,19 +196,21 @@ def install(self, host_target):
195196
if not subdir:
196197
raise RuntimeError('unknown host target {}'.format(host_target))
197198

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))
206204
shutil.copy(os.path.join(self.source_dir, 'bazel-bin', 'tensorflow',
207205
suffixed_lib_name),
208206
os.path.join(self.install_toolchain_path(),
209207
'usr', 'lib', 'swift',
210208
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)
211214

212215
if host_target.startswith('linux'):
213216
versions = (
@@ -218,27 +221,22 @@ def install(self, host_target):
218221
)
219222

220223
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]))
225228
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'))
242240
for header in (
243241
'c_api.h',
244242
'c_api_experimental.h',

0 commit comments

Comments
 (0)