Bazel rules for the ocaml-ctypes library.
Since obazl_ctypes is designed for use with rules_ocaml and opam,
this example shows a complete minimal configuration:
bazel_dep(name = "rules_ocaml", version = "3.0.0")
bazel_dep(name = "tools_opam", version = "1.0.0")
opam = use_extension("@tools_opam//extensions:opam.bzl", "opam")
opam.deps(
pkgs = { "pkg": "x.y.z", ...other opam deps... },
toolchain = "global" # | "local" | "xdg"
ocaml_version = "5.3.0"
)
use_repo(opam, "opam.ocamlsdk")
use_repo(opam, "opam.pkg", ...other opam deps...)
register_toolchains(
"@opam.ocamlsdk//toolchain/selectors/local:all",
"@opam.ocamlsdk//toolchain/profiles:all",
)
bazel_dep(name = "obazl_ctypes", version = "0.23.0")
use_repo(opam, "opam.ctypes", "opam.ctypes-foreign")
Your build code:
load("@obazl_ctypes//build/rules:ctypes.bzl", "ctypes_module")
ctypes_module(
name = "zstd",
functors = {"type_description.ml": "Types",
"function_description.ml": "Functions"},
cclibs = ["@zstd"], (1)
cchdrs = ["zstd.h"]
)
-
In this example,
@zstdis the label of an external Bazel module, registered inMODULE.bazel, that will buildlibzstd.awhen this target is built.
Here Types and Functions are Ctypes-dependent functors,
defined in type_description.ml and function_description.ml respetively:
module Types (F : Ctypes.TYPE) = struct
open F
...
end
module Functions (F : Cstubs.FOREIGN) = struct
open F
...
end
|
Note
|
You can choose your own names in place of Types,
type_description.ml, etc.
|
|
Note
|
ctypes_module is actually a macro, but it behaves like a rule so we call it one.
|
Attribute |
Type |
Default |
Mandatory? |
Notes |
|
string |
none |
yes |
Target name. Sets <api_stem>. |
|
string_map, keys: file names; vals: functor names |
none |
yes |
specifies structure of source code |
|
label_list |
none |
yes |
CC lib being wrapped |
|
string_list |
none |
no |
CC lib headers |
|
string |
|
no |
Name of API module (not archive) to be generated. No relation to dune/opam/findlib pkg naming; |
|
string |
none |
no |
names module embedded in API module, wrapping the Types and Functions modules; |
|
string |
|
no |
name of Types module |
|
string |
|
no |
name of Functions module |
To list the files generated by the build:
bazel query 'kind("generated file", //stubs:*)'
The essential files:
-
Zstd_CtypesTYPE - specialization of mtype Ctypes.TYPE
-
Zstd_CtypesFOREIGN - specialization of mtype Ctypes.FOREIGN
-
libzstd_c_adapter_lib.a - dep of Zstd_CtypesFOREIGN
Conveniences:
-
Zstd_api - simple wrapper, aliases Types and Functions functor applications
-
Zstd_Types (dune: Types_generated) - wrapper, includes Type_description.Types(Zstd_CtypesTYPE). Not really needed.