ocaml-dune/3739.patch

46 lines
1.7 KiB
Diff
Raw Normal View History

From 4a1fee49b97e7bd4da368c83c5a28cdbde9e140d Mon Sep 17 00:00:00 2001
From: Rudi Grinberg <me@rgrinberg.com>
Date: Sat, 22 Aug 2020 20:56:56 -0700
Subject: [PATCH] [configurator] more flexible #define parsing
Allow duplicate values for the same key as long as they are the same.
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
---
otherlibs/configurator/src/v1.ml | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/otherlibs/configurator/src/v1.ml b/otherlibs/configurator/src/v1.ml
index 1cdc5c5672..721c0e7384 100644
--- a/otherlibs/configurator/src/v1.ml
+++ b/otherlibs/configurator/src/v1.ml
@@ -532,13 +532,26 @@ const char *s%i = "BEGIN-%i-false-END";
let extract_values obj_file vars =
let values =
Io.with_lexbuf_from_file obj_file ~f:(Extract_obj.extract [])
- |> Int.Map.of_list_exn
+ |> List.fold_left ~init:Int.Map.empty ~f:(fun acc (key, v) ->
+ Int.Map.update acc ~key ~f:(function
+ | None -> Some (Ok v)
+ | Some (Error vs) -> Some (Error (v :: vs))
+ | Some (Ok v') ->
+ Some
+ ( if v = v' then
+ Ok v'
+ else
+ Error [ v; v' ] )))
in
List.mapi vars ~f:(fun i (name, t) ->
let raw_val =
match Int.Map.find values i with
| None -> die "Unable to get value for %s" name
- | Some v -> v
+ | Some (Ok v) -> v
+ | Some (Error vs) ->
+ let vs = List.sort_uniq ~cmp:compare vs in
+ die "Duplicate values for %s:\n%s" name
+ (vs |> List.map ~f:(sprintf "- %s") |> String.concat ~sep:"\n")
in
let value =
match t with