Choosing which output file to use when including a rules_foreign_cc target as a dependency

36 Views Asked by At

I'm using a rules_foreign_cc rule in my BUILD.bazel file. The external library (qsopt_ex) gets built correctly, and all output files are produced and placed in bazel-bin/external/qsopt_ex/libqsopt_ex. When I try and use it as a dependency in other rules, though, all sorts of problems arise. There are some places where I would need to the static library (libqsopt_ex.a), and others that require the shared one (libqsopt_ex.so). How would I be able to make that distinction?

configure_make(
    name = "qsopt_ex",
    autoreconf = True,
    autoreconf_options = ["--install"],
    configure_in_place = True,
    lib_name = "libqsopt_ex",
    lib_source = ":all_srcs",
    out_binaries = ["esolver"],
    out_shared_libs = [
        "libqsopt_ex.so",
        "libqsopt_ex.so.2",
        "libqsopt_ex.so.2.1.0",
    ],
    out_static_libs = ["libqsopt_ex.a"],
    visibility = ["//visibility:public"],
    deps = ["@zlib"],
)

cc_binary(
    name = "test_static",
    srcs = ["test.cpp"],
    features = ["fully_static_link"],
    linkshared = 0,
    linkstatic = 1,
    deps = [":qsopt_ex"],
)

cc_binary(
    name = "test_shared",
    srcs = ["test.cpp"],
    linkshared = 1,
    linkstatic = 0,
    deps = [":qsopt_ex"],
)

My current workaround is to use a config settings to decide what files to copy:

configure_make(
    name = "qsopt_ex",
    lib_name = "libqsopt_ex",
    out_static_libs = select(
        {
            "@dlinear//tools:dynamic_build": [],
            "//conditions:default": ["libqsot_ex.a"],
        },
    ),
    out_shared_libs = select(
        {
            "@dlinear//tools:dynamic_build": ["libqsopt_ex.so", 
                                              "libqsopt_ex.so.2", 
                                              "libqsopt_ex.so.2.1.0"],
            "//conditions:default": [],
        },
    ),
    lib_source = ":all_srcs",
    visibility = ["//visibility:public"],
    configure_in_place = True,
    autoreconf = True,
    autoreconf_options = ["install"],
    deps = ["@zlib"],
)

This way the deps is forced to use the correct outputs. But each time I change the settings, the original library has to recompile. It's a very slow process and pointless process.

Another approach I tried is using this rules

cc_library(
    name = "qsopt_ex.a",
    srcs = [":qsopt_ex/libqsopt_ex.a"],
    hdrs = glob(["qsopt_ex/*.h"]),
    visibility = ["//visibility:public"],
)

cc_library(
    name = "qsopt_ex.so",
    srcs = [":qsopt_ex/libqsopt_ex.so", ":qsopt_ex/libqsopt_ex.so.2", ":qsopt_ex/libqsopt_ex.so.2.1.0"],
    hdrs = glob(["qsopt_ex/*.h"]),
    visibility = ["//visibility:public"],
)

but no srcs files are found.

0

There are 0 best solutions below