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 test = TestSCons.TestSCons()
41 test.subdir('reftree',
42 ['reftree', 'include'],
46 test.write('SConstruct', """\
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!!!!
57 BStaticLibMerge = Builder(generator = Mylib.Gen_StaticLibMerge)
58 builders = Environment().Dictionary('BUILDERS')
59 builders["StaticLibMerge"] = BStaticLibMerge
61 env = Environment(BUILDERS = builders)
62 e = env.Dictionary() # Slightly easier to type
65 e["GlobalEnv"] = global_env
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")
73 variant_dir = os.path.join(experimenttop, "tmp-bld-dir")
74 src_dir = os.path.join(experimenttop, "src")
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"]])
82 Mylib.Subdirs(env, "src")
83 """ % test.workpath())
85 test.write('Mylib.py', """\
89 def Subdirs(env, dirlist):
90 for file in _subconf_list(dirlist):
91 env.SConscript(file, "env")
93 def _subconf_list(dirlist):
94 return [os.path.join(x, "SConscript") for x in dirlist.split()]
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))
105 def CreateMergedStaticLibrary(env, libname):
106 objpaths = env["GlobalEnv"][libname]
107 libname = "lib%s.a" % (libname)
108 env.StaticLibMerge(target = libname, source = objpaths)
110 # I put the main body of the generator code here to avoid
112 def Gen_StaticLibMerge(source, target, env, for_signature):
115 target_string = str(t)
116 subdir = os.path.dirname(target_string)
120 return [["ar", "cq"] + target + srclist, ["ranlib"] + target]
122 def StaticLibrary(env, target, source):
123 env.StaticLibrary(target, source.split())
125 def SharedLibrary(env, target, source):
126 env.SharedLibrary(target, source.split())
128 def ExportHeader(env, headers):
129 env.Install(dir = env["EXPORT_INCLUDE"], source = headers.split())
131 def ExportLib(env, libs):
132 env.Install(dir = env["EXPORT_LIB"], source = libs.split())
134 def InstallBin(env, bins):
135 env.Install(dir = env["INSTALL_BIN"], source = bins.split())
137 def Program(env, target, source):
138 env.Program(target, source.split())
140 def AddCFlags(env, str):
141 env.Append(CPPFLAGS = " " + str)
143 # QQQ Synonym needed?
144 #def AddCFLAGS(env, str):
145 # AddCFlags(env, str)
147 def AddIncludeDirs(env, str):
148 env.Append(CPPPATH = str.split())
150 def AddLibs(env, str):
151 env.Append(LIBS = str.split())
153 def AddLibDirs(env, str):
154 env.Append(LIBPATH = str.split())
158 test.write(['reftree', 'include', 'lib_a.h'], """\
159 char *a_letter(void);
162 test.write(['reftree', 'include', 'lib_b.h'], """\
163 char *b_letter(void);
166 test.write(['reftree', 'include', 'lib_ja.h'], """\
167 char *j_letter_a(void);
170 test.write(['reftree', 'include', 'lib_jb.h.intentionally-moved'], """\
171 char *j_letter_b(void);
174 test.write(['src', 'SConscript'], """\
175 # --- Begin SConscript boilerplate ---
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")
184 env = env.Clone() # Yes, clobber intentionally
185 # --- End SConscript boilerplate ---
189 test.write(['src', 'lib_geng', 'SConscript'], """\
190 # --- Begin SConscript boilerplate ---
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")
199 env = env.Clone() # Yes, clobber intentionally
200 # --- End SConscript boilerplate ---
202 Mylib.AddCFlags(env, "-DGOOFY_DEMO")
203 Mylib.AddIncludeDirs(env, ".")
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.
209 recurse_env = env.Clone()
210 recurse_env["ENV"] = os.environ
212 # Icky code to set up process environment for "make"
213 # I really ought to drop this into Mylib....
215 fromdict = env.Dictionary()
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
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.
230 todict[k] = env.subst(f)
231 except SCons.Errors.UserError:
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"]
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
243 lib_fullname = env.subst("${LIBPREFIX}g${LIBSUFFIX}")
244 lib_srcs = "libg_1.c libg_2.c libg_3.c".split()
246 lib_objs = [re.sub("\.c$", ".o", x) for x in lib_srcs]
248 Mylib.ExportHeader(env, exported_hdrs)
249 Mylib.ExportLib(env, lib_fullname)
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(".")
260 _ws = re.compile('\s')
267 cmd_generated = "%s $SOURCE" % escape(sys.executable)
268 cmd_justlib = "%s %s -C ${SOURCES[0].dir}" % (escape(sys.executable),
271 ##### Deps appear correct ... but wacky scanning?
276 env.Command(generated_hdrs.split(),
279 recurse_env.Command([lib_fullname] + lib_objs,
280 lib_srcs + (generated_hdrs + " " + static_hdrs).split(),
284 test.write(['src', 'lib_geng', 'MAKE-HEADER.py'], """\
285 #!/usr/bin/env python
291 # chdir to the directory in which this script lives
292 os.chdir(os.path.split(sys.argv[0])[0])
294 for h in ['libg_gx.h', 'libg_gy.h', 'libg_gz.h']:
295 open(h, 'w').write('')
298 test.write(['src', 'lib_geng', 'SConstruct'], """\
303 def write_out(file, dict):
308 file = os.path.split(k)[1]
309 f.write(file + ": " + str(dict[k]) + "\\n")
312 orig_function = CScan.__call__
314 def MyCScan(node, paths, cwd, orig_function=orig_function):
315 deps = orig_function(node, paths, cwd)
320 Scanned[n] = Scanned[n] + 1
323 write_out(r'%s', Scanned)
327 CScan.__call__ = MyCScan
329 env = Environment(CPPPATH = ".")
330 l = env.StaticLibrary("g", Split("libg_1.c libg_2.c libg_3.c"))
332 """ % test.workpath('MyCScan.out'))
334 # These were the original shell script and Makefile from SLF's original
335 # bug report. We're not using them--in order to make this script as
336 # portable as possible, we're using a Python script and a recursive
337 # invocation of SCons--but we're preserving them here for history.
338 #test.write(['src', 'lib_geng', 'MAKE-HEADER.sh'], """\
344 #test.write(['src', 'lib_geng', 'Makefile'], """\
347 #GEN_HDRS = libg_gx.h libg_gy.h libg_gz.h
348 #STATIC_HDRS = libg_w.h
350 #$(GEN_HDRS): generated
352 #generated: MAKE-HEADER.sh
353 # sh ./MAKE-HEADER.sh $(GEN_HDRS)
355 #libg.a: libg_1.o libg_2.o libg_3.o
356 # ar r libg.a libg_1.o libg_2.o libg_3.o
358 #libg_1.c: $(STATIC_HDRS) $(GEN_HDRS)
359 #libg_2.c: $(STATIC_HDRS) $(GEN_HDRS)
360 #libg_3.c: $(STATIC_HDRS) $(GEN_HDRS)
364 # -rm -f libg.a *.o core core.*
367 test.write(['src', 'lib_geng', 'libg_w.h'], """\
370 test.write(['src', 'lib_geng', 'libg_1.c'], """\
380 test.write(['src', 'lib_geng', 'libg_2.c'], """\
392 test.write(['src', 'lib_geng', 'libg_3.c'], """\
402 test.run(stderr=TestSCons.noisy_ar,
403 match=TestSCons.match_re_dotall)
405 test.must_match("MyCScan.out", """\
419 # indent-tabs-mode:nil
421 # vim: set expandtab tabstop=4 shiftwidth=4: