caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* First release of Oug: reference graph from ocaml code
@ 2008-06-03 16:38 Maxence Guesdon
  2008-06-09  8:47 ` [Caml-list] " RABIH.ELCHAAR
  0 siblings, 1 reply; 6+ messages in thread
From: Maxence Guesdon @ 2008-06-03 16:38 UTC (permalink / raw)
  To: caml-list

Hello,

I'm glad to announce the first release of Oug (0.1).

Oug is a code analysis tool building reference graphs from OCaml code, that
is graphs representing which elements (value, module, class, ...) reference
which elements.

It comes with the Ouglib library to be able to embed the analyzer in other
OCaml applications.

From the graph, various outputs can be obtained: list of elements not
referenced anywhere ("dead code"), various graphiz graphs, ...

Oug does not compute the call graph of the given OCaml code; this would
require data flow analysis. For this reason, the "dead code" dump always
consider that every method is referenced.

The homepage is here:
  http://pauillac.inria.fr/~guesdon/oug.en.html

Regards,

Maxence Guesdon


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [Caml-list] First release of Oug: reference graph from ocaml code
  2008-06-03 16:38 First release of Oug: reference graph from ocaml code Maxence Guesdon
@ 2008-06-09  8:47 ` RABIH.ELCHAAR
  2008-06-09 11:43   ` Maxence Guesdon
  0 siblings, 1 reply; 6+ messages in thread
From: RABIH.ELCHAAR @ 2008-06-09  8:47 UTC (permalink / raw)
  To: maxence.guesdon, caml-list

Dear Sir,
Your post on the caml-list concerning the OUG tool attracted my attention, and I am planning to try to make it work, under a Win32, MSVC Ocaml version.

It seems that we need to point to a compiled ocaml source tree, to get OUG to compile.
My question concerns the location of the Ocaml 3.11 under CVS.
Looking at the http://camlcvs.inria.fr/cgi-bin/cvsweb/ocaml/ nothing in the names seems to point to ocaml 3.11.

Can you please point out a branch that responds to this requirement?

TIA,
Rabih Chaar

-----Message d'origine-----
De : caml-list-bounces@yquem.inria.fr [mailto:caml-list-bounces@yquem.inria.fr] De la part de Maxence Guesdon
Envoyé : mardi 3 juin 2008 18:39
À : caml-list@inria.fr
Objet : [Caml-list] First release of Oug: reference graph from ocaml code

Hello,

I'm glad to announce the first release of Oug (0.1).

Oug is a code analysis tool building reference graphs from OCaml code, that
is graphs representing which elements (value, module, class, ...) reference
which elements.

It comes with the Ouglib library to be able to embed the analyzer in other
OCaml applications.

>From the graph, various outputs can be obtained: list of elements not
referenced anywhere ("dead code"), various graphiz graphs, ...

Oug does not compute the call graph of the given OCaml code; this would
require data flow analysis. For this reason, the "dead code" dump always
consider that every method is referenced.

The homepage is here:
  http://pauillac.inria.fr/~guesdon/oug.en.html

Regards,

Maxence Guesdon

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
This message and any attachments (the "message") are confidential, intended solely for the addressee(s), and may contain legally privileged information. 
Any unauthorised use or dissemination is prohibited. 
E-mails are susceptible to alteration. 
Neither Societe Generale Asset Management nor any of its subsidiaries or affiliates shall be liable for the message if altered, changed or falsified. 
  
Find out more about Societe Generale Asset Management's proposal on www.sgam.com
  
                                ******** 
  
Ce message et toutes les pieces jointes (ci-apres le "message") sont confidentiels et susceptibles de contenir des informations couvertes par le secret professionnel.
Ce message est etabli a l'intention exclusive de ses destinataires.
Toute utilisation ou diffusion non autorisee est interdite. 
Tout message electronique est susceptible d'alteration. Societe Generale Asset Management et ses filiales declinent toute responsabilite au titre de ce message s'il a ete altere, deforme ou falsifie. 

Decouvrez l'offre et les services de Societe Generale Asset Management sur le site www.sgam.fr


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] First release of Oug: reference graph from ocaml code
  2008-06-09  8:47 ` [Caml-list] " RABIH.ELCHAAR
@ 2008-06-09 11:43   ` Maxence Guesdon
  2008-06-09 18:03     ` RABIH.ELCHAAR
  0 siblings, 1 reply; 6+ messages in thread
From: Maxence Guesdon @ 2008-06-09 11:43 UTC (permalink / raw)
  To: RABIH.ELCHAAR; +Cc: caml-list

On Mon, 9 Jun 2008 10:47:18 +0200
<RABIH.ELCHAAR@sgam.com> wrote:

> Dear Sir,

Hello,

> Your post on the caml-list concerning the OUG tool attracted my attention, and I am planning to try to make it work, under a Win32, MSVC Ocaml version.
> 
> It seems that we need to point to a compiled ocaml source tree, to get OUG to compile.
> My question concerns the location of the Ocaml 3.11 under CVS.
> Looking at the http://camlcvs.inria.fr/cgi-bin/cvsweb/ocaml/ nothing in the names seems to point to ocaml 3.11.
> 
> Can you please point out a branch that responds to this requirement?

In fact it corresponds to the trunk in the CVS repository, where the future
3.11 version is developped. So it is accessible as told on
  http://camlcvs.inria.fr/cvsserver-eng.html
using "ocaml" as dirname.

Regards,

Maxence Guesdon

-- 
Maxence Guesdon                           http://yquem.inria.fr/~guesdon/
Service Expérimentation et Développements https://devel.inria.fr/rocq/
INRIA Paris-Rocquencourt                  http://www.inria.fr/rocquencourt/




^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [Caml-list] First release of Oug: reference graph from ocaml code
  2008-06-09 11:43   ` Maxence Guesdon
@ 2008-06-09 18:03     ` RABIH.ELCHAAR
  2008-06-10  7:40       ` Maxence Guesdon
  0 siblings, 1 reply; 6+ messages in thread
From: RABIH.ELCHAAR @ 2008-06-09 18:03 UTC (permalink / raw)
  To: Maxence.Guesdon; +Cc: caml-list

Thanks for yor reply.
After some investigations, the tool seems to be only targetting a unix version of ocaml.

Some things in checkocaml.ml are easy to patch, like the separator ":" or ";" according to Sys.os_type. Also the distribution generates ocamlc.exe, which fails the search in function find_in_path.

Nevertheless, there are some calls to fork which is not supported in MSVC distrib. I didn't look more thoroughly to see if this can be patched.

Is there a plan to deliver a portable version of the tool?

TIA,
Rabih Chaar


-----Message d'origine-----
De : caml-list-bounces@yquem.inria.fr [mailto:caml-list-bounces@yquem.inria.fr] De la part de Maxence Guesdon
Envoyé : lundi 9 juin 2008 13:43
À : EL CHAAR Rabih SGAM/AI/SAM
Cc : caml-list@inria.fr
Objet : Re: [Caml-list] First release of Oug: reference graph from ocaml code

On Mon, 9 Jun 2008 10:47:18 +0200
<RABIH.ELCHAAR@sgam.com> wrote:

> Dear Sir,

Hello,

> Your post on the caml-list concerning the OUG tool attracted my attention, and I am planning to try to make it work, under a Win32, MSVC Ocaml version.
> 
> It seems that we need to point to a compiled ocaml source tree, to get OUG to compile.
> My question concerns the location of the Ocaml 3.11 under CVS.
> Looking at the http://camlcvs.inria.fr/cgi-bin/cvsweb/ocaml/ nothing in the names seems to point to ocaml 3.11.
> 
> Can you please point out a branch that responds to this requirement?

In fact it corresponds to the trunk in the CVS repository, where the future
3.11 version is developped. So it is accessible as told on
  http://camlcvs.inria.fr/cvsserver-eng.html
using "ocaml" as dirname.

Regards,

Maxence Guesdon

-- 
Maxence Guesdon                           http://yquem.inria.fr/~guesdon/
Service Expérimentation et Développements https://devel.inria.fr/rocq/
INRIA Paris-Rocquencourt                  http://www.inria.fr/rocquencourt/



_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
This message and any attachments (the "message") are confidential, intended solely for the addressee(s), and may contain legally privileged information. 
Any unauthorised use or dissemination is prohibited. 
E-mails are susceptible to alteration. 
Neither Societe Generale Asset Management nor any of its subsidiaries or affiliates shall be liable for the message if altered, changed or falsified. 
  
Find out more about Societe Generale Asset Management's proposal on www.sgam.com
  
                                ******** 
  
Ce message et toutes les pieces jointes (ci-apres le "message") sont confidentiels et susceptibles de contenir des informations couvertes par le secret professionnel.
Ce message est etabli a l'intention exclusive de ses destinataires.
Toute utilisation ou diffusion non autorisee est interdite. 
Tout message electronique est susceptible d'alteration. Societe Generale Asset Management et ses filiales declinent toute responsabilite au titre de ce message s'il a ete altere, deforme ou falsifie. 

Decouvrez l'offre et les services de Societe Generale Asset Management sur le site www.sgam.fr


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] First release of Oug: reference graph from ocaml code
  2008-06-09 18:03     ` RABIH.ELCHAAR
@ 2008-06-10  7:40       ` Maxence Guesdon
  2008-06-10 14:25         ` RABIH.ELCHAAR
  0 siblings, 1 reply; 6+ messages in thread
From: Maxence Guesdon @ 2008-06-10  7:40 UTC (permalink / raw)
  To: RABIH.ELCHAAR; +Cc: caml-list

On Mon, 9 Jun 2008 20:03:08 +0200
<RABIH.ELCHAAR@sgam.com> wrote:

> Thanks for yor reply.
> After some investigations, the tool seems to be only targetting a unix version of ocaml.

Indeed. I have no Microsoft Windows.

> Some things in checkocaml.ml are easy to patch, like the separator ":" or ";" according to Sys.os_type. Also the distribution generates ocamlc.exe, which fails the search in function find_in_path.
> 
> Nevertheless, there are some calls to fork which is not supported in MSVC distrib. I didn't look more thoroughly to see if this can be patched.
> 
> Is there a plan to deliver a portable version of the tool?

Not from me, but I can integrate another contributed configuration script.

Regards,

Maxence Guesdon


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [Caml-list] First release of Oug: reference graph from ocaml code
  2008-06-10  7:40       ` Maxence Guesdon
@ 2008-06-10 14:25         ` RABIH.ELCHAAR
  0 siblings, 0 replies; 6+ messages in thread
From: RABIH.ELCHAAR @ 2008-06-10 14:25 UTC (permalink / raw)
  To: maxence.guesdon; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 2921 bytes --]

Here is an adaptation of your script to handle both windows and unix environement.
The idea is to replace call to fork with create_process.

I mainly define a Win32Exe module, defining both functions
execvp_to_list and execvp_redirect.
The win32 users should simply open the module.
Some variable were also redefined to support both cases (path separator, ...).

I also had some exceptions when the call to Unix.single_write was called, so I replaced it to Unix.write in the win32 case.

The rest is kept unchanged.

I am adding the modified checkocaml.ml -> checkocaml_win.ml along with the
Diff file: diff -w checkocaml_win.ml checkocaml.ml > conf_script.diff

Please feel free to contact me if you need more precisions.

Sincerely,
Rabih Chaar

-----Message d'origine-----
De : Maxence Guesdon [mailto:maxence.guesdon@inria.fr] 
Envoyé : mardi 10 juin 2008 09:40
À : EL CHAAR Rabih SGAM/AI/SAM
Cc : caml-list@inria.fr
Objet : Re: [Caml-list] First release of Oug: reference graph from ocaml code

On Mon, 9 Jun 2008 20:03:08 +0200
<RABIH.ELCHAAR@sgam.com> wrote:

> Thanks for yor reply.
> After some investigations, the tool seems to be only targetting a unix version of ocaml.

Indeed. I have no Microsoft Windows.

> Some things in checkocaml.ml are easy to patch, like the separator ":" or ";" according to Sys.os_type. Also the distribution generates ocamlc.exe, which fails the search in function find_in_path.
> 
> Nevertheless, there are some calls to fork which is not supported in MSVC distrib. I didn't look more thoroughly to see if this can be patched.
> 
> Is there a plan to deliver a portable version of the tool?

Not from me, but I can integrate another contributed configuration script.

Regards,

Maxence Guesdon
This message and any attachments (the "message") are confidential, intended solely for the addressee(s), and may contain legally privileged information. 
Any unauthorised use or dissemination is prohibited. 
E-mails are susceptible to alteration. 
Neither Societe Generale Asset Management nor any of its subsidiaries or affiliates shall be liable for the message if altered, changed or falsified. 
  
Find out more about Societe Generale Asset Management's proposal on www.sgam.com
  
                                ******** 
  
Ce message et toutes les pieces jointes (ci-apres le "message") sont confidentiels et susceptibles de contenir des informations couvertes par le secret professionnel.
Ce message est etabli a l'intention exclusive de ses destinataires.
Toute utilisation ou diffusion non autorisee est interdite. 
Tout message electronique est susceptible d'alteration. Societe Generale Asset Management et ses filiales declinent toute responsabilite au titre de ce message s'il a ete altere, deforme ou falsifie. 

Decouvrez l'offre et les services de Societe Generale Asset Management sur le site www.sgam.fr

[-- Attachment #2: conf_script.diff --]
[-- Type: application/octet-stream, Size: 6965 bytes --]

36d35
< let _ = Printexc.record_backtrace true
83,90d81
<   (* taking out spaces at end : problems occuring under windows build *)
<   let rec trim_right s =
<     let length = String.length s in
<     match s.[length-1] with
<     |' '|'\n'|'\r'|'\t' -> trim_right (String.sub s 0 (length-1))
<     |_ -> s in
<   let suf = trim_right suf in
<   let s = trim_right s in
130,134c121
< let path_sep =
<   match (Sys.os_type = "Win32")with
<   |false ->":"
<   |true -> ";";;
< 
---
> let path_sep = ":";;
153,156c140
< let get_path () =
<   let s = list_of_path (getenv "PATH") in
< (*   List.iter print_endline s; *)
<   s;;
---
> let get_path () = list_of_path (getenv "PATH");;
233,238d216
< (* patch single_write raising exceptions under windows *)
< let single_write =
<   match (Sys.os_type = "Win32")with
<   |true -> write
<   |false -> single_write
< 
306d283
< 
398,524d374
< 
< (* wrapping execution and fork use to be windows compliant,
<    using therefore create_process *)
< module WinExec =
<   struct
<     let read_text_file f =
<       let ic = open_in f in
<       let rec read s =
<         try
<           let line = input_line ic in
<           read (line::s)
<         with End_of_file ->
<           (List.rev s) in
<       let s = read [] in
<       close_in ic;
<       s
< 
<     let clear f =
<       try
<         Sys.remove f
<       with _ -> ()
< 
<     let default_temp_dir = "C:/temp/"
<     let execvp_to_list cmd args =
<       (* we create file based stdin and stdout, giving the temp dir *)
<       let dir =
<         try
<           Unix.getenv "TEMP"
<         with _ -> default_temp_dir in
<       let makeFile dir tag =
<         let makeAddend ()=
<           let addend = Random.int 20000 in
<           Digest.to_hex (Digest.string (Marshal.to_string addend [])) in
<         let file = ref (dir^makeAddend()^tag) in
<         while (Sys.file_exists !file) do
<           file := dir^makeAddend()^tag;
<         done;
<         !file in
<       let output = makeFile dir "output.txt" in
<       let error = makeFile dir "err.txt" in
<       let (sil,sor,ser) =
<         let sil, sir = pipe () in
<         let sor = Unix.openfile output [O_WRONLY;O_CREAT] 0o640 in
<         let ser = Unix.openfile error [O_WRONLY;O_CREAT] 0o640 in
<         (sil,sor,ser) in
<       let pid =
<         create_process_env cmd (Array.append [| cmd |] args) (Unix.environment())
<           sil sor ser in
<       let _,status = Unix.waitpid [] pid in
<       Unix.close sor;
<       Unix.close ser;
<       match status with
<       | WEXITED 0 ->
<           let res = read_text_file output in
<           clear output;
<           clear error;
<           res
<       | WEXITED i ->
<           let errorTrace =read_text_file error in
<           (* we can print the error trace if needed *)
<           clear output;
<           clear error;
<           raise Exec_failure
<       | _ ->
<           clear output;
<           clear error;
<           raise Exec_failure
< 
<     let execvp_redirect redirections cmd args  =
<       let perm = 0o640 in
<       let temp_file =
<         if List.exists (function In_from_string _ -> true | _ -> false)
<             redirections
<         then Some (create_temp_file ~ext: ".in" ())
<         else None in
<       let rec make_redirect rd =
<         match rd with
<           In_from_file file ->
<             let desc_file = openfile file [O_RDONLY] perm in
<             try_finalize (dup2 desc_file) stdin close desc_file
<         | Out_to_file file ->
<             let desc_file = openfile file [O_WRONLY;O_CREAT;O_TRUNC] perm in
<             try_finalize (dup2 desc_file) stdout close desc_file
<         | Err_to_file file  ->
<             let desc_file = openfile file [O_WRONLY;O_CREAT;O_TRUNC] perm in
<             try_finalize (dup2 desc_file) stderr close desc_file
<         | Out_append_to_file file ->
<             let desc_file = openfile file [O_WRONLY;O_APPEND;O_CREAT] perm in
<             try_finalize (dup2 desc_file) stdout close desc_file
<         | Err_to_out -> dup2 stdout stderr
<         | In_from_string s ->
<             begin match temp_file with
<               Some tmp ->
<                 file_of_string ~file: tmp ~contents: s;
<                 make_redirect (In_from_file tmp);
<             | None -> assert false
<             end
<         | Out_null ->
<             make_redirect (Out_to_file "/dev/null")
<         | Err_null ->
<             make_redirect (Err_to_file "/dev/null")
<         | Silent ->
<             make_redirect Out_null;
<             make_redirect Err_to_out;
<       in
<       (* we save stdin, stdout, stderr to restore them after fork *)
<       let stdout_back = Unix.dup Unix.stdout in
<       let stderr_back = Unix.dup Unix.stderr in
<       let stdin_back = Unix.dup Unix.stdin in
<       List.iter make_redirect redirections;
<       let pid = create_process cmd (Array.append [|cmd|] args) Unix.stdin Unix.stdout Unix.stderr in
<       let _,res = waitpid [] pid in
<       begin match temp_file with
<       |Some tmp -> unlink_f tmp
<       | _ -> ()
<       end;
<       (* restorting stdin, stdout, stderr *)
<       Unix.dup2 stdout_back Unix.stdout;
<       Unix.dup2 stderr_back Unix.stderr;
<       Unix.dup2 stdin_back Unix.stdin;
<       res;;
< end
< 
< (* under windows, replace the two functions by those provided by WinExec module *)
< open WinExec;;
< 
< 
756,761d605
< 
< let exe_extension =
<   match (Sys.os_type="Win32") with
<   |true -> ".exe"
<   |false -> "";;
< 
763c607
<   let ocamlc = ocaml_prog ("ocamlc"^exe_extension) in
---
>   let ocamlc = ocaml_prog "ocamlc" in
771,773c615,617
<     ocaml = ocaml_prog ("ocaml"^exe_extension) ;
<     ocamlopt = ocaml_prog ~err: withopt ("ocamlopt"^exe_extension) ;
<     ocamldoc = ocaml_prog ("ocamldoc"^exe_extension) ;
---
>     ocaml = ocaml_prog "ocaml" ;
>     ocamlopt = ocaml_prog ~err: withopt "ocamlopt" ;
>     ocamldoc = ocaml_prog "ocamldoc" ;
775,782c619,626
<     ocamlyacc = ocaml_prog ("ocamlyacc"^exe_extension) ;
<     ocamllex = ocaml_prog ("ocamllex"^exe_extension) ;
<     ocamldep = ocaml_prog ("ocamldep"^exe_extension) ;
<     ocamlmklib = ocaml_prog ("ocamlmklib"^exe_extension) ;
<     ocamlmktop = ocaml_prog ("ocamlmktop"^exe_extension) ;
<     ocamlprof = ocaml_prog ("ocamlprof"^exe_extension) ;
<     camlp4 = ocaml_prog ("camlp4"^exe_extension) ;
<     ocamlfind = ocaml_prog ~err: ocamlfind ("ocamlfind"^exe_extension) ;
---
>     ocamlyacc = ocaml_prog "ocamlyacc" ;
>     ocamllex = ocaml_prog "ocamllex" ;
>     ocamldep = ocaml_prog "ocamldep" ;
>     ocamlmklib = ocaml_prog "ocamlmklib" ;
>     ocamlmktop = ocaml_prog "ocamlmktop" ;
>     ocamlprof = ocaml_prog "ocamlprof" ;
>     camlp4 = ocaml_prog "camlp4" ;
>     ocamlfind = ocaml_prog ~err: ocamlfind "ocamlfind" ;
984c828
<   try ocaml_prog ~err: true ("ocamlbuild"^exe_extension)
---
>   try ocaml_prog ~err: true "ocamlbuild"

[-- Attachment #3: checkocaml_win.ml --]
[-- Type: application/octet-stream, Size: 33698 bytes --]

(*********************************************************************************)
(*                Oug                                                            *)
(*                                                                               *)
(*    Copyright (C) 2008 Institut National de Recherche en Informatique          *)
(*    et en Automatique. All rights reserved.                                    *)
(*                                                                               *)
(*    This program is free software; you can redistribute it and/or modify       *)
(*    it under the terms of the GNU General Public License as                    *)
(*    published by the Free Software Foundation; either version 2 of the         *)
(*    License, or any later version.                                             *)
(*                                                                               *)
(*    This program is distributed in the hope that it will be useful,            *)
(*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *)
(*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *)
(*    GNU Library General Public License for more details.                       *)
(*                                                                               *)
(*    You should have received a copy of the GNU Library General Public          *)
(*    License along with this program; if not, write to the Free Software        *)
(*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                   *)
(*    02111-1307  USA                                                            *)
(*                                                                               *)
(*    Contact: Maxence.Guesdon@inria.fr                                          *)
(*                                                                               *)
(*********************************************************************************)

(** Detection of OCaml tools and libraries. *)

(* $Id$ *)

(*c==m=[OCaml_conf]=0.3=t==*)


open Sys
open Unix

let _ = Printexc.record_backtrace true
(** {2:configuration Configuration}
    You can change these values to modify the behaviour of some functions. *)

(** The prefix of the temporary files created. *)
let temp_files_prefix = ref "config_check"

(** The default extension for the temporary files created. *)
let temp_files_ext = ref "ml"

(** Set this to [true] to enable debug mode.
    Temporary files are not deleted when it is enabled. *)
let debug = ref false

(** The function used to print progress messages and other information. *)
let print = ref (fun s -> print_string s; flush Pervasives.stdout)

(** [!string_of_bool b] should return a message according to the given boolean.
    Default are yes/no. *)
let string_of_bool = ref (function true -> "yes" | false -> "no")

(** The name of the log file. *)
let log_file = ref "config_check.log"

(** The function to print a given fatal error message. Should exit the
    configuration process. *)
let fatal_error = ref
    (fun s ->
      prerr_endline s;
      prerr_endline (Printf.sprintf "You way have a look at %s for details." !log_file);
      exit 1
    )


(** {2:utils Utilities functions} *)

let try_finalize f x finally y =
  let res = try f x with exn -> finally y; raise exn in
  finally y;
  res;;

let rec restart_on_EINTR f x =
  try f x with Unix_error (EINTR, _, _) -> restart_on_EINTR f x


(** [is_suffix ~suf s] returns [true] if the string [suf] is a suffix of [s]. *)
let is_suffix ~suf s =
  (* taking out spaces at end : problems occuring under windows build *)
  let rec trim_right s =
    let length = String.length s in
    match s.[length-1] with
    |' '|'\n'|'\r'|'\t' -> trim_right (String.sub s 0 (length-1))
    |_ -> s in
  let suf = trim_right suf in
  let s = trim_right s in
  let len_suf = String.length suf in
  let len_s = String.length s in
  len_s >= len_suf &&
  String.sub s (len_s - len_suf) len_suf = suf


(** [create_temp_files ?ext ?contents ()] creates a temporary empty file and
    returns its name. The prefix of the file is specified by {!temp_files_prefix}.
    @param ext can be used to indicate a extension different from {!temp_files_ext}.
    @param contents can be used to specify the content of the file.
 *)
let create_temp_file ?(ext= !temp_files_ext) ?(contents="") () =
  let (file,oc) = Filename.open_temp_file
      !temp_files_prefix ("." ^ ext)
  in
  output_string oc contents;
  close_out oc;
  file


let remove_empty_strings = List.filter ((<>) "")

let string_of_includes l =
  String.concat " " (List.map (fun s -> "-I "^(Filename.quote s)) (remove_empty_strings l))

(** If the given filename has a [.cmo] (resp. [.cma]) extension,
    then return the same filename with a [.cmx] (resp. [.cmxa]) extension.*)
let byte_ext_to_opt f =
  if Filename.check_suffix f ".cmo" then
    Printf.sprintf "%s.cmx" (Filename.chop_extension f)
  else
    if Filename.check_suffix f ".cma" then
      Printf.sprintf "%s.cmxa" (Filename.chop_extension f)
    else
      f

(** {2:path Handling PATH} *)

exception Path of string * string;;
let path_sep =
  match (Sys.os_type = "Win32")with
  |false ->":"
  |true -> ";";;

let path_sep_regexp = Str.regexp (Str.quote path_sep);;

(** [list_of_path string] returns the list of directories from the given string in path format. *)
let list_of_path = Str.split path_sep_regexp;;

(** [path_of_list paths] builds a string in path format.
    @raise Path if a path contains the separator. *)
let path_of_list paths =
  (* Un nom de fichier dans un chemin ne doit pas contenir le séparateur... *)
  let check s =
    if Str.string_match path_sep_regexp s 0 then
      let pos = Str.match_beginning() in
      let mes = Printf.sprintf "Separator string found at position %d" pos in
      raise (Path (s, mes)) in
  List.iter check paths;
  String.concat path_sep paths;;

(** [get_path ()] returns the list of directories indicated by the [PATH] environement variable. *)
let get_path () =
  let s = list_of_path (getenv "PATH") in
(*   List.iter print_endline s; *)
  s;;

(** [find_in_path predicate paths file] returns the list of complete filenames build
    from the directories and the filename, and verifying the given predicate. *)
let find_in_path p paths file =
  List.find_all p (List.map (fun p -> Filename.concat p file) paths);;

(** {2:files Handling files} *)

(** Various tests of a file. *)
type filetest =
  | Fexists (** file exists *)
  | Freadable (** file exists and is readable *)
  | Fwritable (** file exists and is writable *)
  | Fexecutable (** file exists and is executable *)
  | Fdir (** file exists and is a directory *)
  | Freg (** file exists and is a regular file *)
  | Flnk (** file exists and is a symbolic link *)
  | Fnonempty (* file* exists and is non empty *)
  | Fnewer of string (** files exists and is newer than *)
  | Folder of string (** files exists and is older than *)
  | Fequal of string (** files is identical (sams st_ino and st_dev) than *)

let access_map =
  [ Freadable, R_OK; Fwritable, W_OK; Fexecutable, X_OK; Fexists, F_OK; ]

let access_ok_errors =
  [ EACCES ; EROFS; ENOENT; ENOTDIR; ELOOP ]
;;

(** [testfile flags filename] tests whether the given file verifies the given properties. *)
let testfile flags filename =
  let rec split ( (found, left) as both) = function
      [] -> both
    | h :: t ->
        let found_left =
          try List.assoc h access_map  :: found, left
          with Not_found -> found, h::left in
        split found_left t
  in
  let access_flags, flags = split ([],[]) flags in
  let access_flags = if access_flags = [] then [ F_OK ] else access_flags in
  try
    access filename access_flags;
    flags = [] ||
    begin
      let st = (if List.mem Flnk flags then stat else lstat) filename in
      let rec test = function
        | Fdir -> st.st_kind = S_DIR
        | Freg -> st.st_kind = S_REG
        | Flnk -> st.st_kind = S_LNK
        | Fnonempty -> st.st_size > 0
        | Fnewer string -> st.st_mtime > (stat string).st_mtime
        | Folder string -> st.st_mtime < (stat string).st_mtime
        | Fequal string ->
            let st' = stat string in
            st.st_ino = st'.st_ino && st.st_dev = st'.st_dev
        | _ -> assert false
      in
      List.for_all test flags
    end;
  with Unix.Unix_error (err, _, _) when  List.mem err access_ok_errors ->
    false
;;

let buffer_size = 4096
let string_from_descr fd =
  let rec readfd accu =
    let str = String.create buffer_size in
    match restart_on_EINTR (read fd str 0) buffer_size with
    | 0 -> String.concat ""  accu
    | n ->
        let str = if n < buffer_size then String.sub str 0 n else str in
        readfd (str :: accu) in
  readfd []
;;

(* patch single_write raising exceptions under windows *)
let single_write =
  match (Sys.os_type = "Win32")with
  |true -> write
  |false -> single_write

let descr_from_string str fd =
  let rec writefd offset left =
    if left > 0 then
      let n = restart_on_EINTR (single_write fd str offset) left in
      writefd (offset + n) (left - n) in
  writefd 0 (String.length str)
;;

let perm = 0o640;;


(** [string_of_files] returns the contents of the given file as a string.
    @raise Unix.Unix_error if an error occurs.
 *)
let string_of_file file =
  let fd = openfile file [ O_RDONLY ] 0 in
  try_finalize string_from_descr fd close fd
;;

(** [file_of_string ~contents ~file] creates the given file with the given string [str] as contents.
    @raise Unix.Unix_error if an error occurs.
 *)
let file_of_string ~contents ~file =
  let fd = openfile file [ O_WRONLY; O_CREAT; O_TRUNC ] perm in
  try_finalize (descr_from_string contents) fd close fd
;;

(** [input_lines channel] return the list of lines from the given channel.
    The function is tail-recursive.
 *)
let input_lines chan =
  let rec all lines =
    (* intermediate result to be tail rec *)
    match try Some (input_line chan) with End_of_file -> None with
      Some l -> all (l::lines)
    | None -> List.rev lines in
  all []

(** [unlink_f file] removes the given [file] if it exists.
    If the files is a [.ml] file (resp. a [.mli] file), then
    it also removes the [.cmo], [.cmx], [.o] and [.cmi] files
    (resp. the [.cmi] file) if they exist.
    @raise Unix.Unix_error if an error occurs.
 *)
let unlink_f file =
  if !debug then ()
  else
    let files =
      if Filename.check_suffix file ".mli" then
        [file; (Filename.chop_extension file)^".cmi"]
      else
        if Filename.check_suffix file ".ml" then
          let base = Filename.chop_extension file in
          [file; base^".cmi" ; base^".cmo" ; base^".cmx"; base^".o" ]
	else
          [file]
    in
    let f file =
      try unlink file with Unix_error (ENOENT, _, _) -> ()
    in
    List.iter f files



(** {2:exec Handling processes} *)

exception Exec_failure;;

let execvp_to_list cmd args =
  let desc_read, desc_write = pipe () in
  match fork() with
    0 ->
      let exec () =
        close desc_read;
        dup2 desc_write stdout;
        close desc_write;
        execvp cmd (Array.append [| cmd |] args) in
      handle_unix_error exec ()
  | pid ->
      close desc_write;
      let chan = in_channel_of_descr desc_read in
      let after () =
        close_in chan;
        match restart_on_EINTR (waitpid []) pid with
          _, WEXITED 0 -> ()
        | _, _ -> raise Exec_failure in
      try_finalize input_lines chan after ();;

type redirection =
  | In_from_file of string        (** < file *)
  | Out_to_file of string         (** > file *)
  | Err_to_file of string         (** 2> file *)
  | Out_append_to_file of string  (** >> file *)
  | Err_to_out                    (** 2>&1 *)
  | In_from_string of string      (** <<END *)
  | Err_null                      (** 2>/dev/null  *)
  | Out_null                      (** >/dev/null *)
  | Silent                        (** >/dev/null 2>&1 *)
;;

let execvp_redirect redirections cmd args  =
  let perm = 0o640 in
  let temp_file =
    if List.exists (function In_from_string _ -> true | _ -> false)
        redirections
    then Some (create_temp_file ~ext: ".in" ())
    else None in
  let rec make_redirect rd =
    match rd with
      In_from_file file ->
        let desc_file = openfile file [O_RDONLY] perm in
        try_finalize (dup2 desc_file) stdin close desc_file
    | Out_to_file file ->
        let desc_file = openfile file [O_WRONLY;O_CREAT;O_TRUNC] perm in
        try_finalize (dup2 desc_file) stdout close desc_file
    | Err_to_file file  ->
        let desc_file = openfile file [O_WRONLY;O_CREAT;O_TRUNC] perm in
        try_finalize (dup2 desc_file) stderr close desc_file
    | Out_append_to_file file ->
        let desc_file = openfile file [O_WRONLY;O_APPEND;O_CREAT] perm in
        try_finalize (dup2 desc_file) stdout close desc_file
    | Err_to_out -> dup2 stdout stderr
    | In_from_string s ->
        begin match temp_file with
          Some tmp ->
            file_of_string ~file: tmp ~contents: s;
            make_redirect (In_from_file tmp);
        | None -> assert false
        end
    | Out_null ->
        make_redirect (Out_to_file "/dev/null")
    | Err_null ->
        make_redirect (Err_to_file "/dev/null")
    | Silent ->
        make_redirect Out_null;
        make_redirect Err_to_out;
  in
  match fork () with
    0 ->
      let exec () =
        List.iter make_redirect redirections;
        execvp cmd (Array.append [|cmd|] args);
      in
      handle_unix_error exec ();
  | pid ->
      let res = snd (waitpid [] pid) in
      begin match temp_file with
        Some tmp -> unlink_f tmp
      | _ -> ()
      end;
      res
;;


let execvp cmd arg =
  match fork() with
  | 0 -> Unix.execvp cmd (Array.concat [ [| cmd |]; arg ])
  | p -> snd(waitpid [] p)


(* wrapping execution and fork use to be windows compliant,
   using therefore create_process *)
module WinExec =
  struct
    let read_text_file f =
      let ic = open_in f in
      let rec read s =
        try
          let line = input_line ic in
          read (line::s)
        with End_of_file ->
          (List.rev s) in
      let s = read [] in
      close_in ic;
      s

    let clear f =
      try
        Sys.remove f
      with _ -> ()

    let default_temp_dir = "C:/temp/"
    let execvp_to_list cmd args =
      (* we create file based stdin and stdout, giving the temp dir *)
      let dir =
        try
          Unix.getenv "TEMP"
        with _ -> default_temp_dir in
      let makeFile dir tag =
        let makeAddend ()=
          let addend = Random.int 20000 in
          Digest.to_hex (Digest.string (Marshal.to_string addend [])) in
        let file = ref (dir^makeAddend()^tag) in
        while (Sys.file_exists !file) do
          file := dir^makeAddend()^tag;
        done;
        !file in
      let output = makeFile dir "output.txt" in
      let error = makeFile dir "err.txt" in
      let (sil,sor,ser) =
        let sil, sir = pipe () in
        let sor = Unix.openfile output [O_WRONLY;O_CREAT] 0o640 in
        let ser = Unix.openfile error [O_WRONLY;O_CREAT] 0o640 in
        (sil,sor,ser) in
      let pid =
        create_process_env cmd (Array.append [| cmd |] args) (Unix.environment())
          sil sor ser in
      let _,status = Unix.waitpid [] pid in
      Unix.close sor;
      Unix.close ser;
      match status with
      | WEXITED 0 ->
          let res = read_text_file output in
          clear output;
          clear error;
          res
      | WEXITED i ->
          let errorTrace =read_text_file error in
          (* we can print the error trace if needed *)
          clear output;
          clear error;
          raise Exec_failure
      | _ ->
          clear output;
          clear error;
          raise Exec_failure

    let execvp_redirect redirections cmd args  =
      let perm = 0o640 in
      let temp_file =
        if List.exists (function In_from_string _ -> true | _ -> false)
            redirections
        then Some (create_temp_file ~ext: ".in" ())
        else None in
      let rec make_redirect rd =
        match rd with
          In_from_file file ->
            let desc_file = openfile file [O_RDONLY] perm in
            try_finalize (dup2 desc_file) stdin close desc_file
        | Out_to_file file ->
            let desc_file = openfile file [O_WRONLY;O_CREAT;O_TRUNC] perm in
            try_finalize (dup2 desc_file) stdout close desc_file
        | Err_to_file file  ->
            let desc_file = openfile file [O_WRONLY;O_CREAT;O_TRUNC] perm in
            try_finalize (dup2 desc_file) stderr close desc_file
        | Out_append_to_file file ->
            let desc_file = openfile file [O_WRONLY;O_APPEND;O_CREAT] perm in
            try_finalize (dup2 desc_file) stdout close desc_file
        | Err_to_out -> dup2 stdout stderr
        | In_from_string s ->
            begin match temp_file with
              Some tmp ->
                file_of_string ~file: tmp ~contents: s;
                make_redirect (In_from_file tmp);
            | None -> assert false
            end
        | Out_null ->
            make_redirect (Out_to_file "/dev/null")
        | Err_null ->
            make_redirect (Err_to_file "/dev/null")
        | Silent ->
            make_redirect Out_null;
            make_redirect Err_to_out;
      in
      (* we save stdin, stdout, stderr to restore them after fork *)
      let stdout_back = Unix.dup Unix.stdout in
      let stderr_back = Unix.dup Unix.stderr in
      let stdin_back = Unix.dup Unix.stdin in
      List.iter make_redirect redirections;
      let pid = create_process cmd (Array.append [|cmd|] args) Unix.stdin Unix.stdout Unix.stderr in
      let _,res = waitpid [] pid in
      begin match temp_file with
      |Some tmp -> unlink_f tmp
      | _ -> ()
      end;
      (* restorting stdin, stdout, stderr *)
      Unix.dup2 stdout_back Unix.stdout;
      Unix.dup2 stderr_back Unix.stderr;
      Unix.dup2 stdin_back Unix.stdin;
      res;;
end

(* under windows, replace the two functions by those provided by WinExec module *)
open WinExec;;


(** [exec_and_get_first_line com args] tries to execute the given command with
    the given arguments, and read the first line printed by the commande on its
    standard output. If any error occurs or the program doesn't print anything on stdout,
    the function returns [""].*)
let exec_and_get_first_line com args =
  match
    handle_unix_error
      (fun () -> execvp_to_list com args) ()
  with
    [] -> ""
  | h :: _ -> h

(** [exec_status_ok st] returns [true] if the given return status is [Unix.WEXITED 0]. *)
let exec_status_ok = function
    Unix.WEXITED 0 -> true
  | _ -> false


(** {2 Writing to log file} *)

let string_of_date t =
  let d = Unix.localtime t in
  Printf.sprintf "%02d/%02d/%02d %02d:%02d:%02d"
    (d.Unix.tm_year + 1900) (d.Unix.tm_mon+1) d.Unix.tm_mday
    d.Unix.tm_hour d.Unix.tm_min d.Unix.tm_sec

(** [add_to_log str] writes the given string to the {!log_file},
    with the date and time at the beginning of the line and an ending new line.*)
let add_to_log s =
  let oc = open_out_gen
      [Open_wronly ; Open_append ; Open_creat ; Open_text ]
      0o644 !log_file
  in
  Printf.fprintf oc "%s: %s\n"
    (string_of_date (Unix.time()))
    s;
  close_out oc

(** {2 Handling version numbers } *)

type version = int list

(** [v1 << v2] returns [true] if version [v1] is strictly inferior to version [v2]. *)
let (<<) v1 v2 =
  let rec iter = function
      ([], []) -> false
    | ([], _) -> true
    | (_,[]) -> false
    | (h1::q1, h2::q2) ->
        (h1 < h2) or
        (h1 = h2 && (iter (q1,q2)))
  in
  iter (v1,v2)

(** The dummy version number = [[0]]. *)
let dummy_version = [0]

(** [version_of_string s] returns a version number from the given string [s].
    [s] must b in the form [X[.Y[.Z[...]]]]. If the string is not correct,
    then the dummy version is returned. *)
let version_of_string s =
  let l = Str.split (Str.regexp_string ".") s in
  try List.map int_of_string l
  with Failure _ -> dummy_version

(** [string_of_version v] returns a string to display the given version.
    For example, [string_of_version [1;2;3] = "1.2.3"]. *)
let string_of_version v =
  String.concat "." (List.map string_of_int v)

(** {2 Handling OCaml configuration} *)

(** Representation of the (locally detected) ocaml configuration. *)
type ocaml_conf =
    {
     ocaml : string ;
     ocamlc : string ;
     ocamlopt : string ;
     ocamldep : string ;
     ocamldoc : string ;
     ocamldoc_opt : string ;
     ocamllex : string ;
     ocamlyacc : string ;
     ocamlmklib : string ;
     ocamlmktop : string ;
     ocamlprof : string ;
     camlp4 : string;
     ocamlfind : string ;
     version_string : string ;
     version : version ;
   }



exception Program_not_found of string

(** [ocaml_prog file] return the first executable called [file] in
    the directories of the PATH environment variable.
    @param err can be used to indicate whether not finding the program
    should raise the [Program_not_found] exception ([err=true], default)
    or rather return an empty string ([err=false]).
    The function prints messages indicating what program is searched, and the result,
    using the {!print} function.
 *)
let ocaml_prog ?(err=true) file =
  !print (Printf.sprintf "checking for %s... " file);
  match
    handle_unix_error
      (fun () -> find_in_path (testfile [Fexecutable]) (get_path()) file)
      ()
  with
    [] ->
      !print "no\n";
      if err then raise (Program_not_found file) else ""
  | s :: _ ->
      !print (s^"\n"); s
;;

(** [ocaml_libdir conf] uses the [ocamlc] program of the given configuration
    to retrieve the library directory.
    Return [""] if any error occurs.*)
let ocaml_libdir conf =
  match
    handle_unix_error
      (fun () -> execvp_to_list conf.ocamlc [| "-v" |])
      ()
  with
    [] | [_] -> ""
  | _ :: l :: _ ->
      try
	let p = String.index l ':' in
	String.sub l (p+2) (String.length l - p - 2)
      with
	Invalid_argument _
      |	Not_found ->
	  ""
;;

(** [version_of_ocaml_version_string str] returns a {!type:version} value from
    the string representing an ocaml version (which can contain '+', a date, ...). *)
let version_of_ocaml_version_string s =
  let len = String.length s in
  let b = Buffer.create len in
  let rec iter n =
    if n < len then
      match s.[n] with
	'0'..'9' | '.' -> Buffer.add_char b s.[n]; iter (n+1)
      | _ -> ()
  in
  iter 0;
  version_of_string (Buffer.contents b)

(** [check_version version prog] tries to run [prog -version] and return
    whether the given version string is a suffix of the first line printed.
    This function is used to check that an ocaml program has the correct
    version number.
    @param on_err can be used to indicate another function to call it
    the program is not in the correct version. Default is [!]{!fatal_error}.
 *)
let check_version ?(on_err= !fatal_error) version prog =
  match prog with
    "" -> ()
  | _ ->
      match handle_unix_error
	  (fun () -> execvp_to_list prog [| "-version" |]) ()
      with
	[] ->
	  let s = Printf.sprintf "could not get version of %s" prog in
	  on_err s
      | s :: _ ->
	  if not (is_suffix ~suf: version s) then
	    let s = Printf.sprintf "%s is not version %s" prog version in
	    on_err s

(** [check_conf_version conf] verifies that each program of the given configuration
    is of the correct version.
    If a program is not in the correct version, [!]{!fatal_error} is called.
 *)
let check_conf_versions conf =
  List.iter (check_version conf.version_string)
    [ conf.ocamlopt ; conf.ocamldep ; conf.ocamldoc ;
      conf.ocamllex ; conf.ocamlyacc ; conf.ocamlmklib ;
      conf.ocamlmktop ;
      (* not yet conf.ocamlprof ; *)
    ]

(** [check_for_opt_prog version prog] checks whether the [prog.opt] program
    is in the required version, and if so returns this program name; else
    it returns the given program name. *)
let check_for_opt_prog version prog =
  match prog with
    "" -> ""
  | _ ->
      let opt = Printf.sprintf "%s.opt" (Filename.basename prog) in
      try
	let opt = ocaml_prog opt in
	check_version
	  ~on_err:(fun m -> !print (m^"\n"); raise (Program_not_found ""))
	  version opt;
	!print (Printf.sprintf "we can use %s\n" opt);
	opt
      with
	Program_not_found _ -> prog

(** [get_opt_conf conf] returns the same configuration where some program
    names have been replaced by the "opt" version (["..../ocamlc.opt"] instead
    of ["..../ocamlc"] for example).
    To do so, the function verifies if the ".opt" program found for each program
    of the configuration is in the same version of the bytecode program.
    For [ocamldoc], the program in the [ocamldoc] field is not replaced but the [ocamldoc_opt]
    field is set to ["..../ocamldoc.opt"], because both bytecode and native versions
    can be used (for example the bytecode version is required to use custom generators).*)
let get_opt_conf conf =
  let f = check_for_opt_prog conf.version_string in
  { conf with
    ocamlc = f conf.ocamlc ;
    ocamlopt = f conf.ocamlopt ;
    ocamldoc_opt = f conf.ocamldoc ;
    ocamllex = f conf.ocamllex ;
    ocamldep = f conf.ocamldep ;
  }

(** [ocaml_conf ()] detects and returns the Objective Caml configuration found from the PATH.
    The function checks that the programs found are in the same version (see {!check_conf_versions}),
    and refer to the ".opt" programs when they are available (see {!get_opt_conf}).
    @param withopt can be used to raise a {!Program_not_found} exception if the native code compiler is not found;
    default is [false].
    @param ocamlfind can be used to end up with a {!Program_not_found} exception if the [ocamlfind] tool is not found;
    default is [false].
    @raise Program_not found if a required program cannot be found.
 *)

let exe_extension =
  match (Sys.os_type="Win32") with
  |true -> ".exe"
  |false -> "";;

let ocaml_conf ?(withopt=false) ?(ocamlfind=false) () =
  let ocamlc = ocaml_prog ("ocamlc"^exe_extension) in
  let version_string = exec_and_get_first_line  ocamlc [| "-version" |] in
  let version = version_of_ocaml_version_string version_string in
  !print (Printf.sprintf "found OCaml version %s (%s)\n" (string_of_version version) version_string);
  let conf = {
    ocamlc = ocamlc ;
    version_string = version_string ;
    version = version ;
    ocaml = ocaml_prog ("ocaml"^exe_extension) ;
    ocamlopt = ocaml_prog ~err: withopt ("ocamlopt"^exe_extension) ;
    ocamldoc = ocaml_prog ("ocamldoc"^exe_extension) ;
    ocamldoc_opt = "" ;
    ocamlyacc = ocaml_prog ("ocamlyacc"^exe_extension) ;
    ocamllex = ocaml_prog ("ocamllex"^exe_extension) ;
    ocamldep = ocaml_prog ("ocamldep"^exe_extension) ;
    ocamlmklib = ocaml_prog ("ocamlmklib"^exe_extension) ;
    ocamlmktop = ocaml_prog ("ocamlmktop"^exe_extension) ;
    ocamlprof = ocaml_prog ("ocamlprof"^exe_extension) ;
    camlp4 = ocaml_prog ("camlp4"^exe_extension) ;
    ocamlfind = ocaml_prog ~err: ocamlfind ("ocamlfind"^exe_extension) ;
  } in
  check_conf_versions conf;
  get_opt_conf conf

(** [print_conf conf] prints the given configuration using [!]{!print}. *)
let print_conf c =
  let sp = Printf.sprintf in
  !print (sp "Objective-Caml version %s (%s)\n" (string_of_version c.version) c.version_string);
  !print (sp "interpreter:                %s\n" c.ocaml);
  !print (sp "bytecode compiler:          %s\n" c.ocamlc);
  !print (sp "native code compiler:       %s\n" c.ocamlopt);
  !print (sp "documentation generator(s): %s%s\n" c.ocamldoc
	    (if c.ocamldoc_opt <> "" && c.ocamldoc_opt <> c.ocamldoc then
	      sp ", %s" c.ocamldoc_opt else ""
	    )
	 );
  !print (sp "lexer generator:            %s\n" c.ocamllex);
  !print (sp "parser generator:           %s\n" c.ocamlyacc);
  !print (sp "dependencies generator:     %s\n" c.ocamldep);
  !print (sp "library builder:            %s\n" c.ocamlmklib);
  !print (sp "toplevel builder:           %s\n" c.ocamlmktop);
  !print (sp "profiler:                   %s\n" c.ocamlprof);
  !print (sp "camlp4:                     %s\n" c.camlp4);
  (match c.ocamlfind with "" -> () | s ->
    !print (sp "ocamlfind:                  %s\n" s))


(** {2:compiling Testing compilation and link} *)

type compilation_mode = [ `Byte | `Opt ]

let string_of_mode = function
    `Byte -> "byte"
  | `Opt -> "opt"

let ocamlc_of_mode conf = function
    `Byte -> conf.ocamlc
  | `Opt -> conf.ocamlopt

let string_of_com_args com args =
  Printf.sprintf "%s %s"
    com
    (String.concat " "
       (List.map Filename.quote (Array.to_list args)))

let can_compile mode conf ?(includes=[]) ?(options=[]) file =
  let ocamlc = ocamlc_of_mode conf mode in
  let args = Array.of_list
      (
       "-c" ::
       (List.flatten (List.map (fun s -> [ "-I" ; s]) (remove_empty_strings includes))) @
       options @
       [file]
      )
  in
  add_to_log (string_of_com_args ocamlc args);
  exec_status_ok (execvp_redirect [Out_append_to_file !log_file;Err_to_out] ocamlc args)

let can_compile_prog ?mes mode conf ?includes ?options prog =
  (match mes with None -> () | Some s -> !print s);
  let res =
    handle_unix_error (fun () ->
      let file = create_temp_file ~contents: prog () in
      try_finalize (can_compile mode conf ?includes ?options) file unlink_f file) ()
  in
  (match mes with None -> () | Some _ -> !print ((!string_of_bool res)^"\n"));
  res
;;

let ocaml_defined ?mes mode conf ?includes ?options v =
  handle_unix_error (fun () ->
    let prog = Printf.sprintf "let _ = %s;;\n" v in
    can_compile_prog ?mes mode conf ?includes ?options prog) ()
;;

let ocaml_value_has_type ?mes mode conf ?includes ?options v t =
  handle_unix_error (fun () ->
    let prog = Printf.sprintf "let (_ : %s) = %s;;\n" t v in
    can_compile_prog ?mes mode conf ?includes prog) ()
;;

let can_link ?mes mode conf ?out ?(includes=[]) ?(libs=[]) ?(options=[]) files =
  (match mes with None -> () | Some s -> !print s);
  let ocamlc = ocamlc_of_mode conf mode in
  let libs = match mode with
    `Byte -> libs
  | `Opt -> List.map byte_ext_to_opt libs
  in
  let files = match mode with
    `Byte -> files
  | `Opt -> List.map byte_ext_to_opt files
  in
  let outfile = match out with
    None -> create_temp_file ~ext: "out" ()
  | Some f -> f
  in
  let args = Array.of_list
      (
       "-linkall" ::
       "-o" :: outfile ::
       (List.flatten (List.map (fun s -> ["-I"; s]) (remove_empty_strings includes))) @
       libs @
       options @
       files
      )
  in
  add_to_log (string_of_com_args ocamlc args);
  let success =
    exec_status_ok (execvp_redirect [Out_append_to_file !log_file; Err_to_out] ocamlc args)
  in
  (
   match out with
     None -> unlink_f outfile
   | Some _ when not success -> unlink_f outfile
   | _ -> ()
  );
  (match mes with None -> () | Some _ -> !print ((!string_of_bool success)^"\n"));
  success

let try_run com = Sys.command com = 0

(** {2:ocamlfind OCamlfind queries} *)

(** [ocamlfind_query conf package] returns the first line printed by "ocamlfind query package".
    If an error occurs (the package does not exist, ocamlfind cannot be executed, ...),
    the function returns [None].
 *)
let ocamlfind_query conf package =
  match conf.ocamlfind with
    "" -> None
  | _ ->
      try
	match exec_and_get_first_line conf.ocamlfind [| "query"; package |] with
	  "" -> None
	| s -> Some s
      with
	_ -> None

(** {2:substs Handling substitutions specification} *)

let substs = Hashtbl.create 37
let add_subst = Hashtbl.replace substs
let subst_val var =
  try Hashtbl.find substs var
  with Not_found -> ""
let get_substs_list () =
  Hashtbl.fold (fun var v acc -> (var,v)::acc) substs []

let output_substs oc =
  List.iter
    (fun (var, v) -> Printf.fprintf oc "%s=\"%s\"\n" var v)
    (get_substs_list ())

let output_substs_to_file file =
  let contents = String.concat "\n"
      (List.map (fun (var,v) -> Printf.sprintf "%s=\"%s\"" var v) (get_substs_list()))
  in
  file_of_string ~file ~contents

let add_conf_variables c =
  let l =
    [ "OCAML", c.ocaml ;
      "OCAMLC", c.ocamlc ;
      "OCAMLOPT", c.ocamlopt ;
      "OCAMLDEP", c.ocamldep ;
      "OCAMLDOC", c.ocamldoc ;
      "OCAMLDOC_OPT", c.ocamldoc_opt ;
      "OCAMLLEX", c.ocamllex ;
      "OCAMLYACC", c.ocamlyacc ;
      "OCAMLMKLIB", c.ocamlmklib ;
      "OCAMLMKTOP", c.ocamlmktop ;
      "OCAMLPROF", c.ocamlprof ;
      "CAMLP4", c.camlp4 ;
      "OCAMLFIND", c.ocamlfind ;
      "OCAMLBIN", Filename.dirname c.ocamlc;
      "OCAMLLIB", ocaml_libdir c ;
      "OCAMLVERSION", string_of_version c.version ;
    ]
  in
  List.iter (fun (var,v) -> add_subst var v) l


(*/c==m=[OCaml_conf]=0.3=t==*)


let ocaml_required = [3;11]
let conf = ocaml_conf ();;
print_conf conf;;

let modes = if conf.ocamlopt <> "" then [`Byte;`Opt] else [`Byte];;

let _ =
  if conf.version << ocaml_required then
    let msg = Printf.sprintf "Error: OCaml %s required, but found %s."
        (string_of_version ocaml_required) (string_of_version conf.version)
    in
    prerr_endline msg; exit 1

let _ = !print "\n### checking required tools and libraries ###\n"

let ocamlbuild =
  try ocaml_prog ~err: true ("ocamlbuild"^exe_extension)
  with Program_not_found _ ->
    !fatal_error "ocamlbuild not found.";
    assert false
;;

let _ = add_conf_variables conf;;
add_subst "OCAMLBUILD" ocamlbuild ;;

let _ =
  if Array.length Sys.argv < 2 then
    !fatal_error "No output file given for substitutions!"
  else
      output_substs_to_file Sys.argv.(1);;

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-06-10 14:28 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-03 16:38 First release of Oug: reference graph from ocaml code Maxence Guesdon
2008-06-09  8:47 ` [Caml-list] " RABIH.ELCHAAR
2008-06-09 11:43   ` Maxence Guesdon
2008-06-09 18:03     ` RABIH.ELCHAAR
2008-06-10  7:40       ` Maxence Guesdon
2008-06-10 14:25         ` RABIH.ELCHAAR

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).