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