7e715b47eb349db20413b0f2d78a90b52d473b70
[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         my $publibs = "";
574
575         if (defined($ctx->{PUBLIC_DEPENDENCIES})) {
576                 foreach (@{$ctx->{PUBLIC_DEPENDENCIES}}) {
577                         next if ($other->{$_}->{ENABLE} eq "NO");
578                         if (defined($other->{$_}->{PC_NAME})) {
579                                 $pubs .= "$other->{$_}->{PC_NAME} ";
580                         } elsif ($other->{$_}->{TYPE} eq "EXT_LIB") {
581                                 my $e = $other->{$_};
582                                 my $ldflags = join(" ", @{$e->{LDFLAGS}});
583                                 $ldflags .= " " unless $ldflags eq "";
584                                 my $libs = join(" ", @{$e->{LIBS}});
585                                 $libs .= " " unless $libs eq "";
586
587                                 $publibs .= $ldflags.$libs;
588                         } else {
589                                 s/^LIB//g;
590                                 $_ = lc($_);
591
592                                 $privlibs .= "-l$_ ";
593                         }
594                 }
595         }
596
597         if (defined($ctx->{PRIVATE_DEPENDENCIES})) {
598                 foreach (@{$ctx->{PRIVATE_DEPENDENCIES}}) {
599                         next if ($other->{$_}->{ENABLE} eq "NO");
600                         if ($other->{$_}->{TYPE} eq "EXT_LIB") {
601                                 my $e = $other->{$_};
602
603                                 my $ldflags = join(" ", @{$e->{LDFLAGS}});
604                                 $ldflags .= " " unless $ldflags eq "";
605                                 my $libs = join(" ", @{$e->{LIBS}});
606                                 $libs .= " " unless $libs eq "";
607
608                                 $privlibs .= $ldflags.$libs;
609                         } elsif ($other->{$_}->{TYPE} eq "LIBRARY") {
610                                 s/^LIB//g;
611                                 $_ = lc($_);
612
613                                 $privs .= "$_ ";
614                         } else {
615                                 s/^LIB//g;
616                                 $_ = lc($_);
617
618                                 $privlibs .= "-l$_ ";
619                         }
620                 }
621         }
622
623         smb_build::env::PkgConfig($self,
624                 $path,
625                 $link_name,
626                 "-L\${libdir} -l$link_name $publibs",
627                 $privlibs,
628                 "",
629                 "$ctx->{VERSION}",
630                 $ctx->{DESCRIPTION},
631                 defined($ctx->{INIT_FUNCTIONS}),
632                 $pubs,
633                 "",
634                 [
635                         "prefix=$self->{config}->{prefix}",
636                         "exec_prefix=$self->{config}->{exec_prefix}",
637                         "libdir=$self->{config}->{libdir}",
638                         "includedir=$self->{config}->{includedir}"
639                 ]
640         ); 
641         my $abs_srcdir = abs_path($self->{config}->{srcdir});
642         smb_build::env::PkgConfig($self,
643                 "bin/pkgconfig/$link_name-uninstalled.pc",
644                 $link_name,
645                 "-Lbin/shared -Lbin/static -l$link_name",
646                 $privlibs,
647                 join(' ', 
648                         "-I$abs_srcdir",
649                         "-I$abs_srcdir/include",
650                         "-I$abs_srcdir/lib",
651                         "-I$abs_srcdir/lib/replace"),
652                 "$ctx->{VERSION}",
653                 $ctx->{DESCRIPTION},
654                 defined($ctx->{INIT_FUNCTIONS}),
655                 $pubs,
656                 $privs,
657                 [
658                         "prefix=bin/",
659                         "includedir=$ctx->{BASEDIR}"
660                 ]
661         ); 
662 }
663
664 sub ProtoHeader($$)
665 {
666         my ($self,$ctx) = @_;
667
668         my $target = "";
669         my $comment = "Creating ";
670
671         my $priv = undef;
672         my $pub = undef;
673
674         if (defined($ctx->{PRIVATE_PROTO_HEADER})) {
675                 $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER});
676                 $target .= $priv;
677                 $comment .= $priv;
678                 if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
679                         $comment .= " and ";
680                         $target.= " ";
681                 }
682                 push (@{$self->{proto_headers}}, $priv);
683         } else {
684                 $ctx->{PRIVATE_PROTO_HEADER} = $ctx->{PUBLIC_PROTO_HEADER};
685                 $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER});
686         }
687
688         if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
689                 $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER});
690                 $comment .= $pub;
691                 $target .= $pub;
692                 push (@{$self->{proto_headers}}, $pub);
693         } else {
694                 $ctx->{PUBLIC_PROTO_HEADER} = $ctx->{PRIVATE_PROTO_HEADER};
695                 $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER});
696         }
697
698         $self->output("$pub: $ctx->{MK_FILE} \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST:.o=.c) \$(srcdir)/script/mkproto.pl\n");
699         $self->output("\t\@echo \"$comment\"\n");
700         $self->output("\t\@\$(PERL) \$(srcdir)/script/mkproto.pl --srcdir=\$(srcdir) --builddir=\$(builddir) --private=$priv --public=$pub \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)\n\n");
701 }
702
703 sub write($$)
704 {
705         my ($self,$file) = @_;
706
707         $self->output("MANPAGES = ".array2oneperline($self->{manpages})."\n");
708         $self->output("BIN_PROGS = " . array2oneperline($self->{bin_progs}) . "\n");
709         $self->output("SBIN_PROGS = " . array2oneperline($self->{sbin_progs}) . "\n");
710         $self->output("TORTURE_PROGS = " . array2oneperline($self->{torture_progs}) . "\n");
711         $self->output("BINARIES = " . array2oneperline($self->{binaries}) . "\n");
712         $self->output("STATIC_LIBS = " . array2oneperline($self->{static_libs}) . "\n");
713         $self->output("SHARED_LIBS = " . array2oneperline($self->{shared_libs}) . "\n");
714         $self->output("PYTHON_DSOS = " . array2oneperline($self->{python_dsos}) . "\n");
715         $self->output("PYTHON_PYS = " . array2oneperline($self->{python_pys}) . "\n");
716         $self->output("INSTALLABLE_SHARED_LIBS = " . array2oneperline($self->{installable_shared_libs}) . "\n");
717         $self->output("PUBLIC_HEADERS = " . array2oneperline($self->{headers}) . "\n");
718         $self->output("PC_FILES = " . array2oneperline($self->{pc_files}) . "\n");
719         $self->output("ALL_OBJS = " . array2oneperline($self->{all_objs}) . "\n");
720         $self->output("PROTO_HEADERS = " . array2oneperline($self->{proto_headers}) .  "\n");
721         $self->output("SHARED_MODULES = " . array2oneperline($self->{shared_modules}) . "\n");
722         $self->output("PLUGINS = " . array2oneperline($self->{plugins}) . "\n");
723
724         $self->output("\ninstallplugins: \$(PLUGINS)\n".$self->{install_plugins}."\n");
725         $self->output("\nuninstallplugins:\n".$self->{uninstall_plugins}."\n");
726
727         $self->_prepare_mk_files();
728
729         $self->output($self->{mkfile});
730
731         if ($self->{automatic_deps}) {
732                 $self->output("
733 ifneq (\$(MAKECMDGOALS),clean)
734 ifneq (\$(MAKECMDGOALS),distclean)
735 ifneq (\$(MAKECMDGOALS),realdistclean)
736 ifneq (\$(SKIP_DEP_FILES),yes)
737 -include \$(DEP_FILES)
738 endif
739 endif
740 endif
741 endif
742
743 ifneq (\$(SKIP_DEP_FILES),yes)
744 clean::
745         \@echo Removing dependency files
746         \@find . -name '*.d' -o -name '*.hd' | xargs rm -f
747 endif
748 ");
749         } else {
750                 $self->output("include \$(srcdir)/static_deps.mk\n");
751         }
752
753         open(MAKEFILE,">$file") || die ("Can't open $file\n");
754         print MAKEFILE $self->{output};
755         close(MAKEFILE);
756
757         print __FILE__.": creating $file\n";
758 }
759
760 1;