Merge pull request #4584 from siegria/fix_piped_spawn_encoding
[scons.git] / test / ToolSurrogate.py
bloba297056bd72eb59651e8fdcce349dd38141b2a41
1 #!/usr/bin/env python
3 # __COPYRIGHT__
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__"
27 """
28 Test that SCons supports use of a home-brew ToolSurrogate class
29 like we use in our bin/sconsexamples.py script.
30 """
32 import TestSCons
34 test = TestSCons.TestSCons()
36 test.write('SConstruct', """\
37 class Curry:
38 def __init__(self, fun, *args, **kwargs):
39 self.fun = fun
40 self.pending = args[:]
41 self.kwargs = kwargs.copy()
43 def __call__(self, *args, **kwargs):
44 if kwargs and self.kwargs:
45 kw = self.kwargs.copy()
46 kw.update(kwargs)
47 else:
48 kw = kwargs or self.kwargs
50 return self.fun(*self.pending + args, **kw)
52 def Str(target, source, env, cmd=""):
53 result = []
54 for cmd in env.subst_list(cmd, target=target, source=source):
55 result.append(" ".join(map(str, cmd)))
56 return '\\n'.join(result)
58 class ToolSurrogate:
59 def __init__(self, tool, variable, func):
60 self.tool = tool
61 self.variable = variable
62 self.func = func
63 def __call__(self, env):
64 t = Tool(self.tool)
65 t.generate(env)
66 orig = env[self.variable]
67 env[self.variable] = Action(self.func, strfunction=Curry(Str, cmd=orig))
69 def Cat(target, source, env):
70 target = str(target[0])
71 with open(target, "wb") as ofp:
72 for src in map(str, source):
73 with open(src, "rb") as ifp:
74 ofp.write(ifp.read())
76 ToolList = {
77 'posix' : [('cc', 'CCCOM', Cat),
78 ('link', 'LINKCOM', Cat)],
79 'win32' : [('msvc', 'CCCOM', Cat),
80 ('mslink', 'LINKCOM', Cat)]
83 platform = ARGUMENTS['platform']
84 tools = [ToolSurrogate(*t) for t in ToolList[platform]]
86 env = Environment(tools=tools, PROGSUFFIX='.exe', OBJSUFFIX='.obj')
87 env.Program('foo.c')
88 """)
90 test.write('foo.c', "foo.c posix\n")
92 test.run(arguments = '. platform=posix', stdout = test.wrap_stdout("""\
93 cc -o foo.obj -c foo.c
94 cc -o foo.exe foo.obj
95 """))
97 test.write('foo.c', "foo.c win32\n")
99 test.run(arguments = '. platform=win32', stdout = test.wrap_stdout("""\
100 cl /Fofoo.obj /c foo.c /nologo
101 link /nologo /OUT:foo.exe foo.obj
102 embedManifestExeCheck(target, source, env)
103 """))
105 test.pass_test()
107 # Local Variables:
108 # tab-width:4
109 # indent-tabs-mode:nil
110 # End:
111 # vim: set expandtab tabstop=4 shiftwidth=4: