Merge branch 'v4-0-test' of git://git.samba.org/samba into 4-0-local
[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("$ctx->{RESULT_STATIC_LIBRARY}: \$($ctx->{NAME}_FULL_OBJ_LIST)\n");
285 }
286
287 sub Header($$)
288 {
289         my ($self,$ctx) = @_;
290
291         foreach (@{$ctx->{PUBLIC_HEADERS}}) {
292                 push (@{$self->{headers}}, output::add_dir_str($ctx->{BASEDIR}, $_));
293         }
294 }
295
296 sub Binary($$)
297 {
298         my ($self,$ctx) = @_;
299
300         my $extradir = "";
301
302         unless (defined($ctx->{INSTALLDIR})) {
303         } elsif ($ctx->{INSTALLDIR} eq "SBINDIR") {
304                 push (@{$self->{sbin_progs}}, $ctx->{RESULT_BINARY});
305         } elsif ($ctx->{INSTALLDIR} eq "BINDIR") {
306                 push (@{$self->{bin_progs}}, $ctx->{RESULT_BINARY});
307         }
308
309         $self->output("binaries:: $ctx->{TARGET_BINARY}\n");
310
311         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
312         $self->_prepare_list($ctx, "DEPEND_LIST");
313         $self->_prepare_list($ctx, "LINK_FLAGS");
314
315 $self->output(<< "__EOD__"
316 $ctx->{RESULT_BINARY}: \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST)
317         \@echo Linking \$\@
318 __EOD__
319         );
320
321         if (defined($ctx->{USE_HOSTCC}) && $ctx->{USE_HOSTCC} eq "YES") {
322                 $self->output(<< "__EOD__"
323         \@\$(HOSTLD) \$(HOSTLD_FLAGS) -L\${builddir}/bin/static -o \$\@ \$(INSTALL_LINK_FLAGS) \\
324                 \$\($ctx->{NAME}_LINK_FLAGS)
325 __EOD__
326                 );
327         } else {
328                 $self->output(<< "__EOD__"
329         \@\$(BNLD) \$(BNLD_FLAGS) \$(INTERN_LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
330                 \$\($ctx->{NAME}_LINK_FLAGS) 
331
332 __EOD__
333                 );
334         }
335 }
336
337 sub PythonFiles($$)
338 {
339         my ($self,$ctx) = @_;
340
341         foreach (@{$ctx->{PYTHON_FILES}}) {
342                 my $target = "bin/python/".basename($_);
343                 my $source = output::add_dir_str($ctx->{BASEDIR}, $_);
344                 $self->output("$target: $source\n" .
345                                           "\tmkdir -p \$(builddir)/bin/python\n" .
346                               "\tcp $source \$@\n\n");
347                 push (@{$self->{python_pys}}, $target);
348         }
349 }
350
351 sub Manpage($$)
352 {
353         my ($self,$ctx) = @_;
354
355         my $path = output::add_dir_str($ctx->{BASEDIR}, $ctx->{MANPAGE});
356         push (@{$self->{manpages}}, $path);
357 }
358
359 sub ProtoHeader($$)
360 {
361         my ($self,$ctx) = @_;
362
363         my $target = "";
364         my $comment = "Creating ";
365
366         my $priv = undef;
367         my $pub = undef;
368
369         if (defined($ctx->{PRIVATE_PROTO_HEADER})) {
370                 $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER});
371                 $target .= $priv;
372                 $comment .= $priv;
373                 if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
374                         $comment .= " and ";
375                         $target.= " ";
376                 }
377                 push (@{$self->{proto_headers}}, $priv);
378         } else {
379                 $ctx->{PRIVATE_PROTO_HEADER} = $ctx->{PUBLIC_PROTO_HEADER};
380                 $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER});
381         }
382
383         if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
384                 $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER});
385                 $comment .= $pub;
386                 $target .= $pub;
387                 push (@{$self->{proto_headers}}, $pub);
388         } else {
389                 $ctx->{PUBLIC_PROTO_HEADER} = $ctx->{PRIVATE_PROTO_HEADER};
390                 $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER});
391         }
392
393         $self->output("$pub: $ctx->{MK_FILE} \$($ctx->{NAME}_OBJ_LIST:.o=.c) \$(srcdir)/script/mkproto.pl\n");
394         $self->output("\t\@echo \"$comment\"\n");
395         $self->output("\t\@\$(PERL) \$(srcdir)/script/mkproto.pl --srcdir=\$(srcdir) --builddir=\$(builddir) --private=$priv --public=$pub \$($ctx->{NAME}_OBJ_LIST)\n\n");
396 }
397
398 sub write($$)
399 {
400         my ($self, $file) = @_;
401
402         $self->output("MANPAGES = " . array2oneperline($self->{manpages})."\n");
403         $self->output("BIN_PROGS = " . array2oneperline($self->{bin_progs}) . "\n");
404         $self->output("SBIN_PROGS = " . array2oneperline($self->{sbin_progs}) . "\n");
405         $self->output("STATIC_LIBS = " . array2oneperline($self->{static_libs}) . "\n");
406         $self->output("SHARED_LIBS = " . array2oneperline($self->{shared_libs}) . "\n");
407         $self->output("PYTHON_DSOS = " . array2oneperline($self->{python_dsos}) . "\n");
408         $self->output("PYTHON_PYS = " . array2oneperline($self->{python_pys}) . "\n");
409         $self->output("PUBLIC_HEADERS = " . array2oneperline($self->{headers}) . "\n");
410         $self->output("PC_FILES = " . array2oneperline($self->{pc_files}) . "\n");
411         $self->output("ALL_OBJS = " . array2oneperline($self->{all_objs}) . "\n");
412         $self->output("PROTO_HEADERS = " . array2oneperline($self->{proto_headers}) .  "\n");
413         $self->output("PLUGINS = " . array2oneperline($self->{plugins}) . "\n");
414
415         $self->_prepare_mk_files();
416
417         $self->output($self->{mkfile});
418
419         if ($self->{automatic_deps}) {
420                 $self->output("
421 ifneq (\$(MAKECMDGOALS),clean)
422 ifneq (\$(MAKECMDGOALS),distclean)
423 ifneq (\$(MAKECMDGOALS),realdistclean)
424 ifneq (\$(SKIP_DEP_FILES),yes)
425 -include \$(DEP_FILES)
426 endif
427 endif
428 endif
429 endif
430
431 ifneq (\$(SKIP_DEP_FILES),yes)
432 clean::
433         \@echo Removing dependency files
434         \@find . -name '*.d' -o -name '*.hd' | xargs rm -f
435 endif
436 ");
437         } else {
438                 $self->output("include \$(srcdir)/static_deps.mk\n");
439         }
440
441         open(MAKEFILE,">$file") || die ("Can't open $file\n");
442         print MAKEFILE $self->{output};
443         close(MAKEFILE);
444
445         print __FILE__.": creating $file\n";
446 }
447
448 1;