1# Use as a Bazel Dependency
2
3To use libcbor in your
4[Baze](https://bazel.build/)
5project, first add the following section to your project's `WORKSPACE` file.
6Note the location of the `third_party/libcbor.BUILD` file - you may use a
7different location if you wish, but you the file must be make available to
8`WORKSPACE`.
9
10## WORKSPACE
11
12Note, this imports version `0.8.0` - you may need to update the version and
13the sha256 hash.
14
15```python
16# libcbor
17http_archive(
18    name = "libcbor",
19    build_file = "//third_party:libcbor.BUILD",
20    sha256 = "dd04ea1a7df484217058d389e027e7a0143a4f245aa18a9f89a5dd3e1a4fcc9a",
21    strip_prefix = "libcbor-0.8.0",
22    urls = ["https://github.com/PJK/libcbor/archive/refs/tags/v0.8.0.zip"],
23)
24```
25
26## third_party/libcbor.BUILD
27
28Bazel will unzip the libcbor zip file, then copy this file in as `BUILD`.
29Bazel will then use this file to compile libcbor.
30[Cmake](https://cmake.org/)
31is used in two passes: to create the Makefiles, and then to invoke Make to build
32the `libcbor.a` static library. `libcbor.a` and the `.h` files are then made
33available for other packages to use.
34
35```python
36genrule(
37  name = "cbor_cmake",
38  srcs = glob(["**"]),
39  outs = ["libcbor.a", "cbor.h", "cbor/arrays.h", "cbor/bytestrings.h",
40          "cbor/callbacks.h", "cbor/cbor_export.h", "cbor/common.h", "cbor/configuration.h", "cbor/data.h",
41          "cbor/encoding.h", "cbor/floats_ctrls.h", "cbor/ints.h", "cbor/maps.h",
42          "cbor/serialization.h", "cbor/streaming.h", "cbor/strings.h", "cbor/tags.h"],
43  cmd = " && ".join([
44    # Remember where output should go.
45    "INITIAL_WD=`pwd`",
46    # Build libcbor library.
47    "cd `dirname $(location CMakeLists.txt)`",
48    "cmake -DCMAKE_BUILD_TYPE=Release .",
49    "cmake --build .",
50    # Export the .a and .h files for cbor rule, below.
51    "cp src/libcbor.a src/cbor.h $$INITIAL_WD/$(RULEDIR)",
52    "cp src/cbor/*h cbor/configuration.h $$INITIAL_WD/$(RULEDIR)/cbor"]),
53  visibility = ["//visibility:private"],
54)
55
56cc_import(
57  name = "cbor",
58  hdrs = ["cbor.h", "cbor/arrays.h", "cbor/bytestrings.h",
59          "cbor/callbacks.h", "cbor/cbor_export.h", "cbor/common.h", "cbor/configuration.h", "cbor/data.h",
60          "cbor/encoding.h", "cbor/floats_ctrls.h", "cbor/ints.h", "cbor/maps.h",
61          "cbor/serialization.h", "cbor/streaming.h", "cbor/strings.h", "cbor/tags.h"],
62  static_library = "libcbor.a",
63  visibility = ["//visibility:public"],
64)
65```
66
67## third_party/BUILD
68
69The `libcbor.BUILD` file must be make available to the top-level `WORKSPACE`
70file:
71
72```python
73exports_files(["libcbor.BUILD"]))
74```
75
76## Your BUILD File
77
78Add libcbor dependency to your package's `BUILD` file like so:
79
80```python
81cc_library(
82    name = "...",
83    srcs = [ ... ],
84    hdrs = [ ... ],
85    deps = [
86        ...
87        "@libcbor//:cbor",
88    ],
89)
90```
91
92## Your C File
93
94Now you may simply include `cbor.h`:
95
96```c
97#include "cbor.h"
98
99static const uint8_t version = cbor_major_version;
100```
101