Skip to content

Commit 605fb33

Browse files
Yi-Zhou-01dhil
andauthored
Uniform paths (#1153)
This patch resolves #1132 by precomputing the various paths used by Links. The paths are computed during compilation of Links itself. The paths change depending on whether Links was compiled via `opam install` or `make`/`dune build`. In the former case, all the paths are computed relative to the OPAM root of the running switch, whereas in the latter case the paths are computed relative to the source tree that `make` was invoked in. Consequently, Links no longer has to guess or compute paths during startup (well this is a truth with modifications, database drivers are still discovered dynamically, and the default config file is still implicitly resolved if built from source as the testing infrastructure depends on being able to control the config file via the environment variable `LINKS_CONFIG`). In addition, this patch also deprecates the `LINKS_LIB` environment variable, i.e. setting this variable no longer affects the runtime of Links. Co-authored-by: Daniel Hillerström <daniel.hillerstrom@ed.ac.uk>
1 parent c25ef3c commit 605fb33

File tree

9 files changed

+54
-46
lines changed

9 files changed

+54
-46
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,8 @@ opam-release:
176176
dune-release publish -p $(RELEASE_PKGS) distrib
177177
dune-release opam -p $(RELEASE_PKGS) pkg
178178
dune-release opam -p $(RELEASE_PKGS) submit
179+
180+
# Build Links
181+
.PHONY: opam-build-links.opam
182+
opam-build-links.opam: links.opam
183+
$(shell LINKS_BUILT_BY_OPAM=1 dune build -p links)

bin/driver.ml

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ open Utility
55

66
(** Name of the file containing the prelude code. *)
77
let prelude_file =
8-
let prelude_dir =
9-
match Utility.getenv "LINKS_LIB" with
10-
(* If user defined LINKS_LIB then it takes the highest priority *)
11-
| Some path -> path
12-
| None -> locate_file "prelude.links" in
13-
Settings.(option ~default:(Some (Filename.concat prelude_dir "prelude.links")) "prelude"
8+
Settings.(option ~default: (Some Linkspath.prelude) "prelude"
149
|> synopsis "The Links prelude source file"
1510
|> to_string from_string_option
1611
|> convert (Sys.expand ->- some)
@@ -237,20 +232,7 @@ module Phases = struct
237232
Js_CodeGen.output oc Compiler.primitive_bindings;
238233
let runtime_files =
239234
match Settings.get Basicsettings.System.custom_js_runtime with
240-
| [] ->
241-
let file =
242-
begin match Settings.get jslib_dir with
243-
| None | Some "" ->
244-
begin
245-
Filename.concat
246-
(match Utility.getenv "LINKS_LIB" with
247-
| None -> Filename.dirname Sys.executable_name
248-
| Some path -> path)
249-
(Filename.concat "js" "jslib.js")
250-
end
251-
| Some path -> Filename.concat path "jslib.js"
252-
end
253-
in [file]
235+
| [] -> [Filename.concat Linkspath.jslib "jslib.js"]
254236
| files -> files
255237
in
256238
List.iter

core/dune

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
findlib menhirLib links.lens calendar)
2121
(preprocess (pps ppx_deriving.std ppx_deriving_yojson)))
2222

23+
(rule
24+
(targets linkspath.ml)
25+
(deps (:gen ../gen_linkspath/gen_linkspath.exe))
26+
(action (run %{gen} -o %{targets})))
27+
2328
;; Make Query modules available.
2429
(copy_files# query/*.ml)
2530
(copy_files# query/*.mli)

core/moduleUtils.ml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@ let use_stdlib
2323

2424
(* Standard library path *)
2525
let stdlib_path =
26-
let dir =
27-
match Utility.getenv "LINKS_LIB" with
28-
| Some path -> Filename.concat path "stdlib"
29-
| None -> ""
30-
in
31-
Settings.(option ~default:(Some dir) "stdlib_path"
26+
Settings.(option ~default:(Some Linkspath.stdlib) "stdlib_path"
3227
|> to_string from_string_option
3328
|> convert Utility.(Sys.expand ->- some)
3429
|> sync)

core/settings.ml

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,23 +1130,14 @@ let config_loaded = ref false
11301130
let config =
11311131
(* Path to the default config file. *)
11321132
let default_config_file =
1133-
match Utility.getenv "LINKS_CONFIG" with
1134-
(* If user defined LINKS_CONFIG then it takes the highest priority. *)
1135-
| Some path -> Some (Filename.concat path "config")
1133+
(* If there exists a precomputed path then use it as the default config. *)
1134+
match Linkspath.config with
11361135
| None ->
1137-
(* If LINKS_CONFIG is not defined then we search in current directory. *)
1138-
let executable_dir = Filename.dirname Sys.executable_name in
1139-
if Sys.file_exists (Filename.concat executable_dir "config")
1140-
then Some (Filename.concat executable_dir "config")
1141-
else try
1142-
(* If all else failed we search for OPAM installation of Links and
1143-
use a config that it provides. *)
1144-
let opam_links_etc =
1145-
input_line (Unix.open_process_in "opam config var links:etc 2>/dev/null") in
1146-
if Sys.file_exists (Filename.concat opam_links_etc "config")
1147-
then Some (Filename.concat opam_links_etc "config")
1148-
else None
1149-
with End_of_file -> None
1136+
(* If LINKS_CONFIG is defined then use it as the default config. *)
1137+
(match Utility.getenv "LINKS_CONFIG" with
1138+
| Some path -> Some (Filename.concat path "config")
1139+
| None -> None)
1140+
| Some path -> Some path
11501141
in
11511142
(* Load default is the action attached to the [config] setting. If
11521143
[config_loaded] is false it will try to load whatever argument it

core/webserver.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ open Utility
55
open Webserver_types
66
open Var
77

8-
let jslib_dir = Settings.(option "jslibdir"
8+
let jslib_dir = Settings.(option ~default:(Some Linkspath.jslib) "jslibdir"
99
|> synopsis "Server-side (physical) location of the JavaScript runtime"
1010
|> to_string from_string_option
1111
|> convert Utility.(Sys.expand ->- some)

gen_linkspath/dune

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(executable
2+
(name gen_linkspath)
3+
(libraries dune.configurator))

gen_linkspath/gen_linkspath.ml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module C = Configurator.V1
2+
3+
let check_opam =
4+
Sys.getenv_opt "LINKS_BUILT_BY_OPAM" |> Option.is_some
5+
6+
let _ =
7+
let oc = open_out "linkspath.ml" in
8+
if check_opam
9+
then
10+
(* If Links is built by OPAM*)
11+
let lib_path = input_line (Unix.open_process_in "opam var lib") in
12+
let etc_path = input_line (Unix.open_process_in "opam var etc") in
13+
let share_path = input_line (Unix.open_process_in "opam var share") in
14+
Printf.fprintf oc "let config = Some \"%s/links/config\"\n" etc_path;
15+
Printf.fprintf oc "let jslib = \"%s/links/js\"\n" lib_path;
16+
Printf.fprintf oc "let examples = \"%s/links/examples\"\n" share_path;
17+
Printf.fprintf oc "let stdlib = \"%s/links/stdlib\"\n" lib_path;
18+
Printf.fprintf oc "let prelude = \"%s/links/prelude.links\"\n" lib_path;
19+
close_out oc
20+
else
21+
(* If Links is complied from source*)
22+
let git_path = input_line (Unix.open_process_in "git rev-parse --show-toplevel") in
23+
Printf.fprintf oc "let config = %s\n" "None";
24+
Printf.fprintf oc "let jslib = \"%s/lib/js\"\n" git_path;
25+
Printf.fprintf oc "let examples = \"%s/examples\"\n" git_path;
26+
Printf.fprintf oc "let stdlib = \"%s/lib/stdlib\"\n" git_path;
27+
Printf.fprintf oc "let prelude = \"%s/prelude.links\"\n" git_path;
28+
close_out oc

links.opam

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ version: "0.9.7"
1111

1212

1313
build: [
14-
[ "dune" "subst" ] {dev}
15-
[ "dune" "exec" "preinstall/preinstall.exe" "--" "-libdir" _:lib ]
16-
[ "dune" "build" "-p" name "-j" jobs ]
14+
["dune" "exec" "preinstall/preinstall.exe" "--" "-libdir" _:lib]
15+
[make "opam-build-links.opam"]
1716
]
1817

1918
depends: [

0 commit comments

Comments
 (0)