498b61900145bca97abcb26357681bb562a0bf68
[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") {
164                 my $init_fn = $ctx->{INIT_FUNCTION_TYPE};
165                 $init_fn =~ s/\(\*\)/init_module/;
166                 my $proto_fn = $ctx->{INIT_FUNCTION_TYPE};
167                 $proto_fn =~ s/\(\*\)/$ctx->{INIT_FUNCTION}/;
168
169                 $self->output(<< "__EOD__"
170 bin/$ctx->{NAME}_init_module.c:
171         \@echo Creating \$\@
172         \@echo \"#include \\\"includes.h\\\"\" > \$\@
173         \@echo \"$proto_fn;\" >> \$\@
174         \@echo \"_PUBLIC_ $init_fn\" >> \$\@
175         \@echo \"{\" >> \$\@
176         \@echo \"       return $ctx->{INIT_FUNCTION}();\" >> \$\@
177         \@echo \"}\" >> \$\@
178         \@echo \"\" >> \$\@
179 __EOD__
180 );
181                 $init_obj = "bin/$ctx->{NAME}_init_module.o";
182         }
183
184         $self->output(<< "__EOD__"
185 #
186
187 $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}: \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST) $init_obj
188         \@echo Linking \$\@
189         \@mkdir -p $ctx->{SHAREDDIR}
190         \@\$(MDLD) \$(LDFLAGS) \$(MDLD_FLAGS) \$(INTERN_LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
191                 \$($ctx->{NAME}\_FULL_OBJ_LIST) $init_obj \\
192                 \$($ctx->{NAME}_LINK_FLAGS)
193 __EOD__
194 );
195
196         if (defined($ctx->{ALIASES})) {
197                 foreach (@{$ctx->{ALIASES}}) {
198                         $self->output("\t\@rm -f $ctx->{SHAREDDIR}/$_.\$(SHLIBEXT)\n");
199                         $self->output("\t\@ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{SHAREDDIR}/$_.\$(SHLIBEXT)\n");
200                 }
201         }
202         $self->output("\n");
203 }
204
205 sub SharedLibraryPrimitives($$)
206 {
207         my ($self,$ctx) = @_;
208
209         $self->output("$ctx->{NAME}_SOVERSION = $ctx->{SO_VERSION}\n") if (defined($ctx->{SO_VERSION}));
210         $self->output("$ctx->{NAME}_VERSION = $ctx->{VERSION}\n") if (defined($ctx->{VERSION}));
211
212         if (not grep(/STATIC_LIBRARY/, @{$ctx->{OUTPUT_TYPE}})) {
213                 $self->output("$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
214                 $self->_prepare_list($ctx, "FULL_OBJ_LIST");
215         }
216 }
217
218 sub SharedLibrary($$)
219 {
220         my ($self,$ctx) = @_;
221
222         push (@{$self->{shared_libs}}, $ctx->{RESULT_SHARED_LIBRARY}) if (defined($ctx->{SO_VERSION}));
223
224         $self->_prepare_list($ctx, "DEPEND_LIST");
225         $self->_prepare_list($ctx, "LINK_FLAGS");
226
227         my $soarg = "";
228         my $lns = "";
229         if ($self->{config}->{SONAMEFLAG} ne "#" and defined($ctx->{LIBRARY_SONAME})) {
230                 $soarg = "$self->{config}->{SONAMEFLAG}$ctx->{LIBRARY_SONAME}";
231                 if ($ctx->{LIBRARY_REALNAME} ne $ctx->{LIBRARY_SONAME}) {
232                         $lns .= "\n\t\@test \$($ctx->{NAME}_VERSION) = \$($ctx->{NAME}_SOVERSION) || ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{SHAREDDIR}/$ctx->{LIBRARY_SONAME}";
233                 }
234         }
235
236         if (defined($ctx->{LIBRARY_SONAME})) {
237                 $lns .= "\n\t\@ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{SHAREDDIR}/$ctx->{LIBRARY_DEBUGNAME}";
238         }
239
240         $self->output(<< "__EOD__"
241 #
242 $ctx->{RESULT_SHARED_LIBRARY}: \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST)
243         \@echo Linking \$\@
244         \@mkdir -p $ctx->{SHAREDDIR}
245         \@\$(SHLD) \$(LDFLAGS) \$(SHLD_FLAGS) \$(INTERN_LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
246                 \$($ctx->{NAME}\_FULL_OBJ_LIST) \\
247                 \$($ctx->{NAME}_LINK_FLAGS) \\
248                 $soarg$lns
249 __EOD__
250 );
251         $self->output("\n");
252 }
253
254 sub MergedObj($$)
255 {
256         my ($self, $ctx) = @_;
257
258         return unless defined($ctx->{OUTPUT});
259
260         $self->output("$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
261         $self->output(<< "__EOD__"
262 #
263 $ctx->{RESULT_MERGED_OBJ}: \$($ctx->{NAME}_OBJ_LIST)
264         \@echo Partially linking \$@
265         \@mkdir -p bin/mergedobj
266         \$(PARTLINK) -o \$@ \$($ctx->{NAME}_OBJ_LIST)
267
268 __EOD__
269 );
270 }
271
272 sub StaticLibrary($$)
273 {
274         my ($self,$ctx) = @_;
275
276         return unless (defined($ctx->{OBJ_FILES}));
277
278         push (@{$self->{static_libs}}, $ctx->{RESULT_STATIC_LIBRARY}) if ($ctx->{TYPE} eq "LIBRARY");
279
280         $self->output("$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
281         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
282
283         $self->output(<< "__EOD__"
284 #
285 $ctx->{RESULT_STATIC_LIBRARY}: \$($ctx->{NAME}_FULL_OBJ_LIST)
286         \@echo Linking \$@
287         \@rm -f \$@
288         \@mkdir -p $ctx->{STATICDIR}
289         \@\$(STLD) \$(STLD_FLAGS) \$@ \$($ctx->{NAME}_FULL_OBJ_LIST)
290
291 __EOD__
292 );
293 }
294
295 sub Header($$)
296 {
297         my ($self,$ctx) = @_;
298
299         foreach (@{$ctx->{PUBLIC_HEADERS}}) {
300                 push (@{$self->{headers}}, output::add_dir_str($ctx->{BASEDIR}, $_));
301         }
302 }
303
304 sub Binary($$)
305 {
306         my ($self,$ctx) = @_;
307
308         my $extradir = "";
309
310         unless (defined($ctx->{INSTALLDIR})) {
311         } elsif ($ctx->{INSTALLDIR} eq "SBINDIR") {
312                 push (@{$self->{sbin_progs}}, $ctx->{RESULT_BINARY});
313         } elsif ($ctx->{INSTALLDIR} eq "BINDIR") {
314                 push (@{$self->{bin_progs}}, $ctx->{RESULT_BINARY});
315         }
316
317         $self->output("binaries:: $ctx->{TARGET_BINARY}\n");
318
319         $self->_prepare_list($ctx, "FULL_OBJ_LIST");
320         $self->_prepare_list($ctx, "DEPEND_LIST");
321         $self->_prepare_list($ctx, "LINK_FLAGS");
322
323 $self->output(<< "__EOD__"
324 $ctx->{RESULT_BINARY}: \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST)
325         \@echo Linking \$\@
326 __EOD__
327         );
328
329         if (defined($ctx->{USE_HOSTCC}) && $ctx->{USE_HOSTCC} eq "YES") {
330                 $self->output(<< "__EOD__"
331         \@\$(HOSTLD) \$(HOSTLD_FLAGS) -L\${builddir}/bin/static -o \$\@ \$(INSTALL_LINK_FLAGS) \\
332                 \$\($ctx->{NAME}_LINK_FLAGS)
333 __EOD__
334                 );
335         } else {
336                 $self->output(<< "__EOD__"
337         \@\$(BNLD) \$(BNLD_FLAGS) \$(INTERN_LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
338                 \$\($ctx->{NAME}_LINK_FLAGS) 
339
340 __EOD__
341                 );
342         }
343 }
344
345 sub PythonFiles($$)
346 {
347         my ($self,$ctx) = @_;
348
349         foreach (@{$ctx->{PYTHON_FILES}}) {
350                 my $target = "bin/python/".basename($_);
351                 my $source = output::add_dir_str($ctx->{BASEDIR}, $_);
352                 $self->output("$target: $source\n" .
353                                           "\tmkdir -p \$(builddir)/bin/python\n" .
354                               "\tcp $source \$@\n\n");
355                 push (@{$self->{python_pys}}, $target);
356         }
357 }
358
359 sub Manpage($$)
360 {
361         my ($self,$ctx) = @_;
362
363         my $path = output::add_dir_str($ctx->{BASEDIR}, $ctx->{MANPAGE});
364         push (@{$self->{manpages}}, $path);
365 }
366
367 sub ProtoHeader($$)
368 {
369         my ($self,$ctx) = @_;
370
371         my $target = "";
372         my $comment = "Creating ";
373
374         my $priv = undef;
375         my $pub = undef;
376
377         if (defined($ctx->{PRIVATE_PROTO_HEADER})) {
378                 $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER});
379                 $target .= $priv;
380                 $comment .= $priv;
381                 if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
382                         $comment .= " and ";
383                         $target.= " ";
384                 }
385                 push (@{$self->{proto_headers}}, $priv);
386         } else {
387                 $ctx->{PRIVATE_PROTO_HEADER} = $ctx->{PUBLIC_PROTO_HEADER};
388                 $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER});
389         }
390
391         if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
392                 $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER});
393                 $comment .= $pub;
394                 $target .= $pub;
395                 push (@{$self->{proto_headers}}, $pub);
396         } else {
397                 $ctx->{PUBLIC_PROTO_HEADER} = $ctx->{PRIVATE_PROTO_HEADER};
398                 $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER});
399         }
400
401         $self->output("$pub: $ctx->{MK_FILE} \$($ctx->{NAME}_OBJ_LIST:.o=.c) \$(srcdir)/script/mkproto.pl\n");
402         $self->output("\t\@echo \"$comment\"\n");
403         $self->output("\t\@\$(PERL) \$(srcdir)/script/mkproto.pl --srcdir=\$(srcdir) --builddir=\$(builddir) --private=$priv --public=$pub \$($ctx->{NAME}_OBJ_LIST)\n\n");
404 }
405
406 sub write($$)
407 {
408         my ($self, $file) = @_;
409
410         $self->output("MANPAGES = " . array2oneperline($self->{manpages})."\n");
411         $self->output("BIN_PROGS = " . array2oneperline($self->{bin_progs}) . "\n");
412         $self->output("SBIN_PROGS = " . array2oneperline($self->{sbin_progs}) . "\n");
413         $self->output("STATIC_LIBS = " . array2oneperline($self->{static_libs}) . "\n");
414         $self->output("SHARED_LIBS = " . array2oneperline($self->{shared_libs}) . "\n");
415         $self->output("PYTHON_DSOS = " . array2oneperline($self->{python_dsos}) . "\n");
416         $self->output("PYTHON_PYS = " . array2oneperline($self->{python_pys}) . "\n");
417         $self->output("PUBLIC_HEADERS = " . array2oneperline($self->{headers}) . "\n");
418         $self->output("PC_FILES = " . array2oneperline($self->{pc_files}) . "\n");
419         $self->output("ALL_OBJS = " . array2oneperline($self->{all_objs}) . "\n");
420         $self->output("PROTO_HEADERS = " . array2oneperline($self->{proto_headers}) .  "\n");
421         $self->output("PLUGINS = " . array2oneperline($self->{plugins}) . "\n");
422
423         $self->_prepare_mk_files();
424
425         $self->output($self->{mkfile});
426
427         if ($self->{automatic_deps}) {
428                 $self->output("
429 ifneq (\$(MAKECMDGOALS),clean)
430 ifneq (\$(MAKECMDGOALS),distclean)
431 ifneq (\$(MAKECMDGOALS),realdistclean)
432 ifneq (\$(SKIP_DEP_FILES),yes)
433 -include \$(DEP_FILES)
434 endif
435 endif
436 endif
437 endif
438
439 ifneq (\$(SKIP_DEP_FILES),yes)
440 clean::
441         \@echo Removing dependency files
442         \@find . -name '*.d' -o -name '*.hd' | xargs rm -f
443 endif
444 ");
445         } else {
446                 $self->output("include \$(srcdir)/static_deps.mk\n");
447         }
448
449         open(MAKEFILE,">$file") || die ("Can't open $file\n");
450         print MAKEFILE $self->{output};
451         close(MAKEFILE);
452
453         print __FILE__.": creating $file\n";
454 }
455
456 1;