# -*- python -*-

package(default_visibility = ["//visibility:private"])

load(
    "@drake//tools/install:install.bzl",
    "cmake_config",
    "install",
    "install_cmake_config",
)
load(
    "@drake//tools/skylark:drake_cc.bzl",
    "drake_transitive_installed_hdrs_filegroup",
)
load("//tools/lint:lint.bzl", "add_lint_tests")
load(":build_components.bzl", "LIBDRAKE_COMPONENTS")
load("//tools:drake.bzl", "drake_cc_binary")

# TODO(eric.cousineau): Try to make the CMake target `drake-marker` private,
# such that no downstream users can use it?
cmake_config(
    cps_file_name = "drake.cps",
    package = "drake",
)

install_cmake_config(
    package = "drake",
    versioned = 0,
)

# The Drake binary package. libdrake.so contains all the symbols from all the
# LIBDRAKE_COMPONENTS and all the Drake externals. We use linkstatic=1 so
# that the binary package will not contain any references to shared libraries
# inside the build tree.
drake_cc_binary(
    name = "libdrake.so",
    linkshared = 1,
    linkstatic = 1,
    deps = LIBDRAKE_COMPONENTS,
)

# Gather all of libdrake.so's dependent headers, excluding headers that are
# stored in the attic.  (Those will be used only after some path mangling.)
drake_transitive_installed_hdrs_filegroup(
    name = "libdrake_headers_no_attic",
    never_startswith = "attic/",
    deps = LIBDRAKE_COMPONENTS,
)

# Install libdrake.so along with all transitive headers in the same workspace
# (i.e. in Drake itself; not externals).
install(
    name = "install",
    install_tests = [
        "test/snopt_visibility_test.py",
    ],
    targets = ["libdrake.so"],
    hdrs = [":libdrake_headers_no_attic"],
    hdr_dest = "include/drake",
    allowed_externals = ["//:LICENSE.TXT"],  # Root for our #include paths.
    visibility = ["//:__pkg__"],
    deps = [
        ":install_attic_headers",
        ":install_cmake_config",
    ],
)

# Gather the headers that are stored in the attic.
drake_transitive_installed_hdrs_filegroup(
    name = "libdrake_headers_only_attic",
    only_startswith = "attic/",
    deps = LIBDRAKE_COMPONENTS,
)

# Install headers that are stored in the attic by stripping off the "attic/"
# component of the pathname.
install(
    name = "install_attic_headers",
    hdrs = [":libdrake_headers_only_attic"],
    hdr_dest = "include/drake",
    hdr_strip_prefix = ["attic"],
    allowed_externals = ["//:LICENSE.TXT"],
)

# Depend on Gurobi's shared library iff Gurobi is enabled.
cc_library(
    name = "gurobi_deps",
    deps = select({
        "//tools:with_gurobi": ["@gurobi//:gurobi_c"],
        "//conditions:default": [],
    }),
)

# Depend on Mosek's shared library iff Mosek is enabled.
cc_library(
    name = "mosek_deps",
    deps = select({
        "//tools:with_mosek": ["@mosek"],
        "//conditions:default": [],
    }),
)

# Depend on the subset of VTK's shared libraries that Drake uses.
cc_library(
    name = "vtk_deps",
    deps = [
        # TODO(jwnimmer-tri) This duplicates the list of VTK libraries needed
        # by //sensors.  We should find a way for ":drake_shared_library" to be
        # declared without having to repeat this list here.
        "@vtk//:vtkCommonCore",
        "@vtk//:vtkCommonDataModel",
        "@vtk//:vtkCommonTransforms",
        "@vtk//:vtkFiltersGeneral",
        "@vtk//:vtkFiltersSources",
        "@vtk//:vtkIOGeometry",
        "@vtk//:vtkIOImage",
        "@vtk//:vtkRenderingCore",
        "@vtk//:vtkRenderingOSPRay",
        "@vtk//:vtkRenderingOpenGL2",
    ],
)

# Provide a cc_library target that provides libdrake.so, its headers, its
# header-only dependencies, and its required *.so's that are WORKSPACE
# downloads (such as VTK, Gurobi, etc). This is aliased by
# `//:drake_shared_library`, which is what downstream users will consume if
# they wish to link to `libdrake.so`.
#
# TODO(jwnimmer-tri) Ideally, Bazel should be able to handle the depended-on
# *.so files for us, without us having to know up-front here which dependencies
# are coming from the WORKSPACE in the form of *.so.
#
# TODO(jwnimmer-tri) Ideally, drake_cc_library's installed_headers support
# would capture a list of headerfile dependencies, so that we don't need to
# write out the "list of libraries directly mentioned in Drake headers" below.
cc_library(
    name = "drake_shared_library",
    srcs = [":libdrake.so"],
    hdrs = [":libdrake_headers_no_attic"],
    # N.B. To pull in transitive `data` dependencies, we must list
    # `libdrake.so` as a data target.
    data = [":libdrake.so"],
    include_prefix = "drake",
    strip_include_prefix = "/",
    visibility = ["//:__pkg__"],
    deps = [
        ":libdrake_headers_only_attic_cc_library",
        # The list of depended-on *.so files.
        ":gurobi_deps",
        ":mosek_deps",
        ":vtk_deps",
        "//common:drake_marker_shared_library",
        "@dreal",
        "@ignition_math",
        "@ignition_rndf",
        "@lcm",
        "@libprotobuf",
        "@osqp",
        "@scs//:scsdir",
        "@tinyxml2",
    ] + [
        # The list of depended-on header-only libraries that libdrake's header
        # files depend on.
        "//lcmtypes:lcmtypes_cc",
        "@eigen",
        "@fmt",
        "@lcmtypes_bot2_core//:lcmtypes_bot2_core",
        "@lcmtypes_robotlocomotion//:lcmtypes_robotlocomotion",
        "@optitrack_driver//lcmtypes:optitrack_lcmtypes",
        "@spdlog",
        "@stx",
    ],
)

# Include headers that are stored in the attic by stripping off the "attic/"
# component of the pathname.
cc_library(
    name = "libdrake_headers_only_attic_cc_library",
    hdrs = [":libdrake_headers_only_attic"],
    include_prefix = "drake",
    strip_include_prefix = "/attic",
)

add_lint_tests(
    python_lint_extra_srcs = ["build_components_refresh.py"],
)
