summaryrefslogtreecommitdiffstats
path: root/test/Configure.py
blob: 7da4e9e0c7cd5b7c919b2fb8621bde168d6e4325 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/usr/bin/env python
#
# __COPYRIGHT__
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import os
import re
import sys
import shutil

import TestCmd
import TestSCons

if sys.platform == 'win32':
    lib = 'msvcrt'
else:
    lib = 'm'

# to use cygwin compilers on cmd.exe -> uncomment following line
#lib = 'm'

work_cnt = 0
work_dir = None
python = TestSCons.python
test = TestSCons.TestSCons()


def reset(match = 1):
    global test, work_dir, work_cnt
    work_cnt = work_cnt + 1
    work_dir='test%d' % work_cnt
    test.subdir(work_dir)
    if match == 0:
        test.match_func = TestCmd.match_re
    elif match == 1:
        test.match_func = TestCmd.match_re_dotall
    elif match == 2:
        test.match_func = TestCmd.match_exact

def checkFiles(test, files):
    global work_dir
    for f in files:
        test.fail_test( not os.path.isfile( test.workpath(work_dir,f) ) )


def checkLog( test, logfile, numUpToDate, numCache ):
    test.fail_test(not os.path.exists(test.workpath(work_dir, logfile)))
    log = test.read(test.workpath(work_dir, logfile))
    try:
        test.fail_test( len( re.findall( "is up to date", log ) ) != numUpToDate )
        test.fail_test( len( re.findall( "\(cached\): Building \S+ failed in a previous run.", log ) ) != numCache )
    except:
        print "contents of log ", test.workpath(work_dir, logfile), "\n", log
        raise


try:

    # 1.1 if checks are ok, the cache mechanism should work

    reset(match=2)

    test.write([work_dir,  'SConstruct'], """
env = Environment()
import os
env.AppendENVPath('PATH', os.environ['PATH'])
conf = Configure(env)
r1 = conf.CheckLibWithHeader( '%s', 'math.h', 'c' )
r2 = conf.CheckLibWithHeader( None, 'math.h', 'c' )
r3 = conf.CheckLib( '%s', autoadd=0 )
r4 = conf.CheckLib( None, autoadd=0 )
r5 = conf.CheckCHeader( 'math.h' )
r6 = conf.CheckCXXHeader( 'vector' )
env = conf.Finish()
if not (r1 and r2 and r3 and r4 and r5 and r6):
     Exit(1)
""" % (lib,lib))

    required_stdout = test.wrap_stdout(build_str="scons: `.' is up to date.\n",
                                       read_str=
    """Checking for main() in C library %s... yes
Checking for main() in C library None... yes
Checking for main() in C library %s... yes
Checking for main() in C library None... yes
Checking for C header file math.h... yes
Checking for C++ header file vector... yes
""" % (lib, lib))


    test.run(chdir=work_dir, stdout = required_stdout)
    checkLog(test,'config.log', 0, 0 )

    test.run(chdir=work_dir, stdout = required_stdout)
    checkLog(test,'config.log',12, 0 )

    # 1.2 if checks are not ok, the cache mechanism should work as well
    #     (via explicit cache)
    reset(match=2)              # match exactly, "()" is a regexp thing

    test.write([work_dir,  'SConstruct'], """
env = Environment()
import os
env.AppendENVPath('PATH', os.environ['PATH'])
conf = env.Configure()
r1 = conf.CheckCHeader( 'no_std_c_header.h' ) # leads to compile error
r2 = conf.CheckLib( 'no_c_library_SAFFDG' )   # leads to link error
env = conf.Finish()
if not (not r1 and not r2):
     print "FAIL: ", r1, r2
     Exit(1)
""")

    required_stdout = test.wrap_stdout(build_str="scons: `.' is up to date.\n",
                                       read_str=
    """Checking for C header file no_std_c_header.h... no
Checking for main() in C library no_c_library_SAFFDG... no
""")


    test.run(chdir=work_dir, stdout = required_stdout)
    checkLog(test, 'config.log', 0, 0 )

    test.run(chdir=work_dir, stdout = required_stdout)
    checkLog(test, 'config.log', 2, 2 )


    # 2.1 test that normal builds work together with Sconf
    reset()


    test.write([work_dir,  'SConstruct'], """
env = Environment()
import os
env.AppendENVPath('PATH', os.environ['PATH'])
conf = Configure(env)
r1 = conf.CheckCHeader( 'math.h' )
r2 = conf.CheckCHeader( 'no_std_c_header.h' ) # leads to compile error
env = conf.Finish()
Export( 'env' )
SConscript( 'SConscript' )
""")
    test.write([work_dir,  'SConscript'], """
Import( 'env' )
env.Program( 'TestProgram', 'TestProgram.c' )
""")
    test.write([work_dir,  'TestProgram.c'], """
#include <stdio.h>

int main() {
  printf( "Hello\\n" );
}
""")
    required_stdout = test.wrap_stdout(build_str='.*',
                                       read_str=
    """Checking for C header file math.h... yes
Checking for C header file no_std_c_header.h... no
""")
    test.run(chdir=work_dir,  stdout = required_stdout )
    checkLog( test, 'config.log', 0, 0 )

    test.run(chdir=work_dir,  stdout = required_stdout )
    checkLog( test, 'config.log', 3, 1 )


    # 2.2 test that BuildDir builds work together with Sconf
    reset()


    test.write([work_dir,  'SConstruct'], """
env = Environment(LOGFILE='build/config.log')
import os
env.AppendENVPath('PATH', os.environ['PATH'])
BuildDir( 'build', '.' )
conf = env.Configure(conf_dir='build/config.tests', log_file='$LOGFILE')
r1 = conf.CheckCHeader( 'math.h' )
r2 = conf.CheckCHeader( 'no_std_c_header.h' ) # leads to compile error
env = conf.Finish()
Export( 'env' )
# print open( 'build/config.log' ).readlines()
SConscript( 'build/SConscript' )
""")
    test.write([work_dir,  'SConscript'], """
Import( 'env' )
env.Program( 'TestProgram', 'TestProgram.c' )
""")
    test.write([work_dir,  'TestProgram.c'], """
#include <stdio.h>

int main() {
  printf( "Hello\\n" );
}
""")
    required_stdout = test.wrap_stdout(build_str='.*',
                                       read_str=
    """Checking for C header file math.h... yes
Checking for C header file no_std_c_header.h... no
""")
    test.run(chdir=work_dir,  stdout = required_stdout )
    checkLog( test, 'build/config.log', 0, 0 )

    test.run(chdir=work_dir,  stdout = required_stdout )
    checkLog( test, 'build/config.log', 3, 1 )

    # 2.3 test that Configure calls in SConscript files work
    #     even if BuildDir is set
    reset()

    test.subdir( [work_dir, 'sub'], [work_dir, 'sub', 'local'] )
    test.write([work_dir,  'SConstruct'], """
opts = Options()
opts.Add('chdir')
env = Environment(options=opts)
if env['chdir'] == 'yes':
  SConscriptChdir(1)
else:
  SConscriptChdir(0)
BuildDir( 'build', '.' )
SConscript( 'build/SConscript' )
""")
    test.write([work_dir,  'sub', 'local', 'local_header.h'],
               "/* Hello World */" )
    test.write([work_dir,  'SConscript'], """
SConscript( 'sub/SConscript' )
""")
    test.write([work_dir,  'sub', 'SConscript'], """
def CustomTest(context):
  context.Message('Executing Custom Test ... ')
  ret = context.TryCompile('#include "local_header.h"', '.c')
  context.Result(ret)
  return ret

env = Environment(FOO='fff')
env.Append( CPPPATH='local' )
import os
env.AppendENVPath('PATH', os.environ['PATH'])
conf = Configure( env, custom_tests = {'CustomTest' : CustomTest,
                                       '$FOO' : CustomTest} )
if hasattr(conf, 'fff'):
  conf.Message('$FOO should not have been expanded!')
  Exit(1)
if not conf.CheckCHeader( 'math.h' ):
  Exit(1)
if conf.CheckCHeader( 'no_std_c_header.h' ):
  Exit(1)
if not conf.CustomTest():
  Exit(1)
env = conf.Finish()
env.Program( 'TestProgram', 'TestProgram.c' )
""")
    test.write([work_dir, 'sub', 'TestProgram.h'], """
/* Just a test header */
""")
    test.write([work_dir, 'sub', 'TestProgram.c'], """
#include "TestProgram.h"
#include <stdio.h>

int main() {
  printf( "Hello\\n" );
}
""")
    required_stdout = test.wrap_stdout(build_str='.*',
                                       read_str=
    """Checking for C header file math.h... yes
Checking for C header file no_std_c_header.h... no
Executing Custom Test ... ok
""")
    # first with SConscriptChdir(0)
    test.run(chdir=work_dir, stdout = required_stdout, arguments='chdir=no')
    checkFiles( test, [".sconf_temp/.cache", "config.log"] )
    checkLog( test, 'config.log', 0, 0 )

    test.run(chdir=work_dir, stdout = required_stdout, arguments='chdir=no')
    checkFiles( test, [".sconf_temp/.cache", "config.log"] )
    checkLog( test, 'config.log', 5, 1 )

    shutil.rmtree(test.workpath(work_dir, ".sconf_temp"))

    # now with SConscriptChdir(1)
    test.run(chdir=work_dir, stdout = required_stdout, arguments='chdir=yes')
    checkFiles( test, [".sconf_temp/.cache", "config.log"] )
    checkLog( test, 'config.log', 0, 0 )

    test.run(chdir=work_dir, stdout = required_stdout, arguments='chdir=yes')
    checkFiles( test, [".sconf_temp/.cache", "config.log"] )
    checkLog( test, 'config.log', 5, 1 )

    # 3.1 test custom tests
    reset()

    compileOK = '#include <stdio.h>\\nint main() {printf("Hello");return 0;}'
    compileFAIL = "syntax error"
    linkOK = compileOK
    linkFAIL = "void myFunc(); int main() { myFunc(); }"
    runOK = compileOK
    runFAIL = "int main() { return 1; }"
    test.write([work_dir, 'pyAct.py'], 'import sys\nprint sys.argv[1]\nsys.exit(int(sys.argv[1]))\n')
    test.write([work_dir, 'SConstruct'], """
def CheckCustom(test):
    test.Message( 'Executing MyTest ... ' )
    retCompileOK = test.TryCompile( '%s', '.c' )
    retCompileFAIL = test.TryCompile( '%s', '.c' )
    retLinkOK = test.TryLink( '%s', '.c' )
    retLinkFAIL = test.TryLink( '%s', '.c' )
    (retRunOK, outputRunOK) = test.TryRun( '%s', '.c' )
    (retRunFAIL, outputRunFAIL) = test.TryRun( '%s', '.c' )
    (retActOK, outputActOK) = test.TryAction( '%s pyAct.py 0 > $TARGET' )
    (retActFAIL, outputActFAIL) = test.TryAction( '%s pyAct.py 1 > $TARGET' )
    resOK = retCompileOK and retLinkOK and retRunOK and outputRunOK=="Hello"
    resOK = resOK and retActOK and int(outputActOK)==0
    resFAIL = retCompileFAIL or retLinkFAIL or retRunFAIL or outputRunFAIL!=""
    resFAIL = resFAIL or retActFAIL or outputActFAIL!=""
    test.Result( int(resOK and not resFAIL) )
    return resOK and not resFAIL

env = Environment()
import os
env.AppendENVPath('PATH', os.environ['PATH'])
conf = Configure( env, custom_tests={'CheckCustom' : CheckCustom} )
conf.CheckCustom()
env = conf.Finish()
""" % (compileOK, compileFAIL, linkOK, linkFAIL, runOK, runFAIL,
       python, python ) )
    required_stdout = test.wrap_stdout(build_str='.*',
                                       read_str="Executing MyTest ... ok\n")
    test.run(chdir=work_dir, stdout = required_stdout)
    checkLog( test, 'config.log', 0, 0 )

    test.run(chdir=work_dir, stdout = required_stdout)
    checkLog( test, 'config.log', 12, 4 )

    # 4.1 test that calling normal builders from an actual configuring
    # environment works
    reset()

    test.write([work_dir, 'cmd.py'], r"""
import sys
sys.stderr.write( 'Hello World on stderr\n' )
sys.stdout.write( 'Hello World on stdout\n' )
open(sys.argv[1], 'w').write( 'Hello World\n' )
""")

    test.write([work_dir, 'SConstruct'], """
env = Environment()
def CustomTest(*args):
    return 0
conf = env.Configure(custom_tests = {'MyTest' : CustomTest})
if not conf.MyTest():
    env.Command("hello", [], "%s cmd.py $TARGET")
env = conf.Finish()
""" % python)
    test.run(chdir=work_dir, stderr="Hello World on stderr\n")

    # 4.2 test that calling Configure from a builder results in a
    # readable Error
    reset(match=2)

    test.write([work_dir, 'SConstruct'], """
def ConfigureAction(target, source, env):
    env.Configure()
    return 0
env = Environment(BUILDERS = {'MyAction' :
                              Builder(action=Action(ConfigureAction))})
env.MyAction('target', [])
""")
    test.run(chdir=work_dir, status=2,
             stderr="scons: *** Calling Configure from Builders is not supported.\n")

    # 4.3 test the calling Configure from multiple subsidiary,
    # nested SConscript files does *not* result in an error.

    test.subdir([work_dir, 'dir1'],
                [work_dir, 'dir2'],
                [work_dir, 'dir2', 'sub1'],
                [work_dir, 'dir2', 'sub1', 'sub2'])
    test.write([work_dir, 'SConstruct'], """
env = Environment()
SConscript(dirs=['dir1', 'dir2'], exports="env")
""")
    test.write([work_dir, 'dir1', 'SConscript'], """
Import("env")
conf = env.Configure()
conf.Finish()
""")
    test.write([work_dir, 'dir2', 'SConscript'], """
Import("env")
conf = env.Configure()
conf.Finish()
SConscript(dirs=['sub1'], exports="env")
""")
    test.write([work_dir, 'dir2', 'sub1', 'SConscript'], """
Import("env")
conf = env.Configure()
conf.Finish()
SConscript(dirs=['sub2'], exports="env")
""")
    test.write([work_dir, 'dir2', 'sub1', 'sub2', 'SConscript'], """
Import("env")
conf = env.Configure()
conf.Finish()
""")
    test.run(chdir=work_dir)

    test.pass_test()

finally:
    pass
    #os.system( 'find . -type f -exec ls -l {} \;' )
    #print "-------------config.log------------------"
    #print test.read( test.workpath('config.log' ))
    #print "-------------build/config.log------------"
    #print test.read( test.workpath('build/config.log' ))