1104630Ssam# Use as a Bazel Dependency
2104478Ssam
3139749SimpTo use libcbor in your
4104478Ssam[Baze](https://bazel.build/)
5104478Ssamproject, first add the following section to your project's `WORKSPACE` file.
6104478SsamNote the location of the `third_party/libcbor.BUILD` file - you may use a
7162969Sjhbdifferent location if you wish, but you the file must be make available to
8104478Ssam`WORKSPACE`.
9104478Ssam
10104478Ssam## WORKSPACE
11104478Ssam
12104478SsamNote, this imports version `0.8.0` - you may need to update the version and
13104478Ssamthe sha256 hash.
14104478Ssam
15104478Ssam```python
16104478Ssam# libcbor
17104478Ssamhttp_archive(
18104478Ssam    name = "libcbor",
19104478Ssam    build_file = "//third_party:libcbor.BUILD",
20104478Ssam    sha256 = "dd04ea1a7df484217058d389e027e7a0143a4f245aa18a9f89a5dd3e1a4fcc9a",
21104478Ssam    strip_prefix = "libcbor-0.8.0",
22104478Ssam    urls = ["https://github.com/PJK/libcbor/archive/refs/tags/v0.8.0.zip"],
23104478Ssam)
24104478Ssam```
25104478Ssam
26104478Ssam## third_party/libcbor.BUILD
27104478Ssam
28104478SsamBazel will unzip the libcbor zip file, then copy this file in as `BUILD`.
29104478SsamBazel will then use this file to compile libcbor.
30104478Ssam[Cmake](https://cmake.org/)
31104478Ssamis used in two passes: to create the Makefiles, and then to invoke Make to build
32104478Ssamthe `libcbor.a` static library. `libcbor.a` and the `.h` files are then made
33104478Ssamavailable for other packages to use.
34104478Ssam
35104478Ssam```python
36104478Ssamgenrule(
37104478Ssam  name = "cbor_cmake",
38104478Ssam  srcs = glob(["**"]),
39104478Ssam  outs = ["libcbor.a", "cbor.h", "cbor/arrays.h", "cbor/bytestrings.h",
40104478Ssam          "cbor/callbacks.h", "cbor/cbor_export.h", "cbor/common.h", "cbor/configuration.h", "cbor/data.h",
41119418Sobrien          "cbor/encoding.h", "cbor/floats_ctrls.h", "cbor/ints.h", "cbor/maps.h",
42119418Sobrien          "cbor/serialization.h", "cbor/streaming.h", "cbor/strings.h", "cbor/tags.h"],
43119418Sobrien  cmd = " && ".join([
44104478Ssam    # Remember where output should go.
45104478Ssam    "INITIAL_WD=`pwd`",
46104478Ssam    # Build libcbor library.
47104478Ssam    "cd `dirname $(location CMakeLists.txt)`",
48112124Ssam    "cmake -DCMAKE_BUILD_TYPE=Release .",
49112124Ssam    "cmake --build .",
50104478Ssam    # Export the .a and .h files for cbor rule, below.
51104478Ssam    "cp src/libcbor.a src/cbor.h $$INITIAL_WD/$(RULEDIR)",
52104478Ssam    "cp src/cbor/*h cbor/configuration.h $$INITIAL_WD/$(RULEDIR)/cbor"]),
53104478Ssam  visibility = ["//visibility:private"],
54104478Ssam)
55104478Ssam
56129879Sphkcc_import(
57104478Ssam  name = "cbor",
58104478Ssam  hdrs = ["cbor.h", "cbor/arrays.h", "cbor/bytestrings.h",
59104478Ssam          "cbor/callbacks.h", "cbor/cbor_export.h", "cbor/common.h", "cbor/configuration.h", "cbor/data.h",
60104478Ssam          "cbor/encoding.h", "cbor/floats_ctrls.h", "cbor/ints.h", "cbor/maps.h",
61104478Ssam          "cbor/serialization.h", "cbor/streaming.h", "cbor/strings.h", "cbor/tags.h"],
62104478Ssam  static_library = "libcbor.a",
63104478Ssam  visibility = ["//visibility:public"],
64104478Ssam)
65104478Ssam```
66104478Ssam
67104478Ssam## third_party/BUILD
68104478Ssam
69104478SsamThe `libcbor.BUILD` file must be make available to the top-level `WORKSPACE`
70104478Ssamfile:
71104478Ssam
72104478Ssam```python
73104478Ssamexports_files(["libcbor.BUILD"]))
74104478Ssam```
75104478Ssam
76167755Ssam## Your BUILD File
77104478Ssam
78167755SsamAdd libcbor dependency to your package's `BUILD` file like so:
79167755Ssam
80119287Simp```python
81119287Simpcc_library(
82104478Ssam    name = "...",
83104478Ssam    srcs = [ ... ],
84104478Ssam    hdrs = [ ... ],
85104478Ssam    deps = [
86104478Ssam        ...
87104478Ssam        "@libcbor//:cbor",
88104478Ssam    ],
89104478Ssam)
90163648Sru```
91111416Ssam
92104478Ssam## Your C File
93104478Ssam
94104478SsamNow you may simply include `cbor.h`:
95104478Ssam
96112124Ssam```c
97112124Ssam#include "cbor.h"
98112124Ssam
99104478Ssamstatic const uint8_t version = cbor_major_version;
100104478Ssam```
101104478Ssam