-
Notifications
You must be signed in to change notification settings - Fork 5
/
__init__.py
1013 lines (907 loc) · 39.2 KB
/
__init__.py
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""SCons.Tool.qt5
Tool-specific initialization for Qt5.
There normally shouldn't be any need to import this module directly.
It will usually be imported through the generic SCons.Tool.Tool()
selection method.
"""
#
# Copyright (c) 2001-7,2010,2011,2012 The SCons Foundation
#
# 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.
#
import os.path
import re
import subprocess
import SCons.Action
import SCons.Builder
import SCons.Defaults
import SCons.Scanner
import SCons.Tool
import SCons.Util
class ToolQt5Warning(SCons.Warnings.Warning):
pass
class GeneratedMocFileNotIncluded(ToolQt5Warning):
pass
class QtdirNotFound(ToolQt5Warning):
pass
SCons.Warnings.enableWarningClass(ToolQt5Warning)
try:
sorted
except NameError:
# Pre-2.4 Python has no sorted() function.
#
# The pre-2.4 Python list.sort() method does not support
# list.sort(key=) nor list.sort(reverse=) keyword arguments, so
# we must implement the functionality of those keyword arguments
# by hand instead of passing them to list.sort().
def sorted(iterable, cmp=None, key=None, reverse=0):
if key is not None:
result = [(key(x), x) for x in iterable]
else:
result = iterable[:]
if cmp is None:
# Pre-2.3 Python does not support list.sort(None).
result.sort()
else:
result.sort(cmp)
if key is not None:
result = [t1 for t0,t1 in result]
if reverse:
result.reverse()
return result
def _contents_regex(e):
# get_contents() of scons nodes returns a binary buffer, so we convert the regexes also to binary here
# this won't work for specific encodings like UTF-16, but most of the time we will be fine here.
# note that the regexes used here are always pure ascii, so we don't have an issue here.
e = e.encode('ascii')
return e
qrcinclude_re = re.compile(r'<file[^>]*>([^<]*)</file>', re.M)
mocver_re = re.compile(_contents_regex(r'.*(\d+)\.(\d+)\.(\d+).*'))
def transformToWinePath(path) :
pipe = subprocess.Popen('winepath -w "%s"'%path, shell=True, stdout=PIPE).stdout
return pipe.read().strip().replace('\\','/')
header_extensions = [".h", ".hxx", ".hpp", ".hh"]
if SCons.Util.case_sensitive_suffixes('.h', '.H'):
header_extensions.append('.H')
# TODO: The following two lines will work when integrated back to SCons
# TODO: Meanwhile the third line will do the work
#cplusplus = __import__('c++', globals(), locals(), [])
#cxx_suffixes = cplusplus.CXXSuffixes
cxx_suffixes = [".c", ".cxx", ".cpp", ".cc"]
def checkMocIncluded(target, source, env):
moc = target[0]
cpp = source[0]
# looks like cpp.includes is cleared before the build stage :-(
# not really sure about the path transformations (moc.cwd? cpp.cwd?) :-/
path = SCons.Defaults.CScan.path_function(env, moc.cwd)
includes = SCons.Defaults.CScan(cpp, env, path)
if not moc in includes:
SCons.Warnings.warn(
GeneratedMocFileNotIncluded,
"Generated moc file '%s' is not included by '%s'" %
(str(moc), str(cpp)))
def find_file(filename, paths, node_factory):
for dir in paths:
node = node_factory(filename, dir)
if node.rexists():
return node
return None
class _Automoc:
"""
Callable class, which works as an emitter for Programs, SharedLibraries and
StaticLibraries.
"""
def __init__(self, objBuilderName):
self.objBuilderName = objBuilderName
# some regular expressions:
# Q_OBJECT detection
self.qo_search = re.compile(_contents_regex(r'[^A-Za-z0-9]Q_OBJECT[^A-Za-z0-9]'))
# cxx and c comment 'eater'
self.ccomment = re.compile(_contents_regex(r'/\*(.*?)\*/'),re.S)
self.cxxcomment = re.compile(_contents_regex(r'//.*$'),re.M)
# we also allow Q_OBJECT in a literal string
self.literal_qobject = re.compile(_contents_regex(r'"[^\n]*Q_OBJECT[^\n]*"'))
def create_automoc_options(self, env):
"""
Create a dictionary with variables related to Automocing,
based on the current environment.
Is executed once in the __call__ routine.
"""
moc_options = {'auto_scan' : True,
'auto_scan_strategy' : 0,
'gobble_comments' : 0,
'debug' : 0,
'auto_cpppath' : True,
'cpppaths' : []}
try:
if int(env.subst('$QT5_AUTOSCAN')) == 0:
moc_options['auto_scan'] = False
except ValueError:
pass
try:
moc_options['auto_scan_strategy'] = int(env.subst('$QT5_AUTOSCAN_STRATEGY'))
except ValueError:
pass
try:
moc_options['gobble_comments'] = int(env.subst('$QT5_GOBBLECOMMENTS'))
except ValueError:
pass
try:
moc_options['debug'] = int(env.subst('$QT5_DEBUG'))
except ValueError:
pass
try:
if int(env.subst('$QT5_AUTOMOC_SCANCPPPATH')) == 0:
moc_options['auto_cpppath'] = False
except ValueError:
pass
if moc_options['auto_cpppath']:
paths = env.get('QT5_AUTOMOC_CPPPATH', [])
if not paths:
paths = env.get('CPPPATH', [])
moc_options['cpppaths'].extend(paths)
return moc_options
def __automoc_strategy_simple(self, env, moc_options,
cpp, cpp_contents, out_sources):
"""
Default Automoc strategy (Q_OBJECT driven): detect a header file
(alongside the current cpp/cxx) that contains a Q_OBJECT
macro...and MOC it.
If a Q_OBJECT macro is also found in the cpp/cxx itself,
it gets MOCed too.
"""
h=None
for h_ext in header_extensions:
# try to find the header file in the corresponding source
# directory
hname = self.splitext(cpp.name)[0] + h_ext
h = find_file(hname, [cpp.get_dir()]+moc_options['cpppaths'], env.File)
if h:
if moc_options['debug']:
print("scons: qt5: Scanning '%s' (header of '%s')" % (str(h), str(cpp)))
h_contents = h.get_contents()
if moc_options['gobble_comments']:
h_contents = self.ccomment.sub(_contents_regex(''), h_contents)
h_contents = self.cxxcomment.sub(_contents_regex(''), h_contents)
h_contents = self.literal_qobject.sub(_contents_regex('""'), h_contents)
break
if not h and moc_options['debug']:
print("scons: qt5: no header for '%s'." % (str(cpp)))
if h and self.qo_search.search(h_contents):
# h file with the Q_OBJECT macro found -> add moc_cpp
moc_cpp = env.Moc5(h)
if moc_options['debug']:
print("scons: qt5: found Q_OBJECT macro in '%s', moc'ing to '%s'" % (str(h), str(moc_cpp)))
# Now, check whether the corresponding CPP file
# includes the moc'ed output directly...
inc_moc_cpp = _contents_regex(r'^\s*#\s*include\s+"%s"' % str(moc_cpp[0]))
if cpp and re.search(inc_moc_cpp, cpp_contents, re.M):
if moc_options['debug']:
print("scons: qt5: CXX file '%s' directly includes the moc'ed output '%s', no compiling required" % (str(cpp), str(moc_cpp)))
env.Depends(cpp, moc_cpp)
else:
moc_o = self.objBuilder(moc_cpp)
if moc_options['debug']:
print("scons: qt5: compiling '%s' to '%s'" % (str(cpp), str(moc_o)))
out_sources.extend(moc_o)
if cpp and self.qo_search.search(cpp_contents):
# cpp file with Q_OBJECT macro found -> add moc
# (to be included in cpp)
moc = env.Moc5(cpp)
env.Ignore(moc, moc)
if moc_options['debug']:
print("scons: qt5: found Q_OBJECT macro in '%s', moc'ing to '%s'" % (str(cpp), str(moc)))
def __automoc_strategy_include_driven(self, env, moc_options,
cpp, cpp_contents, out_sources):
"""
Automoc strategy #1 (include driven): searches for "include"
statements of MOCed files in the current cpp/cxx file.
This strategy tries to add support for the compilation
of the qtsolutions...
"""
if self.splitext(str(cpp))[1] in cxx_suffixes:
added = False
h_moc = "%s%s%s" % (env.subst('$QT5_XMOCHPREFIX'),
self.splitext(cpp.name)[0],
env.subst('$QT5_XMOCHSUFFIX'))
cxx_moc = "%s%s%s" % (env.subst('$QT5_XMOCCXXPREFIX'),
self.splitext(cpp.name)[0],
env.subst('$QT5_XMOCCXXSUFFIX'))
inc_h_moc = _contents_regex(r'#include\s+"%s"' % h_moc)
inc_cxx_moc = _contents_regex(r'#include\s+"%s"' % cxx_moc)
# Search for special includes in qtsolutions style
if cpp and re.search(inc_h_moc, cpp_contents):
# cpp file with #include directive for a MOCed header found -> add moc
# Try to find header file
h=None
hname=""
for h_ext in header_extensions:
# Try to find the header file in the
# corresponding source directory
hname = self.splitext(cpp.name)[0] + h_ext
h = find_file(hname, [cpp.get_dir()]+moc_options['cpppaths'], env.File)
if h:
if moc_options['debug']:
print("scons: qt5: Scanning '%s' (header of '%s')" % (str(h), str(cpp)))
h_contents = h.get_contents()
if moc_options['gobble_comments']:
h_contents = self.ccomment.sub('', h_contents)
h_contents = self.cxxcomment.sub('', h_contents)
h_contents = self.literal_qobject.sub('""', h_contents)
break
if not h and moc_options['debug']:
print("scons: qt5: no header for '%s'." % (str(cpp)))
if h and self.qo_search.search(h_contents):
# h file with the Q_OBJECT macro found -> add moc_cpp
moc_cpp = env.XMoc5(h)
env.Ignore(moc_cpp, moc_cpp)
added = True
# Removing file from list of sources, because it is not to be
# compiled but simply included by the cpp/cxx file.
for idx, s in enumerate(out_sources):
if hasattr(s, "sources") and len(s.sources) > 0:
if str(s.sources[0]) == h_moc:
out_sources.pop(idx)
break
if moc_options['debug']:
print("scons: qt5: found Q_OBJECT macro in '%s', moc'ing to '%s'" % (str(h), str(h_moc)))
else:
if moc_options['debug']:
print("scons: qt5: found no Q_OBJECT macro in '%s', but a moc'ed version '%s' gets included in '%s'" % (str(h), inc_h_moc, cpp.name))
if cpp and re.search(inc_cxx_moc, cpp_contents):
# cpp file with #include directive for a MOCed cxx file found -> add moc
if self.qo_search.search(cpp_contents):
moc = env.XMoc5(target=cxx_moc, source=cpp)
env.Ignore(moc, moc)
added = True
if moc_options['debug']:
print("scons: qt5: found Q_OBJECT macro in '%s', moc'ing to '%s'" % (str(cpp), str(moc)))
else:
if moc_options['debug']:
print("scons: qt5: found no Q_OBJECT macro in '%s', although a moc'ed version '%s' of itself gets included" % (cpp.name, inc_cxx_moc))
if not added:
# Fallback to default Automoc strategy (Q_OBJECT driven)
self.__automoc_strategy_simple(env, moc_options, cpp,
cpp_contents, out_sources)
def __call__(self, target, source, env):
"""
Smart autoscan function. Gets the list of objects for the Program
or Lib. Adds objects and builders for the special qt5 files.
"""
moc_options = self.create_automoc_options(env)
# some shortcuts used in the scanner
self.splitext = SCons.Util.splitext
self.objBuilder = getattr(env, self.objBuilderName)
# The following is kind of hacky to get builders working properly (FIXME)
objBuilderEnv = self.objBuilder.env
self.objBuilder.env = env
mocBuilderEnv = env.Moc5.env
env.Moc5.env = env
xMocBuilderEnv = env.XMoc5.env
env.XMoc5.env = env
# make a deep copy for the result; MocH objects will be appended
out_sources = source[:]
for obj in source:
if not moc_options['auto_scan']:
break
if isinstance(obj,str): # big kludge!
print("scons: qt5: '%s' MAYBE USING AN OLD SCONS VERSION AND NOT CONVERTED TO 'File'. Discarded." % str(obj))
continue
if not obj.has_builder():
# binary obj file provided
if moc_options['debug']:
print("scons: qt5: '%s' seems to be a binary. Discarded." % str(obj))
continue
cpp = obj.sources[0]
if not self.splitext(str(cpp))[1] in cxx_suffixes:
if moc_options['debug']:
print("scons: qt5: '%s' is no cxx file. Discarded." % str(cpp))
# c or fortran source
continue
try:
cpp_contents = cpp.get_contents()
if moc_options['gobble_comments']:
cpp_contents = self.ccomment.sub('', cpp_contents)
cpp_contents = self.cxxcomment.sub('', cpp_contents)
cpp_contents = self.literal_qobject.sub('""', cpp_contents)
except: continue # may be an still not generated source
if moc_options['auto_scan_strategy'] == 0:
# Default Automoc strategy (Q_OBJECT driven)
self.__automoc_strategy_simple(env, moc_options,
cpp, cpp_contents, out_sources)
else:
# Automoc strategy #1 (include driven)
self.__automoc_strategy_include_driven(env, moc_options,
cpp, cpp_contents, out_sources)
# restore the original env attributes (FIXME)
self.objBuilder.env = objBuilderEnv
env.Moc5.env = mocBuilderEnv
env.XMoc5.env = xMocBuilderEnv
# We return the set of source entries as sorted sequence, else
# the order might accidentally change from one build to another
# and trigger unwanted rebuilds. For proper sorting, a key function
# has to be specified...FS.Entry (and Base nodes in general) do not
# provide a __cmp__, for performance reasons.
return (target, sorted(set(out_sources), key=lambda entry : str(entry)))
AutomocShared = _Automoc('SharedObject')
AutomocStatic = _Automoc('StaticObject')
def _detect(env):
"""Not really safe, but fast method to detect the Qt5 library"""
try: return env['QT5DIR']
except KeyError: pass
try: return env['QTDIR']
except KeyError: pass
try: return os.environ['QT5DIR']
except KeyError: pass
try: return os.environ['QTDIR']
except KeyError: pass
moc = env.WhereIs('moc-qt5') or env.WhereIs('moc5') or env.WhereIs('moc')
if moc:
pipes = subprocess.Popen('%s -v' % moc,
shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=True)
vernumber = pipes.stdout.read()
vernumber = mocver_re.match(vernumber)
if vernumber:
vernumber = [ int(x) for x in vernumber.groups() ]
if vernumber < [5, 0, 0]:
vernumber = '.'.join([str(x) for x in vernumber])
moc = None
SCons.Warnings.warn(
QtdirNotFound,
"QT5DIR variable not defined, and detected moc is for Qt %s" % vernumber)
QT5DIR = os.path.dirname(os.path.dirname(moc))
SCons.Warnings.warn(
QtdirNotFound,
"QT5DIR variable is not defined, using moc executable as a hint (QT5DIR=%s)" % QT5DIR)
return QT5DIR
raise SCons.Errors.StopError(
QtdirNotFound,
"Could not detect Qt 5 installation")
return None
def __scanResources(node, env, path, arg):
# Helper function for scanning .qrc resource files
# I've been careful on providing names relative to the qrc file
# If that was not needed this code could be simplified a lot
def recursiveFiles(basepath, path) :
result = []
for item in os.listdir(os.path.join(basepath, path)) :
itemPath = os.path.join(path, item)
if os.path.isdir(os.path.join(basepath, itemPath)) :
result += recursiveFiles(basepath, itemPath)
else:
result.append(itemPath)
return result
# we assume the default xml encoding (utf-8) here
contents = node.get_contents().decode('utf-8')
includes = qrcinclude_re.findall(contents)
qrcpath = os.path.dirname(node.path)
dirs = [included for included in includes if os.path.isdir(os.path.join(qrcpath,included))]
# dirs need to include files recursively
for dir in dirs :
includes.remove(dir)
includes+=recursiveFiles(qrcpath,dir)
return includes
#
# Scanners
#
__qrcscanner = SCons.Scanner.Scanner(name = 'qrcfile',
function = __scanResources,
argument = None,
skeys = ['.qrc'])
#
# Emitters
#
def __qrc_path(head, prefix, tail, suffix):
if head:
if tail:
return os.path.join(head, "%s%s%s" % (prefix, tail, suffix))
else:
return "%s%s%s" % (prefix, head, suffix)
else:
return "%s%s%s" % (prefix, tail, suffix)
def __qrc_emitter(target, source, env):
sourceBase, sourceExt = os.path.splitext(SCons.Util.to_String(source[0]))
sHead = None
sTail = sourceBase
if sourceBase:
sHead, sTail = os.path.split(sourceBase)
t = __qrc_path(sHead, env.subst('$QT5_QRCCXXPREFIX'),
sTail, env.subst('$QT5_QRCCXXSUFFIX'))
return t, source
#
# Action generators
#
def __moc_generator_from_h(source, target, env, for_signature):
pass_defines = False
try:
if int(env.subst('$QT5_CPPDEFINES_PASSTOMOC')) == 1:
pass_defines = True
except ValueError:
pass
if pass_defines:
return '$QT5_MOC $QT5_MOCDEFINES $QT5_MOCFROMHFLAGS $QT5_MOCINCFLAGS -o $TARGET $SOURCE'
else:
return '$QT5_MOC $QT5_MOCFROMHFLAGS $QT5_MOCINCFLAGS -o $TARGET $SOURCE'
def __moc_generator_from_cxx(source, target, env, for_signature):
pass_defines = False
try:
if int(env.subst('$QT5_CPPDEFINES_PASSTOMOC')) == 1:
pass_defines = True
except ValueError:
pass
if pass_defines:
return ['$QT5_MOC $QT5_MOCDEFINES $QT5_MOCFROMCXXFLAGS $QT5_MOCINCFLAGS -o $TARGET $SOURCE',
SCons.Action.Action(checkMocIncluded,None)]
else:
return ['$QT5_MOC $QT5_MOCFROMCXXFLAGS $QT5_MOCINCFLAGS -o $TARGET $SOURCE',
SCons.Action.Action(checkMocIncluded,None)]
def __mocx_generator_from_h(source, target, env, for_signature):
pass_defines = False
try:
if int(env.subst('$QT5_CPPDEFINES_PASSTOMOC')) == 1:
pass_defines = True
except ValueError:
pass
if pass_defines:
return '$QT5_MOC $QT5_MOCDEFINES $QT5_MOCFROMHFLAGS $QT5_MOCINCFLAGS -o $TARGET $SOURCE'
else:
return '$QT5_MOC $QT5_MOCFROMHFLAGS $QT5_MOCINCFLAGS -o $TARGET $SOURCE'
def __mocx_generator_from_cxx(source, target, env, for_signature):
pass_defines = False
try:
if int(env.subst('$QT5_CPPDEFINES_PASSTOMOC')) == 1:
pass_defines = True
except ValueError:
pass
if pass_defines:
return ['$QT5_MOC $QT5_MOCDEFINES $QT5_MOCFROMCXXFLAGS $QT5_MOCINCFLAGS -o $TARGET $SOURCE',
SCons.Action.Action(checkMocIncluded,None)]
else:
return ['$QT5_MOC $QT5_MOCFROMCXXFLAGS $QT5_MOCINCFLAGS -o $TARGET $SOURCE',
SCons.Action.Action(checkMocIncluded,None)]
def __qrc_generator(source, target, env, for_signature):
name_defined = False
try:
if env.subst('$QT5_QRCFLAGS').find('-name') >= 0:
name_defined = True
except ValueError:
pass
if name_defined:
return '$QT5_RCC $QT5_QRCFLAGS $SOURCE -o $TARGET'
else:
qrc_suffix = env.subst('$QT5_QRCSUFFIX')
src = str(source[0])
head, tail = os.path.split(src)
if tail:
src = tail
qrc_suffix = env.subst('$QT5_QRCSUFFIX')
if src.endswith(qrc_suffix):
qrc_stem = src[:-len(qrc_suffix)]
else:
qrc_stem = src
return '$QT5_RCC $QT5_QRCFLAGS -name %s $SOURCE -o $TARGET' % qrc_stem
#
# Builders
#
__ts_builder = SCons.Builder.Builder(
action = SCons.Action.Action('$QT5_LUPDATECOM','$QT5_LUPDATECOMSTR'),
suffix = '.ts',
source_factory = SCons.Node.FS.Entry)
__qm_builder = SCons.Builder.Builder(
action = SCons.Action.Action('$QT5_LRELEASECOM','$QT5_LRELEASECOMSTR'),
src_suffix = '.ts',
suffix = '.qm')
__qrc_builder = SCons.Builder.Builder(
action = SCons.Action.CommandGeneratorAction(__qrc_generator,
{'cmdstr':'$QT5_QRCCOMSTR'}),
source_scanner = __qrcscanner,
src_suffix = '$QT5_QRCSUFFIX',
suffix = '$QT5_QRCCXXSUFFIX',
prefix = '$QT5_QRCCXXPREFIX',
single_source = 1)
__ex_moc_builder = SCons.Builder.Builder(
action = SCons.Action.CommandGeneratorAction(__moc_generator_from_h,
{'cmdstr':'$QT5_MOCFROMHCOMSTR'}))
__ex_uic_builder = SCons.Builder.Builder(
action = SCons.Action.Action('$QT5_UICCOM', '$QT5_UICCOMSTR'),
src_suffix = '.ui')
#
# Wrappers (pseudo-Builders)
#
def Ts5(env, target, source=None, *args, **kw):
"""
A pseudo-Builder wrapper around the LUPDATE executable of Qt5.
lupdate [options] [source-file|path]... -ts ts-files
"""
if not SCons.Util.is_List(target):
target = [target]
if not source:
source = target[:]
if not SCons.Util.is_List(source):
source = [source]
# Check QT5_CLEAN_TS and use NoClean() function
clean_ts = False
try:
if int(env.subst('$QT5_CLEAN_TS')) == 1:
clean_ts = True
except ValueError:
pass
result = []
for t in target:
obj = __ts_builder.__call__(env, t, source, **kw)
# Prevent deletion of the .ts file, unless explicitly specified
if not clean_ts:
env.NoClean(obj)
# Always make our target "precious", such that it is not deleted
# prior to a rebuild
env.Precious(obj)
# Add to resulting target list
result.extend(obj)
return result
def Qm5(env, target, source=None, *args, **kw):
"""
A pseudo-Builder wrapper around the LRELEASE executable of Qt5.
lrelease [options] ts-files [-qm qm-file]
"""
if not SCons.Util.is_List(target):
target = [target]
if not source:
source = target[:]
if not SCons.Util.is_List(source):
source = [source]
result = []
for t in target:
result.extend(__qm_builder.__call__(env, t, source, **kw))
return result
def Qrc5(env, target, source=None, *args, **kw):
"""
A pseudo-Builder wrapper around the RCC executable of Qt5.
rcc [options] qrc-files -o out-file
"""
if not SCons.Util.is_List(target):
target = [target]
if not source:
source = target[:]
if not SCons.Util.is_List(source):
source = [source]
result = []
for t, s in zip(target, source):
result.extend(__qrc_builder.__call__(env, t, s, **kw))
return result
def ExplicitMoc5(env, target, source, *args, **kw):
"""
A pseudo-Builder wrapper around the MOC executable of Qt5.
moc [options] <header-file>
"""
if not SCons.Util.is_List(target):
target = [target]
if not SCons.Util.is_List(source):
source = [source]
result = []
for t in target:
# Is it a header or a cxx file?
result.extend(__ex_moc_builder.__call__(env, t, source, **kw))
return result
def ExplicitUic5(env, target, source, *args, **kw):
"""
A pseudo-Builder wrapper around the UIC executable of Qt5.
uic [options] <uifile>
"""
if not SCons.Util.is_List(target):
target = [target]
if not SCons.Util.is_List(source):
source = [source]
result = []
for t in target:
result.extend(__ex_uic_builder.__call__(env, t, source, **kw))
return result
def generate(env):
"""Add Builders and construction variables for qt5 to an Environment."""
suffixes = [
'-qt5',
'-qt5.exe',
'5',
'5.exe',
'',
'.exe',
]
command_suffixes = ['-qt5', '5', '']
def locateQt5Command(env, command, qtdir) :
triedPaths = []
for suffix in suffixes :
fullpath = os.path.join(qtdir,'bin',command + suffix)
if os.access(fullpath, os.X_OK) :
return fullpath
triedPaths.append(fullpath)
fullpath = env.Detect([command+s for s in command_suffixes])
if not (fullpath is None) : return fullpath
raise Exception("Qt5 command '" + command + "' not found. Tried: " + ', '.join(triedPaths))
CLVar = SCons.Util.CLVar
Action = SCons.Action.Action
Builder = SCons.Builder.Builder
env['QT5DIR'] = _detect(env)
# TODO: 'Replace' should be 'SetDefault'
# env.SetDefault(
env.Replace(
QT5DIR = _detect(env),
QT5_BINPATH = os.path.join('$QT5DIR', 'bin'),
# TODO: This is not reliable to QT5DIR value changes but needed in order to support '-qt5' variants
QT5_MOC = locateQt5Command(env,'moc', env['QT5DIR']),
QT5_UIC = locateQt5Command(env,'uic', env['QT5DIR']),
QT5_RCC = locateQt5Command(env,'rcc', env['QT5DIR']),
QT5_LUPDATE = locateQt5Command(env,'lupdate', env['QT5DIR']),
QT5_LRELEASE = locateQt5Command(env,'lrelease', env['QT5DIR']),
QT5_AUTOSCAN = 1, # Should the qt5 tool try to figure out, which sources are to be moc'ed?
QT5_AUTOSCAN_STRATEGY = 0, # While scanning for files to moc, should we search for includes in qtsolutions style?
QT5_GOBBLECOMMENTS = 0, # If set to 1, comments are removed before scanning cxx/h files.
QT5_CPPDEFINES_PASSTOMOC = 1, # If set to 1, all CPPDEFINES get passed to the moc executable.
QT5_CLEAN_TS = 0, # If set to 1, translation files (.ts) get cleaned on 'scons -c'
QT5_AUTOMOC_SCANCPPPATH = 1, # If set to 1, the CPPPATHs (or QT5_AUTOMOC_CPPPATH) get scanned for moc'able files
QT5_AUTOMOC_CPPPATH = [], # Alternative paths that get scanned for moc files
# Some Qt5 specific flags. I don't expect someone wants to
# manipulate those ...
QT5_UICFLAGS = CLVar(''),
QT5_MOCFROMHFLAGS = CLVar(''),
QT5_MOCFROMCXXFLAGS = CLVar('-i'),
QT5_QRCFLAGS = '',
QT5_LUPDATEFLAGS = '',
QT5_LRELEASEFLAGS = '',
# suffixes/prefixes for the headers / sources to generate
QT5_UISUFFIX = '.ui',
QT5_UICDECLPREFIX = 'ui_',
QT5_UICDECLSUFFIX = '.h',
QT5_MOCINCPREFIX = '-I',
QT5_MOCHPREFIX = 'moc_',
QT5_MOCHSUFFIX = '$CXXFILESUFFIX',
QT5_MOCCXXPREFIX = '',
QT5_MOCCXXSUFFIX = '.moc',
QT5_QRCSUFFIX = '.qrc',
QT5_QRCCXXSUFFIX = '$CXXFILESUFFIX',
QT5_QRCCXXPREFIX = 'qrc_',
QT5_MOCDEFPREFIX = '-D',
QT5_MOCDEFSUFFIX = '',
QT5_MOCDEFINES = '${_defines(QT5_MOCDEFPREFIX, CPPDEFINES, QT5_MOCDEFSUFFIX, __env__)}',
QT5_MOCCPPPATH = [],
QT5_MOCINCFLAGS = '$( ${_concat(QT5_MOCINCPREFIX, QT5_MOCCPPPATH, INCSUFFIX, __env__, RDirs)} $)',
# Commands for the qt5 support ...
QT5_UICCOM = '$QT5_UIC $QT5_UICFLAGS -o $TARGET $SOURCE',
QT5_LUPDATECOM = '$QT5_LUPDATE $QT5_LUPDATEFLAGS $SOURCES -ts $TARGET',
QT5_LRELEASECOM = '$QT5_LRELEASE $QT5_LRELEASEFLAGS -qm $TARGET $SOURCES',
# Specialized variables for the Extended Automoc support
# (Strategy #1 for qtsolutions)
QT5_XMOCHPREFIX = 'moc_',
QT5_XMOCHSUFFIX = '.cpp',
QT5_XMOCCXXPREFIX = '',
QT5_XMOCCXXSUFFIX = '.moc',
)
env.AddMethod(Ts5, "Ts5")
env.AddMethod(Qm5, "Qm5")
env.AddMethod(Qrc5, "Qrc5")
env.AddMethod(ExplicitMoc5, "ExplicitMoc5")
env.AddMethod(ExplicitUic5, "ExplicitUic5")
# Interface builder
uic5builder = Builder(
action = SCons.Action.Action('$QT5_UICCOM', '$QT5_UICCOMSTR'),
src_suffix='$QT5_UISUFFIX',
suffix='$QT5_UICDECLSUFFIX',
prefix='$QT5_UICDECLPREFIX',
single_source = True
#TODO: Consider the uiscanner on new scons version
)
env['BUILDERS']['Uic5'] = uic5builder
# Metaobject builder
mocBld = Builder(action={}, prefix={}, suffix={})
for h in header_extensions:
act = SCons.Action.CommandGeneratorAction(__moc_generator_from_h,
{'cmdstr':'$QT5_MOCFROMHCOMSTR'})
mocBld.add_action(h, act)
mocBld.prefix[h] = '$QT5_MOCHPREFIX'
mocBld.suffix[h] = '$QT5_MOCHSUFFIX'
for cxx in cxx_suffixes:
act = SCons.Action.CommandGeneratorAction(__moc_generator_from_cxx,
{'cmdstr':'$QT5_MOCFROMCXXCOMSTR'})
mocBld.add_action(cxx, act)
mocBld.prefix[cxx] = '$QT5_MOCCXXPREFIX'
mocBld.suffix[cxx] = '$QT5_MOCCXXSUFFIX'
env['BUILDERS']['Moc5'] = mocBld
# Metaobject builder for the extended auto scan feature
# (Strategy #1 for qtsolutions)
xMocBld = Builder(action={}, prefix={}, suffix={})
for h in header_extensions:
act = SCons.Action.CommandGeneratorAction(__mocx_generator_from_h,
{'cmdstr':'$QT5_MOCXFROMHCOMSTR'})
xMocBld.add_action(h, act)
xMocBld.prefix[h] = '$QT5_XMOCHPREFIX'
xMocBld.suffix[h] = '$QT5_XMOCHSUFFIX'
for cxx in cxx_suffixes:
act = SCons.Action.CommandGeneratorAction(__mocx_generator_from_cxx,
{'cmdstr':'$QT5_MOCXFROMCXXCOMSTR'})
xMocBld.add_action(cxx, act)
xMocBld.prefix[cxx] = '$QT5_XMOCCXXPREFIX'
xMocBld.suffix[cxx] = '$QT5_XMOCCXXSUFFIX'
env['BUILDERS']['XMoc5'] = xMocBld
# Add the Qrc5 action to the CXX file builder (registers the
# *.qrc extension with the Environment)
cfile_builder, cxxfile_builder = SCons.Tool.createCFileBuilders(env)
qrc_act = SCons.Action.CommandGeneratorAction(__qrc_generator,
{'cmdstr':'$QT5_QRCCOMSTR'})
cxxfile_builder.add_action('$QT5_QRCSUFFIX', qrc_act)
cxxfile_builder.add_emitter('$QT5_QRCSUFFIX', __qrc_emitter)
env.Append(SCANNERS=__qrcscanner)
# We use the emitters of Program / StaticLibrary / SharedLibrary
# to scan for moc'able files
# We can't refer to the builders directly, we have to fetch them
# as Environment attributes because that sets them up to be called
# correctly later by our emitter.
env.AppendUnique(PROGEMITTER =[AutomocStatic],
SHLIBEMITTER=[AutomocShared],
LIBEMITTER =[AutomocStatic],
)
# TODO: Does dbusxml2cpp need an adapter
env.AddMethod(enable_modules, "EnableQt5Modules")
def enable_modules(self, modules, debug=False, crosscompiling=False) :
import sys
validModules = [
# Qt Essentials
'QtCore',
'QtGui',
'QtMultimedia',
'QtMultimediaQuick_p',
'QtMultimediaWidgets',
'QtNetwork',
'QtPlatformSupport',
'QtQml',
'QtQmlDevTools',
'QtQuick',
'QtQuickParticles',
'QtSql',
'QtQuickTest',
'QtTest',
'QtWebKit',
'QtWebKitWidgets',
'QtWidgets',
# Qt Add-Ons
'QtConcurrent',
'QtDBus',
'QtOpenGL',
'QtPrintSupport',
'QtDeclarative',
'QtScript',
'QtScriptTools',
'QtSvg',
'QtUiTools',
'QtXml',
'QtXmlPatterns',
# Qt Tools
'QtHelp',
'QtDesigner',
'QtDesignerComponents',
# Other
'QtCLucene',
'QtConcurrent',
'QtV8'
]
pclessModules = [
]
staticModules = [
]
invalidModules=[]
for module in modules:
if module not in validModules :
invalidModules.append(module)
if invalidModules :
raise Exception("Modules %s are not Qt5 modules. Valid Qt5 modules are: %s"% (
str(invalidModules),str(validModules)))
moduleDefines = {
'QtScript' : ['QT_SCRIPT_LIB'],
'QtSvg' : ['QT_SVG_LIB'],
'QtSql' : ['QT_SQL_LIB'],
'QtXml' : ['QT_XML_LIB'],
'QtOpenGL' : ['QT_OPENGL_LIB'],
'QtGui' : ['QT_GUI_LIB'],
'QtNetwork' : ['QT_NETWORK_LIB'],
'QtCore' : ['QT_CORE_LIB'],
'QtWidgets' : ['QT_WIDGETS_LIB'],
}
for module in modules :
try : self.AppendUnique(CPPDEFINES=moduleDefines[module])
except: pass
debugSuffix = ''
if sys.platform in ["darwin", "linux2", "linux"] and not crosscompiling :
if debug : debugSuffix = '_debug'
for module in modules :
if module not in pclessModules : continue
self.AppendUnique(LIBS=[module.replace('Qt','Qt5')+debugSuffix])
self.AppendUnique(LIBPATH=[os.path.join("$QT5DIR","lib")])
self.AppendUnique(CPPPATH=[os.path.join("$QT5DIR","include")])
self.AppendUnique(CPPPATH=[os.path.join("$QT5DIR","include",module)])
pcmodules = [module.replace('Qt','Qt5')+debugSuffix for module in modules if module not in pclessModules ]
if 'Qt5DBus' in pcmodules:
self.AppendUnique(CPPPATH=[os.path.join("$QT5DIR","include","Qt5DBus")])
if "Qt5Assistant" in pcmodules:
self.AppendUnique(CPPPATH=[os.path.join("$QT5DIR","include","Qt5Assistant")])
pcmodules.remove("Qt5Assistant")
pcmodules.append("Qt5AssistantClient")
self.AppendUnique(RPATH=[os.path.join("$QT5DIR","lib")])
self.ParseConfig('pkg-config %s --libs --cflags'% ' '.join(pcmodules))
self["QT5_MOCCPPPATH"] = self["CPPPATH"]
return
if sys.platform == "win32" or crosscompiling :
if crosscompiling:
transformedQtdir = transformToWinePath(self['QT5DIR'])
self['QT5_MOC'] = "QT5DIR=%s %s"%( transformedQtdir, self['QT5_MOC'])
self.AppendUnique(CPPPATH=[os.path.join("$QT5DIR","include")])
try: modules.remove("QtDBus")
except: pass
if debug : debugSuffix = 'd'
if "QtAssistant" in modules:
self.AppendUnique(CPPPATH=[os.path.join("$QT5DIR","include","QtAssistant")])
modules.remove("QtAssistant")
modules.append("QtAssistantClient")
self.AppendUnique(LIBS=['qtmain'+debugSuffix])
self.AppendUnique(LIBS=[lib.replace("Qt","Qt5")+debugSuffix for lib in modules if lib not in staticModules])
self.PrependUnique(LIBS=[lib+debugSuffix for lib in modules if lib in staticModules])
if 'QtOpenGL' in modules:
self.AppendUnique(LIBS=['opengl32'])
self.AppendUnique(CPPPATH=[ '$QT5DIR/include/'])
self.AppendUnique(CPPPATH=[ '$QT5DIR/include/'+module for module in modules])
if crosscompiling :
self["QT5_MOCCPPPATH"] = [
path.replace('$QT5DIR', transformedQtdir)
for path in self['CPPPATH'] ]
else :
self["QT5_MOCCPPPATH"] = self["CPPPATH"]
self.AppendUnique(LIBPATH=[os.path.join('$QT5DIR','lib')])
return
"""
if sys.platform=="darwin" :
# TODO: Test debug version on Mac
self.AppendUnique(LIBPATH=[os.path.join('$QT5DIR','lib')])
self.AppendUnique(LINKFLAGS="-F$QT5DIR/lib")
self.AppendUnique(LINKFLAGS="-L$QT5DIR/lib") #TODO clean!
if debug : debugSuffix = 'd'
for module in modules :
# self.AppendUnique(CPPPATH=[os.path.join("$QT5DIR","include")])
# self.AppendUnique(CPPPATH=[os.path.join("$QT5DIR","include",module)])
# port qt5-mac:
self.AppendUnique(CPPPATH=[os.path.join("$QT5DIR","include", "qt5")])
self.AppendUnique(CPPPATH=[os.path.join("$QT5DIR","include", "qt5", module)])
if module in staticModules :
self.AppendUnique(LIBS=[module+debugSuffix]) # TODO: Add the debug suffix
self.AppendUnique(LIBPATH=[os.path.join("$QT5DIR","lib")])
else :
# self.Append(LINKFLAGS=['-framework', module])
# port qt5-mac:
self.Append(LIBS=module)