From cc845e5fdbf0626063172dcaba99144aec65add8 Mon Sep 17 00:00:00 2001 From: Jeremie Dimino Date: Tue, 6 Mar 2018 18:01:48 +0000 Subject: [PATCH] Make all variables accessible in jbuild files --- doc/jbuild.rst | 9 +++++-- src/super_context.ml | 63 ++++++++++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/doc/jbuild.rst b/doc/jbuild.rst index 626a5aca..71f2cfe4 100644 --- a/doc/jbuild.rst +++ b/doc/jbuild.rst @@ -694,8 +694,13 @@ Jbuilder supports the following variables: - ``null`` is ``/dev/null`` on Unix or ``nul`` on Windows - ``ext_obj``, ``ext_asm``, ``ext_lib``, ``ext_dll`` and ``ext_exe`` are the file extension used for various artifacts -- ``bytecomp_c_libraries`` and ``native_c_libraries`` are list of C libraries - used by the OCaml runtimes +- ``ocaml-config:v`` for every variable ``v`` in the output of + ``ocamlc -config``. Note that output Jbuilder processes the output + of ``ocamlc -config`` in order to make it a bit more stable across + versions, so the exact set of variables accessible this way might + not be exactly the same as what you can see in the output of + ``ocamlc -config``. In particular, variables added in new versions + of OCaml needs to be registered in Jbuilder before they can be used In addition, ``(action ...)`` fields support the following special variables: diff --git a/src/super_context.ml b/src/super_context.ml index faa02a66..68eec227 100644 --- a/src/super_context.ml +++ b/src/super_context.ml @@ -144,32 +144,43 @@ let create let strings l = Strings (l , Split) in let string s = Strings ([s], Split) in let path p = Paths ([p], Split) in - [ "-verbose" , Strings ([] (*"-verbose";*), Concat) - ; "CPP" , strings (context.c_compiler :: cflags @ ["-E"]) - ; "PA_CPP" , strings (context.c_compiler :: cflags - @ ["-undef"; "-traditional"; "-x"; "c"; "-E"]) - ; "CC" , strings (context.c_compiler :: cflags) - ; "CXX" , strings (context.c_compiler :: cxx_flags) - ; "ocaml_bin" , path context.ocaml_bin - ; "OCAML" , path context.ocaml - ; "OCAMLC" , path context.ocamlc - ; "OCAMLOPT" , path ocamlopt - ; "ocaml_version" , string context.version_string - ; "ocaml_where" , string (Path.to_string context.stdlib_dir) - ; "ARCH_SIXTYFOUR" , string (string_of_bool context.arch_sixtyfour) - ; "MAKE" , make - ; "null" , string (Path.to_string Config.dev_null) - ; "ext_obj" , string context.ext_obj - ; "ext_asm" , string context.ext_asm - ; "ext_lib" , string context.ext_lib - ; "ext_dll" , string context.ext_dll - ; "ext_exe" , string context.ext_exe - ; "bytecomp_c_libraries", strings context.bytecomp_c_libraries - ; "native_c_libraries" , strings context.bytecomp_c_libraries - ] - |> String_map.of_list - |> function - | Ok x -> x + let vars = + [ "-verbose" , Strings ([] (*"-verbose";*), Concat) + ; "CPP" , strings (context.c_compiler :: cflags @ ["-E"]) + ; "PA_CPP" , strings (context.c_compiler :: cflags + @ ["-undef"; "-traditional"; + "-x"; "c"; "-E"]) + ; "CC" , strings (context.c_compiler :: cflags) + ; "CXX" , strings (context.c_compiler :: cxx_flags) + ; "ocaml_bin" , path context.ocaml_bin + ; "OCAML" , path context.ocaml + ; "OCAMLC" , path context.ocamlc + ; "OCAMLOPT" , path ocamlopt + ; "ocaml_version" , string context.version_string + ; "ocaml_where" , string (Path.to_string context.stdlib_dir) + ; "ARCH_SIXTYFOUR" , string (string_of_bool context.arch_sixtyfour) + ; "MAKE" , make + ; "null" , string (Path.to_string Config.dev_null) + ; "ext_obj" , string context.ext_obj + ; "ext_asm" , string context.ext_asm + ; "ext_lib" , string context.ext_lib + ; "ext_dll" , string context.ext_dll + ; "ext_exe" , string context.ext_exe + ] + in + let vars = + vars @ + List.map (Ocaml_config.to_list context.ocaml_config) ~f:(fun (k, v) -> + ("ocaml-config:" ^ k, + match (v : Ocaml_config.Value.t) with + | Bool x -> string (string_of_bool x) + | Int x -> string (string_of_int x) + | String x -> string x + | Words x -> strings x + | Prog_and_args x -> strings (x.prog :: x.args))) + in + match String_map.of_list vars with + | Ok x -> x | Error _ -> assert false in { context