cpr icon indicating copy to clipboard operation
cpr copied to clipboard

Add project support Bazel build system.

Open MiyamuraMiyako opened this issue 11 months ago • 3 comments

Is your feature request related to a problem?

I try build static library directly, but lib files can't use in my bazel project. log cpr.lib(threadpool.obj) : error LNK2019: �޷��������ⲿ���� _Cnd_timedwait_for������ "public: __cdecl <lambda_eba37c162f1e7731b0928a0b0fca0ed0>::operator()(void)const " (??R<lambda_eba37c162f1e7731b0928a0b0fca0ed0>@@QEBA@XZ) �������˸÷��� cpr.lib(async.obj) : error LNK2019: �޷��������ⲿ���� __std_init_once_link_alternate_names_and_abort������ "public: __cdecl std::_Init_once_completer::~_Init_once_completer(void)" (??1_Init_once_completer@std@@QEAA@XZ) �������˸÷��� I can't find the reason yet. this is my BUILD.bazel file's some content:

cc_import(
    name = "zlibstatic",
    hdrs = [],
    static_library = "libs/win-x64/zlibstatic.lib",
    visibility = ["//visibility:public"],
)

cc_import(
    name = "curl",
    hdrs = [],
    deps=[":zlibstatic"],
    static_library = "libs/win-x64/libcurl.lib",
    visibility = ["//visibility:public"],
)

cc_import(
    name = "cpr",
    hdrs = glob([
        "src/cpr/*.h",
    ]),
    static_library = "libs/win-x64/cpr.lib",
    deps=[":zlibstatic",":curl"],
    visibility = ["//visibility:public"],
)

cc_binary(
    name = "..........",
    srcs = glob([
        "**/*.cpp",
        "**/*.hpp",
        "**/*.h",
    ]),
    copts = [
        "/utf-8",
        "-std:c++17",
        "-Bv",
        "-W3",
        "-MT",
        "-D_UNICODE",
        "-DUNICODE",
    ],
    includes = ["src"],
    linkopts = [
        "-ENTRY:wWinMainCRTStartup",
        "-SUBSYSTEM:WINDOWS",
        "-NODEFAULTLIB:libcmt.lib",
    ],
    linkstatic = 1,
    deps = [
        ..........
        ":zlibstatic",
        ":curl",
        ":cpr",
        .........
    ],
)

Possible Solution

If cpr support bazel directly, maybe it will be easier to use. But I can't successfully convert the cmake project to a bazel project yet.

Alternatives

No response

Additional Context

No response

MiyamuraMiyako avatar Jan 25 '25 12:01 MiyamuraMiyako

Sadly I never directly worked with bazel yet. Bat some time ago, someone else put in the work to write some bazel rules here: https://github.com/hedronvision/bazel-make-cc-https-easy

I would love to merge a PR adding bazel support if someone is volunteering to maintain it.

COM8 avatar Jan 26 '25 12:01 COM8

Okay, I'm also working on how to do this.

MiyamuraMiyako avatar Jan 28 '25 16:01 MiyamuraMiyako

Progress notes:

Since #1182 , cpr can be full statically linked on Windows. Bazel projects can use rules_foreign_cc to build cpr:

third-party/cpr/repositories.MODULE.bazel:

new_git_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")

all_content = """
filegroup(name = "all", srcs = glob(["**"]), visibility = ["//visibility:public"])
"""

new_git_repository(
    name = "cpr",
    build_file_content = all_content,
    remote = "https://github.com/libcpr/cpr",
    commit = "c121ee0cece3878c5eaa69cfa571a23f75c94336",
)

third-party/cpr/BUILD.bazel:

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

exports_files(["repositories.MODULE.bazel"])

package(default_visibility = ["//visibility:public"])

cmake(
    name = "cpr",
    cache_entries = select({
        "//conditions:default": {
            "CPR_USE_SYSTEM_CURL": "OFF",
            "BUILD_SHARED_LIBS": "OFF",
        },
    }),
    lib_source = "@cpr//:all",
    out_include_dir = "include",
    out_lib_dir = "lib",
    out_static_libs = [
        "zlibstatic.lib",
        "libcurl.lib",
        "cpr.lib",
    ],
    visibility = ["//visibility:public"],
)

Next I will look into how to make cpr itself support Bazel builds.

MiyamuraMiyako avatar Feb 18 '25 11:02 MiyamuraMiyako