critcl_introduction - Introduction To CriTcl
Welcome to the C Runtime In Tcl, CriTcl for short, a system to build C extension packages for Tcl on the fly, from C code embedded within Tcl scripts, for all who wish to make their code go faster.
CriTcl started life as an experiment by Jean-Claude Wippler and was a self-contained Tcl package to build C code into a Tcl/Tk extension on the fly. It was somewhat inspired by Brian Ingerson's Inline for Perl, but is considerably more lightweight.
It is for the last 5% to 10% when pure Tcl, which does go a long way, is not sufficient anymore. I.e. for
when the last bits of performance are needed,
access to 3rd party libraries,
hiding critical pieces of your library or application, and
simply needing features provided only by C.
The following sections first reference a few related documents which may or may not be of interest to you, depending on if you are just a plain user of the system, trying to get and build/install it, or are going deep into the internals for either edification or modification.
This is followed by an introduction to the modes of operation supported by the system, and its general architecture as seen by a user.
CriTcl can be used in three different modes of operation, called
Compile & Run, and
Generate TEA Package
Of these three Compile & Run came first and is the default when using the package directly. In that case the package collects the C fragments, builds them as needed, and caches the results for quick reuse when the same code is used in the future again.
The second mode, Generate Package, was introduced to enable the creation of (prebuilt) deliverable packages which do not depend on the existence of a build system, i.e. C compiler, on the target machine. This was originally done through the experimental Critbind tool, and is now handled by the CriTcl Application, also named critcl.
Newly introduced with Critcl version 3 is Generate TEA Package. This mode constructs a directory hierarchy from the package which can later be built like a regular TEA package, i.e. using
.../configure --prefix ... make all isntall
The system consists of two main layers, as seen in the figure below, plus a support layer containing general packages the system uses during operation.
At the top we have an application built on top of the core packages, providing command line access to the second and third usage modes, i.e. Generate Package and Generate TEA Package.
Below that is the core package providing the essential functionality of the system, plus various utility packages which make common tasks more convenient.
Lastly a layer of supporting packages, mostly external to critcl.
For this pure-Tcl package to be fast users should get one of several possible accelerator packages:
tcllibc
Trf
md5c
The sources of Critcl, should you have gotten them, contain several larger examples show-casing various aspects of the system. These demonstration packages can all be found in the sub-directory "examples/" of the sources.
Lots of smaller examples can be found in the document Using CriTcl, an introduction to Critcl by way of a of examples. These focus more on specific critcl commands than the overall picture shown by the large examples mentioned in the previous paragraph.
The latest changes are found at the top.
Fixed issue 42. Clear ::errorInfo immediately after startup to prevent leakage of irrelevant (caught) errors into our script and confusing the usage code.
Fixed issue 40. Keep the order of libraries, and allow duplicates. Both are things which are occasionally required for proper linking.
Extended the utility package critcl::literals to declare a cproc result-type for a pool.
Further fixed the generated header to handle multiple inclusion.
Bumped version to 1.1.
Fixed issue with utility package critcl::bitmap.
Fixed the generated header to handle multiple inclusion.
Bumped version to 1.0.1.
Created new utility package critcl::enum for the quick and easy setup and use of mappings between C values and Tcl strings. Built on top of critcl::literals.
Added examples demonstrating the use of the utility packages critcl::literals, critcl::bitmap, and critcl::enum
Fixed issue #37, via pull request #38, with thanks to Jos DeCoster. Information was stored into the v::delproc and v::clientdata arrays using a different key than when retrieving the same information, thus failing the latter.
New convenience command critcl::include for easy inclusion of headers and other C files.
New command critcl::make to generate a local header of other C files for use by other parts of a package through inclusion.
New utility package critcl::literals for quick and easy setup of and access to pools of fixed Tcl_Obj* strings. Built on top of critcl::iassoc.
New utility package critcl::bitmap for quick and easy setup and use of mappings between C bitsets and Tcl lists whose string elements represent that set. Built on top of critcl::iassoc.
Fixed code version numbering forgotten with 3.1.9.
Fixed issue #35. In package mode (-pkg) the object cache directory is unique to the process, thus we do not need content-hashing to generate unique file names. A simple counter is sufficient and much faster.
Note that mode "compile & run" is not as blessed and still uses content-hasing with md5 to ensure unique file names in its per-user object cache.
Fixed issue where the ccommand forgot to use its body as input for the UUID generation. Thus ignoring changes to it in mode compile & run, and not rebuilding a library for changed sources. Bug and fix reported by Peter Spjuth.
Fixed issue #27. Added missing platform definitions for various alternate linux and OS X targets.
Fixed issue #28. Added missing -mXX flags for linking at the linux-{32,64}-* targets.
Fixed issue #29. Replaced the use of raw "cheaders" information in the processing of "cdefines" with the proper include directives derived from it.
Fixed the issue behind rejected pull request #30 by Andrew Shadura. Dynamically extract the stubs variable declarations from the Tcl header files and generate matching variable definitions for use in the package code. The generated code will now be always consistent with the headers, even when critcl's own copy of them is replaced by system headers.
Fixed issue #31. Accepted patch by Andrew Shadura, with changes (comments), for easier integration of critcl with OS package systems, replacing critcl's copies of Tcl headers with their own.
Fixed issue #32. Merged pull request by Andrew Shadura. Various typos in documentation and comments.
Fixed issue #34. Handle files starting with a dot better.
Fixed issue with package indices generated for Tcl 8.4. Join the list of commands with semi-colon, not newline.
Fixed issue #26 which brought up use-cases I had forgotten to consider while fixing bug #21 (see critcl 3.1.6).
Fixed issue #24. Extract and unconditionally display compiler warnings found in the build log. Prevents users from missing warnings which, while not causing the build to fail, may still indicate problems.
New feature. Output hook. All non-messaging user output is now routed through the command critcl::print, and users are allowed to override it when using the critcl application-as-package.
New feature, by Ashok P. Nadkarni. Platform configurations can inherit values from configurations defined before them.
Fixed issue #21. While the multi-definition of the stub-table pointer variables was ok with for all the C linkers seen so far C++ linkers did not like this at all. Reworked the code to ensure that this set of variables is generated only once, in the wrapper around all the pieces to assemble.
Fixed issue #22, the handling of the command identifier arguments of critcl::ccommand, critcl::cproc, and critcl::cdata. We now properly allow any Tcl identifier and generate proper internal C identifiers from them.
As part of this the signature of command critcl::name2c changed. The command now delivers a list of four values instead of three. The new value was added at the end.
Further adapted the implementation of package critcl::class, a user of critcl::name2c. This package is now at version 1.0.6 and requires critcl 3.1.6
Lastly fixed the mis-handling of option -cname in critcl::ccommand, and critcl::cproc.
Fixed issue #23.
Fixed issue #19. Made the regular expression extracting the MSVC version number more general to make it work on german language systems. This may have to be revisited in the future, for other Windows locales.
Fixed issue #20. Made option -tea work on windows, at least in a unix emulation environment like msys/mingw.
Bugfix in package critcl::class. Generate a dummy field in the class structure if the class has no class variables. Without this change the structure would be empty, and a number of compilers are not able to handle such a type.
Fixed a typo which broke the win64 configuration.
Fixed issue #16, a typo in the documentation of command critcl::class.
Enhancement. In detail:
Added new argument type "pstring", for "Pascal String", a counted string, i.e. a combination of string pointer and string length.
Added new methods critcl::argtypesupport and ::critcl::argsupport to define and use additional supporting code for an argument type, here used by "pstring" above to define the necessary structure.
Semi-bugfixes in the packages critcl::class and critcl::iassoc. Pragmas for the AS meta data scanner to ensure that the template files are made part of the package. Versions bumped to 1.0.4 and 1.0.1 respectively.
Enhancement. In detail:
Extended critcl::cproc to be able to handle optional arguments, in a limited way. This is automatically available to critcl::class cproc-based methods as well.
Bugfix in lassign emulation for Tcl 8.4. Properly set unused variables to the empty string. Bumped version of emulation package lassign84 to 1.0.1.
Bugfixes all around. In detail:
Fixed the generation of wrong#args errors for critcl::cproc and derived code (critcl::class cproc-based methods). Use NULL if there are no arguments, and take the offset into account.
Fixed the handling of package names by critcl::class. Forgot that they may contain namespace separators. Bumped to version 1.0.1.
Extended a critcl::class generated error message in instance creation for clarity. Bumped to version 1.0.2.
Added a new higher-level package critcl::iassoc.
This package simplifies the creation of code associating data with an interpreter via Tcl's Tcl_(Get|Set)AssocData() APIs. The user can concentrate on his data while all the necessary boilerplate C code to support this is generated by the package.
This package uses several of the new features which were added to the core critcl package, see below.
Added the higher-level package critcl::class.
This package simplifies the creation of C level objects with class and instance commands. The user can write a class definition with class- and instance-variables and -methods similar to a TclOO class, with all the necessary boilerplate C code to support this generated by the package.
This package uses several of the new features which were added to the core critcl package, see below.
Extended the API for handling TEApot metadata. Added the command critcl::meta? to query the stored information. Main use currently envisioned is retrieval of the current package's name by utility commands, for use in constructed names. This particular information is always available due to the static scan of the package file on execution of the first critcl command.
The new packages critcl::iassoc and critcl::class (see above) are users of this command.
Extended the API with a command, critcl::name2c, exposing the process of converting a Tcl name into base name, namespace, and C namespace. This enables higher-level code generators to generate the same type of C identifiers as critcl itself.
The new package critcl::class (see above) is a user of this command.
Extended the API with a command, critcl::source, executing critcl commands found in a separate file in the context of the current file. This enables easier management of larger bodies of code as it allows the user to split such up into easier to digest smaller chunks without causing the generation of multiple packages.
Related to the previous item, extended the API with commands to divert collection of generated C code into memory. This makes it easier to use the commands for embedded C code in higher-level code generators.
See the section Advanced: Diversions for details of the provided commands.
The new package critcl::class (see above) is a user of these facilities.
Extended the API with commands helping developers with the generation of proper C #line directives. This allows higher-level code generators to generate and insert their own directives, ensuring that compile errors in their code are properly attributed.
See the section Advanced: Location management for details of the provided commands.
The new packages critcl::iassoc and critcl::class (see above) are users of these facilities.
Extended the API with commands giving users the ability to define custom argument and result types for ::critcl::cproc.
See the section Advanced: Extending cproc for details of the provided commands.
Fixed the code generated by critcl::c++command. The emitted code handed a non-static string table to Tcl_GetIndexFromObj, in violation of the contract, which requires the table to have a fixed address. This was a memory smash waiting to happen. Thanks to Brian Griffin for alrerting us to the general problem.
Fixed github issue 10. The critcl application now delivers a proper exit code (1) on build failure, instead of always indicating success (status 0).
Fixed github issue 13. Handling of bufferoverflowU.lib for release builds was inconsistent with handling for debug builds. It is now identically handled (conditional) by both cases.
Documentation cleanup, mainly in the installation guide, and the README.md shown by github
Fixed bug in the new code for #line pragmas triggered when specifying C code without leading whitespace.
Extended the documentation to have manpages for the license, source retrieval, installer, and developer's guides.
Fixed generation of the package's initname when the incoming code is read from stdin and has no proper path.
Fixed github issue 11. Now using /LIBPATH instead of -L on Windows (libinclude configuration setting).
Extended critcl to handle -l:path format of -l options. GNU ld 2.22+ handles this by searching for the path as is. Good when specifying static libraries, as plain -l looks for shared libraries in preference over static. critcl handles it now, as older GNU ld's do not understand it, nor the various vendor-specific linkers.
Fixed github issue #12. Critcl now determines the version of MSVC in use and uses it to switch between various link debug options. Simplified the handling of bufferoverflowU.lib also, making use of the same mechanism and collapsing the two configurations sections we had back into one.
Reworked the insertion of #line pragmas into the generated C code to avoid limitations on the line number argument imposed by various compilers, and be more accurate.
Modified argument processing. Option -libdir now also implies -L for its argument.
Extended handling of option -show (critcl::showconfig) to list the path of the configuration file the data is coming from. Good for debugging configuration processing.
Extended the build script with targets to regenerate the embedded documentation, and diagrams, and to generate a release.
Fixed github issues 5 and 8, for the example build.tcl scripts. Working around a missing variable ::errorInfo. It should always be present, however there seem to be revisions of Tcl around which violate this assumption.
Fixed issue in compile-and-run mode where commands put into the auto_index are not found by Tcl's [unknown] command.
Fixed an array key mismatch breaking usage of client data and delete function for procedure. Reported by Jos DeCoster, with patch.
Implemented a command line option -L, an equivalent of option -I, just for library search paths.
Fixed github issues 5 and 8. Working around a missing variable ::errorInfo. It should always be present, however there seem to be revisions of Tcl around which violate this assumption.
Bugfixes all around. In detail:
Fixed recording of Tcl version requirements. Keep package name and version together, unbreaking generated meta data and generated package load command.
Fixed the build scripts: When installing, or wrapping for TEA, generate any missing directories
Modified the build scripts to properly exit the application when the window of their GUI is closed through the (X) button.
Removed an 8.5-ism (open wb) which had slipped into the main build script.
Modified the example build scripts to separate the output for the different examples (and packages) by adding empty lines.
stack::c example bugfix: Include API declarations for use in the companion files.
Extended the documentation: Noted the need for a working installation of a C compiler.
Extended the Windows target definitions and code to handle the manifest files used by modern MS development environments. Note that this code handles both possibilities, environment using manifests, and (old(er)) environments without.
Extended the Windows 64bit target definitions and code to auto-detect the need for the helper library "bufferoverflowU.lib" and reconfigure the compile and link commands appropriately. We assume that the library must be linked when present. This should be no harm if the library is present, yet not needed. Just superfluous. We search for the library in the paths specified by the environment variable LIB.
The command critcl::platform was deprecated in version 2.1, superceded by critcl::targetplatform, yet kept for compatibility. Now it has been removed.
The command critcl::compiled was kept with in version 2.1 with semantics in contradiction to its, for compatibility. This contradiction has been removed, changing the visible semantics of the command to be in line with its name.
The change to version 3 became necessary because of the two incompatible visible changes above.
Extended the application package with code handling a new option -tea. Specifying this option invokes a special mode where critcl generates a TEA package, i.e. wraps the input into a directory hierarchy and support files which provide it TEA-lookalike buildsystem.
This new option, and -pkg, exclude each other. If both are specified the last used option takes precedence.
The generated package directory hierarchy is mostly self-contained, but not fully. It requires not only a working installation of Tcl, but also working installations of the packages md5 and cmdline. Both of these are provided by the Tcllib bundle. Not required, but recommended to have installed are any of the packages which can accelerate md5's operation, i.e. cryptkit, tcllibc, or Trf.
Extended the critcl package with a new command critcl::scan taking the path to a ".critcl" file, statically scanning it, and returning license, version, a list of its companion files, list of imported APIs, and list of developer-specified custom configuration options. This data is the foundation for the TEA wrapping described above.
Note that this is a static scan. While the other build modes can (must) execute the ".critcl" file and make platform-specific decisions regarding the assembled C code, companion files, etc. the TEA wrap mode is not in a position to make platform-specific decisions. It has to wrap everything which might conceivably be needed when actually building. Hence the static scan. This has however its own set of problems, namely the inability to figure out any dynamic construction of companion file paths, at least on its own. Thus:
Extended the API used by critcl-based packages with the command critcl::owns. While this command is ignored by the regular build modes the static scanner described above takes its arguments as the names of companion files which have to be wrapped into the TEA package and could not be figured by the scanner otherwise, like because of dynamic paths to critcl::tsources, critcl::csources, getting sourced directly, or simply being adjunct datafiles.
Extended the API used by critcl-based packages with the command critcl::api for the management of stubs tables, be it their use, and/or declaration and export.
Please see section Stubs Table Management of the critcl package documentation for details.
Extended the API used by critcl-based packages with the command critcl::userconfig for the management of developer-specified custom configuration options, be it their use and/or declaration.
Please see section Custom Build Configuration of the critcl package documentation for details.
Extended the API used by critcl-based packages with the commands critcl::description, critcl::summary, critcl::subject, critcl::meta, and critcl::buildrequirement for the declaration of TEApot meta data for/about the package.
Please see section Package Meta Data of the critcl package documentation for details.
Fixed bug where critcl::tsources interpreted relative paths as relative to the current working directory instead of relative to the ".critcl" file using the command, as all other commands of this type do.
Fixed internals, preventing information collected for multiple ".critcl" files to leak between them. Notably, critcl::tk is not a global configuration option anymore.
Fixed the command critcl::license to be a null-operation in mode "compile & run", instead of throwing an error.
Fixed the critcl application's interference with the "compile & run" result cache in -pkg mode by having it use a wholly separate (and by default transient) directory for that mode.
Fixed bug where changes to a ".critcl" file did not result in a rebuild for mode "compile & run". All relevant API commands now ensure UUID changes.
Fixed bug in the backend handling of critcl::debug where the companion c-sources of a ".critcl" file were not compiled with debug options, although the ".critcl" file was.
Fixed bug in critcl::debug which prevented recognition of mode "all" when it was not the first argument to the command.
Fixed bug in "preload.c" preventing its compilation on non-windows platforms.
Fixed long-standing bug in the handling of namespace qualifiers in the command name argument of critcl::cproc and critcl::ccommand. It is now possible to specify a fully qualified command name without issues.
Extended/reworked critcl::tsources to be the canonical way of declaring ".tcl" companion files even for mode "compile & run".
Extended/reworked critcl::tsources to allow the use of a ".critcl" file as its own Tcl companion file.
Extended critcl::framework to internally check for OS X build target, and to ignore the declaration if its not.
Extended critcl::failed to be callable more than once in a ".critcl" file. The first call forces the build, if it was not done already, to get the result. Further calls return the cached result of the first call.
Extended the handling of environment variable CC in the code determining the compiler to use to deal with (i.e. remove) paths to the compiler, compiler file extensions, and compiler options specified after the compiler itself, leaving only the bare name of the compiler.
Extended the code handling the search for preloaded libraries to print the paths it searched, making debugging of a search failure easier.
A new command critcl::tcl can be used to declare the version of Tcl minimally needed to build and run the ".critcl" file and package. Defaults to 8.4 if not declared. Extended critcl to have the stubs and headers for all of Tcl 8.4, 8.5, and 8.6.
A new command critcl::load forces the build and load of a ".critcl" file. This is the official way for overriding critcl's default lazy-build-&-load-on-demand scheme for mode "compile & run".
Note that after using critcl::load / critcl::failed in a ".critcl" file it is not possible to use critcl commands in that file anymore. Doing so will throw an error.
Extended the generation of '#line' pragmas to use info frame (if available) to provide the C compiler with exact line numbers into the ".critcl" file for the reporting of warnings and errors.
Extended critcl::check with logging to help with debugging build-time checks of the environment, plus an additional optional argument to provide labeling.
Added a new command critcl::checklink which not only tries to check the environment via compiling the code, but also its linkability.
Added a new command critcl::msg for messaging, like command critcl::error is for error reporting. Likewise this is a hook a user of the package is allowed to override. The default implementation, used by mode compile & run does nothing. The implementation for mode generate package prints the message to stdout.
Envisioned use is for the reporting of results determined by critcl::check and critcl::checklink during building, to help with debugging when something goes wrong with a check.
Exposed the argument processing internals of critcl::proc for use by advanced users. The new commands are
critcl::argnames
critcl::argcnames
critcl::argcsignature
critcl::argvardecls
critcl::argconversion
Please see section Advanced Embedded C Code of the critcl package documentation for details.
Extended the critcl package to intercept package provide and record the file -> package name mapping. Plus other internal changes now allow the use of namespaced package names while still using proper path names and init function.
Dropped the unused commands critcl::optimize and critcl::include.
Dropped -lib mode from the critcl application.
Dropped remnants of support for Tcl 8.3 and before.
Jean Claude Wippler, Steve Landers, Andreas Kupries
This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report them at https://github.com/andreas-kupries/critcl/issues. Ideas for enhancements you may have for either package, application, and/or the documentation are also very welcome and should be reported at https://github.com/andreas-kupries/critcl/issues as well.
C code, Embedded C Code, code generator, compile & run, compiler, dynamic code generation, dynamic compilation, generate package, linker, on demand compilation, on-the-fly compilation
Glueing/Embedded C code
Copyright © Jean-Claude Wippler
Copyright © Steve Landers
Copyright © 2011-2013 Andreas Kupries