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