From e4805b48ed4766c11cc97f653c8c3e59e88cf26d Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 15:19:36 +0900 Subject: [PATCH 01/20] deleted: pa_opt310.ml --- pa_opt310.ml | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 pa_opt310.ml diff --git a/pa_opt310.ml b/pa_opt310.ml deleted file mode 100644 index 0789e3f..0000000 --- a/pa_opt310.ml +++ /dev/null @@ -1,16 +0,0 @@ -(* - Public Domain. Use at your own risk! - Author: Martin Jambon -*) - -open Camlp4.PreCast - -EXTEND Gram - Syntax.expr: LEVEL "top" [ - [ id = LIDENT; "??"; e = Syntax.expr LEVEL "top" -> - <:expr< - match $lid:id$ with - [ None -> () - | Some $lid:id$ -> $e$ ] >> ] - ]; -END From 4f3a15d47810eb0167135e83d8f9ba8f52144fab Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 15:23:39 +0900 Subject: [PATCH 02/20] rm trailing whitespaces --- common.ml | 2 +- main.ml | 110 +++++++++++++++++++++++++++--------------------------- ocaml.ml | 38 +++++++++---------- 3 files changed, 75 insertions(+), 75 deletions(-) diff --git a/common.ml b/common.ml index e26249c..1c4b646 100644 --- a/common.ml +++ b/common.ml @@ -2,5 +2,5 @@ let extra_args = ref ([] : string list) let trash = ref ([] : string list) let verbose = ref false let script_dir = ref (Sys.getcwd ()) -let compile : (string -> string -> int) ref = +let compile : (string -> string -> int) ref = ref (fun source result -> failwith "Compile.compile is unset") diff --git a/main.ml b/main.ml index f09a964..aeee893 100644 --- a/main.ml +++ b/main.ml @@ -1,4 +1,4 @@ -(* +(* ocamlscript: a utility to have shell-like optimised scripts in OCaml Copyright 2005 David MENTRE @@ -23,7 +23,7 @@ struct let from = ref (None : [`File of string | `String of string | `Stdin] option) let o = ref None (* choose a name for the compiled executable *) let vm = ref None (* possible bytecode interpreter *) - let extra_args = ref [] + let extra_args = ref [] let help_message = "\ Usage: ocamlscript [ PACKED_OPTIONS [ OPTIONS ] [ -- ] [SCRIPTNAME] [ARGS] ] @@ -48,24 +48,24 @@ A typical self-executable script looks as follows: Structure of the command line: PACKED_OPTIONS: - the first argument of ocamlscript. It is either unpacked into - several arguments that are passed to ocamlscript or into a script name - if this name doesn't start with \"-\". Double-quotes can be used - to enclose arguments that contain whitespace or double-quotes. - Double-quotes must be doubled. For instance, the following + the first argument of ocamlscript. It is either unpacked into + several arguments that are passed to ocamlscript or into a script name + if this name doesn't start with \"-\". Double-quotes can be used + to enclose arguments that contain whitespace or double-quotes. + Double-quotes must be doubled. For instance, the following self-executable script would be compiled into an executable named Hello \"World\": #!/usr/bin/ocamlscript -o \"Hello \"\"World\"\"\" print_endline \"Hello \"World\"\" - - Important note: on some Unix systems, the whole - '-o \"Hello \"\"World\"\"\"' string is passed as a single argument - to ocamlscript. This is why the first argument must be unpacked, + + Important note: on some Unix systems, the whole + '-o \"Hello \"\"World\"\"\"' string is passed as a single argument + to ocamlscript. This is why the first argument must be unpacked, even if ocamlscript is called explicitely from the command line. -OPTIONS: +OPTIONS: any number of arguments in this section are treated like options - to ocamlscript until a either a non-option is encountered, which is + to ocamlscript until a either a non-option is encountered, which is understood as the script name (SCRIPTNAME) or \"--\" which stops the list of arguments that are passed to ocamlscript. @@ -74,19 +74,19 @@ Ocamlscript supports the following options: -help displays a help message and exit --help same as -help -c compile only - -o EXEC_NAME specify a name for the executable + -o EXEC_NAME specify a name for the executable (required if the program is not read from a file) -e PROGRAM execute the code given here instead of reading it from a file -f force recompilation which is otherwise based on last modification dates -debug print messages about what ocamlscript is doing -version prints the version identifier to stdout and exit - read program from stdin instead of a file - -vm VIRTUAL_MACHINE run the executable using this virtual machine (e.g. + -vm VIRTUAL_MACHINE run the executable using this virtual machine (e.g. ocamlrun) \"--\": passed as an argument to ocamlscript in the PACKED_OPTIONS argument or in the OPTIONS argument marks the end of the arguments that - are passed to ocamlscript. Arguments that follow will be + are passed to ocamlscript. Arguments that follow will be interpreted as arguments of the script. Arguments that follow \"--\" in the PACKED_OPTIONS argument will be passed as arguments to the final executable. The first @@ -101,7 +101,7 @@ ocamlscript's website (http://martin.jambon.free.fr/ocamlscript.html). " end -(* more generic than .opt since we can compile with other commands than +(* more generic than .opt since we can compile with other commands than ocamlopt *) let bin_suffix = ".exe" @@ -129,7 +129,7 @@ let endline = if Sys.os_type = "Win32" then "\r\n" else "\n" let output_line oc s = output_string oc s; output_string oc endline (* based on last modification date only. - Doesn't handle dependencies toward runtime things that might change + Doesn't handle dependencies toward runtime things that might change incompatibly (DLLs, bytecode version, ...). ocamlscript -f can be used to force recompilation in these cases. *) let needs_recompile ?log = function @@ -165,7 +165,7 @@ let process_directives lines = let style = ref `Ocaml in let ocaml_lines = List.map - (function + (function / "#" blank* "locstyle" blank* (lower ['_'alnum*] as x) blank* ";;"? blank* eol / as s -> style := read_locstyle x; @@ -206,7 +206,7 @@ let process_directives = (* let split_lines lines = let lines1, lines2 = split_list [] is_sep lines in - let pos1, header = + let pos1, header = match lines1 with / "#!" / :: header -> (2, header) | _ -> (1, lines1) in @@ -225,7 +225,7 @@ let split_lines lines = match lines1 with s :: header when test s -> 2, header | _ -> 1, lines1 in - let pos2 = List.length lines1 + 2 in + let pos2 = List.length lines1 + 2 in (pos1, header, pos2, lines2) let get_dir file = @@ -238,15 +238,15 @@ let write_header ~pos ~source ~source_option ~verbose ~prog_file lines = let extra_args = match !Opt.extra_args with [] -> "" - | l -> + | l -> sprintf "Ocamlscript.Common.extra_args := [ %s];;\n" (String.concat "; " (List.map (fun s -> sprintf "%S" s) l)) in let trash, script_dir = match source_option with - `Stdin + `Stdin | `String _ -> (sprintf "Ocamlscript.Common.trash := \ %S :: !Ocamlscript.Common.trash;;\n" - bin, + bin, Sys.getcwd ()) | `File script_name -> "", get_dir script_name in @@ -267,7 +267,7 @@ open Ocamlscript;; open Utils;; #%i %S;;\n" pos source verbose script_dir extra_args trash pos source; - + List.iter (output_line oc) lines; fprintf oc "\ @@ -289,7 +289,7 @@ let write_body ~pos ~source ~locstyle lines = module Text = struct exception Internal_exit - + let iter_lines_of_channel f ic = try while true do @@ -299,10 +299,10 @@ struct f line done with Internal_exit -> () - + let iter_lines_of_file f file = let ic = open_in file in - try + try iter_lines_of_channel f ic; close_in ic with exn -> @@ -324,7 +324,7 @@ end let split_file = let newline = Str.regexp "\r?\n" in fun ?log source_option -> - let source, lines = + let source, lines = match source_option with `Stdin -> "", Text.lines_of_channel stdin | `String s -> "", (Str.split newline) s @@ -332,13 +332,13 @@ let split_file = let pos1, unprocessed_header, pos2, body = split_lines lines in let header, locstyle = process_directives unprocessed_header in - + let verbose = if log = None then "false" else "true" in - + let prog_file = write_body ~pos:pos2 ~source ~locstyle body in let meta_file = - write_header + write_header ~pos:pos1 ~source ~source_option ~verbose ~prog_file header in (meta_file, prog_file) @@ -361,14 +361,14 @@ let option0 ?(refuse_input = false) x = let result = ref `Yes in (match x with "--" -> result := `Stop - | "-help" + | "-help" | "--help" -> Opt.help := true | "-c" -> Opt.c := true | "-f" -> Opt.f := true | "-debug" -> Opt.debug := true | "-version" -> Opt.version := true - | "-" -> - if refuse_input then + | "-" -> + if refuse_input then failwith "option - is disabled in this context" else Opt.set "source" Opt.from `Stdin @@ -380,8 +380,8 @@ let option1 ?(refuse_input = false) x y = (match x with "-o" -> Opt.set "executable name" Opt.o y | "-vm" -> Opt.set "virtual machine" Opt.vm y - | "-e" -> - if refuse_input then + | "-e" -> + if refuse_input then failwith "option -e is disabled in this context" else Opt.set "source" Opt.from (`String y) @@ -404,17 +404,17 @@ let other_arg x = Opt.extra_args := x :: !Opt.extra_args -let process_ocamlscript_args ?refuse_input ?(accept_non_option = false) l = +let process_ocamlscript_args ?refuse_input ?(accept_non_option = false) l = let rec loop = function - x :: rest as l -> + x :: rest as l -> begin match option0 x with `Stop -> (None, true, rest) | `Yes -> loop rest - | `No -> + | `No -> match l with x :: y :: rest when option1 ?refuse_input x y -> loop rest - | x :: rest -> + | x :: rest -> if start_option1 x then (Some x, false, rest) else if optionx x then @@ -440,8 +440,8 @@ let unquote s = Buffer.contents buf (* -let tokenize_args = - COLLECT '"' (([^'"']|"\"\"")* as x := unquote) '"' +let tokenize_args = + COLLECT '"' (([^'"']|"\"\"")* as x := unquote) '"' | ([^space '"']+ as x) -> x *) (* @@ -451,8 +451,8 @@ let tokenize_args = let token = Str.regexp "\"\\([^\"]\\|\"\"\\)*\"\\|[^ \"]+" in fun s -> List.fold_right - (fun x accu -> - match x with + (fun x accu -> + match x with Str.Delim s -> (if s <> "" && s.[0] = '"' then unquote (String.sub s 1 (String.length s - 2)) @@ -465,11 +465,11 @@ let guess_arg1 s = match tokenize_args s with [s'] when String.length s' >= 1 && s'.[0] <> '-' -> `Script_name | l -> - `Ocamlscript_args (process_ocamlscript_args + `Ocamlscript_args (process_ocamlscript_args ~refuse_input:true ~accept_non_option:true l) -(* name of Sys.argv.(0) in the final process (execution of the binary) +(* name of Sys.argv.(0) in the final process (execution of the binary) depending on where the source program comes from: - from a file: the name of the source file - from stdin: @@ -487,17 +487,17 @@ let guess_arg1 s = let main () = let script_path_option, script_args = match Array.to_list Sys.argv with - ocamlscript :: (arg1 :: other_args as l) -> + ocamlscript :: (arg1 :: other_args as l) -> (match guess_arg1 arg1 with `Script_name -> (`File (absolute arg1), l) | `Ocamlscript_args (opt1, stopped, hardcoded_script_args) -> - let command_line_script_args = + let command_line_script_args = let continued_args = match opt1 with None -> other_args | Some o1 -> o1 :: other_args in if stopped then continued_args - else + else let opt1', stopped', command_line_script_args = process_ocamlscript_args continued_args in (match opt1' with @@ -507,11 +507,11 @@ let main () = command_line_script_args in match !Opt.from with Some `Stdin -> - (`Stdin, + (`Stdin, "" :: (hardcoded_script_args @ command_line_script_args)) | Some (`String s) -> - (`String s, + (`String s, "-e" :: (hardcoded_script_args @ command_line_script_args)) | Some (`File s) -> assert false @@ -521,11 +521,11 @@ let main () = Opt.set "source" Opt.from `Stdin; (`Stdin, "" :: hardcoded_script_args) - | script_name :: l -> + | script_name :: l -> Opt.set "source" Opt.from (`File script_name); - (`File (absolute script_name), + (`File (absolute script_name), script_name :: (hardcoded_script_args @ l))) - | [_] | [] -> + | [_] | [] -> Opt.set "source" Opt.from `Stdin; (`Stdin, [""]) in diff --git a/ocaml.ml b/ocaml.ml index 1252c0b..e1bdba3 100644 --- a/ocaml.ml +++ b/ocaml.ml @@ -18,7 +18,7 @@ let use_camlp4 = ref true (* by default camlp4 is used *) let use_ocamlc = ref false (* by default we want native code *) let use_ocamlfind = ref false (* used only if necessary *) let revised = ref false (* use this to use the revised syntax *) -let ocamlflags = Common.extra_args +let ocamlflags = Common.extra_args (* any options that you may want to pass to ocamlopt *) let ppopt = ref [] (* any options that you may want to pass @@ -33,15 +33,15 @@ let ppsrcloc = ref None (* non-standard source location generator *) let exe s = match Sys.os_type with - "Win32" | "Cygwin"-> + "Win32" | "Cygwin"-> if Filename.check_suffix s ".exe" then s else s ^ ".exe" | "Unix" | _ -> s -let import path = +let import path = let src = !+ path in let dst = Filename.basename src in - let head = + let head = match !pp, !ppsrcloc, !revised with Some _, Some f, _ -> f src | _, _, false -> sprintf "#1 %S;;\n" src @@ -53,7 +53,7 @@ let ocamllex_command input = if !use_ocamllex then Some ((fun () -> ()), new_cmd [!ocamllex; input; - "-o"; "ocamlscript_ocamllex_out.ml"; "-q"], + "-o"; "ocamlscript_ocamllex_out.ml"; "-q"], "ocamlscript_ocamllex_out.ml") else None *) @@ -63,7 +63,7 @@ let file_kind file = else if Filename.check_suffix file ".ml" then `Ml else if Filename.check_suffix file ".mll" then `Mll else if Filename.check_suffix file ".mly" then `Mly - else + else try let prefix = Filename.chop_extension file in let len = String.length file - String.length prefix in @@ -74,14 +74,14 @@ let file_kind file = let extra_command file = match file_kind file with `Mli | `Ml -> ([], [file]) - | `Mll -> + | `Mll -> ([command [!ocamllex; file; "-q"]], [(Filename.chop_extension file) ^ ".ml"]) - | `Mly -> + | `Mly -> let p = Filename.chop_extension file in ([command [!ocamlyacc; file]], [p ^ ".mli"; p ^ ".ml"]) - | `Ext s -> + | `Ext s -> failwith (sprintf "don't know how to handle %s files: %s" s file) | `Unknown -> failwith (sprintf "don't know how to handle this file: %s" file) @@ -114,29 +114,29 @@ let ocaml_command input = else if !use_ocamlc then [!ocamlc] else [!ocamlopt] in - + let flags = !ocamlflags in let camlp4_stuff = if !use_camlp4 then let syntax, camlp4 = if !revised then "camlp4r", !camlp4r else "camlp4o", !camlp4o in - let ppoptions = + let ppoptions = if !ppopt = [] then [] - else + else if really_use_ocamlfind then List.flatten (List.map (fun s -> ["-ppopt"; s]) !ppopt) else !ppopt in if really_use_ocamlfind then "-syntax" :: syntax :: ppoptions - else + else let space = function "" -> "" | s -> " " ^ s in ["-pp"; sprintf "'%s%s'" camlp4 (space (String.concat " " ppoptions))] else [] in let packages = if really_use_ocamlfind then ["-linkpkg"; "-package"; - String.concat "," + String.concat "," (if !use_camlp4 && not (List.mem "camlp4" !packs) then "camlp4" :: !packs else !packs) ] @@ -145,19 +145,19 @@ let ocaml_command input = let extra_sources = !sources in let init () = List.iter import extra_sources in let all_sources = extra_sources @ [input] in - + let xcommands, all_ml_files = extra_commands all_sources in - let args = compiler @ "-o" :: "prog" :: + let args = compiler @ "-o" :: "prog" :: flags @ camlp4_stuff @ packages @ all_ml_files in (init, xcommands, command args, exe "prog") -let compile source result = - let internal_input = +let compile source result = + let internal_input = if !use_ocamllex then "ocamlscript_main.mll" else "ocamlscript_main.ml" in - + let before, xcommands, main_command, internal_output = ocaml_command internal_input in let input = [internal_input, source] in From 0eb735214c70d6eb9dcd63c8cf54f2fa5aeba218 Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 15:30:56 +0900 Subject: [PATCH 03/20] Pipeline: indent whole file plus add missing '|' on each first pattern matched --- pipeline.ml | 126 ++++++++++++++++++++++++++-------------------------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/pipeline.ml b/pipeline.ml index af33c47..c3cdf37 100644 --- a/pipeline.ml +++ b/pipeline.ml @@ -30,7 +30,7 @@ let prng = Random.State.make_self_init ();; let temporary_directory = match Sys.os_type with - "Unix" | "Cygwin" -> (try Sys.getenv "TMPDIR" with Not_found -> "/tmp") + | "Unix" | "Cygwin" -> (try Sys.getenv "TMPDIR" with Not_found -> "/tmp") | "Win32" -> (try Sys.getenv "TEMP" with Not_found -> ".") | _ -> assert false @@ -60,18 +60,18 @@ let rec remove ?log file = try let st = stat file in match st.st_kind with - S_DIR -> - Array.iter (fun name -> remove (file // name)) (Sys.readdir file); - maybe_log log "remove directory %S\n%!" file; - rmdir file - | S_REG - | S_CHR - | S_BLK - | S_LNK - | S_FIFO - | S_SOCK -> - maybe_log log "remove file %S\n%!" file; - Sys.remove file + | S_DIR -> + Array.iter (fun name -> remove (file // name)) (Sys.readdir file); + maybe_log log "remove directory %S\n%!" file; + rmdir file + | S_REG + | S_CHR + | S_BLK + | S_LNK + | S_FIFO + | S_SOCK -> + maybe_log log "remove file %S\n%!" file; + Sys.remove file with e -> () @@ -79,14 +79,14 @@ let rec remove ?log file = let array_command ?stdin ?stdout prog args = let real_stdin, close_stdin = match stdin with - None -> Unix.stdin, false - | Some file -> Unix.openfile file [Unix.O_RDONLY] 0, true in + | None -> Unix.stdin, false + | Some file -> Unix.openfile file [Unix.O_RDONLY] 0, true in let real_stdout, close_stdout = match stdout with - None -> Unix.stdout, false - | Some file -> - Unix.openfile file - [Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC] 0o600, true in + | None -> Unix.stdout, false + | Some file -> + Unix.openfile file + [Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC] 0o600, true in let pid = Unix.create_process prog args real_stdin real_stdout Unix.stderr in let pid', process_status = Unix.waitpid [] pid in assert (pid = pid'); @@ -95,26 +95,26 @@ let array_command ?stdin ?stdout prog args = if close_stdout then (try Unix.close real_stdout with _ -> ()); match process_status with - Unix.WEXITED n -> n - | Unix.WSIGNALED _ -> 2 (* like OCaml's uncaught exceptions *) - | Unix.WSTOPPED _ -> - (* only possible if the call was done using WUNTRACED - or when the child is being traced *) - assert false + | Unix.WEXITED n -> n + | Unix.WSIGNALED _ -> 2 (* like OCaml's uncaught exceptions *) + | Unix.WSTOPPED _ -> + (* only possible if the call was done using WUNTRACED + or when the child is being traced *) + assert false let concat_cmd cmd = String.concat " " cmd.args let run_command ?log cmd = match cmd.args with - [] -> - maybe_log log "empty command\n%!"; 0 - | prog :: _ -> - maybe_log log "%s: %s\n%!" prog (concat_cmd cmd); - let status = - array_command ?stdin:cmd.stdin ?stdout:cmd.stdout - prog (Array.of_list cmd.args) in - maybe_log log "exit status %i\n%!" status; - status + | [] -> + maybe_log log "empty command\n%!"; 0 + | prog :: _ -> + maybe_log log "%s: %s\n%!" prog (concat_cmd cmd); + let status = + array_command ?stdin:cmd.stdin ?stdout:cmd.stdout + prog (Array.of_list cmd.args) in + maybe_log log "exit status %i\n%!" status; + status let exec ?log cmd = maybe_log log "%s\n%!" (concat_cmd cmd); @@ -149,7 +149,7 @@ let copy_file ?log ?(head = "") ?(tail = "") ?(force = false) src dst = let copy_files ?log ?force src dst l = List.iter (fun (src_name, dst_name) -> - copy_file ?log ?force (src @@ src_name) (dst @@ dst_name)) + copy_file ?log ?force (src @@ src_name) (dst @@ dst_name)) l let match_files names settings = @@ -158,43 +158,43 @@ let match_files names settings = List.iter (fun (id, s) -> Hashtbl.replace tbl id (Some s)) settings; let pairs = Hashtbl.fold (fun id opt l -> - let x = - match opt with - None -> (id, id) - | Some s -> (id, s) in - x :: l) tbl [] in + let x = + match opt with + | None -> (id, id) + | Some s -> (id, s) in + x :: l) tbl [] in pairs let flip (a, b) = (b, a) let run ?log - ?(before = fun () -> ()) ?(after = fun () -> ()) - ?(input = []) ?(output = []) p = + ?(before = fun () -> ()) ?(after = fun () -> ()) + ?(input = []) ?(output = []) p = let rec loop l = match l with - [] -> 0 - | cmd :: rest when cmd.stdin = None && cmd.stdout = None -> - (try - let status = exec ?log cmd in - if status = 0 then loop rest - else status - with _ -> 127) - | _ -> failwith "IO redirections: not implemented" in + | [] -> 0 + | cmd :: rest when cmd.stdin = None && cmd.stdout = None -> + (try + let status = exec ?log cmd in + if status = 0 then loop rest + else status + with _ -> 127) + | _ -> failwith "IO redirections: not implemented" in let dir = temp_dir "ocamlpipeline" "" in Fun.protect (fun () -> - let base = Sys.getcwd () in - maybe_log log "change directory %S\n%!" dir; - Sys.chdir dir; - before (); - copy_files ?log base dir (List.map flip (match_files p.input input)); - let status = loop p.commands in - maybe_log log "change directory %S\n%!" base; - after (); - Sys.chdir base; - maybe_log log "command pipeline exits with status %i\n%!" status; - if status = 0 then - copy_files ?log ~force:true dir base (match_files p.output output); - status + let base = Sys.getcwd () in + maybe_log log "change directory %S\n%!" dir; + Sys.chdir dir; + before (); + copy_files ?log base dir (List.map flip (match_files p.input input)); + let status = loop p.commands in + maybe_log log "change directory %S\n%!" base; + after (); + Sys.chdir base; + maybe_log log "command pipeline exits with status %i\n%!" status; + if status = 0 then + copy_files ?log ~force:true dir base (match_files p.output output); + status ) ~finally:(fun () -> remove ?log dir) From d526fc6804fcac75e75d216bf2b6db81c901a3f8 Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 15:38:57 +0900 Subject: [PATCH 04/20] Main: indent whole file add missing '|' on first pattern matched --- main.ml | 345 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 171 insertions(+), 174 deletions(-) diff --git a/main.ml b/main.ml index aeee893..288526a 100644 --- a/main.ml +++ b/main.ml @@ -12,8 +12,8 @@ module Opt = struct let set what opt s = match !opt with - Some _ -> failwith (sprintf "%s is already set" what) - | None -> opt := Some s + | Some _ -> failwith (sprintf "%s is already set" what) + | None -> opt := Some s let help = ref false (* help and exit *) let c = ref false (* compile only *) @@ -107,20 +107,20 @@ let bin_suffix = ".exe" let bin_name src = match !Opt.o with - Some s -> s - | None -> src ^ bin_suffix + | Some s -> s + | None -> src ^ bin_suffix let obin_name src = match !Opt.o with - Some s -> s - | None -> - match src with - `Stdin | `String _ -> failwith "please specify a name \ - for the executable with -o" - | `File s -> s ^ bin_suffix + | Some s -> s + | None -> + match src with + | `Stdin | `String _ -> failwith "please specify a name \ + for the executable with -o" + | `File s -> s ^ bin_suffix let source_name = function - None -> assert false + | None -> assert false | Some s -> s let ( // ) = Filename.concat @@ -133,11 +133,11 @@ let output_line oc s = output_string oc s; output_string oc endline incompatibly (DLLs, bytecode version, ...). ocamlscript -f can be used to force recompilation in these cases. *) let needs_recompile ?log = function - `Stdin | `String _ -> true + | `Stdin | `String _ -> true | `File source -> - let bin = bin_name source in - not (Sys.file_exists bin) || - (Unix.stat bin).Unix.st_mtime <= (Unix.stat source).Unix.st_mtime + let bin = bin_name source in + not (Sys.file_exists bin) || + (Unix.stat bin).Unix.st_mtime <= (Unix.stat source).Unix.st_mtime (* RE sep = "--" blank* eos @@ -149,31 +149,30 @@ let sep = Str.regexp "--[\t ]*$" let is_sep s = Str.string_match sep s 0 let rec split_list accu is_sep = function - [] -> `One (List.rev accu) + | [] -> `One (List.rev accu) | hd :: tl -> - if is_sep hd then `Two (List.rev accu, tl) - else split_list (hd :: accu) is_sep tl + if is_sep hd then `Two (List.rev accu, tl) + else split_list (hd :: accu) is_sep tl let read_locstyle = function - "ocaml" -> `Ocaml + | "ocaml" -> `Ocaml | "blank" -> `Blank | "none" -> `None | _ -> failwith "invalid locstyle directive" -(* -let process_directives lines = - let style = ref `Ocaml in - let ocaml_lines = - List.map - (function - / "#" blank* "locstyle" blank* - (lower ['_'alnum*] as x) blank* ";;"? blank* eol / as s -> - style := read_locstyle x; - String.make (String.length s) ' ' - | s -> s) - lines in - (ocaml_lines, !style) -*) +(* let process_directives lines = + * let style = ref `Ocaml in + * let ocaml_lines = + * List.map + * (function + * | / "#" blank* "locstyle" blank* + * (lower ['_'alnum*] as x) blank* ";;"? blank* eol / as s -> + * style := read_locstyle x; + * String.make (String.length s) ' ' + * | s -> s) + * lines in + * (ocaml_lines, !style) *) + let process_directives = let micmatch_1 = Str.regexp @@ -187,44 +186,42 @@ let process_directives = let micmatch_match_target_1 = micmatch_any_target in (try match micmatch_match_target_1 with - micmatch_1_target as s when true -> - if Str.string_match micmatch_1 micmatch_1_target 0 then - let x = Str.matched_group 1 micmatch_1_target in - fun () -> - style := read_locstyle x; - String.make (String.length s) ' ' - else raise Exit + | micmatch_1_target as s when true -> + if Str.string_match micmatch_1 micmatch_1_target 0 then + let x = Str.matched_group 1 micmatch_1_target in + fun () -> + style := read_locstyle x; + String.make (String.length s) ' ' + else raise Exit | _ -> raise Exit with Exit -> - let s = micmatch_match_target_1 in fun () -> s) + let s = micmatch_match_target_1 in fun () -> s) ()) lines in ocaml_lines, !style -(* -let split_lines lines = - let lines1, lines2 = split_list [] is_sep lines in - let pos1, header = - match lines1 with - / "#!" / :: header -> (2, header) - | _ -> (1, lines1) in - let pos2 = List.length lines1 + 2 in - (pos1, header, pos2, lines2) -*) +(* let split_lines lines = + * let lines1, lines2 = split_list [] is_sep lines in + * let pos1, header = + * match lines1 with + * | / "#!" / :: header -> (2, header) + * | _ -> (1, lines1) in + * let pos2 = List.length lines1 + 2 in + * (pos1, header, pos2, lines2) *) let split_lines lines = let test s = String.length s >= 2 && s.[0] = '#' && s.[1] = '!' in let lines1, lines2 = match split_list [] is_sep lines with - `One (s :: prog) when test s -> [s], prog - | `One prog -> [], prog - | `Two (a, b) -> (a, b) in + | `One (s :: prog) when test s -> [s], prog + | `One prog -> [], prog + | `Two (a, b) -> (a, b) in let (pos1, header) = match lines1 with - s :: header when test s -> 2, header - | _ -> 1, lines1 in + | s :: header when test s -> 2, header + | _ -> 1, lines1 in let pos2 = List.length lines1 + 2 in (pos1, header, pos2, lines2) @@ -237,18 +234,18 @@ let write_header ~pos ~source ~source_option ~verbose ~prog_file lines = let bin = obin_name source_option in let extra_args = match !Opt.extra_args with - [] -> "" - | l -> - sprintf "Ocamlscript.Common.extra_args := [ %s];;\n" - (String.concat "; " (List.map (fun s -> sprintf "%S" s) l)) in + | [] -> "" + | l -> + sprintf "Ocamlscript.Common.extra_args := [ %s];;\n" + (String.concat "; " (List.map (fun s -> sprintf "%S" s) l)) in let trash, script_dir = match source_option with - `Stdin - | `String _ -> (sprintf "Ocamlscript.Common.trash := \ - %S :: !Ocamlscript.Common.trash;;\n" - bin, - Sys.getcwd ()) - | `File script_name -> "", get_dir script_name in + | `Stdin + | `String _ -> (sprintf "Ocamlscript.Common.trash := \ + %S :: !Ocamlscript.Common.trash;;\n" + bin, + Sys.getcwd ()) + | `File script_name -> "", get_dir script_name in let file, oc = Filename.open_temp_file "meta" ".ml" in fprintf oc "\ @@ -266,7 +263,7 @@ Ocamlscript.Common.script_dir := %S;; open Ocamlscript;; open Utils;; #%i %S;;\n" - pos source verbose script_dir extra_args trash pos source; + pos source verbose script_dir extra_args trash pos source; List.iter (output_line oc) lines; @@ -279,9 +276,9 @@ let _ = exit (!Ocamlscript.Common.compile %S %S);;\n" prog_file bin; let write_body ~pos ~source ~locstyle lines = let file, oc = Filename.open_temp_file "prog" ".ml" in (match locstyle with - `Ocaml -> fprintf oc "#%i %S;;\n" pos source - | `Blank -> for i = 1 to pos - 1 do output_string oc endline done - | `None -> ()); + | `Ocaml -> fprintf oc "#%i %S;;\n" pos source + | `Blank -> for i = 1 to pos - 1 do output_string oc endline done + | `None -> ()); List.iter (output_line oc) lines; close_out oc; file @@ -326,9 +323,9 @@ let split_file = fun ?log source_option -> let source, lines = match source_option with - `Stdin -> "", Text.lines_of_channel stdin - | `String s -> "", (Str.split newline) s - | `File file -> file, Text.lines_of_file file in + | `Stdin -> "", Text.lines_of_channel stdin + | `String s -> "", (Str.split newline) s + | `File file -> file, Text.lines_of_file file in let pos1, unprocessed_header, pos2, body = split_lines lines in let header, locstyle = process_directives unprocessed_header in @@ -360,43 +357,43 @@ let absolute path = let option0 ?(refuse_input = false) x = let result = ref `Yes in (match x with - "--" -> result := `Stop - | "-help" - | "--help" -> Opt.help := true - | "-c" -> Opt.c := true - | "-f" -> Opt.f := true - | "-debug" -> Opt.debug := true - | "-version" -> Opt.version := true - | "-" -> - if refuse_input then - failwith "option - is disabled in this context" - else - Opt.set "source" Opt.from `Stdin - | _ -> result := `No); + | "--" -> result := `Stop + | "-help" + | "--help" -> Opt.help := true + | "-c" -> Opt.c := true + | "-f" -> Opt.f := true + | "-debug" -> Opt.debug := true + | "-version" -> Opt.version := true + | "-" -> + if refuse_input then + failwith "option - is disabled in this context" + else + Opt.set "source" Opt.from `Stdin + | _ -> result := `No); !result let option1 ?(refuse_input = false) x y = let result = ref true in (match x with - "-o" -> Opt.set "executable name" Opt.o y - | "-vm" -> Opt.set "virtual machine" Opt.vm y - | "-e" -> - if refuse_input then - failwith "option -e is disabled in this context" - else - Opt.set "source" Opt.from (`String y) - | _ -> result := false); + | "-o" -> Opt.set "executable name" Opt.o y + | "-vm" -> Opt.set "virtual machine" Opt.vm y + | "-e" -> + if refuse_input then + failwith "option -e is disabled in this context" + else + Opt.set "source" Opt.from (`String y) + | _ -> result := false); !result let start_option1 = function - "-o" - | "-vm" - | "-e" -> true - | _ -> false + | "-o" + | "-vm" + | "-e" -> true + | _ -> false let optionx = function - "" -> false + | "" -> false | s when s.[0] = '-' -> failwith (sprintf "%s is not a valid option" s) | _ -> false @@ -406,24 +403,24 @@ let other_arg x = let process_ocamlscript_args ?refuse_input ?(accept_non_option = false) l = let rec loop = function - x :: rest as l -> - begin - match option0 x with - `Stop -> (None, true, rest) - | `Yes -> loop rest - | `No -> - match l with - x :: y :: rest when option1 ?refuse_input x y -> loop rest - | x :: rest -> - if start_option1 x then - (Some x, false, rest) - else if optionx x then - loop rest - else if accept_non_option then - (other_arg x; loop rest) - else (None, false, l) - | [] -> assert false - end + | x :: rest as l -> + begin + match option0 x with + | `Stop -> (None, true, rest) + | `Yes -> loop rest + | `No -> + match l with + | x :: y :: rest when option1 ?refuse_input x y -> loop rest + | x :: rest -> + if start_option1 x then + (Some x, false, rest) + else if optionx x then + loop rest + else if accept_non_option then + (other_arg x; loop rest) + else (None, false, l) + | [] -> assert false + end | [] -> (None, false, []) in loop l @@ -434,8 +431,8 @@ let unquote s = let len = String.length s in while !i < len do match s.[!i] with - '"' -> Buffer.add_char buf '"'; i := !i + 2 - | c -> Buffer.add_char buf c; i := !i + 1 + | '"' -> Buffer.add_char buf '"'; i := !i + 2 + | c -> Buffer.add_char buf c; i := !i + 1 done; Buffer.contents buf @@ -451,23 +448,23 @@ let tokenize_args = let token = Str.regexp "\"\\([^\"]\\|\"\"\\)*\"\\|[^ \"]+" in fun s -> List.fold_right - (fun x accu -> - match x with - Str.Delim s -> - (if s <> "" && s.[0] = '"' then - unquote (String.sub s 1 (String.length s - 2)) - else s) :: accu + (fun x accu -> + match x with + | Str.Delim s -> + (if s <> "" && s.[0] = '"' then + unquote (String.sub s 1 (String.length s - 2)) + else s) :: accu | _ -> accu) - (Str.full_split token s) [] + (Str.full_split token s) [] let guess_arg1 s = match tokenize_args s with - [s'] when String.length s' >= 1 && s'.[0] <> '-' -> `Script_name - | l -> - `Ocamlscript_args (process_ocamlscript_args - ~refuse_input:true - ~accept_non_option:true l) + | [s'] when String.length s' >= 1 && s'.[0] <> '-' -> `Script_name + | l -> + `Ocamlscript_args (process_ocamlscript_args + ~refuse_input:true + ~accept_non_option:true l) (* name of Sys.argv.(0) in the final process (execution of the binary) depending on where the source program comes from: @@ -487,47 +484,47 @@ let guess_arg1 s = let main () = let script_path_option, script_args = match Array.to_list Sys.argv with - ocamlscript :: (arg1 :: other_args as l) -> - (match guess_arg1 arg1 with - `Script_name -> (`File (absolute arg1), l) - | `Ocamlscript_args (opt1, stopped, hardcoded_script_args) -> - let command_line_script_args = - let continued_args = - match opt1 with - None -> other_args - | Some o1 -> o1 :: other_args in - if stopped then continued_args - else - let opt1', stopped', command_line_script_args = - process_ocamlscript_args continued_args in - (match opt1' with - | None -> () - | Some x -> failwith - (sprintf "%s option expects an argument" x)); - command_line_script_args in - match !Opt.from with - Some `Stdin -> - (`Stdin, - "" :: - (hardcoded_script_args @ command_line_script_args)) - | Some (`String s) -> - (`String s, - "-e" :: - (hardcoded_script_args @ command_line_script_args)) - | Some (`File s) -> assert false - | None -> - match command_line_script_args with - [] -> - Opt.set "source" Opt.from `Stdin; - (`Stdin, - "" :: hardcoded_script_args) - | script_name :: l -> - Opt.set "source" Opt.from (`File script_name); - (`File (absolute script_name), - script_name :: (hardcoded_script_args @ l))) - | [_] | [] -> - Opt.set "source" Opt.from `Stdin; - (`Stdin, [""]) in + | ocamlscript :: (arg1 :: other_args as l) -> + (match guess_arg1 arg1 with + | `Script_name -> (`File (absolute arg1), l) + | `Ocamlscript_args (opt1, stopped, hardcoded_script_args) -> + let command_line_script_args = + let continued_args = + match opt1 with + | None -> other_args + | Some o1 -> o1 :: other_args in + if stopped then continued_args + else + let opt1', stopped', command_line_script_args = + process_ocamlscript_args continued_args in + (match opt1' with + | None -> () + | Some x -> failwith + (sprintf "%s option expects an argument" x)); + command_line_script_args in + match !Opt.from with + | Some `Stdin -> + (`Stdin, + "" :: + (hardcoded_script_args @ command_line_script_args)) + | Some (`String s) -> + (`String s, + "-e" :: + (hardcoded_script_args @ command_line_script_args)) + | Some (`File s) -> assert false + | None -> + match command_line_script_args with + | [] -> + Opt.set "source" Opt.from `Stdin; + (`Stdin, + "" :: hardcoded_script_args) + | script_name :: l -> + Opt.set "source" Opt.from (`File script_name); + (`File (absolute script_name), + script_name :: (hardcoded_script_args @ l))) + | [_] | [] -> + Opt.set "source" Opt.from `Stdin; + (`Stdin, [""]) in if !Opt.help then print_string Opt.help_message @@ -546,8 +543,8 @@ let main () = if compilation_status = 0 && not !Opt.c then let real_bin, real_args = match !Opt.vm with - None -> bin, script_args - | Some vm -> vm, (bin :: List.tl script_args) in + | None -> bin, script_args + | Some vm -> vm, (bin :: List.tl script_args) in Unix.execv real_bin (Array.of_list real_args) else (* includes the case where there is non-writeable executable *) exit compilation_status From 84c60f322fd29f9233b15769f6a1b61ac8adc20c Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 15:42:35 +0900 Subject: [PATCH 05/20] Ocaml: indent whole file add missing '|' character on first pattern matched --- ocaml.ml | 86 +++++++++++++++++++++++++++----------------------------- 1 file changed, 41 insertions(+), 45 deletions(-) diff --git a/ocaml.ml b/ocaml.ml index e1bdba3..a1824c6 100644 --- a/ocaml.ml +++ b/ocaml.ml @@ -18,9 +18,8 @@ let use_camlp4 = ref true (* by default camlp4 is used *) let use_ocamlc = ref false (* by default we want native code *) let use_ocamlfind = ref false (* used only if necessary *) let revised = ref false (* use this to use the revised syntax *) -let ocamlflags = Common.extra_args - (* any options that you may want to pass - to ocamlopt *) +let ocamlflags = Common.extra_args (* any options that you may want to pass + to ocamlopt *) let ppopt = ref [] (* any options that you may want to pass to camlp4o or camlp4r *) @@ -33,30 +32,28 @@ let ppsrcloc = ref None (* non-standard source location generator *) let exe s = match Sys.os_type with - "Win32" | "Cygwin"-> - if Filename.check_suffix s ".exe" then s - else s ^ ".exe" - | "Unix" | _ -> s + | "Win32" | "Cygwin"-> + if Filename.check_suffix s ".exe" then s + else s ^ ".exe" + | "Unix" | _ -> s let import path = let src = !+ path in let dst = Filename.basename src in let head = match !pp, !ppsrcloc, !revised with - Some _, Some f, _ -> f src - | _, _, false -> sprintf "#1 %S;;\n" src - | _, _, true -> sprintf "#1 %S;\n" src in + | Some _, Some f, _ -> f src + | _, _, false -> sprintf "#1 %S;;\n" src + | _, _, true -> sprintf "#1 %S;\n" src in Pipeline.copy_file ~head src dst -(* -let ocamllex_command input = - if !use_ocamllex then - Some ((fun () -> ()), - new_cmd [!ocamllex; input; - "-o"; "ocamlscript_ocamllex_out.ml"; "-q"], - "ocamlscript_ocamllex_out.ml") - else None -*) +(* let ocamllex_command input = + * if !use_ocamllex then + * Some ((fun () -> ()), + * new_cmd [!ocamllex; input; + * "-o"; "ocamlscript_ocamllex_out.ml"; "-q"], + * "ocamlscript_ocamllex_out.ml") + * else None *) let file_kind file = if Filename.check_suffix file ".mli" then `Mli @@ -73,25 +70,25 @@ let file_kind file = let extra_command file = match file_kind file with - `Mli | `Ml -> ([], [file]) - | `Mll -> - ([command [!ocamllex; file; "-q"]], - [(Filename.chop_extension file) ^ ".ml"]) - | `Mly -> - let p = Filename.chop_extension file in - ([command [!ocamlyacc; file]], - [p ^ ".mli"; p ^ ".ml"]) - | `Ext s -> - failwith (sprintf "don't know how to handle %s files: %s" s file) - | `Unknown -> - failwith (sprintf "don't know how to handle this file: %s" file) + | `Mli | `Ml -> ([], [file]) + | `Mll -> + ([command [!ocamllex; file; "-q"]], + [(Filename.chop_extension file) ^ ".ml"]) + | `Mly -> + let p = Filename.chop_extension file in + ([command [!ocamlyacc; file]], + [p ^ ".mli"; p ^ ".ml"]) + | `Ext s -> + failwith (sprintf "don't know how to handle %s files: %s" s file) + | `Unknown -> + failwith (sprintf "don't know how to handle this file: %s" file) let pp_command file = match !pp with - None -> [], [file] - | Some f -> - let cmd, files = f file in - ([cmd], files) + | None -> [], [file] + | Some f -> + let cmd, files = f file in + ([cmd], files) let extra_commands sources = @@ -105,15 +102,14 @@ let extra_commands sources = let ocaml_command input = let really_use_ocamlfind = match !use_ocamlfind, !packs with - true, _ | _, _ :: _ -> true - | _ -> false in + | true, _ | _, _ :: _ -> true + | _ -> false in let compiler = if really_use_ocamlfind then if !use_ocamlc then [!ocamlfind; "ocamlc"] else [!ocamlfind; "ocamlopt"] - else - if !use_ocamlc then [!ocamlc] - else [!ocamlopt] in + else if !use_ocamlc then [!ocamlc] + else [!ocamlopt] in let flags = !ocamlflags in let camlp4_stuff = @@ -124,13 +120,13 @@ let ocaml_command input = let ppoptions = if !ppopt = [] then [] else - if really_use_ocamlfind then - List.flatten (List.map (fun s -> ["-ppopt"; s]) !ppopt) - else !ppopt in + if really_use_ocamlfind then + List.flatten (List.map (fun s -> ["-ppopt"; s]) !ppopt) + else !ppopt in if really_use_ocamlfind then "-syntax" :: syntax :: ppoptions else - let space = function "" -> "" | s -> " " ^ s in + let space = function | "" -> "" | s -> " " ^ s in ["-pp"; sprintf "'%s%s'" camlp4 (space (String.concat " " ppoptions))] else [] in let packages = @@ -149,7 +145,7 @@ let ocaml_command input = let xcommands, all_ml_files = extra_commands all_sources in let args = compiler @ "-o" :: "prog" :: - flags @ camlp4_stuff @ packages @ all_ml_files in + flags @ camlp4_stuff @ packages @ all_ml_files in (init, xcommands, command args, exe "prog") From d9eb7d68d0b7e41af265e029bd76e31083b93303 Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 16:02:08 +0900 Subject: [PATCH 06/20] new file: src/version.ml --- src/version.ml | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/version.ml diff --git a/src/version.ml b/src/version.ml new file mode 100644 index 0000000..609f44f --- /dev/null +++ b/src/version.ml @@ -0,0 +1 @@ +let version = "2.0.4" From 5a54ec8f8e5c98f65420a9e3d6f40dd526617193 Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 16:02:20 +0900 Subject: [PATCH 07/20] renamed: common.ml -> src/common.ml renamed: common.mli -> src/common.mli renamed: ocaml.ml -> src/ocaml.ml renamed: ocaml.mli -> src/ocaml.mli renamed: pipeline.ml -> src/pipeline.ml renamed: pipeline.mli -> src/pipeline.mli renamed: std.ml -> src/std.ml renamed: utils.ml -> src/utils.ml renamed: utils.mli -> src/utils.mli --- common.ml => src/common.ml | 0 common.mli => src/common.mli | 0 ocaml.ml => src/ocaml.ml | 0 ocaml.mli => src/ocaml.mli | 0 pipeline.ml => src/pipeline.ml | 0 pipeline.mli => src/pipeline.mli | 0 std.ml => src/std.ml | 0 utils.ml => src/utils.ml | 0 utils.mli => src/utils.mli | 0 9 files changed, 0 insertions(+), 0 deletions(-) rename common.ml => src/common.ml (100%) rename common.mli => src/common.mli (100%) rename ocaml.ml => src/ocaml.ml (100%) rename ocaml.mli => src/ocaml.mli (100%) rename pipeline.ml => src/pipeline.ml (100%) rename pipeline.mli => src/pipeline.mli (100%) rename std.ml => src/std.ml (100%) rename utils.ml => src/utils.ml (100%) rename utils.mli => src/utils.mli (100%) diff --git a/common.ml b/src/common.ml similarity index 100% rename from common.ml rename to src/common.ml diff --git a/common.mli b/src/common.mli similarity index 100% rename from common.mli rename to src/common.mli diff --git a/ocaml.ml b/src/ocaml.ml similarity index 100% rename from ocaml.ml rename to src/ocaml.ml diff --git a/ocaml.mli b/src/ocaml.mli similarity index 100% rename from ocaml.mli rename to src/ocaml.mli diff --git a/pipeline.ml b/src/pipeline.ml similarity index 100% rename from pipeline.ml rename to src/pipeline.ml diff --git a/pipeline.mli b/src/pipeline.mli similarity index 100% rename from pipeline.mli rename to src/pipeline.mli diff --git a/std.ml b/src/std.ml similarity index 100% rename from std.ml rename to src/std.ml diff --git a/utils.ml b/src/utils.ml similarity index 100% rename from utils.ml rename to src/utils.ml diff --git a/utils.mli b/src/utils.mli similarity index 100% rename from utils.mli rename to src/utils.mli From 050b75377dda7916328ad7ee2c8b761bad92a01f Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 16:05:50 +0900 Subject: [PATCH 08/20] renamed: main.ml -> src/main.ml --- main.ml => src/main.ml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename main.ml => src/main.ml (100%) diff --git a/main.ml b/src/main.ml similarity index 100% rename from main.ml rename to src/main.ml From e59b0acf37c621afbf8007e8cf7cfdb6586f3fc9 Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 16:06:46 +0900 Subject: [PATCH 09/20] renamed: src/main.ml -> src/ocamlscript.ml --- src/{main.ml => ocamlscript.ml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{main.ml => ocamlscript.ml} (100%) diff --git a/src/main.ml b/src/ocamlscript.ml similarity index 100% rename from src/main.ml rename to src/ocamlscript.ml From 703c44f064e7495c5ae6764ee2cf72425c7aa388 Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 16:12:24 +0900 Subject: [PATCH 10/20] dune forces us to have a version.mli file ?! --- src/version.mli | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/version.mli diff --git a/src/version.mli b/src/version.mli new file mode 100644 index 0000000..64518ab --- /dev/null +++ b/src/version.mli @@ -0,0 +1 @@ +val version: string From 360189c007f825edcec057bf49844fda9cb44285 Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 16:12:52 +0900 Subject: [PATCH 11/20] created dune files --- dune-project | 1 + src/dune | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 dune-project create mode 100644 src/dune diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..a26d6e2 --- /dev/null +++ b/dune-project @@ -0,0 +1 @@ +(lang dune 1.6) diff --git a/src/dune b/src/dune new file mode 100644 index 0000000..3ae1e46 --- /dev/null +++ b/src/dune @@ -0,0 +1,5 @@ + +(executable + (name ocamlscript) + (modules ocamlscript pipeline version) + (libraries unix str)) From 953e9dc2636a4257207333d83d1eb81263e0a312 Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 16:14:06 +0900 Subject: [PATCH 12/20] opam: impacted by switch to dune --- opam | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/opam b/opam index 177a7d5..9d77e58 100644 --- a/opam +++ b/opam @@ -1,5 +1,5 @@ opam-version: "2.0" -synopsis: "Tool which compiles OCaml scripts into native code" +synopsis: "Tool compiling OCaml scripts to native code" authors: "Martin Jambon" maintainer: ["martin@mjambon.com" "Francois Berenger "] @@ -7,10 +7,8 @@ homepage: "https://github.com/mjambon/ocamlscript" bug-reports: "https://github.com/mjambon/ocamlscript/issues" dev-repo: "git://github.com/mjambon/ocamlscript" depends: ["ocaml" {>= "4.08"} # because of Fun.protect - "ocamlfind"] -build: [make] -install: [make "install"] -remove: ["ocamlfind" "remove" "ocamlscript"] + "dune" {>= "1.11"}] +build: ["dune" "build" "-p" name "-j" jobs] # url { # src: "https://github.com/mjambon/ocamlscript/archive/XXX" # checksum: "md5=XXX" From 25332756fae0b6a32d75e970b8455b59d33073cc Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 16:14:40 +0900 Subject: [PATCH 13/20] compile with dune needed to make unused vars explicit --- src/ocamlscript.ml | 11 +++++------ src/pipeline.ml | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/ocamlscript.ml b/src/ocamlscript.ml index 288526a..9323e1b 100644 --- a/src/ocamlscript.ml +++ b/src/ocamlscript.ml @@ -6,7 +6,6 @@ *) open Printf -open Ocamlscript module Opt = struct @@ -132,7 +131,7 @@ let output_line oc s = output_string oc s; output_string oc endline Doesn't handle dependencies toward runtime things that might change incompatibly (DLLs, bytecode version, ...). ocamlscript -f can be used to force recompilation in these cases. *) -let needs_recompile ?log = function +let needs_recompile ?_log = function | `Stdin | `String _ -> true | `File source -> let bin = bin_name source in @@ -277,7 +276,7 @@ let write_body ~pos ~source ~locstyle lines = let file, oc = Filename.open_temp_file "prog" ".ml" in (match locstyle with | `Ocaml -> fprintf oc "#%i %S;;\n" pos source - | `Blank -> for i = 1 to pos - 1 do output_string oc endline done + | `Blank -> for _i = 1 to pos - 1 do output_string oc endline done | `None -> ()); List.iter (output_line oc) lines; close_out oc; @@ -484,7 +483,7 @@ let guess_arg1 s = let main () = let script_path_option, script_args = match Array.to_list Sys.argv with - | ocamlscript :: (arg1 :: other_args as l) -> + | _ocamlscript :: (arg1 :: other_args as l) -> (match guess_arg1 arg1 with | `Script_name -> (`File (absolute arg1), l) | `Ocamlscript_args (opt1, stopped, hardcoded_script_args) -> @@ -495,7 +494,7 @@ let main () = | Some o1 -> o1 :: other_args in if stopped then continued_args else - let opt1', stopped', command_line_script_args = + let opt1', _stopped', command_line_script_args = process_ocamlscript_args continued_args in (match opt1' with | None -> () @@ -511,7 +510,7 @@ let main () = (`String s, "-e" :: (hardcoded_script_args @ command_line_script_args)) - | Some (`File s) -> assert false + | Some (`File _s) -> assert false | None -> match command_line_script_args with | [] -> diff --git a/src/pipeline.ml b/src/pipeline.ml index c3cdf37..b40041d 100644 --- a/src/pipeline.ml +++ b/src/pipeline.ml @@ -72,7 +72,7 @@ let rec remove ?log file = | S_SOCK -> maybe_log log "remove file %S\n%!" file; Sys.remove file - with e -> () + with _e -> () (* like Sys.command, but without shell interpretation *) From d87b2167b43ec7bf3da98f8577b801a80036b2ec Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 16:16:16 +0900 Subject: [PATCH 14/20] dune: only give public_name for the exe so that dune knows it must be installed --- src/dune | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dune b/src/dune index 3ae1e46..51c5ee5 100644 --- a/src/dune +++ b/src/dune @@ -1,5 +1,5 @@ (executable - (name ocamlscript) + (public_name ocamlscript) (modules ocamlscript pipeline version) (libraries unix str)) From e208f4d79b9ee4ed17cd1de3e8b408d065ed20fb Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 16:16:43 +0900 Subject: [PATCH 15/20] mv opam ocamlscript.opam --- ocamlscript.opam | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 ocamlscript.opam diff --git a/ocamlscript.opam b/ocamlscript.opam new file mode 100644 index 0000000..9d77e58 --- /dev/null +++ b/ocamlscript.opam @@ -0,0 +1,15 @@ +opam-version: "2.0" +synopsis: "Tool compiling OCaml scripts to native code" +authors: "Martin Jambon" +maintainer: ["martin@mjambon.com" + "Francois Berenger "] +homepage: "https://github.com/mjambon/ocamlscript" +bug-reports: "https://github.com/mjambon/ocamlscript/issues" +dev-repo: "git://github.com/mjambon/ocamlscript" +depends: ["ocaml" {>= "4.08"} # because of Fun.protect + "dune" {>= "1.11"}] +build: ["dune" "build" "-p" name "-j" jobs] +# url { +# src: "https://github.com/mjambon/ocamlscript/archive/XXX" +# checksum: "md5=XXX" +# } From 2d44efe2bde0210c7665ab4b33a81259cd32a800 Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 16:16:56 +0900 Subject: [PATCH 16/20] mv opam ocamlscript.opam --- opam | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 opam diff --git a/opam b/opam deleted file mode 100644 index 9d77e58..0000000 --- a/opam +++ /dev/null @@ -1,15 +0,0 @@ -opam-version: "2.0" -synopsis: "Tool compiling OCaml scripts to native code" -authors: "Martin Jambon" -maintainer: ["martin@mjambon.com" - "Francois Berenger "] -homepage: "https://github.com/mjambon/ocamlscript" -bug-reports: "https://github.com/mjambon/ocamlscript/issues" -dev-repo: "git://github.com/mjambon/ocamlscript" -depends: ["ocaml" {>= "4.08"} # because of Fun.protect - "dune" {>= "1.11"}] -build: ["dune" "build" "-p" name "-j" jobs] -# url { -# src: "https://github.com/mjambon/ocamlscript/archive/XXX" -# checksum: "md5=XXX" -# } From 21038c529c39ffe3b5ea469398061df533a55fca Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 17:27:12 +0900 Subject: [PATCH 17/20] updated tests/Makefile --- tests/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Makefile b/tests/Makefile index 4ee9b08..1448aea 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -2,7 +2,7 @@ default: @echo '*** These tests are for developers only! ***' @echo -e '*** They might not work on your platform! ***\n\n' - cp ../ocamlscript . + cp -f ../_build/default/src/ocamlscript.exe ./ocamlscript ./unpack './Hello "World"' ./fulltest arg1 arg2 From a11de9831d875acf634d54e853d7de63a2a937c8 Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 17:31:53 +0900 Subject: [PATCH 18/20] dune: apparently, we need a library plus an exe --- src/dune | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/dune b/src/dune index 51c5ee5..98b8c0d 100644 --- a/src/dune +++ b/src/dune @@ -1,5 +1,11 @@ -(executable - (public_name ocamlscript) +(library + (name ocamlscript) (modules ocamlscript pipeline version) (libraries unix str)) + +(executable + (name main) + (public_name ocamlscript) + (modules main) + (libraries ocamlscript)) From 6b1c15013c378fd7c595f565d0053660384df47f Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 17:32:26 +0900 Subject: [PATCH 19/20] calling Ocamlscript.main() was moved to the new src/main.ml file dune requirement, apparently, since from one module we create both an exe and a library --- src/main.ml | 8 ++++++++ src/ocamlscript.ml | 6 ------ 2 files changed, 8 insertions(+), 6 deletions(-) create mode 100644 src/main.ml diff --git a/src/main.ml b/src/main.ml new file mode 100644 index 0000000..21ba562 --- /dev/null +++ b/src/main.ml @@ -0,0 +1,8 @@ + +open Printf + +let () = + try Ocamlscript.main () + with Failure s -> + (eprintf "ocamlscript: %s\n%!" s; + exit 2) diff --git a/src/ocamlscript.ml b/src/ocamlscript.ml index 9323e1b..3c4ee90 100644 --- a/src/ocamlscript.ml +++ b/src/ocamlscript.ml @@ -547,9 +547,3 @@ let main () = Unix.execv real_bin (Array.of_list real_args) else (* includes the case where there is non-writeable executable *) exit compilation_status - -let _ = - try main () - with Failure s -> - eprintf "ocamlscript: %s\n%!" s; - exit 2 From bc58ce83f4b0cc9db4a11cff53639f05a2730444 Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Mon, 8 Feb 2021 17:41:24 +0900 Subject: [PATCH 20/20] updated makefiles to call the freshly compiled by dune exe --- examples/Makefile | 8 ++++---- tests/Makefile | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/Makefile b/examples/Makefile index 5c7a251..613fec7 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -1,8 +1,8 @@ .PHONY: default clean default: - ../ocamlscript factorial.ml 100 - ../ocamlscript revised.ml - ../ocamlscript with-includes.ml - echo "1+1" | ../ocamlscript calc.ml + ../_build/default/src/main.exe factorial.ml 100 + ../_build/default/src/main.exe revised.ml + ../_build/default/src/main.exe with-includes.ml + echo "1+1" | ../_build/default/src/main.exe calc.ml clean: $(RM) *~ *.exe diff --git a/tests/Makefile b/tests/Makefile index 1448aea..969ebd8 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -2,7 +2,7 @@ default: @echo '*** These tests are for developers only! ***' @echo -e '*** They might not work on your platform! ***\n\n' - cp -f ../_build/default/src/ocamlscript.exe ./ocamlscript + \cp -f ../_build/default/src/main.exe ./ocamlscript ./unpack './Hello "World"' ./fulltest arg1 arg2