[Python-Dev] To -I(nclude) or not to -I(nclude), that is the question...

Skip Montanaro skip@pobox.com
Fri, 18 Oct 2002 17:02:44 -0500


In installing mxBase 2.0.4 on my MacOS 10.2.1 system I get warnings like the
following:

    gcc -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -Wno-long-double -no-cpp-precomp -Imx/Queue/mxQueue -I/Users/skip/local/include/python2.3 -I/usr/include -I/usr/local/include -c mx/Queue/mxQueue/mxQueue.c -o build/temp.darwin-6.1-Power Macintosh-2.3/mx/Queue/mxQueue/mxQueue/mxQueue.o
    cc1: warning: changing search order for system directory "/usr/local/include"
    cc1: warning:   as it has already been specified as a non-system directory
    cc1: warning: changing search order for system directory "/usr/include"
    cc1: warning:   as it has already been specified as a non-system directory

This warning bothers me a bit, as it suggests I'm screwing up the compiler's
notions about header file search order.  Has anyone else seen this and
investigated how to get rid of this problem?  This is related to bug
http://python.org/sf/589427 (which was assigned to me).  It's due to the
gen_preprocess_options() function in distutils/ccompiler.py.  This warning
seems to be related to gcc version >= 3.1.

I have a quick hack in my local copy of distutils/ccompiler.py.  At the
bottom of gen_preprocess_options() I replaced

    for dir in include_dirs:
        pp_opts.append ("-I%s" % dir)

with 

    pp_opts.extend (gen_preprocess_includes(include_dirs))

and added these two functions to the file:

    def gen_preprocess_includes_macosx_gcc(dirs):
        """GCC on MacOSX complains if /usr/include or /usr/local/include are
        mentioned in -I.
        """
        pp_opts = []
        for dir in dirs:
            if dir not in ("/usr/include", "/usr/local/include"):
                pp_opts.append ("-I%s" % dir)
        return pp_opts

    def gen_preprocess_includes(dirs):
        """Generate the -I flags for a compile command."""
        if sys.platform == "darwin":
            return gen_preprocess_includes_macosx_gcc(dirs)

        pp_opts = []
        for dir in dirs:
            pp_opts.append ("-I%s" % dir)
        return pp_opts

This is an obscure solution, at best.  I'd prefer to at least test the
compiler and version.  How can I tell what compiler and version will be used
to compile files, or can't I can this level?  (Seems to me that
gen_preprocess_options begs to be a method of the CCompiler class.)

Thx,

Skip