item2
Next»

contents

 

Introduction - 1

History - 2

Overview - 3

Advanced features - 4

Work in progress - 5

One possibility - 6

Conclusion - 7

 

References

Appendix 1 - blowfish

Appendix 2 - tkspline

3.3 - Building Packages

But shared libraries are much more convenient when distributed using the Tcl package facility. Using this mode, Critcl will generate a Tcl package structure (including the pkgIndex.tcl file). The structure is arranged so that the results of invocations on different architectures are accumulated in platform specific directories and when run, the pkgIndex.tcl file loads the shared library for the current platform. These package structures are useful for creating cross-platform applications - such as Starkits that may be run on several architectures.

So, let's update our  mymath.tcl with a "package provide" command:

   package require critcl
   package provide mymath 1.0

   critcl::cproc noop {} void {}

   critcl::cproc add {int x int y} int {
     return x + y;
   }

   critcl::cproc cube {int x} int {
     return x * x * x;
   }

and build the package using:

   $ critcl -pkg mymath
   Source: mymath.tcl
   Library: mymath.dylib
   Package: lib/mymath

(Note that the .tcl extension is optional on the Critcl command line).

Copying the lib/mymath directory to somewhere on Tcl's auto_path, or to the lib directory of a Starkit, will allow the mymath package to be loaded as follows:

   $ tclkit
   % package require mymath
   1.0
   % add 1 2
   3

As we have seen, CriTcl  generates a lib/mymath directory next to the mymath.tcl source.  Looking inside this we see:

   lib
   `-- mymath
      |-- critcl.tcl
     |-- pkgIndex.tcl
      `-- Darwin-ppc
           |-- critcl.tcl
           `-- mymath.dylib  

Firstly, note that there are two Tcl scripts - critcl.tcl and pkgIndex.tcl. The first contains scripts used by the latter:

   source [file join $dir critcl.tcl]
   critcl::loadlib $dir. mymath 1.0

The critcl.tcl file is more complex - it provides dummy definitions for each Critcl procedure (in case they are called from the other scripts) plus two key procedures:

  • critcl::loadlib which is used by pkgIndex.tcl to load the appropriate shared library and any Tcl files
  • critcl::platform which generates a normalised  name for each platform  (needed because  the contents of tcl_platform array are not always consistent across different architectures)

Note also that there is a platform specific directory containing the shared library, plus a small critcl.tcl script that stores the values of certain Critcl parameters from the build process, in case they are referred to by the package Tcl script.

As the package is built on multiple platforms, Critcl will add similar directories. For example, if we copy mymath.tcl and the lib directory to a Linux platform and run Critcl again:

   $ critcl -pkg mymath
   Source: mymath.tcl
   Library: mymath.so
   Package: lib/mymath

Critcl takes care to store the cached and intermediate files in separate per-user/per-platform directories, so it would be safe to have both the mymath source and the user's home directory on a shared network drive.

see also

Critcl Home Page

Tclkit Home Page

Starkit Home Page

Wikit Home Page

Tclers' Wiki

Steve's Website

Jean-Claude's Website

Paper by S. Landers & J.C. Wippler, as presented at Tcl/Tk 2002 conference - see also original PDF.

Papers & Presentations