Don't throw away cflags if they start with a variable.
[Samba/gbeck.git] / source / build / smb_build / makefile.pm
blob73801c25fd3afdd1e294cfe3c52895cd71e1f0cf
1 # Samba Build System
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;
9 use smb_build::output;
10 use File::Basename;
11 use strict;
13 use Cwd 'abs_path';
15 sub new($$$)
17 my ($myname, $config, $mkfile) = @_;
18 my $self = {};
20 bless($self, $myname);
22 $self->_set_config($config);
24 $self->{output} = "";
26 $self->output("################################################\n");
27 $self->output("# Autogenerated by build/smb_build/makefile.pm #\n");
28 $self->output("################################################\n");
29 $self->output("\n");
30 $self->output($mkfile);
32 return $self;
35 sub _set_config($$)
37 my ($self, $config) = @_;
39 $self->{config} = $config;
41 if (not defined($self->{config}->{srcdir})) {
42 $self->{config}->{srcdir} = '.';
45 if (not defined($self->{config}->{builddir})) {
46 $self->{config}->{builddir} = '.';
49 if ($self->{config}->{prefix} eq "NONE") {
50 $self->{config}->{prefix} = $self->{config}->{ac_default_prefix};
53 if ($self->{config}->{exec_prefix} eq "NONE") {
54 $self->{config}->{exec_prefix} = $self->{config}->{prefix};
58 sub output($$)
60 my ($self, $text) = @_;
62 $self->{output} .= $text;
65 sub _prepare_mk_files($)
67 my $self = shift;
68 my @tmp = ();
70 foreach (@smb_build::config_mk::parsed_files) {
71 s/ .*$//g;
72 push (@tmp, $_);
75 $self->output("MK_FILES = " . array2oneperline(\@tmp) . "\n");
78 sub array2oneperline($)
80 my $array = shift;
81 my $output = "";
83 foreach (@$array) {
84 next unless defined($_);
86 $output .= " \\\n\t\t$_";
89 return $output;
92 sub _prepare_list($$$)
94 my ($self,$ctx,$var) = @_;
95 my @tmparr = ();
97 push(@tmparr, @{$ctx->{$var}}) if defined($ctx->{$var});
99 my $tmplist = array2oneperline(\@tmparr);
100 return if ($tmplist eq "");
102 $self->output("$ctx->{NAME}_$var =$tmplist\n");
105 sub PythonModule($$)
107 my ($self,$ctx) = @_;
109 $self->_prepare_list($ctx, "FULL_OBJ_LIST");
110 $self->_prepare_list($ctx, "DEPEND_LIST");
111 $self->_prepare_list($ctx, "LINK_FLAGS");
113 $self->output("\$(eval \$(call python_c_module_template,$ctx->{LIBRARY_REALNAME},\$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST), \$($ctx->{NAME}\_FULL_OBJ_LIST) \$($ctx->{NAME}_LINK_FLAGS)))\n");
116 sub SharedModule($$)
118 my ($self,$ctx) = @_;
120 my $sane_subsystem = lc($ctx->{SUBSYSTEM});
121 $sane_subsystem =~ s/^lib//;
123 $self->output("PLUGINS += $ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}\n");
124 $self->output("\$(eval \$(call shared_module_install_template,$sane_subsystem,$ctx->{LIBRARY_REALNAME}))\n");
126 $self->_prepare_list($ctx, "FULL_OBJ_LIST");
127 $self->_prepare_list($ctx, "DEPEND_LIST");
128 $self->_prepare_list($ctx, "LINK_FLAGS");
130 if (defined($ctx->{INIT_FUNCTION}) and $ctx->{INIT_FUNCTION_TYPE} =~ /\(\*\)/ and not ($ctx->{INIT_FUNCTION} =~ /\(/)) {
131 $self->output("\$($ctx->{NAME}_OBJ_FILES): CFLAGS+=-D$ctx->{INIT_FUNCTION}=init_module\n");
134 $self->output("\$(eval \$(call shared_module_template,$ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME}, \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST), \$($ctx->{NAME}\_FULL_OBJ_LIST) \$($ctx->{NAME}_LINK_FLAGS)))\n");
137 if (defined($ctx->{ALIASES})) {
138 $self->output("\$(eval \$(foreach alias,". join(' ', @{$ctx->{ALIASES}}) . ",\$(call shared_module_alias_template,$ctx->{SHAREDDIR}/$ctx->{LIBRARY_REALNAME},$sane_subsystem,\$(alias))))\n");
142 sub StaticLibraryPrimitives($$)
144 my ($self,$ctx) = @_;
146 $self->output("$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
147 $self->_prepare_list($ctx, "FULL_OBJ_LIST");
150 sub SharedLibraryPrimitives($$)
152 my ($self,$ctx) = @_;
154 if (not grep(/STATIC_LIBRARY/, @{$ctx->{OUTPUT_TYPE}})) {
155 $self->output("$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
156 $self->_prepare_list($ctx, "FULL_OBJ_LIST");
160 sub SharedLibrary($$)
162 my ($self,$ctx) = @_;
164 $self->output("SHARED_LIBS += $ctx->{RESULT_SHARED_LIBRARY}\n");
166 $self->_prepare_list($ctx, "DEPEND_LIST");
167 $self->_prepare_list($ctx, "LINK_FLAGS");
169 $self->output("\$(eval \$(call shared_library_template,$ctx->{RESULT_SHARED_LIBRARY}, \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST), \$($ctx->{NAME}\_FULL_OBJ_LIST) \$($ctx->{NAME}_LINK_FLAGS),$ctx->{SHAREDDIR}/$ctx->{LIBRARY_SONAME},$ctx->{SHAREDDIR}/$ctx->{LIBRARY_DEBUGNAME}))\n");
172 sub MergedObj($$)
174 my ($self, $ctx) = @_;
176 $self->output("\$(call partial_link_template, $ctx->{OUTPUT}, \$($ctx->{NAME}_OBJ_FILES))\n");
179 sub InitFunctions($$)
181 my ($self, $ctx) = @_;
182 $self->output("\$($ctx->{NAME}_OBJ_FILES): CFLAGS+=-DSTATIC_$ctx->{NAME}_MODULES=\"\$($ctx->{NAME}_INIT_FUNCTIONS)$ctx->{INIT_FUNCTION_SENTINEL}\"\n");
185 sub StaticLibrary($$)
187 my ($self,$ctx) = @_;
189 $self->output("STATIC_LIBS += $ctx->{RESULT_STATIC_LIBRARY}\n") if ($ctx->{TYPE} eq "LIBRARY");
190 $self->output("$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
191 $self->output("$ctx->{RESULT_STATIC_LIBRARY}: \$($ctx->{NAME}_FULL_OBJ_LIST)\n");
194 sub Binary($$)
196 my ($self,$ctx) = @_;
198 unless (defined($ctx->{INSTALLDIR})) {
199 $self->output("BINARIES += $ctx->{TARGET_BINARY}\n");
200 } elsif ($ctx->{INSTALLDIR} eq "SBINDIR") {
201 $self->output("SBIN_PROGS += $ctx->{RESULT_BINARY}\n");
202 } elsif ($ctx->{INSTALLDIR} eq "BINDIR") {
203 $self->output("BIN_PROGS += $ctx->{RESULT_BINARY}\n");
206 $self->_prepare_list($ctx, "FULL_OBJ_LIST");
207 $self->_prepare_list($ctx, "DEPEND_LIST");
208 $self->_prepare_list($ctx, "LINK_FLAGS");
210 if (defined($ctx->{USE_HOSTCC}) && $ctx->{USE_HOSTCC} eq "YES") {
211 $self->output("\$(call host_binary_link_template, $ctx->{RESULT_BINARY}, \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST), \$($ctx->{NAME}_LINK_FLAGS))\n");
212 } else {
213 $self->output("\$(call binary_link_template, $ctx->{RESULT_BINARY}, \$($ctx->{NAME}_DEPEND_LIST) \$($ctx->{NAME}_FULL_OBJ_LIST), \$($ctx->{NAME}_LINK_FLAGS))\n");
217 sub write($$)
219 my ($self, $file) = @_;
221 $self->_prepare_mk_files();
223 $self->output("ALL_OBJS = " . array2oneperline($self->{all_objs}) . "\n");
225 open(MAKEFILE,">$file") || die ("Can't open $file\n");
226 print MAKEFILE $self->{output};
227 close(MAKEFILE);
229 print __FILE__.": creating $file\n";
232 my $sort_available = eval "use sort 'stable'; return 1;";
233 $sort_available = 0 unless defined($sort_available);
235 sub by_path {
236 return 1 if($a =~ m#^\-I/#);
237 return -1 if($b =~ m#^\-I/#);
238 return 0;
241 sub CFlags($$)
243 my ($self, $key) = @_;
245 my $srcdir = $self->{config}->{srcdir};
246 my $builddir = $self->{config}->{builddir};
248 my $src_ne_build = ($srcdir ne $builddir) ? 1 : 0;
250 return unless defined ($key->{FINAL_CFLAGS});
251 return unless (@{$key->{FINAL_CFLAGS}} > 0);
253 my @sorted_cflags = @{$key->{FINAL_CFLAGS}};
254 if ($sort_available) {
255 @sorted_cflags = sort by_path @{$key->{FINAL_CFLAGS}};
258 # Rewrite CFLAGS so that both the source and the build
259 # directories are in the path.
260 my @cflags = ();
261 foreach my $flag (@sorted_cflags) {
262 if($src_ne_build) {
263 if($flag =~ m#^-I([^/].*$)#) {
264 my $dir = $1;
265 if ($dir =~ /^\$\(/) {
266 push (@cflags, $flag);
267 next;
269 $dir =~ s#^\$\((?:src|build)dir\)/?##;
270 push(@cflags, "-I$builddir/$dir", "-I$srcdir/$dir");
271 next;
274 push(@cflags, $flag);
277 my $cflags = join(' ', @cflags);
279 $self->output("\$(patsubst %.ho,%.d,\$($key->{NAME}_OBJ_FILES:.o=.d)) \$($key->{NAME}_OBJ_FILES): CFLAGS+= $cflags\n");