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.
28 The env.AddMethod(function, [name]) function is used to add a method
29 to an environment. It's typically used to add a "pseudo-builder" or
30 wrap up a call to multiple builders. In the first example, we want
31 to install the program into the standard bin dir, but also copy it
32 into a local install/bin dir that might be used to build a package
38 def install_in_bin_dirs(env, source):
39 """Install source in both bin dirs"""
40 i1 = env.Install("$BIN", source)
41 i2 = env.Install("$LOCALBIN", source)
42 return [i1[0], i2][0] # Return a list, like a normal builder
43 env = Environment(BIN='/usr/bin', LOCALBIN='#install/bin')
44 env.AddMethod(install_in_bin_dirs, "InstallInBinDirs")
45 env.InstallInBinDirs(Program('hello.c')) # installs hello in both bin dirs
49 This produces the following:
53 % <userinput>scons -Q</userinput>
54 cc -o hello.o -c hello.c
56 Install file: "hello" as "install/bin/hello"
61 It also gives more flexibility in parsing arguments than you can get
62 with a builder. The next example shows a pseudo-builder with a
63 named argument that modifies the filename, and a separate argument
64 for the resource file (rather than having the builder figure it out
65 by file extension). Also this example demonstrates using the global
66 AddMethod function to add a method to the global Environment class,
67 so it will be used in all subsequently created environments.
73 def BuildTestProg(env, testfile, resourcefile, testdir="tests"):
74 """Build the test program;
75 prepends "test_" to src and target, and puts target into testdir."""
76 srcfile="test_%s.c"%testfile
77 if sys.platform=='win32':
78 target="%s/test_%s$EXESUFFIX"%(testdir,[testfile, resourcefile])
80 target="%s/test_%s$EXESUFFIX"%(testdir,testfile)
81 p = env.Program(target, srcfile)
83 AddMethod(Environment, BuildTestProg)
87 env.BuildTestProg('stuff', resourcefile='res.rc')
91 This produces the following (on Linux, anyway; Windows would include the
96 % <userinput>scons -Q</userinput>
97 cc -o test_stuff.o -c test_stuff.c
98 cc -o tests/test_stuff test_stuff.o
102 Using AddMethod is better than just adding an instance method to an
103 Environment because it gets called as a proper method, and AddMethod
104 provides for copying the method to any copies of the Environment