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
Generate(const FileDescriptor * file,const string & parameter,OutputDirectory * output_directory,string * error) const48 bool ObjectiveCGenerator::Generate(const FileDescriptor* file,
49 const string& parameter,
50 OutputDirectory* output_directory,
51 string* error) const {
52 // -----------------------------------------------------------------
53 // Parse generator options. These options are passed to the compiler using the
54 // --objc_opt flag. The options are passed as a comma separated list of
55 // options along with their values. If the option appears multiple times, only
56 // the last value will be considered.
57 //
58 // e.g. protoc ... --objc_opt=expected_prefixes=file.txt,generate_for_named_framework=MyFramework
59
60 Options generation_options;
61
62 vector<pair<string, string> > options;
63 ParseGeneratorParameter(parameter, &options);
64 for (int i = 0; i < options.size(); i++) {
65 if (options[i].first == "expected_prefixes_path") {
66 // Path to find a file containing the expected prefixes
67 // (objc_class_prefix "PREFIX") for proto packages (package NAME). The
68 // generator will then issue warnings/errors if in the proto files being
69 // generated the option is not listed/wrong/etc in the file.
70 //
71 // The format of the file is:
72 // - An entry is a line of "package=prefix".
73 // - Comments start with "#".
74 // - A comment can go on a line after a expected package/prefix pair.
75 // (i.e. - "package=prefix # comment")
76 //
77 // There is no validation that the prefixes are good prefixes, it is
78 // assumed that they are when you create the file.
79 generation_options.expected_prefixes_path = options[i].second;
80 } else if (options[i].first == "generate_for_named_framework") {
81 // The name of the framework that protos are being generated for. This
82 // will cause the #import statements to be framework based using this
83 // name (i.e. - "#import <NAME/proto.pbobjc.h>).
84 //
85 // NOTE: If this option is used with
86 // named_framework_to_proto_path_mappings_path, then this is effectively
87 // the "default" framework name used for everything that wasn't mapped by
88 // the mapping file.
89 generation_options.generate_for_named_framework = options[i].second;
90 } else if (options[i].first == "named_framework_to_proto_path_mappings_path") {
91 // Path to find a file containing the list of framework names and proto
92 // files. The generator uses this to decide if a proto file
93 // referenced should use a framework style import vs. a user level import
94 // (#import <FRAMEWORK/file.pbobjc.h> vs #import "dir/file.pbobjc.h").
95 //
96 // The format of the file is:
97 // - An entry is a line of "frameworkName: file.proto, dir/file2.proto".
98 // - Comments start with "#".
99 // - A comment can go on a line after a expected package/prefix pair.
100 // (i.e. - "frameworkName: file.proto # comment")
101 //
102 // Any number of files can be listed for a framework, just separate them
103 // with commas.
104 //
105 // There can be multiple lines listing the same frameworkName incase it
106 // has a lot of proto files included in it; having multiple lines makes
107 // things easier to read. If a proto file is not configured in the
108 // mappings file, it will use the default framework name if one was passed
109 // with generate_for_named_framework, or the relative path to it's include
110 // path otherwise.
111 generation_options.named_framework_to_proto_path_mappings_path = options[i].second;
112 } else {
113 *error = "error: Unknown generator option: " + options[i].first;
114 return false;
115 }
116 }
117
118 // -----------------------------------------------------------------
119
120 // Validate the objc prefix/package pairing.
121 if (!ValidateObjCClassPrefix(file, generation_options, error)) {
122 // *error will have been filled in.
123 return false;
124 }
125
126 FileGenerator file_generator(file, generation_options);
127 string filepath = FilePath(file);
128
129 // Generate header.
130 {
131 scoped_ptr<io::ZeroCopyOutputStream> output(
132 output_directory->Open(filepath + ".pbobjc.h"));
133 io::Printer printer(output.get(), '$');
134 file_generator.GenerateHeader(&printer);
135 }
136
137 // Generate m file.
138 {
139 scoped_ptr<io::ZeroCopyOutputStream> output(
140 output_directory->Open(filepath + ".pbobjc.m"));
141 io::Printer printer(output.get(), '$');
142 file_generator.GenerateSource(&printer);
143 }
144
145 return true;
146 }
147
148 } // namespace objectivec
149 } // namespace compiler
150 } // namespace protobuf
151 } // namespace google
152