4c84069fad10ebbf543038dd69f6ca227835ebd4
[scons.git] / test / Scanner / Scanner.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 import TestSCons
28
29 _python_ = TestSCons._python_
30
31 test = TestSCons.TestSCons()
32
33 test.write('build.py', r"""
34 import sys
35 input = open(sys.argv[1], 'rb')
36 output = open(sys.argv[2], 'wb')
37
38 def process(infp, outfp):
39     for line in infp.readlines():
40         if line[:8] == 'include ':
41             file = line[8:-1]
42             process(open(file, 'rb'), outfp)
43         elif line[:8] == 'getfile ':
44             outfp.write('include ')
45             outfp.write(line[8:])
46             # note: converted, but not acted upon
47         else:
48             outfp.write(line)
49
50 process(input, output)
51
52 sys.exit(0)
53 """)
54
55 # Execute a subsidiary SConscript just to make sure we can
56 # get at the Scanner keyword from there.
57
58 test.write('SConstruct', """
59 SConscript('SConscript')
60 """)
61
62 test.write('SConscript', """
63 import re
64
65 include_re = re.compile(r'^include\s+(\S+)$', re.M)
66
67 def kfile_scan(node, env, scanpaths, arg):
68     contents = node.get_text_contents()
69     includes = include_re.findall(contents)
70     return includes
71
72 kscan = Scanner(name = 'kfile',
73                 function = kfile_scan,
74                 argument = None,
75                 skeys = ['.k'])
76
77 env = Environment(K2SCAN=kfile_scan)
78
79 k2scan = env.Scanner(name = 'k2',
80                      # We'd like to do the following, but it will take
81                      # some major surgery to subst() and subst_list(),
82                      # so comment it out for now.
83                      # function = '$K2SCAN',
84                      function = kfile_scan,
85                      argument = None,
86                      skeys = ['.k2'])
87
88 ##########################################################
89 # Test scanner as found automatically from the environment
90 # (backup_source_scanner)
91
92 env = Environment()
93 env.Append(SCANNERS = kscan)
94
95 env.Command('foo', 'foo.k', r'%(_python_)s build.py $SOURCES $TARGET')
96
97 ##########################################################
98 # Test resetting the environment scanners (and specifying as a list).
99
100 env2 = env.Clone()
101 env2.Append(SCANNERS = [k2scan])
102 env2.Command('junk', 'junk.k2', r'%(_python_)s build.py $SOURCES $TARGET')
103
104 ##########################################################
105 # Test specifying a specific source scanner for a target Node
106
107 barbld = Builder(action=r'%(_python_)s build.py $SOURCES  $TARGET',
108                      source_scanner=kscan)
109 env.Append(BUILDERS={'BarBld':barbld})
110 bar = env.BarBld(target='bar', source='bar.in')
111
112 ##########################################################
113 # Test specifying a source scanner for a Builder that gets
114 # automatically applied to targets generated from that Builder
115
116 import string
117
118 def blork(env, target, source):
119     open(str(target[0]), 'wb').write(
120         string.replace(source[0].get_text_contents(), 'getfile', 'MISSEDME'))
121
122 kbld = Builder(action=r'%(_python_)s build.py $SOURCES $TARGET',
123                src_suffix='.lork',
124                suffix='.blork',
125                source_scanner=kscan)
126 blorkbld = Builder(action=blork,
127                    src_suffix='.blork',
128                    suffix='.ork')
129
130 env.Append(BUILDERS={'BLORK':blorkbld, 'KB':kbld})
131
132 blork = env.KB('moo.lork')
133 ork = env.BLORK(blork)
134 Alias('make_ork', ork)
135
136 """ % locals())
137
138 test.write('foo.k', 
139 """foo.k 1 line 1
140 include xxx
141 include yyy
142 foo.k 1 line 4
143 """)
144
145 test.write('bar.in', 
146 """include yyy
147 bar.in 1 line 2
148 bar.in 1 line 3
149 include zzz
150 """)
151
152 test.write('junk.k2', 
153 """include yyy
154 junk.k2 1 line 2
155 junk.k2 1 line 3
156 include zzz
157 """)
158
159 test.write('moo.lork',
160 """include xxx
161 moo.lork 1 line 2
162 include yyy
163 moo.lork 1 line 4
164 include moo.inc
165 """)
166
167 test.write('moo.inc',
168 """getfile zzz
169 """)
170
171 test.write('xxx', "xxx 1\n")
172 test.write('yyy', "yyy 1\n")
173 test.write('zzz', "zzz 1\n")
174
175 expect = test.wrap_stdout("""\
176 %(_python_)s build.py bar.in bar
177 %(_python_)s build.py foo.k foo
178 %(_python_)s build.py junk.k2 junk
179 %(_python_)s build.py moo.lork moo.blork
180 blork(["moo.ork"], ["moo.blork"])
181 """ % locals())
182
183 test.run(arguments = '.', stdout=expect)
184
185 test.must_match('foo', "foo.k 1 line 1\nxxx 1\nyyy 1\nfoo.k 1 line 4\n")
186 test.must_match('bar', "yyy 1\nbar.in 1 line 2\nbar.in 1 line 3\nzzz 1\n")
187 test.must_match('junk', "yyy 1\njunk.k2 1 line 2\njunk.k2 1 line 3\nzzz 1\n")
188 test.must_match('moo.ork', "xxx 1\nmoo.lork 1 line 2\nyyy 1\nmoo.lork 1 line 4\ninclude zzz\n")
189
190 test.up_to_date(arguments = '.')
191
192 test.write('xxx', "xxx 2\n")
193
194 expect = test.wrap_stdout("""\
195 %(_python_)s build.py foo.k foo
196 %(_python_)s build.py moo.lork moo.blork
197 blork(["moo.ork"], ["moo.blork"])
198 """ % locals())
199
200 test.run(arguments = '.', stdout=expect)
201
202 test.must_match('foo', "foo.k 1 line 1\nxxx 2\nyyy 1\nfoo.k 1 line 4\n")
203 test.must_match('bar', "yyy 1\nbar.in 1 line 2\nbar.in 1 line 3\nzzz 1\n")
204 test.must_match('junk', "yyy 1\njunk.k2 1 line 2\njunk.k2 1 line 3\nzzz 1\n")
205 test.must_match('moo.ork', "xxx 2\nmoo.lork 1 line 2\nyyy 1\nmoo.lork 1 line 4\ninclude zzz\n")
206
207 test.write('yyy', "yyy 2\n")
208
209 expect = test.wrap_stdout("""\
210 %(_python_)s build.py bar.in bar
211 %(_python_)s build.py foo.k foo
212 %(_python_)s build.py junk.k2 junk
213 %(_python_)s build.py moo.lork moo.blork
214 blork(["moo.ork"], ["moo.blork"])
215 """ % locals())
216
217 test.run(arguments = '.', stdout=expect)
218
219 test.must_match('foo', "foo.k 1 line 1\nxxx 2\nyyy 2\nfoo.k 1 line 4\n")
220 test.must_match('bar', "yyy 2\nbar.in 1 line 2\nbar.in 1 line 3\nzzz 1\n")
221 test.must_match('junk', "yyy 2\njunk.k2 1 line 2\njunk.k2 1 line 3\nzzz 1\n")
222 test.must_match('moo.ork', "xxx 2\nmoo.lork 1 line 2\nyyy 2\nmoo.lork 1 line 4\ninclude zzz\n")
223
224 test.write('zzz', "zzz 2\n")
225
226 expect = test.wrap_stdout("""\
227 %(_python_)s build.py bar.in bar
228 %(_python_)s build.py junk.k2 junk
229 """ % locals())
230
231 test.run(arguments = '.', stdout=expect)
232
233 test.must_match('foo', "foo.k 1 line 1\nxxx 2\nyyy 2\nfoo.k 1 line 4\n")
234 test.must_match('bar', "yyy 2\nbar.in 1 line 2\nbar.in 1 line 3\nzzz 2\n")
235 test.must_match('junk', "yyy 2\njunk.k2 1 line 2\njunk.k2 1 line 3\nzzz 2\n")
236 test.must_match('moo.ork', "xxx 2\nmoo.lork 1 line 2\nyyy 2\nmoo.lork 1 line 4\ninclude zzz\n")
237
238 test.up_to_date(arguments = 'foo')
239
240 test.pass_test()
241
242 # Local Variables:
243 # tab-width:4
244 # indent-tabs-mode:nil
245 # End:
246 # vim: set expandtab tabstop=4 shiftwidth=4: