Chapter 2
Compiling and Installing Knit

The INSTALL file in the topmost directory of the Knit source tree contains an abbreviated version of these instructions.

Knit consists of the following programs:

knit
The Knit unit language compiler. This is the heart of the system and the program that one runs to process a ‘.unit’ file.
knitdoc
A program that produces HTML documentation from ‘.unit’ files.
mk_unit
A Perl script that can produce a skeleton ‘.unit’ file from a set of object files. This script is useful for “importing” existing code into Knit.
rename_dot_o_files
A tool used by knit to transform ‘.o’ files. While normally invoked “behind the scenes,” this program can also be used on its own to edit symbols that appear within an object file.
knit_c_parser
A parser for C source code, invoked internally by knit.
knit_smartmv
A small Perl script for moving files, invoked internally by knit.
knitGenBundles
A program that produces bundle declarations, invoked internally by mk_unit.

knit, knitdoc, and knitGenBundles are written in the Haskell programming language, and the helper programs rename_dot_o_files and knit_c_parser are written in C. Because Knit can be somewhat tricky to compile, most users will want to download the Knit tools in precompiled form. Users who want to work on Knit itself, however, will need to be able to compile Knit from source.

FreeBSD and Linux are the current Knit development platforms. If you build Knit on an interesting platform, the Knit developers would love to know about it. Of course, if the build fails, they would like to know about that, too! Send build reports, bug reports, praise and damnation to knit-users@flux.cs.utah.edu.

2.1 Acquiring Knit

Knit is available on the World Wide Web under http://www.cs.utah.edu/flux/knit/. Currently, Knit is distributed in two forms: as precompiled binaries for a variety of operating systems, and as source code. We strongly recommend that you use the precompiled binaries for your platform if available. (Please contact the Knit authors if precompiled binaries for your platform are not available. If demand warrants, your platform may be added to the supported set.)

2.2 Building the Knit Programs

If you are using a binary distribution of Knit, you should skip the instructions below and proceed to Section 2.3.

In order to build Knit from source, you will need:

An ELF-based system
Knit is currently implemented only for ELF-based systems.
A Haskell compiler
Version 4.08.1 of ghc, the Glasgow Haskell Compiler1, is our usual compiler. Instead of a compiler, one can use a Haskell interpreter such as Hugs2, but this is definitely not recommended for large projects.
green-card
green-card3 is a foreign function interface generator for Haskell. Green Card is required when one uses ghc to compile Knit, but is optional if one uses Hugs.
An ANSI C compiler
gcc 2.95.2 is our usual compiler. We strongly recommend gcc for building Knit. As of this writing, some of the Knit example programs make use of gcc language extensions, and the OSKit (described in Section 2.5.1 requires gcc in any case. Additionally, as described in Section 2.5.2, advanced projects may want to build a special version of gcc for optimized Knit-based code.
GNU make
Version 3.77 has been tested.
flex or lex
flex version 2.5.4 has been tested. Vendor-provided versions of lex should work, but this has not been extensively tested.
bison or yacc
bison version 1.25 has been tested. Vendor-provided versions of yacc should work, but have not been extensively tested.
libelf
Version 0.7.0 has been tested. libelf4 is a freely available library for manipulating ELF object files. libelf is preinstalled on some operating systems, such as Linux. For other systems, libelf is available from the World Wide Web. Note the special instructions for building libelf in Section 2.2.1.
Disk space
You will need about 10 MB of free disk space to build Knit and the current set of Knit example programs.

Once you have the above-listed tools, you will need to (1) unpack the Knit source distribution tar file and (2) create a build tree to contain the object files. Assuming that you did these things in your home directory, your directory setup would look something like this:

~/knit-1.0.0/
The Knit source tree, unpacked from knit-1.0.0.src.tar.gz.
~/obj/
The directory you create to be the root of your Knit object tree. (Of course, you can call this directory anything you like. It does not have to be called obj.)

2.2.1 Compiling libelf

Before configuring or compiling Knit, you must first have the libelf library and headers available on your system. If your operating system has libelf preinstalled, great: you are done with this step. However, if your system does not come with libelf, you must download and compile (and optionally install) libelf for yourself before proceeding. See Section 2.2 for download and version information.

WARNING: If you have problems with the Knit rename_dot_o_files program — i.e., if it seems to produce invalid object files — you may need to build libelf so that it does not use the mmap system call to manipulate files. (The Knit authors have seen this problem occur when the object file is located in an NFS-mounted filesystem.) To build libelf so that it does not use mmap, first configure the library normally, and then comment out the HAVE_MMAP definition in the generated config.h file. Finally, build and install the library.
If you do not have libelf installed on your system, you will need to use the --with-libelf command line option when you configure Knit for your system, as described in the next section.

2.2.2 Configuring Knit

Given the directory setup described in Section 2.2, you would type the following commands to configure Knit for your operating system:


  cd ~/obj
  ../knit-1.0.0/configure

The configure script in the root of the Knit source tree is an ordinary configure script, generated by GNU autoconf. Type “configure --help” to see all of the script’s command line options. In addition to all of the standard options accepted by autoconf-generated scripts, Knit’s configure script accepts the following Knit-specific options:

--with-libelf=DIR
Use the libelf library installed in DIR . The Knit Makefiles will look for the libelf header files in DIR /include/, and will look for the libelf library itself in DIR /lib/.
--with-greencard-dir=DIR
Use the Green Card files in DIR to build certain Knit C source code files. This option is useful for Hugs-based builds only.

Additionally, the Knit configure script examines the values of the following environment variables:

HC
The name of the Haskell compiler to be used. If this variable is unset, the default value ghc is used.
GREENCARD
The name of the the Green Card program. Default: green-card.
RUNHUGS
The name of the Hugs program. Default: runhugs.

2.2.3 Compiling Knit

Finally, you are ready to compile the Knit tools! If you are using the Glasgow Haskell Compiler, type the following commands to build Knit and its auxiliary programs:


  cd ~/obj/compiler/ghc
  make all

Assuming all goes well, these commands should produce all of the Knit programs that need to be compiled.

If you are using Hugs, type the following instead:


  cd ~/obj/compiler/hugs
  make all

These commands will produce some object files and the C-based helper programs only (i.e., knit_c_parser and rename_dot_o_files). Since Hugs is an interpreter, there is no need to compile the Haskell-based programs.

Of course, if you are using a precompiled binary release of Knit, you do not need to build the binaries at all: you already have them! But you still have to install the programs — read on.

2.3 Installing Knit

You can install the Knit compiler programs in a “standard” place on your system by typing “make install” in the bin subdirectory of your Knit build tree (in our running example, ~/obj/bin).

Special instructions for binary distributions: If you are using a binary distribution of Knit, you will nevertheless need to run the Knit configure script, which will (1) tailor certain Knit scripts to your system, (2) configure the installation process for your system, and (3) produce the Knit example program Makefiles. We encourage you to run the configure script in a directory that is separate from your Knit distribution tree, because Knit will produce many files when processing the examples. So, to configure and install a binary distribution, type something like the following:


  mkdir ~/obj
  cd ~/obj
  ../knit-1.0.0/configure
  cd bin
  make install

Whether you use a source or binary distribution, by default, the root installation directory is /usr/local. You can change this default by specifying the --prefix option to configure, e.g.:


  ../knit-1.0.0/configure --prefix=/usr/local/knit-1.0.0
  
  make -C ~/obj/compiler/ghc all     # Build Knit
  make -C ~/obj/bin          install # Install under `/usr/local/knit-1.0.0'

It is not necessary to install Knit in order to use the provided example programs (described in the next section) or to build your own Knit-based programs.

2.4 Testing Knit

Knit comes with a small set of example programs; these are located in the examples subdirectory of the distribution source tree. Running your newly compiled Knit tools on the provided examples is easy. In a sentence: go to the examples subdirectory of your Knit object tree and type make. This will build all of examples. If everything builds, congratulations! You are now a “techknitian”!

The various example programs are described in greater detail in Chapter 4.

2.5 Related Software

The software described below is not part of Knit, but is often used in conjunction with Knit.

2.5.1 The OSKit

For projects involving operating systems or similar low-level software, you may want to use Knit in conjunction with the OSKit. The OSKit is a framework and set of modularized components and library code, together with extensive documentation, for the construction of operating system kernels, servers, and other OS-level tools. Its purpose is to provide much of the infrastructure “grunge” that usually takes up a large percentage of development time in any operating system or OS-related project, and allow developers to concentrate their efforts on the unique and interesting aspects of the new OS in question. The OSKit is available on the World Wide Web under http://www.cs.utah.edu/flux/oskit/.

Recent versions of the OSKit can be configured to work with Knit. Instructions for using Knit with the OSKit are in Chapter 6. Knit-specific files in the OSKit are located in a subdirectory called oskit/knit. Look there for unit definition files and related files. Refer to the OSKit documentation for further information about the OSKit.

WARNING: Because Knit and the OSKit are both active and evolving projects, you must be careful to get a “matched set” in order to use them together! Knit version 1.0.0 is intended to work with OSKit version 20010214 (a snapshot release). Later versions of either system may not work with previous versions of the other — backward compatibility is not guaranteed!

2.5.2 gcc for Knitted Code

If you use Knit’s “flattening” optimization on certain C sources, including the OSKit, you may need to use a special version of gcc in order to compile your Knit-generated C code. This special version of gcc must ignore certain type errors that may be introduced by Knit, because Knit does not unify identical type declarations that originate in two separate C files. This sounds really dodgy, but we are assuming that you have already built your system without flattening using a normal C compiler. If the unflattened version is free of type errors, then compiling with flattening and the hacked compiler should work just fine.

The instructions for producing a patched version of gcc are contained in the file unsupported/gcc.patch in the Knit distribution tree. The effect of the patch is to allow assignments to variables when the assigned value has a different type, so long as the types of the variable and value have the same storage size. This patch relaxes the C type rules and is okay as long as your program can be compiled in a normal way by an unpatched version of gcc.

WARNING: If you make a patched version of gcc, you should be careful to install it in a very special place with a very special name, so that nobody will use it accidentally to compile anything other than Knit-generated code.