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