r19582: Support building both shared and static versions of libraries at the same
[jelmer/samba4-debian.git] / source / build / smb_build / input.pm
1 # Samba Build System
2 # - the input checking functions
3 #
4 #  Copyright (C) Stefan (metze) Metzmacher 2004
5 #  Copyright (C) Jelmer Vernooij 2004
6 #  Released under the GNU GPL
7
8 use smb_build::config;
9 use strict;
10 package smb_build::input;
11
12 my $srcdir = $config::config{srcdir};
13
14 sub strtrim($)
15 {
16         $_ = shift;
17         s/^[\t\n ]*//g;
18         s/[\t\n ]*$//g;
19         return $_;
20 }
21
22 sub str2array($)
23 {
24         $_ = shift;
25         s/^[\t\n ]*//g;
26         s/[\t\n ]*$//g;
27         s/([\t\n ]+)/ /g;
28
29         return () if (length($_)==0);
30         return split /[ \t\n]/;
31 }
32
33 sub add_libreplace($)
34 {
35         my ($part) = @_;
36
37         return if ($part->{NAME} eq "LIBREPLACE");
38         return if ($part->{NAME} eq "LIBREPLACE_HOSTCC");
39         return if ($part->{NAME} eq "REPLACE_READDIR");
40
41         foreach my $n (@{$part->{PRIVATE_DEPENDENCIES}}) {
42                 return if ($n eq "LIBREPLACE");
43                 return if ($n eq "LIBREPLACE_HOSTCC");
44         }
45         foreach my $n (@{$part->{PUBLIC_DEPENDENCIES}}) {
46                 return if ($n eq "LIBREPLACE");
47                 return if ($n eq "LIBREPLACE_HOSTCC");
48         }
49
50         if (defined($part->{USE_HOSTCC}) && $part->{USE_HOSTCC} eq "YES") {
51                 unshift (@{$part->{PRIVATE_DEPENDENCIES}}, "LIBREPLACE_HOSTCC");
52         } else {
53                 unshift (@{$part->{PRIVATE_DEPENDENCIES}}, "LIBREPLACE");
54         }
55 }
56
57 sub check_subsystem($$$)
58 {
59         my ($INPUT, $subsys, $default_ot) = @_;
60         return if ($subsys->{ENABLE} ne "YES");
61         
62         unless(defined($subsys->{OUTPUT_TYPE})) {
63                 $subsys->{OUTPUT_TYPE} = $default_ot;
64         }
65         add_libreplace($subsys);
66 }
67
68 sub check_module($$$)
69 {
70         my ($INPUT, $mod, $default_ot) = @_;
71
72         die("Module $mod->{NAME} does not have a SUBSYSTEM set") if not defined($mod->{SUBSYSTEM});
73
74         my $use_default = 0;
75         
76         if (not exists($INPUT->{$mod->{SUBSYSTEM}}{INIT_FUNCTIONS})) {
77                 $INPUT->{$mod->{SUBSYSTEM}}{INIT_FUNCTIONS} = [];
78         }
79
80         if (!(defined($INPUT->{$mod->{SUBSYSTEM}}))) {
81                 $mod->{ENABLE} = "NO";
82                 return;
83         }
84
85         return if ($mod->{ENABLE} ne "YES");
86
87         if (exists($INPUT->{$mod->{SUBSYSTEM}}{INIT_FUNCTION_TYPE})) {
88                 $mod->{INIT_FUNCTION_TYPE} = $INPUT->{$mod->{SUBSYSTEM}}{INIT_FUNCTION_TYPE};
89         } else {
90                 $mod->{INIT_FUNCTION_TYPE} = "NTSTATUS (*) (void)";
91         }
92
93         if (not defined($mod->{OUTPUT_TYPE})) {
94                 $mod->{OUTPUT_TYPE} = $default_ot;
95         }
96
97         if (grep(/SHARED_LIBRARY/, @{$mod->{OUTPUT_TYPE}})) {
98                 $mod->{INSTALLDIR} = "MODULESDIR/$mod->{SUBSYSTEM}";
99                 push (@{$mod->{PRIVATE_DEPENDENCIES}}, $mod->{SUBSYSTEM});
100         } 
101         if (grep(/INTEGRATED/, @{$mod->{OUTPUT_TYPE}})) {
102                 push (@{$INPUT->{$mod->{SUBSYSTEM}}{INIT_FUNCTIONS}}, $mod->{INIT_FUNCTION}) if defined($mod->{INIT_FUNCTION});
103         }
104         add_libreplace($mod);
105 }
106
107 sub check_library($$$)
108 {
109         my ($INPUT, $lib, $default_ot) = @_;
110
111         return if ($lib->{ENABLE} ne "YES");
112
113         $lib->{OUTPUT_TYPE} = $default_ot;
114
115         if (defined($lib->{VERSION}) and not defined($lib->{SO_VERSION})) {
116                 print "$lib->{NAME}: Please specify SO_VERSION when specifying VERSION\n";
117                 return;
118         }
119
120         if (defined($lib->{SO_VERSION}) and not defined($lib->{VERSION})) {
121                 print "$lib->{NAME}: Please specify VERSION when specifying SO_VERSION\n";
122                 return;
123         }
124
125         unless (defined($lib->{INIT_FUNCTION_TYPE})) {
126                 $lib->{INIT_FUNCTION_TYPE} = "NTSTATUS (*) (void)";
127         }
128
129         $lib->{INSTALLDIR} = "LIBDIR";
130         add_libreplace($lib);
131 }
132
133 sub check_binary($$)
134 {
135         my ($INPUT, $bin) = @_;
136
137         return if ($bin->{ENABLE} ne "YES");
138
139         ($bin->{BINARY} = (lc $bin->{NAME})) if not defined($bin->{BINARY});
140
141         $bin->{OUTPUT_TYPE} = ["BINARY"];
142         add_libreplace($bin);
143 }
144
145 sub import_integrated($$)
146 {
147         my ($lib, $depend) = @_;
148
149         foreach my $mod (values %$depend) {
150                 next if(not defined($mod->{OUTPUT_TYPE}));
151                 next if(not grep(/INTEGRATED/, @{$mod->{OUTPUT_TYPE}}));
152                 next if(not defined($mod->{SUBSYSTEM}));
153                 next if($mod->{SUBSYSTEM} ne $lib->{NAME});
154                 next if($mod->{ENABLE} ne "YES");
155
156                 push (@{$lib->{FULL_OBJ_LIST}}, "\$($mod->{TYPE}_$mod->{NAME}_FULL_OBJ_LIST)");
157                 push (@{$lib->{LINK_FLAGS}}, "\$($mod->{TYPE}_$mod->{NAME}_LINK_FLAGS)");
158                 push (@{$lib->{PRIVATE_DEPENDENCIES}}, @{$mod->{PUBLIC_DEPENDENCIES}}) if defined($mod->{PUBLIC_DEPENDENCIES});
159                 push (@{$lib->{PRIVATE_DEPENDENCIES}}, @{$mod->{PRIVATE_DEPENDENCIES}}) if defined($mod->{PRIVATE_DEPENDENCIES});
160
161                 $mod->{ENABLE} = "NO";
162         }
163 }
164
165 sub calc_unique_deps($$$$$$)
166 {
167         sub calc_unique_deps($$$$$$);
168         my ($name, $INPUT, $deps, $udeps, $withlibs, $busy) = @_;
169
170         foreach my $n (@$deps) {
171                 die("Dependency unknown: $n") unless (defined($INPUT->{$n}));
172                 die("Recursive dependency: $n, list: " . join(',', @$busy)) if (grep (/^$n$/, @$busy));
173                 next if (grep /^$n$/, @$udeps);
174                 my $dep = $INPUT->{$n};
175
176                 if (defined ($dep->{OUTPUT_TYPE}) && 
177                         ($withlibs or 
178                         (@{$dep->{OUTPUT_TYPE}}[0] eq "INTEGRATED") or 
179                         (@{$dep->{OUTPUT_TYPE}}[0] eq "STATIC_LIBRARY"))) {
180                                 push (@$busy, $dep->{NAME});
181                                 calc_unique_deps($dep->{NAME}, $INPUT, $dep->{PUBLIC_DEPENDENCIES}, $udeps, $withlibs, $busy);
182                                 calc_unique_deps($dep->{NAME}, $INPUT, $dep->{PRIVATE_DEPENDENCIES}, $udeps, $withlibs, $busy);
183                                 pop (@$busy);
184                 }
185
186                 unshift (@{$udeps}, $dep->{NAME});
187         }
188 }
189
190 sub check($$$$$)
191 {
192         my ($INPUT, $enabled, $subsys_ot, $lib_ot, $module_ot) = @_;
193
194         foreach my $part (values %$INPUT) {
195                 unless (defined($part->{STANDARD_VISIBILITY})) {
196                         if ($part->{TYPE} eq "MODULE" or $part->{TYPE} eq "BINARY") {
197                                 $part->{STANDARD_VISIBILITY} = "hidden";
198                         } else {
199                                 $part->{STANDARD_VISIBILITY} = "default";
200                         }
201                 }
202
203                 unless (defined($part->{PUBLIC_HEADERS})) {
204                         $part->{PUBLIC_HEADERS} = [];
205                 }
206                 
207                 if (defined($part->{PUBLIC_PROTO_HEADER})) {
208                         push (@{$part->{PUBLIC_HEADERS}}, $part->{PUBLIC_PROTO_HEADER});
209                 }
210
211                 if (defined($enabled->{$part->{NAME}})) { 
212                         $part->{ENABLE} = $enabled->{$part->{NAME}};
213                         next;
214                 }
215                 
216                 unless(defined($part->{ENABLE})) {
217                         if ($part->{TYPE} eq "EXT_LIB") {
218                                 $part->{ENABLE} = "NO";
219                         } else {
220                                 $part->{ENABLE} = "YES";
221                         }
222                 }
223         }
224
225         foreach my $k (keys %$INPUT) {
226                 my $part = $INPUT->{$k};
227
228                 $part->{LINK_FLAGS} = [];
229                 $part->{FULL_OBJ_LIST} = ["\$($part->{TYPE}_$part->{NAME}_OBJ_LIST)"];
230
231                 check_subsystem($INPUT, $part, $subsys_ot) if ($part->{TYPE} eq "SUBSYSTEM");
232                 check_module($INPUT, $part, $module_ot) if ($part->{TYPE} eq "MODULE");
233                 check_library($INPUT, $part, $lib_ot) if ($part->{TYPE} eq "LIBRARY");
234                 check_binary($INPUT, $part) if ($part->{TYPE} eq "BINARY");
235         }
236
237         foreach my $part (values %$INPUT) {
238                 if (defined($part->{INIT_FUNCTIONS})) {
239                         push (@{$part->{LINK_FLAGS}}, "\$(DYNEXP)");
240                 }
241                 import_integrated($part, $INPUT);
242         }
243
244         foreach my $part (values %$INPUT) {
245                 $part->{UNIQUE_DEPENDENCIES} = [];
246                 calc_unique_deps($part->{NAME}, $INPUT, $part->{PUBLIC_DEPENDENCIES}, $part->{UNIQUE_DEPENDENCIES}, 0, []);
247                 calc_unique_deps($part->{NAME}, $INPUT, $part->{PRIVATE_DEPENDENCIES}, $part->{UNIQUE_DEPENDENCIES}, 0, []);
248         }
249
250         foreach my $part (values %$INPUT) {
251                 $part->{UNIQUE_DEPENDENCIES_ALL} = [];
252                 calc_unique_deps($part->{NAME}, $INPUT, $part->{PUBLIC_DEPENDENCIES}, $part->{UNIQUE_DEPENDENCIES_ALL}, 1, []);
253                 calc_unique_deps($part->{NAME}, $INPUT, $part->{PRIVATE_DEPENDENCIES}, $part->{UNIQUE_DEPENDENCIES_ALL}, 1, []);
254         }
255
256         return $INPUT;
257 }
258
259 1;