Skip to content

Commit

Permalink
Make all processes use the special temp dir
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <[email protected]>
  • Loading branch information
rgrinberg committed Aug 31, 2020
1 parent 3128ea0 commit 5188593
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
35 changes: 4 additions & 31 deletions src/dune_engine/action_exec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,36 +111,9 @@ let validate_context_and_prog context prog =
invalid_prefix (Path.relative Path.build_dir target_name);
invalid_prefix (Path.relative Path.build_dir ("install/" ^ target_name))

module Temp : sig
(** Temp directory used for build actions: _build/temp *)

(** This returns a build path, but we don't rely on that *)
val file : prefix:string -> suffix:string -> Path.t

val env : Env.t -> Env.t

val destroy : Temp.what -> Path.t -> unit
end = struct
let temp_dir = lazy (Temp.create Dir ~prefix:"build" ~suffix:".dune")

let file ~prefix ~suffix =
Temp.temp_in_dir File ~dir:(Lazy.force temp_dir) ~suffix ~prefix

let env env =
let value = Path.to_absolute_filename (Lazy.force temp_dir) in
Env.add env ~var:Env.Var.temp_dir ~value

let destroy = Temp.destroy

let clear () = if Lazy.is_val temp_dir then Temp.clear (Lazy.force temp_dir)

let () = Hooks.End_of_build.always clear
end

let exec_run ~ectx ~eenv prog args =
validate_context_and_prog ectx.context prog;
let env = Temp.env eenv.env in
Process.run (Accept eenv.exit_codes) ~dir:eenv.working_dir ~env
Process.run (Accept eenv.exit_codes) ~dir:eenv.working_dir ~env:eenv.env
~stdout_to:eenv.stdout_to ~stderr_to:eenv.stderr_to
~stdin_from:eenv.stdin_from ~purpose:ectx.purpose prog args
|> Fiber.map ~f:ignore
Expand Down Expand Up @@ -446,7 +419,7 @@ and exec_list ts ~ectx ~eenv =

and exec_pipe outputs ts ~ectx ~eenv =
let tmp_file () =
Temp.file ~prefix:"dune-pipe-action-"
Dtemp.file ~prefix:"dune-pipe-action-"
~suffix:("." ^ Action.Outputs.to_string outputs)
in
let multi_use_eenv =
Expand All @@ -460,15 +433,15 @@ and exec_pipe outputs ts ~ectx ~eenv =
| [] -> assert false
| [ last_t ] ->
let+ result = redirect_in last_t ~ectx ~eenv Stdin in_ in
Temp.destroy File in_;
Dtemp.destroy File in_;
result
| t :: ts -> (
let out = tmp_file () in
let* done_or_deps =
redirect t ~ectx ~eenv:multi_use_eenv ~in_:(Stdin, in_)
~out:(outputs, out) ()
in
Temp.destroy File in_;
Dtemp.destroy File in_;
match done_or_deps with
| Need_more_deps _ as need -> Fiber.return need
| Done -> loop ~in_:out ts )
Expand Down
21 changes: 21 additions & 0 deletions src/dune_engine/dtemp.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
open Stdune

let temp_dir = lazy (Temp.create Dir ~prefix:"build" ~suffix:".dune")

let file ~prefix ~suffix =
Temp.temp_in_dir File ~dir:(Lazy.force temp_dir) ~suffix ~prefix

let add_to_env env =
let value = Path.to_absolute_filename (Lazy.force temp_dir) in
match env with
| None -> Env.add Env.initial ~var:Env.Var.temp_dir ~value
| Some env ->
Env.update env ~var:Env.Var.temp_dir ~f:(function
| None -> Some value
| Some _ as s -> s)

let destroy = Temp.destroy

let clear () = if Lazy.is_val temp_dir then Temp.clear (Lazy.force temp_dir)

let () = Hooks.End_of_build.always clear
13 changes: 13 additions & 0 deletions src/dune_engine/dtemp.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(** Temp directory used by dune processes *)
open Stdune

(** This returns a build path, but we don't rely on that *)
val file : prefix:string -> suffix:string -> Path.t

(** Add the temp env var to the enviornment passed or return the initial
environment with the temp var added. If temp is already set in the
environment, do nothing *)
val add_to_env : Env.t option -> Env.t

(** Destroy the temporary file or directory *)
val destroy : Temp.what -> Path.t -> unit
3 changes: 2 additions & 1 deletion src/dune_engine/process.ml
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,9 @@ let run_internal ?dir ?(stdout_to = Io.stdout) ?(stderr_to = Io.stderr)
let stdout = Io.fd stdout_to in
let stderr = Io.fd stderr_to in
let stdin = Io.fd stdin_from in
let env = Dtemp.add_to_env env in
fun () ->
Spawn.spawn () ~prog:prog_str ~argv ?env ~stdout ~stderr ~stdin
Spawn.spawn () ~prog:prog_str ~argv ~env ~stdout ~stderr ~stdin
in
let pid =
match dir with
Expand Down
2 changes: 1 addition & 1 deletion src/stdune/env.mli
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ val remove : t -> var:Var.t -> t

val diff : t -> t -> t

val update : t -> var:string -> f:(string option -> string option) -> t
val update : t -> var:Var.t -> f:(string option -> string option) -> t

val to_dyn : t -> Dyn.t

Expand Down

0 comments on commit 5188593

Please sign in to comment.