# IFDEF conditional compilation and ocamlbuild

**URL:** <https://discuss.ocaml.org/t/ifdef-conditional-compilation-and-ocamlbuild/3244>\
**Category:** Community\
**Created:** [January 29, 2019, 7:42am UTC](https://discuss.ocaml.org/t/ifdef-conditional-compilation-and-ocamlbuild/3244 "2019-01-29T07:42:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![luc.bouge](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@luc.bouge](https://discuss.ocaml.org/u/luc.bouge)\
**Post date:** [January 29, 2019, 7:42am UTC](https://discuss.ocaml.org/t/ifdef-conditional-compilation-and-ocamlbuild/3244/1 "2019-01-29T07:42:03Z")

</div>

Hello,

I am developing a program in Ocaml for my Programming class.This idea is to provide the students with various versions of the program, using more and more complex techniques.

I would like to design a common master, and then derive the various versions automatically using some conditional compilation facility.

I considered [camlp4 IFDEF](http://caml.inria.fr/pub/docs/manual-camlp4/manual002.html) but it applies to complete expressions, only. Instead, I would like to add or remove cases in long lists of `if ... then ... else if ...`.

Also, I am using `ocamlbuild` and I cannot find how to define `IFDEF` compilation tags in the compilation chain.

Is there any IFDEF-wizard in the community? 🕶

Best regards,

Luc.

---

<div class="post-metadata">

**Author:** ![XVilka](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/xvilka/32/5334_2.png) [@XVilka](https://discuss.ocaml.org/u/XVilka)\
**Post date:** [January 29, 2019, 9:02am UTC](https://discuss.ocaml.org/t/ifdef-conditional-compilation-and-ocamlbuild/3244/2 "2019-01-29T09:02:16Z")

</div>

See [https://github.com/ocaml-community/cppo](https://github.com/ocaml-community/cppo) + [https://jbuilder.readthedocs.io/en/latest/quick-start.html#using-cppo](https://jbuilder.readthedocs.io/en/latest/quick-start.html#using-cppo) (for dune, since using plain ocamlbuild is “an old way of doing things”).

---

<div class="post-metadata">

**Author:** ![XVilka](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/xvilka/32/5334_2.png) [@XVilka](https://discuss.ocaml.org/u/XVilka)\
**Post date:** [January 29, 2019, 9:06am UTC](https://discuss.ocaml.org/t/ifdef-conditional-compilation-and-ocamlbuild/3244/3 "2019-01-29T09:06:32Z")

</div>

Note there is also [https://github.com/janestreet/ppx\_optcomp](https://github.com/janestreet/ppx_optcomp) for using only extension points (PPX)

---

<div class="post-metadata">

**Author:** ![luc.bouge](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@luc.bouge](https://discuss.ocaml.org/u/luc.bouge)\
**Post date:** [January 29, 2019, 9:19am UTC](https://discuss.ocaml.org/t/ifdef-conditional-compilation-and-ocamlbuild/3244/4 "2019-01-29T09:19:39Z")

</div>

Excellent! Let me try as soon as possible. Luc.

---

<div class="post-metadata">

**Author:** ![luc.bouge](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@luc.bouge](https://discuss.ocaml.org/u/luc.bouge)\
**Post date:** [February 5, 2019, 6:23pm UTC](https://discuss.ocaml.org/t/ifdef-conditional-compilation-and-ocamlbuild/3244/5 "2019-02-05T18:23:47Z")

</div>

I confirm: just perfect. Thanks!

I have another question. Is it possible to use ocamlformat on the code equipped with `#ifdef` directives?

I was careful to use them in a structured way, so that formatting the code obtained by ignoring the directives makes sense.

Regards, Luc.

---

<div class="post-metadata">

**Author:** ![XVilka](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/xvilka/32/5334_2.png) [@XVilka](https://discuss.ocaml.org/u/XVilka)\
**Post date:** [February 6, 2019, 4:15am UTC](https://discuss.ocaml.org/t/ifdef-conditional-compilation-and-ocamlbuild/3244/6 "2019-02-06T04:15:03Z")

</div>

From my experience ocamlformat is not perfect yet, but I would just try and check the results in your case.

---

<div class="post-metadata">

**Author:** ![jjb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jjb/32/2713_2.png) [@jjb](https://discuss.ocaml.org/u/jjb)\
**Post date:** [February 6, 2019, 10:36am UTC](https://discuss.ocaml.org/t/ifdef-conditional-compilation-and-ocamlbuild/3244/7 "2019-02-06T10:36:15Z")

</div>

If you do conditional compilation via ppx, as ppx\_optcomp does, then ocamlformat has a chance. With other approaches where the source file does not parse using the standard compiler, ocamlformat will not work.

---

<div class="post-metadata">

**Author:** ![luc.bouge](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@luc.bouge](https://discuss.ocaml.org/u/luc.bouge)\
**Post date:** [February 9, 2019, 6:25pm UTC](https://discuss.ocaml.org/t/ifdef-conditional-compilation-and-ocamlbuild/3244/8 "2019-02-09T18:25:40Z")

</div>

Dear JJB,

I use cppo for conditional compilation. I am not acquainted enough: does it use ppx underneath?

Anyway, I tried with a test file, and it failed.

```
$ opam exec ocamlformat -- --inplace --profile=janestreet --debug test.ml
ocamlformat: ignoring "test.ml" (syntax error)
File "test.ml", line 130, characters 1-5:
Error: Syntax error

```

where line 130 is `#else`.

---

<div class="post-metadata">

**Author:** ![jjb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jjb/32/2713_2.png) [@jjb](https://discuss.ocaml.org/u/jjb)\
**Post date:** [February 9, 2019, 8:18pm UTC](https://discuss.ocaml.org/t/ifdef-conditional-compilation-and-ocamlbuild/3244/9 "2019-02-09T20:18:33Z")

</div>

I don’t know cppo in any detail, but that error message makes me suspect  
that it is not implemented as a ppx transformation.

---

<div class="post-metadata">

**Author:** ![Khady](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/khady/32/469_2.png) [@Khady](https://discuss.ocaml.org/u/Khady)\
**Post date:** [February 10, 2019, 2:28am UTC](https://discuss.ocaml.org/t/ifdef-conditional-compilation-and-ocamlbuild/3244/10 "2019-02-10T02:28:17Z")

</div>

cppo is not built as a ppx. And is not compatible with tools such as ocamlformat.

---

<div class="post-metadata">

**Author:** ![XVilka](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/xvilka/32/5334_2.png) [@XVilka](https://discuss.ocaml.org/u/XVilka)\
**Post date:** [February 10, 2019, 4:29am UTC](https://discuss.ocaml.org/t/ifdef-conditional-compilation-and-ocamlbuild/3244/11 "2019-02-10T04:29:01Z")

</div>

As I mentioned already there is ppx\_optcomp, which should work with ocamlformat.
