2 # SCons python library: wxWidgets configuration
4 # \author Willem van Engen <wvengen@stack.nl>
5 # \date created 2006/10/16
6 # \version v 1.3.2.2 2006/11/24 14:26:47 willem Exp
13 # Runs a system command and returns return value and output
14 def SystemBacktick(program
):
15 # Run and return result
16 pipe
= os
.popen(program
+ ' 2>&1') # TODO: properly redirect stderr to stdout
18 retcode
= pipe
.close() or 0
19 return [retcode
, output
]
21 def SystemWXConfig(env
, args
):
22 if env
['PLATFORM'] == 'win32':
23 return SystemBacktick(env
['wxconfig']+' --wxcfg='+env
['ENV']['WXCFG']+' '+args
+env
['wxconfig_postargs'])
25 return SystemBacktick(env
['wxconfig']+' '+args
+env
['wxconfig_postargs'])
27 # Check version of wx-config
28 # It succeeds with a warning if version check failed.
29 def CheckWXConfigVersion(context
, version
):
30 releaseversion
= SystemWXConfig(context
.env
,'--release')[1]
32 if float(version
) > float(releaseversion
.strip()):
34 except (ValueError, TypeError):
35 context
.Message('version check failed, but ok... ')
39 # Check if requested components exist in wxWidgets
40 def CheckWXConfigComponents(context
, libraries
):
41 # set components, method depending on wxWidgets version
42 if CheckWXConfigVersion(context
, '2.6'):
43 context
.env
['wxconfig_postargs'] += ' '+string
.join(libraries
,',')
44 return SystemWXConfig(context
.env
, '--libs ')[0] == 0
45 # version 2.4 or below, only gl is an optional component with special flag
47 context
.env
['wxconfig'] += ' --gl-libs'
48 return SystemWXConfig(context
.env
, '--libs')[0] == 0
50 # no optional components can be checked, it should be allright
54 # Find wx-config with suitable settings. It tries to use a debug configuration if requested,
55 # but reverts to release or default when that fails.
56 def CheckWXConfigWin(context
, version
, debug
):
57 context
.Message('Checking for wxWidgets >= %s... '%version
)
59 # Some environment variables are required for wx-config to work, check them.
60 if 'WXWIN' not in context
.env
['ENV']:
61 # TODO: maybe try some default location like "C:\Program Files\wxWidgets-*" or registry
62 context
.Message('please set WXWIN in environment... ')
65 # If there's no WXCFG, either there is only one config or we try to find out.
66 if 'WXCFG' not in context
.env
['ENV']:
67 # TODO: try to find one in some sensible order from alternatives
68 # Current guess is: visual studio static, non-unicode
70 # Try debugging version first if requested, else fallback to release
72 context
.env
['ENV']['WXCFG'] = 'vc_lib\mswd'
73 if SystemWXConfig(context
.env
,'--libs')[0] == 0:
74 return CheckWXConfigVersion(context
, version
)
77 context
.env
['ENV']['WXCFG'] = 'vc_lib\msw'
78 if SystemWXConfig(context
.env
,'--libs')[0] == 0:
79 # this is the only configuration: use it
80 return CheckWXConfigVersion(context
, version
)
82 context
.Message('please set WXCFG in environment... ')
85 # WXCFG is in environment, nice.
86 # Try a debugging version if requested: postfix WXCFG with 'd'
88 oldwxcfg
= context
.env
['ENV']['WXCFG']
89 context
.env
['ENV']['WXCFG'] += 'd'
90 if SystemWXConfig(context
.env
,'--libs')[0] == 0:
91 return CheckWXConfigVersion(context
, version
)
92 # Failed: revert to plain WXCFG, use existing environment
93 context
.env
['ENV']['WXCFG'] = oldwxcfg
95 if SystemWXConfig(context
.env
,'--libs')[0] == 0:
96 return CheckWXConfigVersion(context
, version
)
98 # Everything failed ...
102 def CheckWXConfigPosixFind(context
, debug
):
103 # Find a wx-config compatible pathname
104 # wx*-config --> wx*-[0-9]+\.[0-9]+-config / wx<platform>-<version>-config
106 if (context
.env
['wxconfig']):
107 return context
.env
['wxconfig']
111 cfgre
= re
.compile('.*?\/wx(\w+?)(d?)-(\d+\.\d+)-config')
112 for dir in context
.env
['ENV']['PATH'].split(':'):
113 for cfgprog
in glob
.glob(os
.path
.join(dir,'wx*-config')):
114 m
= cfgre
.match(cfgprog
)
115 if m
and not m
.group(1) == 'base':
116 # add to either debug or release list
117 if m
.group(2) == 'release':
118 rellist
.append(cfgprog
)
120 dbglist
.append(cfgprog
)
122 # TODO: sort on version
124 # Now pick the right one
125 if debug
and len(dbglist
)>0:
135 def CheckWXConfigPosix(context
, version
, debug
):
136 # TODO: try several wx-config names
137 context
.Message('Checking for wxWidgets >= %s... '%version
)
139 # If supplied wx-config doesn't work, try to find another one
140 if SystemWXConfig(context
.env
,'--libs')[0] != 0:
141 wx_prog
= CheckWXConfigPosixFind(context
, debug
)
143 context
.Message('not found... ')
145 context
.env
['wxconfig'] = wx_prog
148 return CheckWXConfigVersion(context
, version
)
150 # use `wx-config --debug` if it's in its help
151 helpoutput
= SystemWXConfig(context
.env
,'--help')[1]
152 if helpoutput
.find('--debug') != -1:
153 context
.Message('--debug')
154 if SystemWXConfig(context
.env
,'--debug --libs')[0] == 0:
155 context
.env
['wxconfig'] = context
.env
['wxconfig'] +' --debug'
156 return CheckWXConfigVersion(context
, version
)
158 # If it's plain wx-config, we may need to look for another one for debugging
159 if context
.env
['wxconfig'] == 'wx-config':
160 wx_prog
= CheckWXConfigPosixFind(context
, debug
)
162 context
.env
['wxconfig'] = wx_prog
164 # TODO: possibly warning message when using release instead of debug
165 return CheckWXConfigVersion(context
, version
)
168 def CheckWXConfig(context
, version
, components
, debug
= False):
169 context
.env
['wxconfig_postargs']= ''
171 # Try to find it in path
173 context
.env
['wxconfig']
175 wx_prog
= context
.env
.WhereIs('wx-config')
177 # You could supply wx-config.exe as a fallback option.
178 #wx_prog = os.path.join('scons','wx-config')
179 context
.Message('wx-config not found...')
181 context
.env
['wxconfig'] = wx_prog
183 # Get wx-config invocation and check version
184 if context
.env
['PLATFORM'] == 'win32':
185 res
= CheckWXConfigWin(context
, version
, debug
)
187 res
= CheckWXConfigPosix(context
, version
, debug
)
189 # Make sure we have the required libraries
191 res
= CheckWXConfigComponents(context
, components
)
193 context
.Message('not all components found ['+string
.join(components
,',')+']... ')
198 def ParseWXConfig(env
):
200 # Windows doesn't work with ParseConfig (yet) :(
201 if env
['PLATFORM'] == 'win32':
202 # Use wx-config, yay!
203 # ParseConfig() on windows is broken, so the following is done instead
204 cflags
= SystemWXConfig(env
,'--cxxflags')[1]
205 env
.AppendUnique(CPPFLAGS
= cflags
.strip().split(' '))
206 libs
= SystemWXConfig(env
,'--libs')[1]
207 env
.AppendUnique(LINKFLAGS
= libs
.strip().split(' '))
209 # Here ParseConfig should really work
210 env
.ParseConfig(env
['wxconfig']+' --cxxflags --libs'+env
['wxconfig_postargs'])