Avoid autogenerating the Makefile but rather include a data.mk file
[samba.git] / source4 / build / smb_build / config_mk.pm
index ba8badc3d1fb2098a8214128663cff0f56ba0ec2..aa075490bce071bccec5935626372cbdd89b0c40 100644 (file)
-###########################################################
-### SMB Build System                                   ###
-### - config.mk parsing functions                      ###
-###                                                    ###
-###  Copyright (C) Stefan (metze) Metzmacher 2004      ###
-###  Released under the GNU GPL                                ###
-###########################################################
+# Samba Build System
+# - config.mk parsing functions
+#
+#  Copyright (C) Stefan (metze) Metzmacher 2004
+#  Copyright (C) Jelmer Vernooij 2005
+#  Released under the GNU GPL
+#
 
-package config_mk;
+package smb_build::config_mk;
 use smb_build::input;
+use File::Basename;
 
 use strict;
 
-my %attribute_types = (
-       "NOPROTO" => "bool",
-       "REQUIRED_SUBSYSTEMS" => "list",
-       "OUTPUT_TYPE" => "string",
-       "INIT_OBJ_FILES" => "list",
-       "ADD_OBJ_FILES" => "list",
-       "OBJ_FILES" => "list",
-       "SUBSYSTEM" => "string",
-       "INIT_FUNCTION" => "string",
-       "MAJOR_VERSION" => "string",
-       "MINOR_VERSION" => "string",
-       "RELEASE_VERSION" => "string",
-       "ENABLE" => "bool",
-       "CMD" => "string",
-       "MANPAGE" => "string"
-);
+my $section_types = {
+       "EXT_LIB" => {
+               "LIBS"                  => "list",
+               "CFLAGS"                => "list",
+               "CPPFLAGS"              => "list",
+               "LDFLAGS"               => "list",
+               "PC_NAME" => "string",
+               },
+       "PYTHON" => {
+               SWIG_FILE => "string",
+               "PRIVATE_DEPENDENCIES"  => "list",
+               "PUBLIC_DEPENDENCIES"   => "list",
+               "OBJ_FILES" => "list",
+               "ENABLE"                => "bool",
+               "LDFLAGS"               => "list",
+               "CFLAGS"                => "list",
+       },
+       "SUBSYSTEM" => {
+               "OBJ_FILES"             => "list",
+
+               "PRIVATE_DEPENDENCIES"  => "list",
+               "PUBLIC_DEPENDENCIES"   => "list",
+
+               "ENABLE"                => "bool",
+
+               "MANPAGE"               => "string",
+
+               "PUBLIC_PROTO_HEADER"   => "string",
+               "PRIVATE_PROTO_HEADER"  => "string",
+
+               "PUBLIC_HEADERS"        => "list",
+
+               "CFLAGS"                => "list",
+               "LDFLAGS"               => "list",
+               "STANDARD_VISIBILITY"   => "string",
+               "INIT_FUNCTION_SENTINEL" => "string"
+               },
+       "MODULE" => {
+               "SUBSYSTEM"             => "string",
+
+               "INIT_FUNCTION"         => "string",
+               "OBJ_FILES"             => "list",
+
+               "PRIVATE_DEPENDENCIES"  => "list",
+
+               "ALIASES" => "list",
+
+               "ENABLE"                => "bool",
+
+               "OUTPUT_TYPE"           => "list",
+
+               "MANPAGE"               => "string",
+               "PRIVATE_PROTO_HEADER"  => "string",
+
+               "CFLAGS"                => "list"
+               },
+       "BINARY" => {
+               "OBJ_FILES"             => "list",
+
+               "PRIVATE_DEPENDENCIES"  => "list",
+
+               "ENABLE"                => "bool",
+
+               "MANPAGE"               => "string",
+               "INSTALLDIR"            => "string",
+               "PRIVATE_PROTO_HEADER"  => "string",
+
+               "CFLAGS"                => "list",
+               "LDFLAGS"               => "list",
+               "STANDARD_VISIBILITY"   => "string",
+
+               "USE_HOSTCC"            => "bool"
+               },
+       "LIBRARY" => {
+               "VERSION"               => "string",
+               "SO_VERSION"            => "string",
+               "LIBRARY_REALNAME" => "string",
+
+               "PC_NAME" => "string",
+               "PC_FILE" => "string",
+               
+               "INIT_FUNCTION_TYPE"    => "string",
+               "INIT_FUNCTION_SENTINEL" => "string",
+               "OUTPUT_TYPE"           => "list",
+
+               "OBJ_FILES"             => "list",
+
+               "PRIVATE_DEPENDENCIES"  => "list",
+               "PUBLIC_DEPENDENCIES"   => "list",
+
+               "ENABLE"                => "bool",
+
+               "MANPAGE"               => "string",
+
+               "PUBLIC_HEADERS"        => "list",
+
+               "PUBLIC_PROTO_HEADER"   => "string",
+               "PRIVATE_PROTO_HEADER"  => "string",
+
+               "CFLAGS"                => "list",
+               "LDFLAGS"               => "list",
+               "STANDARD_VISIBILITY"   => "string"
+               }
+};
+
+use vars qw(@parsed_files);
+
+@parsed_files = ();
+
+sub _read_config_file
+{
+       use File::Basename;
+       use Cwd;
+
+       my $srcdir = shift;
+       my $builddir = shift;
+       my $filename = shift;
+       my @dirlist;
+
+       # We need to change our working directory because config.mk files can
+       # give shell commands as the argument to "include". These shell
+       # commands can take arguments that are relative paths and we don't have
+       # a way of sensibly rewriting these.
+       my $cwd = getcwd;
+       chomp $cwd;
+
+       if ($srcdir ne $builddir) {
+               # Push the builddir path on the front, so we prefer builddir
+               # to srcdir when the file exists in both.
+               @dirlist = ($builddir, $srcdir);
+       } else {
+               @dirlist = ($srcdir);
+       }
+
+       foreach my $d (@dirlist) {
+               my @lines;
+               my $basedir;
+
+               chdir $cwd;
+               chdir $d;
+
+               # We need to catch the exception from open in the case where
+               # the filename is actually a shell pipeline. Why is this
+               # different to opening a regular file? Because this is perl!
+               eval {
+                       open(CONFIG_MK, "./$filename");
+                       @lines = <CONFIG_MK>;
+                       close(CONFIG_MK);
+               };
+
+               chdir $cwd;
+               next unless (@lines);
+
+               # I blame abartlett for this crazy hack -- jpeach
+               if ($filename =~ /\|$/) {
+                       $basedir = $builddir;
+               } else {
+                       $basedir = dirname($filename);
+                       push(@parsed_files, $filename);
+               }
+               $basedir =~ s!^($builddir|$srcdir)[/]!!;
+               return ($filename, $basedir, @lines);
+       }
+
+       chdir $cwd;
+       return;
+}
 
 ###########################################################
 # The parsing function which parses the file
 #
-# $result = _parse_config_mk($filename)
+# $result = _parse_config_mk($input, $srcdir, $builddir, $filename)
 #
 # $filename -  the path of the config.mk file
 #              which should be parsed
-#
-# $result -    the resulting structure
-#
-# $result->{ERROR_CODE} -      the error_code, '0' means success
-# $result->{ERROR_STR} -       the error string
-#
-# $result->{$key}{KEY} -       the key == the variable which was parsed
-# $result->{$key}{VAL} -       the value of the variable
-sub _parse_config_mk($)
+sub run_config_mk($$$$)
 {
-       my $filename = shift;
+       sub run_config_mk($$$$);
+       my ($input, $srcdir, $builddir, $filename) = @_;
        my $result;
        my $linenum = -1;
-       my $waiting = 0;
+       my $infragment = 0;
        my $section = "GLOBAL";
-       my $key;
+       my $makefile = "";
+
+       my $basedir;
+
+       my $parsing_file;
+       my @lines;
 
-       $result->{ERROR_CODE} = -1;
+       $ENV{builddir} = $builddir;
+       $ENV{srcdir} = $srcdir;
 
-       open(CONFIG_MK, "< $filename") || die ("Can't open $filename\n");
+       ($parsing_file, $basedir, @lines) =
+           _read_config_file($srcdir, $builddir, $filename);
 
-       while (<CONFIG_MK>) {
-               my $line = $_;
-               my $val;
+       die ("$0: can't open '$filename'")
+               unless ($parsing_file and $basedir and @lines);
 
+       my $line = "";
+       my $prev = "";
+
+       # Emit a line that lets us match up final makefile output with the
+       # corresponding input files. The curlies are so you can match the
+       # BEGIN/END pairs in a text editor.
+       $makefile .= "# BEGIN{ $parsing_file\n";
+
+       foreach (@lines) {
                $linenum++;
 
-               #
-               # lines beginnig with '#' are ignored
-               # 
-               if ($line =~ /^\#.*$/) {
+               # lines beginning with '#' are ignored
+               next if (/^\#.*$/);
+               
+               if (/^(.*)\\$/) {
+                       $prev .= $1;
                        next;
+               } else {
+                       $line = "$prev$_";
+                       $prev = "";
                }
 
-               #
-               #
-               #
-               if (($waiting == 0) && ($line =~ /^\[([a-zA-Z0-9_:]+)\][\t ]*$/)) {
+               if ($line =~ /^\[([-a-zA-Z0-9_:]+)\][\t ]*$/) 
+               {
                        $section = $1;
+                       $infragment = 0;
                        next;
                }
-               
-               #
-               # 1.)   lines with an alphanumeric character indicate
-               #       a new variable, 
-               # 2.)   followed by zero or more whitespaces or tabs
-               # 3.)   then one '=' character
-               # 4.)   followed by the value of the variable
-               # 5.)   a newline ('\n') can be escaped by a '\' before the newline
-               #       and the next line needs to start with a tab ('\t')
-               #
-               if (($waiting == 0) && ($line =~ /^([a-zA-Z0-9_]+)[\t ]*=(.*)$/)) {
-                       $key = $1;
-                       $val = $2;
-
-                       #
-                       # when we have a '\' before the newline 
-                       # then skip it and wait for the next line.
-                       #
-                       if ($val =~ /(.*)(\\)$/) {
-                               $val = $1;
-                               $waiting = 1;           
-                       } else {
-                               $waiting = 0;
-                       }
 
-                       $result->{$section}{$key}{KEY} = $key;
-                       $result->{$section}{$key}{VAL} = $val;
+               # include
+               if ($line =~ /^include (.*)$/) {
+                       my $subfile= $1;
+                       my $subdir = dirname($filename);
+                       $subdir =~ s/^\.$//g;
+                       $subdir =~ s/^\.\///g;
+                       $subdir .= "/" if ($subdir ne "");
+                       $makefile .= run_config_mk($input, $srcdir, $builddir, $subdir.$subfile);
                        next;
                }
 
-               #
-               # when we are waiting for a value to continue then
-               # check if it has a leading tab.
-               #
-               if (($waiting == 1) && ($line =~ /^\t(.*)$/)) {
-                       $val = $1;
-
-                       #
-                       # when we have a '\' before the newline 
-                       # then skip it and wait for the next line.
-                       #
-                       if ($val =~ /(.*)( \\)$/) {
-                               $val = $1;
-                               $waiting = 1;           
-                       } else {
-                               $waiting = 0;
-                       }
-
-                       $result->{$section}{$key}{VAL} .= " ";
-                       $result->{$section}{$key}{VAL} .= $val;
+               # empty line
+               if ($line =~ /^[ \t]*$/) {
+                       $section = "GLOBAL";
+                       if ($infragment) { $makefile.="\n"; }
                        next;
                }
 
-               #
-               # catch empty lines they're ignored
-               # and we're no longer waiting for the value to continue
-               #
-               if ($line =~ /^$/) {
-                       $waiting = 0;
+               # global stuff is considered part of the makefile
+               if ($section eq "GLOBAL") {
+                       if (!$infragment) { $makefile.="\n"; }
+                       $makefile .= $line;
+                       $infragment = 1;
                        next;
                }
 
-               close(CONFIG_MK);
-
-               $result->{ERROR_STR} = "Bad line while parsing $filename\n$filename:$linenum: $line";
+               
+               # Assignment
+               if ($line =~ /^([a-zA-Z0-9_]+)[\t ]*=(.*)$/) {
+                       $result->{$section}{$1}{VAL} = $2;
+                       $result->{$section}{$1}{KEY} = $1;
+               
+                       next;
+               }
 
-               return $result;
+               die("$parsing_file:$linenum: Bad line while parsing $parsing_file");
        }
 
-       close(CONFIG_MK);
-
-       $result->{ERROR_CODE} = 0;
-
-       return $result;
-}
-
-sub import_file($$)
-{
-       my ($input, $filename) = @_;
-
-       my $result = _parse_config_mk($filename);
-
-       die ($result->{ERROR_STR}) unless $result->{ERROR_CODE} == 0;
+       $makefile .= "# }END $parsing_file\n";
 
        foreach my $section (keys %{$result}) {
-               next if ($section eq "ERROR_CODE");
                my ($type, $name) = split(/::/, $section, 2);
-               
+
+               my $sectype = $section_types->{$type};
+               if (not defined($sectype)) {
+                       die($parsing_file.":[".$section."] unknown section type \"".$type."\"!");
+               }
+
                $input->{$name}{NAME} = $name;
                $input->{$name}{TYPE} = $type;
+               $input->{$name}{MK_FILE} = $parsing_file;
+               $input->{$name}{BASEDIR} = $basedir;
 
                foreach my $key (values %{$result->{$section}}) {
-                       $key->{VAL} = input::strtrim($key->{VAL});
-                       my $vartype = $attribute_types{$key->{KEY}};
+                       $key->{VAL} = smb_build::input::strtrim($key->{VAL});
+                       my $vartype = $sectype->{$key->{KEY}};
                        if (not defined($vartype)) {
-                               die("Unknown attribute $key->{KEY}");
+                               die($parsing_file.":[".$section."]: unknown attribute type \"$key->{KEY}\"!");
                        }
                        if ($vartype eq "string") {
                                $input->{$name}{$key->{KEY}} = $key->{VAL};
                        } elsif ($vartype eq "list") {
-                               $input->{$name}{$key->{KEY}} = [input::str2array($key->{VAL})];
+                               $input->{$name}{$key->{KEY}} = [smb_build::input::str2array($key->{VAL})];
                        } elsif ($vartype eq "bool") {
                                if (($key->{VAL} ne "YES") and ($key->{VAL} ne "NO")) {
-                                       die("Invalid value for bool attribute $key->{KEY}: $key->{VAL}");
+                                       die("Invalid value for bool attribute $key->{KEY}: $key->{VAL} in section $section");
                                }
                                $input->{$name}{$key->{KEY}} = $key->{VAL};
                        }
                }
        }
+
+       return $makefile;
 }
+
 1;