load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_flex//flex:flex.bzl", "flex_cc_library")

package(
    default_visibility = [
        "@//:android_cuttlefish",
        "@e2fsprogs//:__subpackages__",
    ],
)

cc_library(
    name = "libselinux",
    srcs = [
        "libselinux/src/android/android.c",
        "libselinux/src/android/android_internal.h",
        "libselinux/src/android/android_seapp.c",
        "libselinux/src/avc.c",
        "libselinux/src/avc_internal.c",
        "libselinux/src/avc_internal.h",
        "libselinux/src/avc_sidtab.c",
        "libselinux/src/avc_sidtab.h",
        "libselinux/src/booleans.c",
        "libselinux/src/callbacks.c",
        "libselinux/src/callbacks.h",
        "libselinux/src/canonicalize_context.c",
        "libselinux/src/checkAccess.c",
        "libselinux/src/check_context.c",
        "libselinux/src/compute_av.c",
        "libselinux/src/compute_create.c",
        "libselinux/src/compute_member.c",
        "libselinux/src/context.c",
        "libselinux/src/context_internal.h",
        "libselinux/src/deny_unknown.c",
        "libselinux/src/disable.c",
        "libselinux/src/enabled.c",
        "libselinux/src/fgetfilecon.c",
        "libselinux/src/file_path_suffixes.h",
        "libselinux/src/freecon.c",
        "libselinux/src/fsetfilecon.c",
        "libselinux/src/get_context_list_internal.h",
        "libselinux/src/get_default_type_internal.h",
        "libselinux/src/get_initial_context.c",
        "libselinux/src/getenforce.c",
        "libselinux/src/getfilecon.c",
        "libselinux/src/getpeercon.c",
        "libselinux/src/hashtab.c",
        "libselinux/src/hashtab.h",
        "libselinux/src/init.c",
        "libselinux/src/label.c",
        "libselinux/src/label_backends_android.c",
        "libselinux/src/label_file.c",
        "libselinux/src/label_file.h",
        "libselinux/src/label_internal.h",
        "libselinux/src/label_support.c",
        "libselinux/src/lgetfilecon.c",
        "libselinux/src/load_policy.c",
        "libselinux/src/lsetfilecon.c",
        "libselinux/src/mapping.c",
        "libselinux/src/mapping.h",
        "libselinux/src/matchpathcon.c",
        "libselinux/src/policy.h",
        "libselinux/src/policyvers.c",
        "libselinux/src/procattr.c",
        "libselinux/src/regex.c",
        "libselinux/src/regex.h",
        "libselinux/src/reject_unknown.c",
        "libselinux/src/selinux_internal.c",
        "libselinux/src/selinux_internal.h",
        "libselinux/src/selinux_netlink.h",
        "libselinux/src/sestatus.c",
        "libselinux/src/setenforce.c",
        "libselinux/src/setfilecon.c",
        "libselinux/src/setrans_client.c",
        "libselinux/src/setrans_internal.h",
        "libselinux/src/sha1.c",
        "libselinux/src/sha1.h",
        "libselinux/src/stringrep.c",
    ],
    hdrs = glob([
        "libselinux/include/**/*.h",
    ]),
    copts = [
        "-Wno-unused-but-set-variable",
    ],
    includes = [
        "libselinux/include",
        "libselinux/src",
    ],
    local_defines = [
        "__ANDROID_VNDK__",
        "_GNU_SOURCE",
        "AUDITD_LOG_TAG=1003",
        "BUILD_HOST",
        "DISABLE_BOOL",
        "DISABLE_SETRANS",
        "LOG_PRI=ALOG",
        "NO_DB_BACKEND",
        "NO_MEDIA_BACKEND",
        "NO_PERSISTENTLY_STORED_PATTERNS",
        "NO_X_BACKEND",
        "PCRE2_CODE_UNIT_WIDTH=8",
        "USE_PCRE2",
    ],
    strip_include_prefix = "libselinux/include",
    deps = [
        ":libsepol_headers",
        "@//android:log_header",
        "@android_system_core//:libcutils",
        "@pcre2",
    ],
)

cc_library(
    name = "libsepol",
    srcs = [
        "libsepol/cil/src/android.c",
        "libsepol/cil/src/cil.c",
        "libsepol/cil/src/cil_binary.c",
        "libsepol/cil/src/cil_binary.h",
        "libsepol/cil/src/cil_build_ast.c",
        "libsepol/cil/src/cil_build_ast.h",
        "libsepol/cil/src/cil_copy_ast.c",
        "libsepol/cil/src/cil_copy_ast.h",
        "libsepol/cil/src/cil_deny.c",
        "libsepol/cil/src/cil_deny.h",
        "libsepol/cil/src/cil_find.c",
        "libsepol/cil/src/cil_find.h",
        "libsepol/cil/src/cil_fqn.c",
        "libsepol/cil/src/cil_fqn.h",
        "libsepol/cil/src/cil_list.c",
        "libsepol/cil/src/cil_log.c",
        "libsepol/cil/src/cil_mem.c",
        "libsepol/cil/src/cil_parser.c",
        "libsepol/cil/src/cil_parser.h",
        "libsepol/cil/src/cil_policy.c",
        "libsepol/cil/src/cil_policy.h",
        "libsepol/cil/src/cil_post.c",
        "libsepol/cil/src/cil_post.h",
        "libsepol/cil/src/cil_reset_ast.c",
        "libsepol/cil/src/cil_reset_ast.h",
        "libsepol/cil/src/cil_resolve_ast.c",
        "libsepol/cil/src/cil_resolve_ast.h",
        "libsepol/cil/src/cil_stack.c",
        "libsepol/cil/src/cil_stack.h",
        "libsepol/cil/src/cil_strpool.c",
        "libsepol/cil/src/cil_strpool.h",
        "libsepol/cil/src/cil_symtab.c",
        "libsepol/cil/src/cil_tree.c",
        "libsepol/cil/src/cil_verify.c",
        "libsepol/cil/src/cil_verify.h",
        "libsepol/cil/src/cil_write_ast.c",
        "libsepol/cil/src/cil_write_ast.h",
        "libsepol/src/assertion.c",
        "libsepol/src/avrule_block.c",
        "libsepol/src/avtab.c",
        "libsepol/src/boolean_internal.h",
        "libsepol/src/boolean_record.c",
        "libsepol/src/booleans.c",
        "libsepol/src/conditional.c",
        "libsepol/src/constraint.c",
        "libsepol/src/context.c",
        "libsepol/src/context.h",
        "libsepol/src/context_internal.h",
        "libsepol/src/context_record.c",
        "libsepol/src/debug.c",
        "libsepol/src/debug.h",
        "libsepol/src/ebitmap.c",
        "libsepol/src/expand.c",
        "libsepol/src/flask.h",
        "libsepol/src/handle.c",
        "libsepol/src/handle.h",
        "libsepol/src/hashtab.c",
        "libsepol/src/hierarchy.c",
        "libsepol/src/ibendport_internal.h",
        "libsepol/src/ibpkey_internal.h",
        "libsepol/src/iface_internal.h",
        "libsepol/src/iface_record.c",
        "libsepol/src/interfaces.c",
        "libsepol/src/kernel_to_cil.c",
        "libsepol/src/kernel_to_common.c",
        "libsepol/src/kernel_to_common.h",
        "libsepol/src/kernel_to_conf.c",
        "libsepol/src/link.c",
        "libsepol/src/mls.c",
        "libsepol/src/mls.h",
        "libsepol/src/module.c",
        "libsepol/src/module_internal.h",
        "libsepol/src/module_to_cil.c",
        "libsepol/src/node_internal.h",
        "libsepol/src/node_record.c",
        "libsepol/src/nodes.c",
        "libsepol/src/optimize.c",
        "libsepol/src/polcaps.c",
        "libsepol/src/policydb.c",
        "libsepol/src/policydb_convert.c",
        "libsepol/src/policydb_internal.h",
        "libsepol/src/policydb_public.c",
        "libsepol/src/policydb_validate.c",
        "libsepol/src/policydb_validate.h",
        "libsepol/src/port_internal.h",
        "libsepol/src/port_record.c",
        "libsepol/src/ports.c",
        "libsepol/src/private.h",
        "libsepol/src/services.c",
        "libsepol/src/sidtab.c",
        "libsepol/src/symtab.c",
        "libsepol/src/user_internal.h",
        "libsepol/src/user_record.c",
        "libsepol/src/users.c",
        "libsepol/src/util.c",
        "libsepol/src/write.c",
    ],
    includes = [
        "libsepol/cil/src",
        "libsepol/src",
    ],
    local_defines = [
        "_GNU_SOURCE",
        "HAVE_REALLOCARRAY",
    ],
    deps = [
        ":libsepol_cil_headers",
        ":libsepol_cil_internal_headers",
        ":libsepol_cil_lexer",
        ":libsepol_headers",
    ],
)

cc_library(
    name = "libsepol_cil_headers",
    hdrs = glob([
        "libsepol/cil/include/**/*.h",
    ]),
    includes = [
        "libsepol/cil/include",
    ],
    strip_include_prefix = "libsepol/cil/include",
)

cc_library(
    name = "libsepol_cil_internal_headers",
    hdrs = [
        "libsepol/cil/src/cil_flavor.h",
        "libsepol/cil/src/cil_internal.h",
        "libsepol/cil/src/cil_lexer.h",
        "libsepol/cil/src/cil_list.h",
        "libsepol/cil/src/cil_log.h",
        "libsepol/cil/src/cil_mem.h",
        "libsepol/cil/src/cil_symtab.h",
        "libsepol/cil/src/cil_tree.h",
    ],
    includes = [
        "libsepol/cil/src",
    ],
    strip_include_prefix = "libsepol/cil/src",
    deps = [
        ":libsepol_cil_headers",
        ":libsepol_headers",
    ],
)

flex_cc_library(
    name = "libsepol_cil_lexer",
    src = "libsepol/cil/src/cil_lexer.l",
    deps = [":libsepol_cil_internal_headers"],
)

cc_library(
    name = "libsepol_headers",
    hdrs = glob([
        "libsepol/include/**/*.h",
    ]),
    includes = [
        "libsepol/include",
    ],
    strip_include_prefix = "libsepol/include",
)

cc_binary(
    name = "sefcontext_compile",
    srcs = [
        "libselinux/src/label_file.h",
        "libselinux/utils/sefcontext_compile.c",
    ],
    local_defines = [
        "PCRE2_CODE_UNIT_WIDTH=8",
        "USE_PCRE2",
    ],
    deps = [
        ":libselinux",
        ":libsepol",
        "@pcre2",
    ],
)
