load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
load("@rules_license//rules:license.bzl", "license")

package(default_applicable_licenses = [":license"])

license(
    name = "license",
    license_kinds = ["@rules_license//licenses/spdx:MIT"],
    license_text = "LICENSE.txt",
)

exports_files(["LICENSE.txt"])

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

cmake(
    name = "z3_dynamic",
    generate_args = [
        "-G Ninja", 
        "-D Z3_EXPORTED_TARGETS=",  # prevents installation, leaving symlinks between dylibs intact on copy
    ],
    lib_source = ":all_files",
    out_binaries = ["z3"],
    out_shared_libs = select({
        "@platforms//os:linux": ["libz3.so"],
        # "@platforms//os:osx": ["libz3.dylib"],    # FIXME: this is not working, libz3<version>.dylib is not copied
        "@platforms//os:windows": ["libz3.dll"],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    visibility = ["//visibility:public"],
)

cmake(
    name = "z3_static",
    generate_args = [
        "-G Ninja", 
        "-D BUILD_SHARED_LIBS=OFF",
        "-D Z3_BUILD_LIBZ3_SHARED=OFF",
    ],
    lib_source = ":all_files",
    out_binaries = ["z3"],
    out_static_libs = select({
        "@platforms//os:linux": ["libz3.a"],
        "@platforms//os:osx": ["libz3.a"],
        "@platforms//os:windows": ["libz3.lib"],     # MSVC with Control Flow Guard enabled by default
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    visibility = ["//visibility:public"],
)

alias(
    name = "z3",
    actual = ":z3_dynamic",
    visibility = ["//visibility:public"],
)
