Home | History | Annotate | Download | only in posix
      1 /* Copyright 2015 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 
     16 #include "tensorflow/core/platform/load_library.h"
     17 
     18 #include <dlfcn.h>
     19 
     20 #include "tensorflow/core/lib/core/errors.h"
     21 
     22 namespace tensorflow {
     23 
     24 namespace internal {
     25 
     26 Status LoadLibrary(const char* library_filename, void** handle) {
     27   *handle = dlopen(library_filename, RTLD_NOW | RTLD_LOCAL);
     28   if (!*handle) {
     29     return errors::NotFound(dlerror());
     30   }
     31   return Status::OK();
     32 }
     33 
     34 Status GetSymbolFromLibrary(void* handle, const char* symbol_name,
     35                             void** symbol) {
     36   *symbol = dlsym(handle, symbol_name);
     37   if (!*symbol) {
     38     return errors::NotFound(dlerror());
     39   }
     40   return Status::OK();
     41 }
     42 
     43 string FormatLibraryFileName(const string& name, const string& version) {
     44   string filename;
     45 #if defined(__APPLE__)
     46   if (version.size() == 0) {
     47     filename = "lib" + name + ".dylib";
     48   } else {
     49     filename = "lib" + name + "." + version + ".dylib";
     50   }
     51 #else
     52   if (version.empty()) {
     53     filename = "lib" + name + ".so";
     54   } else {
     55     filename = "lib" + name + ".so" + "." + version;
     56   }
     57 #endif
     58   return filename;
     59 }
     60 
     61 }  // namespace internal
     62 
     63 }  // namespace tensorflow
     64