Skip to content

Commit

Permalink
Merge pull request #1841 from nojb/toplevel_include_path_env_var
Browse files Browse the repository at this point in the history
Add support for OCAMLTOP_INCLUDE_PATH environment variable
  • Loading branch information
nojb authored Oct 10, 2018
2 parents f5f117e + 795a9a2 commit 8e80195
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 24 deletions.
5 changes: 5 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ Working version
- GPR#2058: full explanation for unsafe cycles in recursive module definitions
(Florian Angeletti, review by Gabriel Scherer, suggested by Ivan Gotovchits)

- GPR#1841, MPR#7808: the environment variable OCAMLTOP_INCLUDE_PATH can now
specify a list of additional include directories for the ocaml toplevel.
(Nicolás Ojeda Bär, request by Daniel Bünzli, review by Daniel Bünzli and
Damien Doligez)

### Code generation and optimizations:

- MPR#7725, GPR#1754: improve AFL instrumentation for objects and lazy values.
Expand Down
29 changes: 6 additions & 23 deletions bytecomp/dll.ml
Original file line number Diff line number Diff line change
Expand Up @@ -132,32 +132,15 @@ let ld_conf_contents () =

(* Split the CAML_LD_LIBRARY_PATH environment variable and return
the corresponding list of directories. *)

let split str sep =
let rec split_rec pos =
if pos >= String.length str then [] else begin
try
let newpos = String.index_from str pos sep in
String.sub str pos (newpos - pos) ::
split_rec (newpos + 1)
with Not_found ->
[String.sub str pos (String.length str - pos)]
end in
split_rec 0

let ld_library_path_contents () =
let path_separator =
match Sys.os_type with
| "Unix" | "Cygwin" -> ':'
| "Win32" -> ';'
| _ -> assert false in
try
split (Sys.getenv "CAML_LD_LIBRARY_PATH") path_separator
with Not_found ->
[]
match Sys.getenv "CAML_LD_LIBRARY_PATH" with
| exception Not_found ->
[]
| s ->
Misc.split_path_contents s

let split_dll_path path =
split path '\000'
Misc.split_path_contents ~sep:'\000' path

(* Initialization for separate compilation *)

Expand Down
7 changes: 6 additions & 1 deletion manual/manual/cmds/top.etex
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ The following command-line options are recognized by the "ocaml" command.
\begin{unix}
The following environment variables are also consulted:
\begin{options}
\item["OCAMLTOP_INCLUDE_PATH"] Additional directories to search for compiled
object code files (".cmi", ".cmo" and ".cma"). The specified directories are
considered from left to right, after the include directories specified on the
command line via "-I" have been searched. Available since OCaml 4.08.

\item["OCAMLTOP_UTF_8"] When printing string values, non-ascii bytes
($ {} > "\0x7E" $) are printed as decimal escape sequence if "OCAMLTOP_UTF_8" is
set to false. Otherwise, they are printed unescaped.
Expand Down Expand Up @@ -212,7 +217,7 @@ directories:
\item Directories added with the "#directory" directive.
\item Directories given on the command line with "-I" options.
\item The standard library directory.
\end{enumerate}
\end{enumerate}

\item[Environment queries]
\begin{options}
Expand Down
8 changes: 8 additions & 0 deletions toplevel/opttopmain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ module Options = Main_args.Make_opttop_options (struct
let anonymous = file_argument
end);;

let () =
let extra_paths =
match Sys.getenv "OCAMLTOP_INCLUDE_PATH" with
| exception Not_found -> []
| s -> Misc.split_path_contents s
in
Clflags.include_dirs := List.rev_append extra_paths !Clflags.include_dirs

let main () =
native_code := true;
let list = ref Options.list in
Expand Down
7 changes: 7 additions & 0 deletions toplevel/topmain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ module Options = Main_args.Make_bytetop_options (struct
let anonymous s = file_argument s
end);;

let () =
let extra_paths =
match Sys.getenv "OCAMLTOP_INCLUDE_PATH" with
| exception Not_found -> []
| s -> Misc.split_path_contents s
in
Clflags.include_dirs := List.rev_append extra_paths !Clflags.include_dirs

let main () =
let ppf = Format.err_formatter in
Expand Down
9 changes: 9 additions & 0 deletions utils/misc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,15 @@ let expand_directory alt s =
(String.sub s 1 (String.length s - 1))
else s

let path_separator =
match Sys.os_type with
| "Win32" -> ';'
| _ -> ':'

let split_path_contents ?(sep = path_separator) = function
| "" -> []
| s -> String.split_on_char sep s

(* Hashtable functions *)

let create_hashtable size init =
Expand Down
7 changes: 7 additions & 0 deletions utils/misc.mli
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ val expand_directory: string -> string -> string
(* [expand_directory alt file] eventually expands a [+] at the
beginning of file into [alt] (an alternate root directory) *)

val split_path_contents: ?sep:char -> string -> string list
(* [split_path_contents ?sep s] interprets [s] as the value of a "PATH"-like
variable and returns the corresponding list of directories. [s] is split
using the platform-specific delimiter, or [~sep] if it is passed.
Returns the empty list if [s] is empty. *)

val create_hashtable: int -> ('a * 'b) list -> ('a, 'b) Hashtbl.t
(* Create a hashtable of the given size and fills it with the
given bindings. *)
Expand Down

0 comments on commit 8e80195

Please sign in to comment.