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