• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iostream>
32 #include <google/protobuf/compiler/objectivec/objectivec_generator.h>
33 #include <google/protobuf/compiler/objectivec/objectivec_file.h>
34 #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
35 #include <google/protobuf/io/printer.h>
36 #include <google/protobuf/io/zero_copy_stream.h>
37 #include <google/protobuf/stubs/strutil.h>
38 
39 namespace google {
40 namespace protobuf {
41 namespace compiler {
42 namespace objectivec {
43 
ObjectiveCGenerator()44 ObjectiveCGenerator::ObjectiveCGenerator() {}
45 
~ObjectiveCGenerator()46 ObjectiveCGenerator::~ObjectiveCGenerator() {}
47 
HasGenerateAll() const48 bool ObjectiveCGenerator::HasGenerateAll() const {
49   return true;
50 }
51 
Generate(const FileDescriptor * file,const string & parameter,GeneratorContext * context,string * error) const52 bool ObjectiveCGenerator::Generate(const FileDescriptor* file,
53                                    const string& parameter,
54                                    GeneratorContext* context,
55                                    string* error) const {
56   *error = "Unimplemented Generate() method. Call GenerateAll() instead.";
57   return false;
58 }
59 
GenerateAll(const std::vector<const FileDescriptor * > & files,const string & parameter,GeneratorContext * context,string * error) const60 bool ObjectiveCGenerator::GenerateAll(const std::vector<const FileDescriptor*>& files,
61                                       const string& parameter,
62                                       GeneratorContext* context,
63                                       string* error) const {
64   // -----------------------------------------------------------------
65   // Parse generator options. These options are passed to the compiler using the
66   // --objc_opt flag. The options are passed as a comma separated list of
67   // options along with their values. If the option appears multiple times, only
68   // the last value will be considered.
69   //
70   // e.g. protoc ... --objc_opt=expected_prefixes=file.txt,generate_for_named_framework=MyFramework
71 
72   Options generation_options;
73 
74   std::vector<std::pair<string, string> > options;
75   ParseGeneratorParameter(parameter, &options);
76   for (int i = 0; i < options.size(); i++) {
77     if (options[i].first == "expected_prefixes_path") {
78       // Path to find a file containing the expected prefixes
79       // (objc_class_prefix "PREFIX") for proto packages (package NAME). The
80       // generator will then issue warnings/errors if in the proto files being
81       // generated the option is not listed/wrong/etc in the file.
82       //
83       // The format of the file is:
84       //   - An entry is a line of "package=prefix".
85       //   - Comments start with "#".
86       //   - A comment can go on a line after a expected package/prefix pair.
87       //     (i.e. - "package=prefix # comment")
88       //
89       // There is no validation that the prefixes are good prefixes, it is
90       // assumed that they are when you create the file.
91       generation_options.expected_prefixes_path = options[i].second;
92     } else if (options[i].first == "expected_prefixes_suppressions") {
93       // A semicolon delimited string that lists the paths of .proto files to
94       // exclude from the package prefix validations (expected_prefixes_path).
95       // This is provided as an "out", to skip some files being checked.
96       SplitStringUsing(options[i].second, ";",
97                        &generation_options.expected_prefixes_suppressions);
98     } else if (options[i].first == "generate_for_named_framework") {
99       // The name of the framework that protos are being generated for. This
100       // will cause the #import statements to be framework based using this
101       // name (i.e. - "#import <NAME/proto.pbobjc.h>).
102       //
103       // NOTE: If this option is used with
104       // named_framework_to_proto_path_mappings_path, then this is effectively
105       // the "default" framework name used for everything that wasn't mapped by
106       // the mapping file.
107       generation_options.generate_for_named_framework = options[i].second;
108     } else if (options[i].first == "named_framework_to_proto_path_mappings_path") {
109       // Path to find a file containing the list of framework names and proto
110       // files. The generator uses this to decide if a proto file
111       // referenced should use a framework style import vs. a user level import
112       // (#import <FRAMEWORK/file.pbobjc.h> vs #import "dir/file.pbobjc.h").
113       //
114       // The format of the file is:
115       //   - An entry is a line of "frameworkName: file.proto, dir/file2.proto".
116       //   - Comments start with "#".
117       //   - A comment can go on a line after a expected package/prefix pair.
118       //     (i.e. - "frameworkName: file.proto # comment")
119       //
120       // Any number of files can be listed for a framework, just separate them
121       // with commas.
122       //
123       // There can be multiple lines listing the same frameworkName incase it
124       // has a lot of proto files included in it; having multiple lines makes
125       // things easier to read. If a proto file is not configured in the
126       // mappings file, it will use the default framework name if one was passed
127       // with generate_for_named_framework, or the relative path to it's include
128       // path otherwise.
129       generation_options.named_framework_to_proto_path_mappings_path = options[i].second;
130     } else {
131       *error = "error: Unknown generator option: " + options[i].first;
132       return false;
133     }
134   }
135 
136   // -----------------------------------------------------------------
137 
138   // Validate the objc prefix/package pairings.
139   if (!ValidateObjCClassPrefixes(files, generation_options, error)) {
140     // *error will have been filled in.
141     return false;
142   }
143 
144   for (int i = 0; i < files.size(); i++) {
145     const FileDescriptor* file = files[i];
146     FileGenerator file_generator(file, generation_options);
147     string filepath = FilePath(file);
148 
149     // Generate header.
150     {
151       std::unique_ptr<io::ZeroCopyOutputStream> output(
152           context->Open(filepath + ".pbobjc.h"));
153       io::Printer printer(output.get(), '$');
154       file_generator.GenerateHeader(&printer);
155     }
156 
157     // Generate m file.
158     {
159       std::unique_ptr<io::ZeroCopyOutputStream> output(
160           context->Open(filepath + ".pbobjc.m"));
161       io::Printer printer(output.get(), '$');
162       file_generator.GenerateSource(&printer);
163     }
164   }
165 
166   return true;
167 }
168 
169 }  // namespace objectivec
170 }  // namespace compiler
171 }  // namespace protobuf
172 }  // namespace google
173