forked from OCamlPro/alt-ergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.ml
175 lines (151 loc) · 4.96 KB
/
configure.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env -S ocaml unix.cma
(* Configure script
goal: register some static configuration & check dependencies
Steps executed:
- parse command line options
- compute actual config options
- output these config options in Makefile.config
- check dune is present
- ask dune to check the external deps
*)
(* configuration options *)
let prefix = ref ""
let libdir = ref ""
let mandir = ref ""
let static = ref false
let pkg = ref ""
(* Parse command line arguments *)
let () =
let args =
Arg.align
[
("--prefix", Arg.Set_string prefix, "<path> prefix directory");
("--libdir", Arg.Set_string libdir, "<path> lib directory");
("--static", Arg.Set static, " Enable statically compilation");
]
in
let anon_fun s =
match !pkg with
| "" -> pkg := s
| _ ->
Format.eprintf "Anonymous argument ignored: '%s'@." s;
exit 1
in
let usage = "./configure [options]" in
Arg.parse args anon_fun usage
(* Small wrapper to set options *)
let update name r f =
match !r with
| "" ->
r := f ();
Format.printf "Using default value for '%s' : %s@." name !r
| s -> Format.printf "Using provided value for '%s' : %s@." name s
(* small wrapper around opam var *)
let opam_var v =
let cmd = Format.asprintf "opam config var %s" v in
let ch = Unix.open_process_in cmd in
let s = input_line ch in
let _ = Unix.close_process_in ch in
s
(* Compute actual values for config options *)
let () =
let prefix_set = !prefix <> "" in
update "prefix" prefix (fun () -> opam_var "prefix");
update "libdir" libdir (fun () ->
if prefix_set then Filename.concat !prefix "lib" else opam_var "lib");
update "mandir" mandir (fun () ->
if prefix_set then Filename.concat !prefix "man" else opam_var "man");
()
(* Output config options into lib/util/config.ml *)
let () =
let f = Filename.(concat "src" @@ concat "lib" @@ concat "util" "config.ml") in
let () = Format.printf "Generating file %s..." f in
let ch = open_out f in
let fmt = Format.formatter_of_out_channel ch in
let () =
Format.fprintf fmt
"(* Static configuration, automatically generated by configure.ml *)@."
in
let () = Format.fprintf fmt {|let libdir = "%s"@.|} !libdir in
let () = Format.fprintf fmt {|let mandir = "%s"@.|} !mandir in
let () = Format.fprintf fmt {|
(* Dynamic configuration, relative to the executable path *)
let follow dir path =
Filename.concat path dir
let abs_exe_path =
let exe_name = Sys.executable_name in
if not (Filename.is_relative exe_name) then exe_name
else begin
let cwd = Sys.getcwd () in
Filename.concat cwd exe_name
end
let datadir =
abs_exe_path
|> Filename.dirname
|> follow Filename.parent_dir_name
|> follow "share"
|> follow "alt-ergo"
let pluginsdir = datadir |> follow "plugins"
let preludesdir = datadir |> follow "preludes"
|} in
let () = close_out ch in
let () = Format.printf "done.@." in
()
(* Output executable flags into tools/text/flags.dune *)
let () =
let f = Filename.(concat "src" @@ concat "bin" @@ concat "text" "flags.dune") in
let () = Format.printf "Generating file %s..." f in
let ch = open_out f in
let fmt = Format.formatter_of_out_channel ch in
let () = Format.fprintf fmt {|(-linkall %s)@.|}
(if !static then "-cclib -static" else "")
in
let () = close_out ch in
let () = Format.printf "done.@." in
()
(* Output config options into Makefile.config *)
let () =
let () = Format.printf "Generating file Makefile.config..." in
let ch = open_out "Makefile.config" in
let fmt = Format.formatter_of_out_channel ch in
let () = Format.fprintf fmt "# Generated by configure@." in
let () = Format.fprintf fmt "prefix=%s@." !prefix in
let () = Format.fprintf fmt "libdir=%s@." !libdir in
let () = Format.fprintf fmt "mandir=%s@." !mandir in
let () = close_out ch in
let () = Format.printf "done.@." in
()
(* Small wrapper to read all the contents of a channel *)
let read_all ch =
let b = Buffer.create 113 in
try
while true do
Buffer.add_channel b ch 30
done;
assert false
with End_of_file -> Buffer.contents b
(* check that dune is present *)
let () =
let cmd = Format.asprintf "which dune" in
let ch = Unix.open_process_in cmd in
let _ = read_all ch in
let res = Unix.close_process_in ch in
match res with
| Unix.WEXITED 0 -> Format.printf "Found dune in path.@."
| _ -> Format.eprintf "ERROR: Couldn't find dune in env@.";
exit 1
(* run dune to check that dependencies are installed *)
let () =
let p_opt = match !pkg with "" -> "" | s -> Format.asprintf "-p %s" s in
let cmd =
Format.asprintf
"dune external-lib-deps --display=quiet --missing %s @install" p_opt
in
let ch = Unix.open_process_in cmd in
let _ = read_all ch in
let res = Unix.close_process_in ch in
match res with
| Unix.WEXITED 0 -> Format.printf "All deps are installed.@."
(* dune already prints the missing libs on stderr *)
| _ -> exit 2
let () = Format.printf "Good to go!@."