load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@bazel_skylib//rules:native_binary.bzl", "native_test")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")

copy_file(
    name = "config_h_generic",
    src = "src/config.h.generic",
    out = "src/config.h",
)

copy_file(
    name = "pcre2_h_generic",
    src = "src/pcre2.h.generic",
    out = "src/pcre2.h",
)

copy_file(
    name = "pcre2_chartables_c",
    src = "src/pcre2_chartables.c.dist",
    out = "src/pcre2_chartables.c",
)

LOCAL_DEFINES = [
    "HAVE_CONFIG_H",
    "SUPPORT_PCRE2_8",
    "SUPPORT_UNICODE",
] + select({
    "@platforms//os:windows": [],
    "//conditions:default": ["HAVE_UNISTD_H"],
})

# Workaround for a Bazel quirk. It is extremely strict about the #include path
# used for internal headers. We have our headers inside src/, but we #include
# them as '#include "pcre2_internal.h"', assuming that src/ is added to the
# compiler's include path. Unfortunately, that violates the conventions used by
# Bazel.
#
# This is a workaround. Note that we can't use the "include = [...]" property
# to add the src/ directory, since that pollutes the include path for projects
# depending on PCRE2 (we must not make our config.h file visible to consumers
# of PCRE2).
cc_library(
    name = "pcre2_internal_headers",
    hdrs = [
        "src/pcre2_compile.h",
        "src/pcre2_internal.h",
        "src/pcre2_intmodedep.h",
        "src/pcre2_jit_match_inc.h",
        "src/pcre2_jit_misc_inc.h",
        "src/pcre2_printint_inc.h",
        "src/pcre2_ucp.h",
        "src/pcre2_ucptables_inc.h",
        "src/pcre2_util.h",
        "src/pcre2test_inc.h",
        ":config_h_generic",
    ],
    strip_include_prefix = "src",
    visibility = ["//visibility:private"],
)

cc_library(
    name = "pcre2",
    srcs = [
        "src/pcre2_auto_possess.c",
        "src/pcre2_chkdint.c",
        "src/pcre2_compile.c",
        "src/pcre2_compile_cgroup.c",
        "src/pcre2_compile_class.c",
        "src/pcre2_config.c",
        "src/pcre2_context.c",
        "src/pcre2_convert.c",
        "src/pcre2_dfa_match.c",
        "src/pcre2_error.c",
        "src/pcre2_extuni.c",
        "src/pcre2_find_bracket.c",
        "src/pcre2_jit_compile.c",
        "src/pcre2_maketables.c",
        "src/pcre2_match.c",
        "src/pcre2_match_data.c",
        "src/pcre2_match_next.c",
        "src/pcre2_newline.c",
        "src/pcre2_ord2utf.c",
        "src/pcre2_pattern_info.c",
        "src/pcre2_script_run.c",
        "src/pcre2_serialize.c",
        "src/pcre2_string_utils.c",
        "src/pcre2_study.c",
        "src/pcre2_substitute.c",
        "src/pcre2_substring.c",
        "src/pcre2_tables.c",
        "src/pcre2_ucd.c",
        "src/pcre2_valid_utf.c",
        "src/pcre2_xclass.c",
        ":pcre2_chartables_c",
    ],
    hdrs = [":pcre2_h_generic"],
    implementation_deps = [":pcre2_internal_headers"],
    defines = [
        "PCRE2_STATIC",
    ],
    local_defines = LOCAL_DEFINES + [
        "PCRE2_CODE_UNIT_WIDTH=8",
    ],
    strip_include_prefix = "src",
    linkstatic = True,
    visibility = ["//visibility:public"],
)

# See below for explanation of why we need this.
#
# https://github.com/bazelbuild/bazel/issues/680
cc_library(
    name = "pcre2posix_dotc_headers",
    hdrs = [
        "src/pcre2_tables.c",
    ],
    strip_include_prefix = "src",
    visibility = ["//visibility:private"],
)

cc_library(
    name = "pcre2-posix",
    srcs = ["src/pcre2posix.c"],
    hdrs = ["src/pcre2posix.h"],
    implementation_deps = [
        ":pcre2_internal_headers",
        ":pcre2posix_dotc_headers",
    ],
    local_defines = LOCAL_DEFINES + [
        "PCRE2_CODE_UNIT_WIDTH=8",
    ],
    strip_include_prefix = "src",
    linkstatic = True,
    visibility = ["//visibility:public"],
    deps = [
        ":pcre2",
    ],
)

# Totally weird issue in Bazel. It won't let you #include any files unless they
# are declared to the build system. OK, fair enough. But - for a cc_binary it
# uses the file extension to determine whether it's a header or a compilation
# unit. But... we have several .c files which are #included, rather than treated
# as a compilation unit.
#
# For cc_library() above, we can overcome this with textual_hdrs. But that
# doesn't work for cc_binary(). Here's our workaround.
#
# https://github.com/bazelbuild/bazel/issues/680
cc_library(
    name = "pcre2test_dotc_headers",
    hdrs = [
        "src/pcre2_chkdint.c",
        "src/pcre2_tables.c",
        "src/pcre2_ucd.c",
        "src/pcre2_valid_utf.c",
    ],
    strip_include_prefix = "src",
    visibility = ["//visibility:private"],
)

cc_binary(
    name = "pcre2test",
    srcs = ["src/pcre2test.c"],
    linkopts = select({
        "@platforms//os:windows": ["-STACK:2500000"],
        "//conditions:default": [],
    }),
    local_defines = LOCAL_DEFINES,
    visibility = ["//visibility:public"],
    deps = [
        ":pcre2",
        ":pcre2-posix",
        ":pcre2_internal_headers",
        ":pcre2test_dotc_headers",
    ],
)

filegroup(
    name = "testdata",
    srcs = glob(["testdata/*"]),
)

native_test(
    name = "pcre2_test",
    size = "small",
    src = select({
        "@platforms//os:windows": "RunTest.bat",
        "//conditions:default": "RunTest",
    }),
    out = select({
        "@platforms//os:windows": "RunTest.bat",
        "//conditions:default": "RunTest",
    }),
    data = [
        ":pcre2test",
        ":testdata",
    ],
)
