"ocaml_beginners"::[] Conditional compilation

View: New views
11 Messages — Rating Filter:   Alert me  

"ocaml_beginners"::[] Conditional compilation

by cultural_sublimation :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Fellow Camels,

How does one handle conditional compilation in OCaml?  I want certain
pieces of debugging code to be included only if a certain environment
variable is set.  This sort of thing is really easy to do in the C
preprocessor with #ifdefs.

And yes, I know I can use cpp also with OCaml, but I was wondering
if there was a more "bactrian" way of doing this.

Thanks,
C.S.



Re: "ocaml_beginners"::[] Conditional compilation

by Matthieu Dubuget :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

cultural_sublimation a écrit :

>
>
> Fellow Camels,
>
> How does one handle conditional compilation in OCaml? I want certain
> pieces of debugging code to be included only if a certain environment
> variable is set. This sort of thing is really easy to do in the C
> preprocessor with #ifdefs.
>
> And yes, I know I can use cpp also with OCaml, but I was wondering
> if there was a more "bactrian" way of doing this.
>
> Thanks,
> C.S.
>

You can use camlp4 with pa_macro.
You have then #define, #if construction,
and can use -D ident and -U ident to add or
remove macro definition.

I do not have the magic invocation at hand,
but you'll find.

Hoping this will help.

Salutations

Matt

Re: "ocaml_beginners"::[] Conditional compilation

by Joerg van den Hoff-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, Oct 28, 2007 at 08:07:46PM -0000, cultural_sublimation wrote:

> Fellow Camels,
>
> How does one handle conditional compilation in OCaml?  I want certain
> pieces of debugging code to be included only if a certain environment
> variable is set.  This sort of thing is really easy to do in the C
> preprocessor with #ifdefs.
>
> And yes, I know I can use cpp also with OCaml, but I was wondering
> if there was a more "bactrian" way of doing this.
>
> Thanks,
> C.S.
>

is  using  `Sys.getenv'  to  simple  minded  and  out of the
question?  this is not conditional compilation, sure, but if
you      use      a     reasonable     "unprobable"     name
("MYPROGRAM_DEBUGGING_OUTPUT"  ...)  for   the   environment
variable, it would do the job, essentially.

joerg

Re: "ocaml_beginners"::[] Conditional compilation

by Richard Jones-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Oct 29, 2007 at 12:35:20PM +0100, Joerg van den Hoff wrote:

> On Sun, Oct 28, 2007 at 08:07:46PM -0000, cultural_sublimation wrote:
> > Fellow Camels,
> >
> > How does one handle conditional compilation in OCaml?  I want certain
> > pieces of debugging code to be included only if a certain environment
> > variable is set.  This sort of thing is really easy to do in the C
> > preprocessor with #ifdefs.
> >
> > And yes, I know I can use cpp also with OCaml, but I was wondering
> > if there was a more "bactrian" way of doing this.
>
> is  using  `Sys.getenv'  to  simple  minded  and  out of the
> question?  this is not conditional compilation, sure, but if
> you      use      a     reasonable     "unprobable"     name
> ("MYPROGRAM_DEBUGGING_OUTPUT"  ...)  for   the   environment
> variable, it would do the job, essentially.

Conditional code works for simple debugging case, but in other cases
you often need C-like macros, impotent as they may be compared to real
macros.

For example:

  let package = "@PACKAGE@"
  let version = "@VERSION@"

where @PACKAGE@ and @VERSION@ are substituted in your Makefile or
configure script.  To do this, just isolate these definitions into a
separate module and use autoconf.

Another example:

  let dialog =
  #if LABLGTK <= 2.0
    GDialog.dialog ~main_title:title ()
  #else
    GDialog.dialog ~title ()
  #endif

[This isn't an exact example, but lablgtk does change the types of its
API calls often enough that compiling against several versions
requires C macros and cannot be done using ordinary if ... then ...
clauses].

Rich.

--
Richard Jones
Red Hat

Re: "ocaml_beginners"::[] Conditional compilation

by clconway :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 10/28/07, Matthieu Dubuget <matthieu.dubuget@...> wrote:

> cultural_sublimation a écrit :
> >
> >
> > Fellow Camels,
> >
> > How does one handle conditional compilation in OCaml? I want certain
> > pieces of debugging code to be included only if a certain environment
> > variable is set. This sort of thing is really easy to do in the C
> > preprocessor with #ifdefs.
> >
> > And yes, I know I can use cpp also with OCaml, but I was wondering
> > if there was a more "bactrian" way of doing this.
> >
> > Thanks,
> > C.S.
> >
>
> You can use camlp4 with pa_macro.
> You have then #define, #if construction,
> and can use -D ident and -U ident to add or
> remove macro definition.
>
> I do not have the magic invocation at hand,
> but you'll find.

In OCaml version <= 3.09:
ocamlc -pp "camlp4o pa_macro.cmo"

To define/undefine symbols on the command line:
ocamlc -pp "camlp4o pa_macro.cmo -DFOO -UBAR"

See also here for camlp4 extensions for debugging and command-line
macros with values:
http://www.cs.nyu.edu/~cconway/tools/index.html

Regards,
Chris

"ocaml_beginners"::[] Re: Conditional compilation

by cultural_sublimation :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Camels,

Thank you everyone for your help.  Now I know it should at least be
possible to achieve what I want.  However, I've never used camlp4
(and I'm running Ocaml 3.10) so I'm afraid that some of your suggestions
just went over my head.  All I want is something as simple as this:

ifdef DEBUG
then Printf.printf "debug!\n"
else ()

Surely that camlp4 allows for this with just a few lines, no?

Thanks,
C.S.



Re: "ocaml_beginners"::[] Re: Conditional compilation

by Matthieu Dubuget :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

cultural_sublimation a écrit :

>
>
> Camels,
>
> Thank you everyone for your help. Now I know it should at least be
> possible to achieve what I want. However, I've never used camlp4
> (and I'm running Ocaml 3.10) so I'm afraid that some of your suggestions
> just went over my head. All I want is something as simple as this:
>
> ifdef DEBUG
> then Printf.printf "debug!\n"
> else ()
>
> Surely that camlp4 allows for this with just a few lines, no?
>

yes.

pa_macro is distributed with Objective Caml.

Here are all the allowed constructions added by pa_macro. It was copied
from the source file.

>  At toplevel (structure item):
>
>      DEFINE <uident>
>      DEFINE <uident> = <expression>
>      DEFINE <uident> (<parameters>) = <expression>
>      IFDEF <uident> THEN <structure_items> [ ELSE <structure_items> ] (END | ENDIF)
>      IFNDEF <uident> THEN <structure_items> [ ELSE <structure_items> ] (END | ENDIF)
>      INCLUDE <string>
>
>   At toplevel (signature item):
>
>      DEFINE <uident>
>      IFDEF <uident> THEN <signature_items> [ ELSE <signature_items> ] (END | ENDIF)
>      IFNDEF <uident> THEN <signature_items> [ ELSE <signature_items> ] (END | ENDIF)
>      INCLUDE <string>
>
>   In expressions:
>
>      IFDEF <uident> THEN <expression> [ ELSE <expression> ] (END | ENDIF)
>      IFNDEF <uident> THEN <expression> [ ELSE <expression> ] (END | ENDIF)
>      DEFINE <lident> = <expression> IN <expression>
>      __FILE__
>      __LOCATION__
>
>   In patterns:
>
>      IFDEF <uident> THEN <pattern> ELSE <pattern> (END | ENDIF)
>      IFNDEF <uident> THEN <pattern> ELSE <pattern> (END | ENDIF)
>
>   As Camlp4 options:
>
>      -D<uident> or -D<uident>=expr   define <uident> with optional value <expr>
>      -U<uident>                      undefine it
>      -I<dir>                         add <dir> to the search path for INCLUDE'd files
>
>   After having used a DEFINE <uident> followed by "= <expression>", you
>   can use it in expressions *and* in patterns. If the expression defining
>   the macro cannot be used as a pattern, there is an error message if
>   it is used in a pattern.
>
>   You can also define a local macro in an expression usigng the DEFINE ... IN form.
>   Note that local macros have lowercase names and can not take parameters.
>
>   If a macro is defined to = NOTHING, and then used as an argument to a function,
>   this will be equivalent to function taking one less argument. Similarly,
>   passing NOTHING as an argument to a macro is equivalent to "erasing" the
>   corresponding parameter from the macro body.
>
>   The toplevel statement INCLUDE <string> can be used to include a
>   file containing macro definitions and also any other toplevel items.
>   The included files are looked up in directories passed in via the -I
>   option, falling back to the current directory.
>
>   The expression __FILE__ returns the current compiled file name.
>   The expression __LOCATION__ returns the current location of itself.


With OCaml 3.10, -pp "camlp4o pa_macro.cmo" is also ok to have pa_macro
used to preprocess your files.

Salutations

Matt

"ocaml_beginners"::[] ocamlopt -p & gprof

by Onur Ozyer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I want to use gprof to profile the perf.


I add the "-p" to ocamlopt, where the sources aer
compiled and ML library is linked. "ocamlopt" works
succesfully.

However, at the last step, where the executables are
linked, I'm having this error:

    Cannot find file std_exit.p.cmx

I don't have a file with "*std_exit*" Does anyone know
why it coud be happening ?

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 

Re: "ocaml_beginners"::[] ocamlopt -p & gprof

by Richard Jones-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Oct 31, 2007 at 02:24:28PM -0700, Onur Ozyer wrote:

> I want to use gprof to profile the perf.
>
>
> I add the "-p" to ocamlopt, where the sources aer
> compiled and ML library is linked. "ocamlopt" works
> succesfully.
>
> However, at the last step, where the executables are
> linked, I'm having this error:
>
>     Cannot find file std_exit.p.cmx
>
> I don't have a file with "*std_exit*" Does anyone know
> why it coud be happening ?

Sounds like a packaging problem.  What operating system / distribution
/ etc?

Rich.

--
Richard Jones
Red Hat

Re: "ocaml_beginners"::[] ocamlopt -p & gprof

by Onur Ozyer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I tried this within Cygwin on Windows 2003.
Ocaml is 3.07+2

Onur
--- Richard Jones <rich@...> wrote:

> On Wed, Oct 31, 2007 at 02:24:28PM -0700, Onur Ozyer
> wrote:
> > I want to use gprof to profile the perf.
> >
> >
> > I add the "-p" to ocamlopt, where the sources aer
> > compiled and ML library is linked. "ocamlopt"
> works
> > succesfully.
> >
> > However, at the last step, where the executables
> are
> > linked, I'm having this error:
> >
> >     Cannot find file std_exit.p.cmx
> >
> > I don't have a file with "*std_exit*" Does anyone
> know
> > why it coud be happening ?
>
> Sounds like a packaging problem.  What operating
> system / distribution
> / etc?
>
> Rich.
>
> --
> Richard Jones
> Red Hat
>


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 

Re: "ocaml_beginners"::[] ocamlopt -p & gprof

by Onur Ozyer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In ocamlopt documentation says, the -p switch is only
supported in UNIX. I'm not sure it rules out Cygwin.

Besides ocamlopt -p, adoes anyanyone have experience
with Ocaml profilers to monitor the execution time and
memory usage on Windows (with or without Cygwin) ?
Any help is appreciated : ),

Onur


--- Onur Ozyer <onurozyer@...> wrote:

> I tried this within Cygwin on Windows 2003.
> Ocaml is 3.07+2
>
> Onur
> --- Richard Jones <rich@...> wrote:
>
> > On Wed, Oct 31, 2007 at 02:24:28PM -0700, Onur
> Ozyer
> > wrote:
> > > I want to use gprof to profile the perf.
> > >
> > >
> > > I add the "-p" to ocamlopt, where the sources
> aer
> > > compiled and ML library is linked. "ocamlopt"
> > works
> > > succesfully.
> > >
> > > However, at the last step, where the executables
> > are
> > > linked, I'm having this error:
> > >
> > >     Cannot find file std_exit.p.cmx
> > >
> > > I don't have a file with "*std_exit*" Does
> anyone
> > know
> > > why it coud be happening ?
> >
> > Sounds like a packaging problem.  What operating
> > system / distribution
> > / etc?
> >
> > Rich.
> >
> > --
> > Richard Jones
> > Red Hat
> >
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam
> protection around
> http://mail.yahoo.com 
>


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com