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

Support for bazel in garden #325

Merged
merged 6 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
129 changes: 41 additions & 88 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,46 +1,32 @@
load(
"//gz_bazel:build_defs.bzl",
"@gz//bazel/skylark:build_defs.bzl",
"GZ_FEATURES",
"GZ_ROOT",
"GZ_VISIBILITY",
"cmake_configure_file",
"generate_include_header",
"generate_yaml",
"gz_config_header",
"gz_configure_header",
"gz_export_header",
"gz_include_header",
)
load(
":gz_msg_gen.bzl",
"@gz//msgs/tools:gz_msgs_generate.bzl",
"get_proto_headers",
"gz_msg_gen",
"gz_msgs_generate",
)

package(
default_visibility = GZ_VISIBILITY,
features = [
"-parse_headers",
"-layering_check",
],
features = GZ_FEATURES,
)

licenses(["notice"])
licenses(["notice"]) # Apache-2.0

exports_files(["LICENSE"])

PROJECT_NAME = "gz-msgs"

PROJECT_MAJOR = 8

PROJECT_MINOR = 0

PROJECT_PATCH = 0

# Generates config.hh based on the version numbers in CMake code.
gz_config_header(
name = "config",
gz_configure_header(
name = "msgs_config_hh",
src = "include/gz/msgs/config.hh.in",
cmakelists = ["CMakeLists.txt"],
project_name = PROJECT_NAME,
project_version = (PROJECT_MAJOR, PROJECT_MINOR, PROJECT_PATCH),
package = "msgs",
)

gz_export_header(
Expand All @@ -57,15 +43,15 @@ public_headers_no_gen = glob([

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

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

generate_include_header(
name = "msghh_genrule",
gz_include_header(
name = "msgs_hh_genrule",
out = "include/gz/msgs.hh",
hdrs = public_headers_no_gen + [
"include/gz/msgs/config.hh",
Expand Down Expand Up @@ -100,89 +86,56 @@ proto_library(
name = "gzmsgs_proto",
srcs = protos,
strip_import_prefix = "proto",
deps = [
"@com_google_protobuf//:any_proto",
],
)

# Create a library of our protobuf message files
proto_library(
name = "gzmsgs_proto_public",
srcs = protos,
strip_import_prefix = "proto",
)

# Generate our custom CC files from the protos
gz_msg_gen(
name = "gzmsgs_proto_cc",
deps = [":gzmsgs_proto"],
gz_msgs_generate(
name = "gzmsgs_cc_proto",
deps = [
":gzmsgs_proto",
"@com_google_protobuf//:any_proto",
],
)

cc_library(
name = "gz_msgs",
name = "msgs",
srcs = [
"src/Factory.cc",
"src/Filesystem.cc",
"src/Utility.cc",
":gzmsgs_proto_cc",
":gzmsgs_cc_proto",
],
hdrs = public_headers,
includes = ["include"],
deps = [
":gzmsgs_proto_cc",
GZ_ROOT + "gz_math",
":gzmsgs_cc_proto",
GZ_ROOT + "math",
"@com_google_protobuf//:protobuf",
"@tinyxml2",
],
)

# use shared library only when absolutely needd
cc_binary(
name = "libgz-msgs.so",
srcs = [
"src/gz.cc",
"src/gz.hh",
],
includes = ["include"],
linkshared = True,
linkstatic = True,
deps = [
":gz_msgs",
],
test_sources = glob(
include = ["src/*_TEST.cc"],
exclude = [],
)

[cc_test(
name = src.replace("/", "_").replace(".cc", "").replace("src_", ""),
srcs = [src],
data = [GZ_ROOT + "gz_msgs/test:desc/stringmsg.desc"],
data = [
"test/desc",
],
env = {
"GZ_BAZEL": "1",
"GZ_BAZEL_PATH": "msgs",
},
deps = [
":gz_msgs",
GZ_ROOT + "gz_math",
GZ_ROOT + "gz_msgs/test:test_utils",
":msgs",
GZ_ROOT + "common/testing",
"@gtest",
"@gtest//:gtest_main",
],
) for src in glob(
[
"src/*_TEST.cc",
],
)]

cmake_configure_file(
name = "msgs.rb",
src = "src/cmd/cmdmsgs.rb.in",
out = "cmdmsgs.rb",
cmakelists = ["CMakeLists.txt"],
defines = [
"library_location=libgz-msgs.so",
"PROJECT_VERSION_FULL=%d.%d.%d" % (PROJECT_MAJOR, PROJECT_MINOR, PROJECT_PATCH), # noqa
"GZ_LIBRARY_NAME=%s" % [PROJECT_NAME],
],
)

CMDS = " - msg : Print information about messages."

generate_yaml(
name = "msgs",
commands = CMDS,
library_name = PROJECT_NAME,
library_version = "%d.%d.%d" % (PROJECT_MAJOR, PROJECT_MINOR, PROJECT_PATCH),
ruby_target = "msgs.rb",
)
) for src in test_sources]
9 changes: 4 additions & 5 deletions src/Factory_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

#include "gz/msgs/vector3d.pb.h"
#include "gz/msgs/serialized_map.pb.h"

#include "gz/msgs/Factory.hh"
#include "test_config.hh"

#include <gz/common/testing/TestPaths.hh>

using namespace gz;

Expand Down Expand Up @@ -72,9 +72,8 @@ TEST(FactoryTest, NewDynamicFactory)
auto msg = msgs::Factory::New("example.msgs.StringMsg");
EXPECT_TRUE(msg.get() == nullptr);

paths =
PROJECT_SOURCE_PATH "/test/desc:"
PROJECT_SOURCE_PATH "/test";
paths = gz::common::testing::TestFile("desc") + ":" +
gz::common::testing::TestFile("");
msgs::Factory::LoadDescriptors(paths);

msg = msgs::Factory::New("example.msgs.StringMsg");
Expand Down
59 changes: 0 additions & 59 deletions test/BUILD.bazel

This file was deleted.

7 changes: 7 additions & 0 deletions tools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@gz//bazel/skylark:gz_py.bzl", "gz_py_binary")

gz_py_binary(
name = "gz_msgs_generate_py",
srcs = ["gz_msgs_generate.py"],
visibility = ["//visibility:public"],
)
104 changes: 104 additions & 0 deletions tools/gz_msgs_generate.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
load("@rules_proto//proto:defs.bzl", "ProtoInfo")
load(
"@gz//bazel/skylark:protobuf.bzl",
"declare_out_files",
"get_include_directory",
"get_out_dir",
"proto_path_to_generated_filename",
"protos_from_context",
)

_VIRTUAL_IMPORTS = "/_virtual_imports/"

def _get_detail_directory(hh_file, ctx):
base = hh_file.path[hh_file.path.index(_VIRTUAL_IMPORTS) + 1:]
base = base.split("/")
base.insert(-1, "details")
return ctx.actions.declare_file("/".join(base))

def get_proto_headers(protos):
out = []
for proto in protos:
split = proto.split("/")[1:]
split[2] = split[2].replace(".proto", ".pb.h")
out.append("/".join(split))
return out

def _gz_msgs_generate_impl(ctx):
protos = protos_from_context(ctx)
out_dir = get_out_dir(protos, ctx)

include_dirs = depset([get_include_directory(proto) for proto in protos])

arguments = [
"--protoc-exec=" + ctx.executable._protoc.path,
"--gz-generator-bin=" + ctx.executable._gz_gen_bin.path,
"--generate-cpp",
"--output-cpp-path=" + out_dir.path,
]

for proto in protos:
arguments.append("--input-path=" + proto.path)

for include_dir in include_dirs.to_list():
arguments.append("--proto-path=" + include_dir)

out_protos = [proto for proto in protos if proto.path.find("gz/msgs") > 0]

cc_files = declare_out_files(out_protos, ctx, "{}.pb.cc")
hh_files = declare_out_files(out_protos, ctx, "{}.pb.h")
detail_hh_files = [_get_detail_directory(file, ctx) for file in hh_files]
out_files = cc_files + hh_files + detail_hh_files

ctx.actions.run(
inputs = protos,
outputs = out_files,
arguments = arguments,
executable = ctx.executable.gz_msgs_generate_py,
tools = [ctx.executable._protoc, ctx.executable._gz_gen_bin, ctx.executable.gz_msgs_generate_py],
)

compilation_context = cc_common.create_compilation_context(
headers = depset(out_files),
system_includes = depset([out_dir.path]),
)

return [
DefaultInfo(files = depset(out_files)),
CcInfo(compilation_context = compilation_context),
]

_gz_msgs_generate_gen = rule(
attrs = {
"deps": attr.label_list(
mandatory = True,
allow_empty = False,
providers = [ProtoInfo],
),
"_protoc": attr.label(
default = Label("@com_google_protobuf//:protoc"),
executable = True,
cfg = "host",
),
"_gz_gen_bin": attr.label(
default = Label("@gz//msgs:gz_msgs_gen"),
executable = True,
cfg = "host",
),
"gz_msgs_generate_py": attr.label(
default = Label("@gz//msgs/tools:gz_msgs_generate_py"),
executable = True,
cfg = "host",
),
},
output_to_genfiles = True,
implementation = _gz_msgs_generate_impl,
)

def gz_msgs_generate(
deps,
**kwargs):
_gz_msgs_generate_gen(
deps = deps,
**kwargs
)
Loading