f2py2e.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. """
  2. f2py2e - Fortran to Python C/API generator. 2nd Edition.
  3. See __usage__ below.
  4. Copyright 1999 -- 2011 Pearu Peterson all rights reserved.
  5. Copyright 2011 -- present NumPy Developers.
  6. Permission to use, modify, and distribute this software is given under the
  7. terms of the NumPy License.
  8. NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
  9. """
  10. import argparse
  11. import os
  12. import pprint
  13. import re
  14. import sys
  15. from numpy.f2py._backends import f2py_build_generator
  16. from . import (
  17. __version__,
  18. auxfuncs,
  19. capi_maps,
  20. cb_rules,
  21. cfuncs,
  22. crackfortran,
  23. f90mod_rules,
  24. rules,
  25. )
  26. from .cfuncs import errmess
  27. f2py_version = __version__.version
  28. numpy_version = __version__.version
  29. # outmess=sys.stdout.write
  30. show = pprint.pprint
  31. outmess = auxfuncs.outmess
  32. MESON_ONLY_VER = (sys.version_info >= (3, 12))
  33. __usage__ =\
  34. f"""Usage:
  35. 1) To construct extension module sources:
  36. f2py [<options>] <fortran files> [[[only:]||[skip:]] \\
  37. <fortran functions> ] \\
  38. [: <fortran files> ...]
  39. 2) To compile fortran files and build extension modules:
  40. f2py -c [<options>, <build_flib options>, <extra options>] <fortran files>
  41. 3) To generate signature files:
  42. f2py -h <filename.pyf> ...< same options as in (1) >
  43. Description: This program generates a Python C/API file (<modulename>module.c)
  44. that contains wrappers for given fortran functions so that they
  45. can be called from Python. With the -c option the corresponding
  46. extension modules are built.
  47. Options:
  48. -h <filename> Write signatures of the fortran routines to file <filename>
  49. and exit. You can then edit <filename> and use it instead
  50. of <fortran files>. If <filename>==stdout then the
  51. signatures are printed to stdout.
  52. <fortran functions> Names of fortran routines for which Python C/API
  53. functions will be generated. Default is all that are found
  54. in <fortran files>.
  55. <fortran files> Paths to fortran/signature files that will be scanned for
  56. <fortran functions> in order to determine their signatures.
  57. skip: Ignore fortran functions that follow until `:'.
  58. only: Use only fortran functions that follow until `:'.
  59. : Get back to <fortran files> mode.
  60. -m <modulename> Name of the module; f2py generates a Python/C API
  61. file <modulename>module.c or extension module <modulename>.
  62. Default is 'untitled'.
  63. '-include<header>' Writes additional headers in the C wrapper, can be passed
  64. multiple times, generates #include <header> each time.
  65. --[no-]lower Do [not] lower the cases in <fortran files>. By default,
  66. --lower is assumed with -h key, and --no-lower without -h key.
  67. --build-dir <dirname> All f2py generated files are created in <dirname>.
  68. Default is tempfile.mkdtemp().
  69. --overwrite-signature Overwrite existing signature file.
  70. --[no-]latex-doc Create (or not) <modulename>module.tex.
  71. Default is --no-latex-doc.
  72. --short-latex Create 'incomplete' LaTeX document (without commands
  73. \\documentclass, \\tableofcontents, and \\begin{{document}},
  74. \\end{{document}}).
  75. --[no-]rest-doc Create (or not) <modulename>module.rst.
  76. Default is --no-rest-doc.
  77. --debug-capi Create C/API code that reports the state of the wrappers
  78. during runtime. Useful for debugging.
  79. --[no-]wrap-functions Create Fortran subroutine wrappers to Fortran 77
  80. functions. --wrap-functions is default because it ensures
  81. maximum portability/compiler independence.
  82. --[no-]freethreading-compatible Create a module that declares it does or
  83. doesn't require the GIL. The default is
  84. --freethreading-compatible for backward
  85. compatibility. Inspect the Fortran code you are wrapping for
  86. thread safety issues before passing
  87. --no-freethreading-compatible, as f2py does not analyze
  88. fortran code for thread safety issues.
  89. --include-paths <path1>:<path2>:... Search include files from the given
  90. directories.
  91. --help-link [..] List system resources found by system_info.py. See also
  92. --link-<resource> switch below. [..] is optional list
  93. of resources names. E.g. try 'f2py --help-link lapack_opt'.
  94. --f2cmap <filename> Load Fortran-to-Python KIND specification from the given
  95. file. Default: .f2py_f2cmap in current directory.
  96. --quiet Run quietly.
  97. --verbose Run with extra verbosity.
  98. --skip-empty-wrappers Only generate wrapper files when needed.
  99. -v Print f2py version ID and exit.
  100. build backend options (only effective with -c)
  101. [NO_MESON] is used to indicate an option not meant to be used
  102. with the meson backend or above Python 3.12:
  103. --fcompiler= Specify Fortran compiler type by vendor [NO_MESON]
  104. --compiler= Specify distutils C compiler type [NO_MESON]
  105. --help-fcompiler List available Fortran compilers and exit [NO_MESON]
  106. --f77exec= Specify the path to F77 compiler [NO_MESON]
  107. --f90exec= Specify the path to F90 compiler [NO_MESON]
  108. --f77flags= Specify F77 compiler flags
  109. --f90flags= Specify F90 compiler flags
  110. --opt= Specify optimization flags [NO_MESON]
  111. --arch= Specify architecture specific optimization flags [NO_MESON]
  112. --noopt Compile without optimization [NO_MESON]
  113. --noarch Compile without arch-dependent optimization [NO_MESON]
  114. --debug Compile with debugging information
  115. --dep <dependency>
  116. Specify a meson dependency for the module. This may
  117. be passed multiple times for multiple dependencies.
  118. Dependencies are stored in a list for further processing.
  119. Example: --dep lapack --dep scalapack
  120. This will identify "lapack" and "scalapack" as dependencies
  121. and remove them from argv, leaving a dependencies list
  122. containing ["lapack", "scalapack"].
  123. --backend <backend_type>
  124. Specify the build backend for the compilation process.
  125. The supported backends are 'meson' and 'distutils'.
  126. If not specified, defaults to 'distutils'. On
  127. Python 3.12 or higher, the default is 'meson'.
  128. Extra options (only effective with -c):
  129. --link-<resource> Link extension module with <resource> as defined
  130. by numpy.distutils/system_info.py. E.g. to link
  131. with optimized LAPACK libraries (vecLib on MacOSX,
  132. ATLAS elsewhere), use --link-lapack_opt.
  133. See also --help-link switch. [NO_MESON]
  134. -L/path/to/lib/ -l<libname>
  135. -D<define> -U<name>
  136. -I/path/to/include/
  137. <filename>.o <filename>.so <filename>.a
  138. Using the following macros may be required with non-gcc Fortran
  139. compilers:
  140. -DPREPEND_FORTRAN -DNO_APPEND_FORTRAN -DUPPERCASE_FORTRAN
  141. When using -DF2PY_REPORT_ATEXIT, a performance report of F2PY
  142. interface is printed out at exit (platforms: Linux).
  143. When using -DF2PY_REPORT_ON_ARRAY_COPY=<int>, a message is
  144. sent to stderr whenever F2PY interface makes a copy of an
  145. array. Integer <int> sets the threshold for array sizes when
  146. a message should be shown.
  147. Version: {f2py_version}
  148. numpy Version: {numpy_version}
  149. License: NumPy license (see LICENSE.txt in the NumPy source code)
  150. Copyright 1999 -- 2011 Pearu Peterson all rights reserved.
  151. Copyright 2011 -- present NumPy Developers.
  152. https://numpy.org/doc/stable/f2py/index.html\n"""
  153. def scaninputline(inputline):
  154. files, skipfuncs, onlyfuncs, debug = [], [], [], []
  155. f, f2, f3, f5, f6, f8, f9, f10 = 1, 0, 0, 0, 0, 0, 0, 0
  156. verbose = 1
  157. emptygen = True
  158. dolc = -1
  159. dolatexdoc = 0
  160. dorestdoc = 0
  161. wrapfuncs = 1
  162. buildpath = '.'
  163. include_paths, freethreading_compatible, inputline = get_newer_options(inputline)
  164. signsfile, modulename = None, None
  165. options = {'buildpath': buildpath,
  166. 'coutput': None,
  167. 'f2py_wrapper_output': None}
  168. for l in inputline:
  169. if l == '':
  170. pass
  171. elif l == 'only:':
  172. f = 0
  173. elif l == 'skip:':
  174. f = -1
  175. elif l == ':':
  176. f = 1
  177. elif l[:8] == '--debug-':
  178. debug.append(l[8:])
  179. elif l == '--lower':
  180. dolc = 1
  181. elif l == '--build-dir':
  182. f6 = 1
  183. elif l == '--no-lower':
  184. dolc = 0
  185. elif l == '--quiet':
  186. verbose = 0
  187. elif l == '--verbose':
  188. verbose += 1
  189. elif l == '--latex-doc':
  190. dolatexdoc = 1
  191. elif l == '--no-latex-doc':
  192. dolatexdoc = 0
  193. elif l == '--rest-doc':
  194. dorestdoc = 1
  195. elif l == '--no-rest-doc':
  196. dorestdoc = 0
  197. elif l == '--wrap-functions':
  198. wrapfuncs = 1
  199. elif l == '--no-wrap-functions':
  200. wrapfuncs = 0
  201. elif l == '--short-latex':
  202. options['shortlatex'] = 1
  203. elif l == '--coutput':
  204. f8 = 1
  205. elif l == '--f2py-wrapper-output':
  206. f9 = 1
  207. elif l == '--f2cmap':
  208. f10 = 1
  209. elif l == '--overwrite-signature':
  210. options['h-overwrite'] = 1
  211. elif l == '-h':
  212. f2 = 1
  213. elif l == '-m':
  214. f3 = 1
  215. elif l[:2] == '-v':
  216. print(f2py_version)
  217. sys.exit()
  218. elif l == '--show-compilers':
  219. f5 = 1
  220. elif l[:8] == '-include':
  221. cfuncs.outneeds['userincludes'].append(l[9:-1])
  222. cfuncs.userincludes[l[9:-1]] = '#include ' + l[8:]
  223. elif l == '--skip-empty-wrappers':
  224. emptygen = False
  225. elif l[0] == '-':
  226. errmess(f'Unknown option {repr(l)}\n')
  227. sys.exit()
  228. elif f2:
  229. f2 = 0
  230. signsfile = l
  231. elif f3:
  232. f3 = 0
  233. modulename = l
  234. elif f6:
  235. f6 = 0
  236. buildpath = l
  237. elif f8:
  238. f8 = 0
  239. options["coutput"] = l
  240. elif f9:
  241. f9 = 0
  242. options["f2py_wrapper_output"] = l
  243. elif f10:
  244. f10 = 0
  245. options["f2cmap_file"] = l
  246. elif f == 1:
  247. try:
  248. with open(l):
  249. pass
  250. files.append(l)
  251. except OSError as detail:
  252. errmess(f'OSError: {detail!s}. Skipping file "{l!s}".\n')
  253. elif f == -1:
  254. skipfuncs.append(l)
  255. elif f == 0:
  256. onlyfuncs.append(l)
  257. if not f5 and not files and not modulename:
  258. print(__usage__)
  259. sys.exit()
  260. if not os.path.isdir(buildpath):
  261. if not verbose:
  262. outmess(f'Creating build directory {buildpath}\n')
  263. os.mkdir(buildpath)
  264. if signsfile:
  265. signsfile = os.path.join(buildpath, signsfile)
  266. if signsfile and os.path.isfile(signsfile) and 'h-overwrite' not in options:
  267. errmess(
  268. f'Signature file "{signsfile}" exists!!! Use --overwrite-signature to overwrite.\n')
  269. sys.exit()
  270. options['emptygen'] = emptygen
  271. options['debug'] = debug
  272. options['verbose'] = verbose
  273. if dolc == -1 and not signsfile:
  274. options['do-lower'] = 0
  275. else:
  276. options['do-lower'] = dolc
  277. if modulename:
  278. options['module'] = modulename
  279. if signsfile:
  280. options['signsfile'] = signsfile
  281. if onlyfuncs:
  282. options['onlyfuncs'] = onlyfuncs
  283. if skipfuncs:
  284. options['skipfuncs'] = skipfuncs
  285. options['dolatexdoc'] = dolatexdoc
  286. options['dorestdoc'] = dorestdoc
  287. options['wrapfuncs'] = wrapfuncs
  288. options['buildpath'] = buildpath
  289. options['include_paths'] = include_paths
  290. options['requires_gil'] = not freethreading_compatible
  291. options.setdefault('f2cmap_file', None)
  292. return files, options
  293. def callcrackfortran(files, options):
  294. rules.options = options
  295. crackfortran.debug = options['debug']
  296. crackfortran.verbose = options['verbose']
  297. if 'module' in options:
  298. crackfortran.f77modulename = options['module']
  299. if 'skipfuncs' in options:
  300. crackfortran.skipfuncs = options['skipfuncs']
  301. if 'onlyfuncs' in options:
  302. crackfortran.onlyfuncs = options['onlyfuncs']
  303. crackfortran.include_paths[:] = options['include_paths']
  304. crackfortran.dolowercase = options['do-lower']
  305. postlist = crackfortran.crackfortran(files)
  306. if 'signsfile' in options:
  307. outmess(f"Saving signatures to file \"{options['signsfile']}\"\n")
  308. pyf = crackfortran.crack2fortran(postlist)
  309. if options['signsfile'][-6:] == 'stdout':
  310. sys.stdout.write(pyf)
  311. else:
  312. with open(options['signsfile'], 'w') as f:
  313. f.write(pyf)
  314. if options["coutput"] is None:
  315. for mod in postlist:
  316. mod["coutput"] = f"{mod['name']}module.c"
  317. else:
  318. for mod in postlist:
  319. mod["coutput"] = options["coutput"]
  320. if options["f2py_wrapper_output"] is None:
  321. for mod in postlist:
  322. mod["f2py_wrapper_output"] = f"{mod['name']}-f2pywrappers.f"
  323. else:
  324. for mod in postlist:
  325. mod["f2py_wrapper_output"] = options["f2py_wrapper_output"]
  326. for mod in postlist:
  327. if options["requires_gil"]:
  328. mod['gil_used'] = 'Py_MOD_GIL_USED'
  329. else:
  330. mod['gil_used'] = 'Py_MOD_GIL_NOT_USED'
  331. # gh-26718 Reset global
  332. crackfortran.f77modulename = ''
  333. return postlist
  334. def buildmodules(lst):
  335. cfuncs.buildcfuncs()
  336. outmess('Building modules...\n')
  337. modules, mnames, isusedby = [], [], {}
  338. for item in lst:
  339. if '__user__' in item['name']:
  340. cb_rules.buildcallbacks(item)
  341. else:
  342. if 'use' in item:
  343. for u in item['use'].keys():
  344. if u not in isusedby:
  345. isusedby[u] = []
  346. isusedby[u].append(item['name'])
  347. modules.append(item)
  348. mnames.append(item['name'])
  349. ret = {}
  350. for module, name in zip(modules, mnames):
  351. if name in isusedby:
  352. outmess('\tSkipping module "%s" which is used by %s.\n' % (
  353. name, ','.join('"%s"' % s for s in isusedby[name])))
  354. else:
  355. um = []
  356. if 'use' in module:
  357. for u in module['use'].keys():
  358. if u in isusedby and u in mnames:
  359. um.append(modules[mnames.index(u)])
  360. else:
  361. outmess(
  362. f'\tModule "{name}" uses nonexisting "{u}" '
  363. 'which will be ignored.\n')
  364. ret[name] = {}
  365. dict_append(ret[name], rules.buildmodule(module, um))
  366. return ret
  367. def dict_append(d_out, d_in):
  368. for (k, v) in d_in.items():
  369. if k not in d_out:
  370. d_out[k] = []
  371. if isinstance(v, list):
  372. d_out[k] = d_out[k] + v
  373. else:
  374. d_out[k].append(v)
  375. def run_main(comline_list):
  376. """
  377. Equivalent to running::
  378. f2py <args>
  379. where ``<args>=string.join(<list>,' ')``, but in Python. Unless
  380. ``-h`` is used, this function returns a dictionary containing
  381. information on generated modules and their dependencies on source
  382. files.
  383. You cannot build extension modules with this function, that is,
  384. using ``-c`` is not allowed. Use the ``compile`` command instead.
  385. Examples
  386. --------
  387. The command ``f2py -m scalar scalar.f`` can be executed from Python as
  388. follows.
  389. .. literalinclude:: ../../source/f2py/code/results/run_main_session.dat
  390. :language: python
  391. """
  392. crackfortran.reset_global_f2py_vars()
  393. f2pydir = os.path.dirname(os.path.abspath(cfuncs.__file__))
  394. fobjhsrc = os.path.join(f2pydir, 'src', 'fortranobject.h')
  395. fobjcsrc = os.path.join(f2pydir, 'src', 'fortranobject.c')
  396. # gh-22819 -- begin
  397. parser = make_f2py_compile_parser()
  398. args, comline_list = parser.parse_known_args(comline_list)
  399. pyf_files, _ = filter_files("", "[.]pyf([.]src|)", comline_list)
  400. # Checks that no existing modulename is defined in a pyf file
  401. # TODO: Remove all this when scaninputline is replaced
  402. if args.module_name:
  403. if "-h" in comline_list:
  404. modname = (
  405. args.module_name
  406. ) # Directly use from args when -h is present
  407. else:
  408. modname = validate_modulename(
  409. pyf_files, args.module_name
  410. ) # Validate modname when -h is not present
  411. comline_list += ['-m', modname] # needed for the rest of scaninputline
  412. # gh-22819 -- end
  413. files, options = scaninputline(comline_list)
  414. auxfuncs.options = options
  415. capi_maps.load_f2cmap_file(options['f2cmap_file'])
  416. postlist = callcrackfortran(files, options)
  417. isusedby = {}
  418. for plist in postlist:
  419. if 'use' in plist:
  420. for u in plist['use'].keys():
  421. if u not in isusedby:
  422. isusedby[u] = []
  423. isusedby[u].append(plist['name'])
  424. for plist in postlist:
  425. module_name = plist['name']
  426. if plist['block'] == 'python module' and '__user__' in module_name:
  427. if module_name in isusedby:
  428. # if not quiet:
  429. usedby = ','.join(f'"{s}"' for s in isusedby[module_name])
  430. outmess(
  431. f'Skipping Makefile build for module "{module_name}" '
  432. f'which is used by {usedby}\n')
  433. if 'signsfile' in options:
  434. if options['verbose'] > 1:
  435. outmess(
  436. 'Stopping. Edit the signature file and then run f2py on the signature file: ')
  437. outmess(f"{os.path.basename(sys.argv[0])} {options['signsfile']}\n")
  438. return
  439. for plist in postlist:
  440. if plist['block'] != 'python module':
  441. if 'python module' not in options:
  442. errmess(
  443. 'Tip: If your original code is Fortran source then you must use -m option.\n')
  444. raise TypeError('All blocks must be python module blocks but got %s' % (
  445. repr(plist['block'])))
  446. auxfuncs.debugoptions = options['debug']
  447. f90mod_rules.options = options
  448. auxfuncs.wrapfuncs = options['wrapfuncs']
  449. ret = buildmodules(postlist)
  450. for mn in ret.keys():
  451. dict_append(ret[mn], {'csrc': fobjcsrc, 'h': fobjhsrc})
  452. return ret
  453. def filter_files(prefix, suffix, files, remove_prefix=None):
  454. """
  455. Filter files by prefix and suffix.
  456. """
  457. filtered, rest = [], []
  458. match = re.compile(prefix + r'.*' + suffix + r'\Z').match
  459. if remove_prefix:
  460. ind = len(prefix)
  461. else:
  462. ind = 0
  463. for file in [x.strip() for x in files]:
  464. if match(file):
  465. filtered.append(file[ind:])
  466. else:
  467. rest.append(file)
  468. return filtered, rest
  469. def get_prefix(module):
  470. p = os.path.dirname(os.path.dirname(module.__file__))
  471. return p
  472. class CombineIncludePaths(argparse.Action):
  473. def __call__(self, parser, namespace, values, option_string=None):
  474. include_paths_set = set(getattr(namespace, 'include_paths', []) or [])
  475. if option_string == "--include_paths":
  476. outmess("Use --include-paths or -I instead of --include_paths which will be removed")
  477. if option_string in {"--include-paths", "--include_paths"}:
  478. include_paths_set.update(values.split(':'))
  479. else:
  480. include_paths_set.add(values)
  481. namespace.include_paths = list(include_paths_set)
  482. def f2py_parser():
  483. parser = argparse.ArgumentParser(add_help=False)
  484. parser.add_argument("-I", dest="include_paths", action=CombineIncludePaths)
  485. parser.add_argument("--include-paths", dest="include_paths", action=CombineIncludePaths)
  486. parser.add_argument("--include_paths", dest="include_paths", action=CombineIncludePaths)
  487. parser.add_argument("--freethreading-compatible", dest="ftcompat", action=argparse.BooleanOptionalAction)
  488. return parser
  489. def get_newer_options(iline):
  490. iline = (' '.join(iline)).split()
  491. parser = f2py_parser()
  492. args, remain = parser.parse_known_args(iline)
  493. ipaths = args.include_paths
  494. if args.include_paths is None:
  495. ipaths = []
  496. return ipaths, args.ftcompat, remain
  497. def make_f2py_compile_parser():
  498. parser = argparse.ArgumentParser(add_help=False)
  499. parser.add_argument("--dep", action="append", dest="dependencies")
  500. parser.add_argument("--backend", choices=['meson', 'distutils'], default='distutils')
  501. parser.add_argument("-m", dest="module_name")
  502. return parser
  503. def preparse_sysargv():
  504. # To keep backwards bug compatibility, newer flags are handled by argparse,
  505. # and `sys.argv` is passed to the rest of `f2py` as is.
  506. parser = make_f2py_compile_parser()
  507. args, remaining_argv = parser.parse_known_args()
  508. sys.argv = [sys.argv[0]] + remaining_argv
  509. backend_key = args.backend
  510. if MESON_ONLY_VER and backend_key == 'distutils':
  511. outmess("Cannot use distutils backend with Python>=3.12,"
  512. " using meson backend instead.\n")
  513. backend_key = "meson"
  514. return {
  515. "dependencies": args.dependencies or [],
  516. "backend": backend_key,
  517. "modulename": args.module_name,
  518. }
  519. def run_compile():
  520. """
  521. Do it all in one call!
  522. """
  523. import tempfile
  524. # Collect dependency flags, preprocess sys.argv
  525. argy = preparse_sysargv()
  526. modulename = argy["modulename"]
  527. if modulename is None:
  528. modulename = 'untitled'
  529. dependencies = argy["dependencies"]
  530. backend_key = argy["backend"]
  531. build_backend = f2py_build_generator(backend_key)
  532. i = sys.argv.index('-c')
  533. del sys.argv[i]
  534. remove_build_dir = 0
  535. try:
  536. i = sys.argv.index('--build-dir')
  537. except ValueError:
  538. i = None
  539. if i is not None:
  540. build_dir = sys.argv[i + 1]
  541. del sys.argv[i + 1]
  542. del sys.argv[i]
  543. else:
  544. remove_build_dir = 1
  545. build_dir = tempfile.mkdtemp()
  546. _reg1 = re.compile(r'--link-')
  547. sysinfo_flags = [_m for _m in sys.argv[1:] if _reg1.match(_m)]
  548. sys.argv = [_m for _m in sys.argv if _m not in sysinfo_flags]
  549. if sysinfo_flags:
  550. sysinfo_flags = [f[7:] for f in sysinfo_flags]
  551. _reg2 = re.compile(
  552. r'--((no-|)(wrap-functions|lower|freethreading-compatible)|debug-capi|quiet|skip-empty-wrappers)|-include')
  553. f2py_flags = [_m for _m in sys.argv[1:] if _reg2.match(_m)]
  554. sys.argv = [_m for _m in sys.argv if _m not in f2py_flags]
  555. f2py_flags2 = []
  556. fl = 0
  557. for a in sys.argv[1:]:
  558. if a in ['only:', 'skip:']:
  559. fl = 1
  560. elif a == ':':
  561. fl = 0
  562. if fl or a == ':':
  563. f2py_flags2.append(a)
  564. if f2py_flags2 and f2py_flags2[-1] != ':':
  565. f2py_flags2.append(':')
  566. f2py_flags.extend(f2py_flags2)
  567. sys.argv = [_m for _m in sys.argv if _m not in f2py_flags2]
  568. _reg3 = re.compile(
  569. r'--((f(90)?compiler(-exec|)|compiler)=|help-compiler)')
  570. flib_flags = [_m for _m in sys.argv[1:] if _reg3.match(_m)]
  571. sys.argv = [_m for _m in sys.argv if _m not in flib_flags]
  572. # TODO: Once distutils is dropped completely, i.e. min_ver >= 3.12, unify into --fflags
  573. reg_f77_f90_flags = re.compile(r'--f(77|90)flags=')
  574. reg_distutils_flags = re.compile(r'--((f(77|90)exec|opt|arch)=|(debug|noopt|noarch|help-fcompiler))')
  575. fc_flags = [_m for _m in sys.argv[1:] if reg_f77_f90_flags.match(_m)]
  576. distutils_flags = [_m for _m in sys.argv[1:] if reg_distutils_flags.match(_m)]
  577. if not (MESON_ONLY_VER or backend_key == 'meson'):
  578. fc_flags.extend(distutils_flags)
  579. sys.argv = [_m for _m in sys.argv if _m not in (fc_flags + distutils_flags)]
  580. del_list = []
  581. for s in flib_flags:
  582. v = '--fcompiler='
  583. if s[:len(v)] == v:
  584. if MESON_ONLY_VER or backend_key == 'meson':
  585. outmess(
  586. "--fcompiler cannot be used with meson,"
  587. "set compiler with the FC environment variable\n"
  588. )
  589. else:
  590. from numpy.distutils import fcompiler
  591. fcompiler.load_all_fcompiler_classes()
  592. allowed_keys = list(fcompiler.fcompiler_class.keys())
  593. nv = ov = s[len(v):].lower()
  594. if ov not in allowed_keys:
  595. vmap = {} # XXX
  596. try:
  597. nv = vmap[ov]
  598. except KeyError:
  599. if ov not in vmap.values():
  600. print(f'Unknown vendor: "{s[len(v):]}"')
  601. nv = ov
  602. i = flib_flags.index(s)
  603. flib_flags[i] = '--fcompiler=' + nv # noqa: B909
  604. continue
  605. for s in del_list:
  606. i = flib_flags.index(s)
  607. del flib_flags[i]
  608. assert len(flib_flags) <= 2, repr(flib_flags)
  609. _reg5 = re.compile(r'--(verbose)')
  610. setup_flags = [_m for _m in sys.argv[1:] if _reg5.match(_m)]
  611. sys.argv = [_m for _m in sys.argv if _m not in setup_flags]
  612. if '--quiet' in f2py_flags:
  613. setup_flags.append('--quiet')
  614. # Ugly filter to remove everything but sources
  615. sources = sys.argv[1:]
  616. f2cmapopt = '--f2cmap'
  617. if f2cmapopt in sys.argv:
  618. i = sys.argv.index(f2cmapopt)
  619. f2py_flags.extend(sys.argv[i:i + 2])
  620. del sys.argv[i + 1], sys.argv[i]
  621. sources = sys.argv[1:]
  622. pyf_files, _sources = filter_files("", "[.]pyf([.]src|)", sources)
  623. sources = pyf_files + _sources
  624. modulename = validate_modulename(pyf_files, modulename)
  625. extra_objects, sources = filter_files('', '[.](o|a|so|dylib)', sources)
  626. library_dirs, sources = filter_files('-L', '', sources, remove_prefix=1)
  627. libraries, sources = filter_files('-l', '', sources, remove_prefix=1)
  628. undef_macros, sources = filter_files('-U', '', sources, remove_prefix=1)
  629. define_macros, sources = filter_files('-D', '', sources, remove_prefix=1)
  630. for i in range(len(define_macros)):
  631. name_value = define_macros[i].split('=', 1)
  632. if len(name_value) == 1:
  633. name_value.append(None)
  634. if len(name_value) == 2:
  635. define_macros[i] = tuple(name_value)
  636. else:
  637. print('Invalid use of -D:', name_value)
  638. # Construct wrappers / signatures / things
  639. if backend_key == 'meson':
  640. if not pyf_files:
  641. outmess('Using meson backend\nWill pass --lower to f2py\nSee https://numpy.org/doc/stable/f2py/buildtools/meson.html\n')
  642. f2py_flags.append('--lower')
  643. run_main(f" {' '.join(f2py_flags)} -m {modulename} {' '.join(sources)}".split())
  644. else:
  645. run_main(f" {' '.join(f2py_flags)} {' '.join(pyf_files)}".split())
  646. # Order matters here, includes are needed for run_main above
  647. include_dirs, _, sources = get_newer_options(sources)
  648. # Now use the builder
  649. builder = build_backend(
  650. modulename,
  651. sources,
  652. extra_objects,
  653. build_dir,
  654. include_dirs,
  655. library_dirs,
  656. libraries,
  657. define_macros,
  658. undef_macros,
  659. f2py_flags,
  660. sysinfo_flags,
  661. fc_flags,
  662. flib_flags,
  663. setup_flags,
  664. remove_build_dir,
  665. {"dependencies": dependencies},
  666. )
  667. builder.compile()
  668. def validate_modulename(pyf_files, modulename='untitled'):
  669. if len(pyf_files) > 1:
  670. raise ValueError("Only one .pyf file per call")
  671. if pyf_files:
  672. pyff = pyf_files[0]
  673. pyf_modname = auxfuncs.get_f2py_modulename(pyff)
  674. if modulename != pyf_modname:
  675. outmess(
  676. f"Ignoring -m {modulename}.\n"
  677. f"{pyff} defines {pyf_modname} to be the modulename.\n"
  678. )
  679. modulename = pyf_modname
  680. return modulename
  681. def main():
  682. if '--help-link' in sys.argv[1:]:
  683. sys.argv.remove('--help-link')
  684. if MESON_ONLY_VER:
  685. outmess("Use --dep for meson builds\n")
  686. else:
  687. from numpy.distutils.system_info import show_all
  688. show_all()
  689. return
  690. if '-c' in sys.argv[1:]:
  691. run_compile()
  692. else:
  693. run_main(sys.argv[1:])