The rewrite rules allow dynamic modification of the include paths. They are preferentially used for trees describing a multi-target application (think of an OS kernel running on many hardware architectures or of a compiler with several code generators).

To be fully operational, the rewrite rules are used in conjunction with 'variables' substitution. The variable value points to directories or contains part of directory name.

CAUTION! Using the inadequate syntax is not detected at configuration time but causes an error only when the 'maps' are activated. Consequently the bug can go unnoticed for a long period of time before happening.

Example (with the [] syntax):

We define a target variable to contain the name of the target architecture for our application (considering this name is part of the directory names):

, 'variables' => { 'v' => ... # Version list , 'target' => { 'name' => 'Target architecture' , 'range' => [qw(intel ppc m68k)] } }

Supposing that the include directives are written as #include "asm/file.h" and there is a set of 'target' asm-xxx directories in /include, we'll get the right hyperlinks with:

, 'incprefix' => [ '/include' ] , 'maps' => [ '\/asm[^\/]*\/' => '\/asm-$target\/' ]

Notes

Another example (with the [] syntax):

This example gives first-level rules to resolve the #include directives in the Linux kernels 2.x and 3.x.

The referenced files are stored in directories /include and /arch for architecture-independent and architecture-dependent files respectively.

The arch/ directory is itself structured not to restrict what can be stored inside. The #included files are found in an include/ subdirectory of the architecture-specific subtree as /arch/xxx/include/asm/yyy.h where xxx is replaced by the architecture name. They are referenced from source code as #include "asm/yyy.h".

Traditionally, the kernel uses variable a for the target architecture. The intended result will be obtained with the following parameters in lxr.conf:

, 'variables' => { 'v' => ... # Version list , 'a' => { 'name' => 'Target architecture' , 'range' => [qw(x86 intel ppc m68k)] # and many more } } , 'incprefix' => [ '/include', '/arch/%=ARCH=%/include' ] , 'maps' => [ '\/arch\/%=ARCH=%\/' => '\/arch\/$a\/' ] The %=ARCH=% above is no magic identifier. It has only been chose as a placeholder with a name thought never to occur as a directory name in the kernel tree. It is of utmost importance to use a never-to-be-seen symbol (thus the % and = characters in the name) otherwise some unforeseen substitutions may happen with surprising results.

The Linux kernel uses one of the most complex configuration management system of the open source world. To cope with sub-architecture variants needs more variable definitions and more 'incprefix' and 'maps' rules. Configuring LXR for the kernel deserves a full chapter.

Refer to the Tips section for two non-trivial examples of rewrite rules.