# Samba Build System # - create output for Makefile # # Copyright (C) Stefan (metze) Metzmacher 2004 # Copyright (C) Jelmer Vernooij 2005 # Released under the GNU GPL package smb_build::makefile; use smb_build::env; use smb_build::output; use File::Basename; use strict; use base 'smb_build::env'; use Cwd 'abs_path'; sub new($$$) { my ($myname, $config, $mkfile) = @_; my $self = new smb_build::env($config); bless($self, $myname); $self->{output} = ""; $self->{mkfile} = $mkfile; $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"); $self->output(".NOTPARALLEL:\n"); } return $self; } sub output($$) { my ($self, $text) = @_; $self->{output} .= $text; } sub _prepare_mk_files($) { my $self = shift; my @tmp = (); foreach (@smb_build::config_mk::parsed_files) { s/ .*$//g; push (@tmp, $_); } if ($self->{gnu_make}) { $self->output(" ifneq (\$(MAKECMDGOALS),clean) ifneq (\$(MAKECMDGOALS),distclean) ifneq (\$(MAKECMDGOALS),realdistclean) "); } $self->output("MK_FILES = " . array2oneperline(\@tmp) . "\n"); if ($self->{gnu_make}) { $self->output(" endif endif endif "); } } sub array2oneperline($) { my $array = shift; my $output = ""; foreach (@$array) { next unless defined($_); $output .= " \\\n\t\t$_"; } return $output; } sub _prepare_list_ex($$$$$) { my ($self,$ctx,$var,$pre,$post) = @_; my @tmparr = (); push(@tmparr, $pre) if defined($pre); push(@tmparr, @{$ctx->{$var}}) if defined($ctx->{$var}); push(@tmparr, $post) if defined($post); my $tmplist = array2oneperline(\@tmparr); return if ($tmplist eq ""); $self->output("$ctx->{NAME}_$var =$tmplist\n"); } sub _prepare_list($$$) { my ($self,$ctx,$var) = @_; $self->_prepare_list_ex($ctx, $var, undef, undef); } sub Integrated($$) { my ($self,$ctx) = @_; $self->_prepare_list($ctx, "OBJ_LIST"); $self->output("$ctx->{SUBSYSTEM}_OBJ_LIST += \$($ctx->{NAME}_OBJ_LIST)\n"); } sub SharedModulePrimitives($$) { my ($self,$ctx) = @_; #FIXME } sub SharedModule($$) { my ($self,$ctx) = @_; my $sane_subsystem = lc($ctx->{SUBSYSTEM}); $sane_subsystem =~ s/^lib//; if ($ctx->{TYPE} eq "PYTHON") { $self->output("PYTHON_DSOS += $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}\n"); } else { $self->output("PLUGINS += $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}\n"); $self->output("installplugins:: $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}\n"); $self->output("\t\@echo Installing $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME} as \$(DESTDIR)\$(modulesdir)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n"); $self->output("\t\@mkdir -p \$(DESTDIR)\$(modulesdir)/$sane_subsystem/\n"); $self->output("\t\@cp $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME} \$(DESTDIR)\$(modulesdir)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n"); if (defined($ctx->{ALIASES})) { foreach (@{$ctx->{ALIASES}}) { $self->output("\t\@rm -f \$(DESTDIR)\$(modulesdir)/$sane_subsystem/$_.\$(SHLIBEXT)\n"); $self->output("\t\@ln -fs $ctx->{LIBRARY_REALNAME} \$(DESTDIR)\$(modulesdir)/$sane_subsystem/$_.\$(SHLIBEXT)\n"); } } $self->output("uninstallplugins::\n"); $self->output("\t\@echo Uninstalling \$(DESTDIR)\$(modulesdir)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n"); $self->output("\t\@-rm \$(DESTDIR)\$(modulesdir)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n"); if (defined($ctx->{ALIASES})) { foreach (@{$ctx->{ALIASES}}) { $self->output("\t\@-rm \$(DESTDIR)\$(modulesdir)/$sane_subsystem/$_.\$(SHLIBEXT)\n"); } } } $self->output("$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n"); $self->_prepare_list($ctx, "OBJ_LIST"); $self->_prepare_list($ctx, "DEPEND_LIST"); $self->_prepare_list($ctx, "LINK_FLAGS"); if (defined($ctx->{INIT_FUNCTION}) and $ctx->{TYPE} ne "PYTHON") { 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("\$($ctx->{NAME}_OBJ_LIST): CFLAGS+=-D$ctx->{INIT_FUNCTION}=init_module\n"); } $self->output(<< "__EOD__" # $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}: \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_OBJ_LIST) \@echo Linking \$\@ \@mkdir -p $ctx->{SHAREDDIR} \@\$(MDLD) \$(MDLD_FLAGS) \$(INTERN_LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\ \$($ctx->{NAME}\_OBJ_LIST) \\ \$($ctx->{NAME}_LINK_FLAGS) __EOD__ ); if (defined($ctx->{ALIASES})) { foreach (@{$ctx->{ALIASES}}) { $self->output("\t\@rm -f $ctx->{SHAREDDIR}/$_.\$(SHLIBEXT)\n"); $self->output("\t\@ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{SHAREDDIR}/$_.\$(SHLIBEXT)\n"); } } $self->output("\n"); } sub SharedLibraryPrimitives($$) { my ($self,$ctx) = @_; $self->output("$ctx->{NAME}_SOVERSION = $ctx->{SO_VERSION}\n") if (defined($ctx->{SO_VERSION})); $self->output("$ctx->{NAME}_VERSION = $ctx->{VERSION}\n") if (defined($ctx->{VERSION})); if (not grep(/STATIC_LIBRARY/, @{$ctx->{OUTPUT_TYPE}})) { $self->output("$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n"); $self->_prepare_list($ctx, "OBJ_LIST"); } } sub SharedLibrary($$) { my ($self,$ctx) = @_; $self->output("SHARED_LIBS += $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}\n") if (defined($ctx->{SO_VERSION})); $self->_prepare_list($ctx, "DEPEND_LIST"); $self->_prepare_list($ctx, "LINK_FLAGS"); my $soarg = ""; my $lns = ""; 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}) { $lns .= "\n\t\@test \$($ctx->{NAME}_VERSION) = \$($ctx->{NAME}_SOVERSION) || ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{SHAREDDIR}/$ctx->{LIBRARY_SONAME}"; } } if (defined($ctx->{LIBRARY_SONAME})) { $lns .= "\n\t\@ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{SHAREDDIR}/$ctx->{LIBRARY_DEBUGNAME}"; } $self->output(<< "__EOD__" # $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}: \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_OBJ_LIST) \@echo Linking \$\@ \@mkdir -p $ctx->{SHAREDDIR} \@\$(SHLD) \$(SHLD_FLAGS) \$(INTERN_LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\ \$($ctx->{NAME}\_OBJ_LIST) \\ \$($ctx->{NAME}_LINK_FLAGS) \\ $soarg$lns __EOD__ ); $self->output("\n"); } sub StaticLibraryPrimitives($$) { my ($self,$ctx) = @_; return unless (defined($ctx->{OBJ_FILES})); $self->_prepare_list($ctx, "OBJ_LIST"); } sub StaticLibrary($$) { my ($self,$ctx) = @_; return unless (defined($ctx->{OBJ_FILES})); $self->output("STATIC_LIBS += $ctx->{TARGET_STATIC_LIBRARY}\n") if ($ctx->{TYPE} eq "LIBRARY"); $self->output("$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n"); $self->output("$ctx->{TARGET_STATIC_LIBRARY}: \$($ctx->{NAME}_OBJ_LIST)\n"); } sub Header($$) { my ($self,$ctx) = @_; return if ($#{$ctx->{PUBLIC_HEADERS}} == -1); $self->output("PUBLIC_HEADERS +="); foreach (@{$ctx->{PUBLIC_HEADERS}}) { $self->output(" " . output::add_dir_str($ctx->{BASEDIR}, $_)); } $self->output("\n"); } sub Binary($$) { my ($self,$ctx) = @_; unless (defined($ctx->{INSTALLDIR})) { } elsif ($ctx->{INSTALLDIR} eq "SBINDIR") { $self->output("SBIN_PROGS += bin/$ctx->{BINARY}\n"); } elsif ($ctx->{INSTALLDIR} eq "BINDIR") { $self->output("BIN_PROGS += bin/$ctx->{BINARY}\n"); } $self->output("binaries:: bin/$ctx->{BINARY}\n"); $self->_prepare_list($ctx, "OBJ_LIST"); $self->_prepare_list($ctx, "DEPEND_LIST"); $self->_prepare_list($ctx, "LINK_FLAGS"); $self->output(<< "__EOD__" bin/$ctx->{BINARY}: \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_OBJ_LIST) \@echo Linking \$\@ __EOD__ ); if (defined($ctx->{USE_HOSTCC}) && $ctx->{USE_HOSTCC} eq "YES") { $self->output(<< "__EOD__" \@\$(HOSTLD) \$(HOSTLD_FLAGS) -L\${builddir}/bin/static -o \$\@ \$(INSTALL_LINK_FLAGS) \\ \$\($ctx->{NAME}_LINK_FLAGS) __EOD__ ); } else { $self->output(<< "__EOD__" \@\$(BNLD) \$(BNLD_FLAGS) \$(INTERN_LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\ \$\($ctx->{NAME}_LINK_FLAGS) __EOD__ ); } } sub PythonFiles($$) { my ($self,$ctx) = @_; foreach (@{$ctx->{PYTHON_FILES}}) { my $target = "bin/python/".basename($_); my $source = output::add_dir_str($ctx->{BASEDIR}, $_); $self->output("$target: $source\n\n"); $self->output("PYTHON_PYS += $target\n"); } } sub Manpage($$) { my ($self,$ctx) = @_; $self->output("MANPAGES += " . output::add_dir_str($ctx->{BASEDIR}, $ctx->{MANPAGE}) . "\n"); } sub ProtoHeader($$) { my ($self,$ctx) = @_; my $target = ""; my $comment = "Creating "; my $priv = undef; my $pub = undef; if (defined($ctx->{PRIVATE_PROTO_HEADER})) { $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER}); $target .= $priv; $comment .= $priv; if (defined($ctx->{PUBLIC_PROTO_HEADER})) { $comment .= " and "; $target.= " "; } $self->output("PROTO_HEADERS += $priv\n"); } else { $ctx->{PRIVATE_PROTO_HEADER} = $ctx->{PUBLIC_PROTO_HEADER}; $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER}); } if (defined($ctx->{PUBLIC_PROTO_HEADER})) { $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER}); $comment .= $pub; $target .= $pub; $self->output("PROTO_HEADERS += $pub\n"); } else { $ctx->{PUBLIC_PROTO_HEADER} = $ctx->{PRIVATE_PROTO_HEADER}; $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER}); } $self->output("$pub: $ctx->{MK_FILE} \$($ctx->{NAME}_OBJ_LIST:.o=.c) \$(srcdir)/script/mkproto.pl\n"); $self->output("\t\@echo \"$comment\"\n"); $self->output("\t\@\$(PERL) \$(srcdir)/script/mkproto.pl --srcdir=\$(srcdir) --builddir=\$(builddir) --private=$priv --public=$pub \$($ctx->{NAME}_OBJ_LIST)\n\n"); } sub write($$) { my ($self, $file) = @_; $self->_prepare_mk_files(); $self->output($self->{mkfile}); if ($self->{automatic_deps}) { $self->output(" ifneq (\$(MAKECMDGOALS),clean) ifneq (\$(MAKECMDGOALS),distclean) ifneq (\$(MAKECMDGOALS),realdistclean) ifneq (\$(SKIP_DEP_FILES),yes) -include \$(DEP_FILES) endif endif endif endif ifneq (\$(SKIP_DEP_FILES),yes) clean:: \@echo Removing dependency files \@find . -name '*.d' -o -name '*.hd' | xargs rm -f endif "); } else { $self->output("include \$(srcdir)/static_deps.mk\n"); } open(MAKEFILE,">$file") || die ("Can't open $file\n"); print MAKEFILE $self->{output}; close(MAKEFILE); print __FILE__.": creating $file\n"; } 1;