2005-12-13 19:38:12 +00:00
# Samba Build System
# - create output for Makefile
#
# Copyright (C) Stefan (metze) Metzmacher 2004
# Copyright (C) Jelmer Vernooij 2005
# Released under the GNU GPL
2004-05-13 10:20:53 +00:00
2005-10-21 21:43:39 +00:00
package smb_build::makefile ;
2007-12-04 14:20:46 +01:00
use smb_build::output ;
2007-11-21 11:10:45 +01:00
use File::Basename ;
2004-11-12 02:00:58 +00:00
use strict ;
2006-04-12 13:02:56 +00:00
use Cwd 'abs_path' ;
2005-10-21 21:43:39 +00:00
2005-10-26 23:36:04 +00:00
sub new ($$$)
2005-10-21 21:43:39 +00:00
{
2005-10-26 23:36:04 +00:00
my ( $ myname , $ config , $ mkfile ) = @ _ ;
2008-02-25 19:11:04 +01:00
my $ self = { } ;
2005-10-21 21:43:39 +00:00
bless ( $ self , $ myname ) ;
2008-02-25 19:11:04 +01:00
$ self - > _set_config ( $ config ) ;
2005-10-21 22:49:23 +00:00
$ self - > { output } = "" ;
$ self - > output ( "################################################\n" ) ;
$ self - > output ( "# Autogenerated by build/smb_build/makefile.pm #\n" ) ;
$ self - > output ( "################################################\n" ) ;
$ self - > output ( "\n" ) ;
2008-03-03 18:57:20 +01:00
$ self - > output ( $ mkfile ) ;
2005-10-21 22:49:23 +00:00
2005-10-21 21:43:39 +00:00
return $ self ;
}
2008-02-25 19:11:04 +01:00
sub _set_config ($$)
{
my ( $ self , $ config ) = @ _ ;
$ self - > { config } = $ config ;
if ( not defined ( $ self - > { config } - > { srcdir } ) ) {
$ self - > { config } - > { srcdir } = '.' ;
}
if ( not defined ( $ self - > { config } - > { builddir } ) ) {
$ self - > { config } - > { builddir } = '.' ;
}
if ( $ self - > { config } - > { prefix } eq "NONE" ) {
$ self - > { config } - > { prefix } = $ self - > { config } - > { ac_default_prefix } ;
}
if ( $ self - > { config } - > { exec_prefix } eq "NONE" ) {
$ self - > { config } - > { exec_prefix } = $ self - > { config } - > { prefix } ;
}
}
2005-10-21 22:49:23 +00:00
sub output ($$)
{
my ( $ self , $ text ) = @ _ ;
$ self - > { output } . = $ text ;
}
2005-10-26 19:07:01 +00:00
sub _prepare_mk_files ($)
2004-05-13 10:20:53 +00:00
{
2005-10-21 22:49:23 +00:00
my $ self = shift ;
2005-10-26 19:07:01 +00:00
my @ tmp = ( ) ;
2005-10-26 14:18:27 +00:00
2005-10-26 19:07:01 +00:00
foreach ( @ smb_build:: config_mk:: parsed_files ) {
s/ .*$//g ;
push ( @ tmp , $ _ ) ;
2005-10-26 14:18:27 +00:00
}
2005-10-26 19:07:01 +00:00
$ self - > output ( "MK_FILES = " . array2oneperline ( \ @ tmp ) . "\n" ) ;
2005-06-27 12:09:52 +00:00
}
2004-05-13 10:20:53 +00:00
sub array2oneperline ($)
{
my $ array = shift ;
my $ output = "" ;
2005-07-21 21:27:39 +00:00
foreach ( @$ array ) {
next unless defined ( $ _ ) ;
2004-05-13 10:20:53 +00:00
2005-07-21 21:27:39 +00:00
$ output . = " \\\n\t\t$_" ;
2004-05-13 10:20:53 +00:00
}
return $ output ;
}
2008-02-16 19:43:10 +01:00
sub _prepare_list ($$$)
2004-05-13 10:20:53 +00:00
{
2008-02-16 19:43:10 +01:00
my ( $ self , $ ctx , $ var ) = @ _ ;
2007-06-27 15:37:03 +00:00
my @ tmparr = ( ) ;
push ( @ tmparr , @ { $ ctx - > { $ var } } ) if defined ( $ ctx - > { $ var } ) ;
2004-05-13 10:20:53 +00:00
2007-06-27 15:37:03 +00:00
my $ tmplist = array2oneperline ( \ @ tmparr ) ;
2005-10-26 19:07:01 +00:00
return if ( $ tmplist eq "" ) ;
2004-05-13 10:20:53 +00:00
2008-02-14 12:03:34 +01:00
$ self - > output ( "$ctx->{NAME}_$var =$tmplist\n" ) ;
2004-05-13 10:20:53 +00:00
}
2008-02-26 16:36:24 +01:00
sub PythonModule ($$)
2008-02-13 21:03:01 +01:00
{
my ( $ self , $ ctx ) = @ _ ;
2008-02-26 16:36:24 +01:00
$ self - > _prepare_list ( $ ctx , "FULL_OBJ_LIST" ) ;
$ self - > _prepare_list ( $ ctx , "DEPEND_LIST" ) ;
$ self - > _prepare_list ( $ ctx , "LINK_FLAGS" ) ;
$ self - > output ( "\$(eval \$(call python_c_module_template,$ctx->{LIBRARY_REALNAME},\$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST), \$($ctx->{NAME}\_FULL_OBJ_LIST) \$($ctx->{NAME}_LINK_FLAGS)))\n" ) ;
2008-02-13 21:03:01 +01:00
}
2007-11-15 15:44:17 +01:00
sub SharedModule ($$)
2004-05-13 10:20:53 +00:00
{
2005-10-21 22:49:23 +00:00
my ( $ self , $ ctx ) = @ _ ;
2004-05-13 10:20:53 +00:00
2007-11-15 15:44:17 +01:00
my $ sane_subsystem = lc ( $ ctx - > { SUBSYSTEM } ) ;
$ sane_subsystem =~ s/^lib// ;
2007-10-05 19:51:29 +00:00
2008-02-26 16:36:24 +01:00
$ self - > output ( "PLUGINS += $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}\n" ) ;
2008-05-11 05:17:50 +02:00
$ self - > output ( "\$(eval \$(call shared_module_install_template,$sane_subsystem,$ctx->{LIBRARY_REALNAME}))\n" ) ;
2008-02-16 16:22:05 +01:00
2007-11-15 15:44:17 +01:00
$ self - > _prepare_list ( $ ctx , "FULL_OBJ_LIST" ) ;
2005-12-13 21:00:52 +00:00
$ self - > _prepare_list ( $ ctx , "DEPEND_LIST" ) ;
2007-06-27 17:48:30 +00:00
$ self - > _prepare_list ( $ ctx , "LINK_FLAGS" ) ;
2004-05-13 10:20:53 +00:00
2008-05-11 01:53:11 +02:00
if ( defined ( $ ctx - > { INIT_FUNCTION } ) and $ ctx - > { INIT_FUNCTION_TYPE } =~ /\(\*\)/ and not ( $ ctx - > { INIT_FUNCTION } =~ /\(/ ) ) {
2008-12-22 21:03:09 +01:00
$ self - > output ( "\$($ctx->{NAME}_OBJ_FILES): CFLAGS+=-D$ctx->{INIT_FUNCTION}=samba_init_module\n" ) ;
2006-03-09 14:39:36 +00:00
}
2008-02-26 17:56:07 +01:00
$ self - > output ( "\$(eval \$(call shared_module_template,$ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}, \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST), \$($ctx->{NAME}\_FULL_OBJ_LIST) \$($ctx->{NAME}_LINK_FLAGS)))\n" ) ;
2007-11-15 15:44:17 +01:00
2008-03-07 23:32:16 +01:00
2007-11-15 15:44:17 +01:00
if ( defined ( $ ctx - > { ALIASES } ) ) {
2008-03-09 00:49:39 +01:00
$ self - > output ( "\$(eval \$(foreach alias," . join ( ' ' , @ { $ ctx - > { ALIASES } } ) . ",\$(call shared_module_alias_template,$ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME},$sane_subsystem,\$(alias))))\n" ) ;
2007-11-15 15:44:17 +01:00
}
}
2008-04-14 17:54:19 +02:00
sub StaticLibraryPrimitives ($$)
{
my ( $ self , $ ctx ) = @ _ ;
$ self - > output ( "$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n" ) ;
$ self - > _prepare_list ( $ ctx , "FULL_OBJ_LIST" ) ;
}
2008-02-13 21:03:01 +01:00
sub SharedLibraryPrimitives ($$)
2007-11-15 15:44:17 +01:00
{
my ( $ self , $ ctx ) = @ _ ;
2008-02-13 21:03:01 +01:00
if ( not grep ( /STATIC_LIBRARY/ , @ { $ ctx - > { OUTPUT_TYPE } } ) ) {
2008-02-14 12:03:34 +01:00
$ self - > output ( "$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n" ) ;
2007-11-15 15:44:17 +01:00
$ self - > _prepare_list ( $ ctx , "FULL_OBJ_LIST" ) ;
}
2008-02-13 21:03:01 +01:00
}
sub SharedLibrary ($$)
{
my ( $ self , $ ctx ) = @ _ ;
2008-03-09 00:49:39 +01:00
$ self - > output ( "SHARED_LIBS += $ctx->{RESULT_SHARED_LIBRARY}\n" ) ;
2008-02-13 21:03:01 +01:00
2007-11-15 15:44:17 +01:00
$ self - > _prepare_list ( $ ctx , "DEPEND_LIST" ) ;
$ self - > _prepare_list ( $ ctx , "LINK_FLAGS" ) ;
2008-03-09 00:49:39 +01:00
$ self - > output ( "\$(eval \$(call shared_library_template,$ctx->{RESULT_SHARED_LIBRARY}, \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST), \$($ctx->{NAME}\_FULL_OBJ_LIST) \$($ctx->{NAME}_LINK_FLAGS),$ctx->{SHAREDDIR}/$ctx->{LIBRARY_SONAME},$ctx->{SHAREDDIR}/$ctx->{LIBRARY_DEBUGNAME}))\n" ) ;
2004-05-13 10:20:53 +00:00
}
2008-02-16 20:38:21 +01:00
sub MergedObj ($$)
{
my ( $ self , $ ctx ) = @ _ ;
2008-03-09 01:42:42 +01:00
$ self - > output ( "\$(call partial_link_template, $ctx->{OUTPUT}, \$($ctx->{NAME}_OBJ_FILES))\n" ) ;
2008-02-16 20:38:21 +01:00
}
2008-02-16 18:38:02 +01:00
sub InitFunctions ($$)
{
my ( $ self , $ ctx ) = @ _ ;
2008-03-03 18:25:28 +01:00
$ self - > output ( "\$($ctx->{NAME}_OBJ_FILES): CFLAGS+=-DSTATIC_$ctx->{NAME}_MODULES=\"\$($ctx->{NAME}_INIT_FUNCTIONS)$ctx->{INIT_FUNCTION_SENTINEL}\"\n" ) ;
2008-02-16 18:38:02 +01:00
}
2008-02-16 17:34:50 +01:00
sub StaticLibrary ($$)
{
my ( $ self , $ ctx ) = @ _ ;
2004-05-13 16:15:46 +00:00
2008-02-18 22:50:44 +01:00
$ self - > output ( "STATIC_LIBS += $ctx->{RESULT_STATIC_LIBRARY}\n" ) if ( $ ctx - > { TYPE } eq "LIBRARY" ) ;
2008-02-16 17:34:50 +01:00
$ self - > output ( "$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n" ) ;
2008-02-18 22:50:44 +01:00
$ self - > output ( "$ctx->{RESULT_STATIC_LIBRARY}: \$($ctx->{NAME}_FULL_OBJ_LIST)\n" ) ;
2004-05-13 10:20:53 +00:00
}
2005-10-26 19:07:01 +00:00
sub Binary ($$)
2004-05-13 10:20:53 +00:00
{
2005-10-21 22:49:23 +00:00
my ( $ self , $ ctx ) = @ _ ;
2004-05-13 10:20:53 +00:00
2005-10-26 19:07:01 +00:00
unless ( defined ( $ ctx - > { INSTALLDIR } ) ) {
} elsif ( $ ctx - > { INSTALLDIR } eq "SBINDIR" ) {
2008-06-26 11:39:59 +02:00
$ self - > output ( "\$(eval \$(call sbinary_install_template,$ctx->{RESULT_BINARY}))\n" ) ;
2005-10-26 19:07:01 +00:00
} elsif ( $ ctx - > { INSTALLDIR } eq "BINDIR" ) {
2008-06-26 11:39:59 +02:00
$ self - > output ( "\$(eval \$(call binary_install_template,$ctx->{RESULT_BINARY}))\n" ) ;
2005-10-26 19:07:01 +00:00
}
2006-04-27 19:50:13 +00:00
$ self - > _prepare_list ( $ ctx , "FULL_OBJ_LIST" ) ;
2005-12-13 21:00:52 +00:00
$ self - > _prepare_list ( $ ctx , "DEPEND_LIST" ) ;
$ self - > _prepare_list ( $ ctx , "LINK_FLAGS" ) ;
2004-05-13 10:20:53 +00:00
2007-04-02 18:14:27 +00:00
if ( defined ( $ ctx - > { USE_HOSTCC } ) && $ ctx - > { USE_HOSTCC } eq "YES" ) {
2008-06-26 11:39:59 +02:00
$ self - > output ( "\$(eval \$(call host_binary_link_template, $ctx->{RESULT_BINARY}, \$($ctx->{NAME}_FULL_OBJ_LIST) \$($ctx->{NAME}_DEPEND_LIST), \$($ctx->{NAME}_LINK_FLAGS)))\n" ) ;
2007-04-02 17:23:23 +00:00
} else {
2008-06-26 11:39:59 +02:00
$ self - > output ( "\$(eval \$(call binary_link_template, $ctx->{RESULT_BINARY}, \$($ctx->{NAME}_FULL_OBJ_LIST) \$($ctx->{NAME}_DEPEND_LIST), \$($ctx->{NAME}_LINK_FLAGS)))\n" ) ;
2007-04-02 17:23:23 +00:00
}
2004-05-13 10:20:53 +00:00
}
2005-10-26 19:07:01 +00:00
sub write ($$)
2004-05-13 10:20:53 +00:00
{
2008-02-13 21:03:01 +01:00
my ( $ self , $ file ) = @ _ ;
2004-05-13 10:20:53 +00:00
2005-10-26 23:36:04 +00:00
$ self - > _prepare_mk_files ( ) ;
2008-05-16 14:40:56 +02:00
$ self - > output ( "ALL_OBJS = " . array2oneperline ( $ self - > { all_objs } ) . "\n" ) ;
2005-09-17 13:08:49 +00:00
open ( MAKEFILE , ">$file" ) || die ( "Can't open $file\n" ) ;
2005-10-21 21:43:39 +00:00
print MAKEFILE $ self - > { output } ;
2005-09-17 13:08:49 +00:00
close ( MAKEFILE ) ;
2004-05-13 10:20:53 +00:00
2005-10-26 20:06:26 +00:00
print __FILE__ . ": creating $file\n" ;
2004-05-13 10:20:53 +00:00
}
2004-11-12 01:40:02 +00:00
2008-02-25 18:29:04 +01:00
my $ sort_available = eval "use sort 'stable'; return 1;" ;
$ sort_available = 0 unless defined ( $ sort_available ) ;
sub by_path {
return 1 if ( $ a =~ m #^\-I/#);
return - 1 if ( $ b =~ m #^\-I/#);
return 0 ;
}
sub CFlags ($$)
{
my ( $ self , $ key ) = @ _ ;
my $ srcdir = $ self - > { config } - > { srcdir } ;
my $ builddir = $ self - > { config } - > { builddir } ;
my $ src_ne_build = ( $ srcdir ne $ builddir ) ? 1 : 0 ;
return unless defined ( $ key - > { FINAL_CFLAGS } ) ;
return unless ( @ { $ key - > { FINAL_CFLAGS } } > 0 ) ;
my @ sorted_cflags = @ { $ key - > { FINAL_CFLAGS } } ;
if ( $ sort_available ) {
@ sorted_cflags = sort by_path @ { $ key - > { FINAL_CFLAGS } } ;
}
# Rewrite CFLAGS so that both the source and the build
# directories are in the path.
my @ cflags = ( ) ;
foreach my $ flag ( @ sorted_cflags ) {
if ( $ src_ne_build ) {
2008-02-26 00:40:40 +01:00
if ( $ flag =~ m #^-I([^/].*$)#) {
my $ dir = $ 1 ;
2008-05-27 17:16:57 +02:00
if ( $ dir =~ /^\$\(/ ) {
push ( @ cflags , $ flag ) ;
next ;
}
2008-02-26 00:40:40 +01:00
$ dir =~ s #^\$\((?:src|build)dir\)/?##;
2008-02-25 18:29:04 +01:00
push ( @ cflags , "-I$builddir/$dir" , "-I$srcdir/$dir" ) ;
2008-02-26 00:40:40 +01:00
next ;
}
2008-02-25 18:29:04 +01:00
}
push ( @ cflags , $ flag ) ;
}
my $ cflags = join ( ' ' , @ cflags ) ;
2008-03-03 23:22:22 +01:00
$ self - > output ( "\$(patsubst %.ho,%.d,\$($key->{NAME}_OBJ_FILES:.o=.d)) \$($key->{NAME}_OBJ_FILES): CFLAGS+= $cflags\n" ) ;
2008-02-25 18:29:04 +01:00
}
2004-11-12 01:40:02 +00:00
1 ;