r26291: build: make use of output::add_dir_str() to avoid ugly foo/../bla pathes
[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 LEX=$self->{config}->{LEX}
221 YACC=$self->{config}->{YACC}
222 YAPP=$self->{config}->{YAPP}
223
224 GCOV=$self->{config}->{GCOV}
225
226 DEFAULT_TEST_OPTIONS=$self->{config}->{DEFAULT_TEST_OPTIONS}
227
228 __EOD__
229 );
230 }
231
232 sub _prepare_mk_files($)
233 {
234         my $self = shift;
235         my @tmp = ();
236
237         foreach (@smb_build::config_mk::parsed_files) {
238                 s/ .*$//g;
239                 push (@tmp, $_);
240         }
241
242         if ($self->{gnu_make}) {
243                 $self->output("
244 ifneq (\$(MAKECMDGOALS),clean)
245 ifneq (\$(MAKECMDGOALS),distclean)
246 ifneq (\$(MAKECMDGOALS),realdistclean)
247 ");
248         }
249
250         $self->output("MK_FILES = " . array2oneperline(\@tmp) . "\n");
251
252         if ($self->{gnu_make}) {
253                 $self->output("
254 endif
255 endif
256 endif
257 ");
258         }
259 }
260
261 sub array2oneperline($)
262 {
263         my $array = shift;
264         my $output = "";
265
266         foreach (@$array) {
267                 next unless defined($_);
268
269                 $output .= " \\\n\t\t$_";
270         }
271
272         return $output;
273 }
274
275 sub _prepare_list_ex($$$$$)
276 {
277         my ($self,$ctx,$var,$pre,$post) = @_;
278         my @tmparr = ();
279
280         push(@tmparr, $pre) if defined($pre);
281         push(@tmparr, @{$ctx->{$var}}) if defined($ctx->{$var});
282         push(@tmparr, $post) if defined($post);
283
284         my $tmplist = array2oneperline(\@tmparr);
285         return if ($tmplist eq "");
286
287         $self->output("$ctx->{TYPE}\_$ctx->{NAME}_$var =$tmplist\n");
288 }
289
290 sub _prepare_list($$$)
291 {
292         my ($self,$ctx,$var) = @_;
293
294         $self->_prepare_list_ex($ctx, $var, undef, undef);
295 }
296
297 sub Integrated($$)
298 {
299         my ($self,$ctx) = @_;
300
301         $self->_prepare_list($ctx, "OBJ_LIST");
302         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
303         $self->_prepare_list($ctx, "LINK_FLAGS");
304 }
305
306 sub SharedModule($$)
307 {
308         my ($self,$ctx) = @_;
309
310         my $init_obj = "";
311
312         my $sane_subsystem = lc($ctx->{SUBSYSTEM});
313         $sane_subsystem =~ s/^lib//;
314         
315         if ($ctx->{TYPE} eq "PYTHON") {
316                 push (@{$self->{python_dsos}}, 
317                         "$ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}");
318         } else {
319                 push (@{$self->{shared_modules}}, "$ctx->{TARGET_SHARED_LIBRARY}");
320                 push (@{$self->{plugins}}, "$ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}");
321                 $self->{install_plugins} .= "\t\@echo Installing $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME} as \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n";
322                 $self->{install_plugins} .= "\t\@mkdir -p \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/\n";
323                 $self->{install_plugins} .= "\t\@cp $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME} \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n";
324                 $self->{uninstall_plugins} .= "\t\@echo Uninstalling \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n";
325                 $self->{uninstall_plugins} .= "\t\@-rm \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n";
326                 if (defined($ctx->{ALIASES})) {
327                         foreach (@{$ctx->{ALIASES}}) {
328                                 $self->{install_plugins} .= "\t\@rm -f \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/$_.\$(SHLIBEXT)\n";
329                                 $self->{install_plugins} .= "\t\@ln -fs $ctx->{LIBRARY_REALNAME} \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/$_.\$(SHLIBEXT)\n";
330                                 $self->{uninstall_plugins} .= "\t\@-rm \$(DESTDIR)\$(MODULESDIR)/$sane_subsystem/$_.\$(SHLIBEXT)\n";
331                         }
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         foreach (@{$ctx->{PUBLIC_HEADERS}}) {
468                 push (@{$self->{headers}}, output::add_dir_str($ctx->{BASEDIR}, $_));
469         }
470 }
471
472 sub Binary($$)
473 {
474         my ($self,$ctx) = @_;
475
476         my $installdir;
477         my $extradir = "";
478
479         if (defined($ctx->{INSTALLDIR}) && $ctx->{INSTALLDIR} =~ /^TORTUREDIR/) {
480                 $extradir = "/torture" . substr($ctx->{INSTALLDIR}, length("TORTUREDIR"));
481         }
482         my $localdir = "bin$extradir";
483
484         $installdir = "bin$extradir";
485
486         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)");
487                 
488         unless (defined($ctx->{INSTALLDIR})) {
489         } elsif ($ctx->{INSTALLDIR} eq "SBINDIR") {
490                 push (@{$self->{sbin_progs}}, "$installdir/$ctx->{BINARY}");
491         } elsif ($ctx->{INSTALLDIR} eq "BINDIR") {
492                 push (@{$self->{bin_progs}}, "$installdir/$ctx->{BINARY}");
493         } elsif ($ctx->{INSTALLDIR} =~ /^TORTUREDIR/) {
494                 push (@{$self->{torture_progs}}, "$installdir/$ctx->{BINARY}");
495         }
496
497
498         push (@{$self->{binaries}}, "$localdir/$ctx->{BINARY}");
499
500         $self->_prepare_list($ctx, "OBJ_LIST");
501         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
502         $self->_prepare_list($ctx, "DEPEND_LIST");
503         $self->_prepare_list($ctx, "LINK_FLAGS");
504
505 $self->output(<< "__EOD__"
506 $installdir/$ctx->{BINARY}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)
507         \@echo Linking \$\@
508 __EOD__
509         );
510
511         if (defined($ctx->{USE_HOSTCC}) && $ctx->{USE_HOSTCC} eq "YES") {
512                 $self->output(<< "__EOD__"
513         \@\$(HOSTLD) \$(HOSTLD_FLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
514                 \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS)
515 __EOD__
516                 );
517         } else {
518                 $self->output(<< "__EOD__"
519         \@\$(BNLD) \$(BNLD_FLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
520                 \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) 
521
522 __EOD__
523                 );
524         }
525 }
526
527 sub PythonFiles($$)
528 {
529         my ($self,$ctx) = @_;
530
531         foreach (@{$ctx->{PYTHON_FILES}}) {
532                 my $target = "bin/python/".basename($_);
533                 my $source = output::add_dir_str($ctx->{BASEDIR}, $_);
534                 $self->output("$target: $source\n" .
535                               "\tcp $source \$@\n\n");
536                 push (@{$self->{python_dsos}}, $target);
537         }
538 }
539
540 sub Manpage($$)
541 {
542         my ($self,$ctx) = @_;
543
544         my $path = output::add_dir_str($ctx->{BASEDIR}, $ctx->{MANPAGE});
545         push (@{$self->{manpages}}, $path);
546 }
547
548 sub PkgConfig($$$)
549 {
550         my ($self,$ctx,$other) = @_;
551         
552         my $link_name = $ctx->{NAME};
553
554         $link_name =~ s/^LIB//g;
555         $link_name = lc($link_name);
556
557         return if (not defined($ctx->{DESCRIPTION}));
558
559         my $path = output::add_dir_str($ctx->{BASEDIR}, "$link_name.pc");
560
561         push (@{$self->{pc_files}}, $path);
562
563         my $pubs;
564         my $privs;
565         my $privlibs;
566
567         if (defined($ctx->{PUBLIC_DEPENDENCIES})) {
568                 foreach (@{$ctx->{PUBLIC_DEPENDENCIES}}) {
569                         next if ($other->{$_}->{ENABLE} eq "NO");
570                         if ($other->{$_}->{TYPE} eq "LIBRARY") {
571                                 s/^LIB//g;
572                                 $_ = lc($_);
573
574                                 $pubs .= "$_ ";
575                         } else {
576                                 s/^LIB//g;
577                                 $_ = lc($_);
578
579                                 $privlibs .= "-l$_ ";
580                         }
581                 }
582         }
583
584         if (defined($ctx->{PRIVATE_DEPENDENCIES})) {
585                 foreach (@{$ctx->{PRIVATE_DEPENDENCIES}}) {
586                         next if ($other->{$_}->{ENABLE} eq "NO");
587                         if ($other->{$_}->{TYPE} eq "LIBRARY") {
588                                 s/^LIB//g;
589                                 $_ = lc($_);
590
591                                 $privs .= "$_ ";
592                         } else {
593                                 s/^LIB//g;
594                                 $_ = lc($_);
595
596                                 $privlibs .= "-l$_ ";
597                         }
598                 }
599         }
600
601         smb_build::env::PkgConfig($self,
602                 $path,
603                 $link_name,
604                 "-L\${libdir} -l$link_name",
605                 $privlibs,
606                 "",
607                 "$ctx->{VERSION}",
608                 $ctx->{DESCRIPTION},
609                 defined($ctx->{INIT_FUNCTIONS}),
610                 $pubs,
611                 "",
612                 [
613                         "prefix=$self->{config}->{prefix}",
614                         "exec_prefix=$self->{config}->{exec_prefix}",
615                         "libdir=$self->{config}->{libdir}",
616                         "includedir=$self->{config}->{includedir}"
617                 ]
618         ); 
619         my $abs_srcdir = abs_path($self->{config}->{srcdir});
620         smb_build::env::PkgConfig($self,
621                 "bin/pkgconfig/$link_name-uninstalled.pc",
622                 $link_name,
623                 "-Lbin/shared -Lbin/static -l$link_name",
624                 $privlibs,
625                 join(' ', 
626                         "-I$abs_srcdir",
627                         "-I$abs_srcdir/include",
628                         "-I$abs_srcdir/lib",
629                         "-I$abs_srcdir/lib/replace"),
630                 "$ctx->{VERSION}",
631                 $ctx->{DESCRIPTION},
632                 defined($ctx->{INIT_FUNCTIONS}),
633                 $pubs,
634                 $privs,
635                 [
636                         "prefix=bin/",
637                         "includedir=$ctx->{BASEDIR}"
638                 ]
639         ); 
640 }
641
642 sub ProtoHeader($$)
643 {
644         my ($self,$ctx) = @_;
645
646         my $target = "";
647         my $comment = "Creating ";
648
649         my $priv = undef;
650         my $pub = undef;
651
652         if (defined($ctx->{PRIVATE_PROTO_HEADER})) {
653                 $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER});
654                 $target .= $priv;
655                 $comment .= $priv;
656                 if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
657                         $comment .= " and ";
658                         $target.= " ";
659                 }
660                 push (@{$self->{proto_headers}}, $priv);
661         } else {
662                 $ctx->{PRIVATE_PROTO_HEADER} = $ctx->{PUBLIC_PROTO_HEADER};
663                 $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER});
664         }
665
666         if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
667                 $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER});
668                 $comment .= $pub;
669                 $target .= $pub;
670                 push (@{$self->{proto_headers}}, $pub);
671         } else {
672                 $ctx->{PUBLIC_PROTO_HEADER} = $ctx->{PRIVATE_PROTO_HEADER};
673                 $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER});
674         }
675
676         $self->output("$pub: $ctx->{MK_FILE} \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST:.o=.c) \$(srcdir)/script/mkproto.pl\n");
677         $self->output("\t\@echo \"$comment\"\n");
678         $self->output("\t\@\$(PERL) \$(srcdir)/script/mkproto.pl --srcdir=\$(srcdir) --builddir=\$(builddir) --private=$priv --public=$pub \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)\n\n");
679 }
680
681 sub write($$)
682 {
683         my ($self,$file) = @_;
684
685         $self->output("MANPAGES = ".array2oneperline($self->{manpages})."\n");
686         $self->output("BIN_PROGS = " . array2oneperline($self->{bin_progs}) . "\n");
687         $self->output("SBIN_PROGS = " . array2oneperline($self->{sbin_progs}) . "\n");
688         $self->output("TORTURE_PROGS = " . array2oneperline($self->{torture_progs}) . "\n");
689         $self->output("BINARIES = " . array2oneperline($self->{binaries}) . "\n");
690         $self->output("STATIC_LIBS = " . array2oneperline($self->{static_libs}) . "\n");
691         $self->output("SHARED_LIBS = " . array2oneperline($self->{shared_libs}) . "\n");
692         $self->output("PYTHON_DSOS = " . array2oneperline($self->{python_dsos}) . "\n");
693         $self->output("INSTALLABLE_SHARED_LIBS = " . array2oneperline($self->{installable_shared_libs}) . "\n");
694         $self->output("PUBLIC_HEADERS = " . array2oneperline($self->{headers}) . "\n");
695         $self->output("PC_FILES = " . array2oneperline($self->{pc_files}) . "\n");
696         $self->output("ALL_OBJS = " . array2oneperline($self->{all_objs}) . "\n");
697         $self->output("PROTO_HEADERS = " . array2oneperline($self->{proto_headers}) .  "\n");
698         $self->output("SHARED_MODULES = " . array2oneperline($self->{shared_modules}) . "\n");
699         $self->output("PLUGINS = " . array2oneperline($self->{plugins}) . "\n");
700
701         $self->output("\ninstallplugins: \$(PLUGINS)\n".$self->{install_plugins}."\n");
702         $self->output("\nuninstallplugins:\n".$self->{uninstall_plugins}."\n");
703
704         $self->_prepare_mk_files();
705
706         $self->output($self->{mkfile});
707
708         if ($self->{automatic_deps}) {
709                 $self->output("
710 ifneq (\$(MAKECMDGOALS),clean)
711 ifneq (\$(MAKECMDGOALS),distclean)
712 ifneq (\$(MAKECMDGOALS),realdistclean)
713 ifneq (\$(SKIP_DEP_FILES),yes)
714 -include \$(DEP_FILES)
715 endif
716 endif
717 endif
718 endif
719
720 ifneq (\$(SKIP_DEP_FILES),yes)
721 clean::
722         \@echo Removing dependency files
723         \@find . -name '*.d' -o -name '*.hd' | xargs rm -f
724 endif
725 ");
726         } else {
727                 $self->output("include \$(srcdir)/static_deps.mk\n");
728         }
729
730         open(MAKEFILE,">$file") || die ("Can't open $file\n");
731         print MAKEFILE $self->{output};
732         close(MAKEFILE);
733
734         print __FILE__.": creating $file\n";
735 }
736
737 1;