3ac4ad53bc6a118ada7dfb9f26e527f0224fe14b
[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->{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 SharedLibrary($$)
191 {
192         my ($self,$ctx) = @_;
193
194         my $installdir;
195         my $init_obj = "";
196         
197         if ($self->{duplicate_build}) {
198                 $installdir = $ctx->{RELEASEDIR};
199         } else {
200                 $installdir = $ctx->{DEBUGDIR};
201         }
202
203         if ($ctx->{TYPE} eq "LIBRARY") {
204                 push (@{$self->{shared_libs}}, "$ctx->{DEBUGDIR}/$ctx->{LIBRARY_REALNAME}");
205                 push (@{$self->{installable_shared_libs}}, "$installdir/$ctx->{LIBRARY_REALNAME}");
206         } elsif ($ctx->{TYPE} eq "MODULE") {
207                 push (@{$self->{shared_modules}}, "$ctx->{DEBUGDIR}/$ctx->{LIBRARY_REALNAME}");
208                 push (@{$self->{plugins}}, "$installdir/$ctx->{LIBRARY_REALNAME}");
209
210                 my $fixedname = $ctx->{NAME};
211
212                 $fixedname =~ s/^$ctx->{SUBSYSTEM}_//g;
213
214                 $self->{install_plugins} .= "\t\@echo Installing $installdir/$ctx->{LIBRARY_REALNAME} as \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$fixedname.\$(SHLIBEXT)\n";
215                 $self->{install_plugins} .= "\t\@mkdir -p \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/\n";
216                 $self->{install_plugins} .= "\t\@cp $installdir/$ctx->{LIBRARY_REALNAME} \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$fixedname.\$(SHLIBEXT)\n";
217                 $self->{uninstall_plugins} .= "\t\@echo Uninstalling \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$fixedname.\$(SHLIBEXT)\n";
218                 $self->{uninstall_plugins} .= "\t\@-rm \$(DESTDIR)\$(MODULESDIR)/$ctx->{SUBSYSTEM}/$fixedname.\$(SHLIBEXT)\n";
219         }
220
221         $self->_prepare_list($ctx, "OBJ_LIST");
222         $self->_prepare_list($ctx, "CFLAGS");
223         $self->_prepare_list($ctx, "DEPEND_LIST");
224         $self->_prepare_list($ctx, "LINK_LIST");
225         $self->_prepare_list($ctx, "LINK_FLAGS");
226
227         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)");
228
229         if ($ctx->{TYPE} eq "MODULE" and defined($ctx->{INIT_FUNCTION})) {
230                 my $init_fn = $ctx->{INIT_FUNCTION_TYPE};
231                 $init_fn =~ s/\(\*\)/init_module/;
232                 my $proto_fn = $ctx->{INIT_FUNCTION_TYPE};
233                 $proto_fn =~ s/\(\*\)/$ctx->{INIT_FUNCTION}/;
234
235                 $self->output(<< "__EOD__"
236 bin/$ctx->{NAME}_init_module.c:
237         \@echo Creating \$\@
238         \@echo \"#include \\\"includes.h\\\"\" > \$\@
239         \@echo \"$proto_fn;\" >> \$\@
240         \@echo -e \"_PUBLIC_ $init_fn \\n{\\n\\treturn $ctx->{INIT_FUNCTION}();\\n}\\n\" >> \$\@
241 __EOD__
242 );
243                 $init_obj = "bin/$ctx->{NAME}_init_module.o";
244         }
245
246         my $soarg = "";
247         my $soargdebug = "";
248         if ($self->{config}->{SONAMEFLAG} ne "" and 
249                 defined($ctx->{LIBRARY_SONAME})) {
250                 $soarg = "$self->{config}->{SONAMEFLAG}$ctx->{LIBRARY_SONAME} ";
251         }
252
253         if ($self->{config}->{SONAMEFLAG} ne "") {
254                 $soargdebug = "$self->{config}->{SONAMEFLAG}$ctx->{LIBRARY_REALNAME} ";
255         }
256
257         if ($self->{duplicate_build}) {
258                 $self->output(<< "__EOD__"
259 #
260
261 $ctx->{TARGET}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) $init_obj
262         \@echo Linking \$\@
263         \@mkdir -p $ctx->{DEBUGDIR}
264         \@\$(SHLD) \$(SHLD_FLAGS) -o \$\@ \$(LOCAL_LINK_FLAGS) \\
265                 \$($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) $soargdebug \\
266                 $init_obj \$($ctx->{TYPE}_$ctx->{NAME}_LINK_LIST)
267
268 __EOD__
269 );
270         }
271
272         $self->output(<< "__EOD__"
273 #
274
275 $installdir/$ctx->{LIBRARY_REALNAME}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) $init_obj
276         \@echo Linking \$\@
277         \@\$(SHLD) \$(SHLD_FLAGS) -o \$\@ \\
278                 \$($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) $soarg \\
279                 $init_obj \$($ctx->{TYPE}_$ctx->{NAME}_LINK_LIST)
280
281 __EOD__
282 );
283 }
284
285 sub MergedObj($$)
286 {
287         my ($self,$ctx) = @_;
288
289         return unless $ctx->{TARGET};
290
291         $self->_prepare_list($ctx, "OBJ_LIST");
292         $self->_prepare_list($ctx, "CFLAGS");
293         $self->_prepare_list($ctx, "DEPEND_LIST");
294
295         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)");
296                 
297         $self->output("$ctx->{TARGET}: \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)\n");
298
299         $self->output("\t\@echo \"Pre-Linking $ctx->{TYPE} $ctx->{NAME}\"\n");
300         $self->output("\t@\$(LD) -r \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) -o $ctx->{TARGET}\n");
301         $self->output("\n");
302 }
303
304 sub ObjList($$)
305 {
306         my ($self,$ctx) = @_;
307
308         return unless $ctx->{TARGET};
309
310         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)");
311                 
312         $self->_prepare_list($ctx, "OBJ_LIST");
313         $self->_prepare_list($ctx, "CFLAGS");
314         $self->_prepare_list($ctx, "DEPEND_LIST");
315         $self->output("$ctx->{TARGET}: ");
316         $self->output("\$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)\n");
317         $self->output("\t\@touch $ctx->{TARGET}\n");
318 }
319
320 sub StaticLibrary($$)
321 {
322         my ($self,$ctx) = @_;
323
324         push (@{$self->{static_libs}}, $ctx->{OUTPUT});
325
326         $self->_prepare_list($ctx, "OBJ_LIST");
327         $self->_prepare_list($ctx, "CFLAGS");
328
329         $self->_prepare_list($ctx, "DEPEND_LIST");
330         $self->_prepare_list($ctx, "LINK_LIST");
331         $self->_prepare_list($ctx, "LINK_FLAGS");
332
333         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)");
334                 
335         $self->output(<< "__EOD__"
336 #
337 $ctx->{TARGET}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) 
338         \@echo Linking \$@
339         \@\$(STLD) \$(STLD_FLAGS) \$@ \\
340                 \$($ctx->{TYPE}_$ctx->{NAME}_LINK_LIST)
341
342 __EOD__
343 );
344 }
345
346 sub Header($$)
347 {
348         my ($self,$ctx) = @_;
349
350         foreach (@{$ctx->{PUBLIC_HEADERS}}) {
351                 push (@{$self->{headers}}, "$ctx->{BASEDIR}/$_");
352         }
353 }
354
355 sub Binary($$)
356 {
357         my ($self,$ctx) = @_;
358
359         my $installdir;
360         
361         if ($self->{duplicate_build}) {
362                 $installdir = "bin/install";
363         } else {
364                 $installdir = "bin";
365         }
366
367         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)");
368                 
369         unless (defined($ctx->{INSTALLDIR})) {
370         } elsif ($ctx->{INSTALLDIR} eq "SBINDIR") {
371                 push (@{$self->{sbin_progs}}, "$installdir/$ctx->{BINARY}");
372         } elsif ($ctx->{INSTALLDIR} eq "BINDIR") {
373                 push (@{$self->{bin_progs}}, "$installdir/$ctx->{BINARY}");
374         }
375
376         push (@{$self->{binaries}}, "bin/$ctx->{BINARY}");
377
378         $self->_prepare_list($ctx, "OBJ_LIST");
379         $self->_prepare_list($ctx, "CFLAGS");
380         $self->_prepare_list($ctx, "DEPEND_LIST");
381         $self->_prepare_list($ctx, "LINK_LIST");
382         $self->_prepare_list($ctx, "LINK_FLAGS");
383
384         if ($self->{duplicate_build}) {
385         $self->output(<< "__EOD__"
386 #
387 bin/$ctx->{BINARY}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) 
388         \@echo Linking \$\@
389         \@\$(CC) \$(LDFLAGS) -o \$\@ \$(LOCAL_LINK_FLAGS) \$(INSTALL_LINK_FLAGS) \\
390                 \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_LIST) \\
391                 \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) 
392
393 __EOD__
394 );
395         }
396
397 $self->output(<< "__EOD__"
398 $installdir/$ctx->{BINARY}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST) 
399         \@echo Linking \$\@
400         \@\$(CC) \$(LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
401                 \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_LIST) \\
402                 \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) 
403
404 __EOD__
405 );
406 }
407
408 sub Manpage($$)
409 {
410         my ($self,$ctx) = @_;
411
412         my $dir = $ctx->{BASEDIR};
413         
414         $dir =~ s/^\.\///g;
415
416         push (@{$self->{manpages}}, "$dir/$ctx->{MANPAGE}");
417 }
418
419 sub PkgConfig($$)
420 {
421         my ($self,$ctx) = @_;
422         
423         my $link_name = $ctx->{NAME};
424
425         $link_name =~ s/^LIB//g;
426         $link_name = lc($link_name);
427
428         if (not defined($ctx->{DESCRIPTION})) {
429                 warn("$ctx->{NAME} has not DESCRIPTION set, not generating .pc file");
430                 return;
431         }
432
433         my $path = "$ctx->{BASEDIR}/$link_name.pc";
434
435         push (@{$self->{pc_files}}, $path);
436
437         smb_build::env::PkgConfig($self,
438                 $path,
439                 $link_name,
440                 "-l$link_name",
441                 "",
442                 "$ctx->{VERSION}",
443                 $ctx->{DESCRIPTION},
444                 1
445         ); 
446 }
447
448 sub ProtoHeader($$)
449 {
450         my ($self,$ctx) = @_;
451
452         my $dir = $ctx->{BASEDIR};
453
454         $dir =~ s/^\.\///g;
455
456         my $comment = "Creating ";
457         if (defined($ctx->{PRIVATE_PROTO_HEADER})) {
458                 $comment.= "$dir/$ctx->{PRIVATE_PROTO_HEADER}";
459                 if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
460                         $comment .= " and ";
461                 }
462                 push (@{$self->{proto_headers}}, "$dir/$ctx->{PRIVATE_PROTO_HEADER}");
463         } else {
464                 $ctx->{PRIVATE_PROTO_HEADER} = $ctx->{PUBLIC_PROTO_HEADER};
465         }
466         
467         if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
468                 $comment.= "$dir/$ctx->{PUBLIC_PROTO_HEADER}";
469                 push (@{$self->{proto_headers}}, "$dir/$ctx->{PUBLIC_PROTO_HEADER}");
470         } else {
471                 $ctx->{PUBLIC_PROTO_HEADER} = $ctx->{PRIVATE_PROTO_HEADER};
472         }       
473
474         $self->output("$dir/$ctx->{PUBLIC_PROTO_HEADER}: \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST:.o=.c)\n");
475         $self->output("\t\@echo \"$comment\"\n");
476
477         $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");
478 }
479
480 sub write($$)
481 {
482         my ($self,$file) = @_;
483
484         $self->output("MANPAGES = ".array2oneperline($self->{manpages})."\n");
485         $self->output("BIN_PROGS = " . array2oneperline($self->{bin_progs}) . "\n");
486         $self->output("SBIN_PROGS = " . array2oneperline($self->{sbin_progs}) . "\n");
487         $self->output("BINARIES = " . array2oneperline($self->{binaries}) . "\n");
488         $self->output("STATIC_LIBS = " . array2oneperline($self->{static_libs}) . "\n");
489         $self->output("SHARED_LIBS = " . array2oneperline($self->{shared_libs}) . "\n");
490         $self->output("INSTALLABLE_SHARED_LIBS = " . array2oneperline($self->{installable_shared_libs}) . "\n");
491         $self->output("PUBLIC_HEADERS = " . array2oneperline($self->{headers}) . "\n");
492         $self->output("PC_FILES = " . array2oneperline($self->{pc_files}) . "\n");
493         $self->output("ALL_OBJS = " . array2oneperline($self->{all_objs}) . "\n");
494         $self->output("PROTO_HEADERS = " . array2oneperline($self->{proto_headers}) .  "\n");
495         $self->output("SHARED_MODULES = " . array2oneperline($self->{shared_modules}) . "\n");
496         $self->output("PLUGINS = " . array2oneperline($self->{plugins}) . "\n");
497
498         $self->output("\ninstallplugins: \$(PLUGINS)\n".$self->{install_plugins}."\n");
499         $self->output("\nuninstallplugins:\n".$self->{uninstall_plugins}."\n");
500
501         # nasty hack to allow running locally
502         if ($self->{duplicate_build}) {
503                 $self->output("bin/libdynconfig.\$(SHLIBEXT): dynconfig-devel.o\n");
504                 $self->output("bin/libdynconfig.\$(SHLIBEXT): LIBRARY_DYNCONFIG_OBJ_LIST=dynconfig-devel.o\n");
505         }
506
507         $self->_prepare_mk_files();
508
509         $self->output($self->{mkfile});
510
511         if ($self->{developer}) {
512                 $self->output("-include \$(DEP_FILES)\n");
513         } else {
514                 $self->output("include static_deps.mk\n");
515         }
516
517         open(MAKEFILE,">$file") || die ("Can't open $file\n");
518         print MAKEFILE $self->{output};
519         close(MAKEFILE);
520
521         print __FILE__.": creating $file\n";
522 }
523
524 1;