6

我正在尝试从 Bazel 构建中使用libhttpserver以及扩展的libmicrohttpd 。这些库的构建过程似乎是这样的:

./bootstrap
mkdir build
cd build
../configure
make

configure && make在更经典的工作流程上,这是我以前从未见过的轻微变化。

  • 有没有人设法让这样的图书馆在 Bazel 下工作?
  • 有人有我可以借鉴的公开例子吗?

我发现最接近支持这一点的是@rules_foreign_cc//tools/build_defs:configure.bzl#configure_make,但这似乎没有引导步骤的概念。即使破解它似乎也不起作用,因为bootstrap脚本最终失败了:

mkdir: cannot create directory 'tmpwrk23': Read-only file system
autopoint: *** cannot create directory tmpwrk23
autopoint: *** Stop.
autoreconf: autopoint failed with exit status: 1

我即将准备好伸手去拿一个genrule(),但这似乎很容易出错......

4

1 回答 1

6

在遇到2020 年 6 月 23 日更新后,我与您走上了相同的道路,并且能够使用rules_foreign_cc项目使用 bazel 编译 libhttpserver 。我在下面添加了代码,但一般来说,rules_foreign_cc 有一个 make 规则,您可以设置覆盖 make_commands 并调用 ./bootstrap。

工作空间:

...
http_archive(
   name = "rules_foreign_cc",
   strip_prefix = "rules_foreign_cc-master",
   url = "https://github.com/bazelbuild/rules_foreign_cc/archive/master.zip",
)
load("@rules_foreign_cc//:workspace_definitions.bzl", "rules_foreign_cc_dependencies")
rules_foreign_cc_dependencies(register_default_tools = True)

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

http_archive(
  name = "rules_cc",
  urls = ["https://github.com/bazelbuild/rules_cc/archive/262ebec3c2296296526740db4aefce68c80de7fa.zip"],
  strip_prefix = "rules_cc-262ebec3c2296296526740db4aefce68c80de7fa",
)

http_archive(
    name = "libgnutls",
    build_file_content = all_content,
    strip_prefix = "gnutls-3.6.15",
    urls = ["https://www.gnupg.org/ftp/gcrypt/gnutls/v3.6/gnutls-3.6.15.tar.xz"],
)    

http_archive(
    name = "libhttpserver",
    build_file_content = all_content,
    strip_prefix = "libhttpserver-master",
    urls = ["https://github.com/etr/libhttpserver/archive/master.zip"],
)

http_archive(
    name = "libmicrohttpd",
    build_file_content = all_content,
    strip_prefix = "libmicrohttpd-0.9.71",
    urls = ["https://ftp.gnu.org/gnu/libmicrohttpd/libmicrohttpd-0.9.71.tar.gz"],
)

建造:

load("@rules_foreign_cc//tools/build_defs:configure.bzl", "configure_make")
load("@rules_foreign_cc//tools/build_defs:make.bzl", "make")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")

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

configure_make(
    name = "libgnutls",
    lib_source = "@libgnutls//:all",
    configure_options = ["--with-included-unistring"],
    out_include_dir = "include/gnutls",
    shared_libraries = ["libgnutls.so"],
)

configure_make(
    name = "libmicrohttpd",
    lib_source = "@libmicrohttpd//:all",
    deps = [":libgnutls"],
)

make(
    name = "libhttpserver",
    lib_source = "@libhttpserver//:all",
    make_commands = [
        "./bootstrap",
        "mkdir build_dir",
        "cd build_dir",
        "../configure --prefix=${INSTALLDIR}",
        "make",
        "make install",
    ],
    deps = [":libmicrohttpd", ":libgnutls"],
)

cc_binary(
    name = "hello-world",
    srcs = ["hello_world.cc"],
    deps = [
        ":libhttpserver"
    ],
)

hello_world.cc(libhttpserver github页面上的示例,运行“curl -XGET -v http://localhost:8080/hello”进行测试)

#include <iostream>
#include <httpserver.hpp>

using namespace std;
using namespace httpserver;

class hello_world_resource : public http_resource {
public:
    const std::shared_ptr<http_response> render(const http_request&) {
        return std::shared_ptr<http_response>(new string_response("Hello, World!"));
    }
};

int main(int argc, char** argv) {
    cout << "hello!" << std::endl;
    webserver web_server = create_webserver(8080);

    hello_world_resource resource;
    web_server.register_resource("/hello", &resource);
    web_server.start(true);

    return 0;
}
于 2020-09-21T23:11:41.337 回答