#! /usr/bin/perl
#                              -*- Mode: Perl -*-
# make-kpkg ---
# Author           : root ( root@melkor.pilgrim.umass.edu )
# Created On       : Mon Jun 17 01:10:11 1996
# Created On Node  : melkor.pilgrim.umass.edu
# Last Modified By : Manoj Srivastava
# Last Modified On : Fri Apr  4 11:20:19 1997
# Last Machine Used: tiamat.datasync.com
# Update Count     : 74
# Status           : Unknown, Use with caution!
# HISTORY          :
# Description      :
#
#
require 5.002;
use strict;
use Getopt::Long;

=head1 NAME

make-kpkg - build Debian kernel packages from Linux kernel sources.

=cut

($main::MYNAME     = $main::0) =~ s|.*/||;
$main::Author      = "Manoj Srivastava";
$main::AuthorMail  = "srivasta\@debian.org";
$main::Version     = "3.01";

my $help_opt=0;
my $revision=0;
my $signature="";
my $targ_opt=0;
my $noexec;
my $zimage;
my $bzimage;

my %option_ctl = ("help"       => \$help_opt,
		  "noexec"     => \$noexec,
		  "revision=s" => \$revision,
		  "pgpsign=s"  => \$signature,
		  "targets"    => \$targ_opt,
		  "zimage"     => \$zimage,
		  "bzimage"    => \$bzimage
		 );

=head1 SYNOPSIS

 usage: make-kpkg [options] [target [target ...]] 

=cut

=head1 DESCRIPTION

This manual page explains the  Debian  B<make-kpkg>  utility,
which  is  used  to create the kernel related Debian pack
ages. This utility needs to be run from a top level  Linux
kernel source directory. 

=cut

=head1 OPTIONS

=over 3

=item B<--help> Print out a usage message.

=item B<--revision> number

=over 2

Sets  the  Debian  revision number for the packages produced to the
argument B<number>. This has certain constraints: it may contain only
alphanumerics and the characters + . (full stop, and plus) and should
contain a digit. B<NOTE>: No hyphens allowed. (Look at Chapter 5 of the
Programmers manual for details)

This is because  B<dpkg> imposes an ordering on version numbers,
so that it can tell whether packages are being up or downgraded and
so that dselect can tell whether a package it finds available is
newer than the one installed on the system.


=back

=item B<--pgpsign> name

=over 2

Set the string used to sign the B<changes> file for any external modules
in I</usr/src/modules/> using PGP. This option will override the builtin
default and the site wide customizations stored in the file
I</etc/kernel-pkg.conf>.

=back

=item B<--targets>

=over 2

Prints out a list of known targets. See the Section B<Targets> below.

=back

=item B<--noexec>

=over 2

Pass  a  B<-n> option to the I<make> so that commands are merely
printed  to  the  screen  but  not actually  executed.  This is very
useful for debugging.

=back

=item B<--zimage>

=over 2

Makes a zImage kernel rather than a bzImage  kernel (the  default).
Useful for people having problems with bzImage kernels.

=back

=item B<--bzimage>

=over 2

Makes a bzImage kernel. Useful for people who  want a bzImage kernel
on sites where the default is zIm age kernels.

=back

=back

The options maybe shortened to the smallest unique string,
and may be entered with either a - or a -- prefix, and you
may use a space or an = symbol between  an  option  string
and a value.

=cut


my $usage = <<"EOUSAGE";
This program should be run in a linux kernel source top level directory.
/usr/doc/kernel-package/Problems.gz contains a list of known problems.

usage: $main::MYNAME [options] target [target ...]
  where options are:
 --help                This message.
 --revision number     The debian revision number. ([a-z.+0-9]) (Must
                         have digit, no hyphen allowed)
 --pgpsign  name       A ID used to sign the changes file using pgp.
 --targets             Lists the known targets.
 --zimage              Create a kernel using zImage rather than bzImage
 --bzimage             Create a kernel using bzImage (in case the site
                       wide default is zimage, as set in
                       /etc/kernel-pkg.conf) 
Use one of --zimage or --bzimage, or none, but not both.

Option Format:
The options may be shortened to the smallest unique string, and may
be entered with either a - or a -- prefix, and you may use a space
betweenan option string and a value.

Version: $main::Version
$main::Author <$main::AuthorMail>
EOUSAGE
;

=head1 TARGETS

=over 3

=item B<clean>

=over 3

Cleans the kernel source directory of all files created by target
B<build>, and runs a I<make distclean>. (Please look at a Linux kernel
Makefile for details).  Please note that although we take care of the
list of current kernel configuration contained in the file I<.config>,
the file I<include/linux/autoconf.h> is not preserved.

=back

=item B<buildpackage>

=over 3

This target runs the targets B<clean>, and B<binary>, and produces the
complete package using B<dpkg-buildpackage>.

=back

=item B<binary>

=over 3

This target produces all three Debian kernel packages by running the
targets B<kernel_source>, B<kernel_headers> and B<kernel_image>.

=back

=item B<kernel_source>

=over 3

This target produces a debianised package of the Linux kernel
sources. The package produced also has scripts called at install and
delete times that manage the symbolic link I</usr/src/linux> and
ensure that the link is pointed at the latest source/header package on
the system.

=back

=item B<kernel_headers>

=over 3

This target produces a Debian package containing the header files
included in the Linux kernel. The package produced also has scripts
called at install and delete times that manage the symbolic link
I</usr/src/linux> and ensure that the link is pointed at the latest
source/header package on the system.

=back

=item B<kernel_image>

=over 3

This target produces a Debian package of the Linux kernel source
image, and any modules configured in the kernel configuration file
I<.config>.  If there is no I<.config> file in the kernel source directory,
a default configuration is provided similar to the one used to create
the B<Debian> boot-floppies.  The package produced updates the
I</boot/psdatabase> file at install time, it also update symbolic links
in the root directory to point to the new kernel image in I</boot>.  It
also offers to run the Linux loader, B<LILO>, creating a configuration
file for B<LILO> if needed.  It also offers to put the new kernel on a
floppy, formatting the floppy if needed.  On deletion, the package
checks the version of the kernel running, and refuses to delete a
running kernel.

=back

=item B<build>

=over 3

This target, used by target B<kernel_image> above, compiles the Linux
kernel image.

=back

=item B<modules>

=over 3

This target allows you to build all add-on modules and packages that
are very dependent on the precise kernel version they are compiled for
at the same time you build your kernel image.  This target expects to
find the modules or packages under /usr/src/modules, and, for all such
directories, changes to I</usr/src/modules/x>, and runs the B<kdist> rule in
the local I<debian.rules> file.  This target should create the B<Debian>
module package(s), and may also produce a compressed tar file, and a
compressed diff file, with I<md5sums> recorded in a changes file using
B<dpkg-genchanges>.  The file is signed by the same identity that would
be used to sign the kernel packages.

=back

=item B<modules_config>

=over 3

This target allows you to  configure  all  packages under
I</usr/src/modules>. 

=back

=item B<modules_image>

=over 3


This target allows you to build all packages under I</usr/src/modules>,
but does not create the source or diff files, and does not create and
sign a changes file.

=back

=back

=cut


my %Known_targets=("clean"          => 1,
		   "buildpackage"   => 1,
		   "binary"         => 1,
		   "binary-indep"   => 2,
		   "kernel_source"  => 3,
		   "kernel_headers" => 3,
		   "kernel-source"  => 3,
		   "kernel-headers" => 3,
		   "binary-arch"    => 2,
		   "kernel_image"   => 3,
		   "kernel-image"   => 3,
		   "build"          => 4,
		   "modules"        => 1,
		   "modules_image"  => 1,
		   "modules_config" => 1,
		   "modules-image"  => 1,
		   "modules-config" => 1
		  );

my $targets_help =<<EOTRGT;
 Known Targets are:
============================================================================
|     Targets                      |   Automatically builds                |
============================================================================
| clean                            |                                       |
| buildpackage                     | Builds the whole paclage              |
| binary                           | Builds kernel_{source,headers,image}  |
|       binary-indep               |                                       |
|            kernel_source         |                                       |
|            kernel_headers        |                                       |
|       binary-arch                |                                       |
|            kernel_image          | Builds build                          |
|                           build  |                                       |
| modules                          |                                       |
| modules_image                    |                                       |
| modules_config                   |                                       |
============================================================================
See /usr/lib/kernel-package/rules for details.
EOTRGT
  ;

sub main (){
  my $ret;
  my $rules_file;
  my $target;

  $rules_file = "/usr/lib/kernel-package/rules";
  
  $ret = GetOptions(%option_ctl);
  die "$usage\n" unless $ret;
  if ($help_opt || $targ_opt){
    print "$usage" if $help_opt;
    print "$targets_help" if $targ_opt;
    exit 0;
  }
  if ($bzimage && $zimage){
    print "Use only one of bzimage and zimage options.\n";
    print "$usage\n";
    exit 1;
  }
  # Fine. Now we check targets.
  my $errors = "";
  my $Targets = "";
  foreach $target (@ARGV){
    if ($Known_targets{$target}){
      $Targets .= "$target ";
    }
    else {
      $errors .= "$target ";
    }
  }
  if ($errors){
    print STDERR "Unknown target $errors\n";
    die "$targets_help\n";
  }
  # See if we are running in a linux kernel directory
  if (!(-d "drivers" && -d "kernel" && -d "fs" && -d "include/linux")){
    print STDERR <<EOERR;
We do not seem to be in a top level linux kernel source directory
tree. Since we are trying to make a kernel package, that does not make
sense.  Please change directory to an top level linux kernel source
directory, and try again. (If in case I am wrong, and this is indeed
a top level linux kernel source directory, then I have gotten sadly
out of date with current kernels, and you should upgrade kernel-package)
EOERR
  ;
    exit 1;
  }

  if (!-x "$rules_file"){
    print STDERR <<EOERR1;
    Could not locate the rules file (Normally found at the location
 $rules_file), which would seem an
 impossibility. I give up. 
EOERR1
     ;
  }
  # Fine. Just do it!
  my $command = "$rules_file ";
  if ($signature){
    $command .= "  PGP_SIGNATURE=$signature ";
  } 
  if ($revision){
    $command .= " DEBIAN_REVISION=$revision ";
  }
  if ($noexec){
    $command .= " -n ";
  }
  if ($zimage){
    $command .= " IMAGE_TYPE=zImage ";
  }
  elsif ($bzimage) {
    $command .= " IMAGE_TYPE=bzImage ";
  }
  $command .= " $Targets";
  exec $command; 
}

## Now just call main
&main();

=head1 FILES

Apart from the runtime options, the debian.rules file run by B<make-kpkg>
also looks for site-wide defaults in the file I</etc/kernel-pkg.conf>.
The default configuration allows there to be a site wide override for
the full name and email address of the person responsible for
maintaining the kernel packages on the site, but the
I</etc/kernel-pkg.conf> file is actually a I<Makefile> snippet, and any
legal make directives may be included in there.  B<Note>: Caution is
urged with this file, since you can totally change the way that the
make is run by suitable editing this file. Please look at
I</usr/doc/kernel-package/Problems.gz> for a list of known problems while
compiling kernel images.

=cut


=head1 B<SEE ALSO>

B<kernel-pkg.conf>(5), B<dpkg-deb>(1), B<dpkg-source>(1),  B<make>(1),
B<The GNU Make manual>.

=cut


=head1 BUGS

None Known so far.

=cut

=head1 AUTHOR

This  was  written by Manoj Srivastava <srivasta@debian.org>, for the
Debian GNU/Linux system. 

=cut


exit 0;
__END__
