3

I would like to condense our linux driver code in to only the code that runs on the current kernel. It has parts that are ignored by if statements all the way back to kernele 2.4.x

Have you ever heard of a way to compile the code to an output which will be the working code with out all the stuff ignored by the c compiler if else statements?

I am wondering if we can run make something or gcc something that will simply result in all the code that is used for that build.

So like if I had this .c file below, then after running the make command I should have just the code I need for the newest kernel.

example.c

static somefunction .... {
    avar = 0;
#if (linux_ver >= 2.6.31)
    some newer code
#elseif (linux_ver >= 2.4.24)
    some older code
#else
    original code
#endif

}

after extracting / condensing, example.c would simply read as below

static somefunction .... {
    avar = 0;
    some newer code
}
ndasusers
  • 727
  • 6
  • 12
  • 1
    possible duplicate of [Is there a C pre-processor which eliminates #ifdef blocks based on values defined/undefined?](http://stackoverflow.com/questions/525283/is-there-a-c-pre-processor-which-eliminates-ifdef-blocks-based-on-values-define) – Jonathan Leffler Feb 14 '12 at 05:29

2 Answers2

2

The tool you are after is sunifdef or (more recent) coan.

See also: Is there a C pre-processor which eliminates #ifdef blocks based on values defined or undefined?

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

That's what the preprocessor directives do already. Try running the code through gcc -E (but prepare for a lot of output, as all #includes will be inlined).

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • OP would probably like to avoid getting include files pasted in, hundreds of blank lines added, etc. and just have certain `#if` directives collapsed down. I believe there's a tool for this but I don't know the name... – R.. GitHub STOP HELPING ICE Feb 14 '12 at 05:30