1
+ module C = Configurator. V1
2
+
1
3
open Printf
2
4
3
5
let find_number ~pos str =
@@ -21,43 +23,65 @@ let find_number ~pos str =
21
23
let number_lst, next = loop ~pos in
22
24
String. concat " " number_lst, next
23
25
24
- let () =
25
- let module C = Configurator. V1 in
26
- C. main ~name: " postgresql" (fun _c ->
27
- let cmd = " pg_config --includedir --libdir --version" in
28
- let ic =
29
- try Unix. open_process_in cmd
30
- with exc -> eprintf " could not open pg_config, cmd: '%s'" cmd; raise exc
26
+ let pg_major_minor ic =
27
+ let line = input_line ic in
28
+ let print_fail () =
29
+ eprintf " Unable to find versions from line '%s'" line
30
+ in
31
+ let exit_fail () = print_fail () ; exit 1 in
32
+ try
33
+ let first_space = String. index line ' ' in
34
+ let major, next = find_number ~pos: first_space line in
35
+ let minor =
36
+ (* Can also handle release candidates *)
37
+ let c = line.[next] in
38
+ if c = '.' then fst (find_number ~pos: next line)
39
+ else if c <> 'r' || line.[next + 1 ] <> 'c' then exit_fail ()
40
+ else " 0"
31
41
in
32
- Fun. protect ~finally: (fun () -> close_in ic) (fun () ->
33
- let pgsql_includedir = " -I" ^ input_line ic in
34
- let pgsql_libdir = " -L" ^ input_line ic in
35
- let major, minor =
36
- let line = input_line ic in
37
- let print_fail () =
38
- eprintf " Unable to find versions from line '%s', cmd: '%s'" line cmd
39
- in
40
- let exit_fail () = print_fail () ; exit 1 in
41
- try
42
- let first_space = String. index line ' ' in
43
- let major, next = find_number ~pos: first_space line in
44
- let minor =
45
- (* Can also handle release candidates *)
46
- let c = line.[next] in
47
- if c = '.' then fst (find_number ~pos: next line)
48
- else if c <> 'r' || line.[next + 1 ] <> 'c' then exit_fail ()
49
- else " 0"
50
- in
51
- if major = " " || minor = " " then exit_fail ()
52
- else
53
- " -DPG_OCAML_MAJOR_VERSION=" ^ major,
54
- " -DPG_OCAML_MINOR_VERSION=" ^ minor
55
- with exn -> print_fail () ; raise exn
42
+ if major = " " || minor = " " then exit_fail ()
43
+ else
44
+ " -DPG_OCAML_MAJOR_VERSION=" ^ major,
45
+ " -DPG_OCAML_MINOR_VERSION=" ^ minor
46
+ with exn -> print_fail () ; raise exn
47
+
48
+ let major_minor_from_pgconfig () =
49
+ let cmd = " pg_config --version" in
50
+ let ic =
51
+ try Unix. open_process_in cmd
52
+ with exc -> eprintf " could not open pg_config, cmd: '%s'" cmd; raise exc
53
+ in
54
+ Fun. protect ~finally: (fun () -> close_in ic) (fun () -> pg_major_minor ic)
55
+
56
+ let from_pgconfig () =
57
+ let cmd = " pg_config --includedir --libdir --version" in
58
+ let ic =
59
+ try Unix. open_process_in cmd
60
+ with exc -> eprintf " could not open pg_config, cmd: '%s'" cmd; raise exc
61
+ in
62
+ Fun. protect ~finally: (fun () -> close_in ic) (fun () ->
63
+ let pgsql_includedir = " -I" ^ input_line ic in
64
+ let pgsql_libdir = " -L" ^ input_line ic in
65
+ let major, minor = pg_major_minor ic in
66
+ { C.Pkg_config. cflags = [pgsql_includedir; major; minor]
67
+ ; libs = [pgsql_libdir; " -lpq" ]
68
+ })
69
+
70
+ let () =
71
+ C. main ~name: " postgresql" (fun c ->
72
+ let conf =
73
+ match C.Pkg_config. get c with
74
+ | Some pc ->
75
+ begin match
76
+ C.Pkg_config. query pc ~package: " libpq"
77
+ with
78
+ | Some conf ->
79
+ let major, minor = major_minor_from_pgconfig () in
80
+ { conf with
81
+ C.Pkg_config. cflags = major :: minor :: conf .cflags }
82
+ | None -> { C.Pkg_config. cflags = [] ; libs = [] }
83
+ end
84
+ | None -> from_pgconfig ()
56
85
in
57
- let conf = {
58
- C.Pkg_config.
59
- cflags = [pgsql_includedir; major; minor];
60
- libs = [pgsql_libdir; " -lpq" ];
61
- } in
62
86
C.Flags. write_sexp " c_flags.sexp" conf.cflags;
63
- C.Flags. write_sexp " c_library_flags.sexp" conf.libs))
87
+ C.Flags. write_sexp " c_library_flags.sexp" conf.libs)
0 commit comments