Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bazel updates for Harmonic #397

Merged
merged 26 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
185 changes: 153 additions & 32 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,41 @@ load(
"GZ_FEATURES",
"GZ_ROOT",
"GZ_VISIBILITY",
"gz_configure_file",
"gz_configure_header",
"gz_export_header",
"gz_include_header",
)
load(
"@gz//bazel/lint:lint.bzl",
"add_lint_tests",
)
load(
"@gz//msgs/tools:gz_msgs_generate.bzl",
"get_proto_headers",
"gz_msgs_generate",
"gz_proto_factory",
"gz_proto_library",
)
load("@rules_license//rules:license.bzl", "license")

package(
default_applicable_licenses = [GZ_ROOT + "msgs:license"],
default_visibility = GZ_VISIBILITY,
features = GZ_FEATURES,
)

licenses(["notice"]) # Apache-2.0
license(
name = "license",
package_name = "gz-msgs",
)

licenses(["notice"])

exports_files(["LICENSE"])

gz_configure_header(
name = "msgs_config_hh",
src = "include/gz/msgs/config.hh.in",
src = "core/include/gz/msgs/config.hh.in",
out = "include/gz/msgs/config.hh",
cmakelists = ["CMakeLists.txt"],
mjcarroll marked this conversation as resolved.
Show resolved Hide resolved
package = "msgs",
)
Expand All @@ -37,43 +50,29 @@ gz_export_header(
)

public_headers_no_gen = glob([
"include/gz/msgs/*.hh",
"include/gz/msgs/detail/*.hh",
"core/include/gz/msgs/*.hh",
"core/include/gz/msgs/convert/*.hh",
"core/include/gz/msgs/detail/*.hh",
])

protos = glob(["proto/gz/msgs/*.proto"])

gz_include_header(
name = "messagetypes_hh_genrule",
out = "include/gz/msgs/MessageTypes.hh",
hdrs = get_proto_headers(protos),
strip_prefix = ["gz_msgs"],
)

gz_include_header(
name = "msgs_hh_genrule",
out = "include/gz/msgs.hh",
hdrs = public_headers_no_gen + [
"include/gz/msgs/config.hh",
"include/gz/msgs/Export.hh",
"include/gz/msgs/MessageTypes.hh",
],
)

public_headers = public_headers_no_gen + [
"include/gz/msgs/config.hh",
"include/gz/msgs/Export.hh",
"include/gz/msgs/MessageTypes.hh",
"include/gz/msgs.hh",
]

# Custom Gazebo Protoc plugin
cc_binary(
name = "gz_msgs_gen",
srcs = [
"src/Generator.cc",
"src/Generator.hh",
"src/generator_main.cc",
"core/generator_lite/Generator.cc",
"core/generator_lite/Generator.hh",
"core/generator_lite/generator_main.cc",
],
deps = [
"@com_google_protobuf//:protobuf",
Expand All @@ -91,26 +90,51 @@ proto_library(
],
)

gz_msgs_generate(
gz_proto_library(
name = "gzmsgs_cc_proto",
proto_deps = [":gzmsgs_proto"],
deps = [
":gzmsgs_proto",
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:protobuf",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add @com_google_protobuf//:any_cc_proto to deps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's not a target in this version of protobuf:

$ bazel query "@com_google_protobuf//:*"
@com_google_protobuf//:BUILD
@com_google_protobuf//:any_proto
@com_google_protobuf//:api_proto
@com_google_protobuf//:cc_test_protos_genproto
@com_google_protobuf//:cc_toolchain
@com_google_protobuf//:cc_wkt_protos
@com_google_protobuf//:cc_wkt_protos_genproto
@com_google_protobuf//:compiler_plugin_proto
@com_google_protobuf//:descriptor_proto
@com_google_protobuf//:duration_proto
@com_google_protobuf//:empty_proto
@com_google_protobuf//:field_mask_proto
@com_google_protobuf//:java_toolchain
@com_google_protobuf//:javalite_toolchain
@com_google_protobuf//:linux-aarch64
@com_google_protobuf//:linux-ppc
@com_google_protobuf//:linux-s390x
@com_google_protobuf//:linux-x86_32
@com_google_protobuf//:linux-x86_64
@com_google_protobuf//:macos-aarch64
@com_google_protobuf//:macos-x86_64
@com_google_protobuf//:objectivec
@com_google_protobuf//:protobuf
@com_google_protobuf//:protobuf_headers
@com_google_protobuf//:protobuf_java
@com_google_protobuf//:protobuf_java_util
@com_google_protobuf//:protobuf_javalite
@com_google_protobuf//:protobuf_lite
@com_google_protobuf//:protobuf_objc
@com_google_protobuf//:protobuf_python
@com_google_protobuf//:protobuf_python_genproto
@com_google_protobuf//:protoc
@com_google_protobuf//:protoc_lib
@com_google_protobuf//:python_common_test_protos_genproto
@com_google_protobuf//:python_specific_test_protos_genproto
@com_google_protobuf//:source_context_proto
@com_google_protobuf//:struct_proto
@com_google_protobuf//:timestamp_proto
@com_google_protobuf//:type_proto
@com_google_protobuf//:well_known_type_protos
@com_google_protobuf//:windows-x86_32
@com_google_protobuf//:windows-x86_64
@com_google_protobuf//:wrappers_proto

],
)

gz_proto_factory(
name = "gzmsgs_proto_factory",
cc_output = "core/src/RegisterMsgs.cc",
hh_output = "include/gz/msgs/MessageTypes.hh",
deps = [":gzmsgs_proto"],
)

public_headers = public_headers_no_gen + [
"include/gz/msgs/config.hh",
"include/gz/msgs/Export.hh",
"include/gz/msgs.hh",
"include/gz/msgs/MessageTypes.hh",
]

cc_library(
name = "msgs",
srcs = [
"src/Factory.cc",
"src/Filesystem.cc",
"src/Utility.cc",
":gzmsgs_cc_proto",
"core/src/DynamicFactory.cc",
"core/src/DynamicFactory.hh",
"core/src/Factory.cc",
"core/src/MessageFactory.cc",
"core/src/RegisterMsgs.cc",
"core/src/impl/InstallationDirectories.cc",
],
copts = [
"-Wno-duplicate-decl-specifier",
"-fexceptions",
],
hdrs = public_headers,
includes = ["include"],
includes = [
mjcarroll marked this conversation as resolved.
Show resolved Hide resolved
"core/include",
"core/src",
"include",
],
deps = [
":gzmsgs_cc_proto",
":gzmsgs_proto_factory",
GZ_ROOT + "math",
"@com_google_protobuf//:protobuf",
"@tinyxml2",
Expand Down Expand Up @@ -138,3 +162,100 @@ test_sources = glob(
"@gtest//:gtest_main",
],
) for src in test_sources]

cc_test(
name = "INTEGRATION_headers",
srcs = ["test/integration/headers.cc"],
deps = [
":gzmsgs_cc_proto",
"@gtest",
"@gtest//:gtest_main",
],
)

cc_test(
name = "INTEGRATION_image_msg",
srcs = ["test/integration/image_msg.cc"],
deps = [
":gzmsgs_cc_proto",
"@gtest",
"@gtest//:gtest_main",
],
)

cc_test(
name = "INTEGRATION_Utility",
srcs = ["test/integration/Utility_TEST.cc"],
deps = [
":msgs",
GZ_ROOT + "common/testing",
"@gtest",
"@gtest//:gtest_main",
],
)

cc_test(
name = "INTEGRATION_Factory",
srcs = ["test/integration/Factory_TEST.cc"],
data = ["test/desc/stringmsg.desc"],
defines = [
'GZ_MSGS_TEST_PATH=\\"msgs/test\\"',
],
deps = [
":msgs",
GZ_ROOT + "common/testing",
"@gtest",
"@gtest//:gtest_main",
],
)

cc_test(
name = "INTEGRATION_descriptors",
srcs = ["test/integration/descriptors.cc"],
data = ["test/desc"],
defines = [
'GZ_MSGS_TEST_PATH=\\"msgs/test\\"',
],
deps = [
":msgs",
GZ_ROOT + "common/testing",
"@gtest",
"@gtest//:gtest_main",
],
)

gz_configure_file(
name = "msgs_yaml",
src = "conf/msgs.yaml.in",
out = "msgs.yaml",
defines = [
"PROJECT_NAME_NO_VERSION_LOWER=msgs",
mjcarroll marked this conversation as resolved.
Show resolved Hide resolved
"gz_library_path=msgs/cmdmsgs",
],
package = "msgs",
)

gz_configure_file(
name = "msgs_rb",
src = "core/src/cmd/cmdmsgs.rb.in",
out = "cmdmsgs.rb",
defines = [
"library_location=libgz-msgs.so",
mjcarroll marked this conversation as resolved.
Show resolved Hide resolved
],
package = "msgs",
)

cc_binary(
name = "gz-msgs",
srcs = ["core/src/gz.cc"],
data = [
"cmdmsgs.rb",
"msgs.yaml",
":gzmsgs_proto",
],
linkshared = True,
linkstatic = True,
deps = [":msgs"],
)

add_lint_tests()
116 changes: 116 additions & 0 deletions core/generator_lite/Generator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright 2016 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <algorithm>
#include <filesystem>
#include <iostream>
#include <fstream>
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4100 4512 4127 4068 4244 4267 4251 4146)
#endif

#include <google/protobuf/descriptor.h>
#include <google/protobuf/compiler/code_generator.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/io/zero_copy_stream.h>
#include <google/protobuf/descriptor.pb.h>

#ifdef _MSC_VER
#pragma warning(pop)
#endif

#include "Generator.hh"

namespace google::protobuf::compiler::cpp {

/////////////////////////////////////////////////
Generator::Generator(const std::string &/*_name*/)
{
}

/////////////////////////////////////////////////
Generator::~Generator() = default;

/////////////////////////////////////////////////
bool Generator::Generate(const FileDescriptor *_file,
const std::string &/*_parameter*/,
OutputDirectory *_generatorContext,
std::string * /*_error*/) const
{
std::string delim = ".proto";
auto headerFilename = _file->name();
auto sourceFilename = _file->name();

{
auto pos = headerFilename.rfind(delim);
headerFilename.replace(pos, delim.size(), ".pb.h");
}
{
auto pos = sourceFilename.rfind(delim);
sourceFilename.replace(pos, delim.size(), ".pb.h");
}

{
auto *output = _generatorContext->OpenForInsert(headerFilename, "includes");
auto printer = io::Printer(output, '$');
printer.Print("#include <memory>\n", "name", "includes");
}

{
auto *output = _generatorContext->OpenForInsert(
headerFilename, "namespace_scope");
auto printer = io::Printer(output, '$');

for (auto i = 0; i < _file->message_type_count(); ++i)
{
const auto *desc = _file->message_type(i);
std::string ptrTypes;

// Define std::unique_ptr types for our messages
ptrTypes += "typedef std::unique_ptr<"
+ desc->name() + "> "
+ desc->name() + "UniquePtr;\n";

// Define const std::unique_ptr types for our messages
ptrTypes += "typedef std::unique_ptr<const "
+ desc->name() + "> Const"
+ desc->name() + "UniquePtr;\n";

// Define std::shared_ptr types for our messages
ptrTypes += "typedef std::shared_ptr<"
+ desc->name() + "> "
+ desc->name() + "SharedPtr;\n";

// Define const std::shared_ptr types for our messages
ptrTypes += "typedef std::shared_ptr<const "
+ desc->name() + "> Const"
+ desc->name() + "SharedPtr;\n";

printer.PrintRaw(ptrTypes.c_str());
}
}
return true;
}
} // namespace google::protobuf::compiler::cpp
Loading
Loading