2 # - create output for Makefile
4 # Copyright (C) Stefan (metze) Metzmacher 2004
5 # Copyright (C) Jelmer Vernooij 2005
6 # Released under the GNU GPL
8 package smb_build::makefile;
10 use smb_build::output;
14 use base 'smb_build::env';
19 my ($myname, $config, $mkfile) = @_;
20 my $self = new smb_build::env($config);
22 bless($self, $myname);
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} = [];
37 $self->{mkfile} = $mkfile;
39 $self->output("################################################\n");
40 $self->output("# Autogenerated by build/smb_build/makefile.pm #\n");
41 $self->output("################################################\n");
44 if (!$self->{automatic_deps}) {
45 $self->output("ALL_PREDEP = proto\n");
46 $self->output(".NOTPARALLEL:\n");
54 my ($self, $text) = @_;
56 $self->{output} .= $text;
59 sub _prepare_mk_files($)
64 foreach (@smb_build::config_mk::parsed_files) {
69 if ($self->{gnu_make}) {
71 ifneq (\$(MAKECMDGOALS),clean)
72 ifneq (\$(MAKECMDGOALS),distclean)
73 ifneq (\$(MAKECMDGOALS),realdistclean)
77 $self->output("MK_FILES = " . array2oneperline(\@tmp) . "\n");
79 if ($self->{gnu_make}) {
88 sub array2oneperline($)
94 next unless defined($_);
96 $output .= " \\\n\t\t$_";
102 sub _prepare_list($$$)
104 my ($self,$ctx,$var) = @_;
107 push(@tmparr, @{$ctx->{$var}}) if defined($ctx->{$var});
109 my $tmplist = array2oneperline(\@tmparr);
110 return if ($tmplist eq "");
112 $self->output("$ctx->{NAME}_$var =$tmplist\n");
115 sub SharedModulePrimitives($$)
117 my ($self,$ctx) = @_;
124 my ($self,$ctx) = @_;
128 my $sane_subsystem = lc($ctx->{SUBSYSTEM});
129 $sane_subsystem =~ s/^lib//;
131 if ($ctx->{TYPE} eq "PYTHON") {
132 push (@{$self->{python_dsos}},
133 "$ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}");
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");
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");
151 if (defined($ctx->{ALIASES})) {
152 foreach (@{$ctx->{ALIASES}}) {
153 $self->output("\t\@-rm \$(DESTDIR)\$(modulesdir)/$sane_subsystem/$_.\$(SHLIBEXT)\n");
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");
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}/;
169 $self->output(<< "__EOD__"
170 bin/$ctx->{NAME}_init_module.c:
172 \@echo \"#include \\\"includes.h\\\"\" > \$\@
173 \@echo \"$proto_fn;\" >> \$\@
174 \@echo \"_PUBLIC_ $init_fn\" >> \$\@
176 \@echo \" return $ctx->{INIT_FUNCTION}();\" >> \$\@
181 $init_obj = "bin/$ctx->{NAME}_init_module.o";
184 $self->output(<< "__EOD__"
187 $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}: \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST) $init_obj
189 \@mkdir -p $ctx->{SHAREDDIR}
190 \@\$(MDLD) \$(MDLD_FLAGS) \$(INTERN_LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
191 \$($ctx->{NAME}\_FULL_OBJ_LIST) $init_obj \\
192 \$($ctx->{NAME}_LINK_FLAGS)
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");
205 sub SharedLibraryPrimitives($$)
207 my ($self,$ctx) = @_;
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}));
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");
218 sub SharedLibrary($$)
220 my ($self,$ctx) = @_;
222 push (@{$self->{shared_libs}}, $ctx->{RESULT_SHARED_LIBRARY}) if (defined($ctx->{SO_VERSION}));
224 $self->_prepare_list($ctx, "DEPEND_LIST");
225 $self->_prepare_list($ctx, "LINK_FLAGS");
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}";
236 if (defined($ctx->{LIBRARY_SONAME})) {
237 $lns .= "\n\t\@ln -fs $ctx->{LIBRARY_REALNAME} $ctx->{SHAREDDIR}/$ctx->{LIBRARY_DEBUGNAME}";
240 $self->output(<< "__EOD__"
242 $ctx->{RESULT_SHARED_LIBRARY}: \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST)
244 \@mkdir -p $ctx->{SHAREDDIR}
245 \@\$(SHLD) \$(SHLD_FLAGS) \$(INTERN_LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
246 \$($ctx->{NAME}\_FULL_OBJ_LIST) \\
247 \$($ctx->{NAME}_LINK_FLAGS) \\
256 my ($self, $ctx) = @_;
258 return unless defined($ctx->{OUTPUT});
260 $self->output("$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
261 $self->output(<< "__EOD__"
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)
272 sub StaticLibrary($$)
274 my ($self,$ctx) = @_;
276 return unless (defined($ctx->{OBJ_FILES}));
278 push (@{$self->{static_libs}}, $ctx->{RESULT_STATIC_LIBRARY}) if ($ctx->{TYPE} eq "LIBRARY");
280 $self->output("$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
281 $self->_prepare_list($ctx, "FULL_OBJ_LIST");
283 $self->output(<< "__EOD__"
285 $ctx->{RESULT_STATIC_LIBRARY}: \$($ctx->{NAME}_FULL_OBJ_LIST)
288 \@mkdir -p $ctx->{STATICDIR}
289 \@\$(STLD) \$(STLD_FLAGS) \$@ \$($ctx->{NAME}_FULL_OBJ_LIST)
297 my ($self,$ctx) = @_;
299 foreach (@{$ctx->{PUBLIC_HEADERS}}) {
300 push (@{$self->{headers}}, output::add_dir_str($ctx->{BASEDIR}, $_));
306 my ($self,$ctx) = @_;
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});
317 $self->output("binaries:: $ctx->{TARGET_BINARY}\n");
319 $self->_prepare_list($ctx, "FULL_OBJ_LIST");
320 $self->_prepare_list($ctx, "DEPEND_LIST");
321 $self->_prepare_list($ctx, "LINK_FLAGS");
323 $self->output(<< "__EOD__"
324 $ctx->{RESULT_BINARY}: \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST)
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)
336 $self->output(<< "__EOD__"
337 \@\$(BNLD) \$(BNLD_FLAGS) \$(INTERN_LDFLAGS) -o \$\@ \$(INSTALL_LINK_FLAGS) \\
338 \$\($ctx->{NAME}_LINK_FLAGS)
347 my ($self,$ctx) = @_;
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);
361 my ($self,$ctx) = @_;
363 my $path = output::add_dir_str($ctx->{BASEDIR}, $ctx->{MANPAGE});
364 push (@{$self->{manpages}}, $path);
369 my ($self,$ctx) = @_;
372 my $comment = "Creating ";
377 if (defined($ctx->{PRIVATE_PROTO_HEADER})) {
378 $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER});
381 if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
385 push (@{$self->{proto_headers}}, $priv);
387 $ctx->{PRIVATE_PROTO_HEADER} = $ctx->{PUBLIC_PROTO_HEADER};
388 $priv = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PRIVATE_PROTO_HEADER});
391 if (defined($ctx->{PUBLIC_PROTO_HEADER})) {
392 $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER});
395 push (@{$self->{proto_headers}}, $pub);
397 $ctx->{PUBLIC_PROTO_HEADER} = $ctx->{PRIVATE_PROTO_HEADER};
398 $pub = output::add_dir_str($ctx->{BASEDIR}, $ctx->{PUBLIC_PROTO_HEADER});
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");
408 my ($self, $file) = @_;
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");
423 $self->_prepare_mk_files();
425 $self->output($self->{mkfile});
427 if ($self->{automatic_deps}) {
429 ifneq (\$(MAKECMDGOALS),clean)
430 ifneq (\$(MAKECMDGOALS),distclean)
431 ifneq (\$(MAKECMDGOALS),realdistclean)
432 ifneq (\$(SKIP_DEP_FILES),yes)
433 -include \$(DEP_FILES)
439 ifneq (\$(SKIP_DEP_FILES),yes)
441 \@echo Removing dependency files
442 \@find . -name '*.d' -o -name '*.hd' | xargs rm -f
446 $self->output("include \$(srcdir)/static_deps.mk\n");
449 open(MAKEFILE,">$file") || die ("Can't open $file\n");
450 print MAKEFILE $self->{output};
453 print __FILE__.": creating $file\n";