From ed3c71abd4fd5a57894a554b003f9dd60539b8a5 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Fri, 22 Nov 2019 18:06:19 +0700 Subject: [PATCH] Add a simple relocation library Add the interface to the build info library. Since the relocation library needs some build information. Signed-off-by: Rudi Grinberg --- bin/install_uninstall.ml | 2 +- otherlibs/build-info/src/build_info.ml | 28 ++++++++++++++++++++ otherlibs/build-info/src/build_info.mli | 18 +++++++++++++ otherlibs/build-info/src/build_info_data.mli | 2 ++ otherlibs/build-info/test/run.t | 4 ++- src/dune/context.ml | 3 +-- src/dune/context.mli | 2 +- src/dune/link_time_code_gen.ml | 5 ++++ 8 files changed, 59 insertions(+), 5 deletions(-) diff --git a/bin/install_uninstall.ml b/bin/install_uninstall.ml index 1b5af8dc294..ce1499e2681 100644 --- a/bin/install_uninstall.ml +++ b/bin/install_uninstall.ml @@ -15,7 +15,7 @@ let get_dirs context ~prefix_from_command_line ~libdir_from_command_line = Fiber.return (prefix, Some (Path.relative prefix dir)) | None -> let open Fiber.O in - let* prefix = Context.install_prefix context in + let prefix = Context.install_prefix context in let libdir = match libdir_from_command_line with | None -> Context.install_ocaml_libdir context diff --git a/otherlibs/build-info/src/build_info.ml b/otherlibs/build-info/src/build_info.ml index ffefb20bebf..6fbffe57ee1 100644 --- a/otherlibs/build-info/src/build_info.ml +++ b/otherlibs/build-info/src/build_info.ml @@ -23,4 +23,32 @@ module V1 = struct end let version () = Build_info_data.version + + module Path = struct + type t = string + + let to_string t = t + + let of_string t = t + + let relative = Filename.concat + end + + module Location = struct + type t = + | Etc + | Share of { package : string } + + let component = function + | Etc -> "etc" + | Share { package } -> Printf.sprintf "share/%s" package + end + + let registry_root = + lazy + ( match Sys.getenv_opt "DUNE_REGISTRY_ROOT" with + | Some s -> s + | None -> Build_info_data.artifact_root ) + + let get l = Path.relative (Lazy.force registry_root) (Location.component l) end diff --git a/otherlibs/build-info/src/build_info.mli b/otherlibs/build-info/src/build_info.mli index 9b83215b332..367f9664d58 100644 --- a/otherlibs/build-info/src/build_info.mli +++ b/otherlibs/build-info/src/build_info.mli @@ -29,4 +29,22 @@ module V1 : sig val find : name:string -> Statically_linked_library.t option end + + module Path : sig + type t + + val to_string : t -> string + + val of_string : string -> t + + val relative : t -> string -> t + end + + module Location : sig + type t = + | Etc + | Share of { package : string } + end + + val get : Location.t -> Path.t end diff --git a/otherlibs/build-info/src/build_info_data.mli b/otherlibs/build-info/src/build_info_data.mli index c5549333638..deb980834f9 100644 --- a/otherlibs/build-info/src/build_info_data.mli +++ b/otherlibs/build-info/src/build_info_data.mli @@ -3,3 +3,5 @@ val version : string option val statically_linked_libraries : (string * string option) list + +val artifact_root : string diff --git a/otherlibs/build-info/test/run.t b/otherlibs/build-info/test/run.t index 683e6bf29e4..723cf8f912f 100644 --- a/otherlibs/build-info/test/run.t +++ b/otherlibs/build-info/test/run.t @@ -85,7 +85,8 @@ Once installed, we have the version information: Check what the generated build info module looks like: $ cat _build/default/c/.c.eobjs/build_info_data.ml-gen \ - > | sed 's/"dune-build-info".*/"dune-build-info", Some "XXX"/' + > | sed 's/"dune-build-info".*/"dune-build-info", Some "XXX"/' \ + > | sed 's/let artifact_root = .*/let artifact_root = " .. "/' let eval s = let len = String.length s in if s.[0] = '=' then @@ -104,6 +105,7 @@ Check what the generated build info module looks like: let p0 = eval "%%DUNE_PLACEHOLDER:64:vcs-describe:1:c%%%%%%%%%%%%%%%%%%%%%%%%%%" let version = p0 + let artifact_root = " .. " let statically_linked_libraries = [ "a", p1 diff --git a/src/dune/context.ml b/src/dune/context.ml index 0f45ef00df9..237d66d4795 100644 --- a/src/dune/context.ml +++ b/src/dune/context.ml @@ -765,8 +765,7 @@ end let which t s = which ~cache:t.which_cache ~path:t.path s let install_prefix t = - opam_config_var t "prefix" - >>| function + match Env.get t.env "OPAM_PREFIX" with | Some x -> Path.of_filename_relative_to_initial_cwd x | None -> Path.parent_exn t.ocaml_bin diff --git a/src/dune/context.mli b/src/dune/context.mli index f6859858c5f..fb12944e3a4 100644 --- a/src/dune/context.mli +++ b/src/dune/context.mli @@ -107,7 +107,7 @@ val which : t -> string -> Path.t option val opam_config_var : t -> string -> string option Fiber.t -val install_prefix : t -> Path.t Fiber.t +val install_prefix : t -> Path.t val install_ocaml_libdir : t -> Path.t option Fiber.t diff --git a/src/dune/link_time_code_gen.ml b/src/dune/link_time_code_gen.ml index 80c9ee4644c..e21a3d69c5c 100644 --- a/src/dune/link_time_code_gen.ml +++ b/src/dune/link_time_code_gen.ml @@ -136,6 +136,10 @@ let build_info_code cctx ~libs ~api_version = let p = Path.Build.drop_build_context_exn (CC.dir cctx) in placeholder p in + let artifact_root = + let context = Compilation_context.context cctx in + Context.install_prefix context |> Path.to_absolute_filename + in let libs = List.map libs ~f:(fun lib -> ( Lib.name lib @@ -171,6 +175,7 @@ let build_info_code cctx ~libs ~api_version = (Artifact_substitution.encode ~min_len:64 (Vcs_describe path))); if not (Path.Source.Map.is_empty !placeholders) then pr buf ""; pr buf "let version = %s" version; + pr buf "let artifact_root = %S" artifact_root; pr buf ""; prlist buf "statically_linked_libraries" libs ~f:(fun (name, v) -> pr buf "%S, %s" (Lib_name.to_string name) v);