def f2c(args, dryrun=False): """Apply f2c to compilation arguments Parameters ---------- args : iterable input compiler arguments dryrun : bool, default=True if False run f2c on detected fortran files Returns ------- new_args : list output compiler arguments Examples -------- >>> f2c(['gfortran', 'test.f'], dryrun=True) ['gfortran', 'test.c'] """ new_args = [] found_source = False for arg in args: if arg.endswith(".f"): filename = os.path.abspath(arg) if not dryrun: subprocess.check_call( ["f2c", os.path.basename(filename)], cwd=os.path.dirname(filename)) fix_f2c_clapack_calls(arg[:-2] + ".c") new_args.append(arg[:-2] + ".c") found_source = True else: new_args.append(arg) new_args_str = " ".join(args) if ".so" in new_args_str and "libgfortran.so" not in new_args_str: found_source = True if not found_source: print(f"f2c: source not found, skipping: {new_args_str}") return None return new_args
def test_fix_f2c_clapack_calls(tmpdir): code = dedent( """ #include "f2c.h" int sgemv_(char *trans, integer *m, integer *n, real *alpha) { return 0; } """ ) source_file = tmpdir / "sgemv.c" with open(source_file, "w") as fh: fh.write(code) fix_f2c_clapack_calls(str(source_file)) with open(source_file, "r") as fh: code_fixed = fh.read() assert code_fixed == code.replace("sgemv_", "wsgemv_")