d267f00ddfebdc798009eb46f486a6a2941fd7fe
[scons.git] / test / import.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 """
28 Verify that we can import and use the contents of Platform and Tool
29 modules directly.
30 """
31
32 import re
33 import sys
34
35 import TestSCons
36
37 test = TestSCons.TestSCons()
38
39 SConstruct_path = test.workpath('SConstruct')
40
41 platforms = [
42     'aix',
43     'cygwin',
44     'darwin',
45     'hpux',
46     'irix',
47     'os2',
48     'posix',
49     'sunos',
50     'win32'
51 ]
52
53 for platform in platforms:
54     test.write('SConstruct', """
55 print "Platform %(platform)s"
56 env = Environment(platform = '%(platform)s')
57 import SCons.Platform.%(platform)s
58 x = SCons.Platform.%(platform)s.generate
59 """ % locals())
60     test.run()
61
62 tools = [
63     # Can't import '386asm' everywhere due to Windows registry dependency.
64     'aixc++',
65     'aixcc',
66     'aixf77',
67     'aixlink',
68     'applelink',
69     'ar',
70     'as',
71     'bcc32',
72     'BitKeeper',
73     'c++',
74     'cc',
75     'cvf',
76     'CVS',
77     'default',
78     'dmd',
79     'dvi',
80     'dvipdf',
81     'dvips',
82     'f77',
83     'f90',
84     'f95',
85     'fortran',
86     'g++',
87     'g77',
88     'gas',
89     'gcc',
90     'gnulink',
91     'gs',
92     'hpc++',
93     'hpcc',
94     'hplink',
95     'icc',
96     'icl',
97     'ifort',
98     'ifl',
99     'ilink',
100     'ilink32',
101     'intelc',
102     'jar',
103     'javac',
104     'javah',
105     'latex',
106     'lex',
107     'link',
108     # Can't import 'linkloc' everywhere due to Windows registry dependency.
109     'm4',
110     'masm',
111     'midl',
112     'mingw',
113     'mslib',
114     'mslink',
115     'msvc',
116     'msvs',
117     'mwcc',
118     'mwld',
119     'nasm',
120     'pdf',
121     'pdflatex',
122     'pdftex',
123     'Perforce',
124     'qt',
125     'RCS',
126     'rmic',
127     'rpcgen',
128     'SCCS',
129     'sgiar',
130     'sgic++',
131     'sgicc',
132     'sgilink',
133     'Subversion',
134     'sunar',
135     'sunc++',
136     'suncc',
137     'sunlink',
138     'swig',
139     'tar',
140     'tex',
141     'tlib',
142     'yacc',
143     'zip',
144 ]
145
146 if sys.platform == 'win32':
147     tools.extend([
148         '386asm',
149         'linkloc',
150     ])
151
152 # Intel no compiler warning..
153 intel_no_compiler_warning = """
154 scons: warning: Failed to find Intel compiler for version='None', abi='[^']*'
155 """ + TestSCons.file_expr
156
157 # Intel no top dir warning.
158 intel_no_top_dir_warning = """
159 scons: warning: Can't find Intel compiler top dir for version='None', abi='[^']*'
160 """ + TestSCons.file_expr
161
162 # Intel no license directory warning
163 intel_license_warning = re.escape("""
164 scons: warning: Intel license dir was not found.  Tried using the INTEL_LICENSE_FILE environment variable (), the registry () and the default path (C:\Program Files\Common Files\Intel\Licenses).  Using the default path as a last resort.
165 """) + TestSCons.file_expr
166
167 intel_warnings = [
168     re.compile(intel_license_warning),
169     re.compile(intel_no_compiler_warning),
170     re.compile(intel_no_compiler_warning + intel_license_warning),
171     re.compile(intel_no_top_dir_warning),
172     re.compile(intel_no_top_dir_warning + intel_license_warning),
173 ]
174
175 moc = test.where_is('moc')
176 if moc:
177     import os.path
178
179     qtdir = os.path.dirname(os.path.dirname(moc))
180
181     qt_err = """
182 scons: warning: Could not detect qt, using moc executable as a hint (QTDIR=%(qtdir)s)
183 """ % locals()
184
185 else:
186
187     qt_err = """
188 scons: warning: Could not detect qt, using empty QTDIR
189 """
190
191 qt_warnings = [ re.compile(qt_err + TestSCons.file_expr) ]
192
193 error_output = {
194     'icl' : intel_warnings,
195     'intelc' : intel_warnings,
196     'qt' : qt_warnings,
197 }
198
199 # An SConstruct for importing Tool names that have illegal characters
200 # for Python variable names.
201 indirect_import = """\
202 print "Tool %(tool)s (indirect)"
203 env = Environment(tools = ['%(tool)s'])
204
205 SCons = __import__('SCons.Tool.%(tool)s', globals(), locals(), [])
206 m = getattr(SCons.Tool, '%(tool)s')
207 env = Environment()
208 m.generate(env)
209 """
210
211 # An SConstruct for importing Tool names "normally."
212 direct_import = """\
213 print "Tool %(tool)s (direct)"
214 env = Environment(tools = ['%(tool)s'])
215
216 import SCons.Tool.%(tool)s
217 env = Environment()
218 SCons.Tool.%(tool)s.generate(env)
219 """
220
221 failures = []
222 for tool in tools:
223     if tool[0] in '0123456789' or '+' in tool:
224         test.write('SConstruct', indirect_import % locals())
225     else:
226         test.write('SConstruct', direct_import % locals())
227     test.run(stderr=None)
228     stderr = test.stderr()
229     if stderr:
230         matched = None
231         for expression in error_output.get(tool, []):
232             if expression.match(stderr):
233                 matched = 1
234                 break
235         if not matched:
236             print "Failed importing '%s', stderr:" % tool
237             print stderr
238             failures.append(tool)
239
240 test.fail_test(len(failures))
241
242 test.pass_test()