1 /* Copyright 2016 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 <stdio.h>
17 #include <set>
18
19 #include "tensorflow/core/platform/logging.h"
20 #include "tensorflow/core/platform/protobuf.h"
21 #include "tensorflow/core/platform/protobuf_compiler.h"
22 #include "tensorflow/core/platform/types.h"
23 #include "tensorflow/tools/proto_text/gen_proto_text_functions_lib.h"
24
25 namespace tensorflow {
26
27 namespace {
28 class CrashOnErrorCollector
29 : public tensorflow::protobuf::compiler::MultiFileErrorCollector {
30 public:
~CrashOnErrorCollector()31 ~CrashOnErrorCollector() override {}
32
AddError(const string & filename,int line,int column,const string & message)33 void AddError(const string& filename, int line, int column,
34 const string& message) override {
35 LOG(FATAL) << "Unexpected error at " << filename << "@" << line << ":"
36 << column << " - " << message;
37 }
38 };
39
40 static const char kTensorFlowHeaderPrefix[] = "";
41
42 static const char kPlaceholderFile[] =
43 "tensorflow/tools/proto_text/placeholder.txt";
44
IsPlaceholderFile(const char * s)45 bool IsPlaceholderFile(const char* s) {
46 string ph(kPlaceholderFile);
47 string str(s);
48 return str.size() >= strlen(kPlaceholderFile) &&
49 ph == str.substr(str.size() - ph.size());
50 }
51
52 } // namespace
53
54 // Main program to take input protos and write output pb_text source files that
55 // contain generated proto text input and output functions.
56 //
57 // Main expects:
58 // - First argument is output path
59 // - Second argument is the relative path of the protos to the root. E.g.,
60 // for protos built by a rule in tensorflow/core, this will be
61 // tensorflow/core.
62 // - Then any number of source proto file names, plus one source name must be
63 // placeholder.txt from this gen tool's package. placeholder.txt is
64 // ignored for proto resolution, but is used to determine the root at which
65 // the build tool has placed the source proto files.
66 //
67 // Note that this code doesn't use tensorflow's command line parsing, because of
68 // circular dependencies between libraries if that were done.
69 //
70 // This is meant to be invoked by a genrule. See BUILD for more information.
MainImpl(int argc,char ** argv)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";
129 data = code.cc;
130 }
131
132 const string path = output_root + "/" + proto_path_no_suffix + suffix;
133 FILE* f = fopen(path.c_str(), "w");
134 if (f == nullptr) {
135 // We don't expect this output to be generated. It was specified in the
136 // list of sources solely to satisfy a proto import dependency.
137 continue;
138 }
139 if (fwrite(data.c_str(), 1, data.size(), f) != data.size()) {
140 fclose(f);
141 return -1;
142 }
143 if (fclose(f) != 0) {
144 return -1;
145 }
146 }
147 }
148 return 0;
149 }
150
151 } // namespace tensorflow
152
main(int argc,char ** argv)153 int main(int argc, char** argv) { return tensorflow::MainImpl(argc, argv); }
154