Merge branch 'handle-stdout'
[SXSI/xpathcomp.git] / build
1 #!/usr/bin/env ocaml
2 #use "myocamlbuild_config.ml"
3
4 let valid_targets = [ "distclean"; "clean"; "all" ]
5
6 module Cmdline =
7   struct
8     open Arg
9     let set r s () = r := s
10     let cons r s () = r := s :: !r
11     let targets = ref []
12     let tags = ref []
13     let flavor = ref "native"
14     let verbose = ref ""
15     let jobs = ref 0
16     let specs = align
17       [ "-verbose", Unit (set verbose " -classic-display"),
18         " Display compilation commands";
19
20         "-enable-debug", Unit (cons tags "-tag debug"),
21         " Build with debugging code";
22
23         "-enable-profile", Unit (cons tags "-tag profile"),
24         " Build with profiling code";
25
26         "-enable-log", Unit (cons tags "-tag log"),
27         " Build with tracing code enabled";
28
29         "-byte", Unit (set flavor "byte"),
30         " Produce bytecode instead of native code";
31         "-j", Int (fun i -> if i < 0 || i > 1024
32           then raise (Bad ("Invalid job count: " ^ string_of_int i))
33           else jobs:= i),
34         " Spawns n parallel jobs (0 for unlimited, max = 1024, default = 0)"
35       ]
36     let usage_msg =
37       "usage: " ^ Sys.argv.(0) ^
38         " [options] target ... target\nvalid targets are: " ^
39         ( match valid_targets with
40             [] | [ _ ] -> assert false
41           | l::ll -> (String.concat ", " ll) ^ " and " ^ l)
42     let parse () =
43       parse specs
44       (fun x ->
45         if List.mem x valid_targets
46         then targets := x :: !targets
47         else raise (Bad ("Invalid target: " ^ x))
48       )
49       usage_msg;
50       targets := List.rev !targets;
51       if !targets = [] then targets := [ "all" ]
52   end
53
54 let tests_targets = []
55 ;;
56 let () = Cmdline.parse ()
57 let cmd_list =
58   let ocamlbuild =
59     Printf.sprintf "ocamlbuild %s %s -j %i "
60       !Cmdline.verbose (String.concat " " !Cmdline.tags) !Cmdline.jobs
61   in
62   List.map begin function
63     | "distclean" -> "rm -f myocamlbuild_config.ml; ocamlbuild -clean"
64     | "clean" -> "ocamlbuild -clean"
65     | "all" -> ocamlbuild ^ (List.assoc !Cmdline.flavor main_targets)
66     | test ->
67       ocamlbuild ^ (List.assoc (test,!Cmdline.flavor) tests_targets)
68   end !Cmdline.targets
69
70 let () = List.iter (fun cmd -> ignore (Sys.command cmd) ) cmd_list