oasis icon indicating copy to clipboard operation
oasis copied to clipboard

Provide automatic build version

Open gildor478 opened this issue 4 years ago • 0 comments

This feature request has been migrated from artifact #849 on forge.ocamlcore.org. It was assigned to user102.

user824 posted on 2010-12-29 22:26:24:

Providing something like this in myocamlbuild.ml would be nice: http://brion.inria.fr/gallium/index.php/Automatic_Version_Generation

It'd be even better if version generation is done based on version from VCS (if VCS present), and fallback to version specific in _oasis file otherwise. Or provide both (version and build_version).

For some reason

For now I've appended it: (* OASIS_STOP *) let version = Ocamlbuild_pack.My_unix.run_and_open "(git describe --always --dirty || echo 'exported')" (fun ic -> input_line ic);;

let time = let tm = Unix.gmtime (Unix.time ()) in Printf.sprintf "%04d/%02d/%02d %02dh UTC" (tm.Unix.tm_year + 1900) (tm.Unix.tm_mon + 1) tm.Unix.tm_mday tm.Unix.tm_hour;;

let make_version _ _ = let cmd = Printf.sprintf "value version = %S;\nvalue compile_time = %S;" version time in Cmd (S [ A "echo"; Quote (Sh cmd); Sh ">"; P "version.ml" ]);;

Ocamlbuild_plugin.dispatch (function | After_rules as e -> rule "version.ml" ~prod: "version.ml" make_version; dispatch_default e | e -> dispatch_default e) ;;

user102 replied on 2010-12-29 23:41:13:

At best, I can implement this for the setup-dev mode (i.e. developer mode). It cannot be implemented into setup.ml because it will add a dependency on git/darcs whatever.

I think it may make sense to implement it in setup-dev, do you agree ?

user824 replied on 2010-12-30 08:01:27:

I think what I actually want are:

  • substitution of Version from _oasis (can do with a .ab file already I think)
  • flags for availibility of git (I think I can already get this with BuildTools)
  • a command to generate a build version

So this feature request is about the latter. A field like GenerateBuildVersion (that can be conditionally enabled based on some flags) would suffice. It could take 2 params: the command to run, and a fallback string (accepting substitutions).

For C projects I usually embed the git version in released executables as well, if 'git' is available at build time. If it is not (or we're built from a tarball with no .git) then the hardcoded version from configure is used.

I'd like to use something similar for OCaml as well. And since oasis is here I thought it'd be good to have the version generation in one place instead of everyone reinventing the wheel.

I understand the dependency issue, but I think this could be handled in _oasis via a flag: Executable "foo" Path: . MainIs: main.ml GenerateBuildTime: true if flag(has_git) GenerateBuildVersion: "git describe --always --dirty", $version$

And then myocamlbuild.ml would generate a file like this: version.ml: let version=0.1;; (* from Version in _oasis ) let time = "2010/12/30 07h UTC";; ( generated by myocamlbuild.ml *) let buildversion = "foo-1-gdb60563";;

If git is not available, or we're not in a git repo, or the git command exited with non-zero status, then it would fallback to this: let version=0.1;; (* from Version in _oasis ) let time = "2010/12/30 07h UTC";; ( generated by myocamlbuild.ml *) let buildversion = version;;

user824 replied on 2010-12-30 08:11:47:

Sorry the example should have been: Executable "foo" Path: . MainIs: main.ml GenerateBuildTime: true if flag(has_git) GenerateBuildVersion: "git describe --always --dirty", $version$ else GenerateBuildVersion: $version$

And thanks for patiently answering all my feature requests :)

user102 replied on 2010-12-30 11:35:11:

The version cover the whole package, not a single binary. Moreover it is a distinct parameter in _oasis. I use it for example, in oasis-db to sort packages.

The best I can propose to you is to define an extra variable "version_extra" that will be "" in distributed setup.ml and the result of whatever command you like:

Version: 0.2.1 VersionExtra: git describe --always --dirty

The variable will be set according to SourceRepository, if you don't define it. E.g. latest revision number for svn, latest date change for darcs... + tag if there is uncommited changes.

I still need to decide if I use it by default (e.g $version use it automatically) or if you need to use ${version}${version_extra} when doing substitution.

user824 replied on 2010-12-30 11:43:24:

VersionExtra sounds good, but can ${version_extra} be used to generate a version.ml at build time? (as opposed to generating it at configure time using FilesAB)

gildor478 avatar Oct 24 '20 16:10 gildor478