Reintroduce mergedobj support.
[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->{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->_prepare_list($ctx, "OBJ_LIST");
285         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
286         push(@{$self->{all_objs}}, "\$($ctx->{NAME}_FULL_OBJ_LIST)");
287         $self->output(<< "__EOD__"
288 #
289 $ctx->{TARGET_MERGED_OBJ}: \$($ctx->{NAME}_FULL_OBJ_LIST)
290         \@echo Partially linking \$@
291         \@mkdir -p bin/mergedobj
292         \@\$(LD) -r -o \$@ \$($ctx->{NAME}_FULL_OBJ_LIST)
293
294 __EOD__
295 );
296 }
297
298 sub StaticLibrary($$)
299 {
300         my ($self,$ctx) = @_;
301
302         return unless (defined($ctx->{OBJ_FILES}));
303
304         push (@{$self->{static_libs}}, $ctx->{TARGET_STATIC_LIBRARY}) if ($ctx->{TYPE} eq "LIBRARY");
305
306         $self->output("$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
307         $self->_prepare_list($ctx, "OBJ_LIST");
308         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
309
310         push(@{$self->{all_objs}}, "\$($ctx->{NAME}_FULL_OBJ_LIST)");
311
312         $self->output(<< "__EOD__"
313 #
314 $ctx->{TARGET_STATIC_LIBRARY}: \$($ctx->{NAME}_FULL_OBJ_LIST)
315         \@echo Linking \$@
316         \@rm -f \$@
317         \@mkdir -p $ctx->{STATICDIR}
318         \@\$(STLD) \$(STLD_FLAGS) \$@ \$($ctx->{NAME}_FULL_OBJ_LIST)
319
320 __EOD__
321 );
322 }
323
324 sub Header($$)
325 {
326         my ($self,$ctx) = @_;
327
328         foreach (@{$ctx->{PUBLIC_HEADERS}}) {
329                 push (@{$self->{headers}}, output::add_dir_str($ctx->{BASEDIR}, $_));
330         }
331 }
332
333 sub Binary($$)
334 {
335         my ($self,$ctx) = @_;
336
337         my $installdir;
338         my $extradir = "";
339
340         my $localdir = "bin$extradir";
341
342         $installdir = "bin$extradir";
343
344         push(@{$self->{all_objs}}, "\$($ctx->{NAME}_FULL_OBJ_LIST)");
345                 
346         unless (defined($ctx->{INSTALLDIR})) {
347         } elsif ($ctx->{INSTALLDIR} eq "SBINDIR") {
348                 push (@{$self->{sbin_progs}}, "$installdir/$ctx->{BINARY}");
349         } elsif ($ctx->{INSTALLDIR} eq "BINDIR") {
350                 push (@{$self->{bin_progs}}, "$installdir/$ctx->{BINARY}");
351         }
352
353         $self->output("binaries:: $localdir/$ctx->{BINARY}\n");
354
355         $self->_prepare_list($ctx, "OBJ_LIST");
356         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
357         $self->_prepare_list($ctx, "DEPEND_LIST");
358         $self->_prepare_list($ctx, "LINK_FLAGS");
359
360 $self->output(<< "__EOD__"
361 $installdir/$ctx->{BINARY}: \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST)
362         \@echo Linking \$\@
363 __EOD__
364         );
365
366         if (defined($ctx->{USE_HOSTCC}) && $ctx->{USE_HOSTCC} eq "YES") {
367                 $self->output(<< "__EOD__"
368         \@\$(HOSTLD) \$(HOSTLD_FLAGS) -L\${builddir}/bin/static -o \$\@ \$(INSTALL_LINK_FLAGS) \\
369                 \$\($ctx->{NAME}_LINK_FLAGS)
370 __EOD__
371                 );
372         } else {
373                 $self->output(<< "__EOD__"
374         \@\$(BNLD) \$(BNLD_FLAGS) \$(INTERN_LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
375                 \$\($ctx->{NAME}_LINK_FLAGS) 
376
377 __EOD__
378                 );
379         }
380 }
381
382 sub PythonFiles($$)
383 {
384         my ($self,$ctx) = @_;
385
386         foreach (@{$ctx->{PYTHON_FILES}}) {
387                 my $target = "bin/python/".basename($_);
388                 my $source = output::add_dir_str($ctx->{BASEDIR}, $_);
389                 $self->output("$target: $source\n" .
390                                           "\tmkdir -p \$(builddir)/bin/python\n" .
391                               "\tcp $source \$@\n\n");
392                 push (@{$self->{python_pys}}, $target);
393         }
394 }
395
396 sub Manpage($$)
397 {
398         my ($self,$ctx) = @_;
399
400         my $path = output::add_dir_str($ctx->{BASEDIR}, $ctx->{MANPAGE});
401         push (@{$self->{manpages}}, $path);
402 }
403
404 sub ProtoHeader($$)
405 {
406         my ($self,$ctx) = @_;
407
408         my $target = "";
409         my $comment = "Creating ";
410
411         my $priv = undef;
412         my $pub = undef;
413
414         if (defined($ctx->{PRIVATE_PROTO_HEADER})) {
415                 $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER});
416                 $target .= $priv;
417                 $comment .= $priv;
418                 if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
419                         $comment .= " and ";
420                         $target.= " ";
421                 }
422                 push (@{$self->{proto_headers}}, $priv);
423         } else {
424                 $ctx->{PRIVATE_PROTO_HEADER} = $ctx->{PUBLIC_PROTO_HEADER};
425                 $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER});
426         }
427
428         if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
429                 $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER});
430                 $comment .= $pub;
431                 $target .= $pub;
432                 push (@{$self->{proto_headers}}, $pub);
433         } else {
434                 $ctx->{PUBLIC_PROTO_HEADER} = $ctx->{PRIVATE_PROTO_HEADER};
435                 $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER});
436         }
437
438         $self->output("$pub: $ctx->{MK_FILE} \$($ctx->{NAME}_OBJ_LIST:.o=.c) \$(srcdir)/script/mkproto.pl\n");
439         $self->output("\t\@echo \"$comment\"\n");
440         $self->output("\t\@\$(PERL) \$(srcdir)/script/mkproto.pl --srcdir=\$(srcdir) --builddir=\$(builddir) --private=$priv --public=$pub \$($ctx->{NAME}_OBJ_LIST)\n\n");
441 }
442
443 sub write($$)
444 {
445         my ($self, $file) = @_;
446
447         $self->output("MANPAGES = " . array2oneperline($self->{manpages})."\n");
448         $self->output("BIN_PROGS = " . array2oneperline($self->{bin_progs}) . "\n");
449         $self->output("SBIN_PROGS = " . array2oneperline($self->{sbin_progs}) . "\n");
450         $self->output("STATIC_LIBS = " . array2oneperline($self->{static_libs}) . "\n");
451         $self->output("SHARED_LIBS = " . array2oneperline($self->{shared_libs}) . "\n");
452         $self->output("PYTHON_DSOS = " . array2oneperline($self->{python_dsos}) . "\n");
453         $self->output("PYTHON_PYS = " . array2oneperline($self->{python_pys}) . "\n");
454         $self->output("PUBLIC_HEADERS = " . array2oneperline($self->{headers}) . "\n");
455         $self->output("PC_FILES = " . array2oneperline($self->{pc_files}) . "\n");
456         $self->output("ALL_OBJS = " . array2oneperline($self->{all_objs}) . "\n");
457         $self->output("PROTO_HEADERS = " . array2oneperline($self->{proto_headers}) .  "\n");
458         $self->output("SHARED_MODULES = " . array2oneperline($self->{shared_modules}) . "\n");
459         $self->output("PLUGINS = " . array2oneperline($self->{plugins}) . "\n");
460
461         $self->_prepare_mk_files();
462
463         $self->output($self->{mkfile});
464
465         if ($self->{automatic_deps}) {
466                 $self->output("
467 ifneq (\$(MAKECMDGOALS),clean)
468 ifneq (\$(MAKECMDGOALS),distclean)
469 ifneq (\$(MAKECMDGOALS),realdistclean)
470 ifneq (\$(SKIP_DEP_FILES),yes)
471 -include \$(DEP_FILES)
472 endif
473 endif
474 endif
475 endif
476
477 ifneq (\$(SKIP_DEP_FILES),yes)
478 clean::
479         \@echo Removing dependency files
480         \@find . -name '*.d' -o -name '*.hd' | xargs rm -f
481 endif
482 ");
483         } else {
484                 $self->output("include \$(srcdir)/static_deps.mk\n");
485         }
486
487         open(MAKEFILE,">$file") || die ("Can't open $file\n");
488         print MAKEFILE $self->{output};
489         close(MAKEFILE);
490
491         print __FILE__.": creating $file\n";
492 }
493
494 1;