|
| 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 | + ) |
0 commit comments