| 1 |
'\" |
| 2 |
'\" Copyright (c) 1996 Sun Microsystems, Inc. |
| 3 |
'\" |
| 4 |
'\" See the file "license.terms" for information on usage and redistribution |
| 5 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 6 |
'\" |
| 7 |
.TH pkg_mkIndex n 8.3 Tcl "Tcl Built-In Commands" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 11 |
.SH NAME |
| 12 |
pkg_mkIndex \- Build an index for automatic loading of packages |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
\fBpkg_mkIndex\fR ?\fIoptions...\fR? \fIdir\fR ?\fIpattern pattern ...\fR? |
| 16 |
.fi |
| 17 |
.BE |
| 18 |
.SH DESCRIPTION |
| 19 |
.PP |
| 20 |
\fBPkg_mkIndex\fR is a utility procedure that is part of the standard |
| 21 |
Tcl library. |
| 22 |
It is used to create index files that allow packages to be loaded |
| 23 |
automatically when \fBpackage require\fR commands are executed. |
| 24 |
To use \fBpkg_mkIndex\fR, follow these steps: |
| 25 |
.IP [1] |
| 26 |
Create the package(s). |
| 27 |
Each package may consist of one or more Tcl script files or binary files. |
| 28 |
Binary files must be suitable for loading with the \fBload\fR command |
| 29 |
with a single argument; for example, if the file is \fBtest.so\fR it must |
| 30 |
be possible to load this file with the command \fBload test.so\fR. |
| 31 |
Each script file must contain a \fBpackage provide\fR command to declare |
| 32 |
the package and version number, and each binary file must contain |
| 33 |
a call to \fBTcl_PkgProvide\fR. |
| 34 |
.IP [2] |
| 35 |
Create the index by invoking \fBpkg_mkIndex\fR. |
| 36 |
The \fIdir\fR argument gives the name of a directory and each |
| 37 |
\fIpattern\fR argument is a \fBglob\fR-style pattern that selects |
| 38 |
script or binary files in \fIdir\fR. |
| 39 |
The default pattern is \fB*.tcl\fR and \fB*.[info sharedlibextension]\fR. |
| 40 |
.RS |
| 41 |
.PP |
| 42 |
\fBPkg_mkIndex\fR will create a file \fBpkgIndex.tcl\fR in \fIdir\fR |
| 43 |
with package information about all the files given by the \fIpattern\fR |
| 44 |
arguments. |
| 45 |
It does this by loading each file into a child |
| 46 |
interpreter and seeing what packages |
| 47 |
and new commands appear (this is why it is essential to have |
| 48 |
\fBpackage provide\fR commands or \fBTcl_PkgProvide\fR calls |
| 49 |
in the files, as described above). |
| 50 |
If you have a package split among scripts and binary files, |
| 51 |
or if you have dependencies among files, |
| 52 |
you may have to use the \fB\-load\fR option |
| 53 |
or adjust the order in which \fBpkg_mkIndex\fR processes |
| 54 |
the files. See \fBCOMPLEX CASES\fR below. |
| 55 |
.RE |
| 56 |
.IP [3] |
| 57 |
Install the package as a subdirectory of one of the directories given by |
| 58 |
the \fBtcl_pkgPath\fR variable. If \fB$tcl_pkgPath\fR contains more |
| 59 |
than one directory, machine-dependent packages (e.g., those that |
| 60 |
contain binary shared libraries) should normally be installed |
| 61 |
under the first directory and machine-independent packages (e.g., |
| 62 |
those that contain only Tcl scripts) should be installed under the |
| 63 |
second directory. |
| 64 |
The subdirectory should include |
| 65 |
the package's script and/or binary files as well as the \fBpkgIndex.tcl\fR |
| 66 |
file. As long as the package is installed as a subdirectory of a |
| 67 |
directory in \fB$tcl_pkgPath\fR it will automatically be found during |
| 68 |
\fBpackage require\fR commands. |
| 69 |
.RS |
| 70 |
.PP |
| 71 |
If you install the package anywhere else, then you must ensure that |
| 72 |
the directory containing the package is in the \fBauto_path\fR global variable |
| 73 |
or an immediate subdirectory of one of the directories in \fBauto_path\fR. |
| 74 |
\fBAuto_path\fR contains a list of directories that are searched |
| 75 |
by both the auto-loader and the package loader; by default it |
| 76 |
includes \fB$tcl_pkgPath\fR. |
| 77 |
The package loader also checks all of the subdirectories of the |
| 78 |
directories in \fBauto_path\fR. |
| 79 |
You can add a directory to \fBauto_path\fR explicitly in your |
| 80 |
application, or you can add the directory to your \fBTCLLIBPATH\fR |
| 81 |
environment variable: if this environment variable is present, |
| 82 |
Tcl initializes \fBauto_path\fR from it during application startup. |
| 83 |
.RE |
| 84 |
.IP [4] |
| 85 |
Once the above steps have been taken, all you need to do to use a |
| 86 |
package is to invoke \fBpackage require\fR. |
| 87 |
For example, if versions 2.1, 2.3, and 3.1 of package \fBTest\fR |
| 88 |
have been indexed by \fBpkg_mkIndex\fR, the command |
| 89 |
\fBpackage require Test\fR will make version 3.1 available |
| 90 |
and the command \fBpackage require \-exact Test 2.1\fR will |
| 91 |
make version 2.1 available. |
| 92 |
There may be many versions of a package in the various index files |
| 93 |
in \fBauto_path\fR, but only one will actually be loaded in a given |
| 94 |
interpreter, based on the first call to \fBpackage require\fR. |
| 95 |
Different versions of a package may be loaded in different |
| 96 |
interpreters. |
| 97 |
.SH OPTIONS |
| 98 |
The optional switches are: |
| 99 |
.TP 15 |
| 100 |
\fB\-direct\fR |
| 101 |
The generated index will implement direct loading of the package |
| 102 |
upon \fBpackage require\fR. This is the default. |
| 103 |
.TP 15 |
| 104 |
\fB\-lazy\fR |
| 105 |
The generated index will manage to delay loading the package until the |
| 106 |
use of one of the commands provided by the package, instead of loading |
| 107 |
it immediately upon \fBpackage require\fR. This is not compatible with |
| 108 |
the use of \fIauto_reset\fR, and therefore its use is discouraged. |
| 109 |
.TP 15 |
| 110 |
\fB\-load \fIpkgPat\fR |
| 111 |
The index process will preload any packages that exist in the |
| 112 |
current interpreter and match \fIpkgPat\fR into the child interpreter used to |
| 113 |
generate the index. The pattern match uses string match rules, but without |
| 114 |
making case distinctions. |
| 115 |
See \fBCOMPLEX CASES\fR below. |
| 116 |
.TP 15 |
| 117 |
\fB\-verbose\fR |
| 118 |
Generate output during the indexing process. Output is via |
| 119 |
the \fBtclLog\fR procedure, which by default prints to stderr. |
| 120 |
.TP 15 |
| 121 |
\fB\-\-\fR |
| 122 |
End of the flags, in case \fIdir\fR begins with a dash. |
| 123 |
.SH "PACKAGES AND THE AUTO-LOADER" |
| 124 |
.PP |
| 125 |
The package management facilities overlap somewhat with the auto-loader, |
| 126 |
in that both arrange for files to be loaded on-demand. |
| 127 |
However, package management is a higher-level mechanism that uses |
| 128 |
the auto-loader for the last step in the loading process. |
| 129 |
It is generally better to index a package with \fBpkg_mkIndex\fR |
| 130 |
rather than \fBauto_mkindex\fR because the package mechanism provides |
| 131 |
version control: several versions of a package can be made available |
| 132 |
in the index files, with different applications using different |
| 133 |
versions based on \fBpackage require\fR commands. |
| 134 |
In contrast, \fBauto_mkindex\fR does not understand versions so |
| 135 |
it can only handle a single version of each package. |
| 136 |
It is probably not a good idea to index a given package with both |
| 137 |
\fBpkg_mkIndex\fR and \fBauto_mkindex\fR. |
| 138 |
If you use \fBpkg_mkIndex\fR to index a package, its commands cannot |
| 139 |
be invoked until \fBpackage require\fR has been used to select a |
| 140 |
version; in contrast, packages indexed with \fBauto_mkindex\fR |
| 141 |
can be used immediately since there is no version control. |
| 142 |
.SH "HOW IT WORKS" |
| 143 |
.PP |
| 144 |
\fBPkg_mkIndex\fR depends on the \fBpackage unknown\fR command, |
| 145 |
the \fBpackage ifneeded\fR command, and the auto-loader. |
| 146 |
The first time a \fBpackage require\fR command is invoked, |
| 147 |
the \fBpackage unknown\fR script is invoked. |
| 148 |
This is set by Tcl initialization to a script that |
| 149 |
evaluates all of the \fBpkgIndex.tcl\fR files in the |
| 150 |
\fBauto_path\fR. |
| 151 |
The \fBpkgIndex.tcl\fR files contain \fBpackage ifneeded\fR |
| 152 |
commands for each version of each available package; these commands |
| 153 |
invoke \fBpackage provide\fR commands to announce the |
| 154 |
availability of the package, and they setup auto-loader |
| 155 |
information to load the files of the package. |
| 156 |
If the \fB\-lazy\fR flag was provided when the \fBpkgIndex.tcl\fR |
| 157 |
was generated, |
| 158 |
a given file of a given version of a given package is not |
| 159 |
actually loaded until the first time one of its commands |
| 160 |
is invoked. |
| 161 |
Thus, after invoking \fBpackage require\fR you may |
| 162 |
not see the package's commands in the interpreter, but you will be able |
| 163 |
to invoke the commands and they will be auto-loaded. |
| 164 |
.SH "DIRECT LOADING" |
| 165 |
.PP |
| 166 |
Some packages, for instance packages which use namespaces and export |
| 167 |
commands or those which require special initialization, might select |
| 168 |
that their package files be loaded immediately upon \fBpackage require\fR |
| 169 |
instead of delaying the actual loading to the first use of one of the |
| 170 |
package's command. This is the default mode when generating the package |
| 171 |
index. It can be overridden by specifying the \fB\-lazy\fR argument. |
| 172 |
.SH "COMPLEX CASES" |
| 173 |
Most complex cases of dependencies among scripts |
| 174 |
and binary files, and packages being split among scripts and |
| 175 |
binary files are handled OK. However, you may have to adjust |
| 176 |
the order in which files are processed by \fBpkg_mkIndex\fR. |
| 177 |
These issues are described in detail below. |
| 178 |
.PP |
| 179 |
If each script or file contains one package, and packages |
| 180 |
are only contained in one file, then things are easy. |
| 181 |
You simply specify all files to be indexed in any order |
| 182 |
with some glob patterns. |
| 183 |
.PP |
| 184 |
In general, it is OK for scripts to have dependencies on other |
| 185 |
packages. |
| 186 |
If scripts contain \fBpackage require\fR commands, these are |
| 187 |
stubbed out in the interpreter used to process the scripts, |
| 188 |
so these do not cause problems. |
| 189 |
If scripts call into other packages in global code, |
| 190 |
these calls are handled by a stub \fBunknown\fR command. |
| 191 |
However, if scripts make variable references to other package's |
| 192 |
variables in global code, these will cause errors. That is |
| 193 |
also bad coding style. |
| 194 |
.PP |
| 195 |
If binary files have dependencies on other packages, things |
| 196 |
can become tricky because it is not possible to stub out |
| 197 |
C-level APIs such as \fBTcl_PkgRequire\fR API |
| 198 |
when loading a binary file. |
| 199 |
For example, suppose the BLT package requires Tk, and expresses |
| 200 |
this with a call to \fBTcl_PkgRequire\fR in its \fBBlt_Init\fR routine. |
| 201 |
To support this, you must run \fBpkg_mkIndex\fR in an interpreter that |
| 202 |
has Tk loaded. You can achieve this with the |
| 203 |
\fB\-load \fIpkgPat\fR option. If you specify this option, |
| 204 |
\fBpkg_mkIndex\fR will load any packages listed by |
| 205 |
\fBinfo loaded\fR and that match \fIpkgPat\fR |
| 206 |
into the interpreter used to process files. |
| 207 |
In most cases this will satisfy the \fBTcl_PkgRequire\fR calls |
| 208 |
made by binary files. |
| 209 |
.PP |
| 210 |
If you are indexing two binary files and one depends on the other, |
| 211 |
you should specify the one that has dependencies last. |
| 212 |
This way the one without dependencies will get loaded and indexed, |
| 213 |
and then the package it provides |
| 214 |
will be available when the second file is processed. |
| 215 |
You may also need to load the first package into the |
| 216 |
temporary interpreter used to create the index by using |
| 217 |
the \fB\-load\fR flag; |
| 218 |
it will not hurt to specify package patterns that are not yet loaded. |
| 219 |
.PP |
| 220 |
If you have a package that is split across scripts and a binary file, |
| 221 |
then you should avoid the \fB\-load\fR flag. The problem is that |
| 222 |
if you load a package before computing the index it masks any |
| 223 |
other files that provide part of the same package. |
| 224 |
If you must use \fB\-load\fR, |
| 225 |
then you must specify the scripts first; otherwise the package loaded from |
| 226 |
the binary file may mask the package defined by the scripts. |
| 227 |
.SH "SEE ALSO" |
| 228 |
package(n) |
| 229 |
.SH KEYWORDS |
| 230 |
auto-load, index, package, version |
| 231 |
'\"Local Variables: |
| 232 |
'\"mode: nroff |
| 233 |
'\"End: |