-
Notifications
You must be signed in to change notification settings - Fork 48
/
discover.ml
79 lines (73 loc) · 2.15 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
72
73
74
75
76
77
78
79
module C = Configurator.V1
let directory_exists fsp =
Sys.file_exists fsp && Sys.is_directory fsp
let default c : C.Pkg_config.package_conf =
if C.ocaml_config_var_exn c "system" = "macosx" then
if directory_exists "/usr/local/opt/openssl" then
{ libs = ["-L/usr/local/opt/openssl/lib"]
; cflags = ["-I/usr/local/opt/openssl/include"]
}
else
{ libs = ["-L/opt/local/lib"]
; cflags = ["-I/opt/local/include"]
}
else
{ libs = ["-lssl"; "-lcrypto"]
; cflags = []
}
let prog fun_name =
Printf.sprintf {c|
#include <openssl/ssl.h>
int main(int argc, char **argv) {
void *foo = %s;
return 0;
}|c} fun_name
let function_tests =
[ "TLSv1_1_method", "HAVE_TLS11"
; "TLSv1_2_method", "HAVE_TLS12"
; "EC_KEY_free", "HAVE_EC"
; "SSL_set_alpn_protos", "HAVE_ALPN"
]
let macro_tests =
[ "SSL_set_tlsext_host_name", "HAVE_SNI"
; "SSL_EXT_TLS1_3_ONLY", "HAVE_TLS13"
]
let () =
C.main ~name:"ssl" (fun c ->
let default = default c in
let conf =
match C.Pkg_config.get c with
| None -> default
| Some pc ->
begin match (C.Pkg_config.query pc ~package:"openssl") with
| Some s -> s
| None -> default
end
in
let results =
C.C_define.import c
~c_flags:conf.cflags
~includes:["openssl/ssl.h"]
(List.map (fun (c, _) -> (c, C.C_define.Type.Switch))
macro_tests)
in
let defines =
List.combine macro_tests results
|> List.map (fun ((name, const), (name', value)) ->
assert (name = name');
(const, value))
in
let defines =
List.fold_left (fun acc (fun_name, var_name) ->
let defined =
prog fun_name
|> C.c_test c ~c_flags:conf.cflags ~link_flags:conf.libs
in
( var_name
, C.C_define.Value.Switch defined
) :: acc
) defines function_tests
in
C.C_define.gen_header_file c ~fname:"ocaml_ssl.h" defines;
C.Flags.write_sexp "c_library_flags.sexp" conf.libs;
C.Flags.write_sexp "c_flags.sexp" conf.cflags)