caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Build number and date in OCaml?
@ 2013-04-09 11:38 Étienne André
  2013-04-09 12:18 ` Jeremie Dimino
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Étienne André @ 2013-04-09 11:38 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 763 bytes --]

Hi,

I've been using OCaml for a couple of years, but without using any advanced
feature; so my question may be a little naive.
Is there any way to insert easily the current date and time of compiling,
as well as, e.g., an incremental build number in an OCaml program?
So that it is printed at runtime, e.g., in the program header.

I quite stupidly used the Unix.gettimeofday() function before realizing
that it is of course executed at runtime.

Of course, I could do it using an external script that would modify the
OCaml source code before compiling, but is there any native OCaml feature
for achieving this in a cleaner manner?

Thank you,
--
Étienne André
Université Paris 13, Sorbonne Paris Cité
http://www-lipn.univ-paris13.fr/~andre

[-- Attachment #2: Type: text/html, Size: 876 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Build number and date in OCaml?
  2013-04-09 11:38 [Caml-list] Build number and date in OCaml? Étienne André
@ 2013-04-09 12:18 ` Jeremie Dimino
  2013-04-10  8:49 ` Julien Signoles
  2013-04-11 13:00 ` Alain Frisch
  2 siblings, 0 replies; 6+ messages in thread
From: Jeremie Dimino @ 2013-04-09 12:18 UTC (permalink / raw)
  To: Étienne André; +Cc: caml-list

Hi Etienne,

On Tue, Apr 9, 2013 at 12:38 PM, Étienne André
<Etienne.Andre@univ-paris13.fr> wrote:
> I've been using OCaml for a couple of years, but without using any advanced
> feature; so my question may be a little naive.
> Is there any way to insert easily the current date and time of compiling, as
> well as, e.g., an incremental build number in an OCaml program?
> So that it is printed at runtime, e.g., in the program header.
>
> I quite stupidly used the Unix.gettimeofday() function before realizing that
> it is of course executed at runtime.
>
> Of course, I could do it using an external script that would modify the
> OCaml source code before compiling, but is there any native OCaml feature
> for achieving this in a cleaner manner?

I don't know of such a feature.  One thing you can do is to write a
small camlp4/ppx extension that will replace for instance BUILD_DATE
by the current date.

But I think the standard way to have the build date/number at runtime
is to generate a small file containing this information and link it
with the other units. If you don't want to recompile everything every
time the date changes, just make sure that the other units do not
depend on the implementation on this file (i.e. they must not see the
.cmx), or generate a C file instead of a .ml file.

Cheers,

-- 
Jeremie

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Build number and date in OCaml?
  2013-04-09 11:38 [Caml-list] Build number and date in OCaml? Étienne André
  2013-04-09 12:18 ` Jeremie Dimino
@ 2013-04-10  8:49 ` Julien Signoles
  2013-04-10  9:08   ` David Allsopp
  2013-04-11 13:00 ` Alain Frisch
  2 siblings, 1 reply; 6+ messages in thread
From: Julien Signoles @ 2013-04-10  8:49 UTC (permalink / raw)
  To: Étienne André; +Cc: Caml List

[-- Attachment #1: Type: text/plain, Size: 1180 bytes --]

Hello,

2013/4/9 Étienne André <Etienne.Andre@univ-paris13.fr>

> I've been using OCaml for a couple of years, but without using any
> advanced feature; so my question may be a little naive.
> Is there any way to insert easily the current date and time of compiling,
> as well as, e.g., an incremental build number in an OCaml program?

So that it is printed at runtime, e.g., in the program header.
>

This kind of information is part of your build process and are not directly
accessible in OCaml. If you want to access it in your OCaml program, you
have to pass them from the build environment to the program environment. As
Jeremie Dimino said, the usual way is to general a small OCaml file at
build time and to link it to your program.

For instance, if you use 'make', you could have the following lines in your
Makefile:

VERSION=...
config.ml: Makefile
  echo "let version = \"$(VERSION)\"" > $@
  echo "let compilation_date = \"`date`\" >> $@

CMO_FILES = config.cmo ... (* other cmo files)

Of course, it is better to add the corresponding config.mli by hand:
config.mli:
val version: string
val date: string

Hope this helps,
Julien

[-- Attachment #2: Type: text/html, Size: 1876 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [Caml-list] Build number and date in OCaml?
  2013-04-10  8:49 ` Julien Signoles
@ 2013-04-10  9:08   ` David Allsopp
  0 siblings, 0 replies; 6+ messages in thread
From: David Allsopp @ 2013-04-10  9:08 UTC (permalink / raw)
  To: Julien Signoles, Étienne André; +Cc: Caml List

Julien Signoles wrote:
> 2013/4/9 Étienne André <Etienne.Andre@univ-paris13.fr>
> I've been using OCaml for a couple of years, but without using any advanced feature;
> so my question may be a little naive. Is there any way to insert easily the current 
> date and time of compiling, as well as, e.g., an incremental build number in an OCaml
> program? So that it is printed at runtime, e.g., in the program header.
>
> This kind of information is part of your build process and are not directly accessible
> in OCaml. If you want to access it in your OCaml program, you have to pass them from 
> the build environment to the program environment. As Jeremie Dimino said, the usual way
> is to general a small OCaml file at build time and to link it to your program.
> For instance, if you use 'make', you could have the following lines in your Makefile:
> VERSION=...
> config.ml: Makefile

NB - if you want the build stamp ever to be updated, it'll need to depend on more than Makefile. You can either have it depend on all of your ML source files or, if using GNU make, you could declare config.ml as .PHONY (which means it will be rebuilt at every invocation of make).

<snip>

> CMO_FILES = config.cmo ... (* other cmo files *)

There's a further subtlety which can come into play if your build has more than one output, which is to place config.cmo / config.cmx as an order-only dependency. For example, suppose your build system has two programs whose sources are Foo.ml and Bar.ml both of which depend on Common.ml. If you structure your Makefile as:

Config.ml: Foo.ml Bar.ml Common.ml
	...

foo.exe: Common.cmx Foo.cmx | Config.cmx
	...

bar.exe: Common.cmx Bar.cmx | Config.cmx
	...

then updates to Foo.ml will not cause bar.exe to be rebuilt. The advantage of this approach is that the generating of the build stamp does not interfere with make - i.e. you don't get any additional recompilations. Of course, there are times where you do want all output programs to have the same build stamp but most of the time (i.e. while developing!) you don't want a change in one small part of the system to force recompilation/linking of the whole system... 

HTH,


David

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Build number and date in OCaml?
  2013-04-09 11:38 [Caml-list] Build number and date in OCaml? Étienne André
  2013-04-09 12:18 ` Jeremie Dimino
  2013-04-10  8:49 ` Julien Signoles
@ 2013-04-11 13:00 ` Alain Frisch
  2013-04-12 14:38   ` Daniel Weil
  2 siblings, 1 reply; 6+ messages in thread
From: Alain Frisch @ 2013-04-11 13:00 UTC (permalink / raw)
  To: Étienne André, caml-list

On 04/09/2013 01:38 PM, Étienne André wrote:
> I quite stupidly used the Unix.gettimeofday() function before realizing
> that it is of course executed at runtime.

As others suggested, you can tell your build system to generate an ad 
hoc file containing the compile-time information.  Another approach is 
to use a preprocessor to inject such compile-time information into the 
source code "on the fly" during its compilation.  This can be done with 
a dedicated Camlp4 syntax extension or a -ppx preprocessor (available in 
trunk only, with syntactic extension points being designed in the 
extension_points branch of the OCaml SVN).

As an illustration of the -ppx approach, I've created a tiny 
preprocessor which uses the OCaml toplevel to evaluate expressions and 
inserts the result as constants in the compiled code.

The source code for this -ppx preprocessor can be found here:

http://caml.inria.fr/cgi-bin/viewvc.cgi/ocaml/branches/extension_points/experimental/frisch/eval.ml?&view=markup

and here is an example of what you can write with it:

http://caml.inria.fr/cgi-bin/viewvc.cgi/ocaml/branches/extension_points/experimental/frisch/test_eval.ml?&view=markup

(To play with it, you need to checkout the extension_points branch and 
after compiling it: cd experimental/frisch && make eval)

-- Alain

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Build number and date in OCaml?
  2013-04-11 13:00 ` Alain Frisch
@ 2013-04-12 14:38   ` Daniel Weil
  0 siblings, 0 replies; 6+ messages in thread
From: Daniel Weil @ 2013-04-12 14:38 UTC (permalink / raw)
  Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 2440 bytes --]

If you like ocambuild, an alternative is to add a small rule in your
ocamlbuild plugin.
The ocamlbuild wiki gives an example of ocamlbuild plugin that create a "
version.ml" file in the _build directory at each build. You can then link
your code with this version.ml file.

http://brion.inria.fr/gallium/index.php/Automatic_Version_Generation

Daniel




2013/4/11 Alain Frisch <alain.frisch@lexifi.com>

> On 04/09/2013 01:38 PM, Étienne André wrote:
>
>> I quite stupidly used the Unix.gettimeofday() function before realizing
>> that it is of course executed at runtime.
>>
>
> As others suggested, you can tell your build system to generate an ad hoc
> file containing the compile-time information.  Another approach is to use a
> preprocessor to inject such compile-time information into the source code
> "on the fly" during its compilation.  This can be done with a dedicated
> Camlp4 syntax extension or a -ppx preprocessor (available in trunk only,
> with syntactic extension points being designed in the extension_points
> branch of the OCaml SVN).
>
> As an illustration of the -ppx approach, I've created a tiny preprocessor
> which uses the OCaml toplevel to evaluate expressions and inserts the
> result as constants in the compiled code.
>
> The source code for this -ppx preprocessor can be found here:
>
> http://caml.inria.fr/cgi-bin/**viewvc.cgi/ocaml/branches/**
> extension_points/experimental/**frisch/eval.ml?&view=markup<http://caml.inria.fr/cgi-bin/viewvc.cgi/ocaml/branches/extension_points/experimental/frisch/eval.ml?&view=markup>
>
> and here is an example of what you can write with it:
>
> http://caml.inria.fr/cgi-bin/**viewvc.cgi/ocaml/branches/**
> extension_points/experimental/**frisch/test_eval.ml?&view=**markup<http://caml.inria.fr/cgi-bin/viewvc.cgi/ocaml/branches/extension_points/experimental/frisch/test_eval.ml?&view=markup>
>
> (To play with it, you need to checkout the extension_points branch and
> after compiling it: cd experimental/frisch && make eval)
>
> -- Alain
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/**arc/caml-list<https://sympa.inria.fr/sympa/arc/caml-list>
> Beginner's list: http://groups.yahoo.com/group/**ocaml_beginners<http://groups.yahoo.com/group/ocaml_beginners>
> Bug reports: http://caml.inria.fr/bin/caml-**bugs<http://caml.inria.fr/bin/caml-bugs>
>

[-- Attachment #2: Type: text/html, Size: 3503 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-04-12 14:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-09 11:38 [Caml-list] Build number and date in OCaml? Étienne André
2013-04-09 12:18 ` Jeremie Dimino
2013-04-10  8:49 ` Julien Signoles
2013-04-10  9:08   ` David Allsopp
2013-04-11 13:00 ` Alain Frisch
2013-04-12 14:38   ` Daniel Weil

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).