/
objectivec_generator.cc
291 lines (269 loc) · 12.9 KB
/
objectivec_generator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
#include <google/protobuf/compiler/objectivec/objectivec_generator.h>
#include <google/protobuf/compiler/objectivec/objectivec_file.h>
#include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/io/zero_copy_stream.h>
#include <google/protobuf/stubs/strutil.h>
namespace google {
namespace protobuf {
namespace compiler {
namespace objectivec {
namespace {
// Convert a string with "yes"/"no" (case insensitive) to a boolean, returning
// true/false for if the input string was a valid value. If the input string is
// invalid, `result` is unchanged.
bool StringToBool(const std::string& value, bool* result) {
std::string upper_value(value);
UpperString(&upper_value);
if (upper_value == "NO") {
*result = false;
return true;
}
if (upper_value == "YES") {
*result = true;
return true;
}
return false;
}
} // namespace
ObjectiveCGenerator::ObjectiveCGenerator() {}
ObjectiveCGenerator::~ObjectiveCGenerator() {}
bool ObjectiveCGenerator::HasGenerateAll() const {
return true;
}
bool ObjectiveCGenerator::Generate(const FileDescriptor* file,
const std::string& parameter,
GeneratorContext* context,
std::string* error) const {
*error = "Unimplemented Generate() method. Call GenerateAll() instead.";
return false;
}
bool ObjectiveCGenerator::GenerateAll(
const std::vector<const FileDescriptor*>& files,
const std::string& parameter, GeneratorContext* context,
std::string* error) const {
// -----------------------------------------------------------------
// Parse generator options. These options are passed to the compiler using the
// --objc_opt flag. The options are passed as a comma separated list of
// options along with their values. If the option appears multiple times, only
// the last value will be considered.
//
// e.g. protoc ... --objc_opt=expected_prefixes=file.txt,generate_for_named_framework=MyFramework
Options generation_options;
std::vector<std::pair<std::string, std::string> > options;
ParseGeneratorParameter(parameter, &options);
for (int i = 0; i < options.size(); i++) {
if (options[i].first == "expected_prefixes_path") {
// Path to find a file containing the expected prefixes
// (objc_class_prefix "PREFIX") for proto packages (package NAME). The
// generator will then issue warnings/errors if in the proto files being
// generated the option is not listed/wrong/etc in the file.
//
// The format of the file is:
// - An entry is a line of "package=prefix".
// - Comments start with "#".
// - A comment can go on a line after a expected package/prefix pair.
// (i.e. - "package=prefix # comment")
// - For files that do NOT have a proto package (not recommended), an
// entry can be made as "no_package:PATH=prefix", where PATH is the
// path for the .proto file.
//
// There is no validation that the prefixes are good prefixes, it is
// assumed that they are when you create the file.
generation_options.expected_prefixes_path = options[i].second;
} else if (options[i].first == "expected_prefixes_suppressions") {
// A semicolon delimited string that lists the paths of .proto files to
// exclude from the package prefix validations (expected_prefixes_path).
// This is provided as an "out", to skip some files being checked.
for (StringPiece split_piece : Split(
options[i].second, ";", true)) {
generation_options.expected_prefixes_suppressions.push_back(
std::string(split_piece));
}
} else if (options[i].first == "prefixes_must_be_registered") {
// If objc prefix file option value must be registered to be used. This
// option has no meaning if an "expected_prefixes_path" isn't set. The
// available options are:
// "no": They don't have to be registered.
// "yes": They must be registered and an error will be raised if a files
// tried to use a prefix that isn't registered.
// Default is "no".
if (!StringToBool(options[i].second,
&generation_options.prefixes_must_be_registered)) {
*error = "error: Unknown value for prefixes_must_be_registered: " + options[i].second;
return false;
}
} else if (options[i].first == "require_prefixes") {
// If every file must have an objc prefix file option to be used. The
// available options are:
// "no": Files can be generated without the prefix option.
// "yes": Files must have the objc prefix option, and an error will be
// raised if a files doesn't have one.
// Default is "no".
if (!StringToBool(options[i].second,
&generation_options.require_prefixes)) {
*error = "error: Unknown value for require_prefixes: " + options[i].second;
return false;
}
} else if (options[i].first == "generate_for_named_framework") {
// The name of the framework that protos are being generated for. This
// will cause the #import statements to be framework based using this
// name (i.e. - "#import <NAME/proto.pbobjc.h>).
//
// NOTE: If this option is used with
// named_framework_to_proto_path_mappings_path, then this is effectively
// the "default" framework name used for everything that wasn't mapped by
// the mapping file.
generation_options.generate_for_named_framework = options[i].second;
} else if (options[i].first == "named_framework_to_proto_path_mappings_path") {
// Path to find a file containing the list of framework names and proto
// files. The generator uses this to decide if a proto file
// referenced should use a framework style import vs. a user level import
// (#import <FRAMEWORK/file.pbobjc.h> vs #import "dir/file.pbobjc.h").
//
// The format of the file is:
// - An entry is a line of "frameworkName: file.proto, dir/file2.proto".
// - Comments start with "#".
// - A comment can go on a line after a expected package/prefix pair.
// (i.e. - "frameworkName: file.proto # comment")
//
// Any number of files can be listed for a framework, just separate them
// with commas.
//
// There can be multiple lines listing the same frameworkName in case it
// has a lot of proto files included in it; having multiple lines makes
// things easier to read. If a proto file is not configured in the
// mappings file, it will use the default framework name if one was passed
// with generate_for_named_framework, or the relative path to it's include
// path otherwise.
generation_options.named_framework_to_proto_path_mappings_path = options[i].second;
} else if (options[i].first == "runtime_import_prefix") {
// Path to use as a prefix on #imports of runtime provided headers in the
// generated files. When integrating ObjC protos into a build system,
// this can be used to avoid having to add the runtime directory to the
// header search path since the generate #import will be more complete.
generation_options.runtime_import_prefix =
StripSuffixString(options[i].second, "/");
} else if (options[i].first == "objc_class_prefix_mappings_path") {
// Path to use for when loading the objc class prefix mappings to use.
// The `objc_class_prefix` file option is always honored first if one is present.
//
// The format of the file is:
// - An entry is a line of "objc_class_prefix: file.proto, dir/file2.proto".
// - Comments start with "#".
// - A comment can go on a line after a expected package/prefix pair.
// (i.e. - "GPB: descriptor.proto # comment")
//
SetObjCClassPrefixMappingsPath(options[i].second);
} else if (options[i].first == "use_package_as_prefix") {
// Controls how the symbols should be prefixed to avoid symbols
// collisions. The objc_class_prefix file option is always honored first
// followed by the objc_class_prefix_mappings_path generator option. This is is just
// what to do if either of these options are not set. The available options are:
// "no": Not prefixed (the existing mode).
// "yes": Make a prefix out of the proto package.
bool value = false;
if (StringToBool(options[i].second, &value)) {
SetUseProtoPackageAsDefaultPrefix(value);
} else {
*error = "error: Unknown use_package_as_prefix: " + options[i].second;
return false;
}
} else if (options[i].first == "proto_package_prefix_exceptions_path") {
// Path to find a file containing the list of proto package names that are
// exceptions when use_package_as_prefix is enabled. This can be used to
// migrate packages one at a time to use_package_as_prefix since there
// are likely code updates needed with each one.
//
// The format of the file is:
// - An entry is a line of "proto.package.name".
// - Comments start with "#".
// - A comment can go on a line after a expected package/prefix pair.
// (i.e. - "some.proto.package # comment")
SetProtoPackagePrefixExceptionList(options[i].second);
} else {
*error = "error: Unknown generator option: " + options[i].first;
return false;
}
}
// -----------------------------------------------------------------
// These are not official generation options and could be removed/changed in
// the future and doing that won't count as a breaking change.
bool headers_only = getenv("GPB_OBJC_HEADERS_ONLY") != NULL;
std::unordered_set<std::string> skip_impls;
if (getenv("GPB_OBJC_SKIP_IMPLS_FILE") != NULL) {
std::ifstream skip_file(getenv("GPB_OBJC_SKIP_IMPLS_FILE"));
if (skip_file.is_open()) {
std::string line;
while (std::getline(skip_file, line)) {
skip_impls.insert(line);
}
} else {
*error = "error: Failed to open GPB_OBJC_SKIP_IMPLS_FILE file";
return false;
}
}
// -----------------------------------------------------------------
// Validate the objc prefix/package pairings.
if (!ValidateObjCClassPrefixes(files, generation_options, error)) {
// *error will have been filled in.
return false;
}
for (int i = 0; i < files.size(); i++) {
const FileDescriptor* file = files[i];
FileGenerator file_generator(file, generation_options);
std::string filepath = FilePath(file);
// Generate header.
{
std::unique_ptr<io::ZeroCopyOutputStream> output(
context->Open(filepath + ".pbobjc.h"));
io::Printer printer(output.get(), '$');
file_generator.GenerateHeader(&printer);
}
// Generate m file.
if (!headers_only && skip_impls.count(file->name()) == 0) {
std::unique_ptr<io::ZeroCopyOutputStream> output(
context->Open(filepath + ".pbobjc.m"));
io::Printer printer(output.get(), '$');
file_generator.GenerateSource(&printer);
}
}
return true;
}
} // namespace objectivec
} // namespace compiler
} // namespace protobuf
} // namespace google