scons-gnu-arguments

User's Manual

Paweł Tomulik

2015-11-08


Table of Contents

1. Introduction

The scons-gnu-arguments package is an extension to SCons which provides SCons with several command-line variables and options defined by GNU standards.

Many open-source projects use autotools and make to build software. These tools provide a set of standard command-line options and variables such as --prefix or CC. Their users may easily configure project with a custom installation prefix (./configure --prefix=/usr/bin) or chose specific compiler via command-line variables (make CC=clang). All these options are very mature and well-known to open-source developers and package maintainers. They are not avaiable in SCons, however, because SCons is not make nor automake. This is understandable, but also frustrates some developers and package mainainers that are used to GNU tools. As for the moment, there is no cheap and easy way to add the multitude of existing GNU comman-line options to scons. This package tries to change this.

WIth scons-gnu-arguments you may add groups of predefined GNU command line options/variables to SCons with just a few lines of code.

The package only works in pair with scons-arguments, so scons-arguments must be installed along with scons-gnu-arguments in a project that uses GNU arguments.

2. Quick Start

It's recommended to also take a look at the scons-arguments package, as some functions appearing in the following examples are explained in that package's documentation.

As a quick example, let us write a simple SConstruct file which installs a shell script in an appropriate installation directory, as defined by GNU standards (in $bindir, if you know what I mean). The installation path shall be configurable via command-line variables prefix, exec_prefix and bindir as it usually happens in projects based on autotools.

Example 1. Install shell script in $bindir

Write the follwing two files, hello:

#! /bin/sh
echo 'hello world!'

and SConstruct:

# SConstruct
import SConsGnuArguments.InstallDirs

env = Environment()                                   # SCons environment, you should know it
var = Variables()                                     # container for SCons CLI variables

decls = SConsGnuArguments.InstallDirs.Declarations() # declare arguments
args  = decls.Commit(env, var, True)                  # say "no more arguments" to scons
args.Postprocess(env, var, True)                      # transfer CLI arguments to env

# The rest is quite usual
hello_i = env.InstallAs("${bindir}/hello", 'hello')   # NOTE the ${bindir}

env.Alias('install', hello_i)
env.AlwaysBuild('install')

Now build the project and install the compiled program as follows:

ptomulik@barakus:$ scons -Q install prefix=preinst/usr
Install file: "hello" as "preinst/usr/bin/hello"

Note, that the installation directory may be changed with prefix, exec_prefix and bindir CLI variables.



If you wonder what just happened, let's define appropriate help option to document our CLI variables we've just created

Example 2. Install shell script in $bindir, also add --help-variables option to SConstruct
# SConstruct
import SConsGnuArguments.InstallDirs

env = Environment()                                   # SCons environment, you should know it
var = Variables()                                     # container for SCons CLI variables

decls = SConsGnuArguments.InstallDirs.Declarations() # declare arguments
args  = decls.Commit(env, var, True)                  # say "no more arguments" to scons
args.Postprocess(env, var, True)                      # transfer CLI arguments to env

# The following adds --help-variables option which documents CLI variables.
AddOption( '--help-variables', dest='help_variables', action='store_true',
         help='print help for CLI variables' )
if GetOption('help_variables'):
  print args.GenerateVariablesHelpText(var, env)
  Exit(0)

# The rest is quite usual
hello_i = env.InstallAs("${bindir}/hello", 'hello')   # NOTE the ${bindir}

env.Alias('install', hello_i)
env.AlwaysBuild('install')

Now dump some info about variables to the console

ptomulik@barakus:$ scons -Q --help-variables

docdir: The directory for installing documentation files (other than Info) for this package.
    default: ${datarootdir}/doc/${install_package}
    actual: /usr/local/share/doc/
<... lot of output here ...>
prefix: Installation prefix
    default: /usr/local
    actual: /usr/local
<... lot of output ...>
prefix: Installation prefix
    default: /usr/local
    actual: /usr/local
<... lot of output ...>
bindir: The directory for installing executable programs that users can run.
    default: ${exec_prefix}/bin
    actual: /usr/local/bin


In the above example, we have used arguments from the submodule InstallDirs, which defines variables for GNU installation directories. The package SConsGnuArguments contains several submodules, each providing some subset of standarized GNU command-line arguments. They will be discussed in next sections.

3. Using SConsGnuArguments

There are several submodules in the SConsGnuArguments, each providing a subset of predefined GNU command-line arguments. For example, GNU directory variables are defined in a module named SConsGnuArguments.InstallDirs. Each relevant submodule provides two public functions

  • Names() - which lists all argument names defined by the module,

  • Declarations() - which provides declarations of SCons arguments predefined by the submodule.

We'll focus only on the second function, the first one shall be quite obvious. The simplest use is to declare all the arguments predefined by a submodule

import SConsGnuArguments.<Submodule>
decls = SConsGnuArguments.<Submodule>.Declarations()

This declares all arguments predefined in the <Submodule> (substitute <Submodule> with an actual submodule name). The full list of arguments predefined by particular submodules may be found in API documentation. If you want only some of them, you may define special filter to allow only particular arguments. Currently filters are supported that use argument names. Most generic approach is to create a callable object (lambda, function, whatever) and pass it as a name_filter keyword argument to Declarations().

allowed = ['prefix', 'exec_prefix', 'bindir']
decls = SConsGnuArguments.<Submodule>.Declarations(name_filter = lambda s : s in allowed)

The name_filter should be a callable which takes a string (argument's name) and returns True (to include argument) or False (to exclude argument). The above example may be made simpler, because sequences may also be used as a name filter, so the same may be achieved with

decls = SConsGnuArguments.<Submodule>.Declarations(name_filter = [ 'prefix', 'exec_prefix', 'bindir' ])

Arguments declared with SConsGnuArguments.<Submodule>.Declarations() may create a command-line variables (scons variable=value) and/or command-line options (scons --option=value). For example, the submodule InstallDirs only creates command-line variables. This may be changed, for example you may wish to have command-line options generated by InstallDirs (or you may want to have both, CLI variables and options, it's not a problem). To enable command-line options and disable variables, manipulate var_key_transform and opt_key_transform parameters as follows.

decls = SConsGnuArguments.<Submodule>.Declarations(var_key_transform = False, opt_key_transform = True)

This should generate command-line options such as --prefix or --exec-prefix and prevent from generating command-line variables such as prefix and exec_prefix. Note that command-line options can't be saved to a file by using scons-arguments API, whereas command-line variables may be saved easily.

By default construction variables, command-line variables and command-line options inherit their names directly from their arguments. That is, if we have an argument named prefix, then its corresponding construction variable will be named prefix as well as command-line variable and command-line option (note, that command-line options get lowercased by default). This may be changed easily, for example there is a very quick and easy way to add prefixes/suffixes to the generated names. For example, we may want to add "GNU_" prefix to corresponding construction variables, such that argument, like bindir, will be seen as GNU_bindir in SCons environment. This may be achieved with env_key_prefix parameter as follows.

decls = SConsGnuArguments.<Submodule>.Declarations(env_key_prefix = 'GNU_')

The possible arguments that allow to customize name generation for construction variables, command-line variables and command-line options are summarized in the following table

Parameter Description
env_key_prefix Prefix to be prepended to the name of construction variable
env_key_suffix Suffix to be appended to the name of construction variable
var_key_prefix Prefix to be prepended to the name of command-line variable
var_key_suffix Suffix to be appended to the name of command-line variable
opt_key_prefix Prefix to be prepended to the key identifying command-line option
opt_key_suffix Suffix to be appended to the key identifying command-line option
opt_name_prefix Prefix to be prepended to the name of command-line option
opt_name_suffix Suffix to be appended to the name of command-line option
opt_prefix Prefix to be used as a starter for command-line option, usually '--'
env_key_transform A callable object used to generate names of construction variables based on argument names. Providing this usually ignores all the prefix/suffix arguments. It may also be set to True to force the usage of default generator, or to False to disable the generation of construction variables.
var_key_transform A callable object used to generate names of command-line variables based on argument names. Providing this usually ignores all the prefix/suffix arguments. It may also be set to True to force the usage of default generator, or to False to disable the generation of command-line variables.
opt_key_transform A callable object used to generate keys identifying command-line options based on argument names. Providing this usually ignores all the prefix/suffix arguments. It may also be set to True to force the usage of default generator, or to False to disable the generation of command-line options.
option_transform A callable object used to generate options names based on argument names. Providing this usually ignores all the prefix/suffix arguments.

4. Submodules in SConsGnuArguments

The following subsections describe shortly submodules found in the SConsGnuArguments package and list arguments they define.

4.1. InstallDirs

Defines GNU directory variables, also called GNU installation directories, which are used mainly by GNU-compatible installers. The arguments defined by submodule by default create command-line variables (and no command-line options).

Declaring arguments predefined in InstallDirs

decls = SConsGnuArguments.InstallDirs.Declarations(...)

Arguments provided by InstallDirs:

prefix Installation prefix
exec_prefix Installation prefix for executable files
bindir The directory for installing executable programs that users can run.
sbindir The directory for installing executable programs that can be run from the shell, but are only generally useful to system administrators.
libexecdir The directory for installing executable programs to be run by other programs rather than by users.
datarootdir The root of the directory tree for read-only architecture-independent data files.
datadir The directory for installing idiosyncratic read-only architecture-independent data files for this program.
sysconfdir The directory for installing read-only data files that pertain to a single machine - that is to say, files for configuring a host.
sharedstatedir The directory for installing architecture-independent data files which the programs modify while they run.

localstatedir

The directory for installing data files which the programs modify while they run, and that pertain to one specific machine.
includedir The directory for installing header files to be included by user programs with the C #include preprocessor directive.
oldincludedir The directory for installing #include header files for use with compilers other than GCC.
docdir The directory for installing documentation files (other than Info) for this package.
infodir The directory for installing the Info files for this package.
htmldir Directory for installing documentation files in the html format.
dvidir Directory for installing documentation files in the dvi format.
pdfdir Directory for installing documentation files in the pdf format.
psdir Directory for installing documentation files in the ps format.
libdir The directory for object files and libraries of object code.
lispdir The directory for installing any Emacs Lisp files in this package.
localedir The directory for installing locale-specific message catalogs for this package.
mandir The top-level directory for installing the man pages (if any) for this package.
man1dir .. man9dir Simmilar to mandir
man1ext .. man9ext Extensions for manpage files.
pkgdatadir The directory for installing idiosyncratic read-only architecture-independent data files for this program.
pkgincludedir The directory for installing header files to be included by user programs with the C #include preprocessor directive.
pkglibdir The directory for object files and libraries of object code.
pkglibexecdir The directory for installing executable programs to be run by other programs rather than by users.

4.2. AltPrograms

Defines arguments that correspond to GNU autoconf alternative programs. The arguments defined by submodule by default create command-line variables (but no command-line options).

Declaring arguments predefined in AltPrograms

decls = SConsGnuArguments.AltPrograms.Declarations(...)

Arguments provided by AltPrograms:

AWK The awk program
EGREP The egrep program
FGREP The fgrep program
GREP The grep program
INSTALL The program used to install files
INSTALL_DATA The program used to install data
INSTALL_PROGRAM The program used to install programs
INSTALL_SCRIPT The program used to install scripts
LEX The lex program
LEX_OUTPUT_ROOT The base of the file name that the LEX generates
LEXLIB Library that should be linked to LEX-generated program
LN_S Either 'ln -s', 'cp -pR' or just 'ln'
MKDIR_P Either 'mkdir -p' or 'install-sh'
RANLIB The ranlib program
SED The sed program
YACC The yacc program