Support 'mkinclude' command in .mk files.
[ira/wip.git] / source / build / smb_build / config_mk.pm
index 5c5c0ad92b84e553fe28b737fbe6c3f1059108c1..b7dc9697a9667f74bc113de9e1004e408a8041d6 100644 (file)
@@ -18,7 +18,17 @@ my $section_types = {
                "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",
 
@@ -36,7 +46,8 @@ my $section_types = {
 
                "CFLAGS"                => "list",
                "LDFLAGS"               => "list",
-               "STANDARD_VISIBILITY"   => "string"
+               "STANDARD_VISIBILITY"   => "string",
+               "INIT_FUNCTION_SENTINEL" => "string"
                },
        "MODULE" => {
                "SUBSYSTEM"             => "string",
@@ -44,21 +55,16 @@ my $section_types = {
                "INIT_FUNCTION"         => "string",
                "OBJ_FILES"             => "list",
 
-               "PUBLIC_DEPENDENCIES"   => "list",
                "PRIVATE_DEPENDENCIES"  => "list",
 
                "ALIASES" => "list",
 
                "ENABLE"                => "bool",
 
-               "OUTPUT_TYPE"           => "string",
+               "OUTPUT_TYPE"           => "list",
 
                "MANPAGE"               => "string",
                "PRIVATE_PROTO_HEADER"  => "string",
-               "PUBLIC_PROTO_HEADER"   => "string",
-
-
-               "PUBLIC_HEADERS"        => "list",
 
                "CFLAGS"                => "list"
                },
@@ -72,10 +78,9 @@ my $section_types = {
                "MANPAGE"               => "string",
                "INSTALLDIR"            => "string",
                "PRIVATE_PROTO_HEADER"  => "string",
-               "PUBLIC_PROTO_HEADER"   => "string",
-               "PUBLIC_HEADERS"        => "list", 
 
                "CFLAGS"                => "list",
+               "LDFLAGS"               => "list",
                "STANDARD_VISIBILITY"   => "string",
 
                "USE_HOSTCC"            => "bool"
@@ -84,13 +89,16 @@ my $section_types = {
                "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",
 
-               "DESCRIPTION"           => "string",
-
                "PRIVATE_DEPENDENCIES"  => "list",
                "PUBLIC_DEPENDENCIES"   => "list",
 
@@ -113,10 +121,69 @@ 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
@@ -130,26 +197,28 @@ sub run_config_mk($$$$)
        my $section = "GLOBAL";
        my $makefile = "";
 
-       my $parsing_file = $builddir."/".$filename;
+       my $basedir;
 
-       $ENV{samba_builddir} = $builddir;
-       $ENV{samba_srcdir} = $srcdir;
-       
-       if (!open(CONFIG_MK, $parsing_file)) { 
-               $parsing_file = $srcdir."/".$filename;
-               open(CONFIG_MK, $parsing_file) or 
-                   die("Can't open neither `$builddir."/".$filename' nor `$srcdir/$filename'\n");
-       }
-       
-       push (@parsed_files, $parsing_file);
-       
-       
-       my @lines = <CONFIG_MK>;
-       close(CONFIG_MK);
+       my $parsing_file;
+       my @lines;
+
+       $ENV{builddir} = $builddir;
+       $ENV{srcdir} = $srcdir;
+
+       ($parsing_file, $basedir, @lines) =
+           _read_config_file($srcdir, $builddir, $filename);
+
+       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++;
 
@@ -173,10 +242,17 @@ sub run_config_mk($$$$)
 
                # include
                if ($line =~ /^include (.*)$/) {
-                       $makefile .= run_config_mk($input, $srcdir, $builddir, dirname($filename)."/$1");
+                       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;
                }
 
+               $line =~ s/^mkinclude /include /;
+
                # empty line
                if ($line =~ /^[ \t]*$/) {
                        $section = "GLOBAL";
@@ -204,6 +280,8 @@ sub run_config_mk($$$$)
                die("$parsing_file:$linenum: Bad line while parsing $parsing_file");
        }
 
+       $makefile .= "# }END $parsing_file\n";
+
        foreach my $section (keys %{$result}) {
                my ($type, $name) = split(/::/, $section, 2);
 
@@ -215,7 +293,7 @@ sub run_config_mk($$$$)
                $input->{$name}{NAME} = $name;
                $input->{$name}{TYPE} = $type;
                $input->{$name}{MK_FILE} = $parsing_file;
-               $input->{$name}{BASEDIR} = dirname($filename);
+               $input->{$name}{BASEDIR} = $basedir;
 
                foreach my $key (values %{$result->{$section}}) {
                        $key->{VAL} = smb_build::input::strtrim($key->{VAL});