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