Skip to content

Commit

Permalink
Bazel build support for ign-msgs6
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll committed Apr 27, 2021
1 parent acf45d4 commit 2df1f07
Show file tree
Hide file tree
Showing 4 changed files with 558 additions and 0 deletions.
188 changes: 188 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
load(
"//ign_bazel:build_defs.bzl",
"IGNITION_ROOT",
"IGNITION_VISIBILITY",
"cmake_configure_file",
"generate_include_header",
"generate_yaml",
"ign_config_header",
"ign_export_header",
)
load(
":ign_msg_gen.bzl",
"get_proto_headers",
"ign_msg_gen",
)

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

licenses(["notice"])

exports_files(["LICENSE"])

PROJECT_NAME = "ignition-msgs"

PROJECT_MAJOR = 6

PROJECT_MINOR = 0

PROJECT_PATCH = 0

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

ign_export_header(
name = "include/ignition/msgs/Export.hh",
export_base = "IGNITION_MSGS",
lib_name = "ignition-msgs",
visibility = ["//visibility:private"],
)

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

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

generate_include_header(
name = "messagetypeshh_genrule",
out = "include/ignition/msgs/MessageTypes.hh",
hdrs = get_proto_headers(protos),
strip_prefix = ["ign_msgs"],
)

generate_include_header(
name = "msghh_genrule",
out = "include/ignition/msgs.hh",
hdrs = public_headers_no_gen + [
"include/ignition/msgs/config.hh",
"include/ignition/msgs/Export.hh",
"include/ignition/msgs/MessageTypes.hh",
],
)

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

# Custom Ignition Protoc plugin
cc_binary(
name = "ign_msgs_gen",
srcs = [
"src/Generator.cc",
"src/Generator.hh",
"src/generator_main.cc",
],
deps = [
"@com_google_protobuf//:protobuf",
"@com_google_protobuf//:protoc_lib",
],
)

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

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

# Generate our custom CC files from the protos
ign_msg_gen(
name = "ignmsgs_proto_cc",
deps = [":ignmsgs_proto"],
)

cc_library(
name = "ign_msgs",
srcs = [
"src/Factory.cc",
"src/Filesystem.cc",
"src/Utility.cc",
":ignmsgs_proto_cc",
],
hdrs = public_headers,
includes = ["include"],
deps = [
":ignmsgs_proto_cc",
IGNITION_ROOT + "ign_math",
"@com_google_protobuf//:protobuf",
"@tinyxml2",
],
)

# use shared library only when absolutely needd
cc_binary(
name = "libignition-msgs.so",
srcs = [
"src/ign.cc",
"src/ign.hh",
],
includes = ["include"],
linkshared = True,
linkstatic = True,
deps = [
":ign_msgs",
],
)

[cc_test(
name = src.replace("/", "_").replace(".cc", "").replace("src_", ""),
srcs = [src],
data = [IGNITION_ROOT + "ign_msgs/test:desc/stringmsg.desc"],
deps = [
":ign_msgs",
IGNITION_ROOT + "ign_math",
IGNITION_ROOT + "ign_msgs/test:test_utils",
"@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=libignition-msgs.so",
"PROJECT_VERSION_FULL=%d.%d.%d" % (PROJECT_MAJOR, PROJECT_MINOR, PROJECT_PATCH), # noqa
"IGN_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",
)
76 changes: 76 additions & 0 deletions ign_msg_gen.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
load("@rules_proto//proto:defs.bzl", "ProtoInfo")
load(
":protobuf.bzl",
"declare_out_files",
"get_include_directory",
"get_out_dir",
"proto_path_to_generated_filename",
"protos_from_context",
)

def ign_msg_gen_impl(ctx):
protos = protos_from_context(ctx)
dir_out = get_out_dir(protos, ctx)

cc_files = declare_out_files(protos, ctx, "{}.pb.cc")
hh_files = declare_out_files(protos, ctx, "{}.pb.h")
out_files = cc_files + hh_files

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

args = [
"--cpp_out=" + dir_out.path,
"--plugin=protoc-gen-ignmsgs=" + ctx.executable._plugin.path,
"--ignmsgs_out=" + dir_out.path,
]

for include_dir in include_dirs.to_list():
args.append("--proto_path=" + include_dir)

ctx.actions.run(
outputs = out_files,
inputs = protos,
tools = [ctx.executable._plugin, ctx.executable._protoc],
executable = ctx.executable._protoc,
arguments = args + [proto.path for proto in protos],
)

compilation_context = cc_common.create_compilation_context(
includes = depset([dir_out.path]),
)

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

ign_msg_gen = rule(
attrs = {
"deps": attr.label_list(
mandatory = True,
allow_empty = False,
providers = [ProtoInfo],
),
"_plugin": attr.label(
default = Label("//ign_msgs:ign_msgs_gen"),
executable = True,
cfg = "host",
),
"_protoc": attr.label(
default = Label("@com_google_protobuf//:protoc"),
executable = True,
cfg = "host",
),
},
# We generate .h files, so we need to output to genfiles.
output_to_genfiles = True,
implementation = ign_msg_gen_impl,
)

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
Loading

0 comments on commit 2df1f07

Please sign in to comment.