THE MIDORI BUILD SCRIPT FUNCTIONS
=================================


In order to standardise build scripts a set of functions has been defined and
everybody creating *.mlz packages is highly encuraged to use those.

The system consists of four different parts:
        
        - environment variables
        - generic "build" function
        - set of default configure/compile/install functions
        - set of helper functions and variables


        

Environment Variables
---------------------

The build system defines lots of environment variables (check the
.config.sh file, most are defined there). The more important ones are

        TOPDIR          Full path of the Midori Linux tree
        BUILD           The build area. All packages are extraced into this
                        directory
        TOOLS           The directory used to store libraries for compilation
                        and executables that are needed to create the image.
                        Examples are the gcc compiler used to create all executables
        ROOT            The directory where the midori image is created
        IMAGE           Directory where the partition images are created.
        APPS            List of directories that contain *.mlz packages.

        GCC             The compiler you MUST use to compile libraries and executables
                        for the image
        CFLAGS          It is strongly recommanded that you use those (gives small 
                        executables, and enables some special features in gcc and cpp
                        that help in creating identical binaries).
        LDFLAGS         Use these flags for linking binaries

        TARGETARCH      Many 'configure' scripts accept a host type, this
			variable contains the target hosttype used to create
			gcc/binutils/...

        JMAKE           Command to run make -j with the correct argument for the number
                        of processors on your machine. Use this instead of the standard
                        make unless your package does not support parallel make.
                        


The Generic Build Function
--------------------------

The generic build function is designed to execute a list of user defined
functions in order to create some files and install them either in the $TOOLS
directory or in the $ROOT directory as part of the Midori image.

Each function can either be guarded with a special file so that the function
will only be executed once unless some options or files have changed. However,
functions can also be executed each time the build script runs. Additionally
one can define dangerous functions that are not execute if the build directory
contains a '.precious' file. This is intended for people working in one of
those directories so that their work is not suddenly deleted.

For each function you can define a list of options that would force a
reexecution of this function if an option changed. Also, you can list a number
of files and the function will be reexecuted if the timestamp on any of these
files is newer that the last time the function executed successfully.

For the build function to work you need to define the following variables:

        PACKAGE         Name of the package, which must also be the name 
                        of the directory in which everything gets build. 
                        I.e. your build directory must be $BUILD/$PACKAGE

        FUNCTIONS       list of functions to execute. By default it is set to

                        FUNCTIONS="findfiles untar patchit configure compile installlocal installroot"

                        This means that build will execute the following functions
                        in order (notice the prefix 'ml_'):

                                ml_findfiles
                                ml_untar
                                ml_patchit
                                ml_installconfig
                                ml_configure
                                ml_compile
                                ml_installlocal
                                ml_installroot

                        You MUST have a function called 'installconfig'.
                        See below for a description of those functions


        EXECUTE_ALWAYS  contains a list of functions (that must be part of 'FUNCTIONS')
                        to always execute, regardless of options/file, ... .
                        By default it is set to

                        EXECUTE_ALWAYS="findfiles installconfig installroot"

        DANGEROUS_FUNCTIONS
                        lists functions that should not be executed if a file
                        $BUILD/$PACKAGE/.precious exists. In this case the build
                        will abort with an error.

                        By default it is set to:

                        DANGEROUS_FUNCTIONS="untar patchit"

        KERNELOPT       List of kernel options to turn on (without the 'CONFIG_')
        KERNELMOD       List of kernel modules to compile and install
                        (without the 'CONFIG_')
        BUSYBOXR        busybox tools to install in the root partition
        BUSYBOXU        busybox tools to install in the usr partition
                        Those four variables are only useful if you use the default
                        installconfig function.
        DOCFILES        List of files to be installed in the docs/ directory.
                        Usually you just create the files in the docs/ subdirecory
                        of your package and 'findfiles' will find them.

        CLEANIMAGE_OPTIONS
                        Whenever the configuration file is changed, the build 
                        system executes a 'make clean'. However, some options
                        require a 'make cleanimage' if they are changed. List those
                        option names in this variable.
                 


For each function you can define a set of options that trigger a reexecution
of this function (and all the following ones) if they change, and you can define
a list of files that trigger reexecution if their timestamp is newer than the
last time the function was executed successfully.

To do so you need to set two variables, whose name depend on the function. For
the function 'foo' (as set in FUNCTIONS, which executes the function
'ml_foo()') you need to use the variables FOO_OPTIONS and FOO_FILES. Note that
the function name is the same except for the uppercase characters. Say that you
need to reconfigure a package if either the option 'SPECIAL_CONFIG' or
'DEBUG_PROGRAMS' (which should be defined in the config file) changes. You
would set

        FOO_OPTIONS="$SPECIAL_CONFIG $DEBUG_PROGRAMS"

Note the '$' sign: you store the expanded version of the variables in this string.
The build function simply checks if this string changed between two builds.

If your build function depends on some files, you should list them in FOO_FILES 
as in 
        FOO_FILES="special/config.h special/Makefile.in"

The pathnames must either be absolute or relative to $BUILD

Note that your build functions are always called with the current directory set
to $BUILD (and not $BUILD/$PACKAGE as you may expect, as this directory may
not exist yet). There is no need to 'cd' back to $BUILD at the end of the 
function.

The build argument supports 0, one or two arguments. In the latter case the
first agument must be either 'force' or 'upto', and the second one a function.
'force' forces the reexecution of this function (but does not execute it),
and 'upto' will execute all functions up to and including the one passed
as argument if necessary. I.e.

        build force compile

will force a recompilation during the next build and
        
        build upto installlocal

will compile a package and install the necessary files in the $TOOLS
directory, but it will not install anything in the $ROOT area.
If you just want to execute one function you can use

        build functionname

This version is used by several base packages to install the links into 
the kernel header files after they made changes to the include diretory.


The Default Functions
---------------------

By default the following functions are defined. Note that those are actually
all defined as ml_function_default, and ml_function is defined as

        function ml_function() { ml_function_default }

This allows you to use those functions yourself while adding to ml_function, as
in
        function ml_configure() {
                ml_configure_default
                perl -pi 's/bla/foo/' x.c
        }

The default functions are designed to make your life easy if you work with
simple packages. They assume that your package named foo-2.3_ML2.4-4.mlz
extracts into a directory called foo-2.3 (this is a MUST, otherwise the build
will fail), and that it contains a tarball called foo-2.3.tar.gz that that
extracts into a directory called foo-2.3. Patchfiles will be found too if
they are named foo-2.3.patch*.


ml_findfiles
-------------

Find files sets TARFILES to $BUILD/$PACKAGE/$PACKAGE.{tar.gz,tar.bz2,tgz}, if that file exists.

It will also set PATCHIT_FILES to the files listed in PATCHFILES
(with the correct path added).

And lastly it will find all the documentation files stored in docs/


ml_untar
--------

ml_untar assumes that you defined a list of tarfiles in TARFILES that have to
be extracted in the directory $BUILD (see findfiles for the default tarballs 
used). It knows how to handle tarballs with filenames ending in tar.gz, tar.bz2 and tgz.


ml_patchit
----------

This function executes a 

        patch $PATCHARG < $n 

for all patchfiles listed in PATCHFILES. The path is assumed to be relative to
$BUILD/$PACKAGE. It knows how to handled *.gz, *.bz2 and uncompressed patches.

If necessary you can redefine PATCHARG, by default "-p0" is used.


ml_installconfig
----------------

Write the kernel and busybox config vars into the respective files
in order to change the configuration if necessary and copy the documentation
files into the main docs/packages/ directory


ml_configure
------------

This function runs the ./configure program (either in $BUILD/$PACKAGE
for standard packages, or $BUILD/$PACKAGE/$PACKAGE for *.mlz packages).
It does not pass any arguments by default, but by setting

        CONFIGOPTIONS

options can be passed to the configure script.


ml_compile
----------

This function runs the $JMAKE program (either in $BUILD/$PACKAGE
for standard packages, or $BUILD/$PACKAGE/$PACKAGE for *.mlz packages).
The default options are set to

COMPILEOPTIONS="CC=\"$CC\" GCC=\"$GCC\" CFLAGS=\"$CFLAGS\" LDFLAGS=\"$LDFLAGS\""

but can be changed as necessary.


ml_installlocal
---------------

Is supposed to copy files into the $TOOLS directory. It does nothing
and must be redefined by the build script.


ml_installroot
--------------

This last function should copy files into the $ROOT tree, which is
the Midori Linux root tree. By default this function copies runtime
test files into the appropriate directories. If your package includes
such tests you may want to at least call ml_installroot_default at
the beginning of your own ml_installroot function.


Helper Functions
----------------

To make your life a bit easier, a few additional functions are defined:


ml_initscript
-------------

Used to create initialization and shutdown scripts. Please see initscripts.txt.


ml_required_option
------------------

The function requires two arguments, an option (variable name) and the 
expected value. If the options does not have the necessary value, the 
build aborts. This can be used to check if certain packages have been
installed. For example

        ml_required_option CPP_LIB yes

checks if the users config file set CPP_LIB to yes in order to install
the c++ library


ml_required_package
-------------------

Takes a package name and an optional version number. It will
install that package or fail and abort the build if it does
not exits or has been disabled.


ml_conflicting_package
----------------------

Takes a package name and aborts if that package will be installed.
Used to guard two mutually exclusive packages.


ml_check_for_package
--------------------

Takes a package name and (optionally) a version number. It will silently
fail if that package does not exist and will abort installation of the
current package without an error. This is used for example by all X
applications: if X is not installed they will not compile and install either.


ml_is_package_installed
-----------------------

Takes a package name and (optionally) a version number and returns a 0
if that package is installed, otherwise it returns a 1.


Example
-------

First a very simple example, the building of the package pump (dhcp client):

pump requires popt:

        ml_required_package popt

Compiling is special (strip is needed), so we redefine that function: 

        function ml_compile() {
                cd $PACKAGE/$PACKAGE
                $JMAKE CFLAGS=" -Wall -D__STANDALONE__ $CFLAGS -DVERSION=\"\\\"0.7.8\\\"\"" LDFLAGS="$LDFLAGS" LOADLIBES="$LDFLAGS -lpopt -lresolv " pump
                strip pump
        }

Install pump into the $ROOT tree and add a special symlink

        function ml_installroot() {
                ln -sf ../tmp $ROOT/var/run
                cp $PACKAGE/$PACKAGE/pump $ROOT/sbin
        }


For a more complex example check the glibc or kernel build scripts
(in their respective mlz packages)


