Add batch 1 (YosysHQ_picorv32, alexforencich_verilog-ethernet, The-OpenROAD-Project_OpenROAD, darklife_darkriscv, corundum_corundum)
f61bac1 verified | # SPDX-License-Identifier: BSD-3-Clause | |
| # Copyright (c) 2025-2025, The OpenROAD Authors | |
| """A TCL SWIG wrapping rule. | |
| These rules generate a C++ src file that is expected to be used as srcs in | |
| cc_library or cc_binary rules. See below for expected usage. | |
| cc_library(srcs=[":tcl_foo"]) | |
| tcl_wrap_cc(name = "tcl_foo", srcs=["exception.i"],...) | |
| """ | |
| load( | |
| "//bazel:swig_common.bzl", | |
| "get_transitive_includes", | |
| "get_transitive_options", | |
| "get_transitive_srcs", | |
| ) | |
| TclSwigInfo = provider( | |
| "TclSwigInfo for taking dependencies on other swig info rules", | |
| fields = [ | |
| "transitive_srcs", | |
| "includes", | |
| "swig_options", | |
| ], | |
| ) | |
| def _tcl_wrap_cc_impl(ctx): | |
| """Generates a single C++ file from the provided srcs in a DefaultInfo. | |
| """ | |
| if len(ctx.files.srcs) > 1 and not ctx.attr.root_swig_src: | |
| fail("If multiple src files are provided, root_swig_src must be specified.") | |
| swig_lib_dir = ctx.file._swig_swg.dirname | |
| root_file = ctx.file.root_swig_src or ctx.files.srcs[0] | |
| outfile_name = ctx.attr.out or (ctx.attr.name + ".cc") | |
| output_file = ctx.actions.declare_file(outfile_name) | |
| include_root_directory = "" | |
| if ctx.label.workspace_root: | |
| include_root_directory = ctx.label.workspace_root + "/" | |
| if ctx.label.package: | |
| include_root_directory += ctx.label.package + "/" | |
| src_inputs = get_transitive_srcs( | |
| TclSwigInfo, | |
| ctx.files.srcs + ctx.files.root_swig_src, | |
| ctx.attr.deps, | |
| ) | |
| includes_paths = get_transitive_includes( | |
| TclSwigInfo, | |
| ["{}{}".format(include_root_directory, include) for include in ctx.attr.swig_includes], | |
| ctx.attr.deps, | |
| ) | |
| swig_options = get_transitive_options(TclSwigInfo, ctx.attr.swig_options, ctx.attr.deps) | |
| args = ctx.actions.args() | |
| args.add("-tcl8") | |
| args.add("-c++") | |
| if ctx.attr.module: | |
| args.add("-module") | |
| args.add(ctx.attr.module) | |
| if ctx.attr.namespace_prefix: | |
| args.add("-namespace") | |
| args.add("-prefix") | |
| args.add(ctx.attr.namespace_prefix) | |
| args.add_all(swig_options.to_list()) | |
| args.add_all(includes_paths.to_list(), format_each = "-I%s") | |
| args.add("-o") | |
| args.add(output_file.path) | |
| args.add(root_file.path) | |
| ctx.actions.run( | |
| outputs = [output_file], | |
| inputs = src_inputs, | |
| arguments = [args], | |
| env = {"SWIG_LIB": swig_lib_dir}, | |
| tools = ctx.files._swig_lib, | |
| executable = ctx.executable._swig, | |
| ) | |
| output_files = [output_file] | |
| if ctx.attr.runtime_header: | |
| runtime_header = ctx.actions.declare_file(ctx.attr.runtime_header) | |
| runtime_args = ctx.actions.args() | |
| runtime_args.add("-tcl8") | |
| runtime_args.add("-external-runtime") | |
| runtime_args.add(runtime_header) | |
| ctx.actions.run( | |
| outputs = [runtime_header], | |
| inputs = depset(ctx.files._swig_lib), | |
| arguments = [runtime_args], | |
| env = {"SWIG_LIB": swig_lib_dir}, | |
| executable = ctx.executable._swig, | |
| tools = ctx.files._swig_lib, | |
| toolchain = None, | |
| ) | |
| output_files.append(runtime_header) | |
| return [ | |
| DefaultInfo(files = depset(output_files)), | |
| TclSwigInfo( | |
| transitive_srcs = src_inputs, | |
| includes = includes_paths, | |
| swig_options = swig_options, | |
| ), | |
| ] | |
| tcl_wrap_cc = rule( | |
| implementation = _tcl_wrap_cc_impl, | |
| attrs = { | |
| "deps": attr.label_list( | |
| allow_empty = True, | |
| doc = "tcl_wrap_cc dependencies", | |
| providers = [TclSwigInfo], | |
| ), | |
| "module": attr.string( | |
| mandatory = False, | |
| default = "", | |
| doc = "swig module", | |
| ), | |
| "namespace_prefix": attr.string( | |
| mandatory = False, | |
| default = "", | |
| doc = "swig namespace prefix", | |
| ), | |
| "out": attr.string( | |
| doc = "The name of the C++ source file generated by these rules. If not set, defaults to '<name>.cc'.", | |
| ), | |
| "root_swig_src": attr.label( | |
| allow_single_file = [".swig", ".i"], | |
| doc = """If more than one swig file is included in this rule. | |
| The root file must be explicitly provided. This is the file which will be passed to | |
| swig for generation.""", | |
| ), | |
| "runtime_header": attr.string( | |
| doc = "If non-empty, also generate the SWIG external runtime " + | |
| "header under this filename (via `swig -external-runtime`).", | |
| ), | |
| "srcs": attr.label_list( | |
| allow_empty = False, | |
| allow_files = [".i", ".swig", ".h", ".hpp", ".hh"], | |
| doc = "Swig files that generate C++ files", | |
| ), | |
| "swig_includes": attr.string_list( | |
| doc = "List of directories relative to the BUILD file to append as -I flags to SWIG", | |
| ), | |
| "swig_options": attr.string_list( | |
| doc = "args to pass directly to the swig binary", | |
| ), | |
| "_swig": attr.label( | |
| default = "@swig", | |
| executable = True, | |
| allow_files = True, | |
| cfg = "exec", | |
| ), | |
| "_swig_lib": attr.label( | |
| default = "@swig//:lib_tcl", | |
| allow_files = True, | |
| ), | |
| "_swig_swg": attr.label( | |
| default = "@swig//:swig_swg", | |
| allow_single_file = True, | |
| doc = "SWIG swig.swg library file used for determining SWIG_LIB " + | |
| "env variable (internal attribute).", | |
| ), | |
| }, | |
| ) | |