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
14 # Runs a system command and returns return value and output
15 def SystemBacktick(program
):
16 # Run and return result
17 pipe
= os
.popen(program
+ ' 2>&1') # TODO: properly redirect stderr to stdout
19 retcode
= pipe
.close() or 0
20 return [retcode
, output
]
22 def SystemWXConfig(env
, args
):
23 if env
['PLATFORM'] == 'win32':
24 return SystemBacktick(env
['wxconfig']+' --wxcfg='+env
['ENV']['WXCFG']+' '+args
+env
['wxconfig_postargs'])
26 return SystemBacktick(env
['wxconfig']+' '+args
+env
['wxconfig_postargs'])
28 # Check version of wx-config
29 # It succeeds with a warning if version check failed.
30 def CheckWXConfigVersion(context
, version
):
31 releaseversion
= SystemWXConfig(context
.env
,'--release')[1]
33 if float(version
) > float(releaseversion
.strip()):
35 except (ValueError, TypeError):
36 context
.Message('version check failed, but ok... ')
40 # Check if requested components exist in wxWidgets
41 def CheckWXConfigComponents(context
, libraries
):
42 # set components, method depending on wxWidgets version
43 if CheckWXConfigVersion(context
, '2.6'):
44 context
.env
['wxconfig_postargs'] += ' '+string
.join(libraries
,',')
45 return SystemWXConfig(context
.env
, '--libs ')[0] == 0
46 # version 2.4 or below, only gl is an optional component with special flag
48 context
.env
['wxconfig'] += ' --gl-libs'
49 return SystemWXConfig(context
.env
, '--libs')[0] == 0
51 # no optional components can be checked, it should be allright
55 # Find wx-config with suitable settings. It tries to use a debug configuration if requested,
56 # but reverts to release or default when that fails.
57 def CheckWXConfigWin(context
, version
, debug
):
58 context
.Message('Checking for wxWidgets >= %s... '%version
)
60 # Some environment variables are required for wx-config to work, check them.
61 if 'WXWIN' not in context
.env
['ENV']:
62 # TODO: maybe try some default location like "C:\Program Files\wxWidgets-*" or registry
63 context
.Message('please set WXWIN in environment... ')
66 # If there's no WXCFG, either there is only one config or we try to find out.
67 if 'WXCFG' not in context
.env
['ENV']:
68 # TODO: try to find one in some sensible order from alternatives
69 # Current guess is: visual studio static, non-unicode
71 # Try debugging version first if requested, else fallback to release
73 context
.env
['ENV']['WXCFG'] = 'vc_lib\mswd'
74 if SystemWXConfig(context
.env
,'--libs')[0] == 0:
75 return CheckWXConfigVersion(context
, version
)
78 context
.env
['ENV']['WXCFG'] = 'vc_lib\msw'
79 if SystemWXConfig(context
.env
,'--libs')[0] == 0:
80 # this is the only configuration: use it
81 return CheckWXConfigVersion(context
, version
)
83 context
.Message('please set WXCFG in environment... ')
86 # WXCFG is in environment, nice.
87 # Try a debugging version if requested: postfix WXCFG with 'd'
89 oldwxcfg
= context
.env
['ENV']['WXCFG']
90 context
.env
['ENV']['WXCFG'] += 'd'
91 if SystemWXConfig(context
.env
,'--libs')[0] == 0:
92 return CheckWXConfigVersion(context
, version
)
93 # Failed: revert to plain WXCFG, use existing environment
94 context
.env
['ENV']['WXCFG'] = oldwxcfg
96 if SystemWXConfig(context
.env
,'--libs')[0] == 0:
97 return CheckWXConfigVersion(context
, version
)
99 # Everything failed ...
103 def CheckWXConfigPosixFind(context
, debug
):
104 # Find a wx-config compatible pathname
105 # wx*-config --> wx*-[0-9]+\.[0-9]+-config / wx<platform>-<version>-config
107 if (context
.env
['wxconfig']):
108 return context
.env
['wxconfig']
112 cfgre
= re
.compile('.*?\/wx(\w+?)(d?)-(\d+\.\d+)-config')
113 for dir in context
.env
['ENV']['PATH'].split(':'):
114 for cfgprog
in glob
.glob(os
.path
.join(dir,'wx*-config')):
115 m
= cfgre
.match(cfgprog
)
116 if m
and not m
.group(1) == 'base':
117 # add to either debug or release list
118 if m
.group(2) == 'release':
119 rellist
.append(cfgprog
)
121 dbglist
.append(cfgprog
)
123 # TODO: sort on version
125 # Now pick the right one
126 if debug
and len(dbglist
)>0:
136 def CheckWXConfigPosix(context
, version
, debug
):
137 # TODO: try several wx-config names
138 context
.Message('Checking for wxWidgets >= %s... '%version
)
140 # If supplied wx-config doesn't work, try to find another one
141 if SystemWXConfig(context
.env
,'--libs')[0] != 0:
142 wx_prog
= CheckWXConfigPosixFind(context
, debug
)
144 context
.Message('not found... ')
146 context
.env
['wxconfig'] = wx_prog
149 return CheckWXConfigVersion(context
, version
)
151 # use `wx-config --debug` if it's in its help
152 helpoutput
= SystemWXConfig(context
.env
,'--help')[1]
153 if helpoutput
.find('--debug') != -1:
154 context
.Message('--debug')
155 if SystemWXConfig(context
.env
,'--debug --libs')[0] == 0:
156 context
.env
['wxconfig'] = context
.env
['wxconfig'] +' --debug'
157 return CheckWXConfigVersion(context
, version
)
159 # If it's plain wx-config, we may need to look for another one for debugging
160 if context
.env
['wxconfig'] == 'wx-config':
161 wx_prog
= CheckWXConfigPosixFind(context
, debug
)
163 context
.env
['wxconfig'] = wx_prog
165 # TODO: possibly warning message when using release instead of debug
166 return CheckWXConfigVersion(context
, version
)
169 def CheckWXConfig(context
, version
, components
, debug
= False):
170 context
.env
['wxconfig_postargs']= ''
172 # Try to find it in path
174 context
.env
['wxconfig']
176 wx_prog
= context
.env
.WhereIs('wx-config')
178 # You could supply wx-config.exe as a fallback option.
179 #wx_prog = os.path.join('scons','wx-config')
180 context
.Message('wx-config not found...')
182 context
.env
['wxconfig'] = wx_prog
184 # Get wx-config invocation and check version
185 if context
.env
['PLATFORM'] == 'win32':
186 res
= CheckWXConfigWin(context
, version
, debug
)
188 res
= CheckWXConfigPosix(context
, version
, debug
)
190 # Make sure we have the required libraries
192 res
= CheckWXConfigComponents(context
, components
)
194 context
.Message('not all components found ['+string
.join(components
,',')+']... ')
199 def ParseWXConfig(env
):
201 # Windows doesn't work with ParseConfig (yet) :(
202 if env
['PLATFORM'] == 'win32':
203 # Use wx-config, yay!
204 # ParseConfig() on windows is broken, so the following is done instead
205 cflags
= SystemWXConfig(env
,'--cxxflags')[1]
206 env
.AppendUnique(CPPFLAGS
= cflags
.strip().split(' '))
207 libs
= SystemWXConfig(env
,'--libs')[1]
208 env
.AppendUnique(LINKFLAGS
= libs
.strip().split(' '))
210 # Here ParseConfig should really work
211 env
.ParseConfig(env
['wxconfig']+' --cxxflags --libs'+env
['wxconfig_postargs'])