Skip to content

Commit

Permalink
Support for bazel in garden (gazebosim#325)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll authored Mar 6, 2023
1 parent 5a6edd1 commit 2aaece3
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 227 deletions.
128 changes: 40 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,55 @@ 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",
],
defines = [
'GZ_MSGS_TEST_PATH=\\"msgs/test\\"',
],
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]
35 changes: 26 additions & 9 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,31 @@ function(gz_msgs_protoc)
set(${gz_msgs_protoc_OUTPUT_RUBY_VAR} ${${gz_msgs_protoc_OUTPUT_RUBY_VAR}} PARENT_SCOPE)
endif()

add_custom_command(
OUTPUT ${output_files}
COMMAND Python3::Interpreter
ARGS tools/gz_msgs_generate.py

set(GENERATE_ARGS
--protoc-exec "$<TARGET_FILE:${gz_msgs_protoc_PROTOC_EXEC}>"
--gz-generator-bin "${GZ_MSGS_GEN_EXECUTABLE}"
--generate-cpp "${gz_msgs_protoc_GENERATE_CPP}"
--generate-ruby "${gz_msgs_protoc_GENERATE_RUBY}"
--generate-ignition "TRUE"
--output-cpp-path "${gz_msgs_protoc_OUTPUT_CPP_DIR}"
--output-ruby-path "${gz_msgs_protoc_OUTPUT_RUBY_DIR}"
--proto-path "${gz_msgs_protoc_PROTO_PATH}"
--input-path "${ABS_FIL}"
--generate-ignition
)

if(${gz_msgs_protoc_GENERATE_CPP})
list(APPEND GENERATE_ARGS
--generate-cpp
--output-cpp-path "${gz_msgs_protoc_OUTPUT_CPP_DIR}")
endif()

if(${gz_msgs_protoc_GENERATE_RUBY})
list(APPEND GENERATE_ARGS
--generate-ruby
--output-ruby-path "${gz_msgs_protoc_OUTPUT_RUBY_DIR}")
endif()

add_custom_command(
OUTPUT ${output_files}
COMMAND Python3::Interpreter
ARGS tools/gz_msgs_generate.py ${GENERATE_ARGS}
DEPENDS
${ABS_FIL}
gz_msgs_gen
Expand Down Expand Up @@ -270,6 +282,11 @@ gz_build_tests(TYPE UNIT
TINYXML2::TINYXML2
)

if (TARGET UNIT_Factory_TEST)
target_compile_definitions(UNIT_Factory_TEST
PRIVATE GZ_MSGS_TEST_PATH="${PROJECT_SOURCE_DIR}/test")
endif()

##################################################
# gz msgs command
if(NOT WIN32)
Expand Down
15 changes: 9 additions & 6 deletions src/Factory_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
*/

#include <gtest/gtest.h>

#include <algorithm>
#include <cstddef>
#include <filesystem>

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

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

using namespace gz;

static constexpr const char * kMsgsTestPath = GZ_MSGS_TEST_PATH;

/////////////////////////////////////////////////
TEST(FactoryTest, Type)
{
Expand Down Expand Up @@ -72,11 +74,12 @@ 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";
msgs::Factory::LoadDescriptors(paths);
std::filesystem::path test_path(kMsgsTestPath);
paths += (test_path / "desc").string();
paths += ":";
paths += test_path.string();

msgs::Factory::LoadDescriptors(paths);
msg = msgs::Factory::New("example.msgs.StringMsg");
EXPECT_TRUE(msg.get() != nullptr);
}
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"],
)
Loading

0 comments on commit 2aaece3

Please sign in to comment.