Change the rule signature
Now rules are arrows of type: (unit, Action.t) Build.t They don't execute command directly, but instead build a serializable action to execute.
This commit is contained in:
parent
2967987356
commit
adf423a595
312
src/action.ml
312
src/action.ml
|
@ -1,62 +1,93 @@
|
|||
open Import
|
||||
open Sexp.Of_sexp
|
||||
|
||||
type var_expansion =
|
||||
| Not_found
|
||||
| Path of Path.t
|
||||
| Paths of Path.t list
|
||||
| Str of string
|
||||
|
||||
let expand_str ~dir ~f template =
|
||||
String_with_vars.expand template ~f:(fun var ->
|
||||
match f var with
|
||||
| Not_found -> None
|
||||
| Path path -> Some (Path.reach ~from:dir path)
|
||||
| Paths l -> Some (List.map l ~f:(Path.reach ~from:dir) |> String.concat ~sep:" ")
|
||||
| Str s -> Some s)
|
||||
|
||||
let expand_path ~dir ~f template =
|
||||
match String_with_vars.just_a_var template with
|
||||
| None -> expand_str ~dir ~f template |> Path.relative dir
|
||||
| Some v ->
|
||||
match f v with
|
||||
| Not_found -> expand_str ~dir ~f template |> Path.relative dir
|
||||
| Path p -> p
|
||||
| Str s -> Path.relative dir s
|
||||
| Paths l ->
|
||||
List.map l ~f:(Path.reach ~from:dir)
|
||||
|> String.concat ~sep:" "
|
||||
|> Path.of_string
|
||||
|
||||
module Mini_shexp = struct
|
||||
type 'a t =
|
||||
| Run of 'a * 'a list
|
||||
| Chdir of 'a * 'a t
|
||||
| Setenv of 'a * 'a * 'a t
|
||||
| With_stdout_to of 'a * 'a t
|
||||
| Progn of 'a t list
|
||||
type ('a, 'path) t =
|
||||
| Run of 'path * 'a list
|
||||
| Chdir of 'path * ('a, 'path) t
|
||||
| Setenv of 'a * 'a * ('a, 'path) t
|
||||
| With_stdout_to of 'path * ('a, 'path) t
|
||||
| Progn of ('a, 'path) t list
|
||||
| Echo of 'a
|
||||
| Cat of 'a
|
||||
| Copy of 'a * 'a
|
||||
| Symlink of 'a * 'a
|
||||
| Copy_and_add_line_directive of 'a * 'a
|
||||
| Create_file of 'path
|
||||
| Cat of 'path
|
||||
| Copy of 'path * 'path
|
||||
| Symlink of 'path * 'path
|
||||
| Copy_and_add_line_directive of 'path * 'path
|
||||
| System of 'a
|
||||
|
||||
let rec t a sexp =
|
||||
let rec t a p sexp =
|
||||
sum
|
||||
[ cstr_rest "run" (a @> nil) a (fun prog args -> Run (prog, args))
|
||||
; cstr "chdir" (a @> t a @> nil) (fun dn t -> Chdir (dn, t))
|
||||
; cstr "setenv" (a @> a @> t a @> nil) (fun k v t -> Setenv (k, v, t))
|
||||
; cstr "with-stdout-to" (a @> t a @> nil) (fun fn t -> With_stdout_to (fn, t))
|
||||
; cstr_rest "progn" nil (t a) (fun l -> Progn l)
|
||||
[ cstr_rest "run" (p @> nil) a (fun prog args -> Run (prog, args))
|
||||
; cstr "chdir" (p @> t a p @> nil) (fun dn t -> Chdir (dn, t))
|
||||
; cstr "setenv" (a @> a @> t a p @> nil) (fun k v t -> Setenv (k, v, t))
|
||||
; cstr "with-stdout-to" (p @> t a p @> nil) (fun fn t -> With_stdout_to (fn, t))
|
||||
; cstr_rest "progn" nil (t a p) (fun l -> Progn l)
|
||||
; cstr "echo" (a @> nil) (fun x -> Echo x)
|
||||
; cstr "cat" (a @> nil) (fun x -> Cat x)
|
||||
; cstr "copy" (a @> a @> nil) (fun src dst -> Copy (src, dst))
|
||||
; cstr "cat" (p @> nil) (fun x -> Cat x)
|
||||
; cstr "create-file" (p @> nil) (fun x -> Create_file x)
|
||||
; cstr "copy" (p @> p @> nil) (fun src dst -> Copy (src, dst))
|
||||
(*
|
||||
(* We don't expose symlink to the user yet since this might complicate things *)
|
||||
; cstr "symlink" (a @> a @> nil) (fun src dst -> Symlink (dst, Cat src))
|
||||
*)
|
||||
; cstr "copy-and-add-line-directive" (a @> a @> nil) (fun src dst ->
|
||||
; cstr "copy-and-add-line-directive" (p @> p @> nil) (fun src dst ->
|
||||
Copy_and_add_line_directive (src, dst))
|
||||
; cstr "system" (a @> nil) (fun cmd -> System cmd)
|
||||
]
|
||||
sexp
|
||||
|
||||
let rec expand dir t ~f =
|
||||
let rec expand dir t ~f : (string, Path.t) t =
|
||||
match t with
|
||||
| Run (prog, args) ->
|
||||
Run (f dir prog,
|
||||
List.map args ~f:(fun arg -> f dir arg))
|
||||
Run (expand_path ~dir ~f prog,
|
||||
List.map args ~f:(fun arg -> expand_str ~dir ~f arg))
|
||||
| Chdir (fn, t) ->
|
||||
let fn = f dir fn in
|
||||
Chdir (fn, expand (Path.relative dir fn) t ~f)
|
||||
let fn = expand_path ~dir ~f fn in
|
||||
Chdir (fn, expand fn t ~f)
|
||||
| Setenv (var, value, t) ->
|
||||
Setenv (f dir var, f dir value, expand dir t ~f)
|
||||
Setenv (expand_str ~dir ~f var, expand_str ~dir ~f value,
|
||||
expand dir t ~f)
|
||||
| With_stdout_to (fn, t) ->
|
||||
With_stdout_to (f dir fn, expand dir t ~f)
|
||||
With_stdout_to (expand_path ~dir ~f fn, expand dir t ~f)
|
||||
| Progn l -> Progn (List.map l ~f:(fun t -> expand dir t ~f))
|
||||
| Echo x -> Echo (f dir x)
|
||||
| Cat x -> Cat (f dir x)
|
||||
| Echo x -> Echo (expand_str ~dir ~f x)
|
||||
| Cat x -> Cat (expand_path ~dir ~f x)
|
||||
| Create_file x -> Create_file (expand_path ~dir ~f x)
|
||||
| Copy (x, y) ->
|
||||
Copy (f dir x, f dir y)
|
||||
Copy (expand_path ~dir ~f x, expand_path ~dir ~f y)
|
||||
| Symlink (x, y) ->
|
||||
Symlink (f dir x, f dir y)
|
||||
Symlink (expand_path ~dir ~f x, expand_path ~dir ~f y)
|
||||
| Copy_and_add_line_directive (x, y) ->
|
||||
Copy_and_add_line_directive (f dir x, f dir y)
|
||||
| System x -> System (f dir x)
|
||||
Copy_and_add_line_directive (expand_path ~dir ~f x, expand_path ~dir ~f y)
|
||||
| System x -> System (expand_str ~dir ~f x)
|
||||
|
||||
let rec fold t ~init:acc ~f =
|
||||
match t with
|
||||
|
@ -67,71 +98,218 @@ module Mini_shexp = struct
|
|||
| Progn l -> List.fold_left l ~init:acc ~f:(fun init t -> fold t ~init ~f)
|
||||
| Echo x -> f acc x
|
||||
| Cat x -> f acc x
|
||||
| Create_file x -> f acc x
|
||||
| Copy (x, y) -> f (f acc x) y
|
||||
| Symlink (x, y) -> f (f acc x) y
|
||||
| Copy_and_add_line_directive (x, y) -> f (f acc x) y
|
||||
| System x -> f acc x
|
||||
|
||||
let rec sexp_of_t f : _ -> Sexp.t = function
|
||||
| Run (a, xs) -> List (Atom "run" :: f a :: List.map xs ~f)
|
||||
| Chdir (a, r) -> List [Atom "chdir" ; f a ; sexp_of_t f r]
|
||||
| Setenv (k, v, r) -> List [Atom "setenv" ; f k ; f v ; sexp_of_t f r]
|
||||
| With_stdout_to (fn, r) -> List [Atom "with-stdout-to"; f fn; sexp_of_t f r]
|
||||
| Progn l -> List (Atom "progn" :: List.map l ~f:(sexp_of_t f))
|
||||
let rec sexp_of_t f g : _ -> Sexp.t = function
|
||||
| Run (a, xs) -> List (Atom "run" :: g a :: List.map xs ~f)
|
||||
| Chdir (a, r) -> List [Atom "chdir" ; g a ; sexp_of_t f g r]
|
||||
| Setenv (k, v, r) -> List [Atom "setenv" ; f k ; f v ; sexp_of_t f g r]
|
||||
| With_stdout_to (fn, r) -> List [Atom "with-stdout-to"; g fn; sexp_of_t f g r]
|
||||
| Progn l -> List (Atom "progn" :: List.map l ~f:(sexp_of_t f g))
|
||||
| Echo x -> List [Atom "echo"; f x]
|
||||
| Cat x -> List [Atom "cat"; f x]
|
||||
| Cat x -> List [Atom "cat"; g x]
|
||||
| Create_file x -> List [Atom "create-file"; g x]
|
||||
| Copy (x, y) ->
|
||||
List [Atom "copy"; f x; f y]
|
||||
List [Atom "copy"; g x; g y]
|
||||
| Symlink (x, y) ->
|
||||
List [Atom "symlink"; f x; f y]
|
||||
List [Atom "symlink"; g x; g y]
|
||||
| Copy_and_add_line_directive (x, y) ->
|
||||
List [Atom "copy-and-add-line-directive"; f x; f y]
|
||||
List [Atom "copy-and-add-line-directive"; g x; g y]
|
||||
| System x -> List [Atom "system"; f x]
|
||||
|
||||
open Future
|
||||
|
||||
let run ~dir ~env ~env_extra ~stdout_to ~tail prog args =
|
||||
let stdout_to : Future.stdout_to =
|
||||
match stdout_to with
|
||||
| None -> Terminal
|
||||
| Some (fn, oc) -> Opened_file { filename = fn; tail; desc = Channel oc }
|
||||
in
|
||||
let env = Context.extend_env ~vars:env_extra ~env in
|
||||
Future.run Strict ~dir:(Path.to_string dir) ~env ~stdout_to
|
||||
(Path.reach ~from:dir prog) args
|
||||
|
||||
let rec exec t ~dir ~env ~env_extra ~stdout_to ~tail =
|
||||
match t with
|
||||
| Run (prog, args) ->
|
||||
run ~dir ~env ~env_extra ~stdout_to ~tail prog args
|
||||
| Chdir (dir, t) ->
|
||||
exec t ~env ~env_extra ~stdout_to ~tail ~dir
|
||||
| Setenv (var, value, t) ->
|
||||
exec t ~dir ~env ~stdout_to ~tail
|
||||
~env_extra:(String_map.add env_extra ~key:var ~data:value)
|
||||
| With_stdout_to (fn, t) ->
|
||||
if tail then Option.iter stdout_to ~f:(fun (_, oc) -> close_out oc);
|
||||
let fn = Path.to_string fn in
|
||||
exec t ~dir ~env ~env_extra ~tail
|
||||
~stdout_to:(Some (fn, open_out_bin fn))
|
||||
| Progn l ->
|
||||
exec_list l ~dir ~env ~env_extra ~stdout_to ~tail
|
||||
| Echo str ->
|
||||
return
|
||||
(match stdout_to with
|
||||
| None -> print_string str; flush stdout
|
||||
| Some (_, oc) ->
|
||||
output_string oc str;
|
||||
if tail then close_out oc)
|
||||
| Cat fn ->
|
||||
with_file_in (Path.to_string fn) ~f:(fun ic ->
|
||||
match stdout_to with
|
||||
| None -> copy_channels ic stdout
|
||||
| Some (_, oc) ->
|
||||
copy_channels ic oc;
|
||||
if tail then close_out oc);
|
||||
return ()
|
||||
| Create_file fn ->
|
||||
let fn = Path.to_string fn in
|
||||
if Sys.file_exists fn then Sys.remove fn;
|
||||
Unix.close (Unix.openfile fn [O_CREAT; O_TRUNC; O_WRONLY] 0o666);
|
||||
return ()
|
||||
| Copy (src, dst) ->
|
||||
copy_file ~src:(Path.to_string src) ~dst:(Path.to_string dst);
|
||||
return ()
|
||||
| Symlink (src, dst) ->
|
||||
if Sys.win32 then
|
||||
copy_file ~src:(Path.to_string src) ~dst:(Path.to_string dst)
|
||||
else begin
|
||||
let src =
|
||||
if Path.is_root dst then
|
||||
Path.to_string src
|
||||
else
|
||||
Path.reach ~from:(Path.parent dst) src
|
||||
in
|
||||
let dst = Path.to_string dst in
|
||||
match Unix.readlink dst with
|
||||
| target ->
|
||||
if target <> src then begin
|
||||
Unix.unlink dst;
|
||||
Unix.symlink src dst
|
||||
end
|
||||
| exception _ ->
|
||||
Unix.symlink src dst
|
||||
end;
|
||||
return ()
|
||||
| Copy_and_add_line_directive (src, dst) ->
|
||||
with_file_in (Path.to_string src) ~f:(fun ic ->
|
||||
with_file_out (Path.to_string dst) ~f:(fun oc ->
|
||||
let fn =
|
||||
match Path.extract_build_context src with
|
||||
| None -> src
|
||||
| Some (_, rem) -> rem
|
||||
in
|
||||
Printf.fprintf oc "# 1 %S\n" (Path.to_string fn);
|
||||
copy_channels ic oc));
|
||||
return ()
|
||||
| System cmd ->
|
||||
let path, arg, err =
|
||||
Utils.system_shell ~needed_to:"interpret (system ...) actions"
|
||||
in
|
||||
match err with
|
||||
| Some err -> err.fail ()
|
||||
| None ->
|
||||
run ~dir ~env ~env_extra ~stdout_to ~tail path [arg; cmd]
|
||||
|
||||
and exec_list l ~dir ~env ~env_extra ~stdout_to ~tail =
|
||||
match l with
|
||||
| [] ->
|
||||
if tail then Option.iter stdout_to ~f:(fun (_, oc) -> close_out oc);
|
||||
Future.return ()
|
||||
| [t] ->
|
||||
exec t ~dir ~env ~env_extra ~stdout_to ~tail
|
||||
| t :: rest ->
|
||||
exec t ~dir ~env ~env_extra ~stdout_to ~tail:false >>= fun () ->
|
||||
exec_list rest ~dir ~env ~env_extra ~stdout_to ~tail
|
||||
end
|
||||
|
||||
module Desc = struct
|
||||
module T = struct
|
||||
type 'a t =
|
||||
module Ast = struct
|
||||
type ('a, 'path) t =
|
||||
| Bash of 'a
|
||||
| Shexp of 'a Mini_shexp.t
|
||||
| Shexp of ('a, 'path) Mini_shexp.t
|
||||
|
||||
let t a sexp =
|
||||
let t a b sexp =
|
||||
match sexp with
|
||||
| Atom _ -> Bash (a sexp)
|
||||
| List _ -> Shexp (Mini_shexp.t a sexp)
|
||||
| List _ -> Shexp (Mini_shexp.t a b sexp)
|
||||
|
||||
type context = Path.t
|
||||
|
||||
let expand dir t ~f =
|
||||
match t with
|
||||
| Bash x -> Bash (f dir x)
|
||||
| Shexp x -> Shexp (Mini_shexp.expand dir x ~f)
|
||||
let sexp_of_t f g : _ -> Sexp.t = function
|
||||
| Bash a -> List [Atom "bash" ; f a]
|
||||
| Shexp a -> List [Atom "shexp" ; Mini_shexp.sexp_of_t f g a]
|
||||
|
||||
let fold t ~init ~f =
|
||||
match t with
|
||||
| Bash x -> f init x
|
||||
| Shexp x -> Mini_shexp.fold x ~init ~f
|
||||
|
||||
let sexp_of_t f : _ -> Sexp.t = function
|
||||
| Bash a -> List [Atom "bash" ; f a]
|
||||
| Shexp a -> List [Atom "shexp" ; Mini_shexp.sexp_of_t f a]
|
||||
end
|
||||
|
||||
include T
|
||||
type t = (string, Path.t) Ast.t
|
||||
let t = Ast.t string Path.t
|
||||
let sexp_of_t = Ast.sexp_of_t Sexp.To_sexp.string Path.sexp_of_t
|
||||
|
||||
module Unexpanded = String_with_vars.Lift(T)
|
||||
module Unexpanded = struct
|
||||
type t = (String_with_vars.t, String_with_vars.t) Ast.t
|
||||
let t = Ast.t String_with_vars.t String_with_vars.t
|
||||
let sexp_of_t = Ast.sexp_of_t String_with_vars.sexp_of_t String_with_vars.sexp_of_t
|
||||
|
||||
let fold_vars t ~init ~f =
|
||||
Ast.fold t ~init ~f:(fun acc pat ->
|
||||
String_with_vars.fold ~init:acc pat ~f)
|
||||
|
||||
let expand dir (t : t) ~f : (_, _) Ast.t =
|
||||
match t with
|
||||
| Bash x -> Bash (expand_str ~dir ~f x)
|
||||
| Shexp x -> Shexp (Mini_shexp.expand dir x ~f)
|
||||
end
|
||||
end
|
||||
|
||||
type t =
|
||||
{ env : string array
|
||||
{ context : Context.t option
|
||||
; dir : Path.t
|
||||
; action : string Desc.t
|
||||
; action : Desc.t
|
||||
}
|
||||
|
||||
let sexp_of_t { env; dir; action } =
|
||||
let open Sexp.To_sexp in
|
||||
Sexp.List
|
||||
[ List [ Atom "env" ; array string env ]
|
||||
; List [ Atom "dir" ; string (Path.to_string dir) ]
|
||||
; List [ Atom "action"; Desc.sexp_of_t string action ]
|
||||
let t contexts sexp =
|
||||
let open Sexp.Of_sexp in
|
||||
let context sexp =
|
||||
let name = string sexp in
|
||||
match String_map.find name contexts with
|
||||
| None -> of_sexp_errorf sexp "Context %s not found" name
|
||||
| Some c -> c
|
||||
in
|
||||
record
|
||||
(field_o "context" context >>= fun context ->
|
||||
field "dir" Path.t >>= fun dir ->
|
||||
field "action" Desc.t >>= fun action ->
|
||||
return { context; dir; action })
|
||||
sexp
|
||||
|
||||
let sexp_of_t { context; dir; action } =
|
||||
let fields : Sexp.t list =
|
||||
[ List [ Atom "dir" ; Path.sexp_of_t dir ]
|
||||
; List [ Atom "action" ; Desc.sexp_of_t action ]
|
||||
]
|
||||
in
|
||||
let fields =
|
||||
match context with
|
||||
| None -> fields
|
||||
| Some { name; _ } -> List [ Atom "context"; Atom name ] :: fields
|
||||
in
|
||||
Sexp.List fields
|
||||
|
||||
let exec { action; dir; context } =
|
||||
let env =
|
||||
match context with
|
||||
| None -> Lazy.force Context.initial_env
|
||||
| Some c -> c.env
|
||||
in
|
||||
match action with
|
||||
| Bash cmd ->
|
||||
Future.run Strict ~dir:(Path.to_string dir) ~env
|
||||
"/bin/bash" ["-e"; "-u"; "-o"; "pipefail"; "-c"; cmd]
|
||||
| Shexp shexp ->
|
||||
Mini_shexp.exec shexp ~dir ~env ~env_extra:String_map.empty
|
||||
~stdout_to:None ~tail:true
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
open! Import
|
||||
|
||||
type var_expansion =
|
||||
| Not_found
|
||||
| Path of Path.t
|
||||
| Paths of Path.t list
|
||||
| Str of string
|
||||
|
||||
module Mini_shexp : sig
|
||||
type ('a, 'path) t =
|
||||
| Run of 'path * 'a list
|
||||
| Chdir of 'path * ('a, 'path) t
|
||||
| Setenv of 'a * 'a * ('a, 'path) t
|
||||
| With_stdout_to of 'path * ('a, 'path) t
|
||||
| Progn of ('a, 'path) t list
|
||||
| Echo of 'a
|
||||
| Create_file of 'path
|
||||
| Cat of 'path
|
||||
| Copy of 'path * 'path
|
||||
| Symlink of 'path * 'path
|
||||
| Copy_and_add_line_directive of 'path * 'path
|
||||
| System of 'a
|
||||
|
||||
val t : 'a Sexp.Of_sexp.t -> 'b Sexp.Of_sexp.t -> ('a, 'b) t Sexp.Of_sexp.t
|
||||
val sexp_of_t : 'a Sexp.To_sexp.t -> 'b Sexp.To_sexp.t -> ('a, 'b) t Sexp.To_sexp.t
|
||||
end
|
||||
|
||||
module Desc : sig
|
||||
module Ast : sig
|
||||
type ('a, 'path) t =
|
||||
| Bash of 'a
|
||||
| Shexp of ('a, 'path) Mini_shexp.t
|
||||
|
||||
val t : 'a Sexp.Of_sexp.t -> 'b Sexp.Of_sexp.t -> ('a, 'b) t Sexp.Of_sexp.t
|
||||
val sexp_of_t : 'a Sexp.To_sexp.t -> 'b Sexp.To_sexp.t -> ('a, 'b) t Sexp.To_sexp.t
|
||||
end
|
||||
|
||||
type t = (string, Path.t) Ast.t
|
||||
val t : t Sexp.Of_sexp.t
|
||||
val sexp_of_t : t Sexp.To_sexp.t
|
||||
|
||||
module Unexpanded : sig
|
||||
type desc = t
|
||||
type t = (String_with_vars.t, String_with_vars.t) Ast.t
|
||||
val t : t Sexp.Of_sexp.t
|
||||
val sexp_of_t : t Sexp.To_sexp.t
|
||||
val fold_vars : t -> init:'a -> f:('a -> string -> 'a) -> 'a
|
||||
val expand : Path.t -> t -> f:(string -> var_expansion) -> desc
|
||||
end with type desc := t
|
||||
end
|
||||
|
||||
type t =
|
||||
{ context : Context.t option
|
||||
; dir : Path.t
|
||||
; action : Desc.t
|
||||
}
|
||||
|
||||
val t : Context.t String_map.t -> t Sexp.Of_sexp.t
|
||||
val sexp_of_t : t Sexp.To_sexp.t
|
||||
val exec : t -> unit Future.t
|
|
@ -91,6 +91,6 @@ let rules store ~prefixes ~tree =
|
|||
let rule =
|
||||
Build_interpret.Rule.make
|
||||
(Build.path_set deps >>>
|
||||
Build.touch alias.file)
|
||||
Build.create_file alias.file)
|
||||
in
|
||||
rule :: acc)
|
||||
|
|
213
src/build.ml
213
src/build.ml
|
@ -18,14 +18,10 @@ type lib_dep_kind =
|
|||
type lib_deps = lib_dep_kind String_map.t
|
||||
|
||||
module Repr = struct
|
||||
type ('a, 'b) prim =
|
||||
{ targets : Path.t list
|
||||
; exec : 'a -> 'b Future.t
|
||||
}
|
||||
type ('a, 'b) t =
|
||||
| Arr : ('a -> 'b) -> ('a, 'b) t
|
||||
| Prim : ('a, 'b) prim -> ('a, 'b) t
|
||||
| Store_vfile : 'a Vspec.t -> ('a, unit) t
|
||||
| Targets : Path.t list -> ('a, 'a) t
|
||||
| Store_vfile : 'a Vspec.t -> ('a, Action.t) t
|
||||
| Compose : ('a, 'b) t * ('b, 'c) t -> ('a, 'c) t
|
||||
| First : ('a, 'b) t -> ('a * 'c, 'b * 'c) t
|
||||
| Second : ('a, 'b) t -> ('c * 'a, 'c * 'b) t
|
||||
|
@ -121,13 +117,6 @@ let files_recursively_in ~dir =
|
|||
in
|
||||
path_set (loop src_dir Pset.empty)
|
||||
|
||||
let prim ~targets exec = Prim { targets; exec }
|
||||
|
||||
let create_files ~targets exec =
|
||||
prim ~targets (fun x -> Future.return (exec x))
|
||||
let create_file ~target exec =
|
||||
create_files ~targets:[target] exec
|
||||
|
||||
let store_vfile spec = Store_vfile spec
|
||||
|
||||
let get_prog (prog : _ Prog_spec.t) =
|
||||
|
@ -145,7 +134,7 @@ let prog_and_args ~dir prog args =
|
|||
>>>
|
||||
arr fst))
|
||||
|
||||
let run ?(dir=Path.root) ?stdout_to ?env ?(extra_targets=[]) prog args =
|
||||
let run ?(dir=Path.root) ?stdout_to ?context ?(extra_targets=[]) prog args =
|
||||
let extra_targets =
|
||||
match stdout_to with
|
||||
| None -> extra_targets
|
||||
|
@ -154,169 +143,59 @@ let run ?(dir=Path.root) ?stdout_to ?env ?(extra_targets=[]) prog args =
|
|||
let targets = Arg_spec.add_targets args extra_targets in
|
||||
prog_and_args ~dir prog args
|
||||
>>>
|
||||
prim ~targets
|
||||
(fun (prog, args) ->
|
||||
let stdout_to =
|
||||
Targets targets
|
||||
>>^ (fun (prog, args) ->
|
||||
let action : (_, _) Action.Mini_shexp.t = Run (prog, args) in
|
||||
let action =
|
||||
match stdout_to with
|
||||
| None -> Future.Terminal
|
||||
| Some path -> File (Path.to_string path)
|
||||
| None -> action
|
||||
| Some path -> With_stdout_to (path, action)
|
||||
in
|
||||
Future.run Strict ~dir:(Path.to_string dir) ~stdout_to ?env
|
||||
(Path.reach prog ~from:dir) args)
|
||||
{ Action.
|
||||
dir
|
||||
; context
|
||||
; action = Shexp action
|
||||
})
|
||||
|
||||
module Shexp = struct
|
||||
open Future
|
||||
open Action.Mini_shexp
|
||||
let action ?(dir=Path.root) ?context ~targets action =
|
||||
Targets targets
|
||||
>>^ fun () ->
|
||||
{ Action. context; dir; action }
|
||||
|
||||
let run ~dir ~env ~env_extra ~stdout_to ~tail prog args =
|
||||
let stdout_to : Future.stdout_to =
|
||||
match stdout_to with
|
||||
| None -> Terminal
|
||||
| Some (fn, oc) -> Opened_file { filename = fn; tail; desc = Channel oc }
|
||||
in
|
||||
let env = Context.extend_env ~vars:env_extra ~env in
|
||||
Future.run Strict ~dir:(Path.to_string dir) ~env ~stdout_to prog args
|
||||
let shexp ?dir ?context ~targets shexp =
|
||||
action ?dir ?context ~targets (Shexp shexp)
|
||||
|
||||
let rec exec t ~dir ~env ~env_extra ~stdout_to ~tail =
|
||||
match t with
|
||||
| Run (prog, args) ->
|
||||
run ~dir ~env ~env_extra ~stdout_to ~tail prog args
|
||||
| Chdir (fn, t) ->
|
||||
exec t ~env ~env_extra ~stdout_to ~tail ~dir:(Path.relative dir fn)
|
||||
| Setenv (var, value, t) ->
|
||||
exec t ~dir ~env ~stdout_to ~tail
|
||||
~env_extra:(String_map.add env_extra ~key:var ~data:value)
|
||||
| With_stdout_to (fn, t) ->
|
||||
if tail then Option.iter stdout_to ~f:(fun (_, oc) -> close_out oc);
|
||||
let fn = Path.to_string (Path.relative dir fn) in
|
||||
exec t ~dir ~env ~env_extra ~tail
|
||||
~stdout_to:(Some (fn, open_out_bin fn))
|
||||
| Progn l ->
|
||||
exec_list l ~dir ~env ~env_extra ~stdout_to ~tail
|
||||
| Echo str ->
|
||||
return
|
||||
(match stdout_to with
|
||||
| None -> print_string str; flush stdout
|
||||
| Some (_, oc) ->
|
||||
output_string oc str;
|
||||
if tail then close_out oc)
|
||||
| Cat fn ->
|
||||
let fn = Path.to_string (Path.relative dir fn) in
|
||||
with_file_in fn ~f:(fun ic ->
|
||||
match stdout_to with
|
||||
| None -> copy_channels ic stdout
|
||||
| Some (_, oc) ->
|
||||
copy_channels ic oc;
|
||||
if tail then close_out oc);
|
||||
return ()
|
||||
| Copy (src, dst) ->
|
||||
let src = Path.relative dir src in
|
||||
let dst = Path.relative dir dst in
|
||||
copy_file ~src:(Path.to_string src) ~dst:(Path.to_string dst);
|
||||
return ()
|
||||
| Symlink (src, dst) ->
|
||||
let src = Path.relative dir src in
|
||||
let dst = Path.relative dir dst in
|
||||
if Sys.win32 then
|
||||
copy_file ~src:(Path.to_string src) ~dst:(Path.to_string dst)
|
||||
else begin
|
||||
let src =
|
||||
if Path.is_root dst then
|
||||
Path.to_string src
|
||||
else
|
||||
Path.reach ~from:(Path.parent dst) src
|
||||
in
|
||||
let dst = Path.to_string dst in
|
||||
match Unix.readlink dst with
|
||||
| target ->
|
||||
if target <> src then begin
|
||||
Unix.unlink dst;
|
||||
Unix.symlink src dst
|
||||
end
|
||||
| exception _ ->
|
||||
Unix.symlink src dst
|
||||
end;
|
||||
return ()
|
||||
| Copy_and_add_line_directive (src, dst) ->
|
||||
let src = Path.relative dir src in
|
||||
let dst = Path.relative dir dst in
|
||||
with_file_in (Path.to_string src) ~f:(fun ic ->
|
||||
with_file_out (Path.to_string dst) ~f:(fun oc ->
|
||||
let fn =
|
||||
match Path.extract_build_context src with
|
||||
| None -> src
|
||||
| Some (_, rem) -> rem
|
||||
in
|
||||
Printf.fprintf oc "# 1 %S\n" (Path.to_string fn);
|
||||
copy_channels ic oc));
|
||||
return ()
|
||||
| System cmd ->
|
||||
let path, arg, err =
|
||||
Utils.system_shell ~needed_to:"interpret (system ...) actions"
|
||||
in
|
||||
match err with
|
||||
| Some err -> err.fail ()
|
||||
| None ->
|
||||
run ~dir ~env ~env_extra ~stdout_to ~tail
|
||||
(Path.to_string path) [arg; cmd]
|
||||
let echo fn s =
|
||||
shexp ~targets:[fn] (With_stdout_to (fn, Echo s))
|
||||
|
||||
and exec_list l ~dir ~env ~env_extra ~stdout_to ~tail =
|
||||
match l with
|
||||
| [] ->
|
||||
if tail then Option.iter stdout_to ~f:(fun (_, oc) -> close_out oc);
|
||||
Future.return ()
|
||||
| [t] ->
|
||||
exec t ~dir ~env ~env_extra ~stdout_to ~tail
|
||||
| t :: rest ->
|
||||
exec t ~dir ~env ~env_extra ~stdout_to ~tail:false >>= fun () ->
|
||||
exec_list rest ~dir ~env ~env_extra ~stdout_to ~tail
|
||||
|
||||
let exec t ~dir ~env =
|
||||
exec t ~dir ~env ~env_extra:String_map.empty ~stdout_to:None ~tail:true
|
||||
end
|
||||
|
||||
let action action ~dir ~env ~targets =
|
||||
prim ~targets (fun () ->
|
||||
match (action : _ Action.Desc.t) with
|
||||
| Bash cmd ->
|
||||
Future.run Strict ~dir:(Path.to_string dir) ~env
|
||||
"/bin/bash" ["-e"; "-u"; "-o"; "pipefail"; "-c"; cmd]
|
||||
| Shexp shexp ->
|
||||
Shexp.exec ~dir ~env shexp)
|
||||
|
||||
let echo fn =
|
||||
create_file ~target:fn (fun data ->
|
||||
with_file_out (Path.to_string fn) ~f:(fun oc -> output_string oc data))
|
||||
let echo_dyn fn =
|
||||
Targets [fn]
|
||||
>>^ fun s ->
|
||||
{ Action.
|
||||
context = None
|
||||
; dir = Path.root
|
||||
; action = Shexp (With_stdout_to (fn, Echo s))
|
||||
}
|
||||
|
||||
let copy ~src ~dst =
|
||||
path src >>>
|
||||
create_file ~target:dst (fun () ->
|
||||
copy_file ~src:(Path.to_string src) ~dst:(Path.to_string dst))
|
||||
shexp ~targets:[dst] (Copy (src, dst))
|
||||
|
||||
let symlink ~src ~dst =
|
||||
if Sys.win32 then
|
||||
copy ~src ~dst
|
||||
else
|
||||
path src >>>
|
||||
create_file ~target:dst (fun () ->
|
||||
let src =
|
||||
if Path.is_root dst then
|
||||
Path.to_string src
|
||||
else
|
||||
Path.reach ~from:(Path.parent dst) src
|
||||
in
|
||||
let dst = Path.to_string dst in
|
||||
match Unix.readlink dst with
|
||||
| target ->
|
||||
if target <> src then begin
|
||||
Unix.unlink dst;
|
||||
Unix.symlink src dst
|
||||
end
|
||||
| exception _ ->
|
||||
Unix.symlink src dst)
|
||||
shexp ~targets:[dst] (Symlink (src, dst))
|
||||
|
||||
let touch target =
|
||||
create_file ~target (fun _ ->
|
||||
Unix.close
|
||||
(Unix.openfile (Path.to_string target)
|
||||
[O_CREAT; O_TRUNC; O_WRONLY] 0o666))
|
||||
let create_file fn =
|
||||
shexp ~targets:[fn] (Create_file fn)
|
||||
|
||||
let and_create_file fn =
|
||||
arr (fun (action : Action.t) ->
|
||||
{ action with
|
||||
action =
|
||||
match action.action with
|
||||
| Bash cmd ->
|
||||
let fn = quote_for_shell (Path.to_string fn) in
|
||||
Bash (sprintf "(%s); rm -f %s; touch %s" cmd fn fn)
|
||||
| Shexp shexp ->
|
||||
Shexp (Progn [shexp; Create_file fn])
|
||||
})
|
||||
|
|
|
@ -8,14 +8,11 @@ val arr : ('a -> 'b) -> ('a, 'b) t
|
|||
|
||||
val return : 'a -> (unit, 'a) t
|
||||
|
||||
val create_file : target:Path.t -> ('a -> 'b) -> ('a, 'b) t
|
||||
val create_files : targets:Path.t list -> ('a -> 'b) -> ('a, 'b) t
|
||||
|
||||
module Vspec : sig
|
||||
type 'a t = T : Path.t * 'a Vfile_kind.t -> 'a t
|
||||
end
|
||||
|
||||
val store_vfile : 'a Vspec.t -> ('a, unit) t
|
||||
val store_vfile : 'a Vspec.t -> ('a, Action.t) t
|
||||
|
||||
module O : sig
|
||||
val ( >>> ) : ('a, 'b) t -> ('b, 'c) t -> ('a, 'c) t
|
||||
|
@ -58,27 +55,37 @@ end
|
|||
val run
|
||||
: ?dir:Path.t
|
||||
-> ?stdout_to:Path.t
|
||||
-> ?env:string array
|
||||
-> ?context:Context.t
|
||||
-> ?extra_targets:Path.t list
|
||||
-> 'a Prog_spec.t
|
||||
-> 'a Arg_spec.t list
|
||||
-> ('a, unit) t
|
||||
-> ('a, Action.t) t
|
||||
|
||||
val action
|
||||
: string Action.Desc.t
|
||||
-> dir:Path.t
|
||||
-> env:string array
|
||||
: ?dir:Path.t
|
||||
-> ?context:Context.t
|
||||
-> targets:Path.t list
|
||||
-> (unit, unit) t
|
||||
-> Action.Desc.t
|
||||
-> (unit, Action.t) t
|
||||
|
||||
val shexp
|
||||
: ?dir:Path.t
|
||||
-> ?context:Context.t
|
||||
-> targets:Path.t list
|
||||
-> (string, Path.t) Action.Mini_shexp.t
|
||||
-> (unit, Action.t) t
|
||||
|
||||
(** Create a file with the given contents. *)
|
||||
val echo : Path.t -> (string, unit) t
|
||||
val echo : Path.t -> string -> (unit, Action.t) t
|
||||
val echo_dyn : Path.t -> (string, Action.t) t
|
||||
|
||||
val copy : src:Path.t -> dst:Path.t -> (unit, unit) t
|
||||
val copy : src:Path.t -> dst:Path.t -> (unit, Action.t) t
|
||||
|
||||
val symlink : src:Path.t -> dst:Path.t -> (unit, unit) t
|
||||
val symlink : src:Path.t -> dst:Path.t -> (unit, Action.t) t
|
||||
|
||||
val touch : Path.t -> (unit, unit) t
|
||||
val create_file : Path.t -> (unit, Action.t) t
|
||||
|
||||
val and_create_file : Path.t -> (Action.t, Action.t) t
|
||||
|
||||
type lib_dep_kind =
|
||||
| Optional
|
||||
|
@ -96,14 +103,10 @@ type lib_deps = lib_dep_kind String_map.t
|
|||
|
||||
|
||||
module Repr : sig
|
||||
type ('a, 'b) prim =
|
||||
{ targets : Path.t list
|
||||
; exec : 'a -> 'b Future.t
|
||||
}
|
||||
type ('a, 'b) t =
|
||||
| Arr : ('a -> 'b) -> ('a, 'b) t
|
||||
| Prim : ('a, 'b) prim -> ('a, 'b) t
|
||||
| Store_vfile : 'a Vspec.t -> ('a, unit) t
|
||||
| Targets : Path.t list -> ('a, 'a) t
|
||||
| Store_vfile : 'a Vspec.t -> ('a, Action.t) t
|
||||
| Compose : ('a, 'b) t * ('b, 'c) t -> ('a, 'c) t
|
||||
| First : ('a, 'b) t -> ('a * 'c, 'b * 'c) t
|
||||
| Second : ('a, 'b) t -> ('c * 'a, 'c * 'b) t
|
||||
|
|
|
@ -23,7 +23,7 @@ let deps t ~all_targets_by_dir =
|
|||
let rec loop : type a b. (a, b) t -> Pset.t -> Pset.t = fun t acc ->
|
||||
match t with
|
||||
| Arr _ -> acc
|
||||
| Prim _ -> acc
|
||||
| Targets _ -> acc
|
||||
| Store_vfile _ -> acc
|
||||
| Compose (a, b) -> loop a (loop b acc)
|
||||
| First t -> loop t acc
|
||||
|
@ -50,7 +50,7 @@ let lib_deps =
|
|||
= fun t acc ->
|
||||
match t with
|
||||
| Arr _ -> acc
|
||||
| Prim _ -> acc
|
||||
| Targets _ -> acc
|
||||
| Store_vfile _ -> acc
|
||||
| Compose (a, b) -> loop a (loop b acc)
|
||||
| First t -> loop t acc
|
||||
|
@ -76,7 +76,7 @@ let targets =
|
|||
let rec loop : type a b. (a, b) t -> Target.t list -> Target.t list = fun t acc ->
|
||||
match t with
|
||||
| Arr _ -> acc
|
||||
| Prim { targets; _ } ->
|
||||
| Targets targets ->
|
||||
List.fold_left targets ~init:acc ~f:(fun acc fn -> Target.Normal fn :: acc)
|
||||
| Store_vfile spec -> Vfile spec :: acc
|
||||
| Compose (a, b) -> loop a (loop b acc)
|
||||
|
@ -95,7 +95,7 @@ let targets =
|
|||
|
||||
module Rule = struct
|
||||
type t =
|
||||
{ build : (unit, unit) Build.t
|
||||
{ build : (unit, Action.t) Build.t
|
||||
; targets : Target.t list
|
||||
}
|
||||
|
||||
|
|
|
@ -11,11 +11,11 @@ end
|
|||
|
||||
module Rule : sig
|
||||
type t =
|
||||
{ build : (unit, unit) Build.t
|
||||
{ build : (unit, Action.t) Build.t
|
||||
; targets : Target.t list
|
||||
}
|
||||
|
||||
val make : (unit, unit) Build.t -> t
|
||||
val make : (unit, Action.t) Build.t -> t
|
||||
end
|
||||
|
||||
val deps
|
||||
|
|
|
@ -22,9 +22,7 @@ module Rule = struct
|
|||
type t =
|
||||
{ deps : Pset.t
|
||||
; targets : Pset.t
|
||||
; (* Keep the arrow around so that we can do more query, such as for finding external
|
||||
library dependencies *)
|
||||
build : (unit, unit) Build.t
|
||||
; build : (unit, Action.t) Build.t
|
||||
; mutable exec : Exec_status.t
|
||||
}
|
||||
end
|
||||
|
@ -154,19 +152,26 @@ let save_vfile (type a) (module K : Vfile_kind.S with type t = a) fn x =
|
|||
module Build_exec = struct
|
||||
open Build.Repr
|
||||
|
||||
let nop =
|
||||
{ Action.
|
||||
context = None
|
||||
; dir = Path.root
|
||||
; action = Shexp (Progn [])
|
||||
}
|
||||
|
||||
let exec bs t x ~targeting =
|
||||
let rec exec
|
||||
: type a b. (a, b) t -> a -> b Future.t = fun t x ->
|
||||
let return = Future.return in
|
||||
match t with
|
||||
| Arr f -> return (f x)
|
||||
| Prim { exec; _ } -> exec x
|
||||
| Targets _ -> return x
|
||||
| Store_vfile (Vspec.T (fn, kind)) ->
|
||||
let file = get_file bs fn (Sexp_file kind) in
|
||||
assert (file.data = None);
|
||||
file.data <- Some x;
|
||||
save_vfile kind fn x;
|
||||
Future.return ()
|
||||
Future.return nop
|
||||
| Compose (a, b) ->
|
||||
exec a x >>= exec b
|
||||
| First t ->
|
||||
|
@ -248,6 +253,8 @@ let compile_rule t ~all_targets_by_dir ?(allow_override=false) pre_rule =
|
|||
(Pset.fold deps ~init:[] ~f:(fun fn acc -> wait_for_file t fn ~targeting :: acc))
|
||||
>>= fun () ->
|
||||
Build_exec.exec t build () ~targeting
|
||||
>>= fun action ->
|
||||
Action.exec action
|
||||
) in
|
||||
let rule =
|
||||
{ Rule.
|
||||
|
|
|
@ -129,3 +129,5 @@ val opam_config_var : t -> string -> string option Future.t
|
|||
val install_prefix : t -> Path.t Future.t
|
||||
|
||||
val env_for_exec : t -> string array
|
||||
|
||||
val initial_env : string array Lazy.t
|
||||
|
|
107
src/gen_rules.ml
107
src/gen_rules.ml
|
@ -241,15 +241,8 @@ module Gen(P : Params) = struct
|
|||
List.map (Lib_db.resolve_selects t ~from:dir lib_deps) ~f:(fun { dst_fn; src_fn } ->
|
||||
let src = Path.relative dir src_fn in
|
||||
let dst = Path.relative dir dst_fn in
|
||||
Build.path src
|
||||
>>>
|
||||
Build.create_files ~targets:[dst] (fun () ->
|
||||
let src_fn = Path.to_string src in
|
||||
let dst_fn = Path.to_string dst in
|
||||
with_file_in src_fn ~f:(fun ic ->
|
||||
with_file_out dst_fn ~f:(fun oc ->
|
||||
Printf.fprintf oc "# 1 \"%s\"\n" src_fn;
|
||||
copy_channels ic oc))))
|
||||
Build.shexp ~targets:[dst]
|
||||
(Copy_and_add_line_directive (src, dst)))
|
||||
|
||||
(* Hides [t] so that we don't resolve things statically *)
|
||||
let t = ()
|
||||
|
@ -286,22 +279,31 @@ module Gen(P : Params) = struct
|
|||
|
||||
[@@@warning "-32"]
|
||||
|
||||
let run ?(dir=ctx.build_dir) ?stdout_to ?(env=ctx.env) ?extra_targets prog args =
|
||||
Build.run ~dir ?stdout_to ~env ?extra_targets prog args
|
||||
let run ?(dir=ctx.build_dir) ?stdout_to ?extra_targets prog args =
|
||||
Build.run ~dir ?stdout_to ~context:ctx ?extra_targets prog args
|
||||
|
||||
let bash ?dir ?stdout_to ?env ?extra_targets cmd =
|
||||
run (Dep (Path.absolute "/bin/bash")) ?dir ?stdout_to ?env ?extra_targets
|
||||
let bash ?dir ?stdout_to ?extra_targets cmd =
|
||||
run (Dep (Path.absolute "/bin/bash")) ?dir ?stdout_to ?extra_targets
|
||||
[ As ["-e"; "-u"; "-o"; "pipefail"; "-c"; cmd] ]
|
||||
|
||||
let system ?dir ?stdout_to ?env ?extra_targets cmd ~needed_to =
|
||||
let system ?dir ?stdout_to ?extra_targets cmd ~needed_to =
|
||||
let path, arg, fail = Utils.system_shell ~needed_to in
|
||||
let build =
|
||||
run (Dep path) ?dir ?stdout_to ?env ?extra_targets
|
||||
run (Dep path) ?dir ?stdout_to ?extra_targets
|
||||
[ As [arg; cmd] ]
|
||||
in
|
||||
match fail with
|
||||
| None -> build
|
||||
| Some fail -> Build.fail fail >>> build
|
||||
|
||||
let action ?dir ~targets action =
|
||||
Build.action ?dir ~context:ctx ~targets action
|
||||
|
||||
let shexp ?dir ~targets shexp =
|
||||
Build.shexp ?dir ~context:ctx ~targets shexp
|
||||
|
||||
let shexp_context_independent ?dir ~targets shexp =
|
||||
Build.shexp ?dir ~targets shexp
|
||||
end
|
||||
|
||||
module Alias = struct
|
||||
|
@ -745,7 +747,7 @@ module Gen(P : Params) = struct
|
|||
|> List.map ~f:(Printf.sprintf "%s\n")
|
||||
|> String.concat ~sep:"")
|
||||
>>>
|
||||
Build.echo path
|
||||
Build.echo_dyn path
|
||||
)
|
||||
| _ ->
|
||||
()
|
||||
|
@ -978,8 +980,7 @@ module Gen(P : Params) = struct
|
|||
let mk_lib_cm_all (lib : Library.t) ~dir ~modules cm_kind =
|
||||
let deps = cm_files ~dir (String_map.values modules) ~cm_kind in
|
||||
add_rule (Build.paths deps >>>
|
||||
Build.return "" >>>
|
||||
Build.echo (lib_cm_all lib ~dir cm_kind))
|
||||
Build.create_file (lib_cm_all lib ~dir cm_kind))
|
||||
|
||||
let expand_includes ~dir includes =
|
||||
Arg_spec.As (List.concat_map includes ~f:(fun s ->
|
||||
|
@ -1109,7 +1110,7 @@ module Gen(P : Params) = struct
|
|||
|> List.map ~f:(fun (m : Module.t) ->
|
||||
sprintf "module %s = %s\n" m.name (Module.real_unit_name m))
|
||||
|> String.concat ~sep:"")
|
||||
>>> Build.echo (Path.relative dir m.ml_fname)));
|
||||
>>> Build.echo_dyn (Path.relative dir m.ml_fname)));
|
||||
|
||||
let requires, real_requires =
|
||||
requires ~dir ~dep_kind ~item:lib.name
|
||||
|
@ -1300,7 +1301,7 @@ module Gen(P : Params) = struct
|
|||
-> dep_kind:Build.lib_dep_kind
|
||||
-> targets:Path.t list
|
||||
-> deps:Dep_conf.t list
|
||||
-> (unit, unit) Build.t
|
||||
-> (unit, Action.t) Build.t
|
||||
end = struct
|
||||
module U = Action.Desc.Unexpanded
|
||||
|
||||
|
@ -1338,7 +1339,7 @@ module Gen(P : Params) = struct
|
|||
; lib_deps = String_set.empty
|
||||
}
|
||||
in
|
||||
U.fold t ~init ~f:(fun acc var ->
|
||||
U.fold_vars t ~init ~f:(fun acc var ->
|
||||
let module A = Artifacts in
|
||||
match String.lsplit2 var ~on:':' with
|
||||
| Some ("exe" , s) -> add_artifact acc ~var (Ok (Path.relative dir s))
|
||||
|
@ -1354,23 +1355,27 @@ module Gen(P : Params) = struct
|
|||
add_artifact acc ~var ~lib_dep res
|
||||
| _ -> acc)
|
||||
|
||||
let expand_string_with_vars =
|
||||
let dep_exn ~dir name = function
|
||||
| Some dep -> Path.reach ~from:dir dep
|
||||
let expand_var =
|
||||
let dep_exn name = function
|
||||
| Some dep -> dep
|
||||
| None -> die "cannot use ${%s} with files_recursively_in" name
|
||||
in
|
||||
fun ~artifacts ~targets ~deps dir var_name ->
|
||||
fun ~artifacts ~targets ~deps var_name ->
|
||||
match String_map.find var_name artifacts with
|
||||
| Some path -> Some (Path.reach ~from:dir path)
|
||||
| Some path -> Action.Path path
|
||||
| None ->
|
||||
match var_name with
|
||||
| "@" -> Some (String.concat ~sep:" "
|
||||
(List.map targets ~f:(Path.reach ~from:dir)))
|
||||
| "<" -> Some (match deps with [] -> "" | dep1::_ -> dep_exn ~dir var_name dep1)
|
||||
| "@" -> Paths targets
|
||||
| "<" -> (match deps with
|
||||
| [] -> Str ""
|
||||
| dep1 :: _ -> Path (dep_exn var_name dep1))
|
||||
| "^" ->
|
||||
let deps = List.map deps ~f:(dep_exn ~dir var_name) in
|
||||
Some (String.concat ~sep:" " deps)
|
||||
| _ -> root_var_lookup ~dir var_name
|
||||
Paths (List.map deps ~f:(dep_exn var_name))
|
||||
| "ROOT" -> Path Path.root
|
||||
| _ ->
|
||||
match String_map.find var_name dollar_var_map with
|
||||
| Some s -> Str s
|
||||
| _ -> Not_found
|
||||
|
||||
let run t ~dir ~dep_kind ~targets ~deps =
|
||||
let deps =
|
||||
|
@ -1381,7 +1386,7 @@ module Gen(P : Params) = struct
|
|||
let forms = extract_artifacts ~dir t in
|
||||
let t =
|
||||
U.expand dir t
|
||||
~f:(expand_string_with_vars ~artifacts:forms.artifacts ~targets ~deps)
|
||||
~f:(expand_var ~artifacts:forms.artifacts ~targets ~deps)
|
||||
in
|
||||
let build =
|
||||
Build.record_lib_deps ~dir ~kind:dep_kind
|
||||
|
@ -1390,7 +1395,7 @@ module Gen(P : Params) = struct
|
|||
>>>
|
||||
Build.paths (String_map.values forms.artifacts)
|
||||
>>>
|
||||
Build.action t ~dir ~env:ctx.env ~targets
|
||||
Build.action t ~dir ~targets
|
||||
in
|
||||
match forms.failures with
|
||||
| [] -> build
|
||||
|
@ -1427,12 +1432,14 @@ module Gen(P : Params) = struct
|
|||
|> Digest.to_hex in
|
||||
let alias = Alias.make alias_conf.name ~dir in
|
||||
let digest_path = Path.extend_basename (Alias.file alias) ~suffix:("-" ^ digest) in
|
||||
let dummy = Build.touch digest_path in
|
||||
Alias.add_deps alias [digest_path];
|
||||
let deps =
|
||||
let deps = Dep_conf_interpret.dep_of_list ~dir alias_conf.deps in
|
||||
match alias_conf.action with
|
||||
| None -> deps
|
||||
add_rule
|
||||
(match alias_conf.action with
|
||||
| None ->
|
||||
deps
|
||||
>>>
|
||||
Build.create_file digest_path
|
||||
| Some action ->
|
||||
deps
|
||||
>>> Action_interpret.run
|
||||
|
@ -1441,8 +1448,8 @@ module Gen(P : Params) = struct
|
|||
~dep_kind:Required
|
||||
~targets:[]
|
||||
~deps:alias_conf.deps
|
||||
in
|
||||
add_rule (deps >>> dummy)
|
||||
>>>
|
||||
Build.and_create_file digest_path)
|
||||
|
||||
(* +-----------------------------------------------------------------+
|
||||
| Modules listing |
|
||||
|
@ -1645,10 +1652,9 @@ module Gen(P : Params) = struct
|
|||
in
|
||||
add_rule
|
||||
(Build.fanout meta template
|
||||
>>>
|
||||
Build.create_file ~target:meta_path (fun ((meta : Meta.t), template) ->
|
||||
with_file_out (Path.to_string meta_path) ~f:(fun oc ->
|
||||
let ppf = Format.formatter_of_out_channel oc in
|
||||
>>^ (fun ((meta : Meta.t), template) ->
|
||||
let buf = Buffer.create 1024 in
|
||||
let ppf = Format.formatter_of_buffer buf in
|
||||
Format.pp_open_vbox ppf 0;
|
||||
List.iter template ~f:(fun s ->
|
||||
if String.is_prefix s ~prefix:"#" then
|
||||
|
@ -1660,7 +1666,10 @@ module Gen(P : Params) = struct
|
|||
else
|
||||
Format.fprintf ppf "%s@," s);
|
||||
Format.pp_close_box ppf ();
|
||||
Format.pp_print_flush ppf ())));
|
||||
Format.pp_print_flush ppf ();
|
||||
Buffer.contents buf)
|
||||
>>>
|
||||
Build.echo_dyn meta_path);
|
||||
|
||||
if has_meta || has_meta_tmpl then
|
||||
Some pkg.name
|
||||
|
@ -1781,9 +1790,11 @@ module Gen(P : Params) = struct
|
|||
in
|
||||
let entries = local_install_rules entries ~package in
|
||||
add_rule
|
||||
(Build.path_set (Install.files entries) >>>
|
||||
Build.create_file ~target:fn (fun () ->
|
||||
Install.write_install_file fn entries))
|
||||
(Build.path_set (Install.files entries)
|
||||
>>^ (fun () ->
|
||||
Install.gen_install_file entries)
|
||||
>>>
|
||||
Build.echo_dyn fn)
|
||||
|
||||
let () = String_map.iter P.packages ~f:(fun ~key:_ ~data:pkg ->
|
||||
install_file pkg.Package.path pkg.name)
|
||||
|
|
|
@ -119,9 +119,9 @@ let group entries =
|
|||
|> SMap.of_alist_multi
|
||||
|> SMap.bindings
|
||||
|
||||
let write_install_file file entries =
|
||||
with_file_out (Path.to_string file) ~f:(fun oc ->
|
||||
let pr fmt = Printf.fprintf oc (fmt ^^ "\n") in
|
||||
let gen_install_file entries =
|
||||
let buf = Buffer.create 4096 in
|
||||
let pr fmt = Printf.bprintf buf (fmt ^^ "\n") in
|
||||
List.iter (group entries) ~f:(fun (section, entries) ->
|
||||
pr "%s: [" (Section.to_string section);
|
||||
List.iter entries ~f:(fun (e : Entry.t) ->
|
||||
|
@ -129,4 +129,5 @@ let write_install_file file entries =
|
|||
match e.dst with
|
||||
| None -> pr " %S" src
|
||||
| Some dst -> pr " %S {%S}" src dst);
|
||||
pr "]"))
|
||||
pr "]");
|
||||
Buffer.contents buf
|
||||
|
|
|
@ -31,4 +31,4 @@ module Entry : sig
|
|||
end
|
||||
|
||||
val files : Entry.t list -> Path.Set.t
|
||||
val write_install_file : Path.t -> Entry.t list -> unit
|
||||
val gen_install_file : Entry.t list -> string
|
||||
|
|
61
src/path.ml
61
src/path.ml
|
@ -104,6 +104,51 @@ module Local = struct
|
|||
in
|
||||
loop initial_t (explode_path path)
|
||||
|
||||
let is_canonicalized =
|
||||
let rec before_slash s i =
|
||||
if i < 0 then
|
||||
false
|
||||
else
|
||||
match s.[i] with
|
||||
| '/' -> false
|
||||
| '.' -> before_dot_slash s (i - 1)
|
||||
| _ -> in_component s (i - 1)
|
||||
and before_dot_slash s i =
|
||||
if i < 0 then
|
||||
false
|
||||
else
|
||||
match s.[i] with
|
||||
| '/' -> false
|
||||
| '.' -> before_dot_dot_slash s (i - 1)
|
||||
| _ -> in_component s (i - 1)
|
||||
and before_dot_dot_slash s i =
|
||||
if i < 0 then
|
||||
false
|
||||
else
|
||||
match s.[i] with
|
||||
| '/' -> false
|
||||
| _ -> in_component s (i - 1)
|
||||
and in_component s i =
|
||||
if i < 0 then
|
||||
true
|
||||
else
|
||||
match s.[i] with
|
||||
| '/' -> before_slash s (i - 1)
|
||||
| _ -> in_component s (i - 1)
|
||||
in
|
||||
fun s ->
|
||||
let len = String.length s in
|
||||
if len = 0 then
|
||||
true
|
||||
else
|
||||
before_slash s (len - 1)
|
||||
|
||||
let of_string s =
|
||||
if is_canonicalized s then
|
||||
s
|
||||
else
|
||||
relative "" s
|
||||
|
||||
let rec mkdir_p = function
|
||||
| "" -> ()
|
||||
| t ->
|
||||
|
@ -176,8 +221,6 @@ let to_string = function
|
|||
| "" -> "."
|
||||
| t -> t
|
||||
|
||||
let sexp_of_t t = Sexp.Atom (to_string t)
|
||||
|
||||
let root = ""
|
||||
|
||||
let relative t fn =
|
||||
|
@ -189,7 +232,16 @@ let relative t fn =
|
|||
| _ , false -> fn
|
||||
| false, true -> External.relative t fn
|
||||
|
||||
let of_string t = relative "" t
|
||||
let of_string = function
|
||||
| "" -> ""
|
||||
| s ->
|
||||
if Filename.is_relative s then
|
||||
Local.of_string s
|
||||
else
|
||||
s
|
||||
|
||||
let t sexp = of_string (Sexp.Of_sexp.string sexp)
|
||||
let sexp_of_t t = Sexp.Atom (to_string t)
|
||||
|
||||
let absolute =
|
||||
let initial_dir = Sys.getcwd () in
|
||||
|
@ -289,6 +341,3 @@ let insert_after_build_dir_exn =
|
|||
sprintf "_build/%s/%s" b rest
|
||||
| _ ->
|
||||
error a b
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -26,6 +26,9 @@ end
|
|||
|
||||
type t
|
||||
|
||||
val t : t Sexp.Of_sexp.t
|
||||
val sexp_of_t : t Sexp.To_sexp.t
|
||||
|
||||
val compare : t -> t -> int
|
||||
|
||||
module Set : Set.S with type elt = t
|
||||
|
|
|
@ -57,6 +57,10 @@ let of_string s = of_tokens (Token.tokenise s)
|
|||
|
||||
let t sexp = of_string (Sexp.Of_sexp.string sexp)
|
||||
|
||||
let just_a_var = function
|
||||
| [Var (_, s)] -> Some s
|
||||
| _ -> None
|
||||
|
||||
let sexp_of_var_syntax = function
|
||||
| Parens -> Sexp.Atom "parens"
|
||||
| Braces -> Sexp.Atom "braces"
|
||||
|
@ -88,7 +92,7 @@ let expand t ~f =
|
|||
| Parens -> sprintf "$(%s)" v
|
||||
| Braces -> sprintf "${%s}" v)
|
||||
|> String.concat ~sep:""
|
||||
|
||||
(*
|
||||
let expand_with_context context t ~f =
|
||||
List.map t ~f:(function
|
||||
| Text s -> s
|
||||
|
@ -100,27 +104,4 @@ let expand_with_context context t ~f =
|
|||
| Parens -> sprintf "$(%s)" v
|
||||
| Braces -> sprintf "${%s}" v)
|
||||
|> String.concat ~sep:""
|
||||
|
||||
module type Container = sig
|
||||
type 'a t
|
||||
val t : 'a Sexp.Of_sexp.t -> 'a t Sexp.Of_sexp.t
|
||||
val sexp_of_t : ('a -> Sexp.t) -> 'a t -> Sexp.t
|
||||
|
||||
type context
|
||||
val expand : context -> 'a t -> f:(context -> 'a -> string) -> string t
|
||||
val fold : 'a t -> init:'b -> f:('b -> 'a -> 'b) -> 'b
|
||||
end
|
||||
|
||||
module Lift(M : Container) = struct
|
||||
type nonrec t = t M.t
|
||||
let t sexp = M.t t sexp
|
||||
|
||||
let sexp_of_t a = M.sexp_of_t sexp_of_t a
|
||||
|
||||
let fold t ~init ~f =
|
||||
M.fold t ~init ~f:(fun acc x -> fold x ~init:acc ~f)
|
||||
|
||||
let expand context (t : t) ~f =
|
||||
M.expand context t ~f:(expand_with_context ~f)
|
||||
end
|
||||
|
||||
*)
|
||||
|
|
|
@ -11,33 +11,10 @@ val sexp_of_t : t -> Sexp.t
|
|||
|
||||
val of_string : string -> t
|
||||
|
||||
val just_a_var : t -> string option
|
||||
|
||||
val vars : t -> String_set.t
|
||||
|
||||
val fold : t -> init:'a -> f:('a -> string -> 'a) -> 'a
|
||||
|
||||
val expand : t -> f:(string -> string option) -> string
|
||||
|
||||
module type Container = sig
|
||||
type 'a t
|
||||
val t : 'a Sexp.Of_sexp.t -> 'a t Sexp.Of_sexp.t
|
||||
val sexp_of_t : ('a -> Sexp.t) -> 'a t -> Sexp.t
|
||||
|
||||
type context
|
||||
val expand : context -> 'a t -> f:(context -> 'a -> string) -> string t
|
||||
val fold : 'a t -> init:'b -> f:('b -> 'a -> 'b) -> 'b
|
||||
end
|
||||
|
||||
module Lift(M : Container) : sig
|
||||
type nonrec t = t M.t
|
||||
val t : t Sexp.Of_sexp.t
|
||||
|
||||
val sexp_of_t : t -> Sexp.t
|
||||
|
||||
val fold : t -> init:'a -> f:('a -> string -> 'a) -> 'a
|
||||
|
||||
val expand
|
||||
: M.context
|
||||
-> t
|
||||
-> f:(M.context -> string -> string option)
|
||||
-> string M.t
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue