You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

gen_install_headers.py 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python3
  2. import argparse
  3. from pathlib import Path
  4. def write_openblas_config_header(dest_dir, version, config_last_path, template_path):
  5. config_h_path = dest_dir / "openblas_config.h"
  6. with config_h_path.open('w') as f:
  7. f.write("#ifndef OPENBLAS_CONFIG_H\n")
  8. f.write("#define OPENBLAS_CONFIG_H\n")
  9. with config_last_path.open('r') as config_last:
  10. for line in config_last:
  11. if line.strip():
  12. defines = line.split('#define ')
  13. for define in defines:
  14. if define.strip():
  15. parts = define.split(maxsplit=1)
  16. if len(parts) > 0:
  17. macro_name = parts[0]
  18. rest_of_line = " ".join(parts[1:]) if len(parts) > 1 else ""
  19. line_to_write = f"#define OPENBLAS_{macro_name} {rest_of_line}"
  20. f.write(f"{line_to_write.strip()}\n")
  21. f.write(f'#define OPENBLAS_VERSION " OpenBLAS {version} "\n')
  22. with template_path.open('r') as template:
  23. f.write(template.read())
  24. f.write("#endif /* OPENBLAS_CONFIG_H */\n")
  25. print(f"Generated openblas_config.h in {dest_dir}")
  26. def write_f77blas_header(dest_dir, common_interface_path):
  27. f77blas_h_path = dest_dir / "f77blas.h"
  28. with f77blas_h_path.open('w') as f:
  29. f.write("#ifndef OPENBLAS_F77BLAS_H\n")
  30. f.write("#define OPENBLAS_F77BLAS_H\n")
  31. f.write('#include "openblas_config.h"\n')
  32. with common_interface_path.open('r') as common_interface:
  33. f.write(common_interface.read())
  34. f.write("#endif\n")
  35. print(f"Generated f77blas.h in {dest_dir}")
  36. def write_cblas_header(dest_dir, cblas_path, symbol_prefix, symbol_suffix):
  37. cblas_h_path = dest_dir / "cblas.h"
  38. with cblas_path.open('r') as cblas_file:
  39. content = cblas_file.read()
  40. if symbol_prefix:
  41. content = re.sub(r'\bcblas', f'{symbol_prefix}cblas', content)
  42. content = re.sub(r'\bopenblas', f'{symbol_prefix}openblas', content)
  43. content = re.sub(f'{symbol_prefix}openblas_complex', 'openblas_complex', content)
  44. content = re.sub(r'\bgoto', f'{symbol_prefix}goto', content)
  45. if symbol_suffix:
  46. content = re.sub(r'\bcblas(\w*)', r'cblas\1' + symbol_suffix, content)
  47. content = re.sub(r'\bopenblas(\w*)', r'openblas\1' + symbol_suffix, content)
  48. content = re.sub(r'\bgoto(\w*)', r'goto\1' + symbol_suffix, content)
  49. content = re.sub(r'openblas_complex_(\w*)' + symbol_suffix, r'openblas_complex_\1', content)
  50. content = content.replace('common', 'openblas_config')
  51. with cblas_h_path.open('w') as f:
  52. f.write(content)
  53. print(f"Generated cblas.h in {dest_dir}")
  54. def main():
  55. parser = argparse.ArgumentParser(description="Generate OpenBLAS headers")
  56. parser.add_argument('--dest-dir', required=True, help="Destination directory for headers")
  57. parser.add_argument('--version', required=True, help="OpenBLAS version")
  58. parser.add_argument('--config-last', required=True, help="Path to config_last.h")
  59. parser.add_argument('--template', required=True, help="Path to openblas_config_template.h")
  60. parser.add_argument('--common-interface', required=True, help="Path to common_interface.h")
  61. parser.add_argument('--cblas', required=True, help="Path to cblas.h")
  62. parser.add_argument('--symbol-prefix', default="", help="Symbol prefix for cblas.h")
  63. parser.add_argument('--symbol-suffix', default="", help="Symbol suffix for cblas.h")
  64. parser.add_argument('--generate-f77blas', action='store_true', help="Generate f77blas.h")
  65. parser.add_argument('--generate-cblas', action='store_true', help="Generate cblas.h")
  66. args = parser.parse_args()
  67. dest_dir = Path(args.dest_dir)
  68. dest_dir.mkdir(parents=True, exist_ok=True)
  69. config_last_path = Path(args.config_last)
  70. template_path = Path(args.template)
  71. common_interface_path = Path(args.common_interface)
  72. cblas_path = Path(args.cblas)
  73. write_openblas_config_header(dest_dir, args.version, config_last_path, template_path)
  74. if args.generate_f77blas:
  75. write_f77blas_header(dest_dir, common_interface_path)
  76. if args.generate_cblas:
  77. write_cblas_header(dest_dir, cblas_path, args.symbol_prefix, args.symbol_suffix)
  78. if __name__ == "__main__":
  79. main()