Beispiel #1
0
def jit_compile(ccode, compiler=GNUCompiler):
    """JIT compile the given ccode.

    :param ccode: String of C source code.
    :param compiler: The toolchain used for compilation. GNUCompiler by default.

    :return: The name of the compilation unit.
    """

    hash_key = sha1(str(ccode).encode()).hexdigest()
    basename = path.join(get_tmp_dir(), hash_key)

    src_file = "%s.%s" % (basename, compiler.src_ext)
    if platform == "linux" or platform == "linux2":
        lib_file = "%s.so" % basename
    elif platform == "darwin":
        lib_file = "%s.dylib" % basename
    elif platform == "win32" or platform == "win64":
        lib_file = "%s.dll" % basename

    tic = time()
    extension_file_from_string(toolchain=compiler, ext_file=lib_file,
                               source_string=ccode, source_name=src_file)
    toc = time()
    log("%s: compiled %s [%.2f s]" % (compiler, src_file, toc-tic))

    return basename
Beispiel #2
0
def jit_compile(ccode, basename, compiler=GNUCompiler):
    """JIT compiles the given ccode and returns the lib filepath.

    :param ccode: String of C source code.
    :param basename: The string used to name various files for this compilation.
    :param compiler: The toolchain used for compilation. GNUCompiler by default.
    :return: Path to compiled lib
    """
    src_file = "%s.cpp" % basename
    lib_file = "%s.so" % basename
    log("%s: Compiling %s" % (compiler, src_file))
    extension_file_from_string(toolchain=compiler,
                               ext_file=lib_file,
                               source_string=ccode,
                               source_name=src_file)

    return lib_file