ocamldep
Compute OCaml compilation dependencies
SYNOPSIS
ocamldep [options]
PARAMETERS
-I
Adds
-modules
Outputs a list of module names imported by the specified source files, one module name per line. This is different from the default Makefile-style output.
-impl
Treats subsequent arguments as implementation files (.ml, .mll, .mly), overriding the default extension-based detection.
-intf
Treats subsequent arguments as interface files (.mli), overriding the default extension-based detection.
-raw
Prints raw dependencies without adding suffixes (.d, .cmo, etc.) or header comments. Useful for custom parsing.
-resolve-path
Resolves file paths to their canonical form.
-files
Outputs the list of source files passed to ocamldep itself, one file per line.
-pp
Pipes source files through the specified preprocessor
-no-all-deps
Prevents ocamldep from adding a dependency on the pseudo-target all (e.g., all: <target>).
-sort
Sorts the generated dependencies lexicographically.
-version
Prints the ocamldep version number and exits.
-v
Prints verbose information during execution, including progress messages.
-help or --help
Displays a summary of the command's options and exits.
-ml-synonym
Treats files with
-as-ml
Treats
-extension
Adds
DESCRIPTION
ocamldep is a utility distributed with the OCaml compiler. Its primary function is to analyze OCaml source files (including .ml, .mli, .mll, and .mly files) and automatically generate dependency rules suitable for inclusion in Makefiles.
By examining open directives, module interfaces, and implicit module references, ocamldep determines the correct compilation order of OCaml modules. This automation is crucial for large OCaml projects, as it ensures that when a module's interface changes, all dependent modules are correctly recompiled, preventing stale binaries and simplifying the build process. It outputs rules in a format compatible with make, typically target: dependencies lines, which can be directly included in a project's Makefile.
CAVEATS
ocamldep primarily relies on the textual analysis of OCaml source files. It does not inspect compiled interface files (.cmi) directly for dependency resolution, but rather uses the -I paths to locate sources when resolving module names. While it parses source files, complex build setups involving exotic preprocessors or compiler-specific pragmas might require careful handling or specific -pp options to ensure accurate dependency generation. It's designed for simple module dependencies, not for tracking changes in C stubs or external libraries, which must be handled by the broader build system.
TYPICAL MAKEFILE INTEGRATION
ocamldep is most commonly integrated into Makefiles to automatically generate and update dependency rules. A typical setup involves defining a rule to generate .d files (dependency files) from OCaml source files (.ml, .mli). These generated .d files are then included into the main Makefile, allowing make to automatically track changes and recompile only what's necessary. This significantly reduces manual dependency management and ensures build correctness. For example:
%.d: %.ml
ocamldep $< > $@
%.d: %.mli
ocamldep $< > $@
include $(wildcard *.d)
HISTORY
ocamldep has been a fundamental component of the OCaml distribution from its early days, evolving alongside the OCaml language and compiler. Its inclusion reflects the need for robust dependency management in compiled languages, making it an indispensable tool for automating OCaml project builds using systems like make. Its design aligns with the modular nature of OCaml, simplifying the often complex task of maintaining correct build orders in a highly interconnected codebase.