1 (**************************************************************************)
2 (* *)
3 (* OCaml *)
4 (* *)
5 (* Fabrice Le Fessant, EPI Gallium, INRIA Paris-Rocquencourt *)
6 (* *)
7 (* Copyright 2013 Institut National de Recherche en Informatique et *)
8 (* en Automatique. *)
9 (* *)
10 (* All rights reserved. This file is distributed under the terms of *)
11 (* the GNU Lesser General Public License version 2.1, with the *)
12 (* special exception on linking described in the file LICENSE. *)
13 (* *)
14 (**************************************************************************)
15
16 open Compenv
17
18 (* Initialize the search path.
19 [dir] is always searched first (default: the current directory),
20 then the directories specified with the -I option (in command-line order),
21 then the standard library directory (unless the -nostdlib option is given).
22 *)
23
24 let init_path ?(dir="") () =
25 let dirs =
26 if !Clflags.use_threads then "+threads" :: !Clflags.include_dirs
27 else
28 !Clflags.include_dirs
29 in
30 let dirs =
31 !last_include_dirs @ dirs @ Config.flexdll_dirs @ !first_include_dirs
32 in
33 let exp_dirs =
34 List.map (Misc.expand_directory Config.standard_library) dirs in
35 Load_path.init (dir :: List.rev_append exp_dirs (Clflags.std_include_dir ()));
36 Env.reset_cache ()
37
38 (* Return the initial environment in which compilation proceeds. *)
39
40 (* Note: do not do init_path() in initial_env, this breaks
41 toplevel initialization (PR#8227) *)
42
43 let initial_env () =
44 Ident.reinit();
45 let initially_opened_module =
46 if !Clflags.nopervasives then
47 None
48 else
49 Some "Stdlib"
50 in
51 Typemod.initial_env
52 ~loc:(Location.in_file "command line")
53 ~safe_string:(Config.safe_string || not !Clflags.unsafe_string)
54 ~initially_opened_module
55 ~open_implicit_modules:(List.rev !Clflags.open_modules)
56
57 let set_from_env flag Clflags.{ parse; usage; env_var } =
58 try
59 match parse (Sys.getenv env_var) with
60 | None ->
61 Location.prerr_warning Location.none
62 (Warnings.Bad_env_variable (env_var, usage))
63 | Some x -> match !flag with
64 | None -> flag := Some x
65 | Some _ -> ()
66 with
67 Not_found -> ()
68
69 let read_clflags_from_env () =
70 set_from_env Clflags.color Clflags.color_reader;
71 set_from_env Clflags.error_style Clflags.error_style_reader;
72 ()
73
74 let with_ppf_dump ~file_prefix f =
75 let ppf_dump, finally =
76 if not !Clflags.dump_into_file
77 then Format.err_formatter, ignore
78 else
79 let ch = open_out (file_prefix ^ ".dump") in
80 let ppf = Format.formatter_of_out_channel ch in
81 ppf,
82 (fun () ->
83 Format.pp_print_flush ppf ();
84 close_out ch)
85 in
86 Misc.try_finally (fun () -> f ppf_dump) ~always:finally
87