Separate specific #ifdef branches

Posted by detly on Stack Overflow See other posts from Stack Overflow or by detly
Published on 2010-05-07T03:24:52Z Indexed on 2010/05/07 3:28 UTC
Read the original article Hit count: 283

Filed under:
|

In short: I want to generate two different source trees from the current one, based only on one preprocessor macro being defined and another being undefined, with no other changes to the source.

If you are interested, here is my story...

In the beginning, my code was clean. Then we made a new product, and yea, it was better. But the code saw only the same peripheral devices, so we could keep the same code.

Well, almost.

There was one little condition that needed to be changed, so I added:

#if defined(PRODUCT_A)
condition = checkCat();
#elif defined(PRODUCT_B)
condition = checkCat() && checkHat();
#endif

...to one and only one source file. In the general all-source-files-include-this header file, I had:

#if !(defined(PRODUCT_A)||defined(PRODUCT_B))
#error "Don't make me replace you with a small shell script. RTFM."
#endif

...so that people couldn't compile it unless they explicitly defined a product type.

All was well. Oh... except that modifications were made, components changed, and since the new hardware worked better we could significantly re-write the control systems. Now when I look upon the face of the code, there are more than 60 separate areas delineated by either:

#ifdef PRODUCT_A
...
#else
...
#endif

...or the same, but for PRODUCT_B. Or even:

#if defined(PRODUCT_A)
...
#elif defined(PRODUCT_B)
...
#endif

And of course, sometimes sanity took a longer holiday and:

#ifdef PRODUCT_A
...
#endif
#ifdef PRODUCT_B
...
#endif

These conditions wrap anywhere from one to two hundred lines (you'd think that the last one could be done by switching header files, but the function names need to be the same).

This is insane. I would be better off maintaining two separate product-based branches in the source repo and porting any common changes. I realise this now.

Is there something that can generate the two different source trees I need, based only on PRODUCT_A being defined and PRODUCT_B being undefined (and vice-versa), without touching anything else (ie. no header inclusion, no macro expansion, etc)?

© Stack Overflow or respective owner

Related posts about c

    Related posts about preprocessor