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