Mark Beaton
Mark Beaton

Reputation: 2677

Does a C# preprocessing tool exist?

Does anyone know of a utility to preprocess a C# source file without compiling it, in a similar fashion to using the -E flag in GCC? I tried using GCC - it successfully processes #if directives, but it chokes on any #region directives...

Ideally, I'd like to be able to run the tool over a .cs file to remove any #if blocks that evaluate to false, and optionally be able to flag whether to remove/leave intact comments, #region, #pragma directives etc.

To put this in context, I want to be able to a publish some source code that is part of a (much) larger project, whilst removing portions that are only relevant to the larger project. As an example, there are chunks of code that look like this:

#if (SUBPROJECT)
namespace SubProject
#else
namespace CompleteProject
#endif
{
  public class SomeClass()
  {
#if (!SUBPROJECT)
    // This might call a method contained in an assembly that potentially 
    // won't be available to the sub-project, or maybe a method that hooks
    // into a C library via P/Invoke...
    string result = CallSomeCleverMethod();
#else
    // This might call a method that performs a simplified version of the 
    // above, but does so without the above's assembly or P/Invoke 
    // dependencies...
    string result = CallSomeSimpleMethod();
#endif
  }
}

Please remember I'm not trying to do builds here - this is purely about publishing only a subset of a larger project's source code.

Any ideas/help appreciated.

Upvotes: 7

Views: 2395

Answers (5)

gaDZella
gaDZella

Reputation: 71

I faced this some time ago - had to clean up certain outdated C# preprocessor branches in my old project leaving relevant branches intact. The built-it C preprocessor could not do it for me as its output file never contains preprocessor directives. Of course, there are C processor tools such as unifdef or coan that can partially remove outdated branches for C/С++. The thing is that they do not fully support С# syntax - they fail on #region and #pragma, for instance. So, I had to write my own python tool undefine for my task. I used regexp for parsing directives and the sympy library - for simplifying and calculating logical expressions. To resolve your task try the following:

>>python undefine apply -d SUBPROJECT your_project_path

The tool worked fine with my large ~10m lines codebase.

Upvotes: 1

Mark Beaton
Mark Beaton

Reputation: 2677

It turns out using the GNU C preprocessor (cpp) directly (rather than via gcc) provides more control over the output. Using this, I was able to process the source files in a suitable fashion...

Here's an example:

cpp -C -P -DSUBPROJECT Input.cs Output.cs

I tried to get the C/C++ compiler provided with Visual Studio to do something similar, but gave up after a while...

Upvotes: 5

Jack Bolding
Jack Bolding

Reputation: 3829

I do not believe that there are any flags for the csc or msc (mono compiler) to output the preprocessed files.

If I had to do this, and it was a small subset I would compile and then use Reflector to decompile back to C# and export the files. If you have a lot of files, there is an Reflector Add-In called FileGenerator for Reflector.

Unfortunately this will not be a true copy of your codebase -- it will reformat all of the code and you will lose all of the comments.

Upvotes: 3

ichiban
ichiban

Reputation: 6210

I'm not aware of a preprocessor for C# that would allow you to export a subset of your source code without building it.

However, You should be able to achieve the same effect by using a code generation template. Just change to the preprocesor directives to the appropriate template equivalent.

Visual Studio 2008 has a built-in code generation tool called T4. There are other third-party options like CodesmithTools or the free MyGeneration.Net

Upvotes: 2

Brian Sullivan
Brian Sullivan

Reputation: 28623

Might it not be easier, and more idiomatically C#, to create interfaces, and have different implementations of those interfaces? One set that you can package up a as a library and redistribute?

Upvotes: 1

Related Questions