Compiler subroutine to test whether some functions are available
    on the target system. Since the rrdtool headers shipped with most
    packages do not disclose any versioning information, we cannot test
    whether a given function is available that way. Instead, use this to
    manually try to compile code and see if it works.

    Taken from http://stackoverflow.com/questions/28843765/setup-py-check-if-non-python-library-dependency-exists.

def compile_extensions(macros, compat=False):
    """
    Compiler subroutine to test whether some functions are available
    on the target system. Since the rrdtool headers shipped with most
    packages do not disclose any versioning information, we cannot test
    whether a given function is available that way. Instead, use this to
    manually try to compile code and see if it works.

    Taken from http://stackoverflow.com/questions/28843765/setup-py-check-if-non-python-library-dependency-exists.
    """
    import distutils.sysconfig
    import distutils.ccompiler
    import tempfile
    import shutil

    from textwrap import dedent

    # common vars
    libraries = ['rrd']
    include_dirs = [package_dir, '/usr/local/include']
    library_dirs = ['/usr/local/lib']
    compiler_args = dict(
        libraries=libraries,
        include_dirs=include_dirs,
        library_dirs=library_dirs,
        define_macros=macros)
    exts = [Extension('rrdtool', sources=['rrdtoolmodule.c'], **compiler_args)]

    if compat:
        return exts

    # in non-compat mode, try to link to check if the new symbols are present in librrd
    c_code = dedent('''
        #include <rrd.h>
        #include "rrdtoolmodule.h"

        int main(int argc, char *argv[]) {
            rrd_fetch_cb_register(NULL);  /* exists in rrdtool >= 1.5.0 */
            return 0;
        }
    ''')

    tmp_dir = tempfile.mkdtemp(prefix='tmp_python_rrdtool')
    bin_file_name = os.path.join(tmp_dir, 'test_rrdtool')
    file_name = bin_file_name + '.c'

    with open(file_name, 'w') as fp:
        fp.write(c_code)

    # try to compile it
    compiler = distutils.ccompiler.new_compiler()
    assert isinstance(compiler, distutils.ccompiler.CCompiler)
    for s in include_dirs:
        compiler.add_include_dir(s)
    for s in library_dirs:
        compiler.add_library_dir(s)
    for s in libraries:
        compiler.add_library(s)
    for s in macros:
        compiler.define_macro(*s)
    distutils.sysconfig.customize_compiler(compiler)

    try:
        compiler.link_executable(
            compiler.compile([file_name]),
            bin_file_name,
            libraries=libraries)
    except CompileError:
        sys.exit('Error: Unable to compile the binary module. Do you have the rrdtool header and libraries installed?')
        ret = None
    except LinkError as exc:
        shutil.rmtree(tmp_dir)
        raise  # re-raise
    else:
        return exts  # seems to be available, compile in regular way

    shutil.rmtree(tmp_dir)
    return ret