Skip to content

tensorflow: streamline the installation rules a bit #30047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 54 additions & 56 deletions utils/swift_build_support/swift_build_support/products/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,27 @@ def install(self, host_target):


# SWIFT_ENABLE_TENSORFLOW
def _get_tensorflow_library(host):
if host.startswith('macosx'):
return ('libtensorflow.2.1.0.dylib', 'libtensorflow.dylib')

if host.startswith('linux'):
return ('libtensorflow.so.2.1.0', 'libtensorflow.so')

raise RuntimeError('unknown host target {}'.format(host))

def _silenced(op):
def inner(*args, **kwargs):
try:
return op(*args, **kwargs)
except OSError:
pass
return inner

def _symlink(dest, src):
_silenced(os.unlink)(src)
os.symlink(dest, src)

class TensorFlow(product.Product):
@classmethod
def product_source_name(cls):
Expand All @@ -121,29 +142,14 @@ def is_build_script_impl_product(cls):
def should_build(self, host_target):
return self.args.build_tensorflow_swift_apis

def _get_tensorflow_library(self, host):
if host.startswith('macosx'):
return ('libtensorflow.2.1.0.dylib', 'libtensorflow.dylib')

if host.startswith('linux'):
return ('libtensorflow.so.2.1.0', 'libtensorflow.so')

raise RuntimeError('unknown host target {}'.format(host))

def _symlink(self, dest, src):
try:
os.unlink(src)
except OSError:
pass
os.symlink(dest, src)

def build(self, host_target):
with shell.pushd(self.source_dir):
# Run the TensorFlow configure script: `yes "" | ./configure`.
# NOTE: consider rewriting `subprocess` API usages using `shell`
# APIs.
yes_process = subprocess.Popen(['yes', ''], stdout=subprocess.PIPE)
subprocess.check_call(['./configure'], stdin=yes_process.stdout)
subprocess.check_call([os.path.join(self.source_dir, 'configure')],
stdin=yes_process.stdout)
yes_process.terminate()

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

# NOTE: ignore the race condition here ....
try:
os.unlink(os.path.join(self.source_dir, 'bazel-bin', 'tensorflow',
unsuffixed_lib_name))
except OSError:
pass
os.symlink(os.path.join(self.source_dir, 'bazel-bin', 'tensorflow',
suffixed_lib_name),
os.path.join(self.source_dir, 'bazel-bin', 'tensorflow',
unsuffixed_lib_name))
_symlink(os.path.join(self.source_dir, 'bazel-bin', 'tensorflow',
suffixed_lib_name),
os.path.join(self.source_dir, 'bazel-bin', 'tensorflow',
unsuffixed_lib_name))

def should_test(self, host_target):
return False
Expand All @@ -184,7 +185,7 @@ def should_install(self, host_target):

def install(self, host_target):
(suffixed_lib_name, unsuffixed_lib_name) = \
self._get_tensorflow_library(host_target)
_get_tensorflow_library(host_target)

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

try:
os.unlink(os.path.join(self.install_toolchain_path(),
'usr', 'lib', 'swift',
subdir, suffixed_lib_name))
os.makedirs(os.path.join(self.install_toolchain_path(),
'usr', 'lib', 'swift', subdir))
except OSError:
pass
_silenced(os.unlink)(os.path.join(self.install_toolchain_path(),
'usr', 'lib', 'swift', subdir,
suffixed_lib_name))
_silenced(os.makedirs)(os.path.join(self.install_toolchain_path(),
'usr', 'lib', 'swift', subdir))
shutil.copy(os.path.join(self.source_dir, 'bazel-bin', 'tensorflow',
suffixed_lib_name),
os.path.join(self.install_toolchain_path(),
'usr', 'lib', 'swift',
subdir, suffixed_lib_name))
# Add write permissions to `libtensorflow.{dylib,so}`. This is required
# for symbol stripping and code signing.
os.chmod(os.path.join(self.install_toolchain_path(),
'usr', 'lib', 'swift', subdir, suffixed_lib_name),
0o755)

if host_target.startswith('linux'):
versions = (
Expand All @@ -218,27 +221,22 @@ def install(self, host_target):
)

for (index, value) in enumerate(versions[:-1]):
self._symlink(value,
os.path.join(self.install_toolchain_path(),
'usr', 'lib', 'swift', subdir,
versions[index + 1]))
_symlink(value,
os.path.join(self.install_toolchain_path(),
'usr', 'lib', 'swift', subdir,
versions[index + 1]))
else:
self._symlink(suffixed_lib_name,
os.path.join(self.install_toolchain_path(),
'usr', 'lib', 'swift', subdir,
unsuffixed_lib_name))

try:
shutil.rmtree(os.path.join(self.install_toolchain_path(),
'usr', 'lib', 'swift', 'tensorflow'))
except OSError:
pass
try:
os.makedirs(os.path.join(self.install_toolchain_path(),
'usr', 'lib', 'swift', 'tensorflow', 'c',
'eager'))
except OSError:
pass
_symlink(suffixed_lib_name,
os.path.join(self.install_toolchain_path(),
'usr', 'lib', 'swift', subdir,
unsuffixed_lib_name))

_silenced(shutil.rmtree)(os.path.join(self.install_toolchain_path(),
'usr', 'lib', 'swift',
'tensorflow'))
_silenced(os.makedirs)(os.path.join(self.install_toolchain_path(),
'usr', 'lib', 'swift', 'tensorflow',
'c', 'eager'))
for header in (
'c_api.h',
'c_api_experimental.h',
Expand Down