1# Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================= 15"""If possible, exports all symbols with RTLD_GLOBAL. 16 17Note that this file is only imported by pywrap_tensorflow.py if this is a static 18build (meaning there is no explicit framework cc_binary shared object dependency 19of _pywrap_tensorflow_internal.so). For regular (non-static) builds, RTLD_GLOBAL 20is not necessary, since the dynamic dependencies of custom/contrib ops are 21explicit. 22""" 23 24from __future__ import absolute_import 25from __future__ import division 26from __future__ import print_function 27 28import ctypes 29import sys 30 31# On UNIX-based platforms, pywrap_tensorflow is a SWIG-generated python library 32# that dynamically loads _pywrap_tensorflow.so. The default mode for loading 33# keeps all the symbol private and not visible to other libraries that may be 34# loaded. Setting the mode to RTLD_GLOBAL to make the symbols visible, so that 35# custom op libraries imported using `tf.load_op_library()` can access symbols 36# defined in _pywrap_tensorflow.so. 37_use_rtld_global = (hasattr(sys, 'getdlopenflags') 38 and hasattr(sys, 'setdlopenflags')) 39if _use_rtld_global: 40 _default_dlopen_flags = sys.getdlopenflags() 41 42 43def set_dlopen_flags(): 44 if _use_rtld_global: 45 sys.setdlopenflags(_default_dlopen_flags | ctypes.RTLD_GLOBAL) 46 47 48def reset_dlopen_flags(): 49 if _use_rtld_global: 50 sys.setdlopenflags(_default_dlopen_flags) 51