Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / swig / Modules.html
CommitLineData
86530b38
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html>
3<head>
4<title>Working with Modules</title>
5<link rel="stylesheet" type="text/css" href="style.css">
6</head>
7
8<body bgcolor="#ffffff">
9<H1><a name="Modules"></a>15 Working with Modules</H1>
10<!-- INDEX -->
11<div class="sectiontoc">
12<ul>
13<li><a href="#Modules_nn2">The SWIG runtime code</a>
14<li><a href="#external_run_time">External access to the runtime</a>
15<li><a href="#Modules_nn4">A word of caution about static libraries</a>
16<li><a href="#Modules_nn5">References</a>
17<li><a href="#Modules_nn6">Reducing the wrapper file size</a>
18</ul>
19</div>
20<!-- INDEX -->
21
22
23
24<p>
25When first working with SWIG, users commonly start by creating a
26single module. That is, you might define a single SWIG interface that
27wraps some set of C/C++ code. You then compile all of the generated
28wrapper code into a module and use it. For large applications, however,
29this approach is problematic---the size of the generated wrapper code
30can be rather large. Moreover, it is probably easier to manage the
31target language interface when it is broken up into smaller pieces.
32</p>
33
34<p>
35This chapter describes the problem of using SWIG in programs
36where you want to create a collection of modules.
37</p>
38
39<H2><a name="Modules_nn2"></a>15.1 The SWIG runtime code</H2>
40
41
42<p>
43Many of SWIG's target languages generate a set of functions
44commonly known as the "SWIG runtime." These functions are
45primarily related to the runtime type system which checks pointer
46types and performs other tasks such as proper casting of pointer
47values in C++. As a general rule, the statically typed target languages,
48such as Java, use the language's built in static type checking and
49have no need for a SWIG runtime. All the dynamically typed / interpreted
50languages rely on the SWIG runtime.
51</p>
52
53<p>
54The runtime functions are private to each SWIG-generated
55module. That is, the runtime functions are declared with "static"
56linkage and are visible only to the wrapper functions defined in that
57module. The only problem with this approach is that when more than one SWIG
58module is used in the same application, those modules often need to
59share type information. This is especially true for C++ programs
60where SWIG must collect and share information about inheritance
61relationships that cross module boundaries.
62</p>
63
64<p>
65To solve the problem of sharing information across modules, a pointer to the
66type information is stored in a global variable in the target language namespace.
67During module initialization, type information is loaded into the global data
68structure of type information from all modules.
69</p>
70
71<p>
72This can present a problem with threads. If two modules try and load at the same
73time, the type information can become corrupt. SWIG currently does not provide any
74locking, and if you use threads, you must make sure that modules are loaded serially.
75Be careful if you use threads and the automatic module loading that some scripting
76languages provide. One solution is to load all modules before spawning any threads.
77</p>
78
79<H2><a name="external_run_time"></a>15.2 External access to the runtime</H2>
80
81
82<p>As described in <a href="Typemaps.html#runtime_type_checker">The run-time type checker</a>,
83the functions <tt>SWIG_TypeQuery</tt>, <tt>SWIG_NewPointerObj</tt>, and others sometimes need
84to be called. Calling these functions from a typemap is supported, since the typemap code
85is embedded into the <tt>_wrap.c</tt> file, which has those declerations available. If you need
86to call the SWIG run-time functions from another C file, there is one header you need
87to include. To generate the header that needs to be included, run the following command:
88
89<div class="code"><pre>
90$ swig -python -external-runtime &lt;filename&gt;
91</pre></div>
92
93<p>The filename argument is optional and if it is not passed, then the default filename will
94be something like <tt>swigpyrun.h</tt>, depending on the language. This header file should
95be treated like any of the other _wrap.c output files, and should be regenerated when the
96_wrap files are. After including this header, your code will be able to call <tt>SWIG_TypeQuery</tt>,
97<tt>SWIG_NewPointerObj</tt>, <tt>SWIG_ConvertPtr</tt> and others. The exact argument paramaters
98for these functions might differ between language modules; please check the language module chapters
99for more information.</p>
100
101<p>Inside this header the functions are declared static and are included inline into the file,
102and thus the file does not need to be linked against any SWIG libraries or code (you might still
103need to link against the language libraries like libpython-2.3). Data is shared between this
104file and the _wrap.c files through a global variable in the scripting language. It is also
105possible to copy this header file along with the generated wrapper files into your own package,
106so that you can distribute a package that can be compiled without SWIG installed (this works
107because the header file is self contained, and does not need to link with anything).</p>
108
109<H2><a name="Modules_nn4"></a>15.3 A word of caution about static libraries</H2>
110
111
112<p>
113When working with multiple SWIG modules, you should take care not to use static
114libraries. For example, if you have a static library <tt>libfoo.a</tt> and you link a collection
115of SWIG modules with that library, each module will get its own private copy of the library code inserted
116into it. This is very often <b>NOT</b> what you want and it can lead to unexpected or bizarre program
117behavior. When working with dynamically loadable modules, you should try to work exclusively with shared libaries.
118</p>
119
120<H2><a name="Modules_nn5"></a>15.4 References</H2>
121
122
123<p>
124Due to the complexity of working with shared libraries and multiple modules, it might be a good idea to consult
125an outside reference. John Levine's "Linkers and Loaders" is highly recommended.
126</p>
127
128<H2><a name="Modules_nn6"></a>15.5 Reducing the wrapper file size</H2>
129
130
131<p>
132Using multiple modules with the <tt>%import</tt> directive is the most common approach to modularising large projects.
133In this way a number of different wrapper files can be generated, thereby avoiding the generation of a single large wrapper file.
134There are a couple of alternative solutions for reducing the size of a wrapper file through the use of command line options and features.
135</p>
136
137<p>
138<b>-fcompact</b><br>
139This command line option will compact the size of the wrapper file without changing the code generated into the wrapper file.
140It simply removes blank lines and joins lines of code together.
141This is useful for compilers that have a maximum file size that can be handled.
142</p>
143
144<p>
145<b>-fvirtual</b><br>
146This command line option will remove the generation of superfluous virtual method wrappers.
147Consider the following inheritance hierarchy:
148</p>
149
150<div class="code">
151<pre>
152struct Base {
153 virtual void method();
154 ...
155};
156
157struct Derived : Base {
158 virtual void method();
159 ...
160};
161</pre>
162</div>
163
164<p>
165Normally wrappers are generated for both methods, whereas this command line option will suppress the generation of a wrapper for <tt>Derived::method</tt>.
166Normal polymorphic behaviour remains as <tt>Derived::method</tt> will still be called should you have
167a <tt>Derived</tt> instance and call the wrapper for <tt>Base::method</tt>.
168</p>
169
170<p>
171<b>%feature("compactdefaultargs")</b><br>
172This feature can reduce the number of wrapper methods when wrapping methods with default arguments. The section on <a href="SWIGPlus.html#SWIGPlus_default_args">default arguments</a> discusses the feature and it's limitations.
173</p>
174
175</body>
176</html>