@@ -12,6 +12,12 @@ let man =
1212
1313let info = Cmd. info " utop" ~doc ~man
1414
15+ let lock_utop_if_dev_tool_enabled () =
16+ match Lazy. force Lock_dev_tool. is_enabled with
17+ | false -> Memo. return ()
18+ | true -> Lock_dev_tool. lock_dev_tool Utop
19+ ;;
20+
1521let term =
1622 let + builder = Common.Builder. term
1723 and + dir = Arg. (value & pos 0 string " " & Arg. info [] ~docv: " DIR" )
@@ -21,25 +27,41 @@ let term =
2127 let dir = Common. prefix_target common dir in
2228 if not (Path. is_directory (Path. of_string dir))
2329 then User_error. raise [ Pp. textf " cannot find directory: %s" (String. maybe_quoted dir) ];
24- let env, lib_config, utop_path, requires =
30+ let env, utop_path =
2531 Scheduler. go_with_rpc_server ~common ~config (fun () ->
2632 let open Fiber.O in
2733 let * setup = Import.Main. setup () in
2834 build_exn (fun () ->
2935 let open Memo.O in
3036 let * setup = setup in
3137 let context = Import.Main. find_context_exn setup ~name: ctx_name in
32- let utop_target =
33- let utop_target = Filename. concat dir Utop. utop_exe in
34- Path. build (Path.Build. relative (Context. build_dir context) utop_target)
38+ let utop_target_path filename =
39+ Path. build
40+ (Path.Build. relative
41+ (Context. build_dir context)
42+ (Filename. concat dir filename))
3543 in
36- Build_system. file_exists utop_target
44+ let utop_exe = utop_target_path Utop. utop_exe in
45+ let utop_findlib_conf = utop_target_path Utop. utop_findlib_conf in
46+ Build_system. file_exists utop_exe
3747 >> = function
3848 | false ->
3949 User_error. raise
4050 [ Pp. textf " no library is defined in %s" (String. maybe_quoted dir) ]
4151 | true ->
42- let * () = Build_system. build_file utop_target in
52+ let * () = Build_system. build_file utop_exe in
53+ let * () = lock_utop_if_dev_tool_enabled () in
54+ let * utop_dev_tool_lock_dir_exists =
55+ Memo.Lazy. force Utop. utop_dev_tool_lock_dir_exists
56+ in
57+ let * () =
58+ if utop_dev_tool_lock_dir_exists
59+ then
60+ (* Generate the custom findlib.conf file needed when utop is run
61+ as a dev tool. *)
62+ Build_system. build_file utop_findlib_conf
63+ else Memo. return ()
64+ in
4365 let sctx = Import.Main. find_scontext_exn setup ~name: ctx_name in
4466 let * requires =
4567 let dir = Path.Build. relative (Context. build_dir context) dir in
@@ -50,15 +72,29 @@ let term =
5072 let + ocaml = Context. ocaml context in
5173 ocaml.lib_config
5274 and + env = Super_context. context_env sctx in
53- env, lib_config, Path. to_string utop_target, requires))
75+ let env =
76+ Dune_rules.Lib_flags.L. toplevel_ld_paths requires lib_config
77+ |> Path.Set. fold
78+ ~f: (fun dir env ->
79+ Env_path. cons ~var: Ocaml.Env. caml_ld_library_path env ~dir )
80+ ~init: env
81+ in
82+ let env =
83+ if utop_dev_tool_lock_dir_exists
84+ then
85+ (* If there's a utop lockdir then dune will have built utop as a
86+ dev tool. In order for it to run correctly dune needed to
87+ generate a custom findlib.conf that contains the locations of
88+ all of utop's dependencies within the project's _build
89+ directory. Setting this environment variable causes the custom
90+ findlib.conf file to be used instead of the default
91+ findlib.conf. *)
92+ Env. add env ~var: " OCAMLFIND_CONF" ~value: (Path. to_string utop_findlib_conf)
93+ else env
94+ in
95+ env, Path. to_string utop_exe))
5496 in
5597 Hooks.End_of_build. run () ;
56- let env =
57- Dune_rules.Lib_flags.L. toplevel_ld_paths requires lib_config
58- |> Path.Set. fold
59- ~f: (fun dir env -> Env_path. cons ~var: Ocaml.Env. caml_ld_library_path env ~dir )
60- ~init: env
61- in
6298 restore_cwd_and_execve (Common. root common) utop_path args env
6399;;
64100
0 commit comments