From 23576a4832e3b55a81c63c44d1f4b5eab516506b Mon Sep 17 00:00:00 2001 From: Anil Madhavapeddy Date: Fri, 1 Jun 2018 20:14:36 +0100 Subject: [PATCH] configurator: add write_lines function The `write_flags` only works with `(:include` directives, and it is also useful to be able to write a list of lines so that the discovered information can be used in variable expansion actions. For example, ocaml-yaml discovers CFLAGS and then directly has `(run ${CC} ${read-lines:cflags})` actions that use this new write_lines function to list cflags instead of s-expressions. They must be line-by-line or else variable expansion doesnt work since CFLAGS contain spaces. Signed-off-by: Anil Madhavapeddy --- src/configurator/v1.ml | 27 ++++++++++++++++++++------- src/configurator/v1.mli | 30 ++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/configurator/v1.ml b/src/configurator/v1.ml index 1f11d417..152022d9 100644 --- a/src/configurator/v1.ml +++ b/src/configurator/v1.ml @@ -61,6 +61,24 @@ module Temp = struct dir end +module Flags = struct + let extract_words = String.extract_words + + let extract_comma_space_separated_words = + String.extract_comma_space_separated_words + + let extract_blank_separated_words = String.extract_blank_separated_words + + let write_lines fname s = + let path = Path.of_string fname in + Io.write_lines path s + + let write_sexp fname s = + let path = Path.in_source fname in + let sexp = Usexp.List (List.map s ~f:(fun s -> Usexp.Quoted_string s)) in + Io.write_file path (Usexp.to_string sexp ~syntax:Dune) +end + module Find_in_path = struct let path_sep = if Sys.win32 then @@ -485,18 +503,13 @@ module Pkg_config = struct None end -let write_flags fname s = - let path = Path.in_source fname in - let sexp = Usexp.List (List.map s ~f:(fun s -> Usexp.Quoted_string s)) in - Io.write_file path (Usexp.to_string sexp ~syntax:Dune) - let main ?(args=[]) ~name f = let ocamlc = ref ( match Sys.getenv "DUNE_CONFIGURATOR" with | s -> Some s | exception Not_found -> - die "Configurator scripts must be ran with jbuilder. \ - To manually run a script, use $ jbuilder exec." + die "Configurator scripts must be run with Dune. \ + To manually run a script, use $ dune exec." ) in let verbose = ref false in let dest_dir = ref None in diff --git a/src/configurator/v1.mli b/src/configurator/v1.mli index 5eb9ebb7..20cc9292 100644 --- a/src/configurator/v1.mli +++ b/src/configurator/v1.mli @@ -87,10 +87,32 @@ module Pkg_config : sig val query : t -> package:string -> package_conf option end with type configurator := t -val write_flags : string -> string list -> unit -(** [write_flags fname s] write the list of strings [s] to the file - [fname] in an appropriate format so that it can used in jbuild - files with "(:include [fname])". *) +module Flags : sig + + val write_sexp : string -> string list -> unit + (** [write_sexp fname s] writes the list of strings [s] to the file [fname] in + an appropriate format so that it can used in jbuild files with [(:include + [fname])]. *) + + val write_lines : string -> string list -> unit + (** [write_lines fname s] writes the list of string [s] to the file [fname] + with one line per string so that it can be used in Dune action rules with + [%{read-lines:}]. *) + + val extract_comma_space_separated_words : string -> string list + (** [extract_comma_space_separated_words s] returns a list of words in + [s] that are separated by a newline, tab, space or comma character. *) + + val extract_blank_separated_words : string -> string list + (** [extract_blank_separated_words s] returns a list of words in [s] + that are separated by a tab or space character. *) + + val extract_words : string -> is_word_char:(char -> bool) -> string list + (** [extract_words s ~is_word_char] will split the string [s] into + a list of words. A valid word character is defined by the [is_word_char] + predicate returning true and anything else is considered a separator. + Any blank words are filtered out of the results. *) +end (** Typical entry point for configurator programs *) val main