From: Ralf Treinen <treinen@debian.org>
Date: Thu, 2 Apr 2020 12:25:58 +0200
Subject: Fix location where menhir thinks the lib is installed

Debian-bug: #955494
---
 base/Installation.ml | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/base/Installation.ml b/base/Installation.ml
index b13b3f1..2f87dd8 100644
--- a/base/Installation.ml
+++ b/base/Installation.ml
@@ -36,13 +36,28 @@ let rec normalize fn =
    and hope that it is of the form [.../bin/menhir]. We change this to
    [.../lib/menhirLib], and hope that this is where MenhirLib is installed. *)
 
+let capture_stdout cmd args =
+  let stdout = Filename.temp_file "menhir" "" in
+  Fun.protect
+    ~finally:(fun () -> Sys.remove stdout)
+    (fun () ->
+      let c = Filename.quote_command cmd ~stdout args in
+      if Sys.command c = 0 then
+        Some (In_channel.with_open_text stdout In_channel.input_all)
+      else None)
+
 let libdir () =
-  let root =
-    Sys.executable_name
-    |> normalize
-    |> Filename.dirname (* remove [menhir] *)
-    |> Filename.dirname (* remove [bin] *)
-  in
-  Filename.concat
-    root
-    (Filename.concat "lib" "menhirLib")
+  match capture_stdout "ocamlc" [ "-where" ] with
+  | Some p ->
+      (* Debian-specific *)
+      Filename.concat (String.trim p) "menhirLib"
+  | None ->
+      let root =
+        Sys.executable_name
+        |> normalize
+        |> Filename.dirname (* remove [menhir] *)
+        |> Filename.dirname (* remove [bin] *)
+      in
+      Filename.concat
+        root
+        (Filename.concat (Filename.concat "lib" "ocaml") "menhirLib")
