initial import
[vuplus_webkit] / Tools / waf / build / waf_extensions.py
1 # Copyright (C) 2009 Kevin Ollivier  All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions
5 # are met:
6 # 1. Redistributions of source code must retain the above copyright
7 #    notice, this list of conditions and the following disclaimer.
8 # 2. Redistributions in binary form must reproduce the above copyright
9 #    notice, this list of conditions and the following disclaimer in the
10 #    documentation and/or other materials provided with the distribution.
11 #
12 # THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
13 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
15 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
16 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
17 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
19 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
20 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 #
24 # This module is for code where we override waf's default behavior or extend waf
25
26 import os
27 import subprocess
28 import sys
29
30 import Utils
31
32
33 # version of exec_command that handles Windows command lines longer than 32000 chars
34 def exec_command(s, **kw):
35     filename = ''
36     if sys.platform.startswith('win') and len(' '.join(s)) > 32000:
37         import tempfile
38         (fd, filename) = tempfile.mkstemp()
39         t = []
40         for i in s:
41             if i.find(" ") != -1:
42                 i = '"%s"' % i
43             t.append(i)
44
45         t1 = t[1:]
46         # Fix for LNK1170 error
47         filename_str = '@' + filename
48         filename2_str = None
49         if len(' '.join(s)) > 131070:
50             t2 = t[len(t) / 2:]
51             t1 = t[1:len(t) / 2]
52             (fd2, filename2) = tempfile.mkstemp()
53             os.write(fd2, ' '.join(t2))
54             os.close(fd2)
55             filename2_str = '@' + filename2
56         os.write(fd, ' '.join(t1))
57         os.close(fd)
58
59         s = [s[0]]
60         s.append(filename_str)
61         if filename2_str:
62             s.append(filename2_str)
63
64     if 'log' in kw:
65         kw['stdout'] = kw['stderr'] = kw['log']
66         del(kw['log'])
67     kw['shell'] = isinstance(s, str)
68
69     def cleanup():
70         try:
71             if os.path.exists(filename):
72                 os.remove(filename)
73         except:
74             pass
75
76     try:
77         proc = subprocess.Popen(s, **kw)
78         result = proc.wait()
79         cleanup()
80         return result
81
82     except OSError:
83         cleanup()
84         raise
85
86 Utils.exec_command = exec_command
87
88 # Better performing h_file to keep hashing from consuming lots of time
89 import stat
90
91
92 def h_file(filename):
93     st = os.stat(filename)
94     if stat.S_ISDIR(st[stat.ST_MODE]):
95         raise IOError('not a file')
96     m = Utils.md5()
97     m.update(str(st.st_mtime))
98     m.update(str(st.st_size))
99     m.update(filename)
100     return m.digest()
101
102 Utils.h_file = h_file