r14874: Allow overriding LIBRARY_REALNAME
[samba.git] / source / build / smb_build / config_mk.pm
1 # Samba Build System
2 # - config.mk parsing functions
3 #
4 #  Copyright (C) Stefan (metze) Metzmacher 2004
5 #  Copyright (C) Jelmer Vernooij 2005
6 #  Released under the GNU GPL
7 #
8
9 package smb_build::config_mk;
10 use smb_build::input;
11 use File::Basename;
12
13 use strict;
14
15 my $section_types = {
16         "EXT_LIB" => {
17                 "LIBS"                  => "list",
18                 "CFLAGS"                => "list",
19                 "CPPFLAGS"              => "list",
20                 "LDFLAGS"               => "list",
21                 },
22         "SUBSYSTEM" => {
23                 "OBJ_FILES"             => "list",
24
25                 "REQUIRED_SUBSYSTEMS"   => "list",
26
27                 "ENABLE"                => "bool",
28
29                 "MANPAGE"               => "string",
30
31                 "PUBLIC_PROTO_HEADER"   => "string",
32                 "PRIVATE_PROTO_HEADER"  => "string",
33
34                 "PUBLIC_HEADERS"        => "list",
35
36                 "EXTRA_CFLAGS"          => "string",
37                 "STANDARD_VISIBILITY"   => "string"
38                 },
39         "MODULE" => {
40                 "SUBSYSTEM"             => "string",
41
42                 "INIT_FUNCTION"         => "string",
43                 "OBJ_FILES"             => "list",
44
45                 "REQUIRED_SUBSYSTEMS"   => "list",
46
47                 "ALIASES" => "list",
48
49                 "ENABLE"                => "bool",
50
51                 "OUTPUT_TYPE"           => "string",
52
53                 "MANPAGE"               => "string",
54                 "PRIVATE_PROTO_HEADER"  => "string",
55
56                 "EXTRA_CFLAGS"          => "string"
57                 },
58         "BINARY" => {
59                 "OBJ_FILES"             => "list",
60
61                 "REQUIRED_SUBSYSTEMS"   => "list",
62
63                 "ENABLE"                => "bool",
64
65                 "MANPAGE"               => "string",
66                 "INSTALLDIR"            => "string",
67                 "PRIVATE_PROTO_HEADER"  => "string",
68                 "PUBLIC_PROTO_HEADER"   => "string",
69                 "PUBLIC_HEADERS"        => "list", 
70
71                 "EXTRA_CFLAGS"          => "string",
72                 "STANDARD_VISIBILITY"   => "string"
73                 },
74         "LIBRARY" => {
75                 "VERSION"               => "string",
76                 "SO_VERSION"            => "string",
77                 "LIBRARY_REALNAME" => "string",
78                 
79                 "INIT_FUNCTION_TYPE"    => "string",
80
81                 "OBJ_FILES"             => "list",
82
83                 "DESCRIPTION"           => "string",
84
85                 "REQUIRED_SUBSYSTEMS"   => "list",
86
87                 "ENABLE"                => "bool",
88
89                 "MANPAGE"               => "string",
90
91                 "PUBLIC_HEADERS"        => "list",
92
93                 "PUBLIC_PROTO_HEADER"   => "string",
94                 "PRIVATE_PROTO_HEADER"  => "string",
95
96                 "EXTRA_CFLAGS"          => "string",
97                 "STANDARD_VISIBILITY"   => "string"
98                 }
99 };
100
101 use vars qw(@parsed_files);
102
103 @parsed_files = ();
104
105 ###########################################################
106 # The parsing function which parses the file
107 #
108 # $result = _parse_config_mk($filename)
109 #
110 # $filename -   the path of the config.mk file
111 #               which should be parsed
112 sub run_config_mk($$$)
113 {
114         sub run_config_mk($$$);
115         my ($input, $srcdir, $filename) = @_;
116         my $result;
117         my $linenum = -1;
118         my $infragment = 0;
119         my $section = "GLOBAL";
120         my $makefile = "";
121
122         push (@parsed_files, $srcdir."/".$filename);
123         
124         open(CONFIG_MK, $srcdir."/".$filename) or die("Can't open `$srcdir/$filename'\n");
125         my @lines = <CONFIG_MK>;
126         close(CONFIG_MK);
127
128         my $line = "";
129         my $prev = "";
130
131         foreach (@lines) {
132                 $linenum++;
133
134                 # lines beginning with '#' are ignored
135                 next if (/^\#.*$/);
136                 
137                 if (/^(.*)\\$/) {
138                         $prev .= $1;
139                         next;
140                 } else {
141                         $line = "$prev$_";
142                         $prev = "";
143                 }
144
145                 if ($line =~ /^\[([-a-zA-Z0-9_:]+)\][\t ]*$/) 
146                 {
147                         $section = $1;
148                         $infragment = 0;
149                         next;
150                 }
151
152                 # include
153                 if ($line =~ /^include (.*)$/) {
154                         $makefile .= run_config_mk($input, $srcdir, dirname($filename)."/$1");
155                         next;
156                 }
157
158                 # empty line
159                 if ($line =~ /^[ \t]*$/) {
160                         $section = "GLOBAL";
161                         if ($infragment) { $makefile.="\n"; }
162                         next;
163                 }
164
165                 # global stuff is considered part of the makefile
166                 if ($section eq "GLOBAL") {
167                         if (!$infragment) { $makefile.="\n"; }
168                         $makefile .= $line;
169                         $infragment = 1;
170                         next;
171                 }
172
173                 
174                 # Assignment
175                 if ($line =~ /^([a-zA-Z0-9_]+)[\t ]*=(.*)$/) {
176                         $result->{$section}{$1}{VAL} = $2;
177                         $result->{$section}{$1}{KEY} = $1;
178                 
179                         next;
180                 }
181
182                 die("$srcdir."/".$filename:$linenum: Bad line while parsing $srcdir."/".$filename");
183         }
184
185         foreach my $section (keys %{$result}) {
186                 my ($type, $name) = split(/::/, $section, 2);
187
188                 my $sectype = $section_types->{$type};
189                 if (not defined($sectype)) {
190                         die($srcdir."/".$filename.":[".$section."] unknown section type \"".$type."\"!");
191                 }
192
193                 $input->{$name}{NAME} = $name;
194                 $input->{$name}{TYPE} = $type;
195                 $input->{$name}{MK_FILE} = $srcdir."/".$filename;
196                 $input->{$name}{BASEDIR} = dirname($filename);
197
198                 foreach my $key (values %{$result->{$section}}) {
199                         $key->{VAL} = smb_build::input::strtrim($key->{VAL});
200                         my $vartype = $sectype->{$key->{KEY}};
201                         if (not defined($vartype)) {
202                                 die($srcdir."/".$filename.":[".$section."]: unknown attribute type \"$key->{KEY}\"!");
203                         }
204                         if ($vartype eq "string") {
205                                 $input->{$name}{$key->{KEY}} = $key->{VAL};
206                         } elsif ($vartype eq "list") {
207                                 $input->{$name}{$key->{KEY}} = [smb_build::input::str2array($key->{VAL})];
208                         } elsif ($vartype eq "bool") {
209                                 if (($key->{VAL} ne "YES") and ($key->{VAL} ne "NO")) {
210                                         die("Invalid value for bool attribute $key->{KEY}: $key->{VAL} in section $section");
211                                 }
212                                 $input->{$name}{$key->{KEY}} = $key->{VAL};
213                         }
214                 }
215         }
216
217         return $makefile;
218 }
219
220 1;