r14543: Fix some build dependency problems caused by the use of $(builddir)
[bbaumbach/samba-autobuild/.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 strict;
11
12 use base 'smb_build::env';
13
14 sub new($$$)
15 {
16         my ($myname, $config, $mkfile) = @_;
17         my $self = new smb_build::env($config);
18         
19         bless($self, $myname);
20
21         $self->{manpages} = [];
22         $self->{sbin_progs} = [];
23         $self->{bin_progs} = [];
24         $self->{static_libs} = [];
25         $self->{shared_libs} = [];
26         $self->{installable_shared_libs} = [];
27         $self->{headers} = [];
28         $self->{shared_modules} = [];
29         $self->{plugins} = [];
30         $self->{install_plugins} = "";
31         $self->{uninstall_plugins} = "";
32         $self->{pc_files} = [];
33         $self->{proto_headers} = [];
34         $self->{output} = "";
35
36         $self->{mkfile} = $mkfile;
37
38         $self->output("#!gmake\n");
39         $self->output("################################################\n");
40         $self->output("# Autogenerated by build/smb_build/makefile.pm #\n");
41         $self->output("################################################\n");
42         $self->output("\n");
43
44         $self->output("default: all\n\n");
45
46         $self->_prepare_path_vars();
47         $self->_prepare_compiler_linker();
48
49         return $self;
50 }
51
52 sub output($$)
53 {
54         my ($self, $text) = @_;
55
56         $self->{output} .= $text;
57 }
58
59 sub _prepare_path_vars($)
60 {
61         my ($self) = @_;
62
63         $self->output(<< "__EOD__"
64 prefix = $self->{config}->{prefix}
65 exec_prefix = $self->{config}->{exec_prefix}
66 selftest_prefix = $self->{config}->{selftest_prefix}
67 VPATH = $self->{config}->{srcdir}
68 srcdir = $self->{config}->{srcdir}
69 builddir = $self->{config}->{builddir}
70
71 BASEDIR = $self->{config}->{prefix}
72 BINDIR = $self->{config}->{bindir}
73 SBINDIR = $self->{config}->{sbindir}
74 LIBDIR = $self->{config}->{libdir}
75 MODULESDIR = $self->{config}->{modulesdir}
76 INCLUDEDIR = $self->{config}->{includedir}
77 CONFIGDIR = $self->{config}->{sysconfdir}
78 DATADIR = $self->{config}->{datadir}
79 SWATDIR = $self->{config}->{datadir}/swat
80 JSDIR = $self->{config}->{datadir}/js
81 SETUPDIR = $self->{config}->{datadir}/setup
82 VARDIR = $self->{config}->{localstatedir}
83 LOGFILEBASE = $self->{config}->{logfilebase}
84 NCALRPCDIR = $self->{config}->{localstatedir}/ncalrpc
85 LOCKDIR = $self->{config}->{lockdir}
86 PIDDIR = $self->{config}->{piddir}
87 MANDIR = $self->{config}->{mandir}
88 PRIVATEDIR = $self->{config}->{privatedir}
89 WINBINDD_SOCKET_DIR = $self->{config}->{winbindd_socket_dir}
90
91 __EOD__
92 );
93 }
94
95 sub _prepare_compiler_linker($)
96 {
97         my ($self) = @_;
98
99         my $devld_local = "";
100         my $devld_install = "";
101
102         $self->{duplicate_build} = 0;
103         if ($self->{config}->{LIBRARY_OUTPUT_TYPE} eq "SHARED_LIBRARY") {
104                 if ($self->{developer}) {
105                         $self->{duplicate_build} = 1;
106                         $devld_local = " -Wl,-rpath,\$(builddir)/bin";
107                 }
108                 $devld_install = " -Wl,-rpath-link,\$(builddir)/bin";
109         }
110
111         $self->output(<< "__EOD__"
112 SHELL=$self->{config}->{SHELL}
113
114 PERL=$self->{config}->{PERL}
115
116 CPP=$self->{config}->{CPP}
117 CPPFLAGS=$self->{config}->{CPPFLAGS}
118
119 CC=$self->{config}->{CC}
120 CFLAGS=-I\$(srcdir)/include -I\$(srcdir) -I\$(srcdir)/lib -D_SAMBA_BUILD_ -DHAVE_CONFIG_H $self->{config}->{CFLAGS} \$(CPPFLAGS)
121 PICFLAG=$self->{config}->{PICFLAG}
122 HOSTCC=$self->{config}->{HOSTCC}
123
124 LOCAL_LINK_FLAGS=$devld_local
125 INSTALL_LINK_FLAGS=$devld_install
126
127 LD=$self->{config}->{LD} 
128 LDFLAGS=$self->{config}->{LDFLAGS} -L\$(builddir)/bin
129
130 STLD=$self->{config}->{AR}
131 STLD_FLAGS=-rc -L\$(builddir)/bin
132
133 SHLD=$self->{config}->{CC}
134 SHLD_FLAGS=$self->{config}->{LDSHFLAGS} -L\$(builddir)/bin
135 SHLIBEXT=$self->{config}->{SHLIBEXT}
136
137 XSLTPROC=$self->{config}->{XSLTPROC}
138
139 LEX=$self->{config}->{LEX}
140 YACC=$self->{config}->{YACC}
141 YAPP=$self->{config}->{YAPP}
142 PIDL_ARGS=$self->{config}->{PIDL_ARGS}
143
144 GCOV=$self->{config}->{GCOV}
145
146 DEFAULT_TEST_TARGET=$self->{config}->{DEFAULT_TEST_TARGET}
147
148 __EOD__
149 );
150 }
151
152 sub _prepare_mk_files($)
153 {
154         my $self = shift;
155         my @tmp = ();
156
157         
158         foreach (@smb_build::config_mk::parsed_files) {
159                 s/ .*$//g;
160                 push (@tmp, $_);
161         }
162
163         $self->output("MK_FILES = " . array2oneperline(\@tmp) . "\n");
164 }
165
166 sub array2oneperline($)
167 {
168         my $array = shift;
169         my $output = "";
170
171         foreach (@$array) {
172                 next unless defined($_);
173
174                 $output .= " \\\n\t\t$_";
175         }
176
177         return $output;
178 }
179
180 sub _prepare_list($$$)
181 {
182         my ($self,$ctx,$var) = @_;
183
184         my $tmplist = array2oneperline($ctx->{$var});
185         return if ($tmplist eq "");
186
187         $self->output("$ctx->{TYPE}\_$ctx->{NAME}_$var =$tmplist\n");
188 }
189
190 sub DependencyInfo($$)
191 {
192         my ($self,$ctx) = @_;
193
194         $self->output("bin/deps/$ctx->{TYPE}_$ctx->{NAME}: \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST:.o=.c)");
195         $self->output("\n");
196         $self->output("\t\@echo \"Generating dependency info for $ctx->{NAME}\"\n");
197         $self->output("\t\@./script/cdeps.pl \$^ > \$@\n");
198         $self->output("\n");
199         $self->output("-include bin/deps/$ctx->{TYPE}_$ctx->{NAME}\n\n");
200 }
201
202 sub SharedLibrary($$)
203 {
204         my ($self,$ctx) = @_;
205
206         my $installdir;
207         my $init_obj = "";
208         
209         if ($self->{duplicate_build}) {
210                 $installdir = $ctx->{RELEASEDIR};
211         } else {
212                 $installdir = $ctx->{DEBUGDIR};
213         }
214
215         if ($ctx->{TYPE} eq "LIBRARY") {
216                 push (@{$self->{shared_libs}}, "$ctx->{DEBUGDIR}/$ctx->{LIBRARY_REALNAME}");
217                 push (@{$self->{installable_shared_libs}}, "$installdir/$ctx->{LIBRARY_REALNAME}");
218         } elsif ($ctx->{TYPE} eq "MODULE") {
219                 push (@{$self->{shared_modules}}, "$ctx->{DEBUGDIR}/$ctx->{LIBRARY_REALNAME}");
220                 push (@{$self->{plugins}}, "$installdir/$ctx->{LIBRARY_REALNAME}");
221
222                 my $fixedname = $ctx->{NAME};
223
224                 $fixedname =~ s/^$ctx->{SUBSYSTEM}_//g;
225
226                 $self->{install_plugins} .= "\t\@echo Installing $installdir/$ctx->{LIBRARY_REALNAME} as \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$fixedname.\$(SHLIBEXT)\n";
227                 $self->{install_plugins} .= "\t\@mkdir -p \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/\n";
228                 $self->{install_plugins} .= "\t\@cp $installdir/$ctx->{LIBRARY_REALNAME} \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$fixedname.\$(SHLIBEXT)\n";
229                 $self->{uninstall_plugins} .= "\t\@echo Uninstalling \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$fixedname.\$(SHLIBEXT)\n";
230                 $self->{uninstall_plugins} .= "\t\@-rm \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$fixedname.\$(SHLIBEXT)\n";
231         }
232
233         $self->_prepare_list($ctx, "OBJ_LIST");
234         $self->_prepare_list($ctx, "CFLAGS");
235         $self->_prepare_list($ctx, "DEPEND_LIST");
236         $self->_prepare_list($ctx, "LINK_LIST");
237         $self->_prepare_list($ctx, "LINK_FLAGS");
238
239         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)");
240
241         if ($ctx->{TYPE} eq "MODULE" and defined($ctx->{INIT_FUNCTION})) {
242                 my $init_fn = $ctx->{INIT_FUNCTION_TYPE};
243                 $init_fn =~ s/\(\*\)/init_module/;
244                 my $proto_fn = $ctx->{INIT_FUNCTION_TYPE};
245                 $proto_fn =~ s/\(\*\)/$ctx->{INIT_FUNCTION}/;
246
247                 $self->output(<< "__EOD__"
248 bin/$ctx->{NAME}_init_module.c:
249         \@echo Creating \$\@
250         \@echo \"#include \\\"includes.h\\\"\" > \$\@
251         \@echo \"$proto_fn;\" >> \$\@
252         \@echo -e \"_PUBLIC_ $init_fn \\n{\\n\\treturn $ctx->{INIT_FUNCTION}();\\n}\\n\" >> \$\@
253 __EOD__
254 );
255                 $init_obj = "bin/$ctx->{NAME}_init_module.o";
256         }
257
258         my $soarg = "";
259         my $soargdebug = "";
260         if ($self->{config}->{SONAMEFLAG} ne "" and 
261                 defined($ctx->{LIBRARY_SONAME})) {
262                 $soarg = "$self->{config}->{SONAMEFLAG}$ctx->{LIBRARY_SONAME} ";
263         }
264
265         if ($self->{config}->{SONAMEFLAG} ne "") {
266                 $soargdebug = "$self->{config}->{SONAMEFLAG}$ctx->{LIBRARY_REALNAME} ";
267         }
268
269         if ($self->{duplicate_build}) {
270                 $self->output(<< "__EOD__"
271 #
272
273 $ctx->{TARGET}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) $init_obj
274         \@echo Linking \$\@
275         \@mkdir -p $ctx->{DEBUGDIR}
276         \@\$(SHLD) \$(SHLD_FLAGS) -o \$\@ \$(LOCAL_LINK_FLAGS) \\
277                 \$($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) $soargdebug \\
278                 $init_obj \$($ctx->{TYPE}_$ctx->{NAME}_LINK_LIST)
279
280 __EOD__
281 );
282         }
283
284         $self->output(<< "__EOD__"
285 #
286
287 $installdir/$ctx->{LIBRARY_REALNAME}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) $init_obj
288         \@echo Linking \$\@
289         \@\$(SHLD) \$(SHLD_FLAGS) -o \$\@ \\
290                 \$($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) $soarg \\
291                 $init_obj \$($ctx->{TYPE}_$ctx->{NAME}_LINK_LIST)
292
293 __EOD__
294 );
295 }
296
297 sub MergedObj($$)
298 {
299         my ($self,$ctx) = @_;
300
301         return unless $ctx->{TARGET};
302
303         $self->_prepare_list($ctx, "OBJ_LIST");
304         $self->_prepare_list($ctx, "CFLAGS");
305         $self->_prepare_list($ctx, "DEPEND_LIST");
306
307         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)");
308                 
309         $self->output("$ctx->{TARGET}: \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)\n");
310
311         $self->output("\t\@echo \"Pre-Linking $ctx->{TYPE} $ctx->{NAME}\"\n");
312         $self->output("\t@\$(LD) -r \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) -o $ctx->{TARGET}\n");
313         $self->output("\n");
314 }
315
316 sub ObjList($$)
317 {
318         my ($self,$ctx) = @_;
319
320         return unless $ctx->{TARGET};
321
322         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)");
323                 
324         $self->_prepare_list($ctx, "OBJ_LIST");
325         $self->_prepare_list($ctx, "CFLAGS");
326         $self->_prepare_list($ctx, "DEPEND_LIST");
327         $self->output("$ctx->{TARGET}: ");
328         $self->output("\$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)\n");
329         $self->output("\t\@touch $ctx->{TARGET}\n");
330 }
331
332 sub StaticLibrary($$)
333 {
334         my ($self,$ctx) = @_;
335
336         push (@{$self->{static_libs}}, $ctx->{OUTPUT});
337
338         $self->_prepare_list($ctx, "OBJ_LIST");
339         $self->_prepare_list($ctx, "CFLAGS");
340
341         $self->_prepare_list($ctx, "DEPEND_LIST");
342         $self->_prepare_list($ctx, "LINK_LIST");
343         $self->_prepare_list($ctx, "LINK_FLAGS");
344
345         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)");
346                 
347         $self->output(<< "__EOD__"
348 #
349 $ctx->{TARGET}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) 
350         \@echo Linking \$@
351         \@\$(STLD) \$(STLD_FLAGS) \$@ \\
352                 \$($ctx->{TYPE}_$ctx->{NAME}_LINK_LIST)
353
354 __EOD__
355 );
356 }
357
358 sub Header($$)
359 {
360         my ($self,$ctx) = @_;
361
362         foreach (@{$ctx->{PUBLIC_HEADERS}}) {
363                 push (@{$self->{headers}}, "$ctx->{BASEDIR}/$_");
364         }
365 }
366
367 sub Binary($$)
368 {
369         my ($self,$ctx) = @_;
370
371         my $installdir;
372         
373         if ($self->{duplicate_build}) {
374                 $installdir = "bin/install";
375         } else {
376                 $installdir = "bin";
377         }
378
379         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)");
380                 
381         unless (defined($ctx->{INSTALLDIR})) {
382         } elsif ($ctx->{INSTALLDIR} eq "SBINDIR") {
383                 push (@{$self->{sbin_progs}}, "$installdir/$ctx->{BINARY}");
384         } elsif ($ctx->{INSTALLDIR} eq "BINDIR") {
385                 push (@{$self->{bin_progs}}, "$installdir/$ctx->{BINARY}");
386         }
387
388         push (@{$self->{binaries}}, "bin/$ctx->{BINARY}");
389
390         $self->_prepare_list($ctx, "OBJ_LIST");
391         $self->_prepare_list($ctx, "CFLAGS");
392         $self->_prepare_list($ctx, "DEPEND_LIST");
393         $self->_prepare_list($ctx, "LINK_LIST");
394         $self->_prepare_list($ctx, "LINK_FLAGS");
395
396         if ($self->{duplicate_build}) {
397         $self->output(<< "__EOD__"
398 #
399 bin/$ctx->{BINARY}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) 
400         \@echo Linking \$\@
401         \@\$(CC) \$(LDFLAGS) -o \$\@ \$(LOCAL_LINK_FLAGS) \$(INSTALL_LINK_FLAGS) \\
402                 \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_LIST) \\
403                 \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) 
404
405 __EOD__
406 );
407         }
408
409 $self->output(<< "__EOD__"
410 $installdir/$ctx->{BINARY}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) 
411         \@echo Linking \$\@
412         \@\$(CC) \$(LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
413                 \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_LIST) \\
414                 \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) 
415
416 __EOD__
417 );
418 }
419
420 sub Manpage($$)
421 {
422         my ($self,$ctx) = @_;
423
424         my $dir = $ctx->{BASEDIR};
425         
426         $dir =~ s/^\.\///g;
427
428         push (@{$self->{manpages}}, "$dir/$ctx->{MANPAGE}");
429 }
430
431 sub PkgConfig($$)
432 {
433         my ($self,$ctx) = @_;
434         
435         my $link_name = $ctx->{NAME};
436
437         $link_name =~ s/^LIB//g;
438         $link_name = lc($link_name);
439
440         if (not defined($ctx->{DESCRIPTION})) {
441                 warn("$ctx->{NAME} has not DESCRIPTION set, not generating .pc file");
442                 return;
443         }
444
445         my $path = "$ctx->{BASEDIR}/$link_name.pc";
446
447         push (@{$self->{pc_files}}, $path);
448
449         smb_build::env::PkgConfig($self,
450                 $path,
451                 $link_name,
452                 "-l$link_name",
453                 "",
454                 "$ctx->{VERSION}",
455                 $ctx->{DESCRIPTION},
456                 1
457         ); 
458 }
459
460 sub ProtoHeader($$)
461 {
462         my ($self,$ctx) = @_;
463
464         my $dir = $ctx->{BASEDIR};
465
466         $dir =~ s/^\.\///g;
467
468         my $comment = "Creating ";
469         if (defined($ctx->{PRIVATE_PROTO_HEADER})) {
470                 $comment.= "$dir/$ctx->{PRIVATE_PROTO_HEADER}";
471                 if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
472                         $comment .= " and ";
473                 }
474                 push (@{$self->{proto_headers}}, "$dir/$ctx->{PRIVATE_PROTO_HEADER}");
475         } else {
476                 $ctx->{PRIVATE_PROTO_HEADER} = $ctx->{PUBLIC_PROTO_HEADER};
477         }
478         
479         if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
480                 $comment.= "$dir/$ctx->{PUBLIC_PROTO_HEADER}";
481                 push (@{$self->{proto_headers}}, "$dir/$ctx->{PUBLIC_PROTO_HEADER}");
482         } else {
483                 $ctx->{PUBLIC_PROTO_HEADER} = $ctx->{PRIVATE_PROTO_HEADER};
484         }       
485
486         $self->output("$dir/$ctx->{PUBLIC_PROTO_HEADER}: \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST:.o=.c)\n");
487         $self->output("\t\@echo \"$comment\"\n");
488
489         $self->output("\t\@\$(PERL) \$(srcdir)/script/mkproto.pl --private=$dir/$ctx->{PRIVATE_PROTO_HEADER} --public=$dir/$ctx->{PUBLIC_PROTO_HEADER} \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)\n\n");
490 }
491
492 sub write($$)
493 {
494         my ($self,$file) = @_;
495
496         $self->output("MANPAGES = ".array2oneperline($self->{manpages})."\n");
497         $self->output("BIN_PROGS = " . array2oneperline($self->{bin_progs}) . "\n");
498         $self->output("SBIN_PROGS = " . array2oneperline($self->{sbin_progs}) . "\n");
499         $self->output("BINARIES = " . array2oneperline($self->{binaries}) . "\n");
500         $self->output("STATIC_LIBS = " . array2oneperline($self->{static_libs}) . "\n");
501         $self->output("SHARED_LIBS = " . array2oneperline($self->{shared_libs}) . "\n");
502         $self->output("INSTALLABLE_SHARED_LIBS = " . array2oneperline($self->{installable_shared_libs}) . "\n");
503         $self->output("PUBLIC_HEADERS = " . array2oneperline($self->{headers}) . "\n");
504         $self->output("PC_FILES = " . array2oneperline($self->{pc_files}) . "\n");
505         $self->output("ALL_OBJS = " . array2oneperline($self->{all_objs}) . "\n");
506         $self->output("PROTO_HEADERS = " . array2oneperline($self->{proto_headers}) .  "\n");
507         $self->output("SHARED_MODULES = " . array2oneperline($self->{shared_modules}) . "\n");
508         $self->output("PLUGINS = " . array2oneperline($self->{plugins}) . "\n");
509
510         $self->output("\ninstallplugins: \$(PLUGINS)\n".$self->{install_plugins}."\n");
511         $self->output("\nuninstallplugins:\n".$self->{uninstall_plugins}."\n");
512
513         # nasty hack to allow running locally
514         if ($self->{duplicate_build}) {
515                 $self->output("bin/libdynconfig.\$(SHLIBEXT): dynconfig-devel.o\n");
516                 $self->output("bin/libdynconfig.\$(SHLIBEXT): LIBRARY_DYNCONFIG_OBJ_LIST=dynconfig-devel.o\n");
517         }
518
519         $self->_prepare_mk_files();
520
521         if ($self->{developer}) {
522                 $self->output(<<__EOD__
523
524 #-include \$(ALL_OBJS:.o=.d)
525
526 __EOD__
527 );
528         }
529
530         $self->output($self->{mkfile});
531
532         open(MAKEFILE,">$file") || die ("Can't open $file\n");
533         print MAKEFILE $self->{output};
534         close(MAKEFILE);
535
536         print __FILE__.": creating $file\n";
537 }
538
539 1;