| 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 | |
| 52 bool ObjectiveCGenerator::Generate(const FileDescriptor* file, | 48 bool ObjectiveCGenerator::Generate(const FileDescriptor* file, |
| 53 const string& parameter, | 49 const string& parameter, |
| 54 GeneratorContext* context, | 50 OutputDirectory* output_directory, |
| 55 string* error) const { | 51 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 { | |
| 64 // ----------------------------------------------------------------- | 52 // ----------------------------------------------------------------- |
| 65 // Parse generator options. These options are passed to the compiler using the | 53 // Parse generator options. |
| 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 | |
| 71 | 54 |
| 72 Options generation_options; | 55 Options generation_options; |
| 73 | 56 |
| 74 vector<pair<string, string> > options; | 57 vector<pair<string, string> > options; |
| 75 ParseGeneratorParameter(parameter, &options); | 58 ParseGeneratorParameter(parameter, &options); |
| 76 for (int i = 0; i < options.size(); i++) { | 59 for (int i = 0; i < options.size(); i++) { |
| 77 if (options[i].first == "expected_prefixes_path") { | 60 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; | 61 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; | |
| 124 } else { | 62 } else { |
| 125 *error = "error: Unknown generator option: " + options[i].first; | 63 *error = "error: Unknown generator option: " + options[i].first; |
| 126 return false; | 64 return false; |
| 127 } | 65 } |
| 128 } | 66 } |
| 129 | 67 |
| 130 // ----------------------------------------------------------------- | 68 // ----------------------------------------------------------------- |
| 131 | 69 |
| 132 // Validate the objc prefix/package pairings. | 70 // Validate the objc prefix/package pairing. |
| 133 if (!ValidateObjCClassPrefixes(files, generation_options, error)) { | 71 if (!ValidateObjCClassPrefix(file, generation_options, error)) { |
| 134 // *error will have been filled in. | 72 // *error will have been filled in. |
| 135 return false; | 73 return false; |
| 136 } | 74 } |
| 137 | 75 |
| 138 for (int i = 0; i < files.size(); i++) { | 76 FileGenerator file_generator(file, generation_options); |
| 139 const FileDescriptor* file = files[i]; | 77 string filepath = FilePath(file); |
| 140 FileGenerator file_generator(file, generation_options); | |
| 141 string filepath = FilePath(file); | |
| 142 | 78 |
| 143 // Generate header. | 79 // Generate header. |
| 144 { | 80 { |
| 145 scoped_ptr<io::ZeroCopyOutputStream> output( | 81 scoped_ptr<io::ZeroCopyOutputStream> output( |
| 146 context->Open(filepath + ".pbobjc.h")); | 82 output_directory->Open(filepath + ".pbobjc.h")); |
| 147 io::Printer printer(output.get(), '$'); | 83 io::Printer printer(output.get(), '$'); |
| 148 file_generator.GenerateHeader(&printer); | 84 file_generator.GenerateHeader(&printer); |
| 149 } | 85 } |
| 150 | 86 |
| 151 // Generate m file. | 87 // Generate m file. |
| 152 { | 88 { |
| 153 scoped_ptr<io::ZeroCopyOutputStream> output( | 89 scoped_ptr<io::ZeroCopyOutputStream> output( |
| 154 context->Open(filepath + ".pbobjc.m")); | 90 output_directory->Open(filepath + ".pbobjc.m")); |
| 155 io::Printer printer(output.get(), '$'); | 91 io::Printer printer(output.get(), '$'); |
| 156 file_generator.GenerateSource(&printer); | 92 file_generator.GenerateSource(&printer); |
| 157 } | |
| 158 } | 93 } |
| 159 | 94 |
| 160 return true; | 95 return true; |
| 161 } | 96 } |
| 162 | 97 |
| 163 } // namespace objectivec | 98 } // namespace objectivec |
| 164 } // namespace compiler | 99 } // namespace compiler |
| 165 } // namespace protobuf | 100 } // namespace protobuf |
| 166 } // namespace google | 101 } // namespace google |
| OLD | NEW |