Fix native virt-ctrl on Windows.
[virt-top.git] / virt-ctrl / mingw-gcc-wrapper.ml
diff --git a/virt-ctrl/mingw-gcc-wrapper.ml b/virt-ctrl/mingw-gcc-wrapper.ml
new file mode 100755 (executable)
index 0000000..21cdb8f
--- /dev/null
@@ -0,0 +1,70 @@
+(* Wrapper around 'gcc'.  On MinGW, this wrapper understands the '@...'\r
+ * syntax for extending the command line.\r
+ *)\r
+\r
+open Printf\r
+open Unix\r
+\r
+let (//) = Filename.concat\r
+\r
+(* Substitute any @... arguments with the file content. *)\r
+let rec input_all_lines chan =\r
+  try\r
+    let line = input_line chan in\r
+    line :: input_all_lines chan\r
+  with\r
+    End_of_file -> []\r
+\r
+let argv = Array.map (\r
+  fun arg ->\r
+    if arg.[0] = '@' then (\r
+      let chan = open_in (String.sub arg 1 (String.length arg - 1)) in\r
+      let lines = input_all_lines chan in\r
+      close_in chan;\r
+      lines\r
+    ) else\r
+      [arg]\r
+) Sys.argv\r
+\r
+let argv = Array.to_list argv\r
+let argv = List.flatten argv\r
+\r
+(* Find the real gcc.exe on $PATH, but ignore any '.' elements in the path.\r
+ * Note that on Windows, $PATH is split with ';' characters.\r
+ *)\r
+let rec split_find str sep f =\r
+  try\r
+    let i = String.index str sep in\r
+    let n = String.length str in\r
+    let str, str' = String.sub str 0 i, String.sub str (i+1) (n-i-1) in\r
+    match f str with\r
+    | None -> split_find str' sep f  (* not found, keep searching *)\r
+    | Some found -> found\r
+  with\r
+    Not_found ->\r
+      match f str with\r
+      | None -> raise Not_found (* not found at all *)\r
+      | Some found -> found\r
+\r
+let exists filename =\r
+  try access filename [F_OK]; true with Unix_error _ -> false\r
+\r
+let gcc =\r
+  split_find (Sys.getenv "PATH") ';'\r
+    (function\r
+     | "." -> None (* ignore current directory in path *)\r
+     | path ->\r
+       let gcc = path // "gcc.exe" in\r
+       if exists gcc then Some gcc else None)\r
+\r
+(* Finally execute the real gcc with the full argument list.\r
+ * Can't use execv here because then the parent process (ocamlopt) thinks\r
+ * that this process has finished and deletes all the temp files.  Stupid\r
+ * Windoze!\r
+ *)\r
+let _ =\r
+  let argv = List.map Filename.quote (List.tl argv) in\r
+  let cmd = String.concat " " (gcc :: argv) in\r
+  eprintf "mingw-gcc-wrapper: %s\n%!" cmd;\r
+  let r = Sys.command cmd in\r
+  exit r\r