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