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