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