http://scons.tigris.org/issues/show_bug.cgi?id=2329
[scons.git] / test / Scanner / generated.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 """
28 Verify that we only scan generated .h files once.
29
30 This originated as a real-life bug report submitted by Scott Lystig
31 Fritchie.  It's been left as-is, rather than stripped down to bare
32 minimum, partly because it wasn't completely clear what combination of
33 factors triggered the bug Scott saw, and partly because the real-world
34 complexity is valuable in its own right.
35 """
36
37 import TestSCons
38
39 test = TestSCons.TestSCons()
40
41 test.subdir('reftree',
42             ['reftree', 'include'],
43             'src',
44             ['src', 'lib_geng'])
45
46 test.write('SConstruct', """\
47 ###
48 ### QQQ !@#$!@#$!  I need to move the SConstruct file to be "above"
49 ### both the source and install dirs, or the install dependencies
50 ### don't seem to work well!  ARRGH!!!!
51 ###
52
53 experimenttop = r"%s"
54 import os
55 import Mylib
56
57 BStaticLibMerge = Builder(generator = Mylib.Gen_StaticLibMerge)
58 builders = Environment().Dictionary('BUILDERS')
59 builders["StaticLibMerge"] = BStaticLibMerge
60
61 env = Environment(BUILDERS = builders)
62 e = env.Dictionary()    # Slightly easier to type
63
64 global_env = env
65 e["GlobalEnv"] = global_env
66
67 e["REF_INCLUDE"] = os.path.join(experimenttop, "reftree", "include")
68 e["REF_LIB"] = os.path.join(experimenttop, "reftree", "lib")
69 e["EXPORT_INCLUDE"] = os.path.join(experimenttop, "export", "include")
70 e["EXPORT_LIB"] = os.path.join(experimenttop, "export", "lib")
71 e["INSTALL_BIN"] = os.path.join(experimenttop, "install", "bin")
72
73 variant_dir = os.path.join(experimenttop, "tmp-bld-dir")
74 src_dir = os.path.join(experimenttop, "src")
75
76 env.Append(CPPPATH = [e["EXPORT_INCLUDE"]])
77 env.Append(CPPPATH = [e["REF_INCLUDE"]])
78 Mylib.AddLibDirs(env, "/via/Mylib.AddLibPath")
79 env.Append(LIBPATH = [e["EXPORT_LIB"]])
80 env.Append(LIBPATH = [e["REF_LIB"]])
81
82 Mylib.Subdirs(env, "src")
83 """ % test.workpath())
84
85 test.write('Mylib.py', """\
86 import os
87 import re
88
89 def Subdirs(env, dirlist):
90     for file in _subconf_list(dirlist):
91         env.SConscript(file, "env")
92
93 def _subconf_list(dirlist):
94     return [os.path.join(x, "SConscript") for x in dirlist.split()]
95
96 def StaticLibMergeMembers(local_env, libname, hackpath, files):
97     for file in files.split():
98         # QQQ Fix limits in grok'ed regexp
99         tmp = re.sub(".c$", ".o", file)
100         objname = re.sub(".cpp", ".o", tmp)
101         local_env.Object(target = objname, source = file)
102         e = 'local_env["GlobalEnv"].Append(%s = ["%s"])' % (libname, os.path.join(hackpath, objname))
103         exec(e)
104
105 def CreateMergedStaticLibrary(env, libname):
106     objpaths = env["GlobalEnv"][libname]
107     libname = "lib%s.a" % (libname)
108     env.StaticLibMerge(target = libname, source = objpaths)
109
110 # I put the main body of the generator code here to avoid
111 # namespace problems
112 def Gen_StaticLibMerge(source, target, env, for_signature):
113     target_string = ""
114     for t in target:
115         target_string = str(t)
116     subdir = os.path.dirname(target_string)
117     srclist = []
118     for src in source:
119         srclist.append(src)
120     return [["ar", "cq"] + target + srclist, ["ranlib"] + target]
121
122 def StaticLibrary(env, target, source):
123     env.StaticLibrary(target, source.split())
124
125 def SharedLibrary(env, target, source):
126     env.SharedLibrary(target, source.split())
127
128 def ExportHeader(env, headers):
129     env.Install(dir = env["EXPORT_INCLUDE"], source = headers.split())
130
131 def ExportLib(env, libs):
132     env.Install(dir = env["EXPORT_LIB"], source = libs.split())
133
134 def InstallBin(env, bins):
135     env.Install(dir = env["INSTALL_BIN"], source = bins.split())
136
137 def Program(env, target, source):
138     env.Program(target, source.split())
139
140 def AddCFlags(env, str):
141     env.Append(CPPFLAGS = " " + str)
142
143 # QQQ Synonym needed?
144 #def AddCFLAGS(env, str):
145 #    AddCFlags(env, str)
146
147 def AddIncludeDirs(env, str):
148     env.Append(CPPPATH = str.split())
149
150 def AddLibs(env, str):
151     env.Append(LIBS = str.split())
152
153 def AddLibDirs(env, str):
154     env.Append(LIBPATH = str.split())
155
156 """)
157
158 test.write(['reftree', 'include', 'lib_a.h'], """\
159 char *a_letter(void);
160 """)
161
162 test.write(['reftree', 'include', 'lib_b.h'], """\
163 char *b_letter(void);
164 """)
165
166 test.write(['reftree', 'include', 'lib_ja.h'], """\
167 char *j_letter_a(void);
168 """)
169
170 test.write(['reftree', 'include', 'lib_jb.h.intentionally-moved'], """\
171 char *j_letter_b(void);
172 """)
173
174 test.write(['src', 'SConscript'], """\
175 # --- Begin SConscript boilerplate ---
176 import Mylib
177 Import("env")
178
179 #env = env.Clone()    # Yes, clobber intentionally
180 #Make environment changes, such as: Mylib.AddCFlags(env, "-g -D_TEST")
181 #Mylib.Subdirs(env, "lib_a lib_b lib_mergej prog_x")
182 Mylib.Subdirs(env, "lib_geng")
183
184 env = env.Clone()    # Yes, clobber intentionally
185 # --- End SConscript boilerplate ---
186
187 """)
188
189 test.write(['src', 'lib_geng', 'SConscript'], """\
190 # --- Begin SConscript boilerplate ---
191 import sys
192 import Mylib
193 Import("env")
194
195 #env = env.Clone()    # Yes, clobber intentionally
196 #Make environment changes, such as: Mylib.AddCFlags(env, "-g -D_TEST")
197 #Mylib.Subdirs(env, "foo_dir")
198
199 env = env.Clone()    # Yes, clobber intentionally
200 # --- End SConscript boilerplate ---
201
202 Mylib.AddCFlags(env, "-DGOOFY_DEMO")
203 Mylib.AddIncludeDirs(env, ".")
204
205 # Not part of Scott Lystig Fritchies's original stuff:
206 # On Windows, it's import to use the original test environment
207 # when we invoke SCons recursively.
208 import os
209 recurse_env = env.Clone()
210 recurse_env["ENV"] = os.environ
211
212 # Icky code to set up process environment for "make"
213 # I really ought to drop this into Mylib....
214
215 fromdict = env.Dictionary()
216 todict = env["ENV"]
217 import SCons.Util
218 import re
219 for k in fromdict.keys():
220     if k != "ENV" and k != "SCANNERS" and k != "CFLAGS" and k != "CXXFLAGS" \
221     and not SCons.Util.is_Dict(fromdict[k]):
222         # The next line can fail on some systems because it would try to
223         # do env.subst on:
224         #       $RMIC $RMICFLAGS -d ${TARGET.attributes.java_lookupdir} ...
225         # When $TARGET is None, so $TARGET.attributes would throw an
226         # exception, which SCons would turn into a UserError.  They're
227         # not important for this test, so just catch 'em.
228         f = fromdict[k]
229         try:
230              todict[k] = env.subst(f)
231         except SCons.Errors.UserError:
232              pass
233 todict["CFLAGS"] = fromdict["CPPFLAGS"] + " " + \
234     ' '.join(["-I" + x for x in env["CPPPATH"]]) + " " + \
235     ' '.join(["-L" + x for x in env["LIBPATH"]])
236 todict["CXXFLAGS"] = todict["CFLAGS"]
237
238 generated_hdrs = "libg_gx.h libg_gy.h libg_gz.h"
239 static_hdrs = "libg_w.h"
240 #exported_hdrs = generated_hdrs + " " + static_hdrs
241 exported_hdrs = static_hdrs
242 lib_name = "g"
243 lib_fullname = env.subst("${LIBPREFIX}g${LIBSUFFIX}")
244 lib_srcs = "libg_1.c libg_2.c libg_3.c".split()
245 import re
246 lib_objs = [re.sub("\.c$", ".o", x) for x in lib_srcs]
247
248 Mylib.ExportHeader(env, exported_hdrs)
249 Mylib.ExportLib(env, lib_fullname)
250
251 # The following were the original commands from Scott Lystic Fritchie,
252 # making use of a shell script and a Makefile to build the library.
253 # These have been preserved, commented out below, but in order to make
254 # this test portable, we've replaced them with a Python script and a
255 # recursive invocation of SCons (!).
256 #cmd_both = "cd %s ; make generated ; make" % Dir(".")
257 #cmd_generated = "cd %s ; sh MAKE-HEADER.sh" % Dir(".")
258 #cmd_justlib = "cd %s ; make" % Dir(".")
259
260 _ws = re.compile('\s')
261
262 def escape(s):
263     if _ws.search(s):
264         s = '"' + s + '"'
265     return s
266
267 cmd_generated = "%s $SOURCE" % escape(sys.executable)
268 cmd_justlib = "%s %s -C ${SOURCES[0].dir}" % (escape(sys.executable),
269                                               escape(sys.argv[0]))
270
271 ##### Deps appear correct ... but wacky scanning?
272 # Why?
273 #
274 # SCons bug??
275
276 env.Command(generated_hdrs.split(),
277             ["MAKE-HEADER.py"],
278             cmd_generated)
279 recurse_env.Command([lib_fullname] + lib_objs,
280                     lib_srcs + (generated_hdrs + " " + static_hdrs).split(),
281                     cmd_justlib) 
282 """)
283
284 test.write(['src', 'lib_geng', 'MAKE-HEADER.py'], """\
285 #!/usr/bin/env python
286
287 import os
288 import os.path
289 import sys
290
291 # chdir to the directory in which this script lives
292 os.chdir(os.path.split(sys.argv[0])[0])
293
294 for h in ['libg_gx.h', 'libg_gy.h', 'libg_gz.h']:
295     open(h, 'w').write('')
296 """)
297
298 test.write(['src', 'lib_geng', 'SConstruct'], """\
299 import os
300
301 Scanned = {}
302
303 def write_out(file, dict):
304     f = open(file, 'wb')
305     for k in sorted(dict.keys()):
306         file = os.path.split(k)[1]
307         f.write(file + ": " + str(dict[k]) + "\\n")
308     f.close()
309
310 orig_function = CScan.__call__
311
312 def MyCScan(node, paths, cwd, orig_function=orig_function):
313     deps = orig_function(node, paths, cwd)
314
315     global Scanned
316     n = str(node)
317     try:
318         Scanned[n] = Scanned[n] + 1
319     except KeyError:
320         Scanned[n] = 1
321     write_out(r'%s', Scanned)
322
323     return deps
324
325 CScan.__call__ = MyCScan
326
327 env = Environment(CPPPATH = ".")
328 l = env.StaticLibrary("g", Split("libg_1.c libg_2.c libg_3.c"))
329 Default(l)
330 """ % test.workpath('MyCScan.out'))
331
332 # These were the original shell script and Makefile from SLF's original
333 # bug report.  We're not using them--in order to make this script as
334 # portable as possible, we're using a Python script and a recursive
335 # invocation of SCons--but we're preserving them here for history.
336 #test.write(['src', 'lib_geng', 'MAKE-HEADER.sh'], """\
337 ##!/bin/sh
338 #
339 #exec touch $*
340 #""")
341 #
342 #test.write(['src', 'lib_geng', 'Makefile'], """\
343 #all: libg.a
344 #
345 #GEN_HDRS = libg_gx.h libg_gy.h libg_gz.h
346 #STATIC_HDRS = libg_w.h
347 #
348 #$(GEN_HDRS): generated
349 #
350 #generated: MAKE-HEADER.sh
351 #       sh ./MAKE-HEADER.sh $(GEN_HDRS)
352 #
353 #libg.a: libg_1.o libg_2.o libg_3.o
354 #       ar r libg.a libg_1.o libg_2.o libg_3.o
355 #
356 #libg_1.c: $(STATIC_HDRS) $(GEN_HDRS)
357 #libg_2.c: $(STATIC_HDRS) $(GEN_HDRS)
358 #libg_3.c: $(STATIC_HDRS) $(GEN_HDRS)
359 #
360 #clean:
361 #       -rm -f $(GEN_HDRS)
362 #       -rm -f libg.a *.o core core.*
363 #""")
364
365 test.write(['src', 'lib_geng', 'libg_w.h'], """\
366 """)
367
368 test.write(['src', 'lib_geng', 'libg_1.c'], """\
369 #include <libg_w.h>
370 #include <libg_gx.h>
371
372 int g_1()
373 {
374     return 1;
375 }
376 """)
377
378 test.write(['src', 'lib_geng', 'libg_2.c'], """\
379 #include <libg_w.h>
380 #include <libg_gx.h> 
381 #include <libg_gy.h>
382 #include <libg_gz.h>
383
384 int g_2()
385 {
386         return 2;
387 }
388 """)
389
390 test.write(['src', 'lib_geng', 'libg_3.c'], """\
391 #include <libg_w.h>
392 #include <libg_gx.h>
393
394 int g_3()
395 {
396     return 3;
397 }
398 """)
399
400 test.run(stderr=TestSCons.noisy_ar,
401          match=TestSCons.match_re_dotall)
402
403 test.must_match("MyCScan.out", """\
404 libg_1.c: 1
405 libg_2.c: 1
406 libg_3.c: 1
407 libg_gx.h: 1
408 libg_gy.h: 1
409 libg_gz.h: 1
410 libg_w.h: 1
411 """)
412
413 test.pass_test()
414
415 # Local Variables:
416 # tab-width:4
417 # indent-tabs-mode:nil
418 # End:
419 # vim: set expandtab tabstop=4 shiftwidth=4: