7e84b808f4c582b439de1242b09a32fe6d1610fe
[scons.git] / doc / user / add-method.xml
1 <!--
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
26   <para>
27
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
33   from.
34
35   </para>
36
37   <programlisting>
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     
46   </programlisting>
47
48   <para>
49   This produces the following:
50   </para>
51
52   <screen>
53     % <userinput>scons -Q</userinput>
54     cc -o hello.o -c hello.c
55     cc -o hello hello.o
56     Install file: "hello" as "install/bin/hello"
57   </screen>
58
59   <para>
60
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.
68
69   </para>
70
71   <programlisting>
72      import sys
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])
79          else:
80              target="%s/test_%s$EXESUFFIX"%(testdir,testfile)
81          p = env.Program(target, srcfile)
82          return p
83      AddMethod(Environment, BuildTestProg)
84
85      # Now use it
86      env=Environment()
87      env.BuildTestProg('stuff', resourcefile='res.rc')
88   </programlisting>
89
90   <para>
91   This produces the following (on Linux, anyway; Windows would include the
92   resource file):
93   </para>
94
95   <screen>
96     % <userinput>scons -Q</userinput>
97     cc -o test_stuff.o -c test_stuff.c
98     cc -o tests/test_stuff test_stuff.o
99   </screen>
100
101   <para>
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
105   instance.
106   </para>