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 #ifndef TENSORFLOW_CONTRIB_LITE_TOCO_TOCO_PORT_H_ 16 #define TENSORFLOW_CONTRIB_LITE_TOCO_TOCO_PORT_H_ 17 18 // Portability layer for toco tool. Mainly, abstract filesystem access so we 19 // can build and use on google internal environments and on OSX. 20 21 #include <string> 22 #include "google/protobuf/text_format.h" 23 #include "tensorflow/contrib/lite/toco/format_port.h" 24 #include "tensorflow/core/platform/logging.h" 25 #include "tensorflow/core/platform/platform.h" 26 #if defined(PLATFORM_GOOGLE) 27 #include "absl/strings/cord.h" 28 #endif // PLATFORM_GOOGLE 29 30 #ifdef PLATFORM_GOOGLE 31 #define TFLITE_PROTO_NS proto2 32 #else 33 #define TFLITE_PROTO_NS google::protobuf 34 #endif 35 36 namespace toco { 37 namespace port { 38 39 class Status { 40 public: 41 Status() {} 42 43 Status(bool ok, const string& message) : ok_(ok), message_(message) {} 44 45 bool ok() const { return ok_; } 46 47 const string error_message() const { return message_; } 48 49 private: 50 bool ok_ = false; 51 string message_; 52 }; 53 54 void InitGoogle(const char* usage, int* argc, char*** argv, bool remove_flags); 55 void CheckInitGoogleIsDone(const char* message); 56 57 namespace file { 58 class Options {}; 59 inline Options Defaults() { 60 Options o; 61 return o; 62 } 63 Status GetContents(const string& filename, string* contents, 64 const Options& options); 65 Status SetContents(const string& filename, const string& contents, 66 const Options& options); 67 string JoinPath(const string& base, const string& filename); 68 Status Writable(const string& filename); 69 Status Readable(const string& filename, const Options& options); 70 Status Exists(const string& filename, const Options& options); 71 } // namespace file 72 73 // Copy `src` string to `dest`. User must ensure `dest` has enough space. 74 #if defined(PLATFORM_GOOGLE) 75 void CopyToBuffer(const ::Cord& src, char* dest); 76 #endif // PLATFORM_GOOGLE 77 void CopyToBuffer(const string& src, char* dest); 78 } // namespace port 79 80 inline bool ParseFromStringOverload(const std::string& in, 81 TFLITE_PROTO_NS::Message* proto) { 82 return TFLITE_PROTO_NS::TextFormat::ParseFromString(in, proto); 83 } 84 85 template <typename Proto> 86 bool ParseFromStringEitherTextOrBinary(const std::string& input_file_contents, 87 Proto* proto) { 88 if (proto->ParseFromString(input_file_contents)) { 89 return true; 90 } 91 92 if (ParseFromStringOverload(input_file_contents, proto)) { 93 return true; 94 } 95 96 return false; 97 } 98 99 } // namespace toco 100 101 #endif // TENSORFLOW_CONTRIB_LITE_TOCO_TOCO_PORT_H_ 102