r14606: Fix paths in developer mode. This allows 'make quicktest' to work when
[ira/wip.git] / source / build / smb_build / makefile.pm
index 514bfac0b4d731e5ab8cbbb08a3134e98aea5124..ec306c2bb7215d6456480a610dba2ede4b2a5d85 100644 (file)
-###########################################################
-### SMB Build System                                   ###
-### - create output for Makefile                       ###
-###                                                    ###
-###  Copyright (C) Stefan (metze) Metzmacher 2004      ###
-###  Copyright (C) Jelmer Vernooij 2005                        ###
-###  Released under the GNU GPL                                ###
-###########################################################
-
-package makefile;
-use config qw(%config);
-use strict;
+# Samba Build System
+# - create output for Makefile
+#
+#  Copyright (C) Stefan (metze) Metzmacher 2004
+#  Copyright (C) Jelmer Vernooij 2005
+#  Released under the GNU GPL
 
-sub _prepare_path_vars()
-{
-       my $output;
-
-       $output = << '__EOD__';
-prefix = @prefix@
-exec_prefix = @exec_prefix@
-selftest_prefix = @selftest_prefix@
-VPATH = @srcdir@
-srcdir = @srcdir@
-builddir = @builddir@
-
-BASEDIR = @prefix@
-BINDIR = @bindir@
-SBINDIR = @sbindir@
-LIBDIR = @libdir@
-CONFIGDIR = @configdir@
-localstatedir = @localstatedir@
-SWATDIR = @swatdir@
-VARDIR = @localstatedir@
-
-# The permissions to give the executables
-INSTALLPERMS = 0755
-
-# set these to where to find various files
-# These can be overridden by command line switches (see smbd(8))
-# or in smb.conf (see smb.conf(5))
-LOGFILEBASE = @logfilebase@
-CONFIGFILE = $(CONFIGDIR)/smb.conf
-LMHOSTSFILE = $(CONFIGDIR)/lmhosts
-NCALRPCDIR = @localstatedir@/ncalrpc
-
-# This is where smbpasswd et al go
-PRIVATEDIR = @privatedir@
-SMB_PASSWD_FILE = $(PRIVATEDIR)/smbpasswd
-
-# the directory where lock files go
-LOCKDIR = @lockdir@
-
-# the directory where pid files go
-PIDDIR = @piddir@
-
-MANDIR = @mandir@
-
-PATH_FLAGS = -DCONFIGFILE=\"$(CONFIGFILE)\"  -DSBINDIR=\"$(SBINDIR)\" \
-        -DBINDIR=\"$(BINDIR)\" -DLMHOSTSFILE=\"$(LMHOSTSFILE)\" \
-        -DLOCKDIR=\"$(LOCKDIR)\" -DPIDDIR=\"$(PIDDIR)\" -DLIBDIR=\"$(LIBDIR)\" \
-        -DLOGFILEBASE=\"$(LOGFILEBASE)\" -DSHLIBEXT=\"@SHLIBEXT@\" \
-        -DCONFIGDIR=\"$(CONFIGDIR)\" -DNCALRPCDIR=\"$(NCALRPCDIR)\" \
-        -DSWATDIR=\"$(SWATDIR)\" -DSMB_PASSWD_FILE=\"$(SMB_PASSWD_FILE)\" \
-        -DPRIVATE_DIR=\"$(PRIVATEDIR)\"
-__EOD__
+package smb_build::makefile;
+use smb_build::env;
+use strict;
 
-       return $output;
-}
+use base 'smb_build::env';
 
-sub _prepare_compiler_linker()
+sub new($$$)
 {
-       return << "__EOD__";
-SHELL=$config{SHELL}
-PERL=$config{PERL}
-CC=$config{CC}
-CFLAGS=-I\$(srcdir)/include -I\$(srcdir) -D_SAMBA_BUILD_ -DHAVE_CONFIG_H -I\$(srcdir)/lib $config{CFLAGS} $config{CPPFLAGS}
-
-LD=$config{LD}
-LD_FLAGS=$config{LDFLAGS} -Lbin
-
-STLD=$config{AR}
-STLD_FLAGS=-rc
-
-SHLD=$config{CC}
-SHLD_FLAGS=$config{LDSHFLAGS} -Lbin
-
-XSLTPROC=$config{XSLTPROC}
-
-LEX=$config{LEX}
-YACC=$config{YACC}
+       my ($myname, $config, $mkfile) = @_;
+       my $self = new smb_build::env($config);
+       
+       bless($self, $myname);
+
+       $self->{manpages} = [];
+       $self->{sbin_progs} = [];
+       $self->{bin_progs} = [];
+       $self->{static_libs} = [];
+       $self->{shared_libs} = [];
+       $self->{installable_shared_libs} = [];
+       $self->{headers} = [];
+       $self->{shared_modules} = [];
+       $self->{plugins} = [];
+       $self->{install_plugins} = "";
+       $self->{uninstall_plugins} = "";
+       $self->{pc_files} = [];
+       $self->{proto_headers} = [];
+       $self->{output} = "";
+
+       $self->{mkfile} = $mkfile;
+
+       $self->output("#!gmake\n");
+       $self->output("################################################\n");
+       $self->output("# Autogenerated by build/smb_build/makefile.pm #\n");
+       $self->output("################################################\n");
+       $self->output("\n");
+
+       if (!$self->{automatic_deps}) {
+               $self->output("ALL_PREDEP = proto\n");
+       }
 
-CPP=$config{CPP}
+       $self->output("default: all\n\n");
 
-DEFAULT_TEST_TARGET=$config{DEFAULT_TEST_TARGET}
+       $self->_prepare_path_vars();
+       $self->_prepare_compiler_linker();
 
-__EOD__
+       return $self;
 }
 
-sub _prepare_default_rule()
+sub output($$)
 {
-       return << '__EOD__';
-default: all
+       my ($self, $text) = @_;
 
-__EOD__
+       $self->{output} .= $text;
 }
 
-sub _prepare_SUFFIXES()
+sub _prepare_path_vars($)
 {
-       return << '__EOD__';
-.SUFFIXES: .x .c .et .y .l .d .o .h .h.gch .a .so .1 .1.xml .3 .3.xml .5 .5.xml .7 .7.xml
+       my ($self) = @_;
+
+       $self->output(<< "__EOD__"
+prefix = $self->{config}->{prefix}
+exec_prefix = $self->{config}->{exec_prefix}
+selftest_prefix = $self->{config}->{selftest_prefix}
+VPATH = $self->{config}->{srcdir}:heimdal/lib/asn1:heimdal/lib/krb5:heimdal/lib/gssapi:heimdal/lib/hdb:heimdal/lib/roken
+srcdir = $self->{config}->{srcdir}
+builddir = $self->{config}->{builddir}
+
+BASEDIR = $self->{config}->{prefix}
+BINDIR = $self->{config}->{bindir}
+SBINDIR = $self->{config}->{sbindir}
+LIBDIR = $self->{config}->{libdir}
+MODULESDIR = $self->{config}->{modulesdir}
+INCLUDEDIR = $self->{config}->{includedir}
+CONFIGDIR = $self->{config}->{sysconfdir}
+DATADIR = $self->{config}->{datadir}
+SWATDIR = $self->{config}->{datadir}/swat
+JSDIR = $self->{config}->{datadir}/js
+SETUPDIR = $self->{config}->{datadir}/setup
+VARDIR = $self->{config}->{localstatedir}
+LOGFILEBASE = $self->{config}->{logfilebase}
+NCALRPCDIR = $self->{config}->{localstatedir}/ncalrpc
+LOCKDIR = $self->{config}->{lockdir}
+PIDDIR = $self->{config}->{piddir}
+MANDIR = $self->{config}->{mandir}
+PRIVATEDIR = $self->{config}->{privatedir}
+WINBINDD_SOCKET_DIR = $self->{config}->{winbindd_socket_dir}
 
 __EOD__
+);
 }
 
-sub _prepare_man_rule($)
+sub _prepare_compiler_linker($)
 {
-       my $suffix = shift;
+       my ($self) = @_;
 
-       return << "__EOD__";
-.$suffix.xml.$suffix:
-       \$(XSLTPROC) -o \$@ http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl \$<
+       my $devld_local = "";
+       my $devld_install = "";
 
-__EOD__
-}
+       $self->{duplicate_build} = 0;
+       if ($self->{config}->{LIBRARY_OUTPUT_TYPE} eq "SHARED_LIBRARY") {
+               if ($self->{developer}) {
+                       $self->{duplicate_build} = 1;
+                       $devld_local = " -Wl,-rpath,\$(builddir)/bin";
+               }
+               $devld_install = " -Wl,-rpath-link,\$(builddir)/bin";
+       }
 
-sub _prepare_binaries($)
-{
-       my $ctx = shift;
+       $self->output(<< "__EOD__"
+SHELL=$self->{config}->{SHELL}
 
-       my @bbn_list = ();
-       my @sbn_list = ();
+PERL=$self->{config}->{PERL}
 
-       foreach (values %$ctx) {
-               next unless defined $_->{OUTPUT_TYPE};
-               next unless ($_->{OUTPUT_TYPE} eq "BINARY");
+CPP=$self->{config}->{CPP}
+CPPFLAGS=$self->{config}->{CPPFLAGS}
 
-               push(@sbn_list, $_->{OUTPUT}) if ($_->{INSTALLDIR} eq "SBINDIR");
-               push(@bbn_list, $_->{OUTPUT}) if ($_->{INSTALLDIR} eq "BINDIR");
-       }
+CC=$self->{config}->{CC}
+CFLAGS=-I\$(srcdir)/include -I\$(srcdir) -I\$(srcdir)/lib -D_SAMBA_BUILD_ -DHAVE_CONFIG_H $self->{config}->{CFLAGS} \$(CPPFLAGS)
+PICFLAG=$self->{config}->{PICFLAG}
+HOSTCC=$self->{config}->{HOSTCC}
 
-       my $bbn = array2oneperline(\@bbn_list);
-       my $sbn = array2oneperline(\@sbn_list);
-       return << "__EOD__";
-BIN_PROGS = $bbn
-SBIN_PROGS = $sbn
-__EOD__
-}
+LOCAL_LINK_FLAGS=$devld_local
+INSTALL_LINK_FLAGS=$devld_install
 
-sub _prepare_manpages($)
-{
-       my $ctx = shift;
+LD=$self->{config}->{LD} 
+LDFLAGS=$self->{config}->{LDFLAGS} -L\$(builddir)/bin
 
-       my @mp_list = ();
+STLD=$self->{config}->{AR}
+STLD_FLAGS=-rc -L\$(builddir)/bin
 
-       foreach (values %$ctx) {
-               push (@mp_list, $_->{MANPAGE}) if (defined($_->{MANPAGE}) and $_->{MANPAGE} ne "");
-       }
-       
-       my $mp = array2oneperline(\@mp_list);
-       return << "__EOD__";
-MANPAGES = $mp
+SHLD=$self->{config}->{CC}
+SHLD_FLAGS=$self->{config}->{LDSHFLAGS} -L\$(builddir)/bin
+SHLIBEXT=$self->{config}->{SHLIBEXT}
 
-__EOD__
-}
+XSLTPROC=$self->{config}->{XSLTPROC}
 
-sub _prepare_dummy_MAKEDIR()
-{
-       my $ctx = shift;
+LEX=$self->{config}->{LEX}
+YACC=$self->{config}->{YACC}
+YAPP=$self->{config}->{YAPP}
+PIDL_ARGS=$self->{config}->{PIDL_ARGS}
 
-       return  << '__EOD__';
-bin/.dummy:
-       @: >> $@ || : > $@
+GCOV=$self->{config}->{GCOV}
 
-dynconfig.o: dynconfig.c Makefile
-       @echo Compiling $*.c
-       @$(CC) $(CFLAGS) @PICFLAG@ $(PATH_FLAGS) -c $< -o $@
-@BROKEN_CC@    -mv `echo $@ | sed 's%^.*/%%g'` $@
+DEFAULT_TEST_TARGET=$self->{config}->{DEFAULT_TEST_TARGET}
 
 __EOD__
+);
 }
 
-sub _prepare_depend_CC_rule()
+sub _prepare_mk_files($)
 {
-       return << '__EOD__';
+       my $self = shift;
+       my @tmp = ();
 
-.c.d:
-       @echo "Generating dependencies for $<"
-       @$(CC) -MM -MG -MT $(<:.c=.o) -MF $@ $(CFLAGS) $<
-
-__EOD__
-}
-
-###########################################################
-# This function creates a standard make rule which is using $(CC)
-#
-# $output = _prepare_std_CC_rule($srcext,$destext,$flags,$message,$comment)
-#
-# $srcext -    sourcefile extension
-#
-# $destext -   destinationfile extension
-#
-# $flags -     additional compiler flags
-#
-# $message -   logmessage which is echoed while running this rule
-#
-# $comment -   just a comment what this rule should do
-#
-# $output -            the resulting output buffer
-sub _prepare_std_CC_rule($$$$$)
-{
-       my ($src,$dst,$flags,$message,$comment) = @_;
-
-       return << "__EOD__";
-# $comment
-.$src.$dst:
-       \@echo $message \$\*.$src
-       \@\$(CC) `script/cflags.sh \$\@` \$(CFLAGS) $flags -c \$< -o \$\@
-\@BROKEN_CC\@  -mv `echo \$\@ | sed 's%^.*/%%g'` \$\@
+       
+       foreach (@smb_build::config_mk::parsed_files) {
+               s/ .*$//g;
+               push (@tmp, $_);
+       }
 
-__EOD__
+       $self->output("MK_FILES = " . array2oneperline(\@tmp) . "\n");
 }
 
 sub array2oneperline($)
@@ -234,427 +181,365 @@ sub array2oneperline($)
        return $output;
 }
 
-###########################################################
-# This function creates a object file list
-#
-# $output = _prepare_var_obj_list($var, $var_ctx)
-#
-# $var_ctx -           the subsystem context
-#
-# $var_ctx->{NAME}     -       the <var> name
-# $var_ctx->{OBJ_LIST}         -       the list of objectfiles which sould be linked to this <var>
-#
-# $output -            the resulting output buffer
-sub _prepare_obj_list($$)
+sub _prepare_list($$$)
 {
-       my ($var,$ctx) = @_;
+       my ($self,$ctx,$var) = @_;
 
-       my $tmplist = array2oneperline($ctx->{OBJ_LIST});
-       return "" if ($tmplist eq "");
+       my $tmplist = array2oneperline($ctx->{$var});
+       return if ($tmplist eq "");
 
-       return << "__EOD__";
-$var\_$ctx->{NAME}_OBJS =$tmplist
-__EOD__
+       $self->output("$ctx->{TYPE}\_$ctx->{NAME}_$var =$tmplist\n");
 }
 
-sub _prepare_cflags($$)
+sub SharedLibrary($$)
 {
-       my ($var,$ctx) = @_;
+       my ($self,$ctx) = @_;
 
-       my $tmplist = array2oneperline($ctx->{CFLAGS});
-       return "" if ($tmplist eq "");
+       my $installdir;
+       my $init_obj = "";
+       
+       if ($self->{duplicate_build}) {
+               $installdir = $ctx->{RELEASEDIR};
+       } else {
+               $installdir = $ctx->{DEBUGDIR};
+       }
 
-       return << "__EOD__";
-$var\_$ctx->{NAME}_CFLAGS =$tmplist
+       if ($ctx->{TYPE} eq "LIBRARY") {
+               push (@{$self->{shared_libs}}, "$ctx->{DEBUGDIR}/$ctx->{LIBRARY_REALNAME}");
+               push (@{$self->{installable_shared_libs}}, "$installdir/$ctx->{LIBRARY_REALNAME}") if (defined($ctx->{SO_VERSION}));
+       } elsif ($ctx->{TYPE} eq "MODULE") {
+               push (@{$self->{shared_modules}}, "$ctx->{DEBUGDIR}/$ctx->{FIXED_NAME}.\$(SHLIBEXT)");
+               push (@{$self->{plugins}}, "$installdir/$ctx->{LIBRARY_REALNAME}");
+
+               $self->{install_plugins} .= "\t\@echo Installing $installdir/$ctx->{LIBRARY_REALNAME} as \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$ctx->{FIXED_NAME}.\$(SHLIBEXT)\n";
+               $self->{install_plugins} .= "\t\@mkdir -p \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/\n";
+               $self->{install_plugins} .= "\t\@cp $installdir/$ctx->{LIBRARY_REALNAME} \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$ctx->{FIXED_NAME}.\$(SHLIBEXT)\n";
+               $self->{uninstall_plugins} .= "\t\@echo Uninstalling \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$ctx->{FIXED_NAME}.\$(SHLIBEXT)\n";
+               $self->{uninstall_plugins} .= "\t\@-rm \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$ctx->{FIXED_NAME}.\$(SHLIBEXT)\n";
+               if (defined($ctx->{ALIASES})) {
+                       foreach (@{$ctx->{ALIASES}}) {
+                               $self->{install_plugins} .= "\t\@ln -s $ctx->{FIXED_NAME}.\$(SHLIBEXT) \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$_.\$(SHLIBEXT)\n";
+                               $self->{uninstall_plugins} .= "\t\@-rm \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$_.\$(SHLIBEXT)\n";
+                       }
+               }
+       }
 
+       $self->_prepare_list($ctx, "OBJ_LIST");
+       $self->_prepare_list($ctx, "CFLAGS");
+       $self->_prepare_list($ctx, "DEPEND_LIST");
+       $self->_prepare_list($ctx, "LINK_LIST");
+       $self->_prepare_list($ctx, "LINK_FLAGS");
+
+       push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)");
+
+       if ($ctx->{TYPE} eq "MODULE" and defined($ctx->{INIT_FUNCTION})) {
+               my $init_fn = $ctx->{INIT_FUNCTION_TYPE};
+               $init_fn =~ s/\(\*\)/init_module/;
+               my $proto_fn = $ctx->{INIT_FUNCTION_TYPE};
+               $proto_fn =~ s/\(\*\)/$ctx->{INIT_FUNCTION}/;
+
+               $self->output(<< "__EOD__"
+bin/$ctx->{NAME}_init_module.c:
+       \@echo Creating \$\@
+       \@echo \"#include \\\"includes.h\\\"\" > \$\@
+       \@echo \"$proto_fn;\" >> \$\@
+       \@echo -e \"_PUBLIC_ $init_fn \\n{\\n\\treturn $ctx->{INIT_FUNCTION}();\\n}\\n\" >> \$\@
 __EOD__
-}
-
-###########################################################
-# This function creates a make rule for linking a library
-#
-# $output = _prepare_shared_library_rule($library_ctx)
-#
-# $library_ctx -               the library context
-#
-# $library_ctx->{NAME} -               the library name
-#
-# $library_ctx->{DEPEND_LIST} -                the list of rules on which this library depends
-#
-# $library_ctx->{LIBRARY_NAME} -       the shared library name
-# $library_ctx->{LIBRARY_REALNAME} -   the shared library real name
-# $library_ctx->{LIBRARY_SONAME} - the shared library soname
-# $library_ctx->{LINK_LIST} -  the list of objectfiles and external libraries
-#                                      which sould be linked to this shared library
-# $library_ctx->{LINK_FLAGS} - linker flags used by this shared library
-#
-# $output -            the resulting output buffer
-sub _prepare_shared_library_rule($)
-{
-       my $ctx = shift;
-       my $output;
+);
+               $init_obj = "bin/$ctx->{NAME}_init_module.o";
+       }
 
-       my $tmpdepend = array2oneperline($ctx->{DEPEND_LIST});
-       my $tmpshlink = array2oneperline($ctx->{LINK_LIST});
-       my $tmpshflag = array2oneperline($ctx->{LINK_FLAGS});
+       my $soarg = "";
+       my $soargdebug = "";
+       if ($self->{config}->{SONAMEFLAG} ne "" and 
+               defined($ctx->{LIBRARY_SONAME})) {
+               $soarg = "$self->{config}->{SONAMEFLAG}$ctx->{LIBRARY_SONAME} ";
+               if ($ctx->{LIBRARY_REALNAME} ne $ctx->{LIBRARY_SONAME}) {
+                       $soargdebug = "\n\t\@ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{DEBUGDIR}/$ctx->{LIBRARY_SONAME}";
+               }
+       }
 
-       $output = << "__EOD__";
-LIBRARY_$ctx->{NAME}_DEPEND_LIST =$tmpdepend
-LIBRARY_$ctx->{NAME}_SHARED_LINK_LIST =$tmpshlink
-LIBRARY_$ctx->{NAME}_SHARED_LINK_FLAGS =$tmpshflag
+       if ($self->{duplicate_build}) {
+               $self->output(<< "__EOD__"
 #
 
-$ctx->{TARGET}: \$(LIBRARY_$ctx->{NAME}_DEPEND_LIST) \$(LIBRARY_$ctx->{NAME}_OBJS) bin/.dummy
+$ctx->{TARGET}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) $init_obj
        \@echo Linking \$\@
-       \@\$(SHLD) \$(SHLD_FLAGS) -o \$\@ \\
-               \$(LIBRARY_$ctx->{NAME}_SHARED_LINK_FLAGS) \\
-               \$(LIBRARY_$ctx->{NAME}_SHARED_LINK_LIST)
-
+       \@mkdir -p $ctx->{DEBUGDIR}
+       \@\$(SHLD) \$(SHLD_FLAGS) -o \$\@ \$(LOCAL_LINK_FLAGS) \\
+               \$($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) $soarg \\
+               $init_obj \$($ctx->{TYPE}_$ctx->{NAME}_LINK_LIST)$soargdebug
 __EOD__
+);
+               if (defined($ctx->{ALIASES})) {
+                       foreach (@{$ctx->{ALIASES}}) {
+                               $self->output("\t\@ln -s $ctx->{FIXED_NAME}.\$(SHLIBEXT) $ctx->{DEBUGDIR}/$_.\$(SHLIBEXT)\n");
+                       }
+               }
 
-       if (defined($ctx->{LIBRARY_SONAME})) {
-           $output .= << "__EOD__";
-# Symlink $ctx->{LIBRARY_SONAME}
-bin/$ctx->{LIBRARY_SONAME}: bin/$ctx->{LIBRARY_REALNAME} bin/.dummy
-       \@echo Symlink \$\@
-       \@ln -sf $ctx->{LIBRARY_REALNAME} \$\@
-# Symlink $ctx->{LIBRARY_NAME}
-bin/$ctx->{LIBRARY_NAME}: bin/$ctx->{LIBRARY_SONAME} bin/.dummy
-       \@echo Symlink \$\@
-       \@ln -sf $ctx->{LIBRARY_SONAME} \$\@
-
-__EOD__
+               $self->output("\n");
        }
 
-$output .= << "__EOD__";
-library_$ctx->{NAME}: basics bin/lib$ctx->{LIBRARY_NAME}
+       $self->output(<< "__EOD__"
+#
 
-__EOD__
+$installdir/$ctx->{LIBRARY_REALNAME}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) $init_obj
+       \@echo Linking \$\@
+       \@\$(SHLD) \$(SHLD_FLAGS) -o \$\@ \\
+               \$($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) $soarg \\
+               $init_obj \$($ctx->{TYPE}_$ctx->{NAME}_LINK_LIST)
 
-       return $output;
+__EOD__
+);
 }
 
-sub _prepare_mergedobj_rule($)
+sub MergedObj($$)
 {
-       my $ctx = shift;
-
-       return "" unless $ctx->{TARGET};
-
-       my $tmpdepend = array2oneperline($ctx->{DEPEND_LIST});
+       my ($self,$ctx) = @_;
 
-       my $output = "$ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST = $tmpdepend\n";
+       return unless $ctx->{TARGET};
 
-       $output .= "$ctx->{TARGET}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJS)\n";
+       $self->_prepare_list($ctx, "OBJ_LIST");
+       $self->_prepare_list($ctx, "CFLAGS");
+       $self->_prepare_list($ctx, "DEPEND_LIST");
 
-       $output .= "\t\@echo \"Pre-Linking $ctx->{TYPE} $ctx->{NAME}\"\n";
-       $output .= "\t@\$(LD) -r \$($ctx->{TYPE}_$ctx->{NAME}_OBJS) -o $ctx->{TARGET}\n";
-       $output .= "\n";
+       push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)");
+               
+       $self->output("$ctx->{TARGET}: \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)\n");
 
-       return $output;
+       $self->output("\t\@echo \"Pre-Linking $ctx->{TYPE} $ctx->{NAME}\"\n");
+       $self->output("\t@\$(LD) -r \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) -o $ctx->{TARGET}\n");
+       $self->output("\n");
 }
 
-sub _prepare_objlist_rule($)
+sub ObjList($$)
 {
-       my $ctx = shift;
-       my $tmpdepend = array2oneperline($ctx->{DEPEND_LIST});
-
-       return "" unless $ctx->{TARGET};
-
-       my $output = "$ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST = $tmpdepend\n";
-       $output .= "$ctx->{TARGET}: ";
-       $output .= "\$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJS)\n";
-       $output .= "\t\@touch $ctx->{TARGET}\n";
-
-       return $output;
+       my ($self,$ctx) = @_;
+
+       return unless $ctx->{TARGET};
+
+       push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)");
+               
+       $self->_prepare_list($ctx, "OBJ_LIST");
+       $self->_prepare_list($ctx, "CFLAGS");
+       $self->_prepare_list($ctx, "DEPEND_LIST");
+       $self->output("$ctx->{TARGET}: ");
+       $self->output("\$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)\n");
+       $self->output("\t\@touch $ctx->{TARGET}\n");
 }
 
-###########################################################
-# This function creates a make rule for linking a library
-#
-# $output = _prepare_static_library_rule($library_ctx)
-#
-# $library_ctx -               the library context
-#
-# $library_ctx->{NAME} -               the library name
-#
-# $library_ctx->{DEPEND_LIST} -                the list of rules on which this library depends
-#
-# $library_ctx->{LIBRARY_NAME} -       the static library name
-# $library_ctx->{LINK_LIST} -  the list of objectfiles which sould be linked
-#                                      to this static library
-# $library_ctx->{LINK_FLAGS} - linker flags used by this static library
-#
-# $output -            the resulting output buffer
-sub _prepare_static_library_rule($)
+sub StaticLibrary($$)
 {
-       my $ctx = shift;
-       my $output;
+       my ($self,$ctx) = @_;
 
-       my $tmpdepend = array2oneperline($ctx->{DEPEND_LIST});
-       my $tmpstlink = array2oneperline($ctx->{LINK_LIST});
-       my $tmpstflag = array2oneperline($ctx->{LINK_FLAGS});
+       push (@{$self->{static_libs}}, $ctx->{OUTPUT});
 
-       $output = << "__EOD__";
-LIBRARY_$ctx->{NAME}_DEPEND_LIST =$tmpdepend
-#
-LIBRARY_$ctx->{NAME}_STATIC_LINK_LIST =$tmpstlink
+       $self->_prepare_list($ctx, "OBJ_LIST");
+       $self->_prepare_list($ctx, "CFLAGS");
+
+       $self->_prepare_list($ctx, "DEPEND_LIST");
+       $self->_prepare_list($ctx, "LINK_LIST");
+       $self->_prepare_list($ctx, "LINK_FLAGS");
+
+       push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)");
+               
+       $self->output(<< "__EOD__"
 #
-$ctx->{TARGET}: \$(LIBRARY_$ctx->{NAME}_DEPEND_LIST) \$(LIBRARY_$ctx->{NAME}_OBJS) bin/.dummy
+$ctx->{TARGET}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) 
        \@echo Linking \$@
        \@\$(STLD) \$(STLD_FLAGS) \$@ \\
-               \$(LIBRARY_$ctx->{NAME}_STATIC_LINK_LIST)
-
-library_$ctx->{NAME}: basics $ctx->{TARGET}
+               \$($ctx->{TYPE}_$ctx->{NAME}_LINK_LIST)
 
 __EOD__
-
-       return $output;
+);
 }
 
-###########################################################
-# This function creates a make rule for linking a binary
-#
-# $output = _prepare_binary_rule($binary_ctx)
-#
-# $binary_ctx -                the binary context
-#
-# $binary_ctx->{NAME} -                the binary name
-# $binary_ctx->{BINARY} -      the binary binary name
-#
-# $binary_ctx->{DEPEND_LIST} - the list of rules on which this binary depends
-# $binary_ctx->{LINK_LIST} -   the list of objectfiles and external libraries
-#                              which sould be linked to this binary
-# $binary_ctx->{LINK_FLAGS} -  linker flags used by this binary
-#
-# $output -            the resulting output buffer
-sub _prepare_binary_rule($)
+sub Header($$)
 {
-       my $ctx = shift;
-
-       my $tmpdepend = array2oneperline($ctx->{DEPEND_LIST});
-       my $tmplink = array2oneperline($ctx->{LINK_LIST});
-       my $tmpflag = array2oneperline($ctx->{LINK_FLAGS});
-
-       my $output = << "__EOD__";
-#
-BINARY_$ctx->{NAME}_DEPEND_LIST =$tmpdepend
-BINARY_$ctx->{NAME}_LINK_LIST =$tmplink
-BINARY_$ctx->{NAME}_LINK_FLAGS =$tmpflag
-#
-bin/$ctx->{BINARY}: bin/.dummy \$(BINARY_$ctx->{NAME}_DEPEND_LIST) \$(BINARY_$ctx->{NAME}_OBJS)
-       \@echo Linking \$\@
-       \@\$(CC) \$(LD_FLAGS) -o \$\@ \\
-               \$\(BINARY_$ctx->{NAME}_LINK_FLAGS) \\
-               \$\(BINARY_$ctx->{NAME}_LINK_LIST) \\
-               \$\(BINARY_$ctx->{NAME}_LINK_FLAGS)
-binary_$ctx->{BINARY}: basics bin/$ctx->{BINARY}
+       my ($self,$ctx) = @_;
 
-__EOD__
-
-       return $output;
+       foreach (@{$ctx->{PUBLIC_HEADERS}}) {
+               push (@{$self->{headers}}, "$ctx->{BASEDIR}/$_");
+       }
 }
 
-sub _prepare_custom_rule($)
+sub Binary($$)
 {
-       my $ctx = shift;
-       return "
-$ctx->{NAME}: bin/.TARGET_$ctx->{NAME}
-
-bin/.TARGET_$ctx->{NAME}:
-       $ctx->{CMD}
-       touch bin/.TARGET_$ctx->{NAME}
-";
-}
+       my ($self,$ctx) = @_;
 
-sub _prepare_proto_rules()
-{
-       my $output = << '__EOD__';
-# Making this target will just make sure that the prototype files
-# exist, not necessarily that they are up to date.  Since they're
-# removed by 'make clean' this will always be run when you do anything
-# afterwards.
-proto_exists: include/proto.h
-
-delheaders: pch_clean
-       -rm -f $(builddir)/include/proto.h
-
-include/proto.h:
-       @cd $(srcdir) && $(SHELL) script/mkproto.sh "$(PERL)" \
-         -h _PROTO_H_ $(builddir)/include/proto.h \
-         $(PROTO_PROTO_OBJS)
-
-# 'make headers' or 'make proto' calls a subshell because we need to
-# make sure these commands are executed in sequence even for a
-# parallel make.
-headers: delheaders proto_exists
-
-proto: idl headers
-
-proto_test:
-       @[ -f $(builddir)/include/proto.h ] || $(MAKE) proto
-
-clean: delheaders heimdal_clean
-       @echo Removing objects
-       @-find . -name '*.o' -exec rm -f '{}' \;
-       @echo Removing binaries
-       @-rm -f bin/*
-       @echo Removing dummy targets
-       @-rm -f bin/.*_*
-       @echo Removing generated files
-       @-rm -rf librpc/gen_*
-
-
-
-
-
-distclean: clean
-       -rm -f bin/.dummy
-       -rm -f include/config.h include/smb_build.h
-       -rm -f Makefile Makefile.in
-       -rm -f config.status
-       -rm -f config.log config.cache
-       -rm -f samba4-deps.dot
-       -rm -f config.pm config.mk
-       -rm -f lib/registry/winregistry.pc
-__EOD__
+       my $installdir;
+       
+       if ($self->{duplicate_build}) {
+               $installdir = "bin/install";
+       } else {
+               $installdir = "bin";
+       }
 
-       if ($config{developer} eq "yes") {
-               $output .= "\t\@-rm -f \$(_ALL_OBJS_OBJS:.o=.d)\n";
+       push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)");
+               
+       unless (defined($ctx->{INSTALLDIR})) {
+       } elsif ($ctx->{INSTALLDIR} eq "SBINDIR") {
+               push (@{$self->{sbin_progs}}, "$installdir/$ctx->{BINARY}");
+       } elsif ($ctx->{INSTALLDIR} eq "BINDIR") {
+               push (@{$self->{bin_progs}}, "$installdir/$ctx->{BINARY}");
        }
 
-       $output .= << '__EOD__';
+       push (@{$self->{binaries}}, "bin/$ctx->{BINARY}");
 
-removebackup:
-       -rm -f *.bak *~ */*.bak */*~ */*/*.bak */*/*~ */*/*/*.bak */*/*/*~
+       $self->_prepare_list($ctx, "OBJ_LIST");
+       $self->_prepare_list($ctx, "CFLAGS");
+       $self->_prepare_list($ctx, "DEPEND_LIST");
+       $self->_prepare_list($ctx, "LINK_LIST");
+       $self->_prepare_list($ctx, "LINK_FLAGS");
+
+       if ($self->{duplicate_build}) {
+       $self->output(<< "__EOD__"
+#
+bin/$ctx->{BINARY}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) 
+       \@echo Linking \$\@
+       \@\$(CC) \$(LDFLAGS) -o \$\@ \$(LOCAL_LINK_FLAGS) \$(INSTALL_LINK_FLAGS) \\
+               \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_LIST) \\
+               \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) 
 
-realdistclean: distclean removebackup
-       -rm -f include/config.h.in
-       -rm -f include/version.h
-       -rm -f configure
-       -rm -f $(MANPAGES)
 __EOD__
+);
+       }
 
-       return $output;
+$self->output(<< "__EOD__"
+$installdir/$ctx->{BINARY}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) 
+       \@echo Linking \$\@
+       \@\$(CC) \$(LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
+               \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_LIST) \\
+               \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) 
+
+__EOD__
+);
 }
 
-sub _prepare_make_target($)
+sub Manpage($$)
 {
-       my $ctx = shift;
-       my $tmpdepend;
+       my ($self,$ctx) = @_;
 
-       $tmpdepend = array2oneperline($ctx->{DEPEND_LIST});
-
-       return << "__EOD__";
-$ctx->{TARGET}: basics $tmpdepend
+       my $dir = $ctx->{BASEDIR};
+       
+       $dir =~ s/^\.\///g;
 
-__EOD__
+       push (@{$self->{manpages}}, "$dir/$ctx->{MANPAGE}");
 }
 
-sub _prepare_target_settings($)
+sub PkgConfig($$)
 {
-       my $CTX = shift;
-       my $output = "";
+       my ($self,$ctx) = @_;
+       
+       my $link_name = $ctx->{NAME};
 
-       foreach my $key (values %$CTX) {
-               if (defined($key->{OBJ_LIST})) {
-                       $output .= _prepare_obj_list($key->{TYPE}, $key);
-                       $output .= _prepare_cflags($key->{TYPE}, $key);
-               }
-       }
+       $link_name =~ s/^LIB//g;
+       $link_name = lc($link_name);
 
-       return $output;
+       return if (not defined($ctx->{DESCRIPTION}));
+
+       my $path = "$ctx->{BASEDIR}/$link_name.pc";
+
+       push (@{$self->{pc_files}}, $path);
+
+       smb_build::env::PkgConfig($self,
+               $path,
+               $link_name,
+               "-l$link_name",
+               "",
+               "$ctx->{VERSION}",
+               $ctx->{DESCRIPTION},
+               defined($ctx->{INIT_FUNCTIONS})
+       ); 
 }
 
-sub _prepare_rule_lists($)
+sub ProtoHeader($$)
 {
-       my $depend = shift;
-       my $output = "";
+       my ($self,$ctx) = @_;
 
-       foreach my $key (values %{$depend}) {
-               next unless defined $key->{OUTPUT_TYPE};
+       my $dir = $ctx->{BASEDIR};
 
-               ($output .= _prepare_mergedobj_rule($key)) if $key->{OUTPUT_TYPE} eq "MERGEDOBJ";
-               ($output .= _prepare_objlist_rule($key)) if $key->{OUTPUT_TYPE} eq "OBJLIST";
-               ($output .= _prepare_static_library_rule($key)) if $key->{OUTPUT_TYPE} eq "STATIC_LIBRARY";
-               ($output .= _prepare_shared_library_rule($key)) if $key->{OUTPUT_TYPE} eq "SHARED_LIBRARY";
-               ($output .= _prepare_binary_rule($key)) if $key->{OUTPUT_TYPE} eq "BINARY";
-               ($output .= _prepare_custom_rule($key) ) if $key->{TYPE} eq "TARGET";
-       }
+       $dir =~ s/^\.\///g;
 
-       $output .= _prepare_proto_rules();
+       my $target = "";
 
-       return $output;
+       my $comment = "Creating ";
+       if (defined($ctx->{PRIVATE_PROTO_HEADER})) {
+               $target.= "$dir/$ctx->{PRIVATE_PROTO_HEADER}";
+               $comment.= "$dir/$ctx->{PRIVATE_PROTO_HEADER}";
+               if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
+                       $comment .= " and ";
+                       $target.= " ";
+               }
+               push (@{$self->{proto_headers}}, "$dir/$ctx->{PRIVATE_PROTO_HEADER}");
+       } else {
+               $ctx->{PRIVATE_PROTO_HEADER} = $ctx->{PUBLIC_PROTO_HEADER};
+       }
+       
+       if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
+               $comment.= "$dir/$ctx->{PUBLIC_PROTO_HEADER}";
+               $target .= "$dir/$ctx->{PUBLIC_PROTO_HEADER}";
+               push (@{$self->{proto_headers}}, "$dir/$ctx->{PUBLIC_PROTO_HEADER}");
+       } else {
+               $ctx->{PUBLIC_PROTO_HEADER} = $ctx->{PRIVATE_PROTO_HEADER};
+       }       
+
+       $self->output("$dir/$ctx->{PUBLIC_PROTO_HEADER}: \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST:.o=.c)\n");
+       $self->output("\t\@echo \"$comment\"\n");
+
+       $self->output("\t\@\$(PERL) \$(srcdir)/script/mkproto.pl --private=$dir/$ctx->{PRIVATE_PROTO_HEADER} --public=$dir/$ctx->{PUBLIC_PROTO_HEADER} \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)\n\n");
 }
 
-###########################################################
-# This function prepares the output for Makefile
-#
-# $output = _prepare_makefile_in($OUTPUT)
-#
-# $OUTPUT -    the global OUTPUT context
-#
-# $output -            the resulting output buffer
-sub _prepare_makefile_in($)
+sub write($$)
 {
-       my ($CTX) = @_;
-       my $output;
-
-       $output  = "########################################\n";
-       $output .= "# Autogenerated by config.smb_build.pl #\n";
-       $output .= "########################################\n";
-       $output .= "\n";
-
-       $output .= _prepare_path_vars();
-       $output .= _prepare_compiler_linker();
-       $output .= _prepare_default_rule();
-       $output .= _prepare_SUFFIXES();
-       $output .= _prepare_dummy_MAKEDIR();
-       $output .= _prepare_std_CC_rule("c","o",$config{PICFLAG},"Compiling","Rule for std objectfiles");
-       $output .= _prepare_std_CC_rule("h","h.gch",$config{PICFLAG},"Precompiling","Rule for precompiled headerfiles");
-
-       $output .= _prepare_depend_CC_rule();
-       
-       $output .= _prepare_man_rule("1");
-       $output .= _prepare_man_rule("3");
-       $output .= _prepare_man_rule("5");
-       $output .= _prepare_man_rule("7");
-       $output .= _prepare_manpages($CTX);
-       $output .= _prepare_binaries($CTX);
-       $output .= _prepare_target_settings($CTX);
-       $output .= _prepare_rule_lists($CTX);
-
-       if ($config{developer} eq "yes") {
-               $output .= <<__EOD__
-#-include \$(_ALL_OBJS_OBJS:.o=.d)
-IDL_FILES = \$(wildcard librpc/idl/*.idl)
-\$(patsubst librpc/idl/%.idl,librpc/gen_ndr/ndr_%.c,\$(IDL_FILES)) \\
-\$(patsubst librpc/idl/%.idl,librpc/gen_ndr/ndr_\%_c.c,\$(IDL_FILES)) \\
-\$(patsubst librpc/idl/%.idl,librpc/gen_ndr/ndr_%.h,\$(IDL_FILES)): idl
-__EOD__
+       my ($self,$file) = @_;
+
+       $self->output("MANPAGES = ".array2oneperline($self->{manpages})."\n");
+       $self->output("BIN_PROGS = " . array2oneperline($self->{bin_progs}) . "\n");
+       $self->output("SBIN_PROGS = " . array2oneperline($self->{sbin_progs}) . "\n");
+       $self->output("BINARIES = " . array2oneperline($self->{binaries}) . "\n");
+       $self->output("STATIC_LIBS = " . array2oneperline($self->{static_libs}) . "\n");
+       $self->output("SHARED_LIBS = " . array2oneperline($self->{shared_libs}) . "\n");
+       $self->output("INSTALLABLE_SHARED_LIBS = " . array2oneperline($self->{installable_shared_libs}) . "\n");
+       $self->output("PUBLIC_HEADERS = " . array2oneperline($self->{headers}) . "\n");
+       $self->output("PC_FILES = " . array2oneperline($self->{pc_files}) . "\n");
+       $self->output("ALL_OBJS = " . array2oneperline($self->{all_objs}) . "\n");
+       $self->output("PROTO_HEADERS = " . array2oneperline($self->{proto_headers}) .  "\n");
+       $self->output("SHARED_MODULES = " . array2oneperline($self->{shared_modules}) . "\n");
+       $self->output("PLUGINS = " . array2oneperline($self->{plugins}) . "\n");
+
+       $self->output("\ninstallplugins: \$(PLUGINS)\n".$self->{install_plugins}."\n");
+       $self->output("\nuninstallplugins:\n".$self->{uninstall_plugins}."\n");
+
+       # nasty hack to allow running locally
+       if ($self->{duplicate_build}) {
+               $self->output("bin/libdynconfig.\$(SHLIBEXT).0.0.1: dynconfig-devel.o\n");
+               $self->output("bin/libdynconfig.\$(SHLIBEXT).0.0.1: LIBRARY_DYNCONFIG_OBJ_LIST=dynconfig-devel.o\n");
        }
 
-       return $output;
-}
-
-###########################################################
-# This function creates Makefile.in from the OUTPUT 
-# context
-#
-# create_makefile_in($OUTPUT)
-#
-# $OUTPUT      -       the global OUTPUT context
-#
-# $output -            the resulting output buffer
-sub create_makefile_in($$$)
-{
-       my ($CTX, $mk, $file) = @_;
+       $self->_prepare_mk_files();
+
+       $self->output($self->{mkfile});
+
+       if ($self->{automatic_deps}) {
+               $self->output("
+ifneq (\$(MAKECMDGOALS),clean)
+ifneq (\$(MAKECMDGOALS),distclean)
+ifneq (\$(MAKECMDGOALS),realdistclean)
+-include \$(DEP_FILES)
+endif
+endif
+endif
+");
+       } else {
+               $self->output("include static_deps.mk\n");
+       }
 
-       open(MAKEFILE_IN,">$file") || die ("Can't open $file\n");
-       print MAKEFILE_IN _prepare_makefile_in($CTX) . $mk;
-       close(MAKEFILE_IN);
+       open(MAKEFILE,">$file") || die ("Can't open $file\n");
+       print MAKEFILE $self->{output};
+       close(MAKEFILE);
 
-       print "config.smb_build.pl: creating $file\n";
-       return; 
+       print __FILE__.": creating $file\n";
 }
 
 1;