r19507: Merge my DSO fixes branch. Building Samba's libraries as shared libraries
[Samba/aatanasov.git] / source / build / smb_build / makefile.pm
blob259256b9b5c2e55ef69b8e52ce2166a90228a910
1 # Samba Build System
2 # - create output for Makefile
4 # Copyright (C) Stefan (metze) Metzmacher 2004
5 # Copyright (C) Jelmer Vernooij 2005
6 # Released under the GNU GPL
8 package smb_build::makefile;
9 use smb_build::env;
10 use strict;
12 use base 'smb_build::env';
13 use Cwd 'abs_path';
15 sub new($$$)
17 my ($myname, $config, $mkfile) = @_;
18 my $self = new smb_build::env($config);
20 bless($self, $myname);
22 $self->{manpages} = [];
23 $self->{sbin_progs} = [];
24 $self->{bin_progs} = [];
25 $self->{torture_progs} = [];
26 $self->{static_libs} = [];
27 $self->{shared_libs} = [];
28 $self->{installable_shared_libs} = [];
29 $self->{headers} = [];
30 $self->{shared_modules} = [];
31 $self->{plugins} = [];
32 $self->{install_plugins} = "";
33 $self->{uninstall_plugins} = "";
34 $self->{pc_files} = [];
35 $self->{proto_headers} = [];
36 $self->{output} = "";
38 $self->{mkfile} = $mkfile;
40 $self->output("#!gmake\n");
41 $self->output("################################################\n");
42 $self->output("# Autogenerated by build/smb_build/makefile.pm #\n");
43 $self->output("################################################\n");
44 $self->output("\n");
46 $self->output("default: all\n\n");
48 $self->_prepare_path_vars();
49 $self->_prepare_compiler_linker();
51 if (!$self->{automatic_deps}) {
52 $self->output("ALL_PREDEP = proto\n");
53 $self->output(".NOTPARALLEL:\n");
56 return $self;
59 sub output($$)
61 my ($self, $text) = @_;
63 $self->{output} .= $text;
66 sub _prepare_path_vars($)
68 my ($self) = @_;
70 $self->output(<< "__EOD__"
71 prefix = $self->{config}->{prefix}
72 exec_prefix = $self->{config}->{exec_prefix}
73 selftest_prefix = $self->{config}->{selftest_prefix}
74 VPATH = $self->{config}->{srcdir}:heimdal_build:heimdal/lib/asn1:heimdal/lib/krb5:heimdal/lib/gssapi:heimdal/lib/hdb:heimdal/lib/roken:heimdal/lib/des
75 srcdir = $self->{config}->{srcdir}
76 builddir = $self->{config}->{builddir}
77 datarootdir = $self->{config}->{datarootdir}
79 BASEDIR = $self->{config}->{prefix}
80 BINDIR = $self->{config}->{bindir}
81 SBINDIR = $self->{config}->{sbindir}
82 LIBDIR = $self->{config}->{libdir}
83 TORTUREDIR = $self->{config}->{libdir}/torture
84 MODULESDIR = $self->{config}->{modulesdir}
85 INCLUDEDIR = $self->{config}->{includedir}
86 CONFIGDIR = $self->{config}->{sysconfdir}
87 DATADIR = $self->{config}->{datadir}
88 SWATDIR = $self->{config}->{datadir}/swat
89 SERVICESDIR = $self->{config}->{datadir}/services
90 JSDIR = $self->{config}->{datadir}/js
91 SETUPDIR = $self->{config}->{datadir}/setup
92 VARDIR = $self->{config}->{localstatedir}
93 LOGFILEBASE = $self->{config}->{logfilebase}
94 NCALRPCDIR = $self->{config}->{localstatedir}/ncalrpc
95 LOCKDIR = $self->{config}->{lockdir}
96 PIDDIR = $self->{config}->{piddir}
97 MANDIR = $self->{config}->{mandir}
98 PRIVATEDIR = $self->{config}->{privatedir}
99 WINBINDD_SOCKET_DIR = $self->{config}->{winbindd_socket_dir}
101 __EOD__
105 sub _prepare_compiler_linker($)
107 my ($self) = @_;
109 my $devld_install = "";
110 my $builddir_headers = "";
112 if ($self->{config}->{LIBRARY_OUTPUT_TYPE} eq "SHARED_LIBRARY") {
113 $devld_install = " -Wl,-rpath-link,\$(builddir)/bin";
116 if (!(abs_path($self->{config}->{srcdir}) eq abs_path($self->{config}->{builddir}))) {
117 $builddir_headers= "-I\$(builddir)/include -I\$(builddir) -I\$(builddir)/lib ";
120 $self->output(<< "__EOD__"
121 SHELL=$self->{config}->{SHELL}
123 PERL=$self->{config}->{PERL}
125 CPP=$self->{config}->{CPP}
126 CPPFLAGS=$builddir_headers-I\$(srcdir)/include -I\$(srcdir) -I\$(srcdir)/lib -I\$(srcdir)/lib/replace -D_SAMBA_BUILD_=4 -DHAVE_CONFIG_H $self->{config}->{CPPFLAGS}
128 CC=$self->{config}->{CC}
129 CFLAGS=$self->{config}->{CFLAGS} \$(CPPFLAGS)
130 PICFLAG=$self->{config}->{PICFLAG}
131 HOSTCC=$self->{config}->{HOSTCC}
133 INSTALL_LINK_FLAGS=$devld_install
135 LD=$self->{config}->{LD}
136 LDFLAGS=$self->{config}->{LDFLAGS} -L\$(builddir)/bin
138 STLD=$self->{config}->{STLD}
139 STLD_FLAGS=$self->{config}->{STLD_FLAGS}
141 SHLD=$self->{config}->{SHLD}
142 SHLD_FLAGS=$self->{config}->{SHLD_FLAGS} -L\$(builddir)/bin
143 SHLIBEXT=$self->{config}->{SHLIBEXT}
145 XSLTPROC=$self->{config}->{XSLTPROC}
147 LEX=$self->{config}->{LEX}
148 YACC=$self->{config}->{YACC}
149 YAPP=$self->{config}->{YAPP}
150 PIDL_ARGS=$self->{config}->{PIDL_ARGS}
152 GCOV=$self->{config}->{GCOV}
154 DEFAULT_TEST_TARGET=$self->{config}->{DEFAULT_TEST_TARGET}
156 __EOD__
160 sub _prepare_mk_files($)
162 my $self = shift;
163 my @tmp = ();
165 foreach (@smb_build::config_mk::parsed_files) {
166 s/ .*$//g;
167 push (@tmp, $_);
170 $self->output("MK_FILES = " . array2oneperline(\@tmp) . "\n");
173 sub array2oneperline($)
175 my $array = shift;
176 my $output = "";
178 foreach (@$array) {
179 next unless defined($_);
181 $output .= " \\\n\t\t$_";
184 return $output;
187 sub _prepare_list($$$)
189 my ($self,$ctx,$var) = @_;
191 my $tmplist = array2oneperline($ctx->{$var});
192 return if ($tmplist eq "");
194 $self->output("$ctx->{TYPE}\_$ctx->{NAME}_$var =$tmplist\n");
197 sub Integrated($$)
199 my ($self,$ctx) = @_;
201 $self->_prepare_list($ctx, "OBJ_LIST");
202 $self->_prepare_list($ctx, "FULL_OBJ_LIST");
203 $self->_prepare_list($ctx, "LINK_FLAGS");
206 sub SharedLibrary($$)
208 my ($self,$ctx) = @_;
210 my $installdir;
211 my $init_obj = "";
213 $installdir = $ctx->{DEBUGDIR};
215 if ($ctx->{TYPE} eq "LIBRARY") {
216 push (@{$self->{shared_libs}}, "$ctx->{DEBUGDIR}/$ctx->{LIBRARY_REALNAME}") if (defined($ctx->{SO_VERSION}));
217 push (@{$self->{installable_shared_libs}}, "$installdir/$ctx->{LIBRARY_REALNAME}") if (defined($ctx->{SO_VERSION}));
218 } elsif ($ctx->{TYPE} eq "MODULE") {
219 push (@{$self->{shared_modules}}, "$ctx->{TARGET}");
220 push (@{$self->{plugins}}, "$installdir/$ctx->{LIBRARY_REALNAME}");
222 $self->{install_plugins} .= "\t\@echo Installing $installdir/$ctx->{LIBRARY_REALNAME} as \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$ctx->{LIBRARY_REALNAME}\n";
223 $self->{install_plugins} .= "\t\@mkdir -p \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/\n";
224 $self->{install_plugins} .= "\t\@cp $installdir/$ctx->{LIBRARY_REALNAME} \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$ctx->{LIBRARY_REALNAME}\n";
225 $self->{uninstall_plugins} .= "\t\@echo Uninstalling \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$ctx->{LIBRARY_REALNAME}\n";
226 $self->{uninstall_plugins} .= "\t\@-rm \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$ctx->{LIBRARY_REALNAME}\n";
227 if (defined($ctx->{ALIASES})) {
228 foreach (@{$ctx->{ALIASES}}) {
229 $self->{install_plugins} .= "\t\@ln -fs $ctx->{LIBRARY_REALNAME} \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$_.\$(SHLIBEXT)\n";
230 $self->{uninstall_plugins} .= "\t\@-rm \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$_.\$(SHLIBEXT)\n";
235 $self->output("$ctx->{TYPE}_$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
236 $self->_prepare_list($ctx, "OBJ_LIST");
237 $self->_prepare_list($ctx, "FULL_OBJ_LIST");
238 $self->_prepare_list($ctx, "DEPEND_LIST");
239 $self->_prepare_list($ctx, "LINK_FLAGS");
241 push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)");
243 if ($ctx->{TYPE} eq "MODULE" and defined($ctx->{INIT_FUNCTION})) {
244 my $init_fn = $ctx->{INIT_FUNCTION_TYPE};
245 $init_fn =~ s/\(\*\)/init_module/;
246 my $proto_fn = $ctx->{INIT_FUNCTION_TYPE};
247 $proto_fn =~ s/\(\*\)/$ctx->{INIT_FUNCTION}/;
249 $self->output(<< "__EOD__"
250 bin/$ctx->{NAME}_init_module.c:
251 \@echo Creating \$\@
252 \@echo \"#include \\\"includes.h\\\"\" > \$\@
253 \@echo \"$proto_fn;\" >> \$\@
254 \@echo -e \"_PUBLIC_ $init_fn \\n{\\n\\treturn $ctx->{INIT_FUNCTION}();\\n}\\n\" >> \$\@
255 __EOD__
257 $init_obj = "bin/$ctx->{NAME}_init_module.o";
260 my $soarg = "";
261 my $soargdebug = "";
262 if ($self->{config}->{SONAMEFLAG} ne "" and
263 defined($ctx->{LIBRARY_SONAME})) {
264 $soarg = "$self->{config}->{SONAMEFLAG}$ctx->{LIBRARY_SONAME} ";
265 if ($ctx->{LIBRARY_REALNAME} ne $ctx->{LIBRARY_SONAME}) {
266 $soargdebug = "\n\t\@ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{DEBUGDIR}/$ctx->{LIBRARY_SONAME}";
270 my $singlesoarg = "";
272 if ($ctx->{DEBUGDIR} ne $installdir) {
273 $self->output(<< "__EOD__"
276 $ctx->{TARGET}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST) $init_obj
277 \@echo Linking \$\@
278 \@mkdir -p $ctx->{DEBUGDIR}
279 \@\$(SHLD) \$(SHLD_FLAGS) -o \$\@ \$(LOCAL_LINK_FLAGS) \\
280 \$($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) $soarg \\
281 $init_obj $soargdebug
282 __EOD__
284 if (defined($ctx->{ALIASES})) {
285 foreach (@{$ctx->{ALIASES}}) {
286 $self->output("\t\@ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{DEBUGDIR}/$_.\$(SHLIBEXT)\n");
290 $self->output("\n");
291 } else {
292 if ($self->{config}->{SONAMEFLAG} ne "" and
293 defined($ctx->{LIBRARY_SONAME}) and
294 $ctx->{LIBRARY_REALNAME} ne $ctx->{LIBRARY_SONAME}) {
295 $singlesoarg = "\n\t\@ln -fs $ctx->{LIBRARY_REALNAME} $installdir/$ctx->{LIBRARY_SONAME}";
300 $self->output(<< "__EOD__"
303 $installdir/$ctx->{LIBRARY_REALNAME}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST) $init_obj
304 \@echo Linking \$\@
305 \@mkdir -p $installdir
306 \@\$(SHLD) \$(SHLD_FLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
307 \$($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) $soarg \\
308 $init_obj $singlesoarg
310 __EOD__
314 sub StaticLibrary($$)
316 my ($self,$ctx) = @_;
318 return unless (defined($ctx->{OBJ_FILES}));
320 push (@{$self->{static_libs}}, $ctx->{TARGET}) if ($ctx->{TYPE} eq "LIBRARY");
322 $self->output("$ctx->{TYPE}_$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
323 $self->_prepare_list($ctx, "OBJ_LIST");
324 $self->_prepare_list($ctx, "FULL_OBJ_LIST");
326 $self->_prepare_list($ctx, "LINK_FLAGS");
328 push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)");
330 $self->output(<< "__EOD__"
332 $ctx->{TARGET}: \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)
333 \@echo Linking \$@
334 \@rm -f \$@
335 \@\$(STLD) \$(STLD_FLAGS) \$@ \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)
337 __EOD__
341 sub Header($$)
343 my ($self,$ctx) = @_;
345 my $dir = $ctx->{BASEDIR};
347 $dir =~ s/^\.\///g;
349 foreach (@{$ctx->{PUBLIC_HEADERS}}) {
350 push (@{$self->{headers}}, "$dir/$_");
354 sub Binary($$)
356 my ($self,$ctx) = @_;
358 my $installdir;
359 my $extradir = "";
361 if (defined($ctx->{INSTALLDIR}) && $ctx->{INSTALLDIR} =~ /^TORTUREDIR/) {
362 $extradir = "/torture" . substr($ctx->{INSTALLDIR}, length("TORTUREDIR"));
364 my $localdir = "bin$extradir";
366 my $dynconfig = "dynconfig.o";
367 my $dynconfig_install = "dynconfig.o";
369 $installdir = "bin$extradir";
371 push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)");
373 unless (defined($ctx->{INSTALLDIR})) {
374 } elsif ($ctx->{INSTALLDIR} eq "SBINDIR") {
375 push (@{$self->{sbin_progs}}, "$installdir/$ctx->{BINARY}");
376 } elsif ($ctx->{INSTALLDIR} eq "BINDIR") {
377 push (@{$self->{bin_progs}}, "$installdir/$ctx->{BINARY}");
378 } elsif ($ctx->{INSTALLDIR} =~ /^TORTUREDIR/) {
379 push (@{$self->{torture_progs}}, "$installdir/$ctx->{BINARY}");
383 push (@{$self->{binaries}}, "$localdir/$ctx->{BINARY}");
385 $self->_prepare_list($ctx, "OBJ_LIST");
386 $self->_prepare_list($ctx, "FULL_OBJ_LIST");
387 $self->_prepare_list($ctx, "DEPEND_LIST");
388 $self->_prepare_list($ctx, "LINK_FLAGS");
390 $self->output(<< "__EOD__"
391 $installdir/$ctx->{BINARY}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST) $dynconfig_install
392 \@echo Linking \$\@
393 \@\$(LD) \$(LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
394 $dynconfig_install \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS)
396 __EOD__
400 sub Manpage($$)
402 my ($self,$ctx) = @_;
404 my $dir = $ctx->{BASEDIR};
406 $dir =~ s/^\.\///g;
408 push (@{$self->{manpages}}, "$dir/$ctx->{MANPAGE}");
411 sub PkgConfig($$)
413 my ($self,$ctx) = @_;
415 my $link_name = $ctx->{NAME};
417 $link_name =~ s/^LIB//g;
418 $link_name = lc($link_name);
420 return if (not defined($ctx->{DESCRIPTION}));
422 my $path = "$ctx->{BASEDIR}/$link_name.pc";
424 push (@{$self->{pc_files}}, $path);
426 my $pubs;
427 my $privs;
429 if (defined($ctx->{PUBLIC_DEPENDENCIES})) {
430 foreach (@{$ctx->{PUBLIC_DEPENDENCIES}}) {
431 # next unless ($self-> ) {
433 #FIXME $pubs .= "$_ ";
437 if (defined($ctx->{PRIVATE_DEPENDENCIES})) {
438 foreach (@{$ctx->{PRIVATE_DEPENDENCIES}}) {
439 # next unless ($self-> ) {
441 #FIXME $privs .= "$_ ";
445 smb_build::env::PkgConfig($self,
446 $path,
447 $link_name,
448 "-l$link_name",
450 "$ctx->{VERSION}",
451 $ctx->{DESCRIPTION},
452 defined($ctx->{INIT_FUNCTIONS}),
453 $pubs,
454 $privs
458 sub ProtoHeader($$)
460 my ($self,$ctx) = @_;
462 my $dir = $ctx->{BASEDIR};
464 $dir =~ s/^\.\///g;
466 my $target = "";
468 my $comment = "Creating ";
469 if (defined($ctx->{PRIVATE_PROTO_HEADER})) {
470 $target.= "$dir/$ctx->{PRIVATE_PROTO_HEADER}";
471 $comment.= "$dir/$ctx->{PRIVATE_PROTO_HEADER}";
472 if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
473 $comment .= " and ";
474 $target.= " ";
476 push (@{$self->{proto_headers}}, "$dir/$ctx->{PRIVATE_PROTO_HEADER}");
477 } else {
478 $ctx->{PRIVATE_PROTO_HEADER} = $ctx->{PUBLIC_PROTO_HEADER};
481 if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
482 $comment.= "$dir/$ctx->{PUBLIC_PROTO_HEADER}";
483 $target .= "$dir/$ctx->{PUBLIC_PROTO_HEADER}";
484 push (@{$self->{proto_headers}}, "$dir/$ctx->{PUBLIC_PROTO_HEADER}");
485 } else {
486 $ctx->{PUBLIC_PROTO_HEADER} = $ctx->{PRIVATE_PROTO_HEADER};
489 $self->output("$dir/$ctx->{PUBLIC_PROTO_HEADER}: $ctx->{MK_FILE} \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST:.o=.c) \$(srcdir)/script/mkproto.pl\n");
490 $self->output("\t\@echo \"$comment\"\n");
492 $self->output("\t\@\$(PERL) \$(srcdir)/script/mkproto.pl --srcdir=\$(srcdir) --builddir=\$(builddir) --private=$dir/$ctx->{PRIVATE_PROTO_HEADER} --public=$dir/$ctx->{PUBLIC_PROTO_HEADER} \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)\n\n");
495 sub write($$)
497 my ($self,$file) = @_;
499 $self->output("MANPAGES = ".array2oneperline($self->{manpages})."\n");
500 $self->output("BIN_PROGS = " . array2oneperline($self->{bin_progs}) . "\n");
501 $self->output("SBIN_PROGS = " . array2oneperline($self->{sbin_progs}) . "\n");
502 $self->output("TORTURE_PROGS = " . array2oneperline($self->{torture_progs}) . "\n");
503 $self->output("BINARIES = " . array2oneperline($self->{binaries}) . "\n");
504 $self->output("STATIC_LIBS = " . array2oneperline($self->{static_libs}) . "\n");
505 $self->output("SHARED_LIBS = " . array2oneperline($self->{shared_libs}) . "\n");
506 $self->output("INSTALLABLE_SHARED_LIBS = " . array2oneperline($self->{installable_shared_libs}) . "\n");
507 $self->output("PUBLIC_HEADERS = " . array2oneperline($self->{headers}) . "\n");
508 $self->output("PC_FILES = " . array2oneperline($self->{pc_files}) . "\n");
509 $self->output("ALL_OBJS = " . array2oneperline($self->{all_objs}) . "\n");
510 $self->output("PROTO_HEADERS = " . array2oneperline($self->{proto_headers}) . "\n");
511 $self->output("SHARED_MODULES = " . array2oneperline($self->{shared_modules}) . "\n");
512 $self->output("PLUGINS = " . array2oneperline($self->{plugins}) . "\n");
514 $self->output("\ninstallplugins: \$(PLUGINS)\n".$self->{install_plugins}."\n");
515 $self->output("\nuninstallplugins:\n".$self->{uninstall_plugins}."\n");
517 $self->_prepare_mk_files();
519 $self->output($self->{mkfile});
521 if ($self->{automatic_deps}) {
522 $self->output("
523 ifneq (\$(MAKECMDGOALS),clean)
524 ifneq (\$(MAKECMDGOALS),distclean)
525 ifneq (\$(MAKECMDGOALS),realdistclean)
526 -include \$(DEP_FILES)
527 endif
528 endif
529 endif
531 } else {
532 $self->output("include \$(srcdir)/static_deps.mk\n");
535 open(MAKEFILE,">$file") || die ("Can't open $file\n");
536 print MAKEFILE $self->{output};
537 close(MAKEFILE);
539 print __FILE__.": creating $file\n";