# -*- bazel -*-

# This file is only used in cases when we need to build and/or install fmt from
# source, i.e., when we are not using pkg-config.  See repository.bzl for the
# logic to select when that occurs.

load("@drake//tools/install:install.bzl", "install", "install_files")
load("@drake//tools/skylark:cc.bzl", "cc_library")

licenses(["notice"])  # MIT

package(
    default_visibility = ["//visibility:public"],
)

cc_library(
    name = "fmt",
    hdrs = glob([
        "include/fmt/*.h",
    ], exclude = [
        # The POSIX bindings are not header-only.
        "include/fmt/posix.h",
    ]),
    defines = [
        # By tradition, Drake uses the header-only build of fmt.  If we ever
        # change it to use a library, then we'll also need to update the
        # tools/workspace/spdlog/package.BUILD.bazel shared library rules
        # to avoid double-linking.
        "FMT_HEADER_ONLY=1",
        # Avoid macro pollution.
        "FMT_NO_FMT_STRING_ALIAS=1",
    ],
    includes = ["include"],
)

CMAKE_PACKAGE = "fmt"

install_files(
    name = "install_cmake_config",
    dest = "lib/cmake/{}".format(CMAKE_PACKAGE),
    files = [
        "@drake//tools/workspace/fmt:{}-config.cmake".format(CMAKE_PACKAGE),  # noqa
        "@drake//tools/workspace/fmt:{}-config-version.cmake".format(CMAKE_PACKAGE),  # noqa
    ],
    strip_prefix = ["**/"],
    allowed_externals = ["@drake//:.bazelproject"],
    visibility = ["//visibility:private"],
)

install(
    name = "install",
    targets = [":fmt"],
    hdr_dest = "include/" + CMAKE_PACKAGE,
    hdr_strip_prefix = ["include"],
    guess_hdrs = "PACKAGE",
    docs = ["LICENSE.rst"],
    deps = [":install_cmake_config"],
)
