-
Notifications
You must be signed in to change notification settings - Fork 790
python: Add pypi package infrastructure to bazel #8336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
maliberty
merged 15 commits into
The-OpenROAD-Project:master
from
QuantamHD:python_siwg
Sep 17, 2025
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
83c47d1
Add -python support for the bazel build of OR
maliberty c6e154d
filter files in tcl_encode
maliberty 54f43a0
fix PYTHONPATH for bazel startup
maliberty 0691c61
python: Add pypi package infrastructure to bazel
QuantamHD c1dfef3
format-code
QuantamHD 4c82ca0
fixed odb py test
QuantamHD d22581f
black formatting
QuantamHD 901a1fa
formatting
QuantamHD 8ca3162
ifdef guard module names
QuantamHD 4f9bf2a
fix utl_py name
QuantamHD 493a60d
Merge remote-tracking branch 'origin/master' into python_siwg
QuantamHD 0b9dcc1
fixing the helpers.py function
QuantamHD 0001456
black format
QuantamHD 15baeb1
fix bazel logging
QuantamHD 07a3459
Adding further configuration to the py_wheel generation
QuantamHD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
| # Copyright (c) 2025-2025, The OpenROAD Authors | ||
|
|
||
| """A Python 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=[":python_foo"]) | ||
| python_wrap_cc(name = "python_foo", srcs=["exception.i"],...) | ||
| """ | ||
| PythonSwigInfo = provider( | ||
| "PythonSwigInfo for taking dependencies on other swig info rules", | ||
| fields = [ | ||
| "transitive_srcs", | ||
| "includes", | ||
| "swig_options", | ||
| ], | ||
| ) | ||
|
|
||
| def _get_transitive_srcs(srcs, deps): | ||
| return depset( | ||
| srcs, | ||
| transitive = [dep[PythonSwigInfo].transitive_srcs for dep in deps], | ||
| ) | ||
|
|
||
| def _get_transitive_includes(local_includes, deps): | ||
| return depset( | ||
| local_includes, | ||
| transitive = [dep[PythonSwigInfo].includes for dep in deps], | ||
| ) | ||
|
|
||
| def _get_transitive_options(options, deps): | ||
| return depset( | ||
| options, | ||
| transitive = [dep[PythonSwigInfo].swig_options for dep in deps], | ||
| ) | ||
|
|
||
| def _python_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.") | ||
|
|
||
| root_file = ctx.file.root_swig_src or ctx.files.srcs[0] | ||
|
|
||
| cc_outfile_name = ctx.attr.out or (ctx.attr.name + ".cc") | ||
| cc_output_file = ctx.actions.declare_file(cc_outfile_name) | ||
| py_outfile_name = ctx.attr.module + ".py" | ||
| py_output_file = ctx.actions.declare_file(py_outfile_name) | ||
|
|
||
| include_root_directory = "" | ||
| if ctx.label.package: | ||
| include_root_directory = ctx.label.package + "/" | ||
|
|
||
| src_inputs = _get_transitive_srcs(ctx.files.srcs + ctx.files.root_swig_src, ctx.attr.deps) | ||
| includes_paths = _get_transitive_includes( | ||
| ["{}{}".format(include_root_directory, include) for include in ctx.attr.swig_includes], | ||
| ctx.attr.deps, | ||
| ) | ||
| swig_options = _get_transitive_options(ctx.attr.swig_options, ctx.attr.deps) | ||
|
|
||
| args = ctx.actions.args() | ||
| args.add("-DBAZEL=1") | ||
| args.add("-python") | ||
| args.add("-c++") | ||
| args.add("-flatstaticmethod") | ||
| args.add("-module") | ||
| args.add(ctx.attr.module) | ||
| args.add_all(swig_options.to_list()) | ||
| args.add_all(includes_paths.to_list(), format_each = "-I%s") | ||
| args.add("-o") | ||
| args.add(cc_output_file.path) | ||
| args.add(root_file.path) | ||
|
|
||
| ctx.actions.run( | ||
| outputs = [cc_output_file, py_output_file], | ||
| inputs = src_inputs, | ||
| arguments = [args], | ||
| tools = ctx.files._swig, | ||
| executable = ([file for file in ctx.files._swig if file.basename == "swig"][0]), | ||
| ) | ||
| return [ | ||
| DefaultInfo(files = depset([cc_output_file, py_output_file])), | ||
| PythonSwigInfo( | ||
| transitive_srcs = src_inputs, | ||
| includes = includes_paths, | ||
| swig_options = swig_options, | ||
| ), | ||
| ] | ||
|
|
||
| python_wrap_cc = rule( | ||
| implementation = _python_wrap_cc_impl, | ||
| attrs = { | ||
| "deps": attr.label_list( | ||
| allow_empty = True, | ||
| doc = "python_wrap_cc dependencies", | ||
| providers = [PythonSwigInfo], | ||
| ), | ||
| "module": attr.string( | ||
| mandatory = True, | ||
| doc = "swig module", | ||
| ), | ||
| "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.""", | ||
| ), | ||
| "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 = "@org_swig//:swig_stable", | ||
| allow_files = True, | ||
| cfg = "exec", | ||
| ), | ||
| }, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.