r12589: Add missing newline.
[samba.git] / source4 / 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                 "INIT_FUNCTION"         => "string",
24                 "OBJ_FILES"             => "list",
25
26                 "REQUIRED_SUBSYSTEMS"   => "list",
27
28                 "ENABLE"                => "bool",
29                 "NOPROTO"               => "bool",
30
31                 "MANPAGE"               => "string",
32
33                 "PUBLIC_PROTO_HEADER" => "string",
34                 "PRIVATE_PROTO_HEADER" => "string"
35                 },
36         "MODULE" => {
37                 "SUBSYSTEM"             => "string",
38
39                 "INIT_FUNCTION"         => "string",
40                 "OBJ_FILES"             => "list",
41
42                 "REQUIRED_SUBSYSTEMS"   => "list",
43
44                 "ENABLE"                => "bool",
45                 "NOPROTO"               => "bool",
46
47                 "MANPAGE"               => "string",
48                 "PRIVATE_PROTO_HEADER" => "string"
49                 },
50         "BINARY" => {
51                 "OBJ_FILES"             => "list",
52
53                 "REQUIRED_SUBSYSTEMS"   => "list",
54
55                 "ENABLE"                => "bool",
56                 "NOPROTO"               => "bool",
57
58                 "MANPAGE"               => "string",
59                 "INSTALLDIR"            => "string",
60                 "PRIVATE_PROTO_HEADER" => "string"
61                 },
62         "LIBRARY" => {
63                 "MAJOR_VERSION"         => "string",
64                 "MINOR_VERSION"         => "string",
65                 "RELEASE_VERSION"       => "string",
66
67                 "INIT_FUNCTION"         => "string",
68                 "OBJ_FILES"             => "list",
69
70                 "DESCRIPTION" => "string",
71
72                 "REQUIRED_SUBSYSTEMS"   => "list",
73
74                 "ENABLE"                => "bool",
75                 "NOPROTO"               => "bool",
76
77                 "MANPAGE"               => "string",
78
79                 "PUBLIC_HEADERS" => "list",
80
81                 "PUBLIC_PROTO_HEADER" => "string",
82                 "PRIVATE_PROTO_HEADER" => "string"
83                 }
84 };
85
86 use vars qw(@parsed_files);
87
88 @parsed_files = ();
89
90 ###########################################################
91 # The parsing function which parses the file
92 #
93 # $result = _parse_config_mk($filename)
94 #
95 # $filename -   the path of the config.mk file
96 #               which should be parsed
97 sub run_config_mk($$)
98 {
99         sub run_config_mk($$);
100         my ($input, $filename) = @_;
101         my $result;
102         my $linenum = -1;
103         my $infragment = 0;
104         my $section = "GLOBAL";
105         my $makefile = "";
106
107         push (@parsed_files, $filename);
108         
109         open(CONFIG_MK, $filename) or die("Can't open `$filename'\n");
110         my @lines = <CONFIG_MK>;
111         close(CONFIG_MK);
112
113         my $line = "";
114         my $prev = "";
115
116         foreach (@lines) {
117                 $linenum++;
118
119                 # lines beginning with '#' are ignored
120                 next if (/^\#.*$/);
121                 
122                 if (/^(.*)\\$/) {
123                         $prev .= $1;
124                         next;
125                 } else {
126                         $line = "$prev$_";
127                         $prev = "";
128                 }
129
130                 if ($line =~ /^\[([a-zA-Z0-9_:]+)\][\t ]*$/) 
131                 {
132                         $section = $1;
133                         $infragment = 0;
134                         next;
135                 }
136
137                 # include
138                 if ($line =~ /^include (.*)$/) {
139                         $makefile .= run_config_mk($input, dirname($filename)."/$1");
140                         next;
141                 }
142
143                 # empty line
144                 if ($line =~ /^[ \t]*$/) {
145                         $section = "GLOBAL";
146                         if ($infragment) { $makefile.="\n"; }
147                         next;
148                 }
149
150                 # global stuff is considered part of the makefile
151                 if ($section eq "GLOBAL") {
152                         $makefile .= $line;
153                         $infragment = 1;
154                         next;
155                 }
156
157                 
158                 # Assignment
159                 if ($line =~ /^([a-zA-Z0-9_]+)[\t ]*=(.*)$/) {
160                         $result->{$section}{$1}{VAL} = $2;
161                         $result->{$section}{$1}{KEY} = $1;
162                 
163                         next;
164                 }
165
166                 die("$filename:$linenum: Bad line while parsing $filename");
167         }
168
169         foreach my $section (keys %{$result}) {
170                 my ($type, $name) = split(/::/, $section, 2);
171
172                 my $sectype = $section_types->{$type};
173                 if (not defined($sectype)) {
174                         die($filename.":[".$section."] unknown section type \"".$type."\"!");
175                 }
176
177                 $input->{$name}{NAME} = $name;
178                 $input->{$name}{TYPE} = $type;
179                 $input->{$name}{BASEDIR} = dirname($filename);
180
181                 foreach my $key (values %{$result->{$section}}) {
182                         $key->{VAL} = smb_build::input::strtrim($key->{VAL});
183                         my $vartype = $sectype->{$key->{KEY}};
184                         if (not defined($vartype)) {
185                                 die($filename.":[".$section."]: unknown attribute type \"$key->{KEY}\"!");
186                         }
187                         if ($vartype eq "string") {
188                                 $input->{$name}{$key->{KEY}} = $key->{VAL};
189                         } elsif ($vartype eq "list") {
190                                 $input->{$name}{$key->{KEY}} = [smb_build::input::str2array($key->{VAL})];
191                         } elsif ($vartype eq "bool") {
192                                 if (($key->{VAL} ne "YES") and ($key->{VAL} ne "NO")) {
193                                         die("Invalid value for bool attribute $key->{KEY}: $key->{VAL} in section $section");
194                                 }
195                                 $input->{$name}{$key->{KEY}} = $key->{VAL};
196                         }
197                 }
198         }
199
200         return $makefile;
201 }
202
203 1;