OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 27 matching lines...) Expand all Loading... |
38 | 38 |
39 namespace google { | 39 namespace google { |
40 namespace protobuf { | 40 namespace protobuf { |
41 namespace compiler { | 41 namespace compiler { |
42 namespace objectivec { | 42 namespace objectivec { |
43 | 43 |
44 ObjectiveCGenerator::ObjectiveCGenerator() {} | 44 ObjectiveCGenerator::ObjectiveCGenerator() {} |
45 | 45 |
46 ObjectiveCGenerator::~ObjectiveCGenerator() {} | 46 ObjectiveCGenerator::~ObjectiveCGenerator() {} |
47 | 47 |
| 48 bool ObjectiveCGenerator::HasGenerateAll() const { |
| 49 return true; |
| 50 } |
| 51 |
48 bool ObjectiveCGenerator::Generate(const FileDescriptor* file, | 52 bool ObjectiveCGenerator::Generate(const FileDescriptor* file, |
49 const string& parameter, | 53 const string& parameter, |
50 OutputDirectory* output_directory, | 54 GeneratorContext* context, |
51 string* error) const { | 55 string* error) const { |
| 56 *error = "Unimplemented Generate() method. Call GenerateAll() instead."; |
| 57 return false; |
| 58 } |
| 59 |
| 60 bool ObjectiveCGenerator::GenerateAll(const vector<const FileDescriptor*>& files
, |
| 61 const string& parameter, |
| 62 GeneratorContext* context, |
| 63 string* error) const { |
52 // ----------------------------------------------------------------- | 64 // ----------------------------------------------------------------- |
53 // Parse generator options. | 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_fr
amework=MyFramework |
54 | 71 |
55 Options generation_options; | 72 Options generation_options; |
56 | 73 |
57 vector<pair<string, string> > options; | 74 vector<pair<string, string> > options; |
58 ParseGeneratorParameter(parameter, &options); | 75 ParseGeneratorParameter(parameter, &options); |
59 for (int i = 0; i < options.size(); i++) { | 76 for (int i = 0; i < options.size(); i++) { |
60 if (options[i].first == "expected_prefixes_path") { | 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. |
61 generation_options.expected_prefixes_path = options[i].second; | 91 generation_options.expected_prefixes_path = options[i].second; |
| 92 } else if (options[i].first == "generate_for_named_framework") { |
| 93 // The name of the framework that protos are being generated for. This |
| 94 // will cause the #import statements to be framework based using this |
| 95 // name (i.e. - "#import <NAME/proto.pbobjc.h>). |
| 96 // |
| 97 // NOTE: If this option is used with |
| 98 // named_framework_to_proto_path_mappings_path, then this is effectively |
| 99 // the "default" framework name used for everything that wasn't mapped by |
| 100 // the mapping file. |
| 101 generation_options.generate_for_named_framework = options[i].second; |
| 102 } else if (options[i].first == "named_framework_to_proto_path_mappings_path"
) { |
| 103 // Path to find a file containing the list of framework names and proto |
| 104 // files. The generator uses this to decide if a proto file |
| 105 // referenced should use a framework style import vs. a user level import |
| 106 // (#import <FRAMEWORK/file.pbobjc.h> vs #import "dir/file.pbobjc.h"). |
| 107 // |
| 108 // The format of the file is: |
| 109 // - An entry is a line of "frameworkName: file.proto, dir/file2.proto". |
| 110 // - Comments start with "#". |
| 111 // - A comment can go on a line after a expected package/prefix pair. |
| 112 // (i.e. - "frameworkName: file.proto # comment") |
| 113 // |
| 114 // Any number of files can be listed for a framework, just separate them |
| 115 // with commas. |
| 116 // |
| 117 // There can be multiple lines listing the same frameworkName incase it |
| 118 // has a lot of proto files included in it; having multiple lines makes |
| 119 // things easier to read. If a proto file is not configured in the |
| 120 // mappings file, it will use the default framework name if one was passed |
| 121 // with generate_for_named_framework, or the relative path to it's include |
| 122 // path otherwise. |
| 123 generation_options.named_framework_to_proto_path_mappings_path = options[i
].second; |
62 } else { | 124 } else { |
63 *error = "error: Unknown generator option: " + options[i].first; | 125 *error = "error: Unknown generator option: " + options[i].first; |
64 return false; | 126 return false; |
65 } | 127 } |
66 } | 128 } |
67 | 129 |
68 // ----------------------------------------------------------------- | 130 // ----------------------------------------------------------------- |
69 | 131 |
70 // Validate the objc prefix/package pairing. | 132 // Validate the objc prefix/package pairings. |
71 if (!ValidateObjCClassPrefix(file, generation_options, error)) { | 133 if (!ValidateObjCClassPrefixes(files, generation_options, error)) { |
72 // *error will have been filled in. | 134 // *error will have been filled in. |
73 return false; | 135 return false; |
74 } | 136 } |
75 | 137 |
76 FileGenerator file_generator(file, generation_options); | 138 for (int i = 0; i < files.size(); i++) { |
77 string filepath = FilePath(file); | 139 const FileDescriptor* file = files[i]; |
| 140 FileGenerator file_generator(file, generation_options); |
| 141 string filepath = FilePath(file); |
78 | 142 |
79 // Generate header. | 143 // Generate header. |
80 { | 144 { |
81 scoped_ptr<io::ZeroCopyOutputStream> output( | 145 scoped_ptr<io::ZeroCopyOutputStream> output( |
82 output_directory->Open(filepath + ".pbobjc.h")); | 146 context->Open(filepath + ".pbobjc.h")); |
83 io::Printer printer(output.get(), '$'); | 147 io::Printer printer(output.get(), '$'); |
84 file_generator.GenerateHeader(&printer); | 148 file_generator.GenerateHeader(&printer); |
85 } | 149 } |
86 | 150 |
87 // Generate m file. | 151 // Generate m file. |
88 { | 152 { |
89 scoped_ptr<io::ZeroCopyOutputStream> output( | 153 scoped_ptr<io::ZeroCopyOutputStream> output( |
90 output_directory->Open(filepath + ".pbobjc.m")); | 154 context->Open(filepath + ".pbobjc.m")); |
91 io::Printer printer(output.get(), '$'); | 155 io::Printer printer(output.get(), '$'); |
92 file_generator.GenerateSource(&printer); | 156 file_generator.GenerateSource(&printer); |
| 157 } |
93 } | 158 } |
94 | 159 |
95 return true; | 160 return true; |
96 } | 161 } |
97 | 162 |
98 } // namespace objectivec | 163 } // namespace objectivec |
99 } // namespace compiler | 164 } // namespace compiler |
100 } // namespace protobuf | 165 } // namespace protobuf |
101 } // namespace google | 166 } // namespace google |
OLD | NEW |