Skip to content

Commit

Permalink
Use correct suffix for Python C extensions (RobotLocomotion#14730)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiesnape authored Mar 3, 2021
1 parent c17ed4e commit 88b4649
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
3 changes: 2 additions & 1 deletion tools/skylark/pybind.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load("//tools/skylark:py.bzl", "py_library")
load("@cc//:compiler.bzl", "COMPILER_ID")
load("@python//:version.bzl", "PYTHON_EXTENSION_SUFFIX")

# @see bazelbuild/bazel#3493 for needing `@drake//` when loading `install`.
load("@drake//tools/install:install.bzl", "install")
Expand Down Expand Up @@ -54,7 +55,7 @@ def pybind_py_library(

# TODO(eric.cousineau): See if we can keep non-`*.so` target name, but
# output a *.so, so that the target name is similar to what is provided.
cc_so_target = cc_so_name + ".so"
cc_so_target = cc_so_name + PYTHON_EXTENSION_SUFFIX

# GCC and Clang don't always agree / succeed when inferring storage
# duration (#9600). Workaround it for now.
Expand Down
12 changes: 7 additions & 5 deletions tools/workspace/lcm/package.BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ load(
"install_files",
)
load("@drake//tools/lint:python_lint.bzl", "python_lint")
load("@python//:version.bzl", "PYTHON_EXTENSION_SUFFIX")

licenses([
"notice", # BSD-3-Clause
Expand Down Expand Up @@ -166,7 +167,7 @@ cc_binary(
)

cc_binary(
name = "_lcm.so",
name = "_lcm" + PYTHON_EXTENSION_SUFFIX,
srcs = [
"lcm-python/module.c",
"lcm-python/pyeventlog.c",
Expand Down Expand Up @@ -216,19 +217,20 @@ generate_file(
content = """
import ctypes
import os.path
ctypes.cdll.LoadLibrary(os.path.realpath(__path__[0] + '/_lcm.so'))
ctypes.cdll.LoadLibrary(os.path.realpath(
__path__[0] + '/_lcm{extension_suffix}'))
_filename = __path__[0] + \"/lcm-python/lcm/__init__.py\"
with open(_filename) as f:
_code = compile(f.read(), _filename, 'exec')
exec(_code)
""",
""".format(extension_suffix = PYTHON_EXTENSION_SUFFIX),
visibility = ["//visibility:private"],
)

py_library(
name = "lcm-python-upstream",
srcs = ["lcm-python/lcm/__init__.py"], # Actual code from upstream.
data = [":_lcm.so"],
data = [":_lcm" + PYTHON_EXTENSION_SUFFIX],
visibility = ["//visibility:private"],
)

Expand Down Expand Up @@ -293,7 +295,7 @@ install_files(
install(
name = "install_python",
targets = [
":_lcm.so",
":_lcm" + PYTHON_EXTENSION_SUFFIX,
":lcm-python-upstream",
],
library_dest = "@PYTHON_SITE_PACKAGES@/lcm",
Expand Down
7 changes: 7 additions & 0 deletions tools/workspace/python/repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ def _impl(repository_ctx):
# similar to that used in pkg_config.bzl and should be refactored and
# shared instead of being duplicated in both places.

extension_suffix = execute_or_fail(
repository_ctx,
[py_info.python_config, "--extension-suffix"],
).stdout.strip()

includes = []

for cflag in cflags:
Expand Down Expand Up @@ -199,10 +204,12 @@ def _impl(repository_ctx):
# `BUILD.bazel` or `package.BUILD.bazel` files.
PYTHON_BIN_PATH = "{bin_path}"
PYTHON_EXTENSION_SUFFIX = "{extension_suffix}"
PYTHON_VERSION = "{version}"
PYTHON_SITE_PACKAGES_RELPATH = "{site_packages_relpath}"
""".format(
bin_path = py_info.python,
extension_suffix = extension_suffix,
version = py_info.version,
site_packages_relpath = py_info.site_packages_relpath,
)
Expand Down

0 comments on commit 88b4649

Please sign in to comment.