18
18
from pathlib import Path
19
19
from itertools import dropwhile
20
20
import argparse
21
+ import copy
21
22
22
23
from . import crackfortran
23
24
from . import rules
190
191
191
192
def scaninputline (inputline ):
192
193
files , skipfuncs , onlyfuncs , debug = [], [], [], []
193
- f , f2 , f3 , f5 , f6 , f7 , f8 , f9 , f10 = 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
194
+ f , f2 , f3 , f5 , f6 , f8 , f9 , f10 = 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0
194
195
verbose = 1
195
196
emptygen = True
196
197
dolc = - 1
197
198
dolatexdoc = 0
198
199
dorestdoc = 0
199
200
wrapfuncs = 1
200
201
buildpath = '.'
201
- include_paths = []
202
+ include_paths , inputline = get_includes ( inputline )
202
203
signsfile , modulename = None , None
203
204
options = {'buildpath' : buildpath ,
204
205
'coutput' : None ,
@@ -258,14 +259,6 @@ def scaninputline(inputline):
258
259
elif l [:8 ] == '-include' :
259
260
cfuncs .outneeds ['userincludes' ].append (l [9 :- 1 ])
260
261
cfuncs .userincludes [l [9 :- 1 ]] = '#include ' + l [8 :]
261
- elif l [:15 ] in '--include_paths' :
262
- outmess (
263
- 'f2py option --include_paths is deprecated, use --include-paths instead.\n ' )
264
- f7 = 1
265
- elif l [:15 ] in '--include-paths' :
266
- # Similar to using -I with -c, however this is
267
- # also used during generation of wrappers
268
- f7 = 1
269
262
elif l == '--skip-empty-wrappers' :
270
263
emptygen = False
271
264
elif l [0 ] == '-' :
@@ -280,9 +273,6 @@ def scaninputline(inputline):
280
273
elif f6 :
281
274
f6 = 0
282
275
buildpath = l
283
- elif f7 :
284
- f7 = 0
285
- include_paths .extend (l .split (os .pathsep ))
286
276
elif f8 :
287
277
f8 = 0
288
278
options ["coutput" ] = l
@@ -450,7 +440,7 @@ def run_main(comline_list):
450
440
fobjhsrc = os .path .join (f2pydir , 'src' , 'fortranobject.h' )
451
441
fobjcsrc = os .path .join (f2pydir , 'src' , 'fortranobject.c' )
452
442
# gh-22819 -- begin
453
- parser = make_f2py_parser ()
443
+ parser = make_f2py_compile_parser ()
454
444
args , comline_list = parser .parse_known_args (comline_list )
455
445
pyf_files , _ = filter_files ("" , "[.]pyf([.]src|)" , comline_list )
456
446
# Checks that no existing modulename is defined in a pyf file
@@ -532,7 +522,35 @@ def get_prefix(module):
532
522
p = os .path .dirname (os .path .dirname (module .__file__ ))
533
523
return p
534
524
535
- def make_f2py_parser ():
525
+
526
+ class CombineIncludePaths (argparse .Action ):
527
+ def __call__ (self , parser , namespace , values , option_string = None ):
528
+ include_paths_set = set (getattr (namespace , 'include_paths' , []) or [])
529
+ if option_string == "--include_paths" :
530
+ outmess ("Use --include-paths or -I instead of --include_paths which will be removed" )
531
+ if option_string == "--include-paths" or option_string == "--include_paths" :
532
+ include_paths_set .update (values .split (':' ))
533
+ else :
534
+ include_paths_set .add (values )
535
+ setattr (namespace , 'include_paths' , list (include_paths_set ))
536
+
537
+ def include_parser ():
538
+ parser = argparse .ArgumentParser (add_help = False )
539
+ parser .add_argument ("-I" , dest = "include_paths" , action = CombineIncludePaths )
540
+ parser .add_argument ("--include-paths" , dest = "include_paths" , action = CombineIncludePaths )
541
+ parser .add_argument ("--include_paths" , dest = "include_paths" , action = CombineIncludePaths )
542
+ return parser
543
+
544
+ def get_includes (iline ):
545
+ iline = (' ' .join (iline )).split ()
546
+ parser = include_parser ()
547
+ args , remain = parser .parse_known_args (iline )
548
+ ipaths = args .include_paths
549
+ if args .include_paths is None :
550
+ ipaths = []
551
+ return ipaths , remain
552
+
553
+ def make_f2py_compile_parser ():
536
554
parser = argparse .ArgumentParser (add_help = False )
537
555
parser .add_argument ("--dep" , action = "append" , dest = "dependencies" )
538
556
parser .add_argument ("--backend" , choices = ['meson' , 'distutils' ], default = 'distutils' )
@@ -542,7 +560,7 @@ def make_f2py_parser():
542
560
def preparse_sysargv ():
543
561
# To keep backwards bug compatibility, newer flags are handled by argparse,
544
562
# and `sys.argv` is passed to the rest of `f2py` as is.
545
- parser = make_f2py_parser ()
563
+ parser = make_f2py_compile_parser ()
546
564
547
565
args , remaining_argv = parser .parse_known_args ()
548
566
sys .argv = [sys .argv [0 ]] + remaining_argv
@@ -659,19 +677,19 @@ def run_compile():
659
677
if '--quiet' in f2py_flags :
660
678
setup_flags .append ('--quiet' )
661
679
680
+ # Ugly filter to remove everything but sources
662
681
sources = sys .argv [1 :]
663
- for optname in [ '--include_paths' , '--include-paths' , '-- f2cmap']:
664
- if optname in sys .argv :
665
- i = sys .argv .index (optname )
666
- f2py_flags .extend (sys .argv [i :i + 2 ])
667
- del sys .argv [i + 1 ], sys .argv [i ]
668
- sources = sys .argv [1 :]
682
+ f2cmapopt = '--f2cmap'
683
+ if f2cmapopt in sys .argv :
684
+ i = sys .argv .index (f2cmapopt )
685
+ f2py_flags .extend (sys .argv [i :i + 2 ])
686
+ del sys .argv [i + 1 ], sys .argv [i ]
687
+ sources = sys .argv [1 :]
669
688
670
689
pyf_files , _sources = filter_files ("" , "[.]pyf([.]src|)" , sources )
671
690
sources = pyf_files + _sources
672
691
modulename = validate_modulename (pyf_files , modulename )
673
692
extra_objects , sources = filter_files ('' , '[.](o|a|so|dylib)' , sources )
674
- include_dirs , sources = filter_files ('-I' , '' , sources , remove_prefix = 1 )
675
693
library_dirs , sources = filter_files ('-L' , '' , sources , remove_prefix = 1 )
676
694
libraries , sources = filter_files ('-l' , '' , sources , remove_prefix = 1 )
677
695
undef_macros , sources = filter_files ('-U' , '' , sources , remove_prefix = 1 )
@@ -694,6 +712,8 @@ def run_compile():
694
712
else :
695
713
run_main (f" { ' ' .join (f2py_flags )} { ' ' .join (pyf_files )} " .split ())
696
714
715
+ # Order matters here, includes are needed for run_main above
716
+ include_dirs , sources = get_includes (sources )
697
717
# Now use the builder
698
718
builder = build_backend (
699
719
modulename ,
0 commit comments