diff --git a/tools/skylark/pybind.bzl b/tools/skylark/pybind.bzl index b4d7bfbfd259..bc2a6d6b65df 100644 --- a/tools/skylark/pybind.bzl +++ b/tools/skylark/pybind.bzl @@ -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") @@ -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. diff --git a/tools/workspace/lcm/package.BUILD.bazel b/tools/workspace/lcm/package.BUILD.bazel index 547d591ff582..163f7a9e276e 100644 --- a/tools/workspace/lcm/package.BUILD.bazel +++ b/tools/workspace/lcm/package.BUILD.bazel @@ -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 @@ -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", @@ -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"], ) @@ -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", diff --git a/tools/workspace/python/repository.bzl b/tools/workspace/python/repository.bzl index e17c8c1d3f42..cce1fdb88a3d 100644 --- a/tools/workspace/python/repository.bzl +++ b/tools/workspace/python/repository.bzl @@ -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: @@ -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, )