Avoid autogenerating the Makefile but rather include a data.mk file
[jelmer/samba4-debian.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 smb_build::output;
11 use File::Basename;
12 use strict;
13
14 use base 'smb_build::env';
15 use Cwd 'abs_path';
16
17 sub new($$$)
18 {
19         my ($myname, $config, $mkfile) = @_;
20         my $self = new smb_build::env($config);
21         
22         bless($self, $myname);
23
24         $self->{manpages} = [];
25         $self->{sbin_progs} = [];
26         $self->{bin_progs} = [];
27         $self->{torture_progs} = [];
28         $self->{static_libs} = [];
29         $self->{python_dsos} = [];
30         $self->{python_pys} = [];
31         $self->{shared_libs} = [];
32         $self->{installable_shared_libs} = [];
33         $self->{headers} = [];
34         $self->{shared_modules} = [];
35         $self->{plugins} = [];
36         $self->{install_plugins} = "";
37         $self->{uninstall_plugins} = "";
38         $self->{pc_files} = [];
39         $self->{proto_headers} = [];
40         $self->{output} = "";
41
42         $self->{mkfile} = $mkfile;
43
44         $self->output("################################################\n");
45         $self->output("# Autogenerated by build/smb_build/makefile.pm #\n");
46         $self->output("################################################\n");
47         $self->output("\n");
48
49         $self->_prepare_compiler_linker();
50
51         if (!$self->{automatic_deps}) {
52                 $self->output("ALL_PREDEP = proto\n");
53                 $self->output(".NOTPARALLEL:\n");
54         }
55
56         return $self;
57 }
58
59 sub output($$)
60 {
61         my ($self, $text) = @_;
62
63         $self->{output} .= $text;
64 }
65
66 sub _prepare_compiler_linker($)
67 {
68         my ($self) = @_;
69
70         my $builddir_headers = "";
71         my $libdir;
72         my $extra_link_flags = "";
73
74         if ($self->{config}->{USESHARED} eq "true") {
75                 $libdir = "\$(builddir)/bin/shared";
76                 $extra_link_flags = "-Wl,-rpath-link,\$(builddir)/bin/shared";
77         } else {
78                 $libdir = "\$(builddir)/bin/static";
79         }
80         
81         if (!(abs_path($self->{config}->{srcdir}) eq abs_path($self->{config}->{builddir}))) {
82             $builddir_headers= "-I\$(builddir)/include -I\$(builddir) -I\$(builddir)/lib ";
83         }
84
85         $self->output(<< "__EOD__"
86
87 CPPFLAGS=$builddir_headers-I\$(srcdir)/include -I\$(srcdir) -I\$(srcdir)/lib -I\$(srcdir)/lib/replace -I\$(srcdir)/lib/talloc -D_SAMBA_BUILD_=4 -DHAVE_CONFIG_H $self->{config}->{CPPFLAGS}
88
89 INSTALL_LINK_FLAGS=$extra_link_flags
90
91 INTERN_LDFLAGS = -L$libdir
92 __EOD__
93 );
94 }
95
96 sub _prepare_mk_files($)
97 {
98         my $self = shift;
99         my @tmp = ();
100
101         foreach (@smb_build::config_mk::parsed_files) {
102                 s/ .*$//g;
103                 push (@tmp, $_);
104         }
105
106         if ($self->{gnu_make}) {
107                 $self->output("
108 ifneq (\$(MAKECMDGOALS),clean)
109 ifneq (\$(MAKECMDGOALS),distclean)
110 ifneq (\$(MAKECMDGOALS),realdistclean)
111 ");
112         }
113
114         $self->output("MK_FILES = " . array2oneperline(\@tmp) . "\n");
115
116         if ($self->{gnu_make}) {
117                 $self->output("
118 endif
119 endif
120 endif
121 ");
122         }
123 }
124
125 sub array2oneperline($)
126 {
127         my $array = shift;
128         my $output = "";
129
130         foreach (@$array) {
131                 next unless defined($_);
132
133                 $output .= " \\\n\t\t$_";
134         }
135
136         return $output;
137 }
138
139 sub _prepare_list_ex($$$$$)
140 {
141         my ($self,$ctx,$var,$pre,$post) = @_;
142         my @tmparr = ();
143
144         push(@tmparr, $pre) if defined($pre);
145         push(@tmparr, @{$ctx->{$var}}) if defined($ctx->{$var});
146         push(@tmparr, $post) if defined($post);
147
148         my $tmplist = array2oneperline(\@tmparr);
149         return if ($tmplist eq "");
150
151         $self->output("$ctx->{TYPE}\_$ctx->{NAME}_$var =$tmplist\n");
152 }
153
154 sub _prepare_list($$$)
155 {
156         my ($self,$ctx,$var) = @_;
157
158         $self->_prepare_list_ex($ctx, $var, undef, undef);
159 }
160
161 sub Integrated($$)
162 {
163         my ($self,$ctx) = @_;
164
165         $self->_prepare_list($ctx, "OBJ_LIST");
166         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
167         $self->_prepare_list($ctx, "LINK_FLAGS");
168 }
169
170 sub SharedModule($$)
171 {
172         my ($self,$ctx) = @_;
173
174         my $init_obj = "";
175
176         my $sane_subsystem = lc($ctx->{SUBSYSTEM});
177         $sane_subsystem =~ s/^lib//;
178         
179         if ($ctx->{TYPE} eq "PYTHON") {
180                 push (@{$self->{python_dsos}}, 
181                         "$ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}");
182         } else {
183                 push (@{$self->{shared_modules}}, "$ctx->{TARGET_SHARED_LIBRARY}");
184                 push (@{$self->{plugins}}, "$ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}");
185                 $self->{install_plugins} .= "\t\@echo Installing $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME} as \$(DESTDIR)\$(modulesdir)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n";
186                 $self->{install_plugins} .= "\t\@mkdir -p \$(DESTDIR)\$(modulesdir)/$sane_subsystem/\n";
187                 $self->{install_plugins} .= "\t\@cp $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME} \$(DESTDIR)\$(modulesdir)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n";
188                 $self->{uninstall_plugins} .= "\t\@echo Uninstalling \$(DESTDIR)\$(modulesdir)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n";
189                 $self->{uninstall_plugins} .= "\t\@-rm \$(DESTDIR)\$(modulesdir)/$sane_subsystem/$ctx->{LIBRARY_REALNAME}\n";
190                 if (defined($ctx->{ALIASES})) {
191                         foreach (@{$ctx->{ALIASES}}) {
192                                 $self->{install_plugins} .= "\t\@rm -f \$(DESTDIR)\$(modulesdir)/$sane_subsystem/$_.\$(SHLIBEXT)\n";
193                                 $self->{install_plugins} .= "\t\@ln -fs $ctx->{LIBRARY_REALNAME} \$(DESTDIR)\$(modulesdir)/$sane_subsystem/$_.\$(SHLIBEXT)\n";
194                                 $self->{uninstall_plugins} .= "\t\@-rm \$(DESTDIR)\$(modulesdir)/$sane_subsystem/$_.\$(SHLIBEXT)\n";
195                         }
196                 }
197         }
198
199         $self->output("$ctx->{TYPE}_$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
200         $self->_prepare_list($ctx, "OBJ_LIST");
201         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
202         $self->_prepare_list($ctx, "DEPEND_LIST");
203         $self->_prepare_list($ctx, "LINK_FLAGS");
204
205         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)");
206
207         if (defined($ctx->{INIT_FUNCTION}) and $ctx->{TYPE} ne "PYTHON") {
208                 my $init_fn = $ctx->{INIT_FUNCTION_TYPE};
209                 $init_fn =~ s/\(\*\)/init_module/;
210                 my $proto_fn = $ctx->{INIT_FUNCTION_TYPE};
211                 $proto_fn =~ s/\(\*\)/$ctx->{INIT_FUNCTION}/;
212
213                 $self->output(<< "__EOD__"
214 bin/$ctx->{NAME}_init_module.c:
215         \@echo Creating \$\@
216         \@echo \"#include \\\"includes.h\\\"\" > \$\@
217         \@echo \"$proto_fn;\" >> \$\@
218         \@echo \"_PUBLIC_ $init_fn\" >> \$\@
219         \@echo \"{\" >> \$\@
220         \@echo \"       return $ctx->{INIT_FUNCTION}();\" >> \$\@
221         \@echo \"}\" >> \$\@
222         \@echo \"\" >> \$\@
223 __EOD__
224 );
225                 $init_obj = "bin/$ctx->{NAME}_init_module.o";
226         }
227
228         $self->output(<< "__EOD__"
229 #
230
231 $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST) $init_obj
232         \@echo Linking \$\@
233         \@mkdir -p $ctx->{SHAREDDIR}
234         \@\$(MDLD) \$(MDLD_FLAGS) \$(INTERN_LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
235                 \$($ctx->{TYPE}_$ctx->{NAME}\_FULL_OBJ_LIST) $init_obj \\
236                 \$($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS)
237 __EOD__
238 );
239
240         if (defined($ctx->{ALIASES})) {
241                 foreach (@{$ctx->{ALIASES}}) {
242                         $self->output("\t\@rm -f $ctx->{SHAREDDIR}/$_.\$(SHLIBEXT)\n");
243                         $self->output("\t\@ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{SHAREDDIR}/$_.\$(SHLIBEXT)\n");
244                 }
245         }
246         $self->output("\n");
247 }
248
249 sub SharedLibrary($$)
250 {
251         my ($self,$ctx) = @_;
252
253         my $has_static_lib = 0;
254
255         push (@{$self->{shared_libs}}, "$ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}") if (defined($ctx->{SO_VERSION}));
256         push (@{$self->{installable_shared_libs}}, "$ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}") if (defined($ctx->{SO_VERSION}));
257
258         $has_static_lib = 1 if grep(/STATIC_LIBRARY/, @{$ctx->{OUTPUT_TYPE}});
259
260         if (not $has_static_lib) {
261                 $self->output("$ctx->{TYPE}_$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
262                 $self->_prepare_list($ctx, "OBJ_LIST");
263                 $self->_prepare_list($ctx, "FULL_OBJ_LIST");
264         }
265         $self->_prepare_list($ctx, "DEPEND_LIST");
266         $self->_prepare_list($ctx, "LINK_FLAGS");
267 #       $self->_prepare_list_ex($ctx, "LINK_FLAGS", "-Wl,--whole-archive", "-Wl,--no-whole-archive");
268
269         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)");
270
271         my $soarg = "";
272         my $lns = "";
273         if ($self->{config}->{SONAMEFLAG} ne "#" and defined($ctx->{LIBRARY_SONAME})) {
274                 $soarg = "$self->{config}->{SONAMEFLAG}$ctx->{LIBRARY_SONAME}";
275                 if ($ctx->{LIBRARY_REALNAME} ne $ctx->{LIBRARY_SONAME}) {
276                         $lns .= "\n\t\@rm -f $ctx->{SHAREDDIR}/$ctx->{LIBRARY_SONAME}";
277                         $lns .= "\n\t\@ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{SHAREDDIR}/$ctx->{LIBRARY_SONAME}";
278                 }
279         }
280
281         if (defined($ctx->{LIBRARY_SONAME})) {
282                 $lns .= "\n\t\@rm -f $ctx->{SHAREDDIR}/$ctx->{LIBRARY_DEBUGNAME}";
283                 $lns .= "\n\t\@ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{SHAREDDIR}/$ctx->{LIBRARY_DEBUGNAME}";
284         }
285
286         $self->output(<< "__EOD__"
287 #
288
289 $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)
290         \@echo Linking \$\@
291         \@mkdir -p $ctx->{SHAREDDIR}
292         \@\$(SHLD) \$(SHLD_FLAGS) \$(INTERN_LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
293                 \$($ctx->{TYPE}_$ctx->{NAME}\_FULL_OBJ_LIST) \\
294                 \$($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) \\
295                 $soarg$lns
296 __EOD__
297 );
298         $self->output("\n");
299 }
300
301 sub StaticLibrary($$)
302 {
303         my ($self,$ctx) = @_;
304
305         return unless (defined($ctx->{OBJ_FILES}));
306
307         push (@{$self->{static_libs}}, $ctx->{TARGET_STATIC_LIBRARY}) if ($ctx->{TYPE} eq "LIBRARY");
308
309         $self->output("$ctx->{TYPE}_$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
310         $self->_prepare_list($ctx, "OBJ_LIST");
311         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
312
313         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)");
314
315         $self->output(<< "__EOD__"
316 #
317 $ctx->{TARGET_STATIC_LIBRARY}: \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)
318         \@echo Linking \$@
319         \@rm -f \$@
320         \@mkdir -p $ctx->{STATICDIR}
321         \@\$(STLD) \$(STLD_FLAGS) \$@ \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)
322
323 __EOD__
324 );
325 }
326
327 sub Header($$)
328 {
329         my ($self,$ctx) = @_;
330
331         foreach (@{$ctx->{PUBLIC_HEADERS}}) {
332                 push (@{$self->{headers}}, output::add_dir_str($ctx->{BASEDIR}, $_));
333         }
334 }
335
336 sub Binary($$)
337 {
338         my ($self,$ctx) = @_;
339
340         my $installdir;
341         my $extradir = "";
342
343         if (defined($ctx->{INSTALLDIR}) && $ctx->{INSTALLDIR} =~ /^TORTUREDIR/) {
344                 $extradir = "/torture" . substr($ctx->{INSTALLDIR}, length("TORTUREDIR"));
345         }
346         my $localdir = "bin$extradir";
347
348         $installdir = "bin$extradir";
349
350         push(@{$self->{all_objs}}, "\$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)");
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         } elsif ($ctx->{INSTALLDIR} =~ /^TORTUREDIR/) {
358                 push (@{$self->{torture_progs}}, "$installdir/$ctx->{BINARY}");
359         }
360
361         push (@{$self->{binaries}}, "$localdir/$ctx->{BINARY}");
362
363         $self->_prepare_list($ctx, "OBJ_LIST");
364         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
365         $self->_prepare_list($ctx, "DEPEND_LIST");
366         $self->_prepare_list($ctx, "LINK_FLAGS");
367
368 $self->output(<< "__EOD__"
369 $installdir/$ctx->{BINARY}: \$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_FULL_OBJ_LIST)
370         \@echo Linking \$\@
371 __EOD__
372         );
373
374         if (defined($ctx->{USE_HOSTCC}) && $ctx->{USE_HOSTCC} eq "YES") {
375                 $self->output(<< "__EOD__"
376         \@\$(HOSTLD) \$(HOSTLD_FLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
377                 \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS)
378 __EOD__
379                 );
380         } else {
381                 $self->output(<< "__EOD__"
382         \@\$(BNLD) \$(BNLD_FLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
383                 \$\($ctx->{TYPE}_$ctx->{NAME}_LINK_FLAGS) 
384
385 __EOD__
386                 );
387         }
388 }
389
390 sub PythonFiles($$)
391 {
392         my ($self,$ctx) = @_;
393
394         foreach (@{$ctx->{PYTHON_FILES}}) {
395                 my $target = "bin/python/".basename($_);
396                 my $source = output::add_dir_str($ctx->{BASEDIR}, $_);
397                 $self->output("$target: $source\n" .
398                                           "\tmkdir -p \$(builddir)/bin/python\n" .
399                               "\tcp $source \$@\n\n");
400                 push (@{$self->{python_pys}}, $target);
401         }
402 }
403
404 sub Manpage($$)
405 {
406         my ($self,$ctx) = @_;
407
408         my $path = output::add_dir_str($ctx->{BASEDIR}, $ctx->{MANPAGE});
409         push (@{$self->{manpages}}, $path);
410 }
411
412 sub ProtoHeader($$)
413 {
414         my ($self,$ctx) = @_;
415
416         my $target = "";
417         my $comment = "Creating ";
418
419         my $priv = undef;
420         my $pub = undef;
421
422         if (defined($ctx->{PRIVATE_PROTO_HEADER})) {
423                 $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER});
424                 $target .= $priv;
425                 $comment .= $priv;
426                 if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
427                         $comment .= " and ";
428                         $target.= " ";
429                 }
430                 push (@{$self->{proto_headers}}, $priv);
431         } else {
432                 $ctx->{PRIVATE_PROTO_HEADER} = $ctx->{PUBLIC_PROTO_HEADER};
433                 $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER});
434         }
435
436         if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
437                 $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER});
438                 $comment .= $pub;
439                 $target .= $pub;
440                 push (@{$self->{proto_headers}}, $pub);
441         } else {
442                 $ctx->{PUBLIC_PROTO_HEADER} = $ctx->{PRIVATE_PROTO_HEADER};
443                 $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER});
444         }
445
446         $self->output("$pub: $ctx->{MK_FILE} \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST:.o=.c) \$(srcdir)/script/mkproto.pl\n");
447         $self->output("\t\@echo \"$comment\"\n");
448         $self->output("\t\@\$(PERL) \$(srcdir)/script/mkproto.pl --srcdir=\$(srcdir) --builddir=\$(builddir) --private=$priv --public=$pub \$($ctx->{TYPE}_$ctx->{NAME}_OBJ_LIST)\n\n");
449 }
450
451 sub write($$)
452 {
453         my ($self,$file) = @_;
454
455         $self->output("MANPAGES = ".array2oneperline($self->{manpages})."\n");
456         $self->output("BIN_PROGS = " . array2oneperline($self->{bin_progs}) . "\n");
457         $self->output("SBIN_PROGS = " . array2oneperline($self->{sbin_progs}) . "\n");
458         $self->output("TORTURE_PROGS = " . array2oneperline($self->{torture_progs}) . "\n");
459         $self->output("BINARIES = " . array2oneperline($self->{binaries}) . "\n");
460         $self->output("STATIC_LIBS = " . array2oneperline($self->{static_libs}) . "\n");
461         $self->output("SHARED_LIBS = " . array2oneperline($self->{shared_libs}) . "\n");
462         $self->output("PYTHON_DSOS = " . array2oneperline($self->{python_dsos}) . "\n");
463         $self->output("PYTHON_PYS = " . array2oneperline($self->{python_pys}) . "\n");
464         $self->output("INSTALLABLE_SHARED_LIBS = " . array2oneperline($self->{installable_shared_libs}) . "\n");
465         $self->output("PUBLIC_HEADERS = " . array2oneperline($self->{headers}) . "\n");
466         $self->output("PC_FILES = " . array2oneperline($self->{pc_files}) . "\n");
467         $self->output("ALL_OBJS = " . array2oneperline($self->{all_objs}) . "\n");
468         $self->output("PROTO_HEADERS = " . array2oneperline($self->{proto_headers}) .  "\n");
469         $self->output("SHARED_MODULES = " . array2oneperline($self->{shared_modules}) . "\n");
470         $self->output("PLUGINS = " . array2oneperline($self->{plugins}) . "\n");
471
472         $self->output("\ninstallplugins: \$(PLUGINS)\n".$self->{install_plugins}."\n");
473         $self->output("\nuninstallplugins:\n".$self->{uninstall_plugins}."\n");
474
475         $self->_prepare_mk_files();
476
477         $self->output($self->{mkfile});
478
479         if ($self->{automatic_deps}) {
480                 $self->output("
481 ifneq (\$(MAKECMDGOALS),clean)
482 ifneq (\$(MAKECMDGOALS),distclean)
483 ifneq (\$(MAKECMDGOALS),realdistclean)
484 ifneq (\$(SKIP_DEP_FILES),yes)
485 -include \$(DEP_FILES)
486 endif
487 endif
488 endif
489 endif
490
491 ifneq (\$(SKIP_DEP_FILES),yes)
492 clean::
493         \@echo Removing dependency files
494         \@find . -name '*.d' -o -name '*.hd' | xargs rm -f
495 endif
496 ");
497         } else {
498                 $self->output("include \$(srcdir)/static_deps.mk\n");
499         }
500
501         open(MAKEFILE,">$file") || die ("Can't open $file\n");
502         print MAKEFILE $self->{output};
503         close(MAKEFILE);
504
505         print __FILE__.": creating $file\n";
506 }
507
508 1;