summaryrefslogtreecommitdiffstats
path: root/test/QTFLAGS.py
blob: 7964d4437ab2580a4ecc59066ca3849bd0c256e0 (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
#!/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__"

"""
Testing the configuration mechanisms of the 'qt' tool.
"""

import TestSCons
import os.path

python = TestSCons.python
_exe = TestSCons._exe

test = TestSCons.TestSCons()

test.subdir( 'qt', ['qt', 'bin'], ['qt', 'include'], ['qt', 'lib'] )

# create a dummy qt installation

test.write(['qt', 'bin', 'mymoc.py'], """
import getopt
import sys
import string
import re
cmd_opts, args = getopt.getopt(sys.argv[1:], 'wzio:', [])
output = None
impl = 0
opt_string = ''
for opt, arg in cmd_opts:
    if opt == '-o': output = open(arg, 'wb')
    elif opt == '-i': impl = 1
    else: opt_string = opt_string + ' ' + opt
output.write( "/* mymoc.py%s */\\n" % opt_string)
for a in args:
    contents = open(a, 'rb').read()
    subst = r'{ my_qt_symbol( "' + a + '\\\\n" ); }'
    if impl:
        contents = re.sub( r'#include.*', '', contents )
    output.write(string.replace(contents, 'Q_OBJECT', subst))
output.close()
sys.exit(0)
""" )

test.write(['qt', 'bin', 'myuic.py'], """
import sys
import string
output_arg = 0
impl_arg = 0
impl = None
source = None
opt_string = ''
for arg in sys.argv[1:]:
    if output_arg:
        output = open(arg, 'wb')
        output_arg = 0
    elif impl_arg:
        impl = arg
        impl_arg = 0
    elif arg == "-o":
        output_arg = 1
    elif arg == "-impl":
        impl_arg = 1
    elif arg[0:1] == "-":
        opt_string = opt_string + ' ' + arg
    else:
        if source:
            sys.exit(1)
        source = open(arg, 'rb')
output.write("/* myuic.py%s */\\n" % opt_string)
if impl:
    output.write( '#include "' + impl + '"\\n' )
else:
    output.write( '#include "my_qobject.h"\\n' + source.read() + " Q_OBJECT \\n" )
output.close()
sys.exit(0)
""" )

test.write(['qt', 'include', 'my_qobject.h'], r"""
#define Q_OBJECT ;
void my_qt_symbol(const char *arg);
""")

test.write(['qt', 'lib', 'my_qobject.cpp'], r"""
#include "../include/my_qobject.h"
#include <stdio.h>
void my_qt_symbol(const char *arg) {
  printf( arg );
}
""")

test.write(['qt', 'lib', 'SConstruct'], r"""
env = Environment()
env.StaticLibrary( 'myqt', 'my_qobject.cpp' )
""")

test.run(chdir=test.workpath('qt','lib'), arguments = '.')

QT = test.workpath('qt')
QT_LIB = 'myqt'
QT_MOC = '%s %s' % (python, test.workpath('qt','bin','mymoc.py'))
QT_UIC = '%s %s' % (python, test.workpath('qt','bin','myuic.py'))

# 3 test cases with 3 different operation modes

def createSConstruct(test,place,overrides):
    test.write(place, """
env = Environment(QTDIR = r'%s',
                  QT_LIB = r'%s',
                  QT_MOC = r'%s',
                  QT_UIC = r'%s',
                  %s
                  tools=['default','qt'])
if ARGUMENTS.get('build_dir', 0):
    if ARGUMENTS.get('chdir', 0):
        SConscriptChdir(1)
    else:
        SConscriptChdir(0)
    BuildDir('build', '.', duplicate=1)
    sconscript = Dir('build').File('SConscript')
else:
    sconscript = File('SConscript')
Export("env")
SConscript( sconscript )
""" % (QT, QT_LIB, QT_MOC, QT_UIC, overrides))


createSConstruct(test, ['SConstruct'],
                 """QT_UICIMPLFLAGS='-x',
                    QT_UICDECLFLAGS='-y',
                    QT_MOCFROMHFLAGS='-z',
                    QT_MOCFROMCXXFLAGS='-i -w',
                    QT_HSUFFIX='.hpp',
                    QT_MOCNAMEGENERATOR=lambda x,src_suffix,env: x + '.moc.cpp',
                    QT_UISUFFIX='.des',
                    QT_UIHSUFFUX='.des.hpp',
                    CXXFILESUFFIX='.cpp',""")
test.write('SConscript',"""
Import("env")
env.Program('mytest', ['mocFromH.cpp',
                       'mocFromCpp.cpp',
                       'an_ui_file.des',
                       'another_ui_file.des',
                       'main.cpp'])
""")

test.write('mocFromH.hpp', """
#include "my_qobject.h"
void mocFromH() Q_OBJECT
""")

test.write('mocFromH.cpp', """
#include "mocFromH.hpp"
""")

test.write('mocFromCpp.cpp', """
#include "my_qobject.h"
void mocFromCpp() Q_OBJECT
#include "mocFromCpp.moc.cpp"
""")

test.write('an_ui_file.des', """
void an_ui_file()
""")

test.write('another_ui_file.des', """
void another_ui_file()
""")

test.write('another_ui_file.desc.hpp', """
/* just a dependency checker */
""")

test.write('main.cpp', """
#include "mocFromH.hpp"
#include "an_ui_file.hpp"
#include "another_ui_file.hpp"
void mocFromCpp();

int main() {
  mocFromH();
  mocFromCpp();
  an_ui_file();
  another_ui_file();
}
""")

test.run( arguments = "mytest" + _exe )

def _existAll( test, files ):
    return reduce(lambda x,y: x and y,
                  map(os.path.exists,map(test.workpath, files)))
                       
test.fail_test(not _existAll(test, ['mocFromH.moc.cpp',
                                    'mocFromCpp.moc.cpp',
                                    'an_ui_file.cpp',
                                    'an_ui_file.hpp',
                                    'an_ui_file.moc.cpp',
                                    'another_ui_file.cpp',
                                    'another_ui_file.hpp',
                                    'another_ui_file.moc.cpp']))

def _flagTest(test,fileToContentsStart):
    import string
    for f,c in fileToContentsStart.items():
        if string.find(test.read(f), c) != 0:
            return 1
    return 0

test.fail_test(_flagTest(test, {'mocFromH.moc.cpp':'/* mymoc.py -z */',
                                'mocFromCpp.moc.cpp':'/* mymoc.py -w */',
                                'an_ui_file.cpp':'/* myuic.py -x */',
                                'an_ui_file.hpp':'/* myuic.py -y */',
                                'an_ui_file.moc.cpp':'/* mymoc.py -z */'}))

test.pass_test()