Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(pkg): Update our vendored OPAM copy #8351

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/dune_pkg/sys_poll.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ let arch ~path =
match Sys.os_type with
| "Unix" | "Cygwin" -> uname ~path [ "-m" ]
| "Win32" ->
if Sys.word_size = 32 && not (OpamStubs.isWoW64 ())
then Fiber.return @@ Some "i686"
else Fiber.return @@ Some "x86_64"
Fiber.return
@@
(match OpamStubs.getArchitecture () with
| OpamStubs.AMD64 -> Some "x86_64"
| ARM -> Some "arm32"
| ARM64 -> Some "arm64"
| IA64 -> Some "ia64"
| Intel -> Some "x86_32"
| Unknown -> None)
| _ -> Fiber.return None
in
match raw with
Expand Down
90 changes: 90 additions & 0 deletions vendor/opam/src/core/opamCompat.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,93 @@ module Lazy = struct

include Stdlib.Lazy
end

module Filename = struct
[@@@warning "-32"]

let quote s =
let l = String.length s in
let b = Buffer.create (l + 20) in
Buffer.add_char b '\"';
let rec loop i =
if i = l then Buffer.add_char b '\"' else
match s.[i] with
| '\"' -> loop_bs 0 i;
| '\\' -> loop_bs 0 i;
| c -> Buffer.add_char b c; loop (i+1);
and loop_bs n i =
if i = l then begin
Buffer.add_char b '\"';
add_bs n;
end else begin
match s.[i] with
| '\"' -> add_bs (2*n+1); Buffer.add_char b '\"'; loop (i+1);
| '\\' -> loop_bs (n+1) (i+1);
| _ -> add_bs n; loop i
end
and add_bs n = for _j = 1 to n do Buffer.add_char b '\\'; done
in
loop 0;
Buffer.contents b

(*
Quoting commands for execution by cmd.exe is difficult.
1- Each argument is first quoted using the "quote" function above, to
protect it against the processing performed by the C runtime system,
then cmd.exe's special characters are escaped with '^', using
the "quote_cmd" function below. For more details, see
https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23
2- The command and the redirection files, if any, must be double-quoted
in case they contain spaces. This quoting is interpreted by cmd.exe,
not by the C runtime system, hence the "quote" function above
cannot be used. The two characters we don't know how to quote
inside a double-quoted cmd.exe string are double-quote and percent.
We just fail if the command name or the redirection file names
contain a double quote (not allowed in Windows file names, anyway)
or a percent. See function "quote_cmd_filename" below.
3- The whole string passed to Sys.command is then enclosed in double
quotes, which are immediately stripped by cmd.exe. Otherwise,
some of the double quotes from step 2 above can be misparsed.
See e.g. https://stackoverflow.com/a/9965141
*)
let quote_cmd s =
let b = Buffer.create (String.length s + 20) in
String.iter
(fun c ->
match c with
| '(' | ')' | '!' | '^' | '%' | '\"' | '<' | '>' | '&' | '|' ->
Buffer.add_char b '^'; Buffer.add_char b c
| _ ->
Buffer.add_char b c)
s;
Buffer.contents b

let quote_cmd_filename f =
if String.contains f '\"' || String.contains f '%' then
failwith ("Filename.quote_command: bad file name " ^ f)
else if String.contains f ' ' then
"\"" ^ f ^ "\""
else
f
(* Redirections in cmd.exe: see https://ss64.com/nt/syntax-redirection.html
and https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490982(v=technet.10)
*)

(** NOTE: OCaml >= 4.10 *)
let quote_command cmd ?stdin ?stdout ?stderr args =
String.concat "" [
"\"";
quote_cmd_filename cmd;
" ";
quote_cmd (String.concat " " (List.map quote args));
(match stdin with None -> "" | Some f -> " <" ^ quote_cmd_filename f);
(match stdout with None -> "" | Some f -> " >" ^ quote_cmd_filename f);
(match stderr with None -> "" | Some f ->
if stderr = stdout
then " 2>&1"
else " 2>" ^ quote_cmd_filename f);
"\""
]

include Stdlib.Filename
end
8 changes: 8 additions & 0 deletions vendor/opam/src/core/opamCompat.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ module Unix : sig
implementation with double chdir otherwise *)
val realpath: string -> string
end

module Filename: sig
(** NOTE: OCaml >= 4.10 *)

val quote_command :
string -> ?stdin:string -> ?stdout:string -> ?stderr:string
-> string list -> string
end
6 changes: 6 additions & 0 deletions vendor/opam/src/core/opamCoreConfig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type t = {
errlog_length: int;
merged_output: bool;
precise_tracking: bool;
cygbin: string option;
set: bool;
}

Expand All @@ -81,6 +82,7 @@ type 'a options_fun =
?errlog_length:int ->
?merged_output:bool ->
?precise_tracking:bool ->
?cygbin:string ->
'a

let default = {
Expand All @@ -101,6 +103,7 @@ let default = {
errlog_length = 12;
merged_output = true;
precise_tracking = false;
cygbin = None;
set = false;
}

Expand All @@ -119,6 +122,7 @@ let setk k t
?errlog_length
?merged_output
?precise_tracking
?cygbin
=
let (+) x opt = match opt with Some x -> x | None -> x in
k {
Expand All @@ -139,6 +143,7 @@ let setk k t
errlog_length = t.errlog_length + errlog_length;
merged_output = t.merged_output + merged_output;
precise_tracking = t.precise_tracking + precise_tracking;
cygbin = (match cygbin with Some _ -> cygbin | None -> t.cygbin);
set = true;
}

Expand Down Expand Up @@ -179,6 +184,7 @@ let initk k =
?errlog_length:(E.errloglen ())
?merged_output:(E.mergeout ())
?precise_tracking:(E.precisetracking ())
?cygbin:None

let init ?noop:_ = initk (fun () -> ())

Expand Down
2 changes: 2 additions & 0 deletions vendor/opam/src/core/opamCoreConfig.mli
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type t = private {
precise_tracking : bool;
(** If set, will take full md5 of all files when checking diffs (to track
installations), rather than rely on just file size and mtime *)
cygbin: string option;
set : bool;
(** Options have not yet been initialised (i.e. defaults are active) *)
}
Expand All @@ -90,6 +91,7 @@ type 'a options_fun =
?errlog_length:int ->
?merged_output:bool ->
?precise_tracking:bool ->
?cygbin:string ->
'a

val default : t
Expand Down
6 changes: 2 additions & 4 deletions vendor/opam/src/core/opamFilename.ml
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ let exec dirname ?env ?name ?metadata ?keep_going cmds =
(fun () -> OpamSystem.commands ?env ?name ?metadata ?keep_going cmds)

let move_dir ~src ~dst =
OpamSystem.command ~verbose:(OpamSystem.verbose_for_base_commands ())
[ "mv"; Dir.to_string src; Dir.to_string dst ]
OpamSystem.mv (Dir.to_string src) (Dir.to_string dst)

let opt_dir dirname =
if exists_dir dirname then Some dirname else None
Expand Down Expand Up @@ -292,8 +291,7 @@ let install ?warning ?exec ~src ~dst () =

let move ~src ~dst =
if src <> dst then
OpamSystem.command ~verbose:(OpamSystem.verbose_for_base_commands ())
[ "mv"; to_string src; to_string dst ]
OpamSystem.mv (to_string src) (to_string dst)

let readlink src =
if exists src then
Expand Down
35 changes: 19 additions & 16 deletions vendor/opam/src/core/opamProcess.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ module Re = Dune_re
let log ?level fmt =
OpamConsole.log "PROC" ?level fmt

let default_env =
let f () = lazy (
match OpamCoreConfig.(!r.cygbin) with
| Some cygbin -> OpamStd.Env.cyg_env cygbin
| None -> OpamStd.Env.raw_env ()
) in
fun () -> Lazy.force (f ())

let cygwin_create_process_env prog args env fd1 fd2 fd3 =
(*
* Unix.create_process_env correctly converts arguments to a command line for
Expand Down Expand Up @@ -174,8 +182,7 @@ let cygwin_create_process_env prog args env fd1 fd2 fd3 =
List.rev_append prefix (dir::(List.rev_append suffix dirs))
end else
f prefix (dir::suffix) dirs
| [] ->
assert false
| [] -> []
in
Some (key ^ "=" ^ String.concat ";" (f [] [] path_dirs))
| _ ->
Expand Down Expand Up @@ -311,11 +318,13 @@ let resolve_command cmd = !resolve_command_fn cmd
let create_process_env =
if Sys.win32 then
fun cmd ->
let resolved_cmd = resolve_command cmd in
if OpamStd.(Option.map_default Sys.is_cygwin_variant `Native resolved_cmd) = `Cygwin then
if OpamStd.Option.map_default
(OpamStd.Sys.is_cygwin_variant
~cygbin:(OpamCoreConfig.(!r.cygbin)))
false
(resolve_command cmd) then
cygwin_create_process_env cmd
else
Unix.create_process_env cmd
else Unix.create_process_env cmd
else
Unix.create_process_env

Expand All @@ -341,7 +350,7 @@ let create ?info_file ?env_file ?(allow_stdin=not Sys.win32) ?stdout_file ?stder
| None -> (fun f -> f ())
| Some dir ->
Unix.chdir dir;
Fun.protect ~finally:(fun () -> Unix.chdir oldcwd)
OpamStd.Exn.finally (fun () -> Unix.chdir oldcwd)
in
with_chdir dir @@ fun () ->
let stdin_fd,close_stdin =
Expand All @@ -360,7 +369,7 @@ let create ?info_file ?env_file ?(allow_stdin=not Sys.win32) ?stdout_file ?stder
else tee f
in
let env = match env with
| None -> Unix.environment ()
| None -> default_env ()
| Some e -> e in
let time = Unix.gettimeofday () in

Expand Down Expand Up @@ -443,14 +452,8 @@ let create ?info_file ?env_file ?(allow_stdin=not Sys.win32) ?stdout_file ?stder
cmd, args
else
cmd, args in
let create_process, cmd, args =
if Sys.win32 && OpamStd.Sys.is_cygwin_variant cmd = `Cygwin then
cygwin_create_process_env, cmd, args
else
Unix.create_process_env, cmd, args
in
try
create_process
create_process_env
cmd
(Array.of_list (cmd :: args))
env
Expand Down Expand Up @@ -529,7 +532,7 @@ let run_background command =
in
let verbose = is_verbose_command command in
let allow_stdin = OpamStd.Option.default false allow_stdin in
let env = match env with Some e -> e | None -> Unix.environment () in
let env = match env with Some e -> e | None -> default_env () in
let file ext = match name with
| None -> None
| Some n ->
Expand Down
2 changes: 2 additions & 0 deletions vendor/opam/src/core/opamProcess.mli
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,5 @@ val create_process_env :
string -> string array -> string array ->
Unix.file_descr -> Unix.file_descr -> Unix.file_descr ->
int

val default_env : unit -> string array
Loading