caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Conditional compilation
@ 2009-08-18  2:00 Edgar Friendly
  2009-08-18  2:26 ` [Caml-list] " Andre Nathan
  2009-08-18 10:20 ` Richard Jones
  0 siblings, 2 replies; 6+ messages in thread
From: Edgar Friendly @ 2009-08-18  2:00 UTC (permalink / raw)
  To: caml-list

I'm working on a project that has significant build infrastructure using
autoconf, make and ocamlbuild.  I want to have a reasonably simple
trigger, such as a command-line argument to autoconf.  The result of
activating this trigger would be to drastically reduce what gets
compiled -- many files wouldn't need to be compiled, and many sections
of files should be commented out.

The project already uses optcomp for some conditional compilation based
on ocaml version.  I've learned that optcomp creates a new camlp4
command-line flag, [-let], which accepts a "var=value" pair and sets
that for use in [#if var = value] triggers.  I've also dug into
ocamlbuild and found a way to set this flag within the [-pp] argument to
ocamlc.  I've hit some hurdles, and wonder how to overcome them:

1) optcomp is sometimes used as a standalone camlp4 executable (-pp
'build/optcomp/optcomp_o.byte') - in this mode, it seems to accept only
the name of a file as input, and doesn't take the [-let] argument.  What
is the difference between this and "-pp 'camlp4oof
build/optcomp/pa_optcomp.cmo'"?

2) I'm using the identifier "AAA" for my trigger, and I can use
ocamlbuild's tags system to set "-let AAA=true" when the tag "AAA" is
set - is there any way to do "-let AAA=false" when tag "AAA" is unset?
Optcomp gives a compilation error on bound identifiers, so I have to set
it in both cases.

Is there a better way to do this?  The path I'm following is looking
very byzantine compared to C's [-DFOO] + [#ifdef FOO].

E


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

* Re: [Caml-list] Conditional compilation
  2009-08-18  2:00 Conditional compilation Edgar Friendly
@ 2009-08-18  2:26 ` Andre Nathan
  2009-08-18  2:37   ` Edgar Friendly
  2009-08-21  9:39   ` Pietro Abate
  2009-08-18 10:20 ` Richard Jones
  1 sibling, 2 replies; 6+ messages in thread
From: Andre Nathan @ 2009-08-18  2:26 UTC (permalink / raw)
  To: Edgar Friendly; +Cc: caml-list

On Mon, 2009-08-17 at 22:00 -0400, Edgar Friendly wrote:
> Is there a better way to do this?  The path I'm following is looking
> very byzantine compared to C's [-DFOO] + [#ifdef FOO].

In ospec I use

  -pp "camlp4o Camlp4MacroParser.cmo -D FOO"

which can then be tested in the code with

IFDEF FOO THEN
  ...
END


HTH,
Andre


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

* Re: [Caml-list] Conditional compilation
  2009-08-18  2:26 ` [Caml-list] " Andre Nathan
@ 2009-08-18  2:37   ` Edgar Friendly
  2009-08-18  7:00     ` Olivier Andrieu
  2009-08-21  9:39   ` Pietro Abate
  1 sibling, 1 reply; 6+ messages in thread
From: Edgar Friendly @ 2009-08-18  2:37 UTC (permalink / raw)
  To: Andre Nathan; +Cc: caml-list

Andre Nathan wrote:
> On Mon, 2009-08-17 at 22:00 -0400, Edgar Friendly wrote:
>> Is there a better way to do this?  The path I'm following is looking
>> very byzantine compared to C's [-DFOO] + [#ifdef FOO].
> 
> In ospec I use
> 
>   -pp "camlp4o Camlp4MacroParser.cmo -D FOO"
> 
> which can then be tested in the code with
> 
> IFDEF FOO THEN
>   ...
> END
> 
> 
> HTH,
> Andre
> 
It does help - I'd not considered Camlp4MacroParser for the job,
thinking that optcomp would have a good way to do exactly what its name
says - optional compilation.

I'm starting to think that optcomp is just a mismatch for what I'm
trying to do, and I'm trying to use the wrong tool for the job.  That
said, I don't think it's the best solution (especially with already long
compile times for the project) to use both optcomp and
Camlp4MacroParser, so I guess I should figure out how to do
	#if ocaml_version < (3, 11)
in Camlp4MacroParser.  I guess autoconf might be of some help here,
although I don't see an easy way to to pass text from an
autoconf-created Makefile through ocamlbuild into only some files' [-pp]
commands.  I think myocamlbuild.ml might be the key, maybe autoconf can
produce it as well as Makefile.

E.


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

* Re: [Caml-list] Conditional compilation
  2009-08-18  2:37   ` Edgar Friendly
@ 2009-08-18  7:00     ` Olivier Andrieu
  0 siblings, 0 replies; 6+ messages in thread
From: Olivier Andrieu @ 2009-08-18  7:00 UTC (permalink / raw)
  To: Edgar Friendly; +Cc: caml-list

On Tue, Aug 18, 2009 at 04:37, Edgar Friendly<thelema314@gmail.com> wrote:
> I'm starting to think that optcomp is just a mismatch for what I'm
> trying to do, and I'm trying to use the wrong tool for the job.  That
> said, I don't think it's the best solution (especially with already long
> compile times for the project) to use both optcomp and
> Camlp4MacroParser, so I guess I should figure out how to do
>        #if ocaml_version < (3, 11)
> in Camlp4MacroParser.  I guess autoconf might be of some help here,
> although I don't see an easy way to to pass text from an
> autoconf-created Makefile through ocamlbuild into only some files' [-pp]
> commands.  I think myocamlbuild.ml might be the key, maybe autoconf can
> produce it as well as Makefile.

sure, autoconf can produce any kind of file: it simply replace
occurrences of @VARIABLE@.
It's traditionally used to create makefiles or shell scripts but it'll
work on a .ml file just as well.

-- 
  Olivier


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

* Re: [Caml-list] Conditional compilation
  2009-08-18  2:00 Conditional compilation Edgar Friendly
  2009-08-18  2:26 ` [Caml-list] " Andre Nathan
@ 2009-08-18 10:20 ` Richard Jones
  1 sibling, 0 replies; 6+ messages in thread
From: Richard Jones @ 2009-08-18 10:20 UTC (permalink / raw)
  To: Edgar Friendly; +Cc: caml-list

On Mon, Aug 17, 2009 at 10:00:28PM -0400, Edgar Friendly wrote:
> Is there a better way to do this?  The path I'm following is looking
> very byzantine compared to C's [-DFOO] + [#ifdef FOO].

You can just use autoconf's ordinary features:

eg:

  AC_ARG_ENABLE([foo],
          [AS_HELP_STRING([--enable-foo],
            [enable foo @<:@default=no@:>@])],
          [],
          [enable_foo=no])
  
  # Define an automake conditional:
  AM_CONDITIONAL([FOO],[test "x$enable_foo" = "xyes"])
  
  # Define an autoconf substitution:
  if test "x$enable_foo" = "xyes"; then
    FOO=1
  else
    FOO=0
  fi
  AC_SUBST(FOO)

And then in Makefile.am you can use:

  ifdef FOO
  FLAGS = -DFOO=1
  else
  FLAGS =
  endif

and in any autoconf-substituted files you can use:

  @FOO@

We use autoconf/automake with OCaml code all the time, and it works
fine.  There are also OCaml macros available:

  http://forge.ocamlcore.org/projects/ocaml-autoconf/
  http://git.ocamlcore.org/cgi-bin/gitweb.cgi?p=ocaml-autoconf/ocaml-autoconf.git;a=summary

Rich.

-- 
Richard Jones
Red Hat


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

* Re: [Caml-list] Conditional compilation
  2009-08-18  2:26 ` [Caml-list] " Andre Nathan
  2009-08-18  2:37   ` Edgar Friendly
@ 2009-08-21  9:39   ` Pietro Abate
  1 sibling, 0 replies; 6+ messages in thread
From: Pietro Abate @ 2009-08-21  9:39 UTC (permalink / raw)
  To: caml-list

On Mon, Aug 17, 2009 at 11:26:52PM -0300, Andre Nathan wrote:
> On Mon, 2009-08-17 at 22:00 -0400, Edgar Friendly wrote:
> > Is there a better way to do this?  The path I'm following is looking
> > very byzantine compared to C's [-DFOO] + [#ifdef FOO].
> In ospec I use

In my projects I sometimes used directly cpp (Lazy me!). Today I
realized that is not such a good idea cpp interprets c++ comments - like
"\\" - and removes them from your code. For example is you compile the
following empty program with 
> ocamlbuild -pp "cpp" test.native

--- test.ml ---
(* http://localhost.org *)
---------------

you will get an error like :

File "test.ml", line 2, characters 5-7:
Error: Comment not terminated
Command exited with code 2.

that is the result of the preprocessing step :

> cpp test.ml 
# 1 "test.ml"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.ml"

     (* http:
------------------   

Just to say that "-pp camlp4o Camlp4MacroParser.cmo" is a safer choice
... And maybe I was the only fool using cpp with ocaml ...

> 
>   -pp "camlp4o Camlp4MacroParser.cmo -D FOO"
> 
> which can then be tested in the code with
> 
> IFDEF FOO THEN
>   ...
> END
> 
> 
> HTH,
> Andre
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

-- 
----
http://en.wikipedia.org/wiki/Posting_style


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

end of thread, other threads:[~2009-08-21  9:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-18  2:00 Conditional compilation Edgar Friendly
2009-08-18  2:26 ` [Caml-list] " Andre Nathan
2009-08-18  2:37   ` Edgar Friendly
2009-08-18  7:00     ` Olivier Andrieu
2009-08-21  9:39   ` Pietro Abate
2009-08-18 10:20 ` Richard Jones

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).