Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibly support bazel build? #79

Open
yesudeep opened this issue Jul 19, 2020 · 5 comments
Open

Possibly support bazel build? #79

yesudeep opened this issue Jul 19, 2020 · 5 comments

Comments

@yesudeep
Copy link

yesudeep commented Jul 19, 2020

Hi!

Thank you for this amazing library.

Bazel makes it easy to integrate the library by simply specifying the target dependency as "@replxx". The users of the library don't need to know how to build the library as a result. Do you think the library could support building with Bazel directly so people wouldn't have to maintain their own BUILD.bazel files? Here's example usage:

cc_binary(
    name = "executable",
    srcs = ["src/main.cc"],
    deps = [
        "@replxx",       # <-- super simple.
    ],
)

I include it in my workspace using:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "replxx",
    type = "zip",
    url = "https://github.com/AmokHuginnsson/replxx/archive/<commit>.zip",
    sha256 = "<sha256-digest>",
    strip_prefix = "replxx-<commit>",
    # Don't need the following line if project includes a Bazel build file shown below along 
    # with a WORKSPACE file.
    build_file = "//third_party/cc/remote:replxx.BUILD",
)

BUILD.bazel file:

load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")

licenses(["notice"])  # BSD

exports_files(["LICENSE.md"])

cc_binary(
    name = "example_c",
    srcs = [
        "examples/c-api.c",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":examples_util_lib",
    ],
)

cc_binary(
    name = "example_cc",
    srcs = [
        "examples/cxx-api.cxx",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":examples_util_lib",
    ],
)

cc_library(
    name = "examples_util_lib",
    srcs = [
        "examples/util.c",
    ],
    hdrs = [
        "examples/util.h",
    ],
    deps = [
        ":replxx",
    ],
)

cc_library(
    name = "replxx",
    srcs = [
        "src/ConvertUTF.cpp",
        "src/conversion.cxx",
        "src/escape.cxx",
        "src/history.cxx",
        "src/io.cxx",
        "src/prompt.cxx",
        "src/replxx.cxx",
        "src/replxx_impl.cxx",
        "src/util.cxx",
        "src/wcwidth.cpp",
        "src/windows.cxx",
    ],
    hdrs = [
        "include/replxx.h",
        "include/replxx.hxx",
        "src/ConvertUTF.h",
        "src/conversion.hxx",
        "src/escape.hxx",
        "src/history.hxx",
        "src/io.hxx",
        "src/killring.hxx",
        "src/prompt.hxx",
        "src/replxx_impl.hxx",
        "src/unicodestring.hxx",
        "src/utf8string.hxx",
        "src/util.hxx",
        "src/windows.hxx",
    ],
    includes = [
        "include",
    ],
    linkstatic = True,
    visibility = ["//visibility:public"],
)

To build and run the example, I just type bazel run "@replx//:example_cc":

❯ bazel run "@replxx//:example_cc"
INFO: Analyzed target @replxx//:example_cc (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target @replxx//:example_cc up-to-date:
  bazel-bin/external/replxx/example_cc
INFO: Elapsed time: 5.804s, Critical Path: 2.85s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Welcome to Replxx
Press 'tab' to view autocompletions
Type '.help' for help
Type '.quit' or '.exit' to exit

replxx> 

Bazel fetches the correct version and enables reproducible builds. The only changes the user needs are the 1st and the 2nd code snippets. That allows the user to have hermetic, reproducible, and fast builds.

What do you think?

@AmokHuginnsson
Copy link
Owner

If I understand correctly bazel is not complementary to cmake but rather it is its replacement?
I not going to replace cmake build and supporting two build systems seem to be an overkill for me.

@yesudeep
Copy link
Author

yesudeep commented Jul 22, 2020

Namaste!

Bazel can leverage cmake and autotools as well. The fastest builds are when the library supports Bazel directly, however, because then it knows the entire build DAG. The most barebones build that wouldn't require much maintainance would be something like:

load("@rules_cc//cc:defs.bzl", "cc_library")

licenses(["notice"])  # BSD

exports_files(["LICENSE.md"])

cc_library(
    name = "replxx",
    srcs = glob([
        "src/**/*.cpp", 
        "src/**/*.cxx",
    ]),
    hdrs = glob([
        "include/**/*.h", 
        "include/**/*.hxx", 
        "src/**/*.h",
        "src/**/*.hxx",
    ]),
    includes = [
        "include",
    ],
    linkstatic = True,
    visibility = ["//visibility:public"],
)

C++ would do great with clean and easy dependency management. Bazel just makes it easy to pull remote libraries into the workspace dynamically and have repeatable, fast and hermetic builds. Here's an example of how I'm using it currently:

cc_binary(
    name = "foo",
    srcs = [
        "foo.cc",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":repl_lib",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/flags:parse",
        "@fmt",
        "@replxx",   # <-- This is all I need to do to include, link, and compile replxx. 
    ],
)

As a user of a C++ library, I no longer have to deal with conflicts with different installations (system or otherwise) of the library. When I run `bazel build :foo" Bazel fetches a very specific commit from github, so I get a reproducible build. For future builds it caches the download. Referring to and rolling back references to remote C++ libraries becomes easy. However, I'll leave it up to you. Anybody who needs to refer to a specific commit in their workspace for a hermetic build can certainly come back to this thread and use the build files shown here by using them as remote build files. :)

@AmokHuginnsson
Copy link
Owner

The barebones version looks much more appealing to me.
Could you create a pull request with it?

Thank you.

@AmokHuginnsson
Copy link
Owner

Any update on this?

@yesudeep
Copy link
Author

yesudeep commented Dec 4, 2020

Hi, sorry for the delay on this. I'll send a PR in a few.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants