Skip to content

Commit 7f47134

Browse files
authored
Hotfix for rosidl cpp visibility header addition (#371)
1 parent ab1f7e6 commit 7f47134

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

bazel_ros2_rules/ros2/rosidl.bzl

+82-2
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,92 @@ def _rosidl_generate_genrule_impl(ctx):
6666
args.add(ctx.attr.group)
6767
args.add_all(ctx.files.interfaces, map_each = _as_idl_tuple)
6868
inputs = ctx.files.interfaces + ctx.files.includes
69+
70+
expected_outputs = []
71+
cpp_visibility_header_file = None
72+
73+
# The cpp visibility header is not generated by rosidl generate, so we must
74+
# handle its generation separately.
75+
for generated_source in ctx.outputs.generated_sources:
76+
if "cpp__visibility_control.hpp" in generated_source.path:
77+
cpp_visibility_header_file = generated_source
78+
else:
79+
expected_outputs.append(generated_source)
80+
6981
ctx.actions.run_shell(
7082
tools = [ctx.executable._tool],
7183
arguments = [args],
7284
inputs = inputs,
7385
command = "{} $@ > /dev/null".format(
7486
ctx.executable._tool.path,
7587
),
76-
outputs = ctx.outputs.generated_sources,
88+
outputs = expected_outputs,
7789
)
7890

91+
# This is a temporary hack to get the build working again. This should be
92+
# redone so that the real template file from the ROS distribution is used,
93+
# rather than our adaptation below.
94+
# TODO(calderpg-tri) Replace this hack.
95+
if cpp_visibility_header_file != None:
96+
# This template is adapted directly from the mentioned template file.
97+
cpp_visibility_header_template = (
98+
"""
99+
// generated from rosidl_generator_cpp/resource/rosidl_generator_cpp__visibility_control.hpp.in
100+
// generated code does not contain a copyright notice
101+
102+
#ifndef {package_name_upper}__MSG__ROSIDL_GENERATOR_CPP__VISIBILITY_CONTROL_HPP_
103+
#define {package_name_upper}__MSG__ROSIDL_GENERATOR_CPP__VISIBILITY_CONTROL_HPP_
104+
105+
#ifdef __cplusplus
106+
extern "C"
107+
{{
108+
#endif
109+
110+
// This logic was borrowed (then namespaced) from the examples on the gcc wiki:
111+
// https://gcc.gnu.org/wiki/Visibility
112+
113+
#if defined _WIN32 || defined __CYGWIN__
114+
#ifdef __GNUC__
115+
#define ROSIDL_GENERATOR_CPP_EXPORT_{package_name} __attribute__ ((dllexport))
116+
#define ROSIDL_GENERATOR_CPP_IMPORT_{package_name} __attribute__ ((dllimport))
117+
#else
118+
#define ROSIDL_GENERATOR_CPP_EXPORT_{package_name} __declspec(dllexport)
119+
#define ROSIDL_GENERATOR_CPP_IMPORT_{package_name} __declspec(dllimport)
120+
#endif
121+
#ifdef ROSIDL_GENERATOR_CPP_BUILDING_DLL_{package_name}
122+
#define ROSIDL_GENERATOR_CPP_PUBLIC_{package_name} ROSIDL_GENERATOR_CPP_EXPORT_{package_name}
123+
#else
124+
#define ROSIDL_GENERATOR_CPP_PUBLIC_{package_name} ROSIDL_GENERATOR_CPP_IMPORT_{package_name}
125+
#endif
126+
#else
127+
#define ROSIDL_GENERATOR_CPP_EXPORT_{package_name} __attribute__ ((visibility("default")))
128+
#define ROSIDL_GENERATOR_CPP_IMPORT_{package_name}
129+
#if __GNUC__ >= 4
130+
#define ROSIDL_GENERATOR_CPP_PUBLIC_{package_name} __attribute__ ((visibility("default")))
131+
#else
132+
#define ROSIDL_GENERATOR_CPP_PUBLIC_{package_name}
133+
#endif
134+
#endif
135+
136+
#ifdef __cplusplus
137+
}}
138+
#endif
139+
140+
#endif // {package_name_upper}__MSG__ROSIDL_GENERATOR_CPP__VISIBILITY_CONTROL_HPP_
141+
""" # noqa
142+
)
143+
144+
package_name = str(ctx.attr.group)
145+
package_name_upper = package_name.upper()
146+
147+
ctx.actions.write(
148+
output = cpp_visibility_header_file,
149+
content = cpp_visibility_header_template.format(
150+
package_name = package_name,
151+
package_name_upper = package_name_upper,
152+
),
153+
)
154+
79155
rosidl_generate_genrule = rule(
80156
attrs = dict(
81157
generated_sources = attr.output_list(mandatory = True),
@@ -492,7 +568,8 @@ def rosidl_cc_library(
492568
"""
493569
include, root = _deduce_source_paths(group, "cpp")
494570

495-
generated_cc_headers = []
571+
visibility_header = "msg/rosidl_generator_cpp__visibility_control.hpp"
572+
generated_cc_headers = ["{}/{}".format(root, visibility_header)]
496573
for ifc in interfaces:
497574
parent, basename = _deduce_source_parts(ifc)
498575
generated_cc_headers.append(
@@ -507,6 +584,9 @@ def rosidl_cc_library(
507584
generated_cc_headers.append(
508585
"{}/{}/detail/{}__traits.hpp".format(root, parent, basename),
509586
)
587+
generated_cc_headers.append(
588+
"{}/{}/detail/{}__type_support.hpp".format(root, parent, basename),
589+
)
510590

511591
rosidl_generate_genrule(
512592
name = _make_private_name(name, "_gen"),

0 commit comments

Comments
 (0)