# -*- bazel -*-

load("@drake//tools/install:install.bzl", "install")
load("@drake//tools/skylark:cc.bzl", "cc_library")
load(
    "@drake//tools/skylark:drake_cc.bzl",
    "cc_linkonly_library",
)
load(
    "@drake//tools/workspace:cmake_configure_file.bzl",
    "cmake_configure_file",
)

licenses(["notice"])  # BSD-3-Clause

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

# Generate config.h based on the defines= we want in Drake.
cmake_configure_file(
    name = "config",
    src = "src/ccd/config.h.cmake.in",
    out = "src/ccd/config.h",
    strict = True,
    defines = ["CCD_DOUBLE"],
    undefines = ["CCD_SINGLE"],
)

# Group the headers exported by this library.
cc_library(
    name = "hdrs",
    hdrs = [":config"] + glob(["src/ccd/*.h"], allow_empty = False),
    defines = ["CCD_STATIC_DEFINE"],
    includes = ["src"],
)

# Compile the code (using both its exported headers and its private headers).
cc_library(
    name = "compiled",
    srcs = glob(["src/*.c", "src/*.h"], allow_empty = False),
    copts = [
        "-Wno-all",
        "-fvisibility=hidden",
    ],
    includes = ["src"],
    linkstatic = True,
    deps = [":hdrs"],
)

# Strip the private headers out; we just want the object code.
cc_linkonly_library(
    name = "archive",
    deps = [":compiled"],
)

# Combine the public headers with the object code.
# This does not provide the private headers.
cc_library(
    name = "ccd",
    linkstatic = True,
    deps = [
        ":archive",
        ":hdrs",
    ],
    visibility = ["//visibility:public"],
)

install(
    name = "install",
    docs = ["BSD-LICENSE"],
    visibility = ["//visibility:public"],
)
