# -*- bazel -*-

load("@rules_python//python:defs.bzl", "py_library")
load("//:.manifest.bzl", "MANIFEST")

package(default_visibility = ["//:__subpackages__"])

_DRAKE_RUNFILES = MANIFEST["runfiles"]["drake"]

_DRAKE_SHLIBS = glob([
    ".lib/libdrake*.so",
    ".lib/libvtk*.so.*",
    # For Mosek (not enabled by default).
    ".lib/libtbb*.so*",
    ".lib/libmosek64*.so*",
    # For Gurobi (not enabled by default).
    ".lib/libgurobi*.so*",
], exclude = [
    ".lib/libvtk*Python*",
    ".lib/libvtk*-8.2.so.*",
])

_PYTHON_SITE_PACKAGES_RELPATH = MANIFEST["python_site_packages_relpath"]

_DRAKE_ROOT_PACKAGE_RUNFILES = [x for x in _DRAKE_RUNFILES if "/" not in x]

_EXPECTED_DRAKE_RUNFILES_PACKAGES = [
    "bindings/pydrake",
    "common",
    "examples",
    "geometry",
    "manipulation",
    "multibody",
]

_COVERED_DRAKE_RUNFILES = _DRAKE_ROOT_PACKAGE_RUNFILES + [
    x
    for x in _DRAKE_RUNFILES
    if any([
        x.startswith(package + "/")
        for package in _EXPECTED_DRAKE_RUNFILES_PACKAGES
    ])
]

(len(_COVERED_DRAKE_RUNFILES) == len(_DRAKE_RUNFILES)) or fail(
    "EXPECTED_DRAKE_RUNFILES_PACKAGES {} did not cover {}".format(
        _EXPECTED_DRAKE_RUNFILES_PACKAGES,
        _DRAKE_RUNFILES,
    ),
)

filegroup(
    name = ".installed_runfiles",
    data = _DRAKE_ROOT_PACKAGE_RUNFILES,
)

filegroup(
    name = ".all_runfiles",
    data = [
        "//:.installed_runfiles",
        "@drake_models",
    ] + [
        "//{}:.installed_runfiles".format(x)
        for x in _EXPECTED_DRAKE_RUNFILES_PACKAGES
    ],
)

cc_library(
    name = ".lcm_coretypes",
    hdrs = [".include/lcm/lcm/lcm_coretypes.h"],
    strip_include_prefix = ".include/lcm",
)

cc_library(
    name = ".drake_lcm_headers",
    hdrs = glob([".include/drake_lcmtypes/drake/**"]),
    strip_include_prefix = ".include/drake_lcmtypes",
    deps = [":.lcm_coretypes"],
)

cc_library(
    name = ".drake_headers",
    hdrs = glob([".include/drake/**"], exclude = ["**/drake_lcmtypes/**"]),
    strip_include_prefix = ".include",
    deps = [":.drake_lcm_headers"],
)

cc_library(
    name = ".fmt_headers",
    hdrs = glob([".include/fmt/**"], allow_empty = True),
    strip_include_prefix = ".include/fmt",
    visibility = ["@fmt//:__pkg__"],
)

[
    cc_import(
        name = ".imported{}".format(shlib),
        shared_library = shlib,
    )
    for shlib in _DRAKE_SHLIBS
]

cc_library(
    name = "drake_shared_library",
    data = [
        ":.all_runfiles",
    ],
    deps = [
        ":.drake_headers",
        "@eigen",
        "@fmt",
    ] + [
        ":.imported{}".format(shlib)
        for shlib in _DRAKE_SHLIBS
    ],
    visibility = ["//visibility:public"],
)

filegroup(
    name = ".all_shlib_data",
    data = glob([
        ".lib/*.so",
        ".lib/*.so.*",
    ]),
)

_IMPORT = "." + _PYTHON_SITE_PACKAGES_RELPATH

# N.B. This is not a standalone Python library.
# TODO(eric.cousineau): Expose this as an alias
# `@drake//lcmtypes:lcmtypes_drake_py` when it can only depend on specific
# parts of the runfiles (not all of pydrake).
py_library(
    name = ".lcmtypes_drake_py",
    srcs = glob(["*.py"]),
)

py_library(
    name = ".pydrake",
    srcs = glob(include = [
        _IMPORT + "/**/*.py",
    ]),
    data = glob(include = [
        _IMPORT + "/**/*.so",
    ]) + [
        ":.all_runfiles",
        ":.all_shlib_data",
    ],
    deps = [
        ":.lcmtypes_drake_py",
    ],
    imports = [
        _IMPORT,
    ],
)
