When a program uses Tcl and/or Tk, its configuration script (if any) has to check for the presence of these libraries and retrieve some information about them, such as their version, the required link flags, etc. Unfortunately, the process to achieve this is rather obscure (AFAIK), thus it is unknown by many people. This results in unportable (and broken) configuration scripts.

So how is this done? Both libraries install a shell script which carries all the required information, so all you have to do is find the script, read it, and use the variables you need. These scripts are named tclConfig.sh and tkConfig.sh respectively, and are placed in the lib directory relative to their installation prefix. I.e., most of the times, they'll be placed in /usr/lib, /usr/local/lib or other locations such as /usr/pkg/lib in NetBSD.

Let's see a bit of code that shows how to get Tcl's link flags using GNU Autoconf:

AC_ARG_WITH(tcl,
AS_HELP_STRING(--with-tcl=PREFIX, [Enable Tcl support]),,
with_tcl=yes)
if test ${with_tcl} != no; then
if test ${with_tcl} = yes; then
tcl_dirs="/usr/local /usr"
else
tcl_dirs="${with_tcl}"
fi

AC_MSG_CHECKING(for tclConfig.sh script)
found=no
for d in ${tcl_dirs}; do
if test -f ${d}/lib/tclConfig.sh; then
found=yes
break
fi
done
AC_MSG_RESULT(${found})

if test ${found} = no; then
AC_MSG_ERROR(Tcl support requested but the library cannot
be found)
fi
. ${d}/lib/tclConfig.sh
AC_MSG_NOTICE(using "${TCL_LIBS}" to link to Tcl)
AC_SUBST(TCL_LIBS)
fi

The above code snippet does the following:

  1. It defines a command line argument (--with-tcl) to enable or disable Tcl support. This flag accepts a directory name as its argument (aside yes and no) which is treated as Tcl's installation prefix. Note that, if Tcl support was not optional, you'd use AC_ARG_VAR to specify the prefix rather than a flag (because neither of "enable" nor "with" semantics match what you'd want to do).
  2. If Tcl was enabled, it checks whether the user gave a path or simply let the argument take its default value. In the later case, the script falls back to a sane list of directories; otherwise, only the directory given by the user is used.
  3. It walks the directory list looking for a tclConfig.sh script and, when found, stops the process.
  4. If the script wasn't found, the configuration is aborted with a descriptive error message.
  5. If the script was found, it's read and the TCL_LIBS variable is added to the substitution list so that it's available from the Makefiles. We'd do this for any other variable provided by the script. At this point, you'd also check for the detected version (TCL_VERSION and other variables) and compare it to your requisites.

Of course, all of the above also applies to Tk, just changing the name of one library for the other.

Go to posts index

Comments from the original Blogger-hosted post: