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