On Sun, Feb 27, 2022 at 6:07 PM Douglas McIlroy <
douglas.mcilroy(a)dartmouth.edu> wrote:
The X11 tree
was a heavily ifdef-ed. And it needed to be, I don't have
an answer as to how you would reuse all that code on different hardware
in a better way.
Plan 9 did it with #include. The name of the included file was the same for
every architecture. Only the search path for include files changed. Done
with
care, this eliminates the typical upfront #ifdefs.that define constants
and set
flags.
The qemu project does this, for the most part. It makes things a lot
easier, but
does take some getting used to to find the proper file to be included. It
helps
a lot to keep the twisty maze of #ifdefs down to a dull roar.
FreeBSD (and all the BSDs) has a similar philosophy. You define the page
size,
for example, of the target, and the rest of the system is written to that
abstraction
rather than having #ifdefs in a lot of places. NetBSD is much better about
keeping
the total number of #ifdefs out of MI code than FreeBSD. In both cases, most
of the #ifdefs are basically #ifdef __HAS_FUNKY_THING <do funky thing here>
#endif that are needed on a couple of architectures, though usually that
idiom
is expressed by a macro (that's empty for most platforms) or an inline
function.
Other preprocessor conditionals can usually be
replaced by a regular if,
letting
the compiler optimize away the unwanted alternative. This makes
conditionals
obey the scope rules of C.
As long as they are always defined... :) Though older gcc/clang compilers
get cranky
when they view the if expression as a tautology (warning about the idiom
rather than
encouraging it: stupid clang tricks).
Warner