'\" '\" Copyright (c) 1996 Sun Microsystems, Inc. '\" '\" See the file "license.terms" for information on usage and redistribution '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. '\" '\" RCS: @(#) $Id: pkgMkIndex.n,v 1.3 1998/10/17 00:16:38 escoffon Exp $ '\" .so man.macros .TH pkg_mkIndex n 7.6 Tcl "Tcl Built-In Commands" .BS '\" Note: do not modify the .SH NAME line immediately below! .SH NAME pkg_mkIndex \- Build an index for automatic loading of packages .SH SYNOPSIS .nf .VS 8.0.3 \fBpkg_mkIndex ?\fI-direct\fR? ?\fI-verbose\fR? ?\fI-nopkgrequire\fR? \fIdir\fR ?\fIpattern pattern ...\fR? .VE .fi .BE .SH DESCRIPTION .PP \fBPkg_mkIndex\fR is a utility procedure that is part of the standard Tcl library. It is used to create index files that allow packages to be loaded automatically when \fBpackage require\fR commands are executed. To use \fBpkg_mkIndex\fR, follow these steps: .IP [1] Create the package(s). Each package may consist of one or more Tcl script files or binary files. Binary files must be suitable for loading with the \fBload\fR command with a single argument; for example, if the file is \fBtest.so\fR it must be possible to load this file with the command \fBload test.so\fR. Each script file must contain a \fBpackage provide\fR command to declare the package and version number, and each binary file must contain a call to \fBTcl_PkgProvide\fR. .IP [2] Create the index by invoking \fBpkg_mkIndex\fR. The \fIdir\fR argument gives the name of a directory and each \fIpattern\fR argument is a \fBglob\fR-style pattern that selects script or binary files in \fIdir\fR. .VS 8.0.3 The default pattern is \fB*.tcl\fR and \fB*.[info sharedlibextension]\fR. If the optional \fI-direct\fR argument is given, the generated index will manage to load the package immediately upon \fBpackage require\fR instead of delaying loading until actual use of one of the commands. .VE \fBPkg_mkIndex\fR will create a file \fBpkgIndex.tcl\fR in \fIdir\fR with package information about all the files given by the \fIpattern\fR arguments. It does this by loading each file and seeing what packages and new commands appear (this is why it is essential to have \fBpackage provide\fR commands or \fBTcl_PkgProvide\fR calls in the files, as described above). .IP [3] Install the package as a subdirectory of one of the directories given by the \fBtcl_pkgPath\fR variable. If \fB$tcl_pkgPath\fR contains more than one directory, machine-dependent packages (e.g., those that contain binary shared libraries) should normally be installed under the first directory and machine-independent packages (e.g., those that contain only Tcl scripts) should be installed under the second directory. The subdirectory should include the package's script and/or binary files as well as the \fBpkgIndex.tcl\fR file. As long as the package is installed as a subdirectory of a directory in \fB$tcl_pkgPath\fR it will automatically be found during \fBpackage require\fR commands. .RS .LP If you install the package anywhere else, then you must ensure that the directory containing the package is in the \fBauto_path\fR global variable or an immediate subdirectory of one of the directories in \fBauto_path\fR. \fBAuto_path\fR contains a list of directories that are searched by both the auto-loader and the package loader; by default it includes \fB$tcl_pkgPath\fR. The package loader also checks all of the subdirectories of the directories in \fBauto_path\fR. You can add a directory to \fBauto_path\fR explicitly in your application, or you can add the directory to your \fBTCLLIBPATH\fR environment variable: if this environment variable is present, Tcl initializes \fBauto_path\fR from it during application startup. .RE .IP [4] Once the above steps have been taken, all you need to do to use a package is to invoke \fBpackage require\fR. For example, if versions 2.1, 2.3, and 3.1 of package \fBTest\fR have been indexed by \fBpkg_mkIndex\fR, the command \fBpackage require Test\fR will make version 3.1 available and the command \fBpackage require \-exact Test 2.1\fR will make version 2.1 available. There may be many versions of a package in the various index files in \fBauto_path\fR, but only one will actually be loaded in a given interpreter, based on the first call to \fBpackage require\fR. Different versions of a package may be loaded in different interpreters. .SH "PACKAGES AND THE AUTO-LOADER" .PP The package management facilities overlap somewhat with the auto-loader, in that both arrange for files to be loaded on-demand. However, package management is a higher-level mechanism that uses the auto-loader for the last step in the loading process. It is generally better to index a package with \fBpkg_mkIndex\fR rather than \fBauto_mkindex\fR because the package mechanism provides version control: several versions of a package can be made available in the index files, with different applications using different versions based on \fBpackage require\fR commands. In contrast, \fBauto_mkindex\fR does not understand versions so it can only handle a single version of each package. It is probably not a good idea to index a given package with both \fBpkg_mkIndex\fR and \fBauto_mkindex\fR. If you use \fBpkg_mkIndex\fR to index a package, its commands cannot be invoked until \fBpackage require\fR has been used to select a version; in contrast, packages indexed with \fBauto_mkindex\fR can be used immediately since there is no version control. .SH "HOW IT WORKS" .PP \fBPkg_mkIndex\fR depends on the \fBpackage unknown\fR command, the \fBpackage ifneeded\fR command, and the auto-loader. The first time a \fBpackage require\fR command is invoked, the \fBpackage unknown\fR script is invoked. This is set by Tcl initialization to a script that evaluates all of the \fBpkgIndex.tcl\fR files in the \fBauto_path\fR. The \fBpkgIndex.tcl\fR files contain \fBpackage ifneeded\fR commands for each version of each available package; these commands invoke \fBpackage provide\fR commands to announce the availability of the package, and they setup auto-loader information to load the files of the package. .VS 8.0.3 Unless the \fI-direct\fR flag was provided when the \fBpkgIndex.tcl\fR was generated, .VE a given file of a given version of a given package isn't actually loaded until the first time one of its commands is invoked. Thus, after invoking \fBpackage require\fR you .VS 8.0.3 may .VE not see the package's commands in the interpreter, but you will be able to invoke the commands and they will be auto-loaded. .VS 8.0.3 .SH "DIRECT LOADING" .PP Some packages, for instance packages which use namespaces and export commands or those which require special initialization, might select that their package files be loaded immediately upon \fBpackage require\fR instead of delaying the actual loading to the first use of one of the package's command. This mode is enabled when generating the package index by specifying the \fI-direct\fR argument. .VE .VS 8.0.3 .SH "INDEXING OF DEPENDENT PACKAGES" .PP Earlier versions of \fBpkg_mkIndex\fR were unable to handle some packages that did a \fIpackage require\fR for a second package also to be indexed, and then proceeded to execute code from that package (for example in a \fInamespace eval\fR construct). This happended because the autoloading mechanism did not have access to the index file being built, thus causing \fIpackage require\fR to fail. \fBPkg_mkIndex\fR circumvented this problem by redefining \fIpackage require\fR to be a null operation, which in turn then caused the indexing to fail when procedures from the second package were being called in the first. .LP \fBPkg_mkIndex\fR now implements \fIpackage require\fR. Additionally, it does the indexing in multiple passes, until either all files have been indexed, or until the current pass could index no more files. For each pass, files that were indexed are added to a partial \fBpkgIndex.tcl\fR, which is reloaded at the start of each pass. In the case described above, for example, \fBpkg_mkIndex\fR will do two passes. During the first pass, the first package will not index, but the second will. Then, during the second pass, the first package will be able to \fIpackage require\fR the second, and therefore will also be indexed. If the \fI-verbose\fR optional argument is givem, \fBpkg_mkIndex\fR will display its progress. .LP Under some conditions, \fBpkg_mkIndex\fR may not be able to index any file during a pass; for example, if package \fIa\fR requires package \fIb\fR, and package \fIb\fR requires package \fIa\fR, neither one can be indexed. In these situations, you can give the \fI-nopkgrequire\fR optional argument; this will cause \fBpkg_mkIndex\fR to revert to the old behaviour, where \fIpackage require\fR is a null operation. .VE .SH KEYWORDS auto-load, index, package, version