954c476f33801481be0608180a1a6bd95a213bcb
[ira/wip.git] / source / build / smb_build / makefile.pm
1 # Samba Build System
2 # - create output for Makefile
3 #
4 #  Copyright (C) Stefan (metze) Metzmacher 2004
5 #  Copyright (C) Jelmer Vernooij 2005
6 #  Released under the GNU GPL
7
8 package smb_build::makefile;
9 use smb_build::env;
10 use File::Basename;
11 use strict;
12
13 use base 'smb_build::env';
14 use Cwd 'abs_path';
15
16 sub new($$$)
17 {
18         my ($myname, $config, $mkfile) = @_;
19         my $self = new smb_build::env($config);
20         
21         bless($self, $myname);
22
23         $self->{manpages} = [];
24         $self->{sbin_progs} = [];
25         $self->{bin_progs} = [];
26         $self->{torture_progs} = [];
27         $self->{static_libs} = [];
28         $self->{python_dsos} = [];
29         $self->{shared_libs} = [];
30         $self->{installable_shared_libs} = [];
31         $self->{headers} = [];
32         $self->{shared_modules} = [];
33         $self->{plugins} = [];
34         $self->{install_plugins} = "";
35         $self->{uninstall_plugins} = "";
36         $self->{pc_files} = [];
37         $self->{proto_headers} = [];
38         $self->{output} = "";
39
40         $self->{mkfile} = $mkfile;
41
42         $self->output("#!gmake\n");
43         $self->output("################################################\n");
44         $self->output("# Autogenerated by build/smb_build/makefile.pm #\n");
45         $self->output("################################################\n");
46         $self->output("\n");
47
48         $self->output("default: all\n\n");
49
50         $self->_prepare_path_vars();
51         $self->_prepare_suffix_rules();
52         $self->_prepare_compiler_linker();
53
54         if (!$self->{automatic_deps}) {
55                 $self->output("ALL_PREDEP = proto\n");
56                 $self->output(".NOTPARALLEL:\n");
57         }
58
59         return $self;
60 }
61
62 sub output($$)
63 {
64         my ($self, $text) = @_;
65
66         $self->{output} .= $text;
67 }
68
69 sub _prepare_path_vars($)
70 {
71         my ($self) = @_;
72
73         $self->output(<< "__EOD__"
74 prefix = $self->{config}->{prefix}
75 exec_prefix = $self->{config}->{exec_prefix}
76 selftest_prefix = $self->{config}->{selftest_prefix}
77
78 builddir = $self->{config}->{builddir}
79 srcdir = $self->{config}->{srcdir}
80 datarootdir = $self->{config}->{datarootdir}
81
82 VPATH = \$(builddir):\$(srcdir):heimdal_build:heimdal/lib/asn1:heimdal/lib/krb5:heimdal/lib/gssapi:heimdal/lib/hdb:heimdal/lib/roken:heimdal/lib/des
83
84 BASEDIR = $self->{config}->{prefix}
85 BINDIR = $self->{config}->{bindir}
86 SBINDIR = $self->{config}->{sbindir}
87 LIBDIR = $self->{config}->{libdir}
88 TORTUREDIR = $self->{config}->{libdir}/torture
89 MODULESDIR = $self->{config}->{modulesdir}
90 INCLUDEDIR = $self->{config}->{includedir}
91 CONFIGDIR = $self->{config}->{sysconfdir}
92 DATADIR = $self->{config}->{datadir}
93 SWATDIR = \$(DATADIR)/swat
94 JSDIR = \$(DATADIR)/js
95 SETUPDIR = \$(DATADIR)/setup
96 VARDIR = $self->{config}->{localstatedir}
97 LOGFILEBASE = $self->{config}->{logfilebase}
98 NCALRPCDIR = $self->{config}->{localstatedir}/ncalrpc
99 LOCKDIR = $self->{config}->{lockdir}
100 PIDDIR = $self->{config}->{piddir}
101 MANDIR = $self->{config}->{mandir}
102 PRIVATEDIR = $self->{config}->{privatedir}
103 WINBINDD_SOCKET_DIR = $self->{config}->{winbindd_socket_dir}
104
105 __EOD__
106 );
107 }
108
109 sub _prepare_suffix_rules($)
110 {
111         my ($self) = @_;
112         my $first_prereq = '$*.c';
113
114         if ($self->{config}->{GNU_MAKE} eq 'yes') {
115                 $first_prereq = '$<';
116         }
117
118         $self->output(<< "__EOD__"
119 FIRST_PREREQ = $first_prereq
120
121 # Dependencies command
122 DEPENDS = \$(CC) -M -MG -MP -MT \$(<:.c=.o) -MT \$@ \\
123     `\$(PERL) \$(srcdir)/script/cflags.pl \$@` \\
124     \$(CFLAGS) \$(FIRST_PREREQ) -o \$@
125 # Dependencies for host objects
126 HDEPENDS = \$(CC) -M -MG -MP -MT \$(<:.c=.ho) -MT \$@ \\
127     `\$(PERL) \$(srcdir)/script/cflags.pl \$@` \\
128     \$(HOSTCC_FLAGS) \$(FIRST_PREREQ) -o \$@
129 # Dependencies for precompiled headers
130 PCHDEPENDS = \$(CC) -M -MG -MT include/includes.h.gch -MT \$@ \\
131     \$(CFLAGS) \$(FIRST_PREREQ) -o \$@
132
133 # \$< is broken in older BSD versions:
134 # when \$@ is foo/bar.o, \$< could be torture/foo/bar.c
135 # if it also exists. So better use \$* which is foo/bar
136 # and append .c manually to get foo/bar.c
137 #
138 # If we have GNU Make, it is safe to use \$<, which also lets
139 # building with \$srcdir != \$builddir work.
140
141 # Run a static analysis checker
142 CHECK = \$(CC_CHECKER) `\$(PERL) \$(srcdir)/script/cflags.pl \$@` \\
143     \$(CFLAGS) \$(PICFLAG) -c \$(FIRST_PREREQ) -o \$@
144
145 # Run the configured compiler
146 COMPILE = \$(CC) `\$(PERL) \$(srcdir)/script/cflags.pl \$@` \\
147     \$(CFLAGS) \$(PICFLAG) -c \$(FIRST_PREREQ) -o \$@
148
149 # Run the compiler for the build host
150 HCOMPILE = \$(HOSTCC) `\$(PERL) \$(srcdir)/script/cflags.pl \$@` \\
151     \$(HOSTCC_FLAGS) -c \$(FIRST_PREREQ) -o \$@
152
153 # Precompile headers
154 PCHCOMPILE = @\$(CC) -Ilib/replace \\
155     `\$(PERL) \$(srcdir)/script/cflags.pl \$@` \\
156     \$(CFLAGS) \$(PICFLAG) -c \$(FIRST_PREREQ) -o \$@
157
158 __EOD__
159 );
160 }
161
162 sub _prepare_compiler_linker($)
163 {
164         my ($self) = @_;
165
166         my $builddir_headers = "";
167         my $libdir;
168         my $extra_link_flags = "";
169
170         if ($self->{config}->{USESHARED} eq "true") {
171                 $libdir = "\$(builddir)/bin/shared";
172                 $extra_link_flags = "-Wl,-rpath-link,\$(builddir)/bin/shared";
173         } else {
174                 $libdir = "\$(builddir)/bin/static";
175         }
176         
177         if (!(abs_path($self->{config}->{srcdir}) eq abs_path($self->{config}->{builddir}))) {
178             $builddir_headers= "-I\$(builddir)/include -I\$(builddir) -I\$(builddir)/lib ";
179         }
180
181         $self->output(<< "__EOD__"
182 SHELL=$self->{config}->{SHELL}
183
184 PERL=$self->{config}->{PERL}
185 PYTHON=$self->{config}->{PYTHON}
186 SWIG=$self->{config}->{SWIG}
187
188 CPP=$self->{config}->{CPP}
189 CPPFLAGS=$builddir_headers-I\$(srcdir)/include -I\$(srcdir) -I\$(srcdir)/lib -I\$(srcdir)/lib/replace -I\$(srcdir)/lib/talloc -D_SAMBA_BUILD_=4 -DHAVE_CONFIG_H $self->{config}->{CPPFLAGS}
190
191 CC=$self->{config}->{CC}
192 CFLAGS=$self->{config}->{CFLAGS} \$(CPPFLAGS)
193 PICFLAG=$self->{config}->{PICFLAG}
194
195 INSTALL_LINK_FLAGS=$extra_link_flags
196
197 BNLD=$self->{config}->{LD} 
198 BNLD_FLAGS=$self->{config}->{LDFLAGS} -L$libdir
199
200 STLD=$self->{config}->{STLD}
201 STLD_FLAGS=$self->{config}->{STLD_FLAGS}
202
203 SHLD=$self->{config}->{SHLD}
204 SHLD_FLAGS=$self->{config}->{SHLD_FLAGS} -L\$(builddir)/bin/shared
205
206 MDLD=$self->{config}->{MDLD}
207 MDLD_FLAGS=$self->{config}->{MDLD_FLAGS} -L\$(builddir)/bin/shared
208
209 SHLIBEXT=$self->{config}->{SHLIBEXT}
210
211 HOSTCC=$self->{config}->{HOSTCC}
212 HOSTCC_FLAGS=-D_SAMBA_HOSTCC_ $self->{config}->{CFLAGS} \$(CPPFLAGS)
213
214 HOSTLD=$self->{config}->{HOSTLD}
215 HOSTLD_FLAGS=$self->{config}->{LDFLAGS}
216
217 XSLTPROC=$self->{config}->{XSLTPROC}
218
219 LEX=$self->{config}->{LEX}
220 YACC=$self->{config}->{YACC}
221 YAPP=$self->{config}->{YAPP}
222
223 GCOV=$self->{config}->{GCOV}
224
225 DEFAULT_TEST_OPTIONS=$self->{config}->{DEFAULT_TEST_OPTIONS}
226
227 __EOD__
228 );
229 }
230
231 sub _prepare_mk_files($)
232 {
233         my $self = shift;
234         my @tmp = ();
235
236         foreach (@smb_build::config_mk::parsed_files) {
237                 s/ .*$//g;
238                 push (@tmp, $_);
239         }
240
241         if ($self->{gnu_make}) {
242                 $self->output("
243 ifneq (\$(MAKECMDGOALS),clean)
244 ifneq (\$(MAKECMDGOALS),distclean)
245 ifneq (\$(MAKECMDGOALS),realdistclean)
246 ");
247         }
248
249         $self->output("MK_FILES = " . array2oneperline(\@tmp) . "\n");
250
251         if ($self->{gnu_make}) {
252                 $self->output("
253 endif
254 endif
255 endif
256 ");
257         }
258 }
259
260 sub array2oneperline($)
261 {
262         my $array = shift;
263         my $output = "";
264
265         foreach (@$array) {
266                 next unless defined($_);
267
268                 $output .= " \\\n\t\t$_";
269         }
270
271         return $output;
272 }
273
274 sub _prepare_list_ex($$$$$)
275 {
276         my ($self,$ctx,$var,$pre,$post) = @_;
277         my @tmparr = ();
278
279         push(@tmparr, $pre) if defined($pre);
280         push(@tmparr, @{$ctx->{$var}}) if defined($ctx->{$var});
281         push(@tmparr, $post) if defined($post);
282
283         my $tmplist = array2oneperline(\@tmparr);
284         return if ($tmplist eq "");
285
286         $self->output("$ctx->{TYPE}\_$ctx->{NAME}_$var =$tmplist\n");
287 }
288
289 sub _prepare_list($$$)
290 {
291         my ($self,$ctx,$var) = @_;
292
293         $self->_prepare_list_ex($ctx, $var, undef, undef);
294 }
295
296 sub Integrated($$)
297 {
298         my ($self,$ctx) = @_;
299
300         $self->_prepare_list($ctx, "OBJ_LIST");
301         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
302         $self->_prepare_list($ctx, "LINK_FLAGS");
303 }
304
305 sub SharedModule($$)
306 {
307         my ($self,$ctx) = @_;
308
309         my $init_obj = "";
310
311         my $sane_subsystem = lc($ctx->{SUBSYSTEM});
312         $sane_subsystem =~ s/^lib//;
313         
314         if ($ctx->{TYPE} eq "PYTHON") {
315                 push (@{$self->{python_dsos}}, 
316                         "$ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}");
317         }
318
319         push (@{$self->{shared_modules}}, "$ctx->{TARGET_SHARED_LIBRARY}");
320         push (@{$self->{plugins}}, "$ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}");
321
322         $self->{install_plugins} .= "\t\@echo Installing $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME} as \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n";
323         $self->{install_plugins} .= "\t\@mkdir -p \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/\n";
324         $self->{install_plugins} .= "\t\@cp $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME} \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n";
325         $self->{uninstall_plugins} .= "\t\@echo Uninstalling \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n";
326         $self->{uninstall_plugins} .= "\t\@-rm \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n";
327         if (defined($ctx->{ALIASES})) {
328                 foreach (@{$ctx->{ALIASES}}) {
329                         $self->{install_plugins} .= "\t\@rm -f \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/$_.\$(SHLIBEXT)\n";
330                         $self->{install_plugins} .= "\t\@ln -fs $ctx->{LIBRARY_REALNAME} \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/$_.\$(SHLIBEXT)\n";
331                         $self->{uninstall_plugins} .= "\t\@-rm \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/$_.\$(SHLIBEXT)\n";
332                 }
333         }
334
335         $self->output("$ctx->{TYPE}_$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
336         $self->_prepare_list($ctx, "OBJ_LIST");
337         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
338         $self->_prepare_list($ctx, "DEPEND_LIST");
339         $self->_prepare_list($ctx, "LINK_FLAGS");
340
341         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)");
342
343         if (defined($ctx->{INIT_FUNCTION})) {
344                 my $init_fn = $ctx->{INIT_FUNCTION_TYPE};
345                 $init_fn =~ s/\(\*\)/init_module/;
346                 my $proto_fn = $ctx->{INIT_FUNCTION_TYPE};
347                 $proto_fn =~ s/\(\*\)/$ctx->{INIT_FUNCTION}/;
348
349                 $self->output(<< "__EOD__"
350 bin/$ctx->{NAME}_init_module.c:
351         \@echo Creating \$\@
352         \@echo \"#include \\\"includes.h\\\"\" > \$\@
353         \@echo \"$proto_fn;\" >> \$\@
354         \@echo \"_PUBLIC_ $init_fn\" >> \$\@
355         \@echo \"{\" >> \$\@
356         \@echo \"       return $ctx->{INIT_FUNCTION}();\" >> \$\@
357         \@echo \"}\" >> \$\@
358         \@echo \"\" >> \$\@
359 __EOD__
360 );
361                 $init_obj = "bin/$ctx->{NAME}_init_module.o";
362         }
363
364         $self->output(<< "__EOD__"
365 #
366
367 $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST) $init_obj
368         \@echo Linking \$\@
369         \@mkdir -p $ctx->{SHAREDDIR}
370         \@\$(MDLD) \$(MDLD_FLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
371                 \$($ctx->{TYPE}_$ctx->{NAME}\_FULL_OBJ_LIST) $init_obj \\
372                 \$($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS)
373 __EOD__
374 );
375
376         if (defined($ctx->{ALIASES})) {
377                 foreach (@{$ctx->{ALIASES}}) {
378                         $self->output("\t\@rm -f $ctx->{SHAREDDIR}/$_.\$(SHLIBEXT)\n");
379                         $self->output("\t\@ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{SHAREDDIR}/$_.\$(SHLIBEXT)\n");
380                 }
381         }
382         $self->output("\n");
383 }
384
385 sub SharedLibrary($$)
386 {
387         my ($self,$ctx) = @_;
388
389         my $has_static_lib = 0;
390
391         push (@{$self->{shared_libs}}, "$ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}") if (defined($ctx->{SO_VERSION}));
392         push (@{$self->{installable_shared_libs}}, "$ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}") if (defined($ctx->{SO_VERSION}));
393
394         $has_static_lib = 1 if grep(/STATIC_LIBRARY/, @{$ctx->{OUTPUT_TYPE}});
395
396         if (not $has_static_lib) {
397                 $self->output("$ctx->{TYPE}_$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
398                 $self->_prepare_list($ctx, "OBJ_LIST");
399                 $self->_prepare_list($ctx, "FULL_OBJ_LIST");
400         }
401         $self->_prepare_list($ctx, "DEPEND_LIST");
402         $self->_prepare_list($ctx, "LINK_FLAGS");
403 #       $self->_prepare_list_ex($ctx, "LINK_FLAGS", "-Wl,--whole-archive", "-Wl,--no-whole-archive");
404
405         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)");
406
407         my $soarg = "";
408         my $lns = "";
409         if ($self->{config}->{SONAMEFLAG} ne "#" and defined($ctx->{LIBRARY_SONAME})) {
410                 $soarg = "$self->{config}->{SONAMEFLAG}$ctx->{LIBRARY_SONAME}";
411                 if ($ctx->{LIBRARY_REALNAME} ne $ctx->{LIBRARY_SONAME}) {
412                         $lns .= "\n\t\@rm -f $ctx->{SHAREDDIR}/$ctx->{LIBRARY_SONAME}";
413                         $lns .= "\n\t\@ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{SHAREDDIR}/$ctx->{LIBRARY_SONAME}";
414                 }
415         }
416
417         if (defined($ctx->{LIBRARY_SONAME})) {
418                 $lns .= "\n\t\@rm -f $ctx->{SHAREDDIR}/$ctx->{LIBRARY_DEBUGNAME}";
419                 $lns .= "\n\t\@ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{SHAREDDIR}/$ctx->{LIBRARY_DEBUGNAME}";
420         }
421
422         $self->output(<< "__EOD__"
423 #
424
425 $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)
426         \@echo Linking \$\@
427         \@mkdir -p $ctx->{SHAREDDIR}
428         \@\$(SHLD) \$(SHLD_FLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
429                 \$($ctx->{TYPE}_$ctx->{NAME}\_FULL_OBJ_LIST) \\
430                 \$($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) \\
431                 $soarg$lns
432 __EOD__
433 );
434         $self->output("\n");
435 }
436
437 sub StaticLibrary($$)
438 {
439         my ($self,$ctx) = @_;
440
441         return unless (defined($ctx->{OBJ_FILES}));
442
443         push (@{$self->{static_libs}}, $ctx->{TARGET_STATIC_LIBRARY}) if ($ctx->{TYPE} eq "LIBRARY");
444
445         $self->output("$ctx->{TYPE}_$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
446         $self->_prepare_list($ctx, "OBJ_LIST");
447         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
448
449         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)");
450
451         $self->output(<< "__EOD__"
452 #
453 $ctx->{TARGET_STATIC_LIBRARY}: \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)
454         \@echo Linking \$@
455         \@rm -f \$@
456         \@mkdir -p $ctx->{STATICDIR}
457         \@\$(STLD) \$(STLD_FLAGS) \$@ \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)
458
459 __EOD__
460 );
461 }
462
463 sub Header($$)
464 {
465         my ($self,$ctx) = @_;
466
467         my $dir = $ctx->{BASEDIR};
468
469         $dir =~ s/^\.\///g;
470
471         foreach (@{$ctx->{PUBLIC_HEADERS}}) {
472                 push (@{$self->{headers}}, "$dir/$_");
473         }
474 }
475
476 sub Binary($$)
477 {
478         my ($self,$ctx) = @_;
479
480         my $installdir;
481         my $extradir = "";
482
483         if (defined($ctx->{INSTALLDIR}) && $ctx->{INSTALLDIR} =~ /^TORTUREDIR/) {
484                 $extradir = "/torture" . substr($ctx->{INSTALLDIR}, length("TORTUREDIR"));
485         }
486         my $localdir = "bin$extradir";
487
488         $installdir = "bin$extradir";
489
490         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)");
491                 
492         unless (defined($ctx->{INSTALLDIR})) {
493         } elsif ($ctx->{INSTALLDIR} eq "SBINDIR") {
494                 push (@{$self->{sbin_progs}}, "$installdir/$ctx->{BINARY}");
495         } elsif ($ctx->{INSTALLDIR} eq "BINDIR") {
496                 push (@{$self->{bin_progs}}, "$installdir/$ctx->{BINARY}");
497         } elsif ($ctx->{INSTALLDIR} =~ /^TORTUREDIR/) {
498                 push (@{$self->{torture_progs}}, "$installdir/$ctx->{BINARY}");
499         }
500
501
502         push (@{$self->{binaries}}, "$localdir/$ctx->{BINARY}");
503
504         $self->_prepare_list($ctx, "OBJ_LIST");
505         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
506         $self->_prepare_list($ctx, "DEPEND_LIST");
507         $self->_prepare_list($ctx, "LINK_FLAGS");
508
509 $self->output(<< "__EOD__"
510 $installdir/$ctx->{BINARY}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)
511         \@echo Linking \$\@
512 __EOD__
513         );
514
515         if (defined($ctx->{USE_HOSTCC}) && $ctx->{USE_HOSTCC} eq "YES") {
516                 $self->output(<< "__EOD__"
517         \@\$(HOSTLD) \$(HOSTLD_FLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
518                 \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS)
519 __EOD__
520                 );
521         } else {
522                 $self->output(<< "__EOD__"
523         \@\$(BNLD) \$(BNLD_FLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
524                 \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) 
525
526 __EOD__
527                 );
528         }
529 }
530
531 sub PythonFiles($$)
532 {
533         my ($self,$ctx) = @_;
534
535         foreach (@{$ctx->{PYTHON_FILES}}) {
536                 my $target = "bin/python/".basename($_);
537                 $self->output("$target: $ctx->{BASEDIR}/$_\n" .
538                               "\tcp $ctx->{BASEDIR}/$_ \$@\n\n");
539                 push (@{$self->{python_dsos}}, $target);
540   }
541 }
542
543 sub Manpage($$)
544 {
545         my ($self,$ctx) = @_;
546
547         my $dir = $ctx->{BASEDIR};
548         
549         $dir =~ s/^\.\///g;
550
551         push (@{$self->{manpages}}, "$dir/$ctx->{MANPAGE}");
552 }
553
554 sub PkgConfig($$$)
555 {
556         my ($self,$ctx,$other) = @_;
557         
558         my $link_name = $ctx->{NAME};
559
560         $link_name =~ s/^LIB//g;
561         $link_name = lc($link_name);
562
563         return if (not defined($ctx->{DESCRIPTION}));
564
565         my $path = "$ctx->{BASEDIR}/$link_name.pc";
566
567         push (@{$self->{pc_files}}, $path);
568
569         my $pubs;
570         my $privs;
571         my $privlibs;
572
573         if (defined($ctx->{PUBLIC_DEPENDENCIES})) {
574                 foreach (@{$ctx->{PUBLIC_DEPENDENCIES}}) {
575                         next if ($other->{$_}->{ENABLE} eq "NO");
576                         if ($other->{$_}->{TYPE} eq "LIBRARY") {
577                                 s/^LIB//g;
578                                 $_ = lc($_);
579
580                                 $pubs .= "$_ ";
581                         } else {
582                                 s/^LIB//g;
583                                 $_ = lc($_);
584
585                                 $privlibs .= "-l$_ ";
586                         }
587                 }
588         }
589
590         if (defined($ctx->{PRIVATE_DEPENDENCIES})) {
591                 foreach (@{$ctx->{PRIVATE_DEPENDENCIES}}) {
592                         next if ($other->{$_}->{ENABLE} eq "NO");
593                         if ($other->{$_}->{TYPE} eq "LIBRARY") {
594                                 s/^LIB//g;
595                                 $_ = lc($_);
596
597                                 $privs .= "$_ ";
598                         } else {
599                                 s/^LIB//g;
600                                 $_ = lc($_);
601
602                                 $privlibs .= "-l$_ ";
603                         }
604                 }
605         }
606
607         smb_build::env::PkgConfig($self,
608                 $path,
609                 $link_name,
610                 "-L\${libdir} -l$link_name",
611                 $privlibs,
612                 "",
613                 "$ctx->{VERSION}",
614                 $ctx->{DESCRIPTION},
615                 defined($ctx->{INIT_FUNCTIONS}),
616                 $pubs,
617                 "",
618                 [
619                         "prefix=$self->{config}->{prefix}",
620                         "exec_prefix=$self->{config}->{exec_prefix}",
621                         "libdir=$self->{config}->{libdir}",
622                         "includedir=$self->{config}->{includedir}"
623                 ]
624         ); 
625         my $abs_srcdir = abs_path($self->{config}->{srcdir});
626         smb_build::env::PkgConfig($self,
627                 "bin/pkgconfig/$link_name-uninstalled.pc",
628                 $link_name,
629                 "-Lbin/shared -Lbin/static -l$link_name",
630                 $privlibs,
631                 join(' ', 
632                         "-I$abs_srcdir",
633                         "-I$abs_srcdir/include",
634                         "-I$abs_srcdir/lib",
635                         "-I$abs_srcdir/lib/replace"),
636                 "$ctx->{VERSION}",
637                 $ctx->{DESCRIPTION},
638                 defined($ctx->{INIT_FUNCTIONS}),
639                 $pubs,
640                 $privs,
641                 [
642                         "prefix=bin/",
643                         "includedir=$ctx->{BASEDIR}"
644                 ]
645         ); 
646 }
647
648 sub ProtoHeader($$)
649 {
650         my ($self,$ctx) = @_;
651
652         my $dir = $ctx->{BASEDIR};
653
654         $dir =~ s/^\.\///g;
655
656         my $target = "";
657
658         my $comment = "Creating ";
659         if (defined($ctx->{PRIVATE_PROTO_HEADER})) {
660                 $target.= "$dir/$ctx->{PRIVATE_PROTO_HEADER}";
661                 $comment.= "$dir/$ctx->{PRIVATE_PROTO_HEADER}";
662                 if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
663                         $comment .= " and ";
664                         $target.= " ";
665                 }
666                 push (@{$self->{proto_headers}}, "$dir/$ctx->{PRIVATE_PROTO_HEADER}");
667         } else {
668                 $ctx->{PRIVATE_PROTO_HEADER} = $ctx->{PUBLIC_PROTO_HEADER};
669         }
670         
671         if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
672                 $comment.= "$dir/$ctx->{PUBLIC_PROTO_HEADER}";
673                 $target .= "$dir/$ctx->{PUBLIC_PROTO_HEADER}";
674                 push (@{$self->{proto_headers}}, "$dir/$ctx->{PUBLIC_PROTO_HEADER}");
675         } else {
676                 $ctx->{PUBLIC_PROTO_HEADER} = $ctx->{PRIVATE_PROTO_HEADER};
677         }       
678
679         $self->output("$dir/$ctx->{PUBLIC_PROTO_HEADER}: $ctx->{MK_FILE} \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST:.o=.c) \$(srcdir)/script/mkproto.pl\n");
680         $self->output("\t\@echo \"$comment\"\n");
681
682         $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");
683 }
684
685 sub write($$)
686 {
687         my ($self,$file) = @_;
688
689         $self->output("MANPAGES = ".array2oneperline($self->{manpages})."\n");
690         $self->output("BIN_PROGS = " . array2oneperline($self->{bin_progs}) . "\n");
691         $self->output("SBIN_PROGS = " . array2oneperline($self->{sbin_progs}) . "\n");
692         $self->output("TORTURE_PROGS = " . array2oneperline($self->{torture_progs}) . "\n");
693         $self->output("BINARIES = " . array2oneperline($self->{binaries}) . "\n");
694         $self->output("STATIC_LIBS = " . array2oneperline($self->{static_libs}) . "\n");
695         $self->output("SHARED_LIBS = " . array2oneperline($self->{shared_libs}) . "\n");
696         $self->output("PYTHON_DSOS = " . array2oneperline($self->{python_dsos}) . "\n");
697         $self->output("INSTALLABLE_SHARED_LIBS = " . array2oneperline($self->{installable_shared_libs}) . "\n");
698         $self->output("PUBLIC_HEADERS = " . array2oneperline($self->{headers}) . "\n");
699         $self->output("PC_FILES = " . array2oneperline($self->{pc_files}) . "\n");
700         $self->output("ALL_OBJS = " . array2oneperline($self->{all_objs}) . "\n");
701         $self->output("PROTO_HEADERS = " . array2oneperline($self->{proto_headers}) .  "\n");
702         $self->output("SHARED_MODULES = " . array2oneperline($self->{shared_modules}) . "\n");
703         $self->output("PLUGINS = " . array2oneperline($self->{plugins}) . "\n");
704
705         $self->output("\ninstallplugins: \$(PLUGINS)\n".$self->{install_plugins}."\n");
706         $self->output("\nuninstallplugins:\n".$self->{uninstall_plugins}."\n");
707
708         $self->_prepare_mk_files();
709
710         $self->output($self->{mkfile});
711
712         if ($self->{automatic_deps}) {
713                 $self->output("
714 ifneq (\$(MAKECMDGOALS),clean)
715 ifneq (\$(MAKECMDGOALS),distclean)
716 ifneq (\$(MAKECMDGOALS),realdistclean)
717 ifneq (\$(SKIP_DEP_FILES),yes)
718 -include \$(DEP_FILES)
719 endif
720 endif
721 endif
722 endif
723
724 ifneq (\$(SKIP_DEP_FILES),yes)
725 clean::
726         \@echo Removing dependency files
727         \@find . -name '*.d' -o -name '*.hd' | xargs rm -f
728 endif
729 ");
730         } else {
731                 $self->output("include \$(srcdir)/static_deps.mk\n");
732         }
733
734         open(MAKEFILE,">$file") || die ("Can't open $file\n");
735         print MAKEFILE $self->{output};
736         close(MAKEFILE);
737
738         print __FILE__.": creating $file\n";
739 }
740
741 1;