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

copy_file(
    name = "highs-config",
    src = "src/HConfig.h.bazel.in",
    out = "HConfig.h",
    visibility = ["//visibility:public"],
)

cc_library(
    name = "config",
    srcs = ["HConfig.h"],
    visibility = ["//visibility:public"],
)

cc_library(
    name = "highs",
    srcs = glob([
        "extern/filereaderlp/*.cpp",
        "extern/zlib/*.cpp",
        "src/interfaces/highs_c_api.cpp",
        "src/io/*.cpp",
        "src/ipm/*.cpp",
        "src/ipm/ipx/*.cc",
        "src/ipm/basiclu/*.c",
        "src/lp_data/*.cpp",
        "src/mip/*.cpp",
        "src/model/*.cpp",
        "src/parallel/*.cpp",
        "src/pdlp/*.cpp",
        "src/pdlp/cupdlp/*.c",
        "src/presolve/*.cpp",
        "src/qpsolver/*.cpp",
        "src/simplex/*.cpp",
        "src/test/*.cpp",
        "src/util/*.cpp",
    ]),
    hdrs = glob([
        "HConfig.h",
        "**/*.h",
        "src/qpsolver/*.hpp",
        "src/Highs.h",
        "extern/filereaderlp/*.hpp",
        "extern/zstr/*.hpp",
    ]),
    copts = [
        "-Wno-unused-variable",
        "-Wno-unused-but-set-variable",
    ],
    includes = [
        "extern",
        # "extern/filereaderlp",
        # "extern/zstr",
        "src",
        # "src/ipm",
        # "src/ipm/ipx",
        # "src/ipm/basiclu",
        # "src/lp_data",
        # "src/mip",
        # "src/model",
        # "src/parallel",
        # "src/presolve",
        # "src/qpsolver",
        # "src/simplex",
        # "src/test",
        # "src/util",
        "bazel-bin",
    ],
    linkopts = ["-lpthread"],
    visibility = ["//visibility:public"],
    deps = [
        "//:config",
        "@zlib",
    ],
)

cc_binary(
    name = "call-highs-example",
    srcs = ["examples/call_highs_from_cpp.cpp"],
    visibility = ["//visibility:public"],
    deps = [
        "//:highs",
    ],
)

## Add tests
copy_file(
    name = "highs-check-config",
    src = "check/HCheckConfig.h.bazel.in",
    out = "HCheckConfig.h",
)

cc_library(
    name = "check-config",
    srcs = ["HCheckConfig.h"],
)

cc_library(
    name = "test_lib",
    testonly = True,
    srcs = [
        "HCheckConfig.h",
        "check/Avgas.cpp",
        "check/TestMain.cpp",
    ],
    hdrs = [
        "check/Avgas.h",
        "check/SpecialLps.h",
        "check/matrix_multiplication.hpp",
        "extern/catch.hpp",
    ],
    copts = ["-Iextern"],
    data = glob(["check/instances/*"]),
    includes = ["check"],
    deps = [
        ":highs",
        "//:check-config",
    ],
)

TEST_NAMES = [
    "TestAlienBasis",
    "TestBasis",
    "TestBasisSolves",
    "TestCheckSolution",
    "TestCrossover",
    "TestDualize",
    "TestEkk",
    "TestFactor",
    "TestFilereader",
    "TestFreezeBasis",
    "TestHighsGFkSolve",
    "TestHighsHash",
    "TestHighsHessian",
    "TestHighsIntegers",
    "TestHighsModel",
    "TestHighsParallel",
    "TestHighsRbTree",
    "TestHotStart",
    "TestHSet",
    "TestICrash",
    "TestInfo",
    "TestIO",
    "TestIpx",
    "TestLogging",
    "TestLpModification",
    "TestLpOrientation",
    "TestLpSolvers",
    "TestLpValidation",
    "TestMipSolver",
    "TestPresolve",
    "TestQpSolver",
    "TestRanging",
    "TestRays",
    "TestSemiVariables",
    "TestSetup",
    "TestSort",
    "TestSpecialLps",
    "TestThrow",
]

[cc_test(
    name = name,
    srcs = ["check/%s.cpp" % name],
    copts = [
        "-Iextern",
        "-Wno-unused-variable",
        "-Wno-unused-but-set-variable",
    ],
    deps = [
        ":highs",
        ":test_lib",
    ],
) for name in TEST_NAMES]
