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::output;
|
---|
10 | use File::Basename;
|
---|
11 | use strict;
|
---|
12 |
|
---|
13 | use Cwd 'abs_path';
|
---|
14 |
|
---|
15 | sub new($$$)
|
---|
16 | {
|
---|
17 | my ($myname, $config, $mkfile) = @_;
|
---|
18 | my $self = {};
|
---|
19 |
|
---|
20 | bless($self, $myname);
|
---|
21 |
|
---|
22 | $self->_set_config($config);
|
---|
23 |
|
---|
24 | $self->{output} = "";
|
---|
25 |
|
---|
26 | $self->output("################################################\n");
|
---|
27 | $self->output("# Autogenerated by source4/build/smb_build/makefile.pm #\n");
|
---|
28 | $self->output("################################################\n");
|
---|
29 | $self->output("\n");
|
---|
30 | $self->output($mkfile);
|
---|
31 |
|
---|
32 | return $self;
|
---|
33 | }
|
---|
34 |
|
---|
35 | sub _set_config($$)
|
---|
36 | {
|
---|
37 | my ($self, $config) = @_;
|
---|
38 |
|
---|
39 | $self->{config} = $config;
|
---|
40 |
|
---|
41 | if (not defined($self->{config}->{srcdir})) {
|
---|
42 | $self->{config}->{srcdir} = '.';
|
---|
43 | }
|
---|
44 |
|
---|
45 | if (not defined($self->{config}->{builddir})) {
|
---|
46 | $self->{config}->{builddir} = '.';
|
---|
47 | }
|
---|
48 |
|
---|
49 | if ($self->{config}->{prefix} eq "NONE") {
|
---|
50 | $self->{config}->{prefix} = $self->{config}->{ac_default_prefix};
|
---|
51 | }
|
---|
52 |
|
---|
53 | if ($self->{config}->{exec_prefix} eq "NONE") {
|
---|
54 | $self->{config}->{exec_prefix} = $self->{config}->{prefix};
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | sub output($$)
|
---|
59 | {
|
---|
60 | my ($self, $text) = @_;
|
---|
61 |
|
---|
62 | $self->{output} .= $text;
|
---|
63 | }
|
---|
64 |
|
---|
65 | sub _prepare_mk_files($)
|
---|
66 | {
|
---|
67 | my $self = shift;
|
---|
68 | my @tmp = ();
|
---|
69 |
|
---|
70 | foreach (@smb_build::config_mk::parsed_files) {
|
---|
71 | s/ .*$//g;
|
---|
72 | push (@tmp, $_);
|
---|
73 | }
|
---|
74 |
|
---|
75 | $self->output("MK_FILES = " . array2oneperline(\@tmp) . "\n");
|
---|
76 | }
|
---|
77 |
|
---|
78 | sub array2oneperline($)
|
---|
79 | {
|
---|
80 | my $array = shift;
|
---|
81 | my $output = "";
|
---|
82 |
|
---|
83 | foreach (@$array) {
|
---|
84 | next unless defined($_);
|
---|
85 |
|
---|
86 | $output .= " \\\n\t\t$_";
|
---|
87 | }
|
---|
88 |
|
---|
89 | return $output;
|
---|
90 | }
|
---|
91 |
|
---|
92 | sub _prepare_list($$$)
|
---|
93 | {
|
---|
94 | my ($self,$ctx,$var) = @_;
|
---|
95 | my @tmparr = ();
|
---|
96 |
|
---|
97 | push(@tmparr, @{$ctx->{$var}}) if defined($ctx->{$var});
|
---|
98 |
|
---|
99 | my $tmplist = array2oneperline(\@tmparr);
|
---|
100 | return if ($tmplist eq "");
|
---|
101 |
|
---|
102 | $self->output("$ctx->{NAME}_$var =$tmplist\n");
|
---|
103 | }
|
---|
104 |
|
---|
105 | sub PythonModule($$)
|
---|
106 | {
|
---|
107 | my ($self,$ctx) = @_;
|
---|
108 |
|
---|
109 | $self->_prepare_list($ctx, "FULL_OBJ_LIST");
|
---|
110 | $self->_prepare_list($ctx, "DEPEND_LIST");
|
---|
111 | $self->_prepare_list($ctx, "LINK_FLAGS");
|
---|
112 |
|
---|
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");
|
---|
114 | }
|
---|
115 |
|
---|
116 | sub SharedModule($$)
|
---|
117 | {
|
---|
118 | my ($self,$ctx) = @_;
|
---|
119 |
|
---|
120 | my $sane_subsystem = lc($ctx->{SUBSYSTEM});
|
---|
121 | $sane_subsystem =~ s/^lib//;
|
---|
122 |
|
---|
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");
|
---|
125 |
|
---|
126 | $self->_prepare_list($ctx, "FULL_OBJ_LIST");
|
---|
127 | $self->_prepare_list($ctx, "DEPEND_LIST");
|
---|
128 | $self->_prepare_list($ctx, "LINK_FLAGS");
|
---|
129 |
|
---|
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}=samba_init_module\n");
|
---|
132 | }
|
---|
133 |
|
---|
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");
|
---|
135 |
|
---|
136 |
|
---|
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");
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | sub StaticLibraryPrimitives($$)
|
---|
143 | {
|
---|
144 | my ($self,$ctx) = @_;
|
---|
145 |
|
---|
146 | $self->output("$ctx->{NAME}_OUTPUT = $ctx->{OUTPUT}\n");
|
---|
147 | $self->_prepare_list($ctx, "FULL_OBJ_LIST");
|
---|
148 | }
|
---|
149 |
|
---|
150 | sub SharedLibraryPrimitives($$)
|
---|
151 | {
|
---|
152 | my ($self,$ctx) = @_;
|
---|
153 |
|
---|
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");
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | sub SharedLibrary($$)
|
---|
161 | {
|
---|
162 | my ($self,$ctx) = @_;
|
---|
163 |
|
---|
164 | $self->output("SHARED_LIBS += $ctx->{RESULT_SHARED_LIBRARY}\n");
|
---|
165 |
|
---|
166 | $self->_prepare_list($ctx, "DEPEND_LIST");
|
---|
167 | $self->_prepare_list($ctx, "LINK_FLAGS");
|
---|
168 |
|
---|
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");
|
---|
170 | }
|
---|
171 |
|
---|
172 | sub MergedObj($$)
|
---|
173 | {
|
---|
174 | my ($self, $ctx) = @_;
|
---|
175 |
|
---|
176 | $self->output("\$(call partial_link_template, $ctx->{OUTPUT}, \$($ctx->{NAME}_OBJ_FILES))\n");
|
---|
177 | }
|
---|
178 |
|
---|
179 | sub InitFunctions($$)
|
---|
180 | {
|
---|
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");
|
---|
183 | }
|
---|
184 |
|
---|
185 | sub StaticLibrary($$)
|
---|
186 | {
|
---|
187 | my ($self,$ctx) = @_;
|
---|
188 |
|
---|
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");
|
---|
192 | }
|
---|
193 |
|
---|
194 | sub Binary($$)
|
---|
195 | {
|
---|
196 | my ($self,$ctx) = @_;
|
---|
197 |
|
---|
198 | unless (defined($ctx->{INSTALLDIR})) {
|
---|
199 | } elsif ($ctx->{INSTALLDIR} eq "SBINDIR") {
|
---|
200 | $self->output("\$(eval \$(call sbinary_install_template,$ctx->{RESULT_BINARY}))\n");
|
---|
201 | } elsif ($ctx->{INSTALLDIR} eq "BINDIR") {
|
---|
202 | $self->output("\$(eval \$(call binary_install_template,$ctx->{RESULT_BINARY}))\n");
|
---|
203 | }
|
---|
204 |
|
---|
205 | $self->_prepare_list($ctx, "FULL_OBJ_LIST");
|
---|
206 | $self->_prepare_list($ctx, "DEPEND_LIST");
|
---|
207 | $self->_prepare_list($ctx, "LINK_FLAGS");
|
---|
208 |
|
---|
209 | if (defined($ctx->{USE_HOSTCC}) && $ctx->{USE_HOSTCC} eq "YES") {
|
---|
210 | $self->output("\$(eval \$(call host_binary_link_template, $ctx->{RESULT_BINARY}, \$($ctx->{NAME}_FULL_OBJ_LIST) \$($ctx->{NAME}_DEPEND_LIST), \$($ctx->{NAME}_LINK_FLAGS)))\n");
|
---|
211 | } else {
|
---|
212 | $self->output("\$(eval \$(call binary_link_template, $ctx->{RESULT_BINARY}, \$($ctx->{NAME}_FULL_OBJ_LIST) \$($ctx->{NAME}_DEPEND_LIST), \$($ctx->{NAME}_LINK_FLAGS)))\n");
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | sub write($$)
|
---|
217 | {
|
---|
218 | my ($self, $file) = @_;
|
---|
219 |
|
---|
220 | $self->_prepare_mk_files();
|
---|
221 |
|
---|
222 | $self->output("ALL_OBJS = " . array2oneperline($self->{all_objs}) . "\n");
|
---|
223 |
|
---|
224 | open(MAKEFILE,">$file") || die ("Can't open $file\n");
|
---|
225 | print MAKEFILE $self->{output};
|
---|
226 | close(MAKEFILE);
|
---|
227 |
|
---|
228 | print __FILE__.": creating $file\n";
|
---|
229 | }
|
---|
230 |
|
---|
231 | my $sort_available = eval "use sort 'stable'; return 1;";
|
---|
232 | $sort_available = 0 unless defined($sort_available);
|
---|
233 |
|
---|
234 | sub by_path {
|
---|
235 | return 1 if($a =~ m#^\-I/#);
|
---|
236 | return -1 if($b =~ m#^\-I/#);
|
---|
237 | return 0;
|
---|
238 | }
|
---|
239 |
|
---|
240 | sub CFlags($$)
|
---|
241 | {
|
---|
242 | my ($self, $key) = @_;
|
---|
243 |
|
---|
244 | my $srcdir = $self->{config}->{srcdir};
|
---|
245 | my $builddir = $self->{config}->{builddir};
|
---|
246 |
|
---|
247 | my $src_ne_build = ($srcdir ne $builddir) ? 1 : 0;
|
---|
248 |
|
---|
249 | return unless defined ($key->{FINAL_CFLAGS});
|
---|
250 | return unless (@{$key->{FINAL_CFLAGS}} > 0);
|
---|
251 |
|
---|
252 | my @sorted_cflags = @{$key->{FINAL_CFLAGS}};
|
---|
253 | if ($sort_available) {
|
---|
254 | @sorted_cflags = sort by_path @{$key->{FINAL_CFLAGS}};
|
---|
255 | }
|
---|
256 |
|
---|
257 | # Rewrite CFLAGS so that both the source and the build
|
---|
258 | # directories are in the path.
|
---|
259 | my @cflags = ();
|
---|
260 | foreach my $flag (@sorted_cflags) {
|
---|
261 | if($src_ne_build) {
|
---|
262 | if($flag =~ m#^-I([^/].*$)#) {
|
---|
263 | my $dir = $1;
|
---|
264 | if ($dir =~ /^\$\(/) {
|
---|
265 | push (@cflags, $flag);
|
---|
266 | next;
|
---|
267 | }
|
---|
268 | $dir =~ s#^\$\((?:src|build)dir\)/?##;
|
---|
269 | push(@cflags, "-I$builddir/$dir", "-I$srcdir/$dir");
|
---|
270 | next;
|
---|
271 | }
|
---|
272 | }
|
---|
273 | push(@cflags, $flag);
|
---|
274 | }
|
---|
275 |
|
---|
276 | my $cflags = join(' ', @cflags);
|
---|
277 |
|
---|
278 | $self->output("\$(patsubst %.ho,%.d,\$($key->{NAME}_OBJ_FILES:.o=.d)) \$($key->{NAME}_OBJ_FILES): CFLAGS+= $cflags\n");
|
---|
279 | }
|
---|
280 |
|
---|
281 | 1;
|
---|