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