\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#59: Embed Build Information in Tcl Binary Library}
\date{September 04, 2001}
\author{Andreas Kupries}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#59}&\textbf{Embed Build Information in Tcl Binary Library}\\\hline
Author&

Andreas Kupries \tipmail{andreas_kupries}{users.sourceforge.net}
\\
Created&Tuesday, \(\text{4}^{\text{th}}\) September 2001\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.16 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP provides an interface through which Tcl may be queried for information on its own configuration, in order to extract the information directly instead of reading it from a Bourne shell file. An important reason to do this is to have the information not only available but also tightly bound to the binary configured by it, so that the information doesn't get lost.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Foreword}

This TIP proposes a rather small change to Tcl and tries very hard to follow the KISS principle. Given that the casual observer might find it rather long, be assured, the actual specification in here is not very long, nor complicated. Most of the following explanations were added to preserve the KISS principle and head off attempts to extend the TIP beyond its small goal and scope.

Note: All instances of ``Tcl library'' in the following text refer to the generated installable library and not the script library coming with the core.

\section{Background and Rationale}

The main reason for writing this TIP are the disadvantages inherent in the current way of storing the configuration of Tcl, namely in the file \textit{tclConfig.sh}.
\begin{itemize}
\item{}
It is a separate file, easily lost or not installed at all, making it difficult for extension developers to access this information.
\begin{description}
\item[Note]
The non-installation of development files like \textit{tclConfig.sh} might even be required by through vendor policies and such and thus not under the control of the package author or builder.
\end{description}
\item{}
The name does not convey that \textit{tclConfig.sh} contains platform and build specific information. When installing different builds this usually leads to clashes. This makes it again difficult for extension developers to find the right file for their current build.
\item{}
Not every extension generates such a file for use by other extensions.
\end{itemize}

Thus, this TIP proposes:
\begin{itemize}
\item{}
an extension of the public API so that extensions are able to define configuration introspection commands and to declare the returned information during initialization with the information embedded into their installable libraries during compilation.
\item{}
to embed the information about the configuration of the Tcl library as strings into the generated installable library and make them accessible at the script level through Tcl variables, thus allowing developers on any platform Tcl compiles on to access this information.
\end{itemize}

The file \textit{tclConfig.sh} is \textit{not} replaced by this system, both sets of information exist in parallel.

Neither is the variable \textit{tcl\_platform} replaced. This means that some information, like \textit{threaded}, is held redundantly. Other information in \textit{tcl\_platform}, like \textit{user} is runtime and not configuration information. The operating system information is important to a build system, but this is out of the scope of this TIP.

\section{Interface Specification}

Any embedded information is made accessible at the Tcl level through a new command. The name of the command used by Tcl itself is \textit{::tcl::pkgconfig}. Extensions have to use their own commands. These commands will be named \textit{pkgconfig} too and have to be placed within in the namespaces owned by the extensions initializing them.

At the C-level the public API of the Tcl core is extended with a single function to register the embedded configuration information. This function is added to the public stub table of the Tcl core so that it can be used by Tcl and extensions to register their own configuration information in the system during initialization.

The function takes three (3) arguments; first, the name of the package registering its configuration information, second, a pointer to an array of structures, and third a string declaring the encoding used by the configuration values. Each element of the array refers to two strings containing the key and the value associated with that key. The end of the array is signaled by an empty key.

Formalized, name and signature of this new function are
\begingroup\small\begin{verbatim}
 Tcl_RegisterConfig (CONST char* pkgName, Config* configuration, CONST char* valEncoding)

 typedef struct Config {
    char* key;
    char* value;
 }
\end{verbatim}
\endgroup

The string \textit{valEncoding} contains the name of an encoding known to Tcl. All these names are use only characters in the ASCII subset of UTF-8 and are thus implicity in the UTF-8 encoding. It is expected that keys are legible english text and therefore using the ASCII subset of UTF-8. In other words, they are expected to be in UTF-8 too. The values associated with the keys can be any string however. For these the contents of \textit{valEncoding} define which encoding was used to represent the characters of the strings.

During compile time the value of \textit{valEncoding} is specified as a makefile variable for non-\textit{configure} based build systems and through the new option \textit{-{}-with-encoding=FOO} of configure otherwise. The default value is \textit{iso8859-1}.

This approach gives us all what we desire with not too many drawbacks.
\begin{itemize}
\item{}
The default case (no special characters) requires no action on part of the builder at all.
\item{}
The non-default case (path containing special characters like Kanji) is supported.
\item{}
Cross-compilation is unimpeded and no more complex than normal compilation.
\item{}
The requirement for conversion of strings is a drawback, but should not have a big impact on performance. It has no impact on the performance of scripts which do not use the embedded information. The impact is even more negligigble if the result of the conversion is cached.
\end{itemize}

The function will
\begin{itemize}
\item{}
create a namespace having the provided \textit{pkgName}, if not yet existing.
\item{}
create the command \textit{pkgconfig} in that namespace and link it to the provided information so that the keys from \textit{configuration} and their associated values can be retrieved through calls to \textit{pkgconfig}.
\end{itemize}

The command \textit{pkgconfig} will provide two subcommands, \textit{list} and \textit{get}. The first subcommand, \textit{list} takes no arguments and returns a list containing the names of the defined keys. The second subcommand takes one argument, the name of a key and returns the string associated with that key.

\section{How to Gather the Embedded Information}

The information to be embedded is gathered in a platform-specific way and written into the file \textit{generic/tclPkgConfig.c}. The different platforms may employ platform specific intermediate files to hold the information, but in the compilation phase only \textit{tclPkgConfig.c} will be used.
\begin{itemize}
\item{}
Under unix it is determined primarily by the existing \textit{configure} script. The configuration information coming from the Makefile, or from other compile time means, is embedded into the \textit{tclConfig.c} file by means of preprocessor statements (\#ifdef ... \#endif).
\item{}
For the Windows and Mac platforms volunteers may have to create files \textit{tclWinConfig.c.some-ext} containing this information for each supported build environment, like VC++, Borland, Cygwin, etc.

\textit{tclWinConfig.c.vc} = VC++.

\textit{tclWinConfig.c.bc} = Borland.

\textit{tclWinConfig.c.in} = Cygwin. \textit{.in} is used because Cygwin can use configure to determine the values and embed them into a template.
\item{}
As for other platforms, these are handled either like Unix or like Mac, depending on the availability and usability of \textit{configure}.

Volunteers are required to write the appropriate files for their build environment.
\end{itemize}

\section{Specification of Tcl Configuration Information}

The configuration information registered by Tcl itself is specified here. A discussion of the choices made here follows in the next section. Please read this discussion before commenting on the specification.

The values associated with the keys below are all of one of the following types:
\begin{itemize}
\item{}
Boolean flag. Allowed values are all the values which are accepted by Tcl itself. Examples are:
\begingroup\small\begin{verbatim}
       true, false, on, off, 1, 0
\end{verbatim}
\endgroup
\item{}
String. General container for all other information.
\item{}
Templated string. With respect to placeholders in the same format as 'Script' below, but does not have to be a valid Tcl script.
\item{}
Script. A string containing a full Tcl script. The user should handle this string like a procedure body. The script is allowed to contain placeholders to be filled by the user of the string. Placeholders follow the syntax of full-braced Tcl variables, i.e. \(''\)\$\{some\_name\}'. The actual values can be filled in by the user of the configuration information. Possible ways to do so are [regsub], [string map] or [subst -nocommand]. The best way however would be to use the script as a procedure body, with the placeholders as the arguments of the procedure. This will avoid many problems regarding bracing and the protection of special characters.

Which placeholders are possible for a particular script or template is described together with the meaning of the key.

Beyond the placeholders a script or templated string is allowed to contain references to other keys returned by the \textit{config} command. These references use the same variable syntax as the placeholders.
\end{itemize}

The registered keys follow below. They will be always present with some value. Non-boolean keys not applicable to a particular platform will contain the empty string as their value.
\begin{itemize}
\item{}
Configuration of Tcl itself:
\begin{itemize}
\item{}
\textit{debug}. Boolean flag. Set to false if Tcl was not compiled to contain debugging information.
\item{}
\textit{threaded}. Boolean flag. Set to false if Tcl was not compiled as thread-enabled.
\item{}
\textit{profiled}. Boolean flag. Set to false if Tcl was not compiled to contain profiling statements.
\item{}
\textit{64bit}. Boolean flag. Set to false if Tcl was not compiled in 64bit mode.
\item{}
\textit{optimized}. Boolean flag. Set to false if Tcl was compiled without compiler optimizations.
\item{}
\textit{mem\_debug}. Boolean flag. Set to false if Tcl has no memory debugging compiled into it.
\item{}
\textit{compile\_debug}. Boolean flag. Set to false if Tcl has no bytecode compiler debugging compiled in.
\item{}
\textit{compile\_stats}. Boolean flag. Set to false if Tcl has no bytecode compiler statistics compiled in.
\end{itemize}
\item{}
Installation configuration of Tcl. In other words, various important locations.
\begin{itemize}
\item{}
\textit{prefix,runtime}. String. The directory for platform independent files as seen by the interpreter during runtime.
\item{}
\textit{exec\_prefix,runtime} String. The directory for platform dependent files as seen by the interpreter during runtime.
\item{}
\textit{prefix,install}. String. The directory for platform independent files as seen by the installer at install-time.
\item{}
\textit{exec\_prefix,install}. String. The directory for platform dependent files as seen by the installer at install-time.
\end{itemize}
\end{itemize}

\section{Discussion}

The placement of this information into a separate package was proposed but rejected because of the trouble of finding the right information for the right library in the case of multiple configurations installed into the same directory space. Embedding into the library does not cost much space and binds the information tightly to the right spot.

Another reason to do it this way is that this enables us to embed information coming from the Makefile itself (like \textit{MEM\_DEBUG}) or from other compile time means. This would not be possible for a file generated solely by the Tcl configure. It would also restrict the embedding to the platforms which allow the use of \textit{configure} script.

The usage of a separate package to just access the information placed into the Tcl library was also proposed. This was rejected too, due to the overhead for the management of the package in comparison to the small size of the code actually involved.

Another proposal rejected in the early discussions was to have this TIP define an entire build system based upon Tcl. This TIP is certainly a step in this direction and facilitates the building of such a build system (sic!). Still, specifying such here was seen as too large a step right now, with too many issues to be solved and thus delaying the implementation of this TIP.

Only the configuration of the particular variant of the Tcl library or extension which was generated is recorded in the library. No attempt is made to record the information required to allow the compilation of any possible variant of an extension. Doing so would reach again into the bigger topic of specifying a full build system. We've already established that as being out of the intended scope of this TIP.

Note further that the scheme as specified above does not prevent us from adding the full information in a later stage. In other words, it does not restrict the development of a more powerful system in the future.

This should be enough reasoning to allow the acceptance of even this admittedly simple system.

The configuration information registered by Tcl is currently a very small subset of the information in \textit{tclConfig.sh}. A future TIP is planned to provide the missing information in a regular and generalized manner.

If an extension requires more information than provided by the Tcl configuration it will have to obtain this information itself. For instance, TclBlend requires a CLASSPATH, the name of a Java compiler, etc. whereas the TclPython and TclPerl extensions require paths to those environments, etc. It is not reasonable that the configure script for Tcl itself have to accommodate all requirements of all extensions of Tcl. Instead, the configure scripts or whatever other means is used to obtain the configuration information for the extensions should reflect their needs, and register the requirements gathered into their own configuration command. Note that an extension is only expected to create variables for information unique to it. Everything else can be had from the configuration command of Tcl and the extensions it depends on.

This TIP is not in opposition to \cite{tip34} but rather fleshes out one of the many details in the specification which were left open by that TIP.

This TIP also does not propose to change the process for building Tcl itself. The goal is rather to make the building of extensions easier in the future.

A naming convention for keys returned by the \textit{config} command would have been possible but would also require quite a lot more text, both in careful definition of the general categories and in explanations of the choices made.

\section{Implementation}

Work on implementing this feature is tracked at Tcl Patch 507083 at the Tcl project at SourceForge. Implementation effort also takes place on the tip-59-implementation branch in the Tcl CVS repository (see \cite{tip31}).

\section{Copyright}

This document is in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#2}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#31]{tip31}
Don Porter,
miguel sofer,
Jeff Hobbs,
Kevin Kenny,
David Gravereaux,
Donal K. Fellows,
Andreas Kupries,
Donal K. Fellows,
,
Kevin Kenny,
\emph{CVS tags in the Tcl and Tk repositories},
on-line at \url{http://purl.org/tcl/tip/31.html}
\bibitem[TIP \#34]{tip34}
Mo DeJong,
Andreas Kupries,
\emph{Modernize TEA Build System},
on-line at \url{http://purl.org/tcl/tip/34.html}
\end{thebibliography}
\end{document}
