| 1 |
open Dune_site_private |
| 2 |
|
| 3 |
let path_sep = path_sep |
| 4 |
|
| 5 |
module Location = struct |
| 6 |
type t = string |
| 7 |
end |
| 8 |
|
| 9 |
let dirs : (string * Dune_section.t, string) Hashtbl.t = Hashtbl.create 10 |
| 10 |
|
| 11 |
(* multi-bindings first is the one with least priority *) |
| 12 |
|
| 13 |
let () = |
| 14 |
match Sys.getenv_opt dune_dir_locations_env_var with |
| 15 |
| None -> () |
| 16 |
| Some s -> |
| 17 |
(match decode_dune_dir_locations s with |
| 18 |
| None -> |
| 19 |
invalid_arg (Printf.sprintf "Invalid value %s=%S" dune_dir_locations_env_var s) |
| 20 |
| Some entries -> |
| 21 |
List.iter |
| 22 |
(fun { package; section; dir } -> Hashtbl.add dirs (package, section) dir) |
| 23 |
entries) |
| 24 |
;; |
| 25 |
|
| 26 |
(* Parse the replacement format described in [artifact_substitution.ml]. *) |
| 27 |
let eval s = |
| 28 |
let len = String.length s in |
| 29 |
if s.[0] = '=' |
| 30 |
then ( |
| 31 |
let colon_pos = String.index_from s 1 ':' in |
| 32 |
let vlen = int_of_string (String.sub s 1 (colon_pos - 1)) in |
| 33 |
(* This [min] is because the value might have been truncated if it was too |
| 34 |
large *) |
| 35 |
let vlen = min vlen (len - colon_pos - 1) in |
| 36 |
Some (String.sub s (colon_pos + 1) vlen)) |
| 37 |
else None |
| 38 |
[@@inline never] |
| 39 |
;; |
| 40 |
|
| 41 |
let get_dir ~package ~section = Hashtbl.find_all dirs (package, section) |
| 42 |
|
| 43 |
module Hardcoded_ocaml_path = struct |
| 44 |
type t = |
| 45 |
| None |
| 46 |
| Relocatable |
| 47 |
| Hardcoded of string list |
| 48 |
| Findlib_config of string |
| 49 |
|
| 50 |
let t = |
| 51 |
lazy |
| 52 |
(match eval Dune_site_data.hardcoded_ocamlpath with |
| 53 |
| None -> None |
| 54 |
| Some "relocatable" -> Relocatable |
| 55 |
| Some s -> |
| 56 |
let l = String.split_on_char '\000' s in |
| 57 |
(match l with |
| 58 |
| "hardcoded" :: l -> Hardcoded l |
| 59 |
| [ "findlibconfig"; p ] -> Findlib_config p |
| 60 |
| _ -> invalid_arg "dune error: hardcoded_ocamlpath parsing error")) |
| 61 |
;; |
| 62 |
end |
| 63 |
|
| 64 |
let relocatable = |
| 65 |
lazy |
| 66 |
(match Lazy.force Hardcoded_ocaml_path.t with |
| 67 |
| Relocatable -> true |
| 68 |
| _ -> false) |
| 69 |
;; |
| 70 |
|
| 71 |
let prefix = |
| 72 |
lazy |
| 73 |
(let path = Sys.executable_name in |
| 74 |
let bin = Filename.dirname path in |
| 75 |
let prefix = Filename.dirname bin in |
| 76 |
prefix) |
| 77 |
;; |
| 78 |
|
| 79 |
let relocate_if_needed path = |
| 80 |
if Lazy.force relocatable then Filename.concat (Lazy.force prefix) path else path |
| 81 |
;; |
| 82 |
|
| 83 |
let site ~package ~section ~suffix ~encoded = |
| 84 |
let dirs = get_dir ~package ~section in |
| 85 |
let dirs = |
| 86 |
match eval encoded with |
| 87 |
| None -> dirs |
| 88 |
| Some d -> relocate_if_needed d :: dirs |
| 89 |
in |
| 90 |
List.rev_map (fun dir -> Filename.concat dir suffix) dirs |
| 91 |
[@@inline never] |
| 92 |
;; |
| 93 |
|
| 94 |
let sourceroot local = |
| 95 |
match eval local with |
| 96 |
| Some "" -> None |
| 97 |
| Some _ as x -> x |
| 98 |
| None -> |
| 99 |
(* None if the binary is executed from _build but not by dune, which should |
| 100 |
not happen *) |
| 101 |
Sys.getenv_opt dune_sourceroot_env_var |
| 102 |
;; |
| 103 |
|
| 104 |
let ocamlpath = |
| 105 |
lazy |
| 106 |
(let env = |
| 107 |
match Sys.getenv_opt "OCAMLPATH" with |
| 108 |
| None -> [] |
| 109 |
| Some x -> String.split_on_char path_sep x |
| 110 |
in |
| 111 |
let static = |
| 112 |
match Lazy.force Hardcoded_ocaml_path.t with |
| 113 |
| Hardcoded_ocaml_path.None -> |
| 114 |
String.split_on_char path_sep (Sys.getenv dune_ocaml_hardcoded_env_var) |
| 115 |
| Hardcoded_ocaml_path.Relocatable -> [ Filename.concat (Lazy.force prefix) "lib" ] |
| 116 |
| Hardcoded_ocaml_path.Hardcoded l -> l |
| 117 |
| Hardcoded_ocaml_path.Findlib_config _ -> assert false |
| 118 |
in |
| 119 |
env @ static) |
| 120 |
;; |
| 121 |
|
| 122 |
let stdlib = |
| 123 |
lazy |
| 124 |
(match eval Dune_site_data.stdlib_dir with |
| 125 |
| None -> Sys.getenv dune_ocaml_stdlib_env_var |
| 126 |
| Some s -> s) |
| 127 |
;; |