Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New package variable: opamfile #4402

Merged
merged 3 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/pages/Manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,9 @@ the package being defined.
- <a id="pkgvar-build-id">`build-id`</a>:
a hash identifying the precise package version and metadata, and that of all
its dependencies
- <a id="pkgvar-opamfile">`opamfile`</a>:
if the package is installed, path of its opam file, from opam internals,
otherwise not defined

Extra variables can be defined by any package at installation time, using a
[`<pkgname>.config`](#lt-pkgname-gt-config) file with a
Expand Down
2 changes: 1 addition & 1 deletion master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ New option/command/subcommand are prefixed with ◈.
*

## Var
*
* Add `opamfile-loc` as a package variable, containing the location of installed package opam file [#4402 @rjbou]

## Option

Expand Down
16 changes: 16 additions & 0 deletions src/state/opamPackageVar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ let package_variable_names = [
"dev", "True if this is a development package";
"build-id", "A hash identifying the precise package version with all its \
dependencies";
"opamfile", "Path of the curent opam file";
]

let predefined_depends_variables =
Expand Down Expand Up @@ -305,6 +306,21 @@ let resolve st ?opam:opam_arg ?(local=OpamVariable.Map.empty) v =
with Not_found -> Some (string ""))
| "dev", Some opam -> Some (bool (is_dev_package st opam))
| "build-id", Some opam -> OpamStd.Option.map string (build_id st opam)
| "opamfile", Some opam ->
(* Opamfile path is retrieved from overlay directory for pinned packages,
or from temporary repository in /tmp *)
let repos_roots reponame =
match Hashtbl.find st.switch_repos.repos_tmp reponame with
| lazy repo_root -> repo_root
| exception Not_found ->
OpamRepositoryPath.root st.switch_global.root reponame
in
OpamFile.OPAM.get_metadata_dir ~repos_roots opam
|> OpamStd.Option.map (fun d ->
OpamFilename.Op.(d//"opam")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, does this work for pinned packages ? Can't the file be named differently?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for pinned packages, it's stored in the overlay dir, in the file opam, and from repo, its the package dir with name opam.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aah right, it'll never get back to the original srcdir, we always have a mirror, got it! 👍
maybe worth adding a comment ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always there, in the current run of opam (repo dir are tmp ones), yes. In fact, it's the code of OpamRepositoryState.get_root, that is not reachable from OpamPackageVar.
+1 for the comment

|> OpamFilename.to_string
|> string
)
| _, _ -> None
in
let make_package_local v =
Expand Down