~ubuntu-branches/ubuntu/vivid/menhir/vivid

« back to all changes in this revision

Viewing changes to demos/ocamldep.wrapper

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2006-07-11 12:26:18 UTC
  • Revision ID: james.westby@ubuntu.com-20060711122618-dea56bmjs3qlmsd8
Tags: upstream-20060615.dfsg
ImportĀ upstreamĀ versionĀ 20060615.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ocaml
 
2
 
 
3
(* ocamldep.wrapper <filename> ... <filename> - <ocamldep command>
 
4
   runs the <ocamldep command> in an environment where all of the
 
5
   <filenames> listed appear to exist. The files are created, if
 
6
   required, before the command is run, and destroyed afterwards. *)
 
7
 
 
8
open Printf
 
9
 
 
10
(* Parse the command line. The arguments that precede "-" are understood
 
11
   as file names and stored in the list [xs]. The arguments that follow
 
12
   "-" are understood as a command and stored in [command]. *)
 
13
 
 
14
let xs =
 
15
  ref []
 
16
 
 
17
let command =
 
18
  ref ""
 
19
 
 
20
let verbose =
 
21
  ref false
 
22
 
 
23
let rec loop accumulating i =
 
24
  if i = Array.length Sys.argv then
 
25
    ()
 
26
  else if accumulating then
 
27
    match Sys.argv.(i) with
 
28
    | "-v" ->
 
29
        verbose := true;
 
30
        loop true (i+1)
 
31
    | "-" ->
 
32
        loop false (i+1)
 
33
    | _ ->
 
34
        xs := Sys.argv.(i) :: !xs;
 
35
        loop true (i+1)
 
36
  else begin
 
37
    command := sprintf "%s %s" !command (Filename.quote Sys.argv.(i));
 
38
    loop false (i+1)
 
39
  end
 
40
 
 
41
let () =
 
42
  loop true 1
 
43
 
 
44
(* Create the required files if they don't exist, run the command,
 
45
   then destroy any files that we have created. *)
 
46
 
 
47
let rec loop = function
 
48
  | [] ->
 
49
      if !verbose then
 
50
        fprintf stderr "ocamldep.wrapper: running %s\n" !command;
 
51
      Sys.command !command
 
52
  | x :: xs ->
 
53
      if Sys.file_exists x then
 
54
        loop xs
 
55
      else begin
 
56
        if !verbose then
 
57
          fprintf stderr "ocamldep.wrapper: creating fake %s\n" x;
 
58
        let c = open_out x in
 
59
        close_out c;
 
60
        let exitcode = loop xs in
 
61
        if Sys.file_exists x then begin
 
62
          try
 
63
            if !verbose then
 
64
              fprintf stderr "ocamldep.wrapper: removing fake %s..." x;
 
65
            Sys.remove x;
 
66
            if !verbose then
 
67
              fprintf stderr " ok\n"
 
68
          with Sys_error _ ->
 
69
            if !verbose then
 
70
              fprintf stderr " failed\n"
 
71
        end;
 
72
        exitcode
 
73
      end
 
74
 
 
75
let () =
 
76
  exit (loop !xs)
 
77