From 69b76f0c8e4db95b02d37629772a1ce7916bd4af Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Mon, 16 Apr 2018 20:25:51 +0700 Subject: [PATCH] Add ?prelude argument when import constants A user can now define their own macros to use with extracting compile time info --- src/configurator/v1.ml | 3 ++- src/configurator/v1.mli | 3 +++ .../test-cases/configurator/import-define/run.ml | 3 ++- test/blackbox-tests/test-cases/configurator/run.t | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/configurator/v1.ml b/src/configurator/v1.ml index 154c6f37..80d46a0f 100644 --- a/src/configurator/v1.ml +++ b/src/configurator/v1.ml @@ -283,12 +283,13 @@ module C_define = struct | String of string end - let import t ?c_flags ?link_flags ~includes vars = + let import t ?prelude ?c_flags ?link_flags ~includes vars = let buf = Buffer.create 1024 in let pr fmt = Printf.bprintf buf (fmt ^^ "\n") in let includes = "stdio.h" :: includes in List.iter includes ~f:(pr "#include <%s>"); pr ""; + Option.iter prelude ~f:(pr "%s"); pr "int main()"; pr "{"; List.iter vars ~f:(fun (name, (kind : Type.t)) -> diff --git a/src/configurator/v1.mli b/src/configurator/v1.mli index 3212e90e..e2d3c8b8 100644 --- a/src/configurator/v1.mli +++ b/src/configurator/v1.mli @@ -44,6 +44,9 @@ module C_define : sig *) val import : t + -> ?prelude: string + (** Define extra code be used with extracting values below. Note that the + compiled code is never executed. *) -> ?c_flags: string list -> ?link_flags:string list -> includes: string list diff --git a/test/blackbox-tests/test-cases/configurator/import-define/run.ml b/test/blackbox-tests/test-cases/configurator/import-define/run.ml index f1571eb9..412b6486 100644 --- a/test/blackbox-tests/test-cases/configurator/import-define/run.ml +++ b/test/blackbox-tests/test-cases/configurator/import-define/run.ml @@ -4,10 +4,11 @@ let () = let module C_define = Configurator.C_define in Configurator.main ~name:"c_test" (fun t -> C_define.import t + ~prelude:{|#define CONFIGURATOR_TESTING "foobar"|} ~includes:["caml/config.h"] [ "CAML_CONFIG_H", C_define.Type.Switch ; "Page_log", C_define.Type.Int - ; "__func__", C_define.Type.String + ; "CONFIGURATOR_TESTING", C_define.Type.String ] |> List.iter (fun (n, v) -> Printf.printf "%s=%s\n" diff --git a/test/blackbox-tests/test-cases/configurator/run.t b/test/blackbox-tests/test-cases/configurator/run.t index 254d3076..3e29af3b 100644 --- a/test/blackbox-tests/test-cases/configurator/run.t +++ b/test/blackbox-tests/test-cases/configurator/run.t @@ -11,4 +11,4 @@ Importing #define's from code is successful $ jbuilder exec import-define/run.exe CAML_CONFIG_H=true Page_log=12 - __func__=main + CONFIGURATOR_TESTING=foobar