ocamldep
Compute OCaml compilation dependencies
SYNOPSIS
ocamldep [options] source-files
PARAMETERS
-absname
Show absolute filenames in error messages.
-I directory
Add directory to the list of directories to search for included files.
-ml-synonym .ext
Treat files ending with .ext as OCaml implementations rather than interface files
-mli-synonym .ext
Treat files ending with .ext as OCaml interfaces rather than implementation files
-modules
Output a list of module names defined in the input files, rather than dependency rules.
-native
Generate dependencies for native compilation (.cmx files).
-open module
Implicitly open the given module before processing the input files
-pp command
Pipe sources through preprocessor command.
-slash
Use forward slash (/) instead of backslash (\) in file paths.
source-files
One or more OCaml source files (.ml or .mli) to analyze.
DESCRIPTION
The `ocamldep` command analyzes OCaml source files (.ml and .mli) and generates a dependency graph. This graph is used by build systems like `make` to determine the correct order in which to compile files, ensuring that modules are compiled before any modules that depend on them. It considers module interfaces (.mli files) to identify inter-module dependencies, providing a way to build projects efficiently and correctly. `ocamldep` outputs its dependency information suitable for consumption by `make` or other build tools. The information generated includes object files (.cmo and .cmx) dependencies and dependencies between modules based on the modules' interfaces. It identifies which modules must be recompiled when a given interface is modified.
CAVEATS
Handling complex preprocessor directives can sometimes be challenging for `ocamldep`, potentially leading to inaccurate dependency information. Ensure that the preprocessor output is consistent and resolvable. The order of `-I` flags can affect search paths.
UNDERSTANDING DEPENDENCY OUTPUT
The typical output format of `ocamldep` is a set of `make` rules. Each rule specifies a target (e.g., a .cmo or .cmx file) and its dependencies. For example:
file.cmo: file.ml file.mli
This line indicates that `file.cmo` depends on `file.ml` and `file.mli`. If either of these source files changes, `file.cmo` needs to be rebuilt.
INTEGRATING WITH BUILD SYSTEMS
`ocamldep` is usually invoked from within a build system like `make` or `dune`. The output is piped into `make` to automatically generate the dependency graph. Build systems handle the execution of ocamldep. A `Makefile` often contains lines like:
DEPEND = ocamldep *.ml *.mli
%.cmo: %.ml %.mli
$(OCAMLC) -c $<
HISTORY
The `ocamldep` command has been a part of the OCaml toolchain for a long time, evolving alongside the OCaml language and its compilation process. Its core functionality of dependency analysis has been crucial for managing OCaml projects, ensuring proper compilation order and efficient rebuilding. Early versions focused primarily on generating dependencies for bytecode compilation, while later versions added support for native code compilation and more sophisticated dependency tracking.