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