diff --git a/lcmtypes/BUILD.bazel b/lcmtypes/BUILD.bazel index 2090f64cb2f4..46b67835ff0b 100644 --- a/lcmtypes/BUILD.bazel +++ b/lcmtypes/BUILD.bazel @@ -2,7 +2,11 @@ package(default_visibility = ["//visibility:public"]) -load("@drake//tools/install:install.bzl", "install") +load( + "@drake//tools/install:install.bzl", + "install", + "install_py2_duplicates_if_py3", +) load( "@drake//tools/skylark:drake_cc.bzl", "drake_cc_library", @@ -390,6 +394,13 @@ install( visibility = ["//visibility:private"], ) +# For Python2-only `drake_visualizer`. +install_py2_duplicates_if_py3( + name = "install_py2_duplicates_if_py3", + targets = [":lcmtypes_py"], + visibility = ["//visibility:private"], +) + install( name = "install", install_tests = [":test/drake-lcm-spy_install_test.py"], @@ -405,6 +416,7 @@ install( }, deps = [ ":install_cc_headers", + ":install_py2_duplicates_if_py3", "//tools/workspace/optitrack_driver:install", "@lcm//:install", "@lcmtypes_bot2_core//:install", diff --git a/tools/install/install.bzl b/tools/install/install.bzl index 3f421ad80900..ba65beef1d4c 100644 --- a/tools/install/install.bzl +++ b/tools/install/install.bzl @@ -8,7 +8,7 @@ load( "join_paths", "output_path", ) -load("@python//:version.bzl", "PYTHON_SITE_PACKAGES_RELPATH") +load("@python//:version.bzl", "PYTHON_SITE_PACKAGES_RELPATH", "PYTHON_VERSION") InstallInfo = provider() @@ -689,6 +689,40 @@ Args: allowed_externals: List of external packages whose files may be installed. """ +def install_py2_duplicates_if_py3( + name, + targets = None, + py_dest = "@PYTHON_SITE_PACKAGES@", + **kwargs): + """ + Creates a duplicate install, only if Python3 is Bazel's version of Python. + Otherwise, creates an empty install target. + + For `py_dest`, `@PYTHON_SITE_PACKAGES@` will be replaced with + `lib/python2.7/site-packages`. + + This is presently only used to support Python2-only `drake_visualizer`. + """ + cur_major, _ = PYTHON_VERSION.split(".") + if cur_major == "3": + py2_targets = targets + else: + py2_targets = [] + + # Assuming that we will only have one supported major-minor version of + # Python2. + py2_major_minor = "2.7" + py2_dest = py_dest.replace( + "@PYTHON_SITE_PACKAGES@", + "lib/python{}/site-packages".format(py2_major_minor), + ) + install( + name = name, + targets = py2_targets, + py_dest = py2_dest, + **kwargs + ) + #------------------------------------------------------------------------------ # Generate information to install files to specified destination. def _install_files_impl(ctx): diff --git a/tools/workspace/lcmtypes_bot2_core/package.BUILD.bazel b/tools/workspace/lcmtypes_bot2_core/package.BUILD.bazel index 9ace25c30623..267201e85a60 100644 --- a/tools/workspace/lcmtypes_bot2_core/package.BUILD.bazel +++ b/tools/workspace/lcmtypes_bot2_core/package.BUILD.bazel @@ -5,6 +5,7 @@ load( "cmake_config", "install", "install_cmake_config", + "install_py2_duplicates_if_py3", ) load( "@drake//tools/workspace/lcm:lcm.bzl", @@ -64,6 +65,14 @@ install_cmake_config( versioned = 0, ) +# For Python2-only `drake_visualizer`. +install_py2_duplicates_if_py3( + name = "install_py2_duplicates_if_py3", + targets = [":lcmtypes_bot2_core_py"], + py_strip_prefix = ["lcmtypes"], + visibility = ["//visibility:private"], +) + # For docs, see https://github.com/RobotLocomotion/lcmtypes/issues/2 # and https://github.com/openhumanoids/bot_core_lcmtypes/issues/33. install( @@ -81,5 +90,8 @@ install( "share/java/liblcmtypes_bot2_core_java.jar": "lcmtypes_bot2_core.jar", }, allowed_externals = ["@lcmtypes_robotlocomotion//:LICENSE.txt"], - deps = [":install_cmake_config"], + deps = [ + ":install_cmake_config", + ":install_py2_duplicates_if_py3", + ], ) diff --git a/tools/workspace/lcmtypes_robotlocomotion/package.BUILD.bazel b/tools/workspace/lcmtypes_robotlocomotion/package.BUILD.bazel index 209d375befe0..c410828ad79e 100644 --- a/tools/workspace/lcmtypes_robotlocomotion/package.BUILD.bazel +++ b/tools/workspace/lcmtypes_robotlocomotion/package.BUILD.bazel @@ -9,6 +9,7 @@ load( "cmake_config", "install", "install_cmake_config", + "install_py2_duplicates_if_py3", ) load( "@drake//tools/workspace/lcm:lcm.bzl", @@ -54,6 +55,14 @@ install_cmake_config( versioned = 0, ) +# For Python2-only `drake_visualizer`. +install_py2_duplicates_if_py3( + name = "install_py2_duplicates_if_py3", + targets = [":lcmtypes_robotlocomotion_py"], + py_strip_prefix = ["lcmtypes"], + visibility = ["//visibility:private"], +) + install( name = "install", workspace = CMAKE_PACKAGE, @@ -68,7 +77,10 @@ install( rename = { "share/java/liblcmtypes_robotlocomotion_java.jar": "lcmtypes_robotlocomotion.jar", # noqa }, - deps = [":install_cmake_config"], + deps = [ + ":install_cmake_config", + ":install_py2_duplicates_if_py3", + ], ) # See https://github.com/RobotLocomotion/lcmtypes/issues/2 and diff --git a/tools/workspace/optitrack_driver/BUILD.bazel b/tools/workspace/optitrack_driver/BUILD.bazel index abcc92119f66..afba2cd00e28 100644 --- a/tools/workspace/optitrack_driver/BUILD.bazel +++ b/tools/workspace/optitrack_driver/BUILD.bazel @@ -6,6 +6,7 @@ load( "install", "install_cmake_config", "install_files", + "install_py2_duplicates_if_py3", ) load("//tools/lint:lint.bzl", "add_lint_tests") @@ -38,6 +39,16 @@ OPTITRACK_TARGETS = [ "@optitrack_driver//src:optitrack_client_py", ] +# For Python2-only `drake_visualizer`. +install_py2_duplicates_if_py3( + name = "install_py2_duplicates_if_py3", + allowed_externals = OPTITRACK_TARGETS, + py_dest = "@PYTHON_SITE_PACKAGES@/optitrack", + py_strip_prefix = ["**/"], + targets = ["@optitrack_driver//lcmtypes:py_optitrack_lcmtypes"], + visibility = ["//visibility:private"], +) + install( name = "install", workspace = CMAKE_PACKAGE, @@ -57,6 +68,7 @@ install( deps = [ ":install_cmake_config", ":install_optitrack_client", + ":install_py2_duplicates_if_py3", ], )