示例#1
0
    def source_compile(self, src, compiler='gnu', flags='', opt_flags='-O3'):
        # Compile directly for a source code as string.
        # A signature file(*.pyf) is generated automatically.

        logger.debug('source code: {}'.format(src))

        import tempfile
        tmpfile = tempfile.NamedTemporaryFile(mode='w', suffix='.f90', delete=True)
        tmpfile.file.write(src)
        tmpfile.file.flush()

        compile_using_f2py(tmpfile.name, compiler, flags, opt_flags)

        mod_name = basename(tmpfile.name).split('.')[0]
        return get_module_from_file('/tmp/build/', mod_name, self.code_type)
示例#2
0
文件: build.py 项目: wbkifun/my_stuff
def check_and_build_f90_c(code_type, dpath, src_dir, **kwargs):
    if src_dir == "c_f2py":
        c_use_f2py = True

    else:
        c_use_f2py = False

        #
        # Path for include headers
        #
        py_base_dpath = dirname(dirname(sys.executable))
        for root, dnames, fnames in os.walk(py_base_dpath):
            if "pkgs" not in root.split("/"):
                for fname in fnames:
                    if fname == "Python.h":
                        inc_path_py = root
                    elif fname == "arrayobject.h":
                        inc_path_numpy = root

    build_dict = load_build_yaml(dpath)
    src_dir = code_type if src_dir == None else src_dir
    src_dpath = join(dpath, src_dir)
    build_dpath = join(dpath, src_dir, "build")
    os.chdir(build_dpath)

    obj_suffix = {"f90": "f90.so", "c": "c.so"}[code_type]
    env = build_dict[code_type]
    flags = "" if env["flags"] == None else env["flags"]
    opt_flags = "" if env["opt_flags"] == None else env["opt_flags"]

    for target in sorted(build_dict["target"]):
        so_fpath = join(build_dpath, target + ".so")

        #
        # Gather dependent file names
        #
        header_names = list()
        ext_names = list()
        gather_dependency(target, build_dict, header_names, ext_names)

        #
        # Compile dependencies
        #
        for ext_name in ext_names[::-1]:
            #
            # Check if revision
            #
            headers, deps = list(), list()
            gather_dependency(ext_name, build_dict, headers, deps)

            ext_fpath = join(src_dpath, ext_name + "." + code_type)
            o_fpath = join(build_dpath, ext_name + ".o")

            new_compile = False

            if not exists(o_fpath):
                new_compile = True

            else:
                if getmtime(ext_fpath) > getmtime(o_fpath):
                    new_compile = True

                for header in headers:
                    h_fpath = join(src_dpath, header + ".h")
                    if exists(h_fpath) and getmtime(h_fpath) > getmtime(o_fpath):
                        new_compile = True

                    h_fpath = join(build_dpath, header + ".h")
                    if exists(h_fpath) and getmtime(h_fpath) > getmtime(o_fpath):
                        new_compile = True

                for dep in deps:
                    dep_o_fpath = join(build_dpath, dep + ".o")
                    if exists(dep_o_fpath) and getmtime(dep_o_fpath) > getmtime(o_fpath):
                        new_compile = True

            #
            # Compile
            #
            if new_compile:
                if code_type == "f90":
                    compiler = {"gnu": "gfortran", "intel": "ifort"}[env["compiler"]]
                    cmd = "{} -fPIC {} {} -I. -c {}".format(compiler, opt_flags, flags, ext_fpath)

                elif code_type == "c":
                    compiler = {"gnu": "gcc", "intel": "icc"}[env["compiler"]]
                    cmd = "{} -fPIC {} {} -I. -c {}".format(compiler, opt_flags, flags, ext_fpath)

                print("[compile]", cmd.replace(ext_fpath, basename(ext_fpath)))
                execute(cmd)

            else:
                print("{} is up to date.".format(o_fpath))

        #
        # Check if revision
        #
        target_fpath = join(src_dpath, target + "." + code_type)
        so_fpath = join(build_dpath, target + "." + code_type + ".so")

        new_compile = False

        if not exists(so_fpath):
            new_compile = True

        else:
            if getmtime(target_fpath) > getmtime(so_fpath):
                new_compile = True

            for header in header_names:
                h_fpath = join(src_dpath, header + ".h")
                if exists(h_fpath) and getmtime(h_fpath) > getmtime(so_fpath):
                    new_compile = True

                h_fpath = join(build_dpath, header + ".h")
                if exists(h_fpath) and getmtime(h_fpath) > getmtime(so_fpath):
                    new_compile = True

            for ext_name in ext_names:
                ext_o_fpath = join(build_dpath, ext_name + ".o")
                if exists(ext_o_fpath) and getmtime(ext_o_fpath) > getmtime(so_fpath):
                    new_compile = True

        #
        # Compile
        #
        if new_compile:
            if code_type == "c" and not c_use_f2py:
                compiler = {"gnu": "gcc", "intel": "icc"}[env["compiler"]]
                cmd = "{} -fPIC {} {} -I. -I{} -I{} -c {}".format(
                    compiler, opt_flags, flags, inc_path_py, inc_path_numpy, ext_fpath
                )
                print("[compile]", cmd.replace(ext_fpath, basename(ext_fpath)))
                execute(cmd)

            else:
                compile_using_f2py(target_fpath, env["compiler"], flags, opt_flags, ext_names)

        else:
            print("{} is up to date.".format(so_fpath))