Skip to content

Commit ccc69f5

Browse files
j2objc-copybaracopybara-github
authored andcommitted
Add a minimal protoc implementation based on the existing J2ObjC protoc that's used only for generating the class map file, to be used by the eventual J2ObjC proto implementation that will transpile Java proto lite instead of using the custom J2ObjC protoc.
PiperOrigin-RevId: 660072378
1 parent 1c32c19 commit ccc69f5

File tree

8 files changed

+765
-0
lines changed

8 files changed

+765
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
// Author: [email protected] (Keith Stanger)
32+
// Based on original Protocol Buffers design by
33+
// Sanjay Ghemawat, Jeff Dean, Kenton Varda, and others.
34+
35+
// We include all protobuf dependencies here because the required headers are
36+
// different when building against google internal protocol buffers than
37+
// opensource protocol buffers. Additional macros are provided to help resolve
38+
// other differences.
39+
40+
#ifndef GOOGLE_PROTOBUF_COMPILER_J2OBJC_COMMON_H__
41+
#define GOOGLE_PROTOBUF_COMPILER_J2OBJC_COMMON_H__
42+
43+
#include "absl/strings/str_replace.h"
44+
#include "google/protobuf/compiler/code_generator.h"
45+
#include "google/protobuf/io/printer.h"
46+
47+
using namespace google::protobuf::compiler;
48+
49+
#endif // GOOGLE_PROTOBUF_COMPILER_J2OBJC_COMMON_H__
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
// Author: [email protected] (Keith Stanger)
32+
// Based on original Protocol Buffers design by
33+
// Sanjay Ghemawat, Jeff Dean, and others.
34+
35+
#include "google/protobuf/compiler/protolite/j2objc_file.h"
36+
37+
#include "google/protobuf/compiler/protolite/j2objc_helpers.h"
38+
39+
namespace google {
40+
namespace protobuf {
41+
namespace compiler {
42+
namespace j2objc {
43+
44+
FileGenerator::FileGenerator(const FileDescriptor* file)
45+
: file_(file), classname_(FileClassName(file)) {
46+
output_dir_ = JavaPackageToDir(FileJavaPackage(file));
47+
}
48+
49+
FileGenerator::~FileGenerator() {}
50+
51+
bool FileGenerator::Validate(std::string* error) {
52+
// Check that no class name matches the file's class name. This is a common
53+
// problem that leads to Java compile errors that can be hard to understand.
54+
// It's especially bad when using the java_multiple_files, since we would
55+
// end up overwriting the outer class with one of the inner ones.
56+
if (HasConflictingClassName(file_, classname_)) {
57+
error->assign(file_->name());
58+
error->append(
59+
": Cannot generate Java output because the file's outer class name, "
60+
"\"");
61+
error->append(classname_);
62+
error->append(
63+
"\", matches the name of one of the types declared inside it. "
64+
"Please either rename the type or use the java_outer_classname "
65+
"option to specify a different outer class name for the .proto file.");
66+
return false;
67+
}
68+
69+
return true;
70+
}
71+
72+
void PrintProperty(io::Printer* printer, const std::string& key,
73+
const std::string& value) {
74+
printer->Print("$key$=$value$\n", "key", key, "value", value);
75+
}
76+
77+
void PrintClassMappings(const Descriptor* descriptor, io::Printer* printer) {
78+
PrintProperty(printer, JavaClassName(descriptor), ClassName(descriptor));
79+
for (int i = 0; i < descriptor->nested_type_count(); i++) {
80+
PrintClassMappings(descriptor->nested_type(i), printer);
81+
}
82+
}
83+
84+
void FileGenerator::GenerateClassMappings(GeneratorContext* context) {
85+
std::string filename = MappedInputName(file_) + ".clsmap.properties";
86+
std::unique_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
87+
io::Printer printer(output.get(), '$');
88+
PrintProperty(&printer, JavaClassName(file_), ClassName(file_));
89+
for (int i = 0; i < file_->enum_type_count(); i++) {
90+
PrintProperty(&printer, JavaClassName(file_->enum_type(i)),
91+
ClassName(file_->enum_type(i)));
92+
}
93+
for (int i = 0; i < file_->message_type_count(); i++) {
94+
PrintClassMappings(file_->message_type(i), &printer);
95+
}
96+
}
97+
98+
} // namespace j2objc
99+
} // namespace compiler
100+
} // namespace protobuf
101+
} // namespace google
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
// Author: [email protected] (Keith Stanger)
32+
// Based on original Protocol Buffers design by
33+
// Sanjay Ghemawat, Jeff Dean, and others.
34+
35+
#ifndef GOOGLE_PROTOBUF_COMPILER_J2OBJC_FILE_H__
36+
#define GOOGLE_PROTOBUF_COMPILER_J2OBJC_FILE_H__
37+
38+
#include <string>
39+
40+
#include "google/protobuf/compiler/protolite/common.h"
41+
42+
namespace google {
43+
namespace protobuf {
44+
namespace compiler {
45+
namespace j2objc {
46+
47+
class FileGenerator {
48+
public:
49+
explicit FileGenerator(const FileDescriptor* file);
50+
~FileGenerator();
51+
52+
// Checks for problems that would otherwise lead to cryptic compile errors.
53+
// Returns true if there are no problems, or writes an error description to
54+
// the given string and returns false otherwise.
55+
bool Validate(std::string* error);
56+
57+
// Generate the class mapping file.
58+
void GenerateClassMappings(GeneratorContext* generator_context);
59+
60+
private:
61+
const FileDescriptor* file_;
62+
std::string output_dir_;
63+
std::string classname_;
64+
};
65+
66+
} // namespace j2objc
67+
} // namespace compiler
68+
} // namespace protobuf
69+
} // namespace google
70+
71+
#endif // GOOGLE_PROTOBUF_COMPILER_J2OBJC_FILE_H__
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
// Author: [email protected] (Keith Stanger)
32+
// Based on original Protocol Buffers design by
33+
// Sanjay Ghemawat, Jeff Dean, and others.
34+
35+
#include <google/protobuf/compiler/protolite/j2objc_file.h>
36+
#include <google/protobuf/compiler/protolite/j2objc_generator.h>
37+
38+
namespace google {
39+
namespace protobuf {
40+
namespace compiler {
41+
namespace j2objc {
42+
43+
J2ObjCGenerator::J2ObjCGenerator() {}
44+
J2ObjCGenerator::~J2ObjCGenerator() {}
45+
46+
bool J2ObjCGenerator::Generate(const FileDescriptor* file,
47+
const std::string& parameter,
48+
GeneratorContext* context,
49+
std::string* error) const {
50+
FileGenerator file_generator(file);
51+
if (!file_generator.Validate(error)) {
52+
return false;
53+
}
54+
55+
file_generator.GenerateClassMappings(context);
56+
57+
return true;
58+
}
59+
60+
} // namespace j2objc
61+
} // namespace compiler
62+
} // namespace protobuf
63+
} // namespace google
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
// Author: [email protected] (Keith Stanger)
32+
// Based on original Protocol Buffers design by
33+
// Sanjay Ghemawat, Jeff Dean, and others.
34+
//
35+
// Generates Objective-C code for a given .proto file, that is compatible
36+
// with client code that expects a transpiled Java API.
37+
38+
#ifndef GOOGLE_PROTOBUF_COMPILER_J2OBJC_GENERATOR_H__
39+
#define GOOGLE_PROTOBUF_COMPILER_J2OBJC_GENERATOR_H__
40+
41+
#include <string>
42+
43+
#include "google/protobuf/compiler/protolite/common.h"
44+
45+
namespace google {
46+
namespace protobuf {
47+
namespace compiler {
48+
namespace j2objc {
49+
50+
class J2ObjCGenerator : public CodeGenerator {
51+
public:
52+
J2ObjCGenerator();
53+
~J2ObjCGenerator();
54+
55+
// implements CodeGenerator ----------------------------------------
56+
bool Generate(const FileDescriptor* file, const std::string& parameter,
57+
GeneratorContext* context, std::string* error) const override;
58+
59+
uint64_t GetSupportedFeatures() const {
60+
return FEATURE_PROTO3_OPTIONAL;
61+
}
62+
};
63+
64+
} // namespace j2objc
65+
} // namespace compiler
66+
} // namespace protobuf
67+
} // namespace google
68+
69+
#endif // GOOGLE_PROTOBUF_COMPILER_J2OBJC_GENERATOR_H__

0 commit comments

Comments
 (0)