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