Skip to content

Commit

Permalink
build: Add support for Bazel (#760)
Browse files Browse the repository at this point in the history
* Add Bazel support

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Build custom branch

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Fix C++ flag

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Add export file

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Fix bazel execution in CI

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* print dir

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Revert "print dir"

This reverts commit 511b828.

* Fix cache path

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* inject export

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* inject export

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Do not inject definitions

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Update export.h

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Split bazel build

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Remove define from bazel

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Remove export.h

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Generate export.h by bazel

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Update contribution guide

Signed-off-by: Uilian Ries <uilianries@gmail.com>

---------

Signed-off-by: Uilian Ries <uilianries@gmail.com>
Co-authored-by: Michał Cieślar <michalovskyyy@gmail.com>
  • Loading branch information
uilianries and cieslarmichal authored Jun 29, 2024
1 parent 213ce21 commit 6102421
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 2 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/linux-bazel-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: bazel

on:
push:
branches:
- 'main'
paths-ignore:
- 'docs/**'
- '**/*.md'
- 'LICENSE'
workflow_dispatch:
pull_request:
paths-ignore:
- 'docs/**'
- '**/*.md'
- 'LICENSE'

jobs:
bazel:
name: ci-ubuntu-24.04-gcc-bazel
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install ccache
uses: hendrikmuhs/ccache-action@v1.2

- name: Cache Bazel
id: cache-bazel
uses: actions/cache@v4.0.2
env:
cache-name: cache-bazel-data
with:
path: /home/runner/.cache/bazel/_bazel_runner
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('MODULE.bazel') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Build faker-cxx library
run: bazel build //:faker-cxx

- name: Build faker-cxx examples and run
run: |
bazel build //examples:faker-cxx-basic-example
bazel build //examples:faker-cxx-person-example
bazel-bin/examples/faker-cxx-person-example
bazel-bin/examples/faker-cxx-basic-example
- name: Build faker-cxx tests and validate
run: |
bazel build //tests:faker-cxx-ut
bazel-bin/tests/faker-cxx-ut
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ build-linux-clang
#kdevelop temp files
*.kdev4
.kdev4

bazel-*
MODULE.bazel.lock
install/
28 changes: 28 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

genrule(
name = "generate_export_header",
srcs = [],
outs = ["include/faker-cxx/Export.h"],
cmd = """
echo "#pragma once" > $@
echo "#define FAKER_CXX_EXPORT" >> $@
""",
)

cc_library(
name = "faker-cxx",
srcs = glob(["src/**/*.cpp"]),
hdrs = glob(["src/**/*.h", "include/**/*.h"]) + [":generate_export_header"],
includes = ["include", "src"],
visibility = ["//visibility:public"],
deps = ["@fmt//:fmt"],
copts = select({
"//conditions:default": [
"-std=c++20"
],
"@platforms//os:windows": [
"/std:c++20",
],
}),
)
37 changes: 35 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ bottom of the window, and then selecting "Save with Encoding" and choosing "UTF-

## Building and Testing the Project

### Building the Project
### Building the Project with CMake

To build the project, we use [CMake](https://cmake.org/getting-started/) with
[presets](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html).
Expand Down Expand Up @@ -140,6 +140,23 @@ Follow the steps below to build the project:
cmake --build --preset unixlike-gcc-debug-static
```

### Building the Project with Bazel

As alternative, this project can be built using [Bazel](https://bazel.build/). The dependencies are managed directly by
Bazel modules, downloading and building all external dependencies. Follow the steps below to build the project:

1. **Navigate to the project directory:**

```sh
cd /path/to/faker-cxx
```

2. **Build the project:**

```sh
bazel build //:faker-cxx
```

#### Choosing between STL std::format and fmt library

The faker-cxx project uses formated string feature, which can be solved by:
Expand All @@ -165,7 +182,7 @@ acquire `fmt`, the CMake option `USE_SYSTEM_DEPENDENCIES` manages if should be u
In case passing `USE_STD_FORMAT=ON` and `std::format` is not available, CMake will try to use `fmt` library automatically.
Then, in case not finding `fmt`, it will fail.

### Testing the Project
### Testing the Project with CMake/CTest

After building the project, you can run the tests to ensure everything is working correctly. We use CTest for running
tests. Follow the steps below to test the project:
Expand All @@ -176,6 +193,22 @@ tests. Follow the steps below to test the project:
ctest --preset unixlike-gcc-debug-static
```

### Testing the Project with Bazel

As alternative, tests and be built and validated using Bazel as well. Follow the steps below to test the project:

**1. Build the tests using Bazel**

```sh
bazel build //tests:faker-cxx-ut
```

**2. Run the tests generated by Bazel:**

```sh
bazel-bin/tests/faker-cxx-ut
```

### Installing the Project

When wanting to install those generated artifacts like headers files and library, you can use CMake to operate as installer as well:
Expand Down
4 changes: 4 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

bazel_dep(name = "rules_foreign_cc", version = "0.10.1")
bazel_dep(name = "fmt", version = "10.2.1")
bazel_dep(name = "googletest", version = "1.14.0")
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[![apple clang++](https://github.com/cieslarmichal/faker-cxx/actions/workflows/macos-clang-build.yml/badge.svg?branch=main)](https://github.com/cieslarmichal/faker-cxx/actions/workflows/macos-clang-build.yml?query=branch%3Amain)
[![g++](https://github.com/cieslarmichal/faker-cxx/actions/workflows/linux-gxx-build.yml/badge.svg?branch=main)](https://github.com/cieslarmichal/faker-cxx/actions/workflows/linux-gxx-build.yml?query=branch%3Amain)
[![msvc](https://github.com/cieslarmichal/faker-cxx/actions/workflows/windows-msvc-build.yml/badge.svg?branch=main)](https://github.com/cieslarmichal/faker-cxx/actions/workflows/windows-msvc-build.yml?query=branch%3Amain)
[![bazel](https://github.com/cieslarmichal/faker-cxx/actions/workflows/linux-bazel-build.yml/badge.svg?branch=main)](https://github.com/cieslarmichal/faker-cxx/actions/workflows/linux-bazel-build.yml?query=branch%3Amain)
[![codecov](https://codecov.io/github/cieslarmichal/faker-cxx/branch/main/graph/badge.svg?token=0RTV4JFH2U)](https://codecov.io/github/cieslarmichal/faker-cxx)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
[![Chat on Discord](https://img.shields.io/badge/chat-discord-blue?style=flat&logo=discord)](https://discord.gg/h2ur8H6mK6)
Expand Down
1 change: 1 addition & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
workspace(name = "faker-cxx")
29 changes: 29 additions & 0 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "faker-cxx-basic-example",
srcs = ["basic/main.cpp",],
deps = ["//:faker-cxx",],
copts = select({
"//conditions:default": [
"-std=c++20"
],
"@platforms//os:windows": [
"/std:c++20",
],
}),
)

cc_binary(
name = "faker-cxx-person-example",
srcs = ["person/main.cpp",],
deps = ["//:faker-cxx",],
copts = select({
"//conditions:default": [
"-std=c++20"
],
"@platforms//os:windows": [
"/std:c++20",
],
}),
)
16 changes: 16 additions & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_test(
name = "faker-cxx-ut",
srcs = glob(["**/*.cpp"]),
deps = ["//:faker-cxx", "@googletest//:gtest_main"],
includes = ["../src/common", "../src/modules"],
copts = select({
"//conditions:default": [
"-std=c++20"
],
"@platforms//os:windows": [
"/std:c++20",
],
}),
)

0 comments on commit 6102421

Please sign in to comment.