Module type Signatures.PLUGIN
This module contains the functions and values that can be used by plugins.
module StringSet : Stdlib.Set.S with type elt = String.tmodule Options : OPTIONS with type command_spec = Command.specmodule Findlib : FINDLIB with type command_spec = Command.specinclude MISC
val opt_print : (Stdlib.Format.formatter -> 'a -> unit) -> Stdlib.Format.formatter -> 'a option -> unitval the : 'a option -> 'aval getenv : ?default:string -> string -> stringval with_input_file : ?bin:bool -> string -> (Stdlib.in_channel -> 'a) -> 'aval with_output_file : ?bin:bool -> string -> (Stdlib.out_channel -> 'a) -> 'aval with_temp_file : string -> string -> (string -> 'a) -> 'aval read_file : string -> stringval copy_chan : Stdlib.in_channel -> Stdlib.out_channel -> unitval copy_file : string -> string -> unitval print_string_list : Stdlib.Format.formatter -> string list -> unitval (!*) : 'a Stdlib.Lazy.t -> 'aA shortcut to force lazy value (See
Lazy.force).
val (&) : ('a -> 'b) -> 'a -> 'bThe right associative application. Useful when writing to much parentheses: << f (g x ... t) >> becomes << f& g x ... t >> << f (g (h x)) >> becomes << f& g& h x >>
val (|>) : 'a -> ('a -> 'b) -> 'bThe reversed application combinator. Useful to describe some operations chaining. << f x (g y (h z)) >> becomes << z |> h |> g y |> f x >>
val (@:=) : 'a list Stdlib.ref -> 'a list -> unitr @:= lis equivalent tor := !r @ l
type command= Command.t=|Seq of command list|Cmd of spec|Echo of string list * Pathname.t|NopSee
COMMAND.tfor the description of this type.
and spec= Command.spec=|N|S of spec list|A of string|P of string|Px of string|Sh of string|T of Tags.t|V of string|Quote of specSee
COMMAND.specfor the description of this type.
val (/) : Pathname.t -> Pathname.t -> Pathname.tpath1/path2Join the given path names.
val (-.-) : Pathname.t -> string -> Pathname.tpath-.-extensionAdd the given extension to the given pathname.
val (+++) : Tags.t -> Tags.elt option -> Tags.ttags+++optional_tagAdd the given optional tag to the given set of tags if the given option is Some.
val (---) : Tags.t -> Tags.elt option -> Tags.ttags---optional_tagRemove the given optional tag to the given set of tags if the given option is Some.
type env= Pathname.t -> Pathname.tTargets and dependencies in rules are patterns, which are matched by the actual targets requested by the user. For example, if a rule explains how to build "%.cmo" from "%.ml", it will be fired if the user tries to build "foo.cmo".
The environment records the value of the pattern variables (here "%") in this matching. In the context of our example, the environment will be a function substituting "%" by "foo"; calling it on "%.cmo" will return "foo.cmo", and calling it on "%.ml" will return "foo.ml".
For a typical example of use of the environment, see the
ocamldep_ml_commandexample in the documentation ofactionbelow.
type builder= Pathname.t list list -> (Pathname.t, exn) Outcome.t listA builder is a function that waits for a conjunction of alternative targets. The alternatives are here to support some choices, for instance for an OCaml module an alternatives can be foo.cmo or Foo.cmo. Conjunctions are here to enable parallelism: commands that are independent will be run concurrently.
For example, passing
["foo.cmi"; "Foo.cmi"]; ["bar.cmi"; "Bar.cmi"]will build the interfaces of the modules Foo and Bar, trying both possible capitalization of the source file names.For an example of use of a builder function, see the
targetsfunction given as an example in the documentation of theactiontype below.
type action= env -> builder -> Command.tThis is the type for rule actions. An action receive as argument, the environment lookup function (see
env), and a function to dynamically build more targets (seebuilder). An action should return the command to run in order to build the rule productions using the rule dependencies.For example, here is an action to build an
ocamldepcommand, for use in the example of rule given in the documentation ofrulebelow:let ocamldep_ml_command env _build = let arg = env "%.ml" and out = env "%.ml.depends" in let tags = tags_of_pathname arg ++ "ocaml" ++ "ocamldep" in Cmd(S[A "ocamldep"; T tags; A "-modules"; P arg; Sh ">"; Px out])In the example above, the build function is not used, as there are no dynamic dependencies. There are in the example below: we build a list of targets dynamically read from a "foo.itarget" file. The final command returned by the action links each of them in the current directory.
let target_list env build = let itarget = env "%.itarget" in let targets = let dir = Pathname.dirname itarget in let files = string_list_of_file itarget in List.map (fun file -> [Pathname.concat dir file]) files in let results = List.map Outcome.good (build targets) in let link_command result = Cmd (S [A "ln"; A "-sf"; P (Pathname.concat !Options.build_dir result); A Pathname.pwd]) in Seq (List.map link_command results)
val rule : string -> ?tags:string list -> ?prods:string list -> ?deps:string list -> ?prod:string -> ?dep:string -> ?stamp:string -> ?insert:[ `top | `before of string | `after of string | `bottom ] -> ?doc:string -> action -> unitThis is the main function for adding a rule to the ocamlbuild engine.
- The first argument is the name of the rule (should be unique).
- It takes files that the rule produces. Use
~prodfor one file,~prodsfor list of files. - It also takes files that the rule uses. Use
~depfor one file,~depsfor list of files. - It finally takes the action to perform in order to produce the productions files using the dependencies (see
action).
There are some more optional parameters:
- The
~insertargument allows to insert the rules precisely between other rules. - The
~stampargument specifies the name of a file that will be automatically produced by ocamlbuild. This file can serve as a virtual target (or phony target), since it will be filled up by a digest of it dependencies. - The
~tagsargument in deprecated, don't use it.
Finally, the optional
~docargument allows to give an informal explanation of the rule purpose and behavior, that will be displayed byocamlbuild -documentation. For example, it is a good place to specify the commands that will be called, any new tags introduced by the rule, and dynamic dependencies.For example, here is how a rule producing
foo.ml.dependsfromfoo.ml(callingocamldep) is defined, slightly simplified from the built-in definition.rule "ocaml dependencies ml" ~prod:"%.ml.depends" ~dep:"%.ml" ~doc:"call ocamldep to compute a syntactic over-approximation \\ of the dependencies of the corresponding implementation file" ocamldep_ml_commandThe rule that builds a list of targets from a
%.itargetfile is an example of use of the?stampargument. It uses thetarget_listaction provided in the documentation ofactionabove. Besides the targets listed in the%.itargetfile, this command will also produce a%.otargetfile (the "stamp") that contains the digest of all the%.itargetdependencies. This stamp file is the name to use when you want to ask OCamlbuild to build the targets listed: invokingocamlbuild foo.otargetwill build the the targets listed infoo.itarget. Similarly, new rules that would depend on that list of targets should depend on%.otarget(output), not%.itarget(input).rule "target files" ~dep:"%.itarget" ~stamp:"%.otarget" ~doc:"If foo.itarget contains a list of ocamlbuild targets, \ asking ocamlbuild to produce foo.otarget will \ build each of those targets in turn." target_list
val copy_rule : string -> ?insert:[ `top | `before of string | `after of string | `bottom ] -> string -> string -> unitcopy_rule name ?insert source destination
val dep : Tags.elt list -> Pathname.t list -> unitdep tags depsWill builddepswhen alltagswill be activated. If you do not know which tags to use, have a look to the file _build/_log after trying to compile your code.
val pdep : Tags.elt list -> Tags.elt -> (string -> Pathname.t list) -> unitpdep tags ptag depsis equivalent todep tags deps, with an additional parametrized tagptag.depsis now a function which takes the parameter of the tagptagas an argument.Example:
pdep ["ocaml"; "compile"] "autodep" (fun param -> param)says that the tagautodep(file)can now be used to automatically addfileas a dependency when compiling an OCaml program.
val flag : Tags.elt list -> Command.spec -> unitflag tags command_specWill inject the given piece of command (command_spec) when alltagswill be activated. If you do not know which tags to use, have a look to the file _build/_log after trying to compile your code.
val pflag : Tags.elt list -> Tags.elt -> (string -> Command.spec) -> unitAllows to use
flagwith a parametrized tag (aspdepfordep).Example:
pflag ["ocaml"; "compile"] "inline" (fun count -> S [A "-inline"; A count])says that command line option"-inline 42"should be added when compiling OCaml modules tagged with"inline(42)".
val flag_and_dep : Tags.elt list -> Command.spec -> unitflag_and_dep tags command_specCombinesflaganddepfunction. Basically it callsflag tags command_spec, and callsdep tags fileswherefilesis the list of all pathnames incommand_spec. Pathnames selected are those in the constructorPorPx, or the pathname argument of builtins likeEcho.
val pflag_and_dep : Tags.elt list -> Tags.elt -> (string -> Command.spec) -> unitAllows to use
flag_and_depwith a parametrized tag (aspdepfordep).
val mark_tag_used : Tags.elt -> unitmanually mark the tag as "useful" to silence the warning about tags that are not part of any flag declaration.
This is useful, for example, if the tag is used in a flag declaration that is only performed in a conditional branch:
if we_are_on_Windows then flag ["libfoo"] (A "bar");When
we_are_on_Windowsis not true, you could get a warning about "libfoo" not used in any flag declaration.
val non_dependency : Pathname.t -> string -> unitnon_dependency module_path module_nameExample:non_dependency "foo/bar/baz" "Goo"Says that the moduleBazin the filefoo/bar/baz.*does not depend onGoo.
val use_lib : Pathname.t -> Pathname.t -> unituse_lib module_path lib_path
val ocaml_lib : ?extern:bool -> ?byte:bool -> ?native:bool -> ?dir:Pathname.t -> ?tag_name:string -> Pathname.t -> unitocaml_lib <options> library_pathnameDeclare an ocaml library. This informs ocamlbuild and produce tags to use the library; they are named by default use_#brary_name
}
.
Example:
ocaml_lib "foo/bar"will setup the tag use_bar. At link time it will include: foo/bar.cma or foo/bar.cmxa- parameter dir
supply the
~dir:"boo"option to add '-I boo' at link and compile time.
- parameter extern
use ~extern:true for non-ocamlbuild handled libraries. Set this to add libraries whose sources are not in your project.
- parameter byte
use ~byte:false to disable byte mode.
- parameter native
use ~native:false to disable native mode.
- parameter tag_name
Use ~tag_name:"usebar" to override the default tag name.
val expand_module : Pathname.t list -> Pathname.t -> string list -> Pathname.t listexpand_module include_dirs module_name extensionsExample:expand_module ["a";"b";"c"] "Foo" ["cmo";"cmi"] = ["a/foo.cmo"; "a/Foo.cmo"; "a/foo.cmi"; "a/Foo.cmi"; "b/foo.cmo"; "b/Foo.cmo"; "b/foo.cmi"; "b/Foo.cmi"; "c/foo.cmo"; "c/Foo.cmo"; "c/foo.cmi"; "c/Foo.cmi"]
val string_list_of_file : Pathname.t -> string listReads the given file, parse it has list of words separated by blanks. It ignore lines that begins with a '#' character.
val module_name_of_pathname : Pathname.t -> stringTakes a pathname and returns an OCaml module name. Basically it will remove directories and extensions, and then capitalize the string.
val mv : Pathname.t -> Pathname.t -> Command.tThe Unix mv command.
val cp : Pathname.t -> Pathname.t -> Command.tThe Unix cp command.
val ln_f : Pathname.t -> Pathname.t -> Command.tThe Unix ln -f command.
val ln_s : Pathname.t -> Pathname.t -> Command.tThe Unix ln -s command.
val rm_f : Pathname.t -> Command.tThe Unix rm -f command.
val chmod : Command.spec -> Pathname.t -> Command.tThe Unix chmod command (almost deprecated).
val cmp : Pathname.t -> Pathname.t -> Command.tThe Unix cmp command (almost deprecated).
val hide_package_contents : string -> unithide_package_contents pack_nameDon't treat the given package as an open package. So a module will not be replaced during linking by this package even if it contains that module.
val tag_file : Pathname.t -> Tags.elt list -> unittag_file filename tag_listTag the given filename with all given tags. Prefix a tag with the minus sign to remove it. This is usually used as anAfter_ruleshook. For exampletag_file "bla.ml" ["use_unix"]tags the file "bla.ml" with "use_unix" andtag_file "bla.ml" ["-use_unix"]removes the tag "use_unix" from the file "bla.ml".
val tag_any : Tags.elt list -> unittag_any tag_listTag anything with all given tags.
val tags_of_pathname : Pathname.t -> Tags.tReturns the set of tags that applies to the given pathname.
type hook=|Before_hygiene|After_hygiene|Before_options|After_options|Before_rules|After_rulesHere is the list of hooks that the dispatch function have to handle. Generally one responds to one or two hooks (like After_rules) and do nothing in the default case.
val dispatch : (hook -> unit) -> unitdispatch hook_handlerIs the entry point for ocamlbuild plugins. Every plugin must call it with ahook_handlerwhere all calls to plugin functions lives.