Skip to content

Commit ca9a37d

Browse files
shameekgangulyiche033
authored andcommitted
Add bazel build for ogre2 engine (#1157)
Signed-off-by: Shameek Ganguly <[email protected]> (cherry picked from commit 23cdd5a)
1 parent 3e1e838 commit ca9a37d

File tree

4 files changed

+212
-0
lines changed

4 files changed

+212
-0
lines changed

MODULE.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ module(
44
repo_name = "org_gazebosim_gz-rendering",
55
)
66

7+
bazel_dep(name = "bazel_skylib", version = "1.7.1")
8+
bazel_dep(name = "egl-registry", version = "0.0.0-20250527")
79
bazel_dep(name = "googletest", version = "1.15.2")
10+
bazel_dep(name = "ogre-next", version = "2.3.3")
11+
bazel_dep(name = "rules_cc", version = "0.1.1")
812
bazel_dep(name = "rules_license", version = "1.0.0")
913

1014
# Gazebo Dependencies

bazel/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2+
3+
bzl_library(
4+
name = "rules",
5+
srcs = ["gz_rendering_engine_libraries.bzl"],
6+
visibility = ["//:__subpackages__"],
7+
)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
2+
load("@rules_cc//cc:cc_library.bzl", "cc_library")
3+
4+
visibility("public")
5+
6+
def _generate_static_plugin_src_impl(ctx):
7+
ctx.actions.expand_template(
8+
template = ctx.file.plugin_cc,
9+
output = ctx.outputs.out,
10+
substitutions = {
11+
# Macro substitutions:
12+
"GZ_ADD_PLUGIN": "GZ_ADD_STATIC_PLUGIN",
13+
"GZ_ADD_PLUGIN_ALIAS": "GZ_ADD_STATIC_PLUGIN_ALIAS",
14+
# Header substitutions:
15+
"plugin/Register.hh": "plugin/RegisterStatic.hh",
16+
"plugin/RegisterMore.hh": "plugin/RegisterStatic.hh",
17+
},
18+
)
19+
20+
# This rule performs a substitution to link the plugin class to the static
21+
# plugin registry instead of the plugin hook registry for dynamic loading.
22+
_generate_static_plugin_src = rule(
23+
attrs = {
24+
"plugin_cc": attr.label(allow_single_file = True, mandatory = True),
25+
"out": attr.output(mandatory = True),
26+
},
27+
implementation = _generate_static_plugin_src_impl,
28+
)
29+
30+
def gz_rendering_engine_libraries(static_lib_name, so_lib_name, srcs, includes = [], **kwargs):
31+
"""
32+
Adds two library targets for the render engine plugin for static and dynamic loading respectively
33+
34+
Args:
35+
static_lib_name: Name of the `cc_library` target with static linking.
36+
Note that the plugin registration macro is substituted with
37+
`GZ_ADD_STATIC_PLUGIN` in the source file for this target to register
38+
the plugin with the static registry.
39+
The `alwayslink` attribute of this target is set to True, so that
40+
downstream linking preserves symbols which are not referenced
41+
explicitly.
42+
so_lib_name: Name of the `cc_binary` shared library target which can be
43+
loaded at runtime. Set this to empty string if the shared library
44+
target should not be added.
45+
srcs: List of source files including private headers. For example, this
46+
can be a globbed list of *.cc and *.hh files.
47+
Any test files should be excluded and can be added to separate
48+
`cc_test` targets.
49+
includes: List of include dirs to be added to the `cc_library` and
50+
`cc_binary` targets
51+
**kwargs: Forwarded to both the `cc_library` and `cc_binary` targets.
52+
"""
53+
if not static_lib_name:
54+
fail("The static_lib_name field must be non-empty.")
55+
56+
supported_cc_extensions = ["cc", "cpp"]
57+
cc_files = [f for f in srcs if f.split(".")[-1] in supported_cc_extensions]
58+
non_cc_files = [f for f in srcs if f not in cc_files]
59+
60+
if not cc_files:
61+
fail("Did not find any .cc files in the provided srcs for library with static_lib_name '", static_lib_name, "'.")
62+
63+
plugin_dir = "/".join(cc_files[0].split("/")[:-1])
64+
65+
# Run the _generate_static_plugin_src rule to generate modified source files
66+
# suitable for registering the render engine plugin with the static plugin
67+
# registry. Ideally the rule only needs to be run on the source file which
68+
# registers the render engine plugin with the GZ_ADD_PLUGIN macro. However,
69+
# in the bazel analysis phase, there is no way to determine whether a
70+
# particular .cc file registers a render engine plugin or not. To circumvent
71+
# this limitation, the _generate_static_plugin_src rule is simply run for
72+
# all source files. This has a very small overhead of writing out the
73+
# original file as-is into a new source file for the input source files
74+
# which do not register a render engine plugin.
75+
static_cc_files = []
76+
for cc_file in cc_files:
77+
name_without_extension = ".".join(cc_file.split(".")[:-1])
78+
static_plugin_src_gen_without_extension = name_without_extension + "_static_plugin"
79+
static_plugin_src_gen = static_plugin_src_gen_without_extension + ".cc"
80+
_generate_static_plugin_src(
81+
name = static_plugin_src_gen_without_extension,
82+
plugin_cc = cc_file,
83+
out = static_plugin_src_gen,
84+
)
85+
static_cc_files.append(static_plugin_src_gen)
86+
87+
cc_library(
88+
name = static_lib_name,
89+
alwayslink = True,
90+
includes = includes + [plugin_dir],
91+
srcs = non_cc_files + static_cc_files,
92+
**kwargs
93+
)
94+
95+
if so_lib_name:
96+
cc_binary(
97+
name = so_lib_name,
98+
linkshared = True,
99+
includes = includes,
100+
srcs = srcs,
101+
**kwargs
102+
)

ogre2/BUILD.bazel

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Bazel targets for Ogre2 render engine plugin.
2+
load("@rules_gazebo//gazebo:headers.bzl", "gz_export_header")
3+
load("//bazel:gz_rendering_engine_libraries.bzl", "gz_rendering_engine_libraries")
4+
5+
gz_export_header(
6+
name = "Export",
7+
out = "include/gz/rendering/ogre2/Export.hh",
8+
export_base = "GZ_RENDERING_OGRE2",
9+
lib_name = "gz-rendering",
10+
)
11+
12+
filegroup(
13+
name = "ogre_media",
14+
srcs = glob(["src/media/**"]),
15+
)
16+
17+
cc_library(
18+
name = "terra",
19+
srcs = glob([
20+
"src/terrain/Terra/src/*.cpp",
21+
"src/terrain/Terra/src/Hlms/*.cpp",
22+
"src/terrain/Terra/src/Hlms/*.cpp.inc",
23+
"src/terrain/Terra/src/Hlms/PbsListener/*.cpp",
24+
]),
25+
hdrs = glob([
26+
"src/terrain/Terra/include/Terra/*.h",
27+
"src/terrain/Terra/include/Terra/Hlms/*.h",
28+
"src/terrain/Terra/include/Terra/Hlms/PbsListener/*.h",
29+
]),
30+
copts = [
31+
"-Wno-ctad-maybe-unsupported",
32+
"-Wno-unused-value",
33+
"-Wno-non-virtual-dtor",
34+
"-fexceptions",
35+
],
36+
includes = ["src/terrain/Terra/include"],
37+
deps = [
38+
"@ogre-next//:hlms_pbs",
39+
"@ogre-next//:ogre-main",
40+
],
41+
)
42+
43+
# Targets including this under `deps` (for static linking) or under `data` (for
44+
# external library) should also include data deps on the Ogre-Next RenderSystem
45+
# libraries and Plugin libraries that need to be supported. e.g.
46+
# ```
47+
# cc_library(
48+
# name = "my_gz_sim_server_with_ogre2_rendering",
49+
# srcs = my_sources_list,
50+
# data = [
51+
# "@ogre-next//:RenderSystem_GL3Plus.so",
52+
# ],
53+
# deps = [
54+
# "@gz-sim",
55+
# "@gz-sim//:gz-sim-sensors-system-static",
56+
# "@gz-rendering//ogre2:gz-rendering-ogre2-engine-static",
57+
# ]
58+
# )
59+
# ```
60+
#
61+
# Also, the `OGRE2_RESOURCE_PATH` env variable must be set from the runfiles
62+
# path.
63+
gz_rendering_engine_libraries(
64+
srcs = glob([
65+
"include/gz/rendering/ogre2/*.hh",
66+
"src/*.cc",
67+
"src/*.hh",
68+
]) + [
69+
"include/gz/rendering/ogre2/Export.hh",
70+
],
71+
copts = [
72+
"-Wno-ctad-maybe-unsupported",
73+
"-Wno-unused-value",
74+
"-Wno-non-virtual-dtor",
75+
"-fexceptions",
76+
],
77+
data = [":ogre_media"],
78+
includes = ["include"],
79+
local_defines = [
80+
"HAVE_EGL=1",
81+
"OGRE2_RESOURCE_PATH='\"unused\"'",
82+
"OGRE2_VERSION='\"unused\"'",
83+
],
84+
so_lib_name = "libgz-rendering-ogre2.so",
85+
static_lib_name = "gz-rendering-ogre2-engine-static",
86+
visibility = ["//visibility:public"],
87+
deps = [
88+
":terra",
89+
"//:gz-rendering",
90+
"@egl-registry//:EGL_headers",
91+
"@gz-common",
92+
"@gz-math//eigen3",
93+
"@gz-plugin//:register",
94+
"@ogre-next//:hlms_pbs",
95+
"@ogre-next//:hlms_unlit",
96+
"@ogre-next//:ogre-main",
97+
"@ogre-next//:overlay",
98+
],
99+
)

0 commit comments

Comments
 (0)