Main program to take input protos and write output pb_text source files that contain generated proto text input and output functions. Main expects: - First argument is output path - Second argument is the relative path of the protos to the root. E.g., for protos built by a rule in tensorflow/core, this will be tensorflow/core. - Then any number of source proto file names, plus one source name mus
| 69 | // |
| 70 | // This is meant to be invoked by a genrule. See BUILD for more information. |
| 71 | int MainImpl(int argc, char** argv) { |
| 72 | if (argc < 4) { |
| 73 | LOG(ERROR) << "Pass output path, relative path, and at least proto file"; |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | const string output_root = argv[1]; |
| 78 | const string output_relative_path = kTensorFlowHeaderPrefix + string(argv[2]); |
| 79 | |
| 80 | string src_relative_path; |
| 81 | bool has_placeholder = false; |
| 82 | for (int i = 3; i < argc; ++i) { |
| 83 | if (IsPlaceholderFile(argv[i])) { |
| 84 | const string s(argv[i]); |
| 85 | src_relative_path = s.substr(0, s.size() - strlen(kPlaceholderFile)); |
| 86 | has_placeholder = true; |
| 87 | } |
| 88 | } |
| 89 | if (!has_placeholder) { |
| 90 | LOG(ERROR) << kPlaceholderFile << " must be passed"; |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | tensorflow::protobuf::compiler::DiskSourceTree source_tree; |
| 95 | |
| 96 | source_tree.MapPath("", src_relative_path.empty() ? "." : src_relative_path); |
| 97 | CrashOnErrorCollector crash_on_error; |
| 98 | tensorflow::protobuf::compiler::Importer importer(&source_tree, |
| 99 | &crash_on_error); |
| 100 | |
| 101 | for (int i = 3; i < argc; i++) { |
| 102 | if (IsPlaceholderFile(argv[i])) continue; |
| 103 | const string proto_path = string(argv[i]).substr(src_relative_path.size()); |
| 104 | |
| 105 | const tensorflow::protobuf::FileDescriptor* fd = |
| 106 | importer.Import(proto_path); |
| 107 | |
| 108 | const int index = proto_path.find_last_of("."); |
| 109 | string proto_path_no_suffix = proto_path.substr(0, index); |
| 110 | |
| 111 | proto_path_no_suffix = |
| 112 | proto_path_no_suffix.substr(output_relative_path.size()); |
| 113 | |
| 114 | const auto code = |
| 115 | tensorflow::GetProtoTextFunctionCode(*fd, kTensorFlowHeaderPrefix); |
| 116 | |
| 117 | // Three passes, one for each output file. |
| 118 | for (int pass = 0; pass < 3; ++pass) { |
| 119 | string suffix; |
| 120 | string data; |
| 121 | if (pass == 0) { |
| 122 | suffix = ".pb_text.h"; |
| 123 | data = code.header; |
| 124 | } else if (pass == 1) { |
| 125 | suffix = ".pb_text-impl.h"; |
| 126 | data = code.header_impl; |
| 127 | } else { |
| 128 | suffix = ".pb_text.cc"; |
no test coverage detected