Blame numpy/distutils/command/autodist.py

Packit 7a8e5e
"""This module implements additional tests ala autoconf which can be useful.
Packit 7a8e5e
Packit 7a8e5e
"""
Packit 7a8e5e
from __future__ import division, absolute_import, print_function
Packit 7a8e5e
Packit 7a8e5e
Packit 7a8e5e
# We put them here since they could be easily reused outside numpy.distutils
Packit 7a8e5e
Packit 7a8e5e
def check_inline(cmd):
Packit 7a8e5e
    """Return the inline identifier (may be empty)."""
Packit 7a8e5e
    cmd._check_compiler()
Packit 7a8e5e
    body = """
Packit 7a8e5e
#ifndef __cplusplus
Packit 7a8e5e
static %(inline)s int static_func (void)
Packit 7a8e5e
{
Packit 7a8e5e
    return 0;
Packit 7a8e5e
}
Packit 7a8e5e
%(inline)s int nostatic_func (void)
Packit 7a8e5e
{
Packit 7a8e5e
    return 0;
Packit 7a8e5e
}
Packit 7a8e5e
#endif"""
Packit 7a8e5e
Packit 7a8e5e
    for kw in ['inline', '__inline__', '__inline']:
Packit 7a8e5e
        st = cmd.try_compile(body % {'inline': kw}, None, None)
Packit 7a8e5e
        if st:
Packit 7a8e5e
            return kw
Packit 7a8e5e
Packit 7a8e5e
    return ''
Packit 7a8e5e
Packit 7a8e5e
def check_restrict(cmd):
Packit 7a8e5e
    """Return the restrict identifier (may be empty)."""
Packit 7a8e5e
    cmd._check_compiler()
Packit 7a8e5e
    body = """
Packit 7a8e5e
static int static_func (char * %(restrict)s a)
Packit 7a8e5e
{
Packit 7a8e5e
    return 0;
Packit 7a8e5e
}
Packit 7a8e5e
"""
Packit 7a8e5e
Packit 7a8e5e
    for kw in ['restrict', '__restrict__', '__restrict']:
Packit 7a8e5e
        st = cmd.try_compile(body % {'restrict': kw}, None, None)
Packit 7a8e5e
        if st:
Packit 7a8e5e
            return kw
Packit 7a8e5e
Packit 7a8e5e
    return ''
Packit 7a8e5e
Packit 7a8e5e
def check_compiler_gcc4(cmd):
Packit 7a8e5e
    """Return True if the C compiler is GCC 4.x."""
Packit 7a8e5e
    cmd._check_compiler()
Packit 7a8e5e
    body = """
Packit 7a8e5e
int
Packit 7a8e5e
main()
Packit 7a8e5e
{
Packit 7a8e5e
#if (! defined __GNUC__) || (__GNUC__ < 4)
Packit 7a8e5e
#error gcc >= 4 required
Packit 7a8e5e
#endif
Packit 7a8e5e
    return 0;
Packit 7a8e5e
}
Packit 7a8e5e
"""
Packit 7a8e5e
    return cmd.try_compile(body, None, None)
Packit 7a8e5e
Packit 7a8e5e
Packit 7a8e5e
def check_gcc_function_attribute(cmd, attribute, name):
Packit 7a8e5e
    """Return True if the given function attribute is supported."""
Packit 7a8e5e
    cmd._check_compiler()
Packit 7a8e5e
    body = """
Packit 7a8e5e
#pragma GCC diagnostic error "-Wattributes"
Packit 7a8e5e
#pragma clang diagnostic error "-Wattributes"
Packit 7a8e5e
Packit 7a8e5e
int %s %s(void*);
Packit 7a8e5e
Packit 7a8e5e
int
Packit 7a8e5e
main()
Packit 7a8e5e
{
Packit 7a8e5e
    return 0;
Packit 7a8e5e
}
Packit 7a8e5e
""" % (attribute, name)
Packit 7a8e5e
    return cmd.try_compile(body, None, None) != 0
Packit 7a8e5e
Packit 7a8e5e
def check_gcc_variable_attribute(cmd, attribute):
Packit 7a8e5e
    """Return True if the given variable attribute is supported."""
Packit 7a8e5e
    cmd._check_compiler()
Packit 7a8e5e
    body = """
Packit 7a8e5e
#pragma GCC diagnostic error "-Wattributes"
Packit 7a8e5e
#pragma clang diagnostic error "-Wattributes"
Packit 7a8e5e
Packit 7a8e5e
int %s foo;
Packit 7a8e5e
Packit 7a8e5e
int
Packit 7a8e5e
main()
Packit 7a8e5e
{
Packit 7a8e5e
    return 0;
Packit 7a8e5e
}
Packit 7a8e5e
""" % (attribute, )
Packit 7a8e5e
    return cmd.try_compile(body, None, None) != 0