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