-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdiscover.ml
71 lines (61 loc) · 1.82 KB
/
discover.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
module C = Configurator.V1
let preamble ~include_base = Printf.sprintf "#include <%s/mysql.h>" include_base
let detect_src ~include_base =
Printf.sprintf
{|
#include <stddef.h>
%s
int
main(void)
{
MYSQL *m = mysql_init(NULL);
mysql_close(m);
return 0;
}
|}
(preamble ~include_base)
module Variant = struct
type t = { link_flags : string list; include_base : string }
let try_compile c { link_flags; include_base } =
C.c_test c (detect_src ~include_base) ~link_flags
end
let split_flags s = String.split_on_char ' ' (String.trim s)
let use_config cmd c =
match C.Process.run c cmd [ "--libs" ] with
| { exit_code = 0; stdout = libs_out; _ } -> (
match C.Process.run c cmd [ "--variable=pkgincludedir" ] with
| { exit_code = 0; stdout = pkgincludedir; _ } ->
Some
{
Variant.link_flags = split_flags libs_out;
include_base = String.trim pkgincludedir;
}
| _ -> None)
| _ -> None
let static v = fun _ -> Some v
let variants =
Variant.
[
use_config "mariadb_config";
static { link_flags = [ "-lmariadb" ]; include_base = "mariadb" };
static { link_flags = [ "-lmariadbclient" ]; include_base = "mysql" };
use_config "mysql_config";
static { link_flags = [ "-lmysqlclient" ]; include_base = "mysql" };
]
let () =
C.main ~name:"mariadb" @@ fun c ->
let variant =
match
List.find_map
(fun f ->
match f c with
| Some v when Variant.try_compile c v -> Some v
| _ -> None)
variants
with
| Some v -> v
| None -> C.die "Cannot find MariaDB client library."
in
C.Flags.write_sexp "mariadb_link_flags.sexp" variant.Variant.link_flags;
C.Flags.write_lines "mariadb_preamble.h"
[ preamble ~include_base:variant.Variant.include_base ]