• 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 <string>
33 #include <google/protobuf/compiler/objectivec/objectivec_generator.h>
34 #include <google/protobuf/compiler/objectivec/objectivec_file.h>
35 #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
36 #include <google/protobuf/io/printer.h>
37 #include <google/protobuf/io/zero_copy_stream.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 std::string & parameter,GeneratorContext * context,std::string * error) const52 bool ObjectiveCGenerator::Generate(const FileDescriptor* file,
53                                    const std::string& parameter,
54                                    GeneratorContext* context,
55                                    std::string* error) const {
56   *error = "Unimplemented Generate() method. Call GenerateAll() instead.";
57   return false;
58 }
59 
GenerateAll(const std::vector<const FileDescriptor * > & files,const std::string & parameter,GeneratorContext * context,std::string * error) const60 bool ObjectiveCGenerator::GenerateAll(
61     const std::vector<const FileDescriptor*>& files,
62     const std::string& parameter, GeneratorContext* context,
63     std::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<std::string, std::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       for (StringPiece split_piece : Split(
97                options[i].second, ";", true)) {
98         generation_options.expected_prefixes_suppressions.push_back(
99             std::string(split_piece));
100       }
101     } else if (options[i].first == "generate_for_named_framework") {
102       // The name of the framework that protos are being generated for. This
103       // will cause the #import statements to be framework based using this
104       // name (i.e. - "#import <NAME/proto.pbobjc.h>).
105       //
106       // NOTE: If this option is used with
107       // named_framework_to_proto_path_mappings_path, then this is effectively
108       // the "default" framework name used for everything that wasn't mapped by
109       // the mapping file.
110       generation_options.generate_for_named_framework = options[i].second;
111     } else if (options[i].first == "named_framework_to_proto_path_mappings_path") {
112       // Path to find a file containing the list of framework names and proto
113       // files. The generator uses this to decide if a proto file
114       // referenced should use a framework style import vs. a user level import
115       // (#import <FRAMEWORK/file.pbobjc.h> vs #import "dir/file.pbobjc.h").
116       //
117       // The format of the file is:
118       //   - An entry is a line of "frameworkName: file.proto, dir/file2.proto".
119       //   - Comments start with "#".
120       //   - A comment can go on a line after a expected package/prefix pair.
121       //     (i.e. - "frameworkName: file.proto # comment")
122       //
123       // Any number of files can be listed for a framework, just separate them
124       // with commas.
125       //
126       // There can be multiple lines listing the same frameworkName in case it
127       // has a lot of proto files included in it; having multiple lines makes
128       // things easier to read. If a proto file is not configured in the
129       // mappings file, it will use the default framework name if one was passed
130       // with generate_for_named_framework, or the relative path to it's include
131       // path otherwise.
132       generation_options.named_framework_to_proto_path_mappings_path = options[i].second;
133     } else if (options[i].first == "runtime_import_prefix") {
134       // Path to use as a prefix on #imports of runtime provided headers in the
135       // generated files. When integrating ObjC protos into a build system,
136       // this can be used to avoid having to add the runtime directory to the
137       // header search path since the generate #import will be more complete.
138       generation_options.runtime_import_prefix =
139           StripSuffixString(options[i].second, "/");
140     } else {
141       *error = "error: Unknown generator option: " + options[i].first;
142       return false;
143     }
144   }
145 
146   // -----------------------------------------------------------------
147 
148   // Validate the objc prefix/package pairings.
149   if (!ValidateObjCClassPrefixes(files, generation_options, error)) {
150     // *error will have been filled in.
151     return false;
152   }
153 
154   for (int i = 0; i < files.size(); i++) {
155     const FileDescriptor* file = files[i];
156     FileGenerator file_generator(file, generation_options);
157     std::string filepath = FilePath(file);
158 
159     // Generate header.
160     {
161       std::unique_ptr<io::ZeroCopyOutputStream> output(
162           context->Open(filepath + ".pbobjc.h"));
163       io::Printer printer(output.get(), '$');
164       file_generator.GenerateHeader(&printer);
165     }
166 
167     // Generate m file.
168     {
169       std::unique_ptr<io::ZeroCopyOutputStream> output(
170           context->Open(filepath + ".pbobjc.m"));
171       io::Printer printer(output.get(), '$');
172       file_generator.GenerateSource(&printer);
173     }
174   }
175 
176   return true;
177 }
178 
179 }  // namespace objectivec
180 }  // namespace compiler
181 }  // namespace protobuf
182 }  // namespace google
183