Skip to main content

wwyt: A conditional compilation pre-processor

Please note these forums are being decommissioned and use the new and improved forums at
No replies
Joined: 2011-02-01

I'm new to Java, as a long-time C++ programmer, one of the problems I have to tackle is conditional compilation. Java doesn't support (as far as I know), so I searched up and down for a solution. I did find a few, but unfortunately, none of them fit my needs perfectly. So I decided to do one myself. Thiniking that this tool might be helpful for others, so I enhanced it a little to make it more suitable for use by the public, created a web page for it, and publish it as a freeware. So, here it is:
This tool, called wwyt, is a pre-processor that converts Java source files that contain conditional compilation directives (defaults are #if, #else, #elif, and #endif. But just in case these names conflict with other tools, like documentation tools or whatever, wwyt allows you to choose from a few pre-defined sets of names as an alternative.) wwyt will process these directives and create a new file. After compilation, you should use wwyt again to restore the converted files back to their original state. Here is an example of a file embedded with wwyt directives:
public String getVersionName()
String name;

/* #if RELEASE && VER1
name = "Standard Release R1";
#elif RELEASE && VER2
name = "Standard Release R2";
#elif RELEASE && !(VER2 || VER2)
name = "Standard Release R3";
#else */
name = "Internal Debug Build";
// #endif

return name;
Use this command line to pre-process this file (note that the symbols RELEASE and VER2 are defined in the command line):
>wwyt prepro -s RELEASE VER2
A new file will be created, and it will contain statements like following:
public String getVersionName()
String name;

name = "Standard Release R2";

return name;

As can be seen, any line containing wwyt directives will be replaced by a blank line. Only the lines in a block where the Boolean expression evaluates to a value of true will remain in the resulting file. Now you can feed this file to the compiler for compilation. After compiling, you will need to use wwyt again. This time, wwyt will convert the file back to its original content:
>wwyt restore -s
wwyt is a Windows command line tool, I use it with Eclipse and Ant. I always put it in a batch file like this:
>wwyt prepro -s src RELEASE VERSION1
>call ant release
>wwyt restore -s src

OK, for a brief introduction, I guess this is it.. If you are interested, you can download directly from here (this is an exe file). Or, you can visit for more details.

Hope this helps. Enjoy.