# SPDX-License-Identifier: BSD-3-Clause # Copyright (c) 2025-2025, The OpenROAD Authors """Wraps the contents of tcl scripts as a cc files. The rules wrap the tcl scripts in a extern c character array. """ def _tcl_encode_sta_impl(ctx): """Generates a single C++ file from the provided srcs in a DefaultInfo. """ outfile_name = ctx.attr.out if ctx.attr.out else ctx.attr.name + ".cc" output_file = ctx.actions.declare_file(outfile_name) args = ctx.actions.args() args.add(ctx.file._encode_script) args.add(output_file) args.add(ctx.attr.char_array_name) args.add_all(ctx.files.srcs) # Get the Tcl library files (init.tcl...) from tcl_core tcl_libfiles = ctx.attr._tcl_library_files[DefaultInfo] # All tcl_core files live under library/, so split any file's path on # "/library/" to obtain the library directory for TCL_LIBRARY. tcl_lib_dir = ctx.files._tcl_library_files[0].path.split("/library/", 1)[0] + "/library" ctx.actions.run( outputs = [output_file], inputs = depset( direct = ctx.files.srcs + [ctx.file._encode_script], transitive = [tcl_libfiles.files], ), arguments = [args], tools = [ctx.executable._tclsh], executable = ctx.executable._tclsh, env = {"TCL_LIBRARY": tcl_lib_dir}, ) return [DefaultInfo(files = depset([output_file]))] tcl_encode_sta = rule( implementation = _tcl_encode_sta_impl, attrs = { "char_array_name": attr.string( doc = "The name of the extern string array value in the generated file.", mandatory = True, ), "out": attr.string( doc = "The name of the C++ source file generated by these rules.", ), "srcs": attr.label_list( allow_empty = False, allow_files = [".tcl"], doc = "Tcl files to be wrapped.", ), "_encode_script": attr.label( default = "//src/sta:etc/TclEncode.tcl", allow_single_file = True, ), "_tcl_library_files": attr.label( default = "@tcl_lang//:tcl_core", allow_files = True, ), "_tclsh": attr.label( default = "@tcl_lang//:tclsh", executable = True, cfg = "exec", ), }, )