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

Simplify the computing of version numbers #2541

Merged
3 commits merged into from Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@

- Include building stubs in `@check` rules. (@rgrinberg, #2530)

- Get rid of ad-hoc rules for guessing the version. Dune now only
relies on the version written in the `dune-project` file and no
longer read `VERSION` or similar files (#2541, @diml)

1.11.0 (23/07/2019)
-------------------

Expand Down
32 changes: 13 additions & 19 deletions doc/advanced-topics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,22 @@ format is subject to change between versions.

.. _Catapult trace-viewer: https://github.com/catapult-project/catapult/blob/master/tracing/README.md

.. _package-version:

Package version
===============

Note that dune will try to determine the version number of packages
defined in the workspace. While dune itself makes no use of version
numbers, it can be use by external tools such as
`ocamlfind <http://projects.camlcity.org/projects/findlib.html>`__.

Dune determines the version of a package by trying the following
methods in order:

- it looks in the ``<package>.opam`` file for a ``version`` variable
- it looks for a ``<package>.version`` file in the same directory and
reads the first line
- it looks for the version specified in the ``dune-project`` if present
- it looks for a ``version`` file and reads the first line
- it looks for a ``VERSION`` file and reads the first line

``<package>.version``, ``version`` and ``VERSION`` files may be
generated.

If the version can't be determined, dune just won't assign one.
This conversation was marked as resolved.
Show resolved Hide resolved
Dune determine the version of a package by looking at the ``version``
field in the :ref:`package stanza <package>`. If the version field is
not set, it looks at the toplevel ``version`` field in the
``dune-project`` field. If neither are set, dune assume that we are in
development mode and reads the version from the VCS if any. The way it
obtains the version from the VCS in described in :ref:`the build-info
section <build-info>`.

When installing the files of a package on the system, dune
automatically inserts the package version into various metadata files
such as ``META`` and ``dune-package`` files.

.. _ocaml-syntax:

Expand Down
3 changes: 2 additions & 1 deletion doc/concepts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ In addition, ``(action ...)`` fields support the following special variables:

- ``version:<package>`` expands to the version of the given
package. Note that this is only supported for packages that are
being defined in the current scope
being defined in the current scope. How dune determines the version
of a package is described :ref:`here <package-version>`
- ``read:<path>`` expands to the contents of the given file
- ``read-lines:<path>`` expands to the list of lines in the given
file
Expand Down
2 changes: 2 additions & 0 deletions doc/dune-files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ defined in the project:

- ``(documentation <url>)`` - Where the documentation is hosted

.. _package:

package
-------

Expand Down
2 changes: 2 additions & 0 deletions doc/dune-libs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ The following steps must be taken to transition from the old configurator:

.. _pkg-config: https://www.freedesktop.org/wiki/Software/pkg-config/

.. _build-info:

build-info
==========

Expand Down
2 changes: 2 additions & 0 deletions src/dune/build.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ let arr f = Arr f

let return x = Arr (fun _ -> x)

let delayed = arr

let record_lib_deps lib_deps = Record_lib_deps lib_deps

module O = struct
Expand Down
3 changes: 3 additions & 0 deletions src/dune/build.mli
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ val all : ('a, 'b) t list -> ('a, 'b list) t
contains no targets. *)
val lazy_no_targets : ('a, 'b) t Lazy.t -> ('a, 'b) t

(** Delay a static computation until the arrow is evaluated *)
val delayed : (unit -> 'a) -> (unit, 'a) t

(* CR-someday diml: this API is not great, what about:

{[ module Action_with_deps : sig type t val add_file_dependency : t ->
Expand Down
9 changes: 2 additions & 7 deletions src/dune/dune_project.ml
Original file line number Diff line number Diff line change
Expand Up @@ -773,8 +773,7 @@ let parse ~dir ~lang ~opam_packages ~file =
match version with
| None ->
packages
| Some version ->
let version = Some (version, Package.Version_source.Project) in
| Some _ ->
Package.Name.Map.map packages ~f:(fun p ->
match p.version with Some _ -> p | None -> { p with version })
in
Expand Down Expand Up @@ -915,11 +914,7 @@ let load ~dir ~files =
None
in
let* version = Opam_file.get_field opam "version" in
match version with
| String (_, s) ->
Some (s, Package.Version_source.Package)
| _ ->
None
match version with String (_, s) -> Some s | _ -> None
in
{ Package.name
; loc
Expand Down
66 changes: 26 additions & 40 deletions src/dune/expander.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ let expand_env t pform s : Value.t list option =
else
Some [ String (Option.value ~default (Env.get t.env var)) ]

let expand_version scope pform s =
match
Package.Name.Map.find
(Dune_project.packages (Scope.project scope))
(Package.Name.of_string s)
with
| None ->
User_error.raise
~loc:(String_with_vars.Var.loc pform)
[ Pp.textf "Package %S doesn't exist in the current project." s ]
| Some p -> (
match p.version with None -> [ Value.String "" ] | Some s -> [ String s ] )

let expand_var_exn t var syn =
t.expand_var t var syn
|> Option.map ~f:(function
Expand Down Expand Up @@ -120,6 +133,8 @@ let make ~scope ~(context : Context.t) ~lib_artifacts ~bin_artifacts_host =
Some (Ok (expand_ocaml_config (Lazy.force ocaml_config) var s))
| Macro (Env, s) ->
Option.map ~f:Result.ok (expand_env t var s)
| Macro (Version, s) ->
Some (Ok (expand_version scope var s))
| Var Project_root ->
Some (Ok [ Value.Dir (Path.build (Scope.root scope)) ])
| expansion ->
Expand Down Expand Up @@ -234,12 +249,8 @@ let parse_lib_file ~loc s =
| Some (lib, f) ->
(Lib_name.of_string_exn ~loc:(Some loc) lib, f)

type dynamic = { read_package : Package.t -> (unit, string option) Build.t }

let error_expansion_kind = { read_package = (fun _ -> assert false) }

type expansion_kind =
| Dynamic of dynamic
| Dynamic
| Static

let cc_of_c_flags t (cc : (unit, string list) Build.t C.Kind.Dict.t) =
Expand All @@ -254,7 +265,7 @@ let resolve_binary t ~loc ~prog =
| Error e ->
Error { Import.fail = (fun () -> Action.Prog.Not_found.raise e) }

let expand_and_record acc ~map_exe ~dep_kind ~scope ~expansion_kind
let expand_and_record acc ~map_exe ~dep_kind ~expansion_kind
~(dir : Path.Build.t) ~pform t expansion
~(cc : dir:Path.Build.t -> (unit, Value.t list) Build.t C.Kind.Dict.t) =
let key = String_with_vars.Var.full_name pform in
Expand All @@ -268,12 +279,9 @@ let expand_and_record acc ~map_exe ~dep_kind ~scope ~expansion_kind
[ Pp.textf "%s cannot be used in this position"
(String_with_vars.Var.describe pform)
]
| Dynamic _ ->
| Dynamic ->
Resolved_forms.add_ddep acc ~key
in
let { read_package } =
match expansion_kind with Static -> error_expansion_kind | Dynamic d -> d
in
let open Build.O in
match (expansion : Pform.Expansion.t) with
| Var
Expand All @@ -284,7 +292,7 @@ let expand_and_record acc ~map_exe ~dep_kind ~scope ~expansion_kind
| Target
| Named_local
| Values _ )
| Macro ((Ocaml_config | Env), _) ->
| Macro ((Ocaml_config | Env | Version), _) ->
assert false
| Var Cc ->
add_ddep (cc ~dir).c
Expand Down Expand Up @@ -350,28 +358,6 @@ let expand_and_record acc ~map_exe ~dep_kind ~scope ~expansion_kind
let path = relative dir s in
let data = Build.strings path >>^ Value.L.strings in
add_ddep data
| Macro (Version, s) -> (
match
Package.Name.Map.find
(Dune_project.packages (Scope.project scope))
(Package.Name.of_string s)
with
| Some p ->
let open Build.O in
let x =
read_package p
>>^ function None -> [ Value.String "" ] | Some s -> [ String s ]
in
add_ddep x
| None ->
Resolved_forms.add_fail acc
{ fail =
(fun () ->
User_error.raise ~loc
[ Pp.textf "Package %S doesn't exist in the current project."
s
])
} )

let check_multiplicity ~pform ~declaration ~use =
let module Multiplicity = Dune_file.Rule.Targets.Multiplicity in
Expand All @@ -392,7 +378,7 @@ let check_multiplicity ~pform ~declaration ~use =
| Multiple, One ->
error "targets" "target"

let expand_and_record_deps acc ~(dir : Path.Build.t) ~read_package ~dep_kind
let expand_and_record_deps acc ~(dir : Path.Build.t) ~dep_kind
~targets_written_by_user ~map_exe ~expand_var ~cc t pform syntax_version =
let res =
let targets ~(multiplicity : Dune_file.Rule.Targets.Multiplicity.t) =
Expand Down Expand Up @@ -424,7 +410,8 @@ let expand_and_record_deps acc ~(dir : Path.Build.t) ~read_package ~dep_kind
Some s
| Error (expansion : Pform.Expansion.t) -> (
match expansion with
| Var (Project_root | Values _) | Macro ((Ocaml_config | Env), _) ->
| Var (Project_root | Values _)
| Macro ((Ocaml_config | Env | Version), _) ->
assert false (* these have been expanded statically *)
| Var (First_dep | Deps | Named_local) ->
None
Expand All @@ -433,8 +420,7 @@ let expand_and_record_deps acc ~(dir : Path.Build.t) ~read_package ~dep_kind
| Var Target ->
targets ~multiplicity:One
| _ ->
expand_and_record acc ~map_exe ~dep_kind ~scope:t.scope
~expansion_kind:(Dynamic { read_package })
expand_and_record acc ~map_exe ~dep_kind ~expansion_kind:Dynamic
~dir ~pform ~cc t expansion ))
in
Option.iter res ~f:(fun v ->
Expand All @@ -450,7 +436,7 @@ let expand_no_ddeps acc ~dir ~dep_kind ~map_exe ~expand_var ~cc t pform
| Ok s ->
Some s
| Error (expansion : Pform.Expansion.t) ->
expand_and_record acc ~map_exe ~dep_kind ~scope:t.scope ~cc
expand_and_record acc ~map_exe ~dep_kind ~cc
~expansion_kind:Static ~dir ~pform t expansion)
in
Option.iter res ~f:(fun v ->
Expand All @@ -474,8 +460,8 @@ let gen_with_record_deps ~expand t resolved_forms ~dep_kind ~map_exe
in
{ t with expand_var; bindings }

let with_record_deps t resolved_forms ~read_package ~targets_written_by_user =
let expand = expand_and_record_deps ~read_package ~targets_written_by_user in
let with_record_deps t resolved_forms ~targets_written_by_user =
let expand = expand_and_record_deps ~targets_written_by_user in
gen_with_record_deps ~expand t resolved_forms

let with_record_no_ddeps = gen_with_record_deps ~expand:expand_no_ddeps
Expand Down
1 change: 0 additions & 1 deletion src/dune/expander.mli
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ end
val with_record_deps :
t
-> Resolved_forms.t
-> read_package:(Package.t -> (unit, string option) Build.t)
-> targets_written_by_user:Targets.t
-> dep_kind:Lib_deps_info.Kind.t
-> map_exe:(Path.t -> Path.t)
Expand Down
Loading