Skip to content

obazl/rules_ctypes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

obazl_ctypes

Bazel rules for the ocaml-ctypes library.

Usage

Since obazl_ctypes is designed for use with rules_ocaml and opam, this example shows a complete minimal configuration:

MODULE.bazel
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:

BUILD.bazel
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"]
)
  1. In this example, @zstd is the label of an external Bazel module, registered in MODULE.bazel, that will build libzstd.a when this target is built.

Here Types and Functions are Ctypes-dependent functors, defined in type_description.ml and function_description.ml respetively:

type_description.ml
module Types (F : Ctypes.TYPE) = struct
       open F
       ...
end
function_description.ml
module Functions (F : Cstubs.FOREIGN) = struct
  open F
       ...
end
Note
You can choose your own names in place of Types, type_description.ml, etc.

Rule: ctypes_module

Note
ctypes_module is actually a macro, but it behaves like a rule so we call it one.

Attribute

Type

Default

Mandatory?

Notes

name

string

none

yes

Target name. Sets <api_stem>.

functors

string_map, keys: file names; vals: functor names

none

yes

specifies structure of source code

cclibs

label_list

none

yes

CC lib being wrapped

cchdrs

string_list

none

no

CC lib headers

api_name

string

<api_stem>_api

no

Name of API module (not archive) to be generated. No relation to dune/opam/findlib pkg naming;

wrapper

string

none

no

names module embedded in API module, wrapping the Types and Functions modules;

types_module

string

<api_stem>_Types

no

name of Types module

functions_module

string

<api_stem>_Functions

no

name of Functions module

How it works

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.

Comparison: Dune’s ctypes stanza

About

Bazelized fork of ocaml-ctypes

Resources

License

Stars

Watchers

Forks

Packages

No packages published