Porting style [was Re: GPG port to TANDEM]

Marcus Brinkmann Marcus.Brinkmann at ruhr-uni-bochum.de
Tue Jan 14 23:39:07 CET 2003


On Tue, Jan 14, 2003 at 10:57:35AM -0600, David Champion wrote:
> * On 2003.01.14, in <87el7gcalr.fsf at alberti.g10code.de>,
> *	"Werner Koch" <wk at gnupg.org> wrote:
> > 
> > We don't accept changes which render the code unreadable; especially
> > #ifdef __some_ostype__ are in general not acceptable.  There are
> 
> How should it be done? I would have expected the alternatives to be
> less likely to be accepted, so I'm not sure what approach to take. All
> hypothetical, but as a open-source contributor with far more patches
> rejected than accepted, this kind of thing intrigues me.

Few things are specific to a certain CPU or operating system.  Even for
those it is often possible that other operating systems will feature this at
some later time.  So the best way to go about something is to think
feature-based.  Features of an operating system can often be checked
automatically by autoconf, and this ensures that all systems with this
feature can benefit, while others are unaffected.

One simple example is the endianess of a system.  You wouldn't want to
write:

#if defined(__i386__) || ...
...
#else
...
#endif

for endian specific code, and then add to the list each time a new processor
is built or ported to.  Likewise, you want to avoid listing specific
systems anywhere.  The only exception can be if the code absolutely can only
run on a very specific list of systems without exception, even without
future exceptions (in big likelyhood).

So, the not-so-concrete answer is: Find out which features you rely on, and
then use macros to test for a features presence:

#if HAVE_OBSCURE_HELPER_LIBRARY
...
#endif

and then define HAVE_OBSCURE_HELPER_LIBRARY in config.h (via configure) if
the library is found on the system.  Even in configure switch/case
instructions to test for features based on the operating system is
discouraged.  Usually a real check for a features presence can be written.
Good examples in GnuPG:

AC_CHECK_FUNC(gethostbyname, , AC_CHECK_LIB(nsl, gethostbyname,
        [NETLIBS="-lnsl $NETLIBS"]))
AC_CHECK_FUNC(setsockopt, , AC_CHECK_LIB(socket, setsockopt,
        [NETLIBS="-lsocket $NETLIBS"]))

and the typedef/sizeof checks.

Thanks,
Marcus




-- 
`Rhubarb is no Egyptian god.' GNU      http://www.gnu.org    marcus at gnu.org
Marcus Brinkmann              The Hurd http://www.gnu.org/software/hurd/
Marcus.Brinkmann at ruhr-uni-bochum.de
http://www.marcus-brinkmann.de/




More information about the Gnupg-devel mailing list