Mizux / bazel-cpp

Bazel C++ sample

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Github-CI: Build Status Build Status Build Status Build Status

Introduction

| Requirement | Codemap | Dependencies | Build | Tutorial | CI | Appendices | License |

Bazel C++ sample with tests and GitHub CI support.

This project should run on GNU/Linux, MacOS and Windows.

Requirement

You'll need:

  • "Bazel >= 5.0".

Codemap

The project layout is as follow:

Dependencies

To complexify a little, the CMake project is composed of three libraries (Foo, Bar and FooBar) with the following dependencies:

Foo:
Bar:
FooBar: PUBLIC Foo PRIVATE Bar
App: PRIVATE FooBar

note: Since Foo is a public dependency of FooBar, then FooBarApp will see Foo inlude directories

Build

To build this example you should use:

  • on UNIX:

    bazel build -c opt --action_env=BAZEL_CXXOPTS="-std=c++17" --subcommands=true ...
  • on Windows when using MSVC:

    bazel build -c opt --cxxopt="/std:c++17" --subcommands=true ...

Running Tests

To build this example you should use:

  • on UNIX:

    bazel test -c opt --action_env=BAZEL_CXXOPTS="-std=c++17" --test_output=all ...
  • on Windows when using MSVC:

    bazel test -c opt --cxxopt="/std:c++17" --test_output=all ...

Tutorial

Visibility

In Bazel, subdirectories containing BUILD files are known as packages.
The new property visibility will tell Bazel which package(s) can reference this target, in this case the //main package can use hello-time library.

lib/BUILD:

cc_library(
    name = "hello-time",
    srcs = ["hello-time.cc"],
    hdrs = ["hello-time.h"],
    visibility = ["//main:__pkg__"],
)

local dependencies

To use our hello-time libary, an extra dependency is added in the form of //path/to/package:target_name, in this case, it's //lib:hello-time.

main/BUILD:

cc_binary(
    name = "hello-world",
    srcs = ["hello-world.cc"],
    deps = [
        ":hello-greet",
        "//lib:hello-time",
    ],
)

CI Setup

Please take a look at .github/workflows to find the configuration file for each jobs.

To install bazel on each hosted runner, follow these links: ref: https://docs.github.com/en/actions/using-github-hosted-runners/customizing-github-hosted-runners#installing-software-on-windows-runners

Appendices

Few links on the subject...

Resources

Project layout:

  • The Pitchfork Layout Revision 1 (cxx-pflR1)

Bazel:

Misc

Image has been generated using plantuml:

plantuml -Tsvg docs/{file}.dot

So you can find the dot source files in docs.

License

Apache 2. See the LICENSE file for details.

Disclaimer

This is not an official Google product, it is just code that happens to be owned by Google.

About

Bazel C++ sample

License:Apache License 2.0


Languages

Language:C++ 72.1%Language:Makefile 14.3%Language:Dockerfile 9.9%Language:Starlark 3.5%Language:Shell 0.2%