Unit_info(3)
π NAME π
Bestie, welcome to Unit_info(3) from the OCaml library! This is the module that centralizes the handling of compilation files and their metadata. It's giving organizational queen energy, no cap.
π¦ Module π¦
Module Unit_info
β¨ Documentation β¨
The page spells the module header like this:
Module Unit_info
: sig end
Okay so this module is the centralized handling HQ for compilation files and their metadata. And maybe even more importantly β honestly, main character energy β it gives you functions for deriving module names from strings or filenames. Slay.
π Module name convention and computation π
Here are the type besties:
type intf_or_impl =
| Intf
| Impl
type modname = string
type filename = string
type file_prefix = string
type error =
| Invalid_encoding of filename
exception Error of error
val modulize : string -> modnameβmodulize scapitalizes the first letter ofs. Cute little glow-up for your string.val normalize : string -> stringβnormalize suncapitalizes the first letter ofs. The reverse makeover.val lax_modname_from_source : filename -> modnameβlax_modname_from_source filenameismodulize stem, wherestemis the basename of thefilenamefilenamestripped from all its extensions. For instance,lax_modname_from_source "/pa.th/x.ml.pp"is"X". She strips everything, periodt.val strict_modname_from_source : filename -> modnameβ same asUnit_info.lax_modname_from_source, but raises anUnit_info.error.Invalid_encodingerror onfilenamewith invalid utf8 encoding. Strict queen, no exceptions β well, except the exception. π
πΈ Module name validation function πΈ
val is_unit_name : modname -> boolβis_unit_name nameistrueonly ifnamecan be used as a valid module name. It's the vibe check for module names.
π§ Metadata for compilation unit π§
type t
Metadata for a compilation unit:
- the module name associated to the unit
- the filename prefix (dirname + basename with all extensions stripped) for compilation artifacts
- the input source file
For instance, when calling ocamlopt dir/x.mli -o target/y.cmi,
- the input source file is
dir/x.mli - the module name is
Y - the prefix is
target/y val source_file : t -> filenameβsource_file uis the source file ofu. The origin story.val prefix : t -> file_prefixβprefix uis the filename prefix of the unit. The path before the drama.val modname : t -> modnameβmodname uorartifact_modname ais the module name of the unit or compilation artifact. Name reveal!val kind : t -> intf_or_implβkind uis the kind (interface or implementation) of the unit. Is it anIntfgirlie or anImplgirlie? πval check_unit_name : t -> unitβcheck_unit_name uprints a warning if the derived module namemodname ushould not be used as a module name as specified byUnit_info.is_unit_name ~strict:true. It's the friendly warning bestie.val make : ?check_modname:bool -> source_file:filename -> intf_or_impl -> file_prefix -> t
Signature in code block, because it's long and iconic:
val make : ?check_modname:bool -> source_file:filename -> intf_or_impl
-> file_prefix -> t
make ~check ~source_file kind prefix associates both the source_file and the module name Unit_info.lax_modname_from_source target_prefix to the prefix filesystem path prefix.
If check_modname=true, this function emits a warning if the derived module name is not valid according to Unit_info.check_unit_name. Considerate and observant.
module Artifact : sig end
π₯ Build artifacts π₯
π Derived build artifact metadata π
Those functions derive a specific artifact metadata from an unit metadata. It's giving "one unit, many artifact eras."
val cmi : t -> Artifact.tval cmo : t -> Artifact.tval cmx : t -> Artifact.tval obj : t -> Artifact.tval cmt : t -> Artifact.tval cmti : t -> Artifact.tval annot : t -> Artifact.tval companion_obj : Artifact.t -> Artifact.t
The functions below change the type of an artifact by updating the extension of its filename. Those functions purposefully do not cover all artifact kinds because we want to track which artifacts are assumed to be bundled together. iykyk.
val companion_cmt : Artifact.t -> Artifact.tval companion_cmi : Artifact.t -> Artifact.t
Beware thatcompanion_cmi astrips all extensions from the filename ofabefore adding the".cmi"suffix, contrarily to the other functions which only remove the rightmost extension. In other words, the companion cmi of a filesomething.d.cmoissomething.cmiand notsomething.d.cmi. This is a read-the-docs moment, bestie β do not skip. π«Ά
π Mli and cmi derived from implementation files π
The compilation of module implementation changes in presence of mli and cmi files β the function below help to handle this. Considerate little helpers.
val mli_from_source : t -> filenameβmli_from_source uis the interface source filename associated to the unitu. The actual suffix depends onConfig.interface_suffix. Config decides the accessory, basically.val mli_from_artifact : Artifact.t -> filenameβmli_from_artifact tis the name of the interface source file derived from the artifactt. This variant is necessary when handling artifacts derived from unknown source files (e.g. packed modules). Mystery source? She still finds the mli.val is_cmi : Artifact.t -> boolβ check if the artifact is a cmi. Simple vibe check.val find_normalized_cmi : t -> Artifact.tβfind_normalized_cmi ufinds in theload_patha file matching the module namemodname u.
Raises Not_found if no such cmi exists. And if it's not there, it's not there. π
This page was brought to you by OCamldoc, dated 2026-06-22, and it's the Unit_info(3) manpage. Bye bestie β go derive those module names and slay! π
β¨π
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms