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