ocaml_module Last updated Mar 27, 2025

load("@rules_ocaml//build:rules.bzl", "ocaml_module")

Compiles an OCaml module. The module name is determined by rule, based on the struct, sig, name, and module attributes:

  • If the sig attribute is the label of an ocaml_signature target, then the module name is derived from the name of the compiled sigfile, since compiled interface files cannot be renamed. The structfile will be renamed if it does not match the sigfile name.

  • If the sig attribute is a filename, then:

    • if its principal name is equal to the principal name of the file named in the struct attribute, then the module name is derived from it.

    • if the principal names of the sigfile and structfile do not match, then the module name is derived from from the name attribute. Both the sigfile and the structfile will be renamed accordingly.

    • The module attribute may be used to force the module name. Both the sigfile and the structfile will be renamed accordingly.

  • If the sig attribute is not specified (i.e. the structfile is "orphaned"), then the module name will be derived from the structfile name, unless the module attribute is specified, in which case it overrides.

Examples

Configurable defaults

In addition to the Configurable defaults that apply to all ocaml_* rules, the following apply to this rule:

Options

Label Default Comments

@rules_ocaml//cfg/module:deps

@rules_ocaml//cfg:null

list of OCaml deps to add to all ocaml_module instances

@rules_ocaml//cfg/module:cc_deps1

@rules_ocaml//cfg:null

list of cc_deps to add to all ocaml_module instances

@rules_ocaml//cfg/module:cc_linkstatic1

@rules_ocaml//cfg:null

list of cc_deps to link statically (DEPRECATED)

@rules_ocaml//cfg/module:warnings

@1..3@5..28@30..39@43@46..47@49..57@61..62-40

sets -w option for all ocaml_module instances

1 See CC Dependencies for more information on CC deps.

Boolean Flags

These do not support :enable, :disable syntax.
Label Default opts attrib equivalent

@rules_ocaml//cfg/module/linkall

False

-linkall, -no-linkall

@rules_ocaml//cfg/module:verbose

False

-verbose, -no-verbose

Attributes

Name Description Type Mandatory Default

name

A unique name for this target.

Name

required

deps

List of dependencies. May include C/C++ libraries produced by rules_cc.

List of labels

optional

[]

data

Runtime data dependencies: list of labels of data files needed by this module at runtime. This is a standard Bazel attribute; see Typical attributes.

List of labels

optional

[]

cc_deps

Static (.a) or dynamic (.so, .dylib) libraries. Must by built or imported using Bazel’s rules_cc ruleset (thus providing CcInfo output). CC libraries listed here will be built with compilation_mode set to opt. CC libraries listed in deps will be built with the compilation mode in effect for this ocaml_module target, usually fastbuild.

List of labels

optional

[]

cc_linkage

Dictionary specifying C/C++ library dependencies. Allows finer control over linking than the 'cc_deps' attribute. Key: a target label providing CcInfo; value: a linkmode string, which determines which file to link. Valid linkmodes: 'default', 'static', 'dynamic', 'shared' (synonym for 'dynamic'). For more information see CC Dependencies: Linkmode.

Dictionary: Label -> String

optional

{}

deps_runtime

Runtime module dependencies, e.g. .cmxs plugins. Use the data attribute for runtime data dependencies.

List of labels

optional

[]

env

Env variables

Dictionary: String -> String

optional

{}

module_name

Use this string as module name, instead of deriving it from sig or struct

String

optional

""

ns

Label of an ocaml_ns target, which must list this module in its manifest attribute. For bottom-up namespacing. Modules may use this attribute to elect membership in a bottom-up namespace.

Label

optional

None

open

List of OCaml dependencies to be passed with -open.

List of labels

optional

[]

opts

List of compile options; overrides configurable default options. Supports -no- prefix for each option; for example, -no-linkall.

List of strings

optional

[]

pack

Experimental. List of pack submodules. They will be compiled with -for-pack, and this module will be compile with -pack.

List of labels

optional

[]

ppx

Label of ppx_executable target to be used to transform source before compilation.

Label

optional

None

ppx_args

Options to pass to PPX executable passed by the ppx attribute.

List of strings

optional

[]

ppx_data

PPX runtime data dependencies. List of labels of files needed by the PPX executable passed via the ppx attribute when it is executed to transform the source file. For example, a source file using ppx_optcomp may import a file using extension [%%import ]; this file should be listed in this attribute.

List of labels

optional

[]

ppx_print

Format of output of PPX transform: binary (default) or text. Value must be one of @rules_ocaml//ppx/print:binary! or @rules_ocaml//ppx/print:text!.

Label

optional

None

ppx_verbose

-

Boolean

optional

False

sig

Single label of a target producing OCamlSignatureProvider (i.e. rule ocaml_signature) OR a sig source file. Optional.

Label

optional

None

struct

A single module (struct) source file label.

Label

required