r22032: Make sure static libraries end up in bin/static.
[gd/samba/.git] / source4 / build / smb_build / output.pm
1 # SMB Build System
2 # - the output generating functions
3 #
4 #  Copyright (C) Stefan (metze) Metzmacher 2004
5 #  Copyright (C) Jelmer Vernooij 2004
6 #  Released under the GNU GPL
7
8 package output;
9 use strict;
10
11 sub add_dir($$)
12 {
13         my ($dir,$files) = @_;
14         my @ret = ();
15         my $dirsep = "/";
16
17         $dir =~ s/^\.$//g;
18         $dir =~ s/^\.\///g;
19
20         $dirsep = "" if ($dir eq "");
21         
22         foreach (@$files) {
23                 if (substr($_, 0, 1) ne "\$") {
24                         $_ = "$dir$dirsep$_";
25                         s/([^\/\.]+)\/\.\.\///g;
26                         s/([^\/\.]+)\/\.\.\///g;
27                 }
28                 push (@ret, $_);
29         }
30         
31         return @ret;
32 }
33
34 sub generate_shared_library($)
35 {
36         my $lib = shift;
37         my $link_name;
38         my $lib_name;
39
40         $lib->{DEPEND_LIST} = [];
41
42         $link_name = lc($lib->{NAME});
43         $lib_name = $link_name;
44
45         if ($lib->{TYPE} eq "LIBRARY") {
46                 $link_name = $lib->{NAME};
47                 $link_name =~ s/^LIB//;
48                 $link_name = lc($link_name);
49                 $lib_name = "lib$link_name";
50         }
51
52         if (defined($lib->{LIBRARY_REALNAME})) {
53                 $lib->{BASEDIR} =~ s/^\.\///g;
54                 $lib->{LIBRARY_REALNAME} = "$lib->{LIBRARY_REALNAME}";
55                 $lib->{SHAREDDIR} = $lib->{BASEDIR};
56         } else {
57                 if ($lib->{TYPE} eq "MODULE") {
58                         $lib->{SHAREDDIR} = "bin/modules/$lib->{SUBSYSTEM}";
59                         $lib->{LIBRARY_REALNAME} = $link_name;
60                         $lib->{LIBRARY_REALNAME} =~ s/^$lib->{SUBSYSTEM}_//g;
61                         $lib->{LIBRARY_REALNAME}.= ".\$(SHLIBEXT)";
62                 } else {
63                         $lib->{SHAREDDIR} = "bin/shared";
64                         $lib->{LIBRARY_REALNAME} = "$lib_name.\$(SHLIBEXT)";
65                 }
66         }
67
68         $lib->{LIBRARY_DEBUGNAME} = $lib->{LIBRARY_REALNAME};
69
70         if (defined($lib->{VERSION})) {
71                 $lib->{LIBRARY_SONAME} = "$lib->{LIBRARY_REALNAME}.$lib->{SO_VERSION}";
72                 $lib->{LIBRARY_REALNAME} = "$lib->{LIBRARY_REALNAME}.$lib->{VERSION}";
73         } 
74         
75         $lib->{TARGET_SHARED_LIBRARY} = "$lib->{SHAREDDIR}/$lib->{LIBRARY_REALNAME}";
76         $lib->{OUTPUT_SHARED_LIBRARY} = $lib->{TARGET_SHARED_LIBRARY};
77 }
78
79 sub generate_static_library($)
80 {
81         my $lib = shift;
82         my $link_name;
83
84         $lib->{DEPEND_LIST} = [];
85
86         $link_name = $lib->{NAME};
87         $link_name =~ s/^LIB//;
88
89         $lib->{LIBRARY_NAME} = "lib".lc($link_name).".a";
90
91         if (defined($lib->{OBJ_FILES})) {
92                 $lib->{TARGET_STATIC_LIBRARY} = "bin/static/$lib->{LIBRARY_NAME}";
93                 $lib->{STATICDIR} = 'bin/static';
94         } else {
95                 $lib->{TARGET_STATIC_LIBRARY} = "";
96         }
97         $lib->{OUTPUT_STATIC_LIBRARY} = $lib->{TARGET_STATIC_LIBRARY};
98 }
99
100 sub generate_binary($)
101 {
102         my $bin = shift;
103
104         $bin->{DEPEND_LIST} = [];
105         push(@{$bin->{LINK_FLAGS}}, "\$($bin->{TYPE}_$bin->{NAME}\_OBJ_LIST)");
106
107         $bin->{DEBUGDIR} = "bin/";
108         $bin->{TARGET_BINARY} = $bin->{OUTPUT_BINARY} = "$bin->{DEBUGDIR}/$bin->{NAME}";
109         $bin->{BINARY} = $bin->{NAME};
110 }
111
112 sub merge_array($$)
113 {
114         # $dest is a reference to an array
115         # $src is an array
116         my ($dest, $src) = @_;
117
118         return unless defined($src);
119         return unless ($#{$src} >= 0);
120
121         foreach my $line (@{$src}) {
122                 next if (grep /^$line$/, @{$$dest});
123                 push(@{$$dest}, $line);
124         }
125 }
126
127 sub create_output($$)
128 {
129         my ($depend, $config) = @_;
130         my $part;
131
132         foreach $part (values %{$depend}) {
133                 next unless(defined($part->{OUTPUT_TYPE}));
134
135                 # Combine object lists
136                 push(@{$part->{OBJ_LIST}}, add_dir($part->{BASEDIR}, $part->{OBJ_FILES})) if defined($part->{OBJ_FILES});
137
138                 generate_binary($part) if grep(/BINARY/, @{$part->{OUTPUT_TYPE}});
139                 generate_shared_library($part) if grep(/SHARED_LIBRARY/, @{$part->{OUTPUT_TYPE}});
140                 generate_static_library($part) if grep(/STATIC_LIBRARY/, @{$part->{OUTPUT_TYPE}});
141                 $part->{OUTPUT} = $part->{"OUTPUT_" . @{$part->{OUTPUT_TYPE}}[0]};
142                 $part->{TARGET} = $part->{"TARGET_" . @{$part->{OUTPUT_TYPE}}[0]};
143         }
144
145         foreach $part (values %{$depend}) {
146                 next if not defined($part->{OUTPUT_TYPE});
147
148                 merge_array(\$part->{FINAL_CFLAGS}, $part->{CPPFLAGS});
149                 merge_array(\$part->{FINAL_CFLAGS}, $part->{CFLAGS});
150
151                 foreach (reverse @{$part->{UNIQUE_DEPENDENCIES_ALL}}) {
152                         my $elem = $depend->{$_};
153                         next if $elem == $part;
154
155                         merge_array(\$part->{FINAL_CFLAGS}, $elem->{CPPFLAGS});
156                         merge_array(\$part->{FINAL_CFLAGS}, $elem->{CFLAGS});
157                 }
158
159                 # Always import the link options of the unique dependencies
160                 foreach (@{$part->{UNIQUE_DEPENDENCIES}}) {
161                         my $elem = $depend->{$_};
162                         next if $elem == $part;
163
164                         push(@{$part->{LINK_FLAGS}}, $elem->{OUTPUT}) if defined($elem->{OUTPUT});
165                         push(@{$part->{LINK_FLAGS}}, @{$elem->{LIBS}}) if defined($elem->{LIBS});
166                         push(@{$part->{LINK_FLAGS}},@{$elem->{LDFLAGS}}) if defined($elem->{LDFLAGS});
167                         push(@{$part->{DEPEND_LIST}}, $elem->{TARGET}) if defined($elem->{TARGET});
168                 }
169         }
170
171         foreach $part (values %{$depend}) {
172                 if (($part->{STANDARD_VISIBILITY} ne "default") and 
173                         ($config->{visibility_attribute} eq "yes")) {
174                         push(@{$part->{FINAL_CFLAGS}}, "-fvisibility=$part->{STANDARD_VISIBILITY}");
175                 }
176         }
177
178         return $depend;
179 }
180
181 1;