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