Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions phaser/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,31 @@ cc_test(
"//phaser/runtime:phaser_runtime",
"//phaser/testdata:ros_compile_cc_proto",
"//phaser/testdata:ros_compile_phaser",
"@com_google_absl//absl/types:span",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "ros_metadata_protobuf_frontend_test",
srcs = ["ros_metadata_protobuf_frontend_test.cc"],
copts = PHASER_COPTS,
data = ["valgrind.supp"],
deps = [
"//phaser/runtime:phaser_runtime",
"//phaser/testdata:ros_metadata_protobuf_phaser",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "ros_metadata_ros_frontend_test",
srcs = ["ros_metadata_ros_frontend_test.cc"],
copts = PHASER_COPTS,
data = ["valgrind.supp"],
deps = [
"//phaser/runtime:phaser_runtime",
"//phaser/testdata:ros_metadata_ros_phaser",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
51 changes: 42 additions & 9 deletions phaser/compiler/gen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ static void WriteToZeroCopyStream(
static std::string GeneratedFilename(const std::filesystem::path& package_name,
const std::filesystem::path& target_name,
std::string filename) {
if (filename.rfind("../", 0) == 0) {
// Bazel represents external sources as ../<repo>/<path>. Keep only the
// repository-relative path so it cannot escape target_name.
const size_t repository_end = filename.find('/', 3);
filename = filename.substr(repository_end + 1);
} else if (filename.rfind("external/", 0) == 0) {
// protoc can instead preserve the execroot-relative external/<repo>/ form.
const size_t repository_end = filename.find('/', sizeof("external/") - 1);
filename = filename.substr(repository_end + 1);
}
size_t virtual_imports = filename.find("_virtual_imports/");
if (virtual_imports != std::string::npos) {
// This is something like:
Expand All @@ -85,6 +95,17 @@ static std::string GeneratedFilename(const std::filesystem::path& package_name,
return package_name / target_name / filename;
}

static std::string GeneratedIncludeFilename(
const std::filesystem::path& package_name,
const std::filesystem::path& target_name, const std::string& filename) {
std::string result = GeneratedFilename(package_name, target_name, filename);
if (result.rfind("external/", 0) == 0) {
const size_t repository_end = result.find('/', sizeof("external/") - 1);
result = result.substr(repository_end + 1);
}
return result;
}

bool CodeGenerator::Generate(
const google::protobuf::FileDescriptor* file, const std::string& parameter,
google::protobuf::compiler::GeneratorContext* generator_context,
Expand All @@ -107,6 +128,10 @@ bool CodeGenerator::Generate(
generate_active_message_ = option.second.empty() ||
option.second == "true" ||
option.second == "1";
} else if (option.first == "ros_metadata") {
generate_ros_metadata_ = option.second.empty() ||
option.second == "true" ||
option.second == "1";
} else if (option.first == "frontend") {
if (option.second == "protobuf" || option.second.empty()) {
frontend_style_ = FrontendStyle::kProtobuf;
Expand All @@ -123,19 +148,24 @@ bool CodeGenerator::Generate(

const FrontendStyle effective_frontend =
EffectiveFrontendStyle(file, frontend_style_);
const bool effective_ros_metadata =
generate_ros_metadata_ &&
file->package().rfind("google.protobuf", 0) != 0;

// Custom option schemas and other message-free protos need no C++ output.
// descriptor.proto is imported for extensions but must not be emitted as a
// Phaser message graph (it is huge and not a runtime payload type here).
if (file->message_type_count() == 0 && file->enum_type_count() == 0) {
return true;
}
if (file->name() == std::string("google/protobuf/descriptor.proto")) {
if (file->name() == std::string("google/protobuf/descriptor.proto") ||
file->name() == std::string("phaser/options.proto")) {
return true;
}

Generator gen(file, added_namespace_, package_name_, target_name_,
generate_active_message_, effective_frontend);
generate_active_message_, effective_frontend,
effective_ros_metadata);

std::string filename =
GeneratedFilename(package_name_, target_name_, std::string(file->name()));
Expand Down Expand Up @@ -207,17 +237,19 @@ void Generator::CloseNamespace(std::ostream& os) {
Generator::Generator(const google::protobuf::FileDescriptor* file,
const std::string& ns, const std::string& pn,
const std::string& tn, bool generate_active_message,
FrontendStyle frontend_style)
FrontendStyle frontend_style,
bool generate_ros_metadata)
: file_(file),
added_namespace_(ns),
package_name_(pn),
target_name_(tn),
generate_active_message_(generate_active_message),
generate_ros_metadata_(generate_ros_metadata),
frontend_style_(frontend_style) {
for (int i = 0; i < file->message_type_count(); i++) {
message_gens_.push_back(std::make_unique<MessageGenerator>(
file->message_type(i), added_namespace_, std::string(file->package()),
generate_active_message_, frontend_style_));
generate_active_message_, frontend_style_, generate_ros_metadata_));
}
// Enums
for (int i = 0; i < file->enum_type_count(); i++) {
Expand All @@ -240,11 +272,12 @@ void Generator::GenerateHeaders(std::ostream& os, std::string* error) {
if (dep->message_type_count() == 0 && dep->enum_type_count() == 0) {
continue;
}
if (dep->name() == std::string("google/protobuf/descriptor.proto")) {
if (dep->name() == std::string("google/protobuf/descriptor.proto") ||
dep->name() == std::string("phaser/options.proto")) {
continue;
}
std::string base = GeneratedFilename(
package_name_, target_name_, std::string(dep->name()));
std::string base = GeneratedIncludeFilename(package_name_, target_name_,
std::string(dep->name()));
std::filesystem::path p(base);
p.replace_extension(".phaser.h");
os << "#include \"" << p.string() << "\"\n";
Expand Down Expand Up @@ -274,8 +307,8 @@ void Generator::GenerateHeaders(std::ostream& os, std::string* error) {
}

void Generator::GenerateSources(std::ostream& os) {
std::filesystem::path p(GeneratedFilename(package_name_, target_name_,
std::string(file_->name())));
std::filesystem::path p(GeneratedIncludeFilename(package_name_, target_name_,
std::string(file_->name())));
p.replace_extension(".phaser.h");
os << "#include \"" << p.string() << "\"\n";

Expand Down
5 changes: 4 additions & 1 deletion phaser/compiler/gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CodeGenerator : public google::protobuf::compiler::CodeGenerator {
// field. Enabled via the `active_message=true` plugin command-line option
// (set by phaser_library(enable_active_message = True)).
mutable bool generate_active_message_ = false;
mutable bool generate_ros_metadata_ = false;
mutable FrontendStyle frontend_style_ = FrontendStyle::kProtobuf;
};

Expand All @@ -44,7 +45,8 @@ class Generator {
Generator(const google::protobuf::FileDescriptor* file, const std::string& ns,
const std::string& pn, const std::string& tn,
bool generate_active_message = false,
FrontendStyle frontend_style = FrontendStyle::kProtobuf);
FrontendStyle frontend_style = FrontendStyle::kProtobuf,
bool generate_ros_metadata = false);

void GenerateHeaders(std::ostream& os, std::string* error);
void GenerateSources(std::ostream& os);
Expand All @@ -60,6 +62,7 @@ class Generator {
const std::string& package_name_;
const std::string& target_name_;
bool generate_active_message_;
bool generate_ros_metadata_;
FrontendStyle frontend_style_;
};

Expand Down
Loading
Loading