summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/rbdefines_helper.pl
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/rbdefines_helper.pl')
-rwxr-xr-xapps/plugins/lua/rbdefines_helper.pl199
1 files changed, 199 insertions, 0 deletions
diff --git a/apps/plugins/lua/rbdefines_helper.pl b/apps/plugins/lua/rbdefines_helper.pl
new file mode 100755
index 0000000000..671936962e
--- /dev/null
+++ b/apps/plugins/lua/rbdefines_helper.pl
@@ -0,0 +1,199 @@
1#!/usr/bin/env perl
2############################################################################
3# __________ __ ___.
4# Open \______ \ ____ ____ | | _\_ |__ _______ ___
5# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8# \/ \/ \/ \/ \/
9# $Id$
10#
11# Copyright (C) 2019 by William Wilgus
12#
13# All files in this archive are subject to the GNU General Public License.
14# See the file COPYING in the source tree root for full license agreement.
15#
16# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17# KIND, either express or implied.
18#
19############################################################################
20
21#rockbox to lua define generator, add names of constants to the array to include
22
23my @rockbox_defines = (
24 '^HZ$',
25 '^LCD_(DEPTH|HEIGHT|WIDTH)$',
26 '^MODEL_NAME$',
27 '^SCREEN_MAIN$',
28 '^LCD_DEFAULT_(FG|BG)$',
29 '^LCD_REMOTE_(DEPTH|HEIGHT|WIDTH)$',
30 '^LCD_.+(BRIGHT|DARK)COLOR',
31 '^SCREEN_REMOTE$',
32 '^FONT_SYSFIXED$',
33 '^FONT_UI$',
34 '^PLAYLIST_(INSERT|PREPEND|REPLACE)',
35 '^TOUCHSCREEN_(POINT|BUTTON)$',
36 '^HOME_DIR$',
37 '^PLUGIN_DIR$',
38 '^PLUGIN(_APPS_|_GAMES_|_)DATA_DIR$',
39 '^ROCKBOX_DIR$',
40 '^VIEWERS_DATA_DIR$');
41
42my @captured_defines;
43my @names_seen;
44my @all_defines;
45
46############# precompile regex for speed #############
47my $def_regex = qr/#define\s+([^\s\r\n]+)\s+([^\r\n]+)/;
48my $quot_regex = qr/.*([\"\']).*/;
49my $num_regex = qr/.*([\+\-\*\\|&\d]).*/;
50
51print <<EOF
52#include <stdio.h>
53#include <stdbool.h>
54
55#define stringify(s) #s
56
57/* auto generated defines */
58
59
60EOF
61;
62
63while(my $line = <STDIN>)
64{
65
66 if($line =~ $def_regex) #does it begin with #define?
67 {
68 push(@all_defines, $line); #save all defines for possible ambiguous macros
69 $name = $1;
70 next if $name =~ /^(ATTRIBUTE_|print|__).*/; #don't add reserved
71 $value = $2;
72
73 if(grep($name =~ $_, @rockbox_defines))
74 {
75 push(@names_seen, $name);
76 push(@captured_defines, {'name' => $name, 'value' => $value});
77 print $line
78 }
79 else
80 {
81 $name =~ s{(\(.*\))}{}gx; #remove (...) on function type macros
82 #ifndef guard so we don't clash with the host
83 printf "#ifndef %s\n%s#endif\n\n", $name, $line;
84 }
85 }
86 elsif($line =~ /^(?!.*;)enum.*{/) #enum {
87 {
88 print $line;
89 next if($line =~ /.*};.*/);
90 do_enum($line)
91 }
92 elsif($line =~ /^(?!.*[;\(\)])enum.*/) #enum
93 {
94 #need to be careful might be a function returning enum
95 my $lastline = $line;
96 next if($line =~ /.*};.*/);
97 ($line = <STDIN>);
98 if($line =~ /.*{.*/)
99 {
100 print $lastline;
101 print $line;
102 }
103 else { next; }
104 do_enum($line)
105 }
106
107}
108
109#Sort the functions
110my @sorted_defines = sort { @$a{'name'} cmp @$b{'name'} } @captured_defines;
111
112printf "int main(void)\n{\n";
113printf "\tprintf(\"--[[Autogenerated rockbox constants]]\\n\\n\");";
114# Print the C array
115foreach my $define (@sorted_defines)
116{
117 if(@$define{'value'} =~ /^0[xX][0-9a-fA-F]+$/) #hex number
118 {
119 printf "\tprintf(\"rb[\\\"%%s\\\"] = 0x%%x\\n\", stringify(%s), %s);\n", @$define{'name'}, @$define{'name'};
120 }
121 elsif(@$define{'value'} =~ /^[0-9]+$/) #number
122 {
123 printf "\tprintf(\"rb[\\\"%%s\\\"] = %%d\\n\", stringify(%s), %s);\n", @$define{'name'}, @$define{'name'};
124 }
125 else #might be a string but we don't know since the macro isn't expanded far enough
126 {
127 my $max_depth = 10;
128 my $var = @$define{'value'};
129 while($max_depth > 0) #follow the chain of #defines until we can see what type
130 {
131 $max_depth--;
132 $var = "\\\s*#define\\s+$var\\s+.*";
133 #warn $var;
134 if(my ($x) = grep($_ =~ /($var)/, @all_defines)) #check all the defines
135 {
136 #warn $x;
137 if($x =~ $def_regex)
138 {
139 $var = $2;
140 #warn $var;
141 last if ($var =~ $quot_regex);
142 last if ($var =~ $num_regex);
143 next
144 }
145 #warn "end ".$var;
146 last
147 }
148 else
149 {
150 $var = @$define{'value'};
151 last
152 }
153 }
154 if ($var =~$quot_regex) #has a quote it is a string
155 {
156 #guard with empty literals "" so gcc throws an error if it isn't a string
157 printf "\tprintf(\"rb[\\\"%%s\\\"] = \\\"%%s\\\"\\n\", stringify(%s), \"\" %s \"\");\n", @$define{'name'}, @$define{'name'};
158 }
159 elsif ($var =~$num_regex) #it must be a number
160 {
161 printf "\tprintf(\"rb[\\\"%%s\\\"] = %%d\\n\", stringify(%s), %s);\n", @$define{'name'}, @$define{'name'};
162 }
163 else { warn "Skipping ".@$define{'name'}." indeterminate macro type\n"; }
164 }
165}
166
167print <<EOF
168 return 0;
169}
170
171EOF
172;
173
174sub do_enum {
175 my ($line) = @_;
176
177 while($line = <STDIN>)
178 {
179
180 if($line =~ /.*};.*/)
181 {
182 print $line;
183 last
184 }
185 elsif($line =~ /([^\s,\t]+)\s*=?.*,?/)
186 {
187 $name = $1;
188 #printf "%s WHATTT?", $name;
189 $value = "0"; #enums are always integers
190 }
191 print $line;
192 if(grep($name =~ $_, @rockbox_defines))
193 {
194 push(@names_seen, $name);
195 push(@captured_defines, {'name' => $name, 'value' => $value});
196 }
197
198 }
199}