Skip to content

Commit

Permalink
Merge pull request #4088 from MisterDA/configurator-c-libraries
Browse files Browse the repository at this point in the history
Always link the c_libraries in the configurator build command
  • Loading branch information
rgrinberg authored Jan 21, 2021
2 parents 68856a3 + da5aa5a commit 329ba07
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
- Fixed memory blow up introduced in 2.8.0 (#4144, fixes #4134,
@jeremiedimino)

- Configurator: always link the C libraries in the build command
(#4088, @MisterDA).

2.8.1 (14/01/2021)
------------------

Expand Down
21 changes: 16 additions & 5 deletions otherlibs/configurator/src/v1.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type t =
; c_compiler : string
; stdlib_dir : string
; ccomp_type : string
; c_libraries : string list
; ocamlc_config : Ocaml_config.Vars.t
; ocamlc_config_cmd : string
}
Expand Down Expand Up @@ -310,16 +311,21 @@ let read_dot_dune_configurator_file ~build_dir =

let fill_in_fields_that_depends_on_ocamlc_config t =
let get = ocaml_config_var_exn t in
let c_compiler =
let get_flags var =
get var |> String.trim |> Flags.extract_blank_separated_words
in
let c_compiler, c_libraries =
match Ocaml_config.Vars.find t.ocamlc_config "c_compiler" with
| Some c_comp -> c_comp ^ " " ^ get "ocamlc_cflags"
| None -> get "bytecomp_c_compiler"
| Some c_comp ->
(c_comp ^ " " ^ get "ocamlc_cflags", get_flags "native_c_libraries")
| None -> (get "bytecomp_c_compiler", get_flags "bytecomp_c_libraries")
in
{ t with
ext_obj = get "ext_obj"
; c_compiler
; stdlib_dir = get "standard_library"
; ccomp_type = get "ccomp_type"
; c_libraries
}

let create_from_inside_dune ~dest_dir ~log ~build_dir ~name =
Expand All @@ -344,6 +350,7 @@ let create_from_inside_dune ~dest_dir ~log ~build_dir ~name =
; c_compiler = ""
; stdlib_dir = ""
; ccomp_type = ""
; c_libraries = []
}

let create ?dest_dir ?ocamlc ?(log = ignore) name =
Expand Down Expand Up @@ -372,6 +379,7 @@ let create ?dest_dir ?ocamlc ?(log = ignore) name =
; c_compiler = ""
; stdlib_dir = ""
; ccomp_type = ""
; c_libraries = []
; ocamlc_config = Ocaml_config.Vars.of_list_exn []
; ocamlc_config_cmd
}
Expand Down Expand Up @@ -410,12 +418,13 @@ let compile_and_link_c_prog t ?(c_flags = []) ?(link_flags = []) code =
let ok =
if need_to_compile_and_link_separately t then
run_ok (c_flags @ [ "-I"; t.stdlib_dir; "-c"; c_fname ])
&& run_ok ("-o" :: exe_fname :: obj_fname :: link_flags)
&& run_ok (("-o" :: exe_fname :: obj_fname :: t.c_libraries) @ link_flags)
else
run_ok
(List.concat
[ c_flags
; [ "-I"; t.stdlib_dir; "-o"; exe_fname; c_fname ]
; t.c_libraries
; link_flags
])
in
Expand All @@ -436,7 +445,9 @@ let compile_c_prog t ?(c_flags = []) code =
let ok =
Process.run_command_ok t ~dir
(Process.command_args t.c_compiler
(c_flags @ [ "-I"; t.stdlib_dir; "-o"; obj_fname; "-c"; c_fname ]))
( c_flags
@ "-I" :: t.stdlib_dir :: "-o" :: obj_fname :: "-c" :: c_fname
:: t.c_libraries ))
in
if ok then
Ok obj_fname
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module C = Configurator.V1

let unix = {|
#include <math.h>
void *addr = &sin;
int main(void) {
return 0;
}
|}

let windows = {|
#include <winsock2.h>
void *addr = &gethostname;
int main(void) {
return 0;
}
|}

let main c =
let code = if Sys.os_type = "Win32" then windows else unix in
let b = C.c_test c code in
let f = open_out_bin "out" in
output_char f (if b then '1' else '0');
close_out f

let () = C.main ~name:"configurator-c-libraries" main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(executable
(name discover)
(libraries dune.configurator))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 2.8)
13 changes: 13 additions & 0 deletions test/blackbox-tests/test-cases/configurator-c-libraries.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Test that configurator always picks the value of the `c_libraries`
flag from `ocamlc -config`. If not, there's a failure to link a
configuration test program that uses functions from these libraries.
For that, we need functions outside of libc. On Unix, that would be
`sin(3)` that requires the `-lm` flag for the math library, and on
Windows `gethostname` that requires WinSock2 (`ws2_32.dll`).
link successfully
==================================
$ dune exec -- ./discover.exe
$ cat out
1

0 comments on commit 329ba07

Please sign in to comment.