r10091: Fix perl warnings
[metze/samba/wip.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
12 use strict;
13
14 my %attribute_types = (
15         "NOPROTO" => "bool",
16         "REQUIRED_SUBSYSTEMS" => "list",
17         "OUTPUT_TYPE" => "string",
18         "INIT_OBJ_FILES" => "list",
19         "ADD_OBJ_FILES" => "list",
20         "OBJ_FILES" => "list",
21         "SUBSYSTEM" => "string",
22         "CFLAGS" => "list",
23         "CPPFLAGS" => "list",
24         "LDFLAGS" => "list",
25         "INSTALLDIR" => "string",
26         "LIBS" => "list",
27         "INIT_FUNCTION" => "string",
28         "MAJOR_VERSION" => "string",
29         "MINOR_VERSION" => "string",
30         "RELEASE_VERSION" => "string",
31         "ENABLE" => "bool",
32         "CMD" => "string",
33         "MANPAGE" => "string"
34 );
35
36 use vars qw(@parsed_files);
37
38 @parsed_files = ();
39
40 ###########################################################
41 # The parsing function which parses the file
42 #
43 # $result = _parse_config_mk($filename)
44 #
45 # $filename -   the path of the config.mk file
46 #               which should be parsed
47 sub run_config_mk($$)
48 {
49         sub run_config_mk($$);
50         my ($input, $filename) = @_;
51         my $result;
52         my $linenum = -1;
53         my $infragment = 0;
54         my $section = "GLOBAL";
55         my $makefile = "";
56
57         push (@parsed_files, $filename);
58         
59         open(CONFIG_MK, $filename) or die("Can't open `$filename'\n");
60         my @lines = <CONFIG_MK>;
61         close(CONFIG_MK);
62
63         my $line = "";
64         my $prev = "";
65
66         foreach (@lines) {
67                 $linenum++;
68
69                 # lines beginning with '#' are ignored
70                 next if (/^\#.*$/);
71                 
72                 if (/^(.*)\\$/) {
73                         $prev .= $1;
74                         next;
75                 } else {
76                         $line = "$prev$_";
77                         $prev = "";
78                 }
79
80                 if ($line =~ /^\[([a-zA-Z0-9_:]+)\][\t ]*$/) 
81                 {
82                         $section = $1;
83                         $infragment = 0;
84                         next;
85                 }
86
87                 # include
88                 if ($line =~ /^include (.*)$/) {
89                         $makefile .= run_config_mk($input, $1);
90                         next;
91                 }
92
93                 # empty line
94                 if ($line =~ /^[ \t]*$/) {
95                         $section = "GLOBAL";
96                         if ($infragment) { $makefile.="\n"; }
97                         next;
98                 }
99
100                 # global stuff is considered part of the makefile
101                 if ($section eq "GLOBAL") {
102                         $makefile .= $line;
103                         $infragment = 1;
104                         next;
105                 }
106
107                 
108                 # Assignment
109                 if ($line =~ /^([a-zA-Z0-9_]+)[\t ]*=(.*)$/) {
110                         $result->{$section}{$1}{VAL} = $2;
111                         $result->{$section}{$1}{KEY} = $1;
112                 
113                         next;
114                 }
115
116                 die("$filename:$linenum: Bad line while parsing $filename");
117         }
118
119         foreach my $section (keys %{$result}) {
120                 my ($type, $name) = split(/::/, $section, 2);
121                 
122                 $input->{$name}{NAME} = $name;
123                 $input->{$name}{TYPE} = $type;
124
125                 foreach my $key (values %{$result->{$section}}) {
126                         $key->{VAL} = smb_build::input::strtrim($key->{VAL});
127                         my $vartype = $attribute_types{$key->{KEY}};
128                         if (not defined($vartype)) {
129                                 die("$filename:Unknown attribute $key->{KEY} with value $key->{VAL} in section $section");
130                         }
131                         if ($vartype eq "string") {
132                                 $input->{$name}{$key->{KEY}} = $key->{VAL};
133                         } elsif ($vartype eq "list") {
134                                 $input->{$name}{$key->{KEY}} = [smb_build::input::str2array($key->{VAL})];
135                         } elsif ($vartype eq "bool") {
136                                 if (($key->{VAL} ne "YES") and ($key->{VAL} ne "NO")) {
137                                         die("Invalid value for bool attribute $key->{KEY}: $key->{VAL} in section $section");
138                                 }
139                                 $input->{$name}{$key->{KEY}} = $key->{VAL};
140                         }
141                 }
142         }
143
144         return $makefile;
145 }
146
147 1;