pkg_mkIndex(n)
π pkg_mkIndex(n) β the Tcl built-in that builds your package index π
okay bestie, gather round, because we're talking about pkg_mkIndex from the Tcl Built-In Commands manual, and honestly? it's giving organizational queen. β¨
πΈ NAME πΈ
pkg_mkIndex β build an index for automatic loading of packages π
β¨ SYNOPSIS β¨
pkg_mkIndex ?options...? dir ?pattern pattern ...?
π¦ DESCRIPTION π¦
Pkg_mkIndex is a utility procedure that's part of the standard Tcl library, and no cap, it's lowkey the backbone of the whole vibe. It creates index files that let packages get loaded automatically whenever package require commands run. π
To use pkg_mkIndex, literally just follow these steps, bestie:
- Create the package(s). π± Each package can be one or more Tcl script files or binary files. Binary files have to be suitable for loading with the
loadcommand using a single argument β so for example, if the file istest.so, it must be possible to load it with the commandload test.so. Each script file must contain apackage providecommand to declare the package and its version number, and each binary file must contain a call toTcl_PkgProvide. That part is not optional, iykyk. - Create the index by invoking
pkg_mkIndex. π₯ Thedirargument is the name of a directory, and eachpatternargument is a glob-style pattern that picks out script or binary files indir. The default pattern is*.tcland*.[info sharedlibextension].
Pkg_mkIndex will create a file pkgIndex.tcl in dir with package information about all the files given by the pattern arguments. How does it know all that? It loads each file into a child interpreter and watches what packages and new commands show up β which is exactly why it's essential to have those package provide commands or Tcl_PkgProvide calls in the files, like we said above. π
If you've got a package split across scripts and binary files, or if your files have dependencies on each other, you may have to use the -load option or adjust the order in which pkg_mkIndex processes the files. See COMPLEX CASES below. π
- Install the package as a subdirectory of one of the directories given by the
tcl_pkgPathvariable. π¦ If$tcl_pkgPathcontains more than one directory, machine-dependent packages (like the ones with binary shared libraries) should normally go under the first directory, and machine-independent packages (like the ones with only Tcl scripts) should go under the second directory. The subdirectory should include the package's script and/or binary files plus thepkgIndex.tclfile. As long as the package is installed as a subdirectory of a directory in$tcl_pkgPath, it will automatically be found duringpackage requirecommands. slay.
If you install the package anywhere else, then you must make sure the directory containing the package is in the auto_path global variable, or is an immediate subdirectory of one of the directories in auto_path. Auto_path holds a list of directories that both the auto-loader and the package loader search, and by default it includes $tcl_pkgPath. The package loader also checks all of the subdirectories of the directories in auto_path. You can add a directory to auto_path explicitly in your application, or you can add the directory to your TCLLIBPATH environment variable: if that environment variable is present, Tcl initializes auto_path from it during application startup. π«Ά
- Use it, periodt. π Once all the above steps are done, all you have to do to use a package is invoke
package require. For example, if versions2.1,2.3, and3.1of packageTesthave been indexed bypkg_mkIndex, then the commandpackage require Testwill make version3.1available, and the commandpackage require -exact Test 2.1will make version2.1available. There may be many versions of a package across the various index files inauto_path, but only one will actually get loaded in a given interpreter, based on the first call topackage require. Different versions of a package may be loaded in different interpreters. π
π OPTIONS π
The optional switches are, and yes, we're doing all of them:
-directβ the generated index will implement direct loading of the package uponpackage require. This is the default. π-lazyβ the generated index will manage to delay loading the package until one of the commands provided by the package gets used, instead of loading it immediately uponpackage require. This is not compatible with the use ofauto_reset, and therefore its use is discouraged. β οΈ-loadpkgPatβ the index process will preload any packages that exist in the current interpreter and matchpkgPatinto the child interpreter used to generate the index. The pattern match usesstring matchrules, but without making case distinctions. See COMPLEX CASES below. π-verboseβ generate output during the indexing process. Output goes via thetclLogprocedure, which by default prints tostderr. π--β end of the flags, in casedirbegins with a dash. (sneaky little dash, we see you π)
π PACKAGES AND THE AUTO-LOADER π
The package management facilities overlap a little with the auto-loader, in that both arrange for files to be loaded on-demand. But listen β package management is a higher-level mechanism that uses the auto-loader for the last step in the loading process. π§
It's generally better to index a package with pkg_mkIndex rather than auto_mkindex, 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 package require commands. In contrast, auto_mkindex 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 pkg_mkIndex and auto_mkindex. And if you use pkg_mkIndex to index a package, its commands cannot be invoked until package require has been used to select a version; in contrast, packages indexed with auto_mkindex can be used immediately since there is no version control. π
π₯ HOW IT WORKS π₯
Pkg_mkIndex depends on the package unknown command, the package ifneeded command, and the auto-loader. Here's the tea:
The first time a package require command is invoked, the package unknown script gets invoked. This is set by Tcl initialization to a script that evaluates all of the pkgIndex.tcl files in the auto_path. Those pkgIndex.tcl files contain package ifneeded commands for each version of each available package; these commands invoke package provide commands to announce that the package is available, and they set up auto-loader information to load the files of the package. β¨
If the -lazy flag was provided when the pkgIndex.tcl was generated, a given file of a given version of a given package is not actually loaded until the first time one of its commands is invoked. So after invoking package require you may not see the package's commands in the interpreter β but you will be able to invoke the commands, and they'll be auto-loaded. main character energy honestly. π
π DIRECT LOADING π
Some packages β for instance packages that use namespaces and export commands, or those that require special initialization β might select that their package files be loaded immediately upon package require, instead of delaying the actual loading to the first use of one of the package's commands. This is the default mode when generating the package index. It can be overridden by specifying the -lazy argument. πΈ
π COMPLEX CASES π
Most complex cases of dependencies among scripts and binary files, and packages being split among scripts and binary files, are handled OK. However, you may have to adjust the order in which files are processed by pkg_mkIndex. These issues are described in detail below. π
If each script or file contains one package, and packages are only contained in one file, then things are easy β you simply specify all files to be indexed in any order with some glob patterns. π
In general, it is OK for scripts to have dependencies on other packages. If scripts contain package require commands, these are stubbed out in the interpreter used to process the scripts, so they don't cause problems. If scripts call into other packages in global code, those calls are handled by a stub unknown command. However, if scripts make variable references to other packages' variables in global code, these will cause errors. That is also bad coding style, just saying. π
If binary files have dependencies on other packages, things can become tricky, because it is not possible to stub out C-level APIs such as the Tcl_PkgRequire API when loading a binary file. For example, suppose the BLT package requires Tk, and expresses this with a call to Tcl_PkgRequire in its Blt_Init routine. To support this, you must run pkg_mkIndex in an interpreter that has Tk loaded. You can achieve this with the -load pkgPat option. If you specify this option, pkg_mkIndex will load any packages listed by info loaded that match pkgPat into the interpreter used to process files. In most cases this will satisfy the Tcl_PkgRequire calls made by binary files. π₯
If you are indexing two binary files and one depends on the other, you should specify the one that has dependencies last. That way the one without dependencies gets loaded and indexed first, and then the package it provides will be available when the second file is processed. You may also need to load the first package into the temporary interpreter used to create the index by using the -load flag; it won't hurt to specify package patterns that aren't loaded yet. π
If you have a package that is split across scripts and a binary file, then you should avoid the -load flag. The problem is that if you load a package before computing the index, it masks any other files that provide part of the same package. If you must use -load, then you must specify the scripts first; otherwise the package loaded from the binary file may mask the package defined by the scripts. π
π SEE ALSO π
β KEYWORDS β
auto-load, index, package, version
and that's the whole page, bestie π«Ά this one came from the Tcl 8.3 era of the pkg_mkIndex(n) manual, so if you're out here indexing packages like it's 2000-something, you're in exactly the right place. go forth and package require, you gorgeous thing πβ¨π
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- Copyright (c) 1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms