From f44b8bdb1be134b410a6e85e8b7f844e6dfc5671 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Wed, 25 Apr 2018 18:22:48 +0700 Subject: [PATCH] Unhardcode _build dir everywhere --- bin/main.ml | 2 +- src/action.ml | 4 ++-- src/build_system.ml | 10 +++++----- src/context.mli | 2 +- src/log.ml | 5 ++--- src/stdune/path.ml | 6 ++++++ src/stdune/path.mli | 6 ++++++ src/utils.ml | 4 ++-- 8 files changed, 25 insertions(+), 14 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index 108a5740..8b0cfd55 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -728,7 +728,7 @@ let clean = set_common common ~targets:[]; Build_system.files_in_source_tree_to_delete () |> List.iter ~f:Path.unlink_no_err; - Path.(rm_rf (append root (of_string "_build"))) + Path.rm_rf Path.build_dir end in ( Term.(const go $ common) diff --git a/src/action.ml b/src/action.ml index 1615635e..18fe861e 100644 --- a/src/action.ml +++ b/src/action.ml @@ -647,10 +647,10 @@ module Promotion = struct Io.copy_file ~src ~dst end - let db_file = Path.of_string "_build/.to-promote" + let db_file = Path.relative_build_dir ".to-promote" let dump_db db = - if Sys.file_exists "_build" then begin + if Path.build_dir_exists () then begin match db with | [] -> if Path.exists db_file then Path.unlink_no_err db_file | l -> diff --git a/src/build_system.ml b/src/build_system.ml index 3e0bd40b..e27fde86 100644 --- a/src/build_system.ml +++ b/src/build_system.ml @@ -14,7 +14,7 @@ let misc_dir = Path.(relative build_dir) ".misc" module Promoted_to_delete = struct let db = ref [] - let fn = Path.of_string "_build/.to-delete-in-source-tree" + let fn = Path.relative_build_dir ".to-delete-in-source-tree" let add p = db := p :: !db @@ -27,7 +27,7 @@ module Promoted_to_delete = struct let dump () = let db = Pset.union (Pset.of_list !db) (Pset.of_list (load ())) in - if Sys.file_exists "_build" then + if Path.build_dir_exists () then Io.write_file fn (String.concat ~sep:"" (List.map (Pset.to_list db) ~f:(fun p -> @@ -1106,7 +1106,7 @@ let stamp_file_for_files_of t ~dir ~ext = module Trace = struct type t = (Path.t, Digest.t) Hashtbl.t - let file = Path.of_string "_build/.db" + let file = Path.relative_build_dir ".db" let dump (trace : t) = let sexp = @@ -1118,7 +1118,7 @@ module Trace = struct Sexp.List [ Path.sexp_of_t path; Atom (Sexp.Atom.of_digest hash) ])) in - if Sys.file_exists "_build" then + if Path.build_dir_exists () then Io.write_file file (Sexp.to_string sexp) let load () = @@ -1451,7 +1451,7 @@ let get_collector t ~dir = (if Path.is_in_source_tree dir then "Build_system.get_collector called on source directory" else if dir = Path.build_dir then - "Build_system.get_collector called on _build" + "Build_system.get_collector called on build_dir" else if not (Path.is_local dir) then "Build_system.get_collector called on external directory" else diff --git a/src/context.mli b/src/context.mli index 461bba23..b3fcc57c 100644 --- a/src/context.mli +++ b/src/context.mli @@ -8,7 +8,7 @@ - opam switch contexts, where one opam switch correspond to one context - each context is built into a sub-directory of "_build": + each context is built into a sub-directory of Path.build_dir (usually _build): - _build/default for the default context - _build/ for other contexts diff --git a/src/log.ml b/src/log.ml index c78811d3..51a8b389 100644 --- a/src/log.ml +++ b/src/log.ml @@ -12,9 +12,8 @@ type t = real option let no_log = None let create ?(display=Config.default.display) () = - if not (Sys.file_exists "_build") then - Unix.mkdir "_build" 0o777; - let oc = Io.open_out (Path.of_string "_build/log") in + Path.ensure_build_dir_exists (); + let oc = Io.open_out (Path.relative_build_dir "log") in Printf.fprintf oc "# %s\n# OCAMLPARAM: %s\n%!" (String.concat (List.map (Array.to_list Sys.argv) ~f:quote_for_shell) ~sep:" ") (match Env.get Env.initial "OCAMLPARAM" with diff --git a/src/stdune/path.ml b/src/stdune/path.ml index 2ee27dbe..8c592f04 100644 --- a/src/stdune/path.ml +++ b/src/stdune/path.ml @@ -453,6 +453,12 @@ let unlink t = unlink_operation (to_string t) let unlink_no_err t = try unlink t with _ -> () +let build_dir_exists () = is_directory build_dir + +let ensure_build_dir_exists () = Local.mkdir_p build_dir + +let relative_build_dir = relative build_dir + let extend_basename t ~suffix = t ^ suffix let insert_after_build_dir_exn = diff --git a/src/stdune/path.mli b/src/stdune/path.mli index f4c9984f..ecaee546 100644 --- a/src/stdune/path.mli +++ b/src/stdune/path.mli @@ -146,3 +146,9 @@ val extension : t -> string val drop_prefix : t -> prefix:t -> string option val pp : Format.formatter -> t -> unit + +val build_dir_exists : unit -> bool + +val ensure_build_dir_exists : unit -> unit + +val relative_build_dir : string -> t diff --git a/src/utils.ml b/src/utils.ml index 31c89d76..75a593a1 100644 --- a/src/utils.ml +++ b/src/utils.ml @@ -183,7 +183,7 @@ module Cached_digest = struct let remove fn = Hashtbl.remove cache fn - let db_file = Path.of_string "_build/.digest-db" + let db_file = Path.relative_build_dir ".digest-db" let dump () = let module Pmap = Path.Map in @@ -199,7 +199,7 @@ module Cached_digest = struct (Int64.bits_of_float file.timestamp)) ])) in - if Sys.file_exists "_build" then + if Path.build_dir_exists () then Io.write_file db_file (Sexp.to_string sexp) let load () =