# -*- python -*-

load("@drake//tools/install:install.bzl", "install")
load("//tools/lint:lint.bzl", "add_lint_tests")
load("//tools/skylark:test_tags.bzl", "gurobi_test_tags", "mosek_test_tags")
load(
    "//tools/skylark:pybind.bzl",
    "drake_pybind_library",
    "get_drake_py_installs",
    "get_pybind_package_info",
)
load(
    "//tools/skylark:drake_py.bzl",
    "drake_py_library",
    "drake_py_test",
    "drake_py_unittest",
)

package(default_visibility = [
    "//bindings/pydrake:__subpackages__",
])

# This determines how `PYTHONPATH` is configured, and how to install the
# bindings.
PACKAGE_INFO = get_pybind_package_info("//bindings")

drake_py_library(
    name = "module_py",
    srcs = ["__init__.py"],
    imports = PACKAGE_INFO.py_imports,
    deps = [
        "//bindings/pydrake:module_py",
    ],
)

# TODO(eric.cousineau): Move this into `multibody`.
drake_pybind_library(
    name = "ik_py",
    cc_deps = ["//bindings/pydrake:documentation_pybind"],
    cc_srcs = ["ik_py.cc"],
    package_info = PACKAGE_INFO,
    py_deps = [
        "//bindings/pydrake/multibody:rigid_body_tree_py",
    ],
    py_srcs = ["ik.py"],
)

# TODO(eric.cousineau): Rename `mathematicalprogram` to
# `mathematical_program` when `pydrake` is renamed to `drake`.
drake_pybind_library(
    name = "mathematicalprogram_py",
    cc_deps = [
        "//bindings/pydrake:autodiff_types_pybind",
        "//bindings/pydrake:documentation_pybind",
        "//bindings/pydrake:symbolic_types_pybind",
        "//bindings/pydrake/util:deprecation_pybind",
        "//bindings/pydrake/util:drake_optional_pybind",
    ],
    cc_srcs = ["mathematicalprogram_py.cc"],
    package_info = PACKAGE_INFO,
    py_deps = [
        "//bindings/pydrake:autodiffutils_py",
        "//bindings/pydrake:symbolic_py",
    ],
    py_srcs = ["mathematicalprogram.py"],
)

drake_pybind_library(
    name = "gurobi_py",
    cc_deps = ["//bindings/pydrake:documentation_pybind"],
    cc_so_name = "gurobi",
    cc_srcs = ["gurobi_py.cc"],
    package_info = PACKAGE_INFO,
    py_deps = [":mathematicalprogram_py"],
)

drake_pybind_library(
    name = "ipopt_py",
    cc_deps = ["//bindings/pydrake:documentation_pybind"],
    cc_so_name = "ipopt",
    cc_srcs = ["ipopt_py.cc"],
    package_info = PACKAGE_INFO,
    py_deps = [":mathematicalprogram_py"],
)

drake_pybind_library(
    name = "mosek_py",
    cc_deps = ["//bindings/pydrake:documentation_pybind"],
    cc_so_name = "mosek",
    cc_srcs = ["mosek_py.cc"],
    package_info = PACKAGE_INFO,
    py_deps = [":mathematicalprogram_py"],
)

drake_pybind_library(
    name = "osqp_py",
    cc_deps = ["//bindings/pydrake:documentation_pybind"],
    cc_so_name = "osqp",
    cc_srcs = ["osqp_py.cc"],
    package_info = PACKAGE_INFO,
    py_deps = [":mathematicalprogram_py"],
)

PY_LIBRARIES_WITH_INSTALL = [
    ":gurobi_py",
    ":ik_py",
    ":ipopt_py",
    ":mathematicalprogram_py",
    ":mosek_py",
    ":osqp_py",
]

PY_LIBRARIES = [
    ":module_py",
]

# Symbol roll-up (for user ease).
drake_py_library(
    name = "all_py",
    srcs = ["all.py"],
    deps = PY_LIBRARIES_WITH_INSTALL + PY_LIBRARIES,
)

# Package roll-up (for Bazel dependencies).
drake_py_library(
    name = "solvers",
    deps = [":all_py"],
)

install(
    name = "install",
    targets = PY_LIBRARIES + [":all_py"],
    py_dest = PACKAGE_INFO.py_dest,
    deps = get_drake_py_installs(PY_LIBRARIES_WITH_INSTALL),
)

drake_py_unittest(
    name = "gurobi_solver_test",
    tags = gurobi_test_tags(),
    deps = [":gurobi_py"],
)

drake_py_unittest(
    name = "ipopt_solver_test",
    args = select({
        "//tools:no_ipopt": ["TestIpoptSolver.unavailable"],
        "//conditions:default": [],
    }),
    deps = [":ipopt_py"],
)

drake_py_unittest(
    name = "mosek_solver_test",
    tags = mosek_test_tags(),
    deps = [":mosek_py"],
)

drake_py_unittest(
    name = "osqp_solver_test",
    args = select({
        "//tools:no_osqp": ["TestOsqpSolver.unavailable"],
        "//conditions:default": [],
    }),
    deps = [":osqp_py"],
)

drake_py_unittest(
    name = "mathematicalprogram_test",
    tags = gurobi_test_tags(),
    deps = [":mathematicalprogram_py"],
)

drake_py_test(
    name = "pr2_ik_test",
    # TODO(jwnimmer-tri) Port this to drake_py_unittest.
    allow_import_unittest = True,
    data = ["//examples/pr2:models"],
    tags = ["snopt"],
    deps = [":ik_py"],
)

drake_py_unittest(
    name = "rbt_ik_test",
    data = ["//examples/pendulum:models"],
    tags = ["snopt"],
    deps = [":ik_py"],
)

add_lint_tests()
