Skip to content

Commit

Permalink
feature(boot): remove reliance on shell
Browse files Browse the repository at this point in the history
Previously, we'd run the shell to discover the number of processes by
using `Unix.open_process_full`. The shell isn't being used for anything,
so we switch to executing the process directly.

Signed-off-by: Rudi Grinberg <[email protected]>

<!-- ps-id: f2284477-89a3-4579-baca-cd4c9ff2f26d -->
  • Loading branch information
rgrinberg committed Mar 11, 2023
1 parent 1402ef7 commit 64cde9f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Unreleased
----------

- Remove reliance on shell for bootstrapping. Previously, we'd use the shell to
get the number of processors. (#7274, @rgrinberg)

- Bootstrap: correctly detect the number of processors by allowing `nproc` to be
looked up in `$PATH` (#7272, @Alizter)

Expand Down
55 changes: 35 additions & 20 deletions boot/duneboot.ml
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,41 @@ let path =
| exception Not_found -> []
| s -> split_path s

let find_prog ~f =
let rec search = function
| [] -> None
| dir :: rest -> (
match f dir with
| None -> search rest
| Some fn -> Some (dir, fn))
in
search path

let exe = if Sys.win32 then ".exe" else ""

(** {2 Concurrency level} *)

let concurrency =
let try_run_and_capture_line cmd =
let ic, oc, ec = Unix.open_process_full cmd (Unix.environment ()) in
let line =
match input_line ic with
| s -> Some s
| exception End_of_file -> None
in
match (Unix.close_process_full (ic, oc, ec), line) with
| WEXITED 0, Some s -> Some s
| _ -> None
let try_run_and_capture_line (prog, args) =
match
find_prog ~f:(fun dir ->
if Sys.file_exists (dir ^/ prog) then Some prog else None)
with
| None -> None
| Some (dir, prog) -> (
let path = dir ^/ prog in
let args = Array.of_list @@ (path :: args) in
let ic, oc, ec =
Unix.open_process_args_full path args (Unix.environment ())
in
let line =
match input_line ic with
| s -> Some s
| exception End_of_file -> None
in
match (Unix.close_process_full (ic, oc, ec), line) with
| WEXITED 0, Some s -> Some s
| _ -> None)
in
match concurrency with
| Some n -> n
Expand All @@ -218,7 +238,10 @@ let concurrency =
| n -> n)
else
let commands =
[ "nproc"; "getconf _NPROCESSORS_ONLN"; "getconf NPROCESSORS_ONLN" ]
[ ("nproc", [])
; ("getconf", [ "_NPROCESSORS_ONLN" ])
; ("getconf", [ "NPROCESSORS_ONLN" ])
]
in
let rec loop = function
| [] -> 1
Expand Down Expand Up @@ -546,15 +569,7 @@ end = struct
let fn = dir ^/ prog ^ exe in
if Sys.file_exists fn then Some fn else None

let find_prog prog =
let rec search = function
| [] -> None
| dir :: rest -> (
match best_prog dir prog with
| None -> search rest
| Some fn -> Some (dir, fn))
in
search path
let find_prog prog = find_prog ~f:(fun dir -> best_prog dir prog)

let get_prog dir prog =
match best_prog dir prog with
Expand Down

0 comments on commit 64cde9f

Please sign in to comment.