Skip to content

Commit

Permalink
Merge pull request #196 from andyjgf/bazexample
Browse files Browse the repository at this point in the history
Add a bazel-built example.
  • Loading branch information
PJK committed Oct 16, 2021
2 parents 2cf83af + 047eaee commit a1656be
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ venv
**.DS_Store
.vscode
requirements.txt
examples/bazel/bazel-bazel
examples/bazel/bazel-bin
examples/bazel/bazel-out
examples/bazel/bazel-testlogs
11 changes: 9 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ script:
/clang-format.sh --verbose
git diff-index --quiet HEAD
fi;
- mkdir build && cd build
- mkdir build && pushd build
- cmake -DWITH_TESTS=ON
-DCBOR_CUSTOM_ALLOC=ON
-DCMAKE_BUILD_TYPE=Debug
Expand All @@ -69,7 +69,14 @@ script:
if grep -q 'Memory Leak\|IPW\|Uninitialized Memory Conditional\|Uninitialized Memory Read' memcheck.out; then
exit 1
fi;
# Go back to repo root
- popd
- >
if [ "$TRAVIS_OS_NAME" = "linux" -a "$TRAVIS_ARCH" = "amd64" ]; then
pushd examples/bazel
bazel run -s src:hello
popd
fi;
after_success:
- if [ "$TRAVIS_OS_NAME" = "linux" -a "$CC" = "gcc" -a "$TRAVIS_ARCH" = "amd64" ]; then codecov; fi
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Next
- Callbacks for bytestrings, strings, arrays, and maps use uint64_t instead of size_t to allow handling of large items that exceed size_t even if size_t < uint64_t
- cbor_decode explicitly checks size to avoid overflows (previously broken, potentially resulting in erroneous decoding on affected systems)
- The change should be a noop for 64b systems
- Added a [Bazel](https://bazel.build/) build example [[#196]](https://github.com/PJK/libcbor/pull/196) (by [andyjgf@](https://github.com/andyjgf))

0.8.0 (2020-09-20)
---------------------
Expand Down
33 changes: 33 additions & 0 deletions examples/bazel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Bazel Example

This directory shows an example of using LibCbor in a project that builds with Bazel.

## Compile

To build the project:

```shell
bazel build src:all
```

## Test

To test the code:

```shell
bazel test src:all
```

## Run

To run the demo:

```shell
bazel run src:hello
```

or

```shell
bazel-bin/src/hello
```
21 changes: 21 additions & 0 deletions examples/bazel/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
workspace(name = "libcbor_bazel_example")

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

# Google Test
http_archive(
name = "gtest",
sha256 = "94c634d499558a76fa649edb13721dce6e98fb1e7018dfaeba3cd7a083945e91",
strip_prefix = "googletest-release-1.10.0",
url = "https://github.com/google/googletest/archive/release-1.10.0.zip",
)

# libcbor
http_archive(
name = "libcbor",
build_file = "//third_party:libcbor.BUILD",
sha256 = "dd04ea1a7df484217058d389e027e7a0143a4f245aa18a9f89a5dd3e1a4fcc9a",
strip_prefix = "libcbor-0.8.0",
urls = ["https://github.com/PJK/libcbor/archive/refs/tags/v0.8.0.zip"],
)

46 changes: 46 additions & 0 deletions examples/bazel/src/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_library(
name = "src",
srcs = [
"hello.cc",
],
hdrs = [
"hello.h",
],
visibility = [
"//src:__pkg__",
],
deps = [
"@libcbor//:cbor",
],
)

cc_test(
name = "tests",
size = "small",
srcs = [
"hello_test.cc",
],
visibility = [
"//visibility:private",
],
deps = [
":src",
"@gtest//:gtest_main",
"@libcbor//:cbor",
],
)


cc_binary(
name = "hello",
srcs = [
"main.cc",
],
deps = [
":src",
],
)

8 changes: 8 additions & 0 deletions examples/bazel/src/hello.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "src/hello.h"

#include "cbor.h"

uint8_t cbor_version() {
return cbor_major_version;
}

8 changes: 8 additions & 0 deletions examples/bazel/src/hello.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef HELLO_H_
#define HELLO_H_

#include <cstdint>

uint8_t cbor_version();

#endif // HELLO_H_
10 changes: 10 additions & 0 deletions examples/bazel/src/hello_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "src/hello.h"

#include "gtest/gtest.h"

class HelloTest : public ::testing::Test {};

TEST_F(HelloTest, CborVersion) {
EXPECT_EQ(cbor_version(), 0);
}

9 changes: 9 additions & 0 deletions examples/bazel/src/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "src/hello.h"

#include <stdio.h>

int main() {
printf("Hello, v=%d\n", cbor_version());
return 0;
}

1 change: 1 addition & 0 deletions examples/bazel/third_party/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports_files(["libcbor.BUILD"])
19 changes: 19 additions & 0 deletions examples/bazel/third_party/libcbor.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cc_library(
name = "cbor",
srcs = glob([
"src/**/*.h",
"src/**/*.c",

]),
deps = [
"@libcbor_bazel_example//third_party/libcbor:config",
],
hdrs = [
"src/cbor.h",
],
includes = [
"src",
],
visibility = ["//visibility:public"],
)

11 changes: 11 additions & 0 deletions examples/bazel/third_party/libcbor/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cc_library(
name = "config",
hdrs = [
"cbor/cbor_export.h",
"cbor/configuration.h",
],
includes = [
"./",
],
visibility = ["//visibility:public"],
)
42 changes: 42 additions & 0 deletions examples/bazel/third_party/libcbor/cbor/cbor_export.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

#ifndef CBOR_EXPORT_H
#define CBOR_EXPORT_H

#ifdef CBOR_STATIC_DEFINE
#define CBOR_EXPORT
#define CBOR_NO_EXPORT
#else
#ifndef CBOR_EXPORT
#ifdef cbor_EXPORTS
/* We are building this library */
#define CBOR_EXPORT
#else
/* We are using this library */
#define CBOR_EXPORT
#endif
#endif

#ifndef CBOR_NO_EXPORT
#define CBOR_NO_EXPORT
#endif
#endif

#ifndef CBOR_DEPRECATED
#define CBOR_DEPRECATED __attribute__((__deprecated__))
#endif

#ifndef CBOR_DEPRECATED_EXPORT
#define CBOR_DEPRECATED_EXPORT CBOR_EXPORT CBOR_DEPRECATED
#endif

#ifndef CBOR_DEPRECATED_NO_EXPORT
#define CBOR_DEPRECATED_NO_EXPORT CBOR_NO_EXPORT CBOR_DEPRECATED
#endif

#if 0 /* DEFINE_NO_DEPRECATED */
#ifndef CBOR_NO_DEPRECATED
#define CBOR_NO_DEPRECATED
#endif
#endif

#endif /* CBOR_EXPORT_H */
16 changes: 16 additions & 0 deletions examples/bazel/third_party/libcbor/cbor/configuration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef LIBCBOR_CONFIGURATION_H
#define LIBCBOR_CONFIGURATION_H

#define CBOR_MAJOR_VERSION 0
#define CBOR_MINOR_VERSION 8
#define CBOR_PATCH_VERSION 0

#define CBOR_CUSTOM_ALLOC 1
#define CBOR_BUFFER_GROWTH 2
#define CBOR_MAX_STACK_SIZE 2048
#define CBOR_PRETTY_PRINTER 1

#define CBOR_RESTRICT_SPECIFIER restrict
#define CBOR_INLINE_SPECIFIER

#endif // LIBCBOR_CONFIGURATION_H

0 comments on commit a1656be

Please sign in to comment.