Exemple #1
0
def CHECK_CODE(conf,
               code,
               define,
               always=False,
               execute=False,
               addmain=True,
               add_headers=True,
               mandatory=False,
               headers=None,
               msg=None,
               cflags='',
               includes='# .',
               local_include=True,
               lib=None,
               link=True,
               define_ret=False,
               quote=False,
               on_target=True):
    '''check if some code compiles and/or runs'''

    if CONFIG_SET(conf, define):
        return True

    if headers is not None:
        CHECK_HEADERS(conf, headers=headers, lib=lib)

    if add_headers:
        hdrs = header_list(conf, headers=headers, lib=lib)
    else:
        hdrs = ''
    if execute:
        execute = 1
    else:
        execute = 0

    defs = conf.get_config_header()

    if addmain:
        fragment = '%s\n%s\n int main(void) { %s; return 0; }\n' % (defs, hdrs,
                                                                    code)
    else:
        fragment = '%s\n%s\n%s\n' % (defs, hdrs, code)

    if msg is None:
        msg = "Checking for %s" % define

    cflags = TO_LIST(cflags)

    if local_include:
        cflags.append('-I%s' % conf.curdir)

    if not link:
        type = 'nolink'
    else:
        type = 'cprogram'

    uselib = TO_LIST(lib)

    (ccflags, ldflags, cpppath) = library_flags(conf, uselib)

    includes = TO_LIST(includes)
    includes.extend(cpppath)

    uselib = [l.upper() for l in uselib]

    cflags.extend(ccflags)

    if on_target:
        exec_args = conf.SAMBA_CROSS_ARGS(msg=msg)
    else:
        exec_args = []

    conf.COMPOUND_START(msg)

    ret = conf.check(fragment=fragment,
                     execute=execute,
                     define_name=define,
                     mandatory=mandatory,
                     ccflags=cflags,
                     ldflags=ldflags,
                     includes=includes,
                     uselib=uselib,
                     type=type,
                     msg=msg,
                     quote=quote,
                     exec_args=exec_args,
                     define_ret=define_ret)
    if not ret and CONFIG_SET(conf, define):
        # sometimes conf.check() returns false, but it
        # sets the define. Maybe a waf bug?
        ret = True
    if ret:
        if not define_ret:
            conf.DEFINE(define, 1)
            conf.COMPOUND_END(True)
        else:
            conf.COMPOUND_END(conf.env[define])
        return True
    if always:
        conf.DEFINE(define, 0)
    conf.COMPOUND_END(False)
    return False
Exemple #2
0
def CHECK_LIBRARY_SUPPORT(conf, rpath=False, version_script=False, msg=None):
    '''see if the platform supports building libraries'''

    if msg is None:
        if rpath:
            msg = "rpath library support"
        else:
            msg = "building library support"

    dir = find_config_dir(conf)

    bdir = os.path.join(dir, 'testbuild')
    if not os.path.exists(bdir):
        os.makedirs(bdir)

    env = conf.env

    subdir = os.path.join(dir, "libdir")

    os.makedirs(subdir)

    dest = open(os.path.join(subdir, 'lib1.c'), 'w')
    dest.write('int lib_func(void) { return 42; }\n')
    dest.close()

    dest = open(os.path.join(dir, 'main.c'), 'w')
    dest.write('int main(void) {return !(lib_func() == 42);}\n')
    dest.close()

    bld = Build.BuildContext()
    bld.log = conf.log
    bld.all_envs.update(conf.all_envs)
    bld.all_envs['default'] = env
    bld.lst_variants = bld.all_envs.keys()
    bld.load_dirs(dir, bdir)

    bld.rescan(bld.srcnode)

    ldflags = []
    if version_script:
        ldflags.append("-Wl,--version-script=%s/vscript" % bld.path.abspath())
        dest = open(os.path.join(dir,'vscript'), 'w')
        dest.write('TEST_1.0A2 { global: *; };\n')
        dest.close()

    bld(features='cc cshlib',
        source='libdir/lib1.c',
        target='libdir/lib1',
        ldflags=ldflags,
        name='lib1')

    o = bld(features='cc cprogram',
            source='main.c',
            target='prog1',
            uselib_local='lib1')

    if rpath:
        o.rpath=os.path.join(bdir, 'default/libdir')

    # compile the program
    try:
        bld.compile()
    except:
        conf.check_message(msg, '', False)
        return False

    # path for execution
    lastprog = o.link_task.outputs[0].abspath(env)

    if not rpath:
        if 'LD_LIBRARY_PATH' in os.environ:
            old_ld_library_path = os.environ['LD_LIBRARY_PATH']
        else:
            old_ld_library_path = None
        ADD_LD_LIBRARY_PATH(os.path.join(bdir, 'default/libdir'))

    # we need to run the program, try to get its result
    args = conf.SAMBA_CROSS_ARGS(msg=msg)
    proc = Utils.pproc.Popen([lastprog] + args, stdout=Utils.pproc.PIPE, stderr=Utils.pproc.PIPE)
    (out, err) = proc.communicate()
    w = conf.log.write
    w(str(out))
    w('\n')
    w(str(err))
    w('\nreturncode %r\n' % proc.returncode)
    ret = (proc.returncode == 0)

    if not rpath:
        os.environ['LD_LIBRARY_PATH'] = old_ld_library_path or ''

    conf.check_message(msg, '', ret)
    return ret