0130a57dd322397b01bc24db44501caafae25828
[aubio.git] / waflib / Tools / fc_config.py
1 #! /usr/bin/env python
2 # encoding: utf-8
3 # WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file
4
5 import re,shutil,os,sys,string,shlex
6 from waflib.Configure import conf
7 from waflib.TaskGen import feature,after_method,before_method
8 from waflib import Build,Utils
9 FC_FRAGMENT='        program main\n        end     program main\n'
10 FC_FRAGMENT2='        PROGRAM MAIN\n        END\n'
11 @conf
12 def fc_flags(conf):
13         v=conf.env
14         v['FC_SRC_F']=[]
15         v['FC_TGT_F']=['-c','-o']
16         v['FCINCPATH_ST']='-I%s'
17         v['FCDEFINES_ST']='-D%s'
18         if not v['LINK_FC']:v['LINK_FC']=v['FC']
19         v['FCLNK_SRC_F']=[]
20         v['FCLNK_TGT_F']=['-o']
21         v['FCFLAGS_fcshlib']=['-fpic']
22         v['LINKFLAGS_fcshlib']=['-shared']
23         v['fcshlib_PATTERN']='lib%s.so'
24         v['fcstlib_PATTERN']='lib%s.a'
25         v['FCLIB_ST']='-l%s'
26         v['FCLIBPATH_ST']='-L%s'
27         v['FCSTLIB_ST']='-l%s'
28         v['FCSTLIBPATH_ST']='-L%s'
29         v['FCSTLIB_MARKER']='-Wl,-Bstatic'
30         v['FCSHLIB_MARKER']='-Wl,-Bdynamic'
31         v['SONAME_ST']='-Wl,-h,%s'
32 @conf
33 def fc_add_flags(conf):
34         conf.add_os_flags('FCFLAGS')
35         conf.add_os_flags('LDFLAGS','LINKFLAGS')
36 @conf
37 def check_fortran(self,*k,**kw):
38         self.check_cc(fragment=FC_FRAGMENT,compile_filename='test.f',features='fc fcprogram',msg='Compiling a simple fortran app')
39 @conf
40 def check_fc(self,*k,**kw):
41         kw['compiler']='fc'
42         if not'compile_mode'in kw:
43                 kw['compile_mode']='fc'
44         if not'type'in kw:
45                 kw['type']='fcprogram'
46         if not'compile_filename'in kw:
47                 kw['compile_filename']='test.f90'
48         if not'code'in kw:
49                 kw['code']=FC_FRAGMENT
50         return self.check(*k,**kw)
51 @conf
52 def fortran_modifier_darwin(conf):
53         v=conf.env
54         v['FCFLAGS_fcshlib']=['-fPIC','-compatibility_version','1','-current_version','1']
55         v['LINKFLAGS_fcshlib']=['-dynamiclib']
56         v['fcshlib_PATTERN']='lib%s.dylib'
57         v['FRAMEWORKPATH_ST']='-F%s'
58         v['FRAMEWORK_ST']='-framework %s'
59         v['LINKFLAGS_fcstlib']=[]
60         v['FCSHLIB_MARKER']=''
61         v['FCSTLIB_MARKER']=''
62         v['SONAME_ST']=''
63 @conf
64 def fortran_modifier_win32(conf):
65         v=conf.env
66         v['fcprogram_PATTERN']=v['fcprogram_test_PATTERN']='%s.exe'
67         v['fcshlib_PATTERN']='%s.dll'
68         v['implib_PATTERN']='lib%s.dll.a'
69         v['IMPLIB_ST']='-Wl,--out-implib,%s'
70         v['FCFLAGS_fcshlib']=[]
71         v.append_value('FCFLAGS_fcshlib',['-DDLL_EXPORT'])
72         v.append_value('LINKFLAGS',['-Wl,--enable-auto-import'])
73 @conf
74 def fortran_modifier_cygwin(conf):
75         fortran_modifier_win32(conf)
76         v=conf.env
77         v['fcshlib_PATTERN']='cyg%s.dll'
78         v.append_value('LINKFLAGS_fcshlib',['-Wl,--enable-auto-image-base'])
79         v['FCFLAGS_fcshlib']=[]
80 @conf
81 def check_fortran_dummy_main(self,*k,**kw):
82         if not self.env.CC:
83                 self.fatal('A c compiler is required for check_fortran_dummy_main')
84         lst=['MAIN__','__MAIN','_MAIN','MAIN_','MAIN']
85         lst.extend([m.lower()for m in lst])
86         lst.append('')
87         self.start_msg('Detecting whether we need a dummy main')
88         for main in lst:
89                 kw['fortran_main']=main
90                 try:
91                         self.check_cc(fragment='int %s() { return 0; }\n'%(main or'test'),features='c fcprogram',mandatory=True)
92                         if not main:
93                                 self.env.FC_MAIN=-1
94                                 self.end_msg('no')
95                         else:
96                                 self.env.FC_MAIN=main
97                                 self.end_msg('yes %s'%main)
98                         break
99                 except self.errors.ConfigurationError:
100                         pass
101         else:
102                 self.end_msg('not found')
103                 self.fatal('could not detect whether fortran requires a dummy main, see the config.log')
104 GCC_DRIVER_LINE=re.compile('^Driving:')
105 POSIX_STATIC_EXT=re.compile('\S+\.a')
106 POSIX_LIB_FLAGS=re.compile('-l\S+')
107 @conf
108 def is_link_verbose(self,txt):
109         assert isinstance(txt,str)
110         for line in txt.splitlines():
111                 if not GCC_DRIVER_LINE.search(line):
112                         if POSIX_STATIC_EXT.search(line)or POSIX_LIB_FLAGS.search(line):
113                                 return True
114         return False
115 @conf
116 def check_fortran_verbose_flag(self,*k,**kw):
117         self.start_msg('fortran link verbose flag')
118         for x in['-v','--verbose','-verbose','-V']:
119                 try:
120                         self.check_cc(features='fc fcprogram_test',fragment=FC_FRAGMENT2,compile_filename='test.f',linkflags=[x],mandatory=True)
121                 except self.errors.ConfigurationError:
122                         pass
123                 else:
124                         if self.is_link_verbose(self.test_bld.err)or self.is_link_verbose(self.test_bld.out):
125                                 self.end_msg(x)
126                                 break
127         else:
128                 self.end_msg('failure')
129                 self.fatal('Could not obtain the fortran link verbose flag (see config.log)')
130         self.env.FC_VERBOSE_FLAG=x
131         return x
132 LINKFLAGS_IGNORED=[r'-lang*',r'-lcrt[a-zA-Z0-9\.]*\.o',r'-lc$',r'-lSystem',r'-libmil',r'-LIST:*',r'-LNO:*']
133 if os.name=='nt':
134         LINKFLAGS_IGNORED.extend([r'-lfrt*',r'-luser32',r'-lkernel32',r'-ladvapi32',r'-lmsvcrt',r'-lshell32',r'-lmingw',r'-lmoldname'])
135 else:
136         LINKFLAGS_IGNORED.append(r'-lgcc*')
137 RLINKFLAGS_IGNORED=[re.compile(f)for f in LINKFLAGS_IGNORED]
138 def _match_ignore(line):
139         for i in RLINKFLAGS_IGNORED:
140                 if i.match(line):
141                         return True
142         return False
143 def parse_fortran_link(lines):
144         final_flags=[]
145         for line in lines:
146                 if not GCC_DRIVER_LINE.match(line):
147                         _parse_flink_line(line,final_flags)
148         return final_flags
149 SPACE_OPTS=re.compile('^-[LRuYz]$')
150 NOSPACE_OPTS=re.compile('^-[RL]')
151 def _parse_flink_line(line,final_flags):
152         lexer=shlex.shlex(line,posix=True)
153         lexer.whitespace_split=True
154         t=lexer.get_token()
155         tmp_flags=[]
156         while t:
157                 def parse(token):
158                         if _match_ignore(token):
159                                 pass
160                         elif token.startswith('-lkernel32')and sys.platform=='cygwin':
161                                 tmp_flags.append(token)
162                         elif SPACE_OPTS.match(token):
163                                 t=lexer.get_token()
164                                 if t.startswith('P,'):
165                                         t=t[2:]
166                                 for opt in t.split(os.pathsep):
167                                         tmp_flags.append('-L%s'%opt)
168                         elif NOSPACE_OPTS.match(token):
169                                 tmp_flags.append(token)
170                         elif POSIX_LIB_FLAGS.match(token):
171                                 tmp_flags.append(token)
172                         else:
173                                 pass
174                         t=lexer.get_token()
175                         return t
176                 t=parse(t)
177         final_flags.extend(tmp_flags)
178         return final_flags
179 @conf
180 def check_fortran_clib(self,autoadd=True,*k,**kw):
181         if not self.env.FC_VERBOSE_FLAG:
182                 self.fatal('env.FC_VERBOSE_FLAG is not set: execute check_fortran_verbose_flag?')
183         self.start_msg('Getting fortran runtime link flags')
184         try:
185                 self.check_cc(fragment=FC_FRAGMENT2,compile_filename='test.f',features='fc fcprogram_test',linkflags=[self.env.FC_VERBOSE_FLAG])
186         except Exception:
187                 self.end_msg(False)
188                 if kw.get('mandatory',True):
189                         conf.fatal('Could not find the c library flags')
190         else:
191                 out=self.test_bld.err
192                 flags=parse_fortran_link(out.splitlines())
193                 self.end_msg('ok (%s)'%' '.join(flags))
194                 self.env.LINKFLAGS_CLIB=flags
195                 return flags
196         return[]
197 def getoutput(conf,cmd,stdin=False):
198         if stdin:
199                 stdin=Utils.subprocess.PIPE
200         else:
201                 stdin=None
202         env=conf.env.env or None
203         try:
204                 p=Utils.subprocess.Popen(cmd,stdin=stdin,stdout=Utils.subprocess.PIPE,stderr=Utils.subprocess.PIPE,env=env)
205                 if stdin:
206                         p.stdin.write('\n')
207                 out,err=p.communicate()
208         except Exception:
209                 conf.fatal('could not determine the compiler version %r'%cmd)
210         if not isinstance(out,str):
211                 out=out.decode(sys.stdout.encoding or'iso8859-1')
212         if not isinstance(err,str):
213                 err=err.decode(sys.stdout.encoding or'iso8859-1')
214         return(out,err)
215 ROUTINES_CODE="""\
216       subroutine foobar()
217       return
218       end
219       subroutine foo_bar()
220       return
221       end
222 """
223 MAIN_CODE="""
224 void %(dummy_func_nounder)s(void);
225 void %(dummy_func_under)s(void);
226 int %(main_func_name)s() {
227   %(dummy_func_nounder)s();
228   %(dummy_func_under)s();
229   return 0;
230 }
231 """
232 @feature('link_main_routines_func')
233 @before_method('process_source')
234 def link_main_routines_tg_method(self):
235         def write_test_file(task):
236                 task.outputs[0].write(task.generator.code)
237         bld=self.bld
238         bld(rule=write_test_file,target='main.c',code=MAIN_CODE%self.__dict__)
239         bld(rule=write_test_file,target='test.f',code=ROUTINES_CODE)
240         bld(features='fc fcstlib',source='test.f',target='test')
241         bld(features='c fcprogram',source='main.c',target='app',use='test')
242 def mangling_schemes():
243         for u in['_','']:
244                 for du in['','_']:
245                         for c in["lower","upper"]:
246                                 yield(u,du,c)
247 def mangle_name(u,du,c,name):
248         return getattr(name,c)()+u+(name.find('_')!=-1 and du or'')
249 @conf
250 def check_fortran_mangling(self,*k,**kw):
251         if not self.env.CC:
252                 self.fatal('A c compiler is required for link_main_routines')
253         if not self.env.FC:
254                 self.fatal('A fortran compiler is required for link_main_routines')
255         if not self.env.FC_MAIN:
256                 self.fatal('Checking for mangling requires self.env.FC_MAIN (execute "check_fortran_dummy_main" first?)')
257         self.start_msg('Getting fortran mangling scheme')
258         for(u,du,c)in mangling_schemes():
259                 try:
260                         self.check_cc(compile_filename=[],features='link_main_routines_func',msg='nomsg',errmsg='nomsg',mandatory=True,dummy_func_nounder=mangle_name(u,du,c,"foobar"),dummy_func_under=mangle_name(u,du,c,"foo_bar"),main_func_name=self.env.FC_MAIN)
261                 except self.errors.ConfigurationError:
262                         pass
263                 else:
264                         self.end_msg("ok ('%s', '%s', '%s-case')"%(u,du,c))
265                         self.env.FORTRAN_MANGLING=(u,du,c)
266                         break
267         else:
268                 self.end_msg(False)
269                 self.fatal('mangler not found')
270         return(u,du,c)
271 @feature('pyext')
272 @before_method('propagate_uselib_vars','apply_link')
273 def set_lib_pat(self):
274         self.env['fcshlib_PATTERN']=self.env['pyext_PATTERN']
275 @conf
276 def detect_openmp(self):
277         for x in['-fopenmp','-openmp','-mp','-xopenmp','-omp','-qsmp=omp']:
278                 try:
279                         self.check_fc(msg='Checking for OpenMP flag %s'%x,fragment='program main\n  call omp_get_num_threads()\nend program main',fcflags=x,linkflags=x,uselib_store='OPENMP')
280                 except self.errors.ConfigurationError:
281                         pass
282                 else:
283                         break
284         else:
285                 self.fatal('Could not find OpenMP')