Skip to content

Commit

Permalink
Trim imports for bundled generated protos.
Browse files Browse the repository at this point in the history
To avoid a cycle between headers, have the WKTs use minimal imports instead
of using the helper to get everything from the library.

Fixes #4301
Fixes #4403
  • Loading branch information
thomasvl committed Apr 2, 2018
1 parent 7bf47a6 commit bca797d
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 33 deletions.
2 changes: 1 addition & 1 deletion objectivec/GPBProtocolBuffers_RuntimeSupport.h
Expand Up @@ -31,7 +31,7 @@
// This header is meant to only be used by the generated source, it should not
// be included in code using protocol buffers.

#import "GPBProtocolBuffers.h"
#import "GPBBootstrap.h"

#import "GPBDescriptor_PackagePrivate.h"
#import "GPBExtensionInternals.h"
Expand Down
8 changes: 6 additions & 2 deletions objectivec/google/protobuf/Any.pbobjc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions objectivec/google/protobuf/Api.pbobjc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions objectivec/google/protobuf/Duration.pbobjc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions objectivec/google/protobuf/Empty.pbobjc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions objectivec/google/protobuf/FieldMask.pbobjc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions objectivec/google/protobuf/SourceContext.pbobjc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions objectivec/google/protobuf/Struct.pbobjc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions objectivec/google/protobuf/Timestamp.pbobjc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions objectivec/google/protobuf/Type.pbobjc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions objectivec/google/protobuf/Wrappers.pbobjc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 40 additions & 11 deletions src/google/protobuf/compiler/objectivec/objectivec_file.cc
Expand Up @@ -214,7 +214,17 @@ FileGenerator::~FileGenerator() {
}

void FileGenerator::GenerateHeader(io::Printer *printer) {
PrintFileRuntimePreamble(printer, "GPBProtocolBuffers.h");
std::set<string> headers;
// Generated files bundled with the library get minimal imports, everything
// else gets the wrapper so everything is usable.
if (IsProtobufLibraryBundledProtoFile(file_)) {
headers.insert("GPBRootObject.h");
headers.insert("GPBMessage.h");
headers.insert("GPBDescriptor.h");
} else {
headers.insert("GPBProtocolBuffers.h");
}
PrintFileRuntimePreamble(printer, headers);

// Add some verification that the generated code matches the source the
// code is being compiled with.
Expand Down Expand Up @@ -337,7 +347,9 @@ void FileGenerator::GenerateHeader(io::Printer *printer) {

void FileGenerator::GenerateSource(io::Printer *printer) {
// #import the runtime support.
PrintFileRuntimePreamble(printer, "GPBProtocolBuffers_RuntimeSupport.h");
std::set<string> headers;
headers.insert("GPBProtocolBuffers_RuntimeSupport.h");
PrintFileRuntimePreamble(printer, headers);

// Enums use atomic in the generated code, so add the system import as needed.
if (FileContainsEnums(file_)) {
Expand Down Expand Up @@ -566,7 +578,7 @@ void FileGenerator::GenerateSource(io::Printer *printer) {
// files. This currently only supports the runtime coming from a framework
// as defined by the official CocoaPod.
void FileGenerator::PrintFileRuntimePreamble(
io::Printer* printer, const string& header_to_import) const {
io::Printer* printer, const std::set<string>& headers_to_import) const {
printer->Print(
"// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
"// source: $filename$\n"
Expand All @@ -575,22 +587,39 @@ void FileGenerator::PrintFileRuntimePreamble(

const string framework_name(ProtobufLibraryFrameworkName);
const string cpp_symbol(ProtobufFrameworkImportSymbol(framework_name));

printer->Print(
"// This CPP symbol can be defined to use imports that match up to the framework\n"
"// imports needed when using CocoaPods.\n"
"#if !defined($cpp_symbol$)\n"
" #define $cpp_symbol$ 0\n"
"#endif\n"
"\n"
"#if $cpp_symbol$\n"
" #import <$framework_name$/$header$>\n"
"#else\n"
" #import \"$header$\"\n"
"#if $cpp_symbol$\n",
"cpp_symbol", cpp_symbol);


for (std::set<string>::const_iterator iter = headers_to_import.begin();
iter != headers_to_import.end(); ++iter) {
printer->Print(
" #import <$framework_name$/$header$>\n",
"header", *iter,
"framework_name", framework_name);
}

printer->Print(
"#else\n");

for (std::set<string>::const_iterator iter = headers_to_import.begin();
iter != headers_to_import.end(); ++iter) {
printer->Print(
" #import \"$header$\"\n",
"header", *iter);
}

printer->Print(
"#endif\n"
"\n",
"cpp_symbol", cpp_symbol,
"header", header_to_import,
"framework_name", framework_name);
"\n");
}

} // namespace objectivec
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/objectivec/objectivec_file.h
Expand Up @@ -74,7 +74,7 @@ class FileGenerator {
const Options options_;

void PrintFileRuntimePreamble(
io::Printer* printer, const string& header_to_import) const;
io::Printer* printer, const std::set<string>& headers_to_import) const;

GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileGenerator);
};
Expand Down

0 comments on commit bca797d

Please sign in to comment.