diff --git a/.gitignore b/.gitignore index 6258738a..3a936d81 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/.travis.yml b/.travis.yml index 85f6b211..3cd58dfa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 228810fe..ba2c92f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) --------------------- diff --git a/examples/bazel/README.md b/examples/bazel/README.md new file mode 100644 index 00000000..455e2d38 --- /dev/null +++ b/examples/bazel/README.md @@ -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 +``` diff --git a/examples/bazel/WORKSPACE b/examples/bazel/WORKSPACE new file mode 100644 index 00000000..231f01db --- /dev/null +++ b/examples/bazel/WORKSPACE @@ -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"], +) + diff --git a/examples/bazel/src/BUILD b/examples/bazel/src/BUILD new file mode 100644 index 00000000..d3acb578 --- /dev/null +++ b/examples/bazel/src/BUILD @@ -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", + ], +) + diff --git a/examples/bazel/src/hello.cc b/examples/bazel/src/hello.cc new file mode 100644 index 00000000..8ff59edf --- /dev/null +++ b/examples/bazel/src/hello.cc @@ -0,0 +1,8 @@ +#include "src/hello.h" + +#include "cbor.h" + +uint8_t cbor_version() { + return cbor_major_version; +} + diff --git a/examples/bazel/src/hello.h b/examples/bazel/src/hello.h new file mode 100644 index 00000000..b4ecafaf --- /dev/null +++ b/examples/bazel/src/hello.h @@ -0,0 +1,8 @@ +#ifndef HELLO_H_ +#define HELLO_H_ + +#include + +uint8_t cbor_version(); + +#endif // HELLO_H_ diff --git a/examples/bazel/src/hello_test.cc b/examples/bazel/src/hello_test.cc new file mode 100644 index 00000000..68d8633c --- /dev/null +++ b/examples/bazel/src/hello_test.cc @@ -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); +} + diff --git a/examples/bazel/src/main.cc b/examples/bazel/src/main.cc new file mode 100644 index 00000000..27e1a4d1 --- /dev/null +++ b/examples/bazel/src/main.cc @@ -0,0 +1,9 @@ +#include "src/hello.h" + +#include + +int main() { + printf("Hello, v=%d\n", cbor_version()); + return 0; +} + diff --git a/examples/bazel/third_party/BUILD b/examples/bazel/third_party/BUILD new file mode 100644 index 00000000..c4c443f1 --- /dev/null +++ b/examples/bazel/third_party/BUILD @@ -0,0 +1 @@ +exports_files(["libcbor.BUILD"]) diff --git a/examples/bazel/third_party/libcbor.BUILD b/examples/bazel/third_party/libcbor.BUILD new file mode 100644 index 00000000..f5fe32a1 --- /dev/null +++ b/examples/bazel/third_party/libcbor.BUILD @@ -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"], +) + diff --git a/examples/bazel/third_party/libcbor/BUILD b/examples/bazel/third_party/libcbor/BUILD new file mode 100644 index 00000000..2ef8702f --- /dev/null +++ b/examples/bazel/third_party/libcbor/BUILD @@ -0,0 +1,11 @@ +cc_library( + name = "config", + hdrs = [ + "cbor/cbor_export.h", + "cbor/configuration.h", + ], + includes = [ + "./", + ], + visibility = ["//visibility:public"], +) diff --git a/examples/bazel/third_party/libcbor/cbor/cbor_export.h b/examples/bazel/third_party/libcbor/cbor/cbor_export.h new file mode 100644 index 00000000..8bf3dea8 --- /dev/null +++ b/examples/bazel/third_party/libcbor/cbor/cbor_export.h @@ -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 */ diff --git a/examples/bazel/third_party/libcbor/cbor/configuration.h b/examples/bazel/third_party/libcbor/cbor/configuration.h new file mode 100644 index 00000000..c8c01576 --- /dev/null +++ b/examples/bazel/third_party/libcbor/cbor/configuration.h @@ -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