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:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
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.
25 __revision__
= "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
28 Verify that we only scan generated .h files once.
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.
39 _python_
= TestSCons
._python
_
41 test
= TestSCons
.TestSCons()
43 test
.subdir('reftree',
44 ['reftree', 'include'],
48 test
.write('SConstruct', """\
50 ### QQQ !@#$!@#$! I need to move the SConstruct file to be "above"
51 ### both the source and install dirs, or the install dependencies
52 ### don't seem to work well! ARRGH!!!!
59 BStaticLibMerge = Builder(generator = Mylib.Gen_StaticLibMerge)
60 builders = Environment().Dictionary('BUILDERS')
61 builders["StaticLibMerge"] = BStaticLibMerge
63 env = Environment(BUILDERS = builders)
64 e = env.Dictionary() # Slightly easier to type
67 e["GlobalEnv"] = global_env
69 e["REF_INCLUDE"] = os.path.join(experimenttop, "reftree", "include")
70 e["REF_LIB"] = os.path.join(experimenttop, "reftree", "lib")
71 e["EXPORT_INCLUDE"] = os.path.join(experimenttop, "export", "include")
72 e["EXPORT_LIB"] = os.path.join(experimenttop, "export", "lib")
73 e["INSTALL_BIN"] = os.path.join(experimenttop, "install", "bin")
75 variant_dir = os.path.join(experimenttop, "tmp-bld-dir")
76 src_dir = os.path.join(experimenttop, "src")
78 env.Append(CPPPATH = [e["EXPORT_INCLUDE"]])
79 env.Append(CPPPATH = [e["REF_INCLUDE"]])
80 Mylib.AddLibDirs(env, "/via/Mylib.AddLibPath")
81 env.Append(LIBPATH = [e["EXPORT_LIB"]])
82 env.Append(LIBPATH = [e["REF_LIB"]])
84 Mylib.Subdirs(env, "src")
85 """ % test
.workpath())
87 test
.write('Mylib.py', """\
91 def Subdirs(env, dirlist):
92 for file in _subconf_list(dirlist):
93 env.SConscript(file, "env")
95 def _subconf_list(dirlist):
96 return [os.path.join(x, "SConscript") for x in dirlist.split()]
98 def StaticLibMergeMembers(local_env, libname, hackpath, files):
99 for file in files.split():
100 # QQQ Fix limits in grok'ed regexp
101 tmp = re.sub(".c$", ".o", file)
102 objname = re.sub(".cpp", ".o", tmp)
103 local_env.Object(target = objname, source = file)
104 e = 'local_env["GlobalEnv"].Append(%s = ["%s"])' % (libname, os.path.join(hackpath, objname))
107 def CreateMergedStaticLibrary(env, libname):
108 objpaths = env["GlobalEnv"][libname]
109 libname = "lib%s.a" % (libname)
110 env.StaticLibMerge(target = libname, source = objpaths)
112 # I put the main body of the generator code here to avoid
114 def Gen_StaticLibMerge(source, target, env, for_signature):
117 target_string = str(t)
118 subdir = os.path.dirname(target_string)
122 return [["ar", "cq"] + target + srclist, ["ranlib"] + target]
124 def StaticLibrary(env, target, source):
125 env.StaticLibrary(target, source.split())
127 def SharedLibrary(env, target, source):
128 env.SharedLibrary(target, source.split())
130 def ExportHeader(env, headers):
131 env.Install(dir = env["EXPORT_INCLUDE"], source = headers.split())
133 def ExportLib(env, libs):
134 env.Install(dir = env["EXPORT_LIB"], source = libs.split())
136 def InstallBin(env, bins):
137 env.Install(dir = env["INSTALL_BIN"], source = bins.split())
139 def Program(env, target, source):
140 env.Program(target, source.split())
142 def AddCFlags(env, str):
143 env.Append(CPPFLAGS = " " + str)
145 # QQQ Synonym needed?
146 #def AddCFLAGS(env, str):
147 # AddCFlags(env, str)
149 def AddIncludeDirs(env, str):
150 env.Append(CPPPATH = str.split())
152 def AddLibs(env, str):
153 env.Append(LIBS = str.split())
155 def AddLibDirs(env, str):
156 env.Append(LIBPATH = str.split())
160 test
.write(['reftree', 'include', 'lib_a.h'], """\
161 char *a_letter(void);
164 test
.write(['reftree', 'include', 'lib_b.h'], """\
165 char *b_letter(void);
168 test
.write(['reftree', 'include', 'lib_ja.h'], """\
169 char *j_letter_a(void);
172 test
.write(['reftree', 'include', 'lib_jb.h.intentionally-moved'], """\
173 char *j_letter_b(void);
176 test
.write(['src', 'SConscript'], """\
177 # --- Begin SConscript boilerplate ---
181 #env = env.Clone() # Yes, clobber intentionally
182 #Make environment changes, such as: Mylib.AddCFlags(env, "-g -D_TEST")
183 #Mylib.Subdirs(env, "lib_a lib_b lib_mergej prog_x")
184 Mylib.Subdirs(env, "lib_geng")
186 env = env.Clone() # Yes, clobber intentionally
187 # --- End SConscript boilerplate ---
191 test
.write(['src', 'lib_geng', 'SConscript'], r
"""
192 # --- Begin SConscript boilerplate ---
197 #env = env.Clone() # Yes, clobber intentionally
198 #Make environment changes, such as: Mylib.AddCFlags(env, "-g -D_TEST")
199 #Mylib.Subdirs(env, "foo_dir")
201 env = env.Clone() # Yes, clobber intentionally
202 # --- End SConscript boilerplate ---
204 Mylib.AddCFlags(env, "-DGOOFY_DEMO")
205 Mylib.AddIncludeDirs(env, ".")
207 # Not part of Scott Lystig Fritchies's original stuff:
208 # On Windows, it's import to use the original test environment
209 # when we invoke SCons recursively.
211 recurse_env = env.Clone()
212 recurse_env["ENV"] = os.environ
214 # Icky code to set up process environment for "make"
215 # I really ought to drop this into Mylib....
217 fromdict = env.Dictionary()
221 for k in fromdict.keys():
222 if k != "ENV" and k != "SCANNERS" and k != "CFLAGS" and k != "CXXFLAGS" \
223 and not SCons.Util.is_Dict(fromdict[k]):
224 # The next line can fail on some systems because it would try to
226 # $RMIC $RMICFLAGS -d ${TARGET.attributes.java_lookupdir} ...
227 # When $TARGET is None, so $TARGET.attributes would throw an
228 # exception, which SCons would turn into a UserError. They're
229 # not important for this test, so just catch 'em.
232 todict[k] = env.subst(f)
233 except SCons.Errors.UserError:
235 todict["CFLAGS"] = fromdict["CPPFLAGS"] + " " + \
236 ' '.join(["-I" + x for x in env["CPPPATH"]]) + " " + \
237 ' '.join(["-L" + x for x in env["LIBPATH"]])
238 todict["CXXFLAGS"] = todict["CFLAGS"]
240 generated_hdrs = "libg_gx.h libg_gy.h libg_gz.h"
241 static_hdrs = "libg_w.h"
242 #exported_hdrs = generated_hdrs + " " + static_hdrs
243 exported_hdrs = static_hdrs
245 lib_fullname = env.subst("${LIBPREFIX}g${LIBSUFFIX}")
246 lib_srcs = "libg_1.c libg_2.c libg_3.c".split()
248 lib_objs = [re.sub(r"\.c$", ".o", x) for x in lib_srcs]
250 Mylib.ExportHeader(env, exported_hdrs)
251 Mylib.ExportLib(env, lib_fullname)
253 # The following were the original commands from Scott Lystic Fritchie,
254 # making use of a shell script and a Makefile to build the library.
255 # These have been preserved, commented out below, but in order to make
256 # this test portable, we've replaced them with a Python script and a
257 # recursive invocation of SCons (!).
258 #cmd_both = "cd %s ; make generated ; make" % Dir(".")
259 #cmd_generated = "cd %s ; sh MAKE-HEADER.sh" % Dir(".")
260 #cmd_justlib = "cd %s ; make" % Dir(".")
262 _ws = re.compile(r'\s')
269 cmd_generated = "%s $SOURCE" % escape(sys.executable)
270 cmd_justlib = "%s %s -C ${SOURCES[0].dir}" % (escape(sys.executable),
273 ##### Deps appear correct ... but wacky scanning?
278 env.Command(generated_hdrs.split(),
281 recurse_env.Command([lib_fullname] + lib_objs,
282 lib_srcs + (generated_hdrs + " " + static_hdrs).split(),
286 test
.write(['src', 'lib_geng', 'MAKE-HEADER.py'], """\
293 # chdir to the directory in which this script lives
294 os.chdir(os.path.split(sys.argv[0])[0])
296 for h in ['libg_gx.h', 'libg_gy.h', 'libg_gz.h']:
297 with open(h, 'w') as f:
301 test
.write(['src', 'lib_geng', 'SConstruct'], """\
306 def write_out(fname, dict):
307 with open(fname, 'w') as f:
308 for k in sorted(dict.keys()):
309 name = os.path.split(k)[1]
310 f.write(name + ": " + str(dict[k]) + "\\n")
312 # A hand-coded new-style class proxy to wrap the underlying C Scanner
313 # with a method that counts the calls.
315 # This is more complicated than it used to be with old-style classes
316 # because the .__*__() methods in new-style classes are not looked
317 # up on the instance, but resolve to the actual wrapped class methods,
318 # so we have to handle those directly.
319 class CScannerCounter:
320 def __init__(self, original_CScanner, *args, **kw):
321 self.original_CScanner = original_CScanner
322 def __eq__(self, *args, **kw):
323 return self.original_CScanner.__eq__(*args, **kw)
324 def __hash__(self, *args, **kw):
325 return self.original_CScanner.__hash__(*args, **kw)
326 def __str__(self, *args, **kw):
327 return self.original_CScanner.__str__(*args, **kw)
328 def __getattr__(self, *args, **kw):
329 return self.original_CScanner.__getattribute__(*args, **kw)
330 def __call__(self, node, *args, **kw):
334 Scanned[n] = Scanned[n] + 1
337 write_out(r'%s', Scanned)
338 return self.original_CScanner(node, *args, **kw)
341 MyCScanner = CScannerCounter(SCons.Script.CScanner)
342 SCons.Tool.SourceFileScanner.add_scanner('.c', MyCScanner)
343 SCons.Tool.SourceFileScanner.add_scanner('.h', MyCScanner)
345 env = Environment(CPPPATH = ".")
346 l = env.StaticLibrary("g", Split("libg_1.c libg_2.c libg_3.c"))
348 """ % test
.workpath('MyCScan.out'))
350 # These were the original shell script and Makefile from SLF's original
351 # bug report. We're not using them--in order to make this script as
352 # portable as possible, we're using a Python script and a recursive
353 # invocation of SCons--but we're preserving them here for history.
354 #test.write(['src', 'lib_geng', 'MAKE-HEADER.sh'], """\
360 #test.write(['src', 'lib_geng', 'Makefile'], """\
363 #GEN_HDRS = libg_gx.h libg_gy.h libg_gz.h
364 #STATIC_HDRS = libg_w.h
366 #$(GEN_HDRS): generated
368 #generated: MAKE-HEADER.sh
369 # sh ./MAKE-HEADER.sh $(GEN_HDRS)
371 #libg.a: libg_1.o libg_2.o libg_3.o
372 # ar r libg.a libg_1.o libg_2.o libg_3.o
374 #libg_1.c: $(STATIC_HDRS) $(GEN_HDRS)
375 #libg_2.c: $(STATIC_HDRS) $(GEN_HDRS)
376 #libg_3.c: $(STATIC_HDRS) $(GEN_HDRS)
380 # -rm -f libg.a *.o core core.*
383 test
.write(['src', 'lib_geng', 'libg_w.h'], """\
386 test
.write(['src', 'lib_geng', 'libg_1.c'], """\
396 test
.write(['src', 'lib_geng', 'libg_2.c'], """\
408 test
.write(['src', 'lib_geng', 'libg_3.c'], """\
418 test
.run(stderr
=TestSCons
.noisy_ar
,
419 match
=TestSCons
.match_re_dotall
)
421 # Note that the generated .h files still get scanned twice,
422 # but that's really once each as a child of libg_1.o and libg_2.o.
424 # TODO(sgk): can the duplication be eliminated safely? Batch build
425 # support "eliminated" the duplication before in a way that broke a
426 # use case that ended up in test/Depends/no-Builder.py (issue 2647).
428 test
.must_match("MyCScan.out", """\
442 # indent-tabs-mode:nil
444 # vim: set expandtab tabstop=4 shiftwidth=4: