summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
Diffstat (limited to 'apps')
-rw-r--r--apps/plugins/lua/lua.make7
-rwxr-xr-xapps/plugins/lua/rbdefines_helper.pl199
-rw-r--r--apps/plugins/lua/rocklib.c8
-rw-r--r--apps/plugins/lua/rocklua.c8
4 files changed, 215 insertions, 7 deletions
diff --git a/apps/plugins/lua/lua.make b/apps/plugins/lua/lua.make
index b5a0c74633..f54d35bc44 100644
--- a/apps/plugins/lua/lua.make
+++ b/apps/plugins/lua/lua.make
@@ -35,11 +35,16 @@ else
35 ROCKS += $(LUA_BUILDDIR)/lua.rock 35 ROCKS += $(LUA_BUILDDIR)/lua.rock
36endif 36endif
37 37
38$(LUA_BUILDDIR)/lua.rock: $(LUA_OBJ) $(TLSFLIB) $(LUA_BUILDDIR)/actions.lua $(LUA_BUILDDIR)/buttons.lua $(LUA_BUILDDIR)/settings.lua $(LUA_BUILDDIR)/rocklib_aux.o $(LUA_INCLUDELIST) 38$(LUA_BUILDDIR)/lua.rock: $(LUA_OBJ) $(TLSFLIB) $(LUA_BUILDDIR)/actions.lua $(LUA_BUILDDIR)/buttons.lua $(LUA_BUILDDIR)/settings.lua $(LUA_BUILDDIR)/rocklib_aux.o $(LUA_BUILDDIR)/rb_defines.lua $(LUA_INCLUDELIST)
39 39
40$(LUA_BUILDDIR)/actions.lua: $(LUA_OBJ) $(LUA_SRCDIR)/action_helper.pl 40$(LUA_BUILDDIR)/actions.lua: $(LUA_OBJ) $(LUA_SRCDIR)/action_helper.pl
41 $(call PRINTS,GEN $(@F))$(CC) $(PLUGINFLAGS) $(INCLUDES) -E -P $(APPSDIR)/plugins/lib/pluginlib_actions.h | $(LUA_SRCDIR)/action_helper.pl > $(LUA_BUILDDIR)/actions.lua 41 $(call PRINTS,GEN $(@F))$(CC) $(PLUGINFLAGS) $(INCLUDES) -E -P $(APPSDIR)/plugins/lib/pluginlib_actions.h | $(LUA_SRCDIR)/action_helper.pl > $(LUA_BUILDDIR)/actions.lua
42 42
43$(LUA_BUILDDIR)/rb_defines.lua: $(LUA_OBJ) $(LUA_SRCDIR)/rbdefines_helper.pl
44 $(SILENT)$(CC) $(INCLUDES) -dD -E -P $(TARGET) $(CFLAGS) -include plugin.h - < /dev/null | $(LUA_SRCDIR)/rbdefines_helper.pl | \
45 $(HOSTCC) -fno-builtin $(HOST_INCLUDES) -x c -o $(LUA_BUILDDIR)/rbdefines_helper -
46 $(call PRINTS,GEN $(@F))$(LUA_BUILDDIR)/rbdefines_helper > $(LUA_BUILDDIR)/rb_defines.lua
47
43$(LUA_BUILDDIR)/settings.lua: $(LUA_OBJ) $(LUA_SRCDIR)/settings_helper.pl 48$(LUA_BUILDDIR)/settings.lua: $(LUA_OBJ) $(LUA_SRCDIR)/settings_helper.pl
44 $(SILENT)$(CC) $(INCLUDES) -E -P $(TARGET) $(CFLAGS) -include plugin.h -include cuesheet.h - < /dev/null | $(LUA_SRCDIR)/settings_helper.pl | \ 49 $(SILENT)$(CC) $(INCLUDES) -E -P $(TARGET) $(CFLAGS) -include plugin.h -include cuesheet.h - < /dev/null | $(LUA_SRCDIR)/settings_helper.pl | \
45 $(CC) $(INCLUDES) $(TARGET) $(CFLAGS) -S -x c -include config.h -include plugin.h -o $(LUA_BUILDDIR)/settings_helper.s - 50 $(CC) $(INCLUDES) $(TARGET) $(CFLAGS) -S -x c -include config.h -include plugin.h -o $(LUA_BUILDDIR)/settings_helper.s -
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}
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c
index 426dd079af..1d20989009 100644
--- a/apps/plugins/lua/rocklib.c
+++ b/apps/plugins/lua/rocklib.c
@@ -793,7 +793,11 @@ LUALIB_API int luaopen_rock(lua_State *L)
793{ 793{
794 luaL_register(L, LUA_ROCKLIBNAME, rocklib); 794 luaL_register(L, LUA_ROCKLIBNAME, rocklib);
795 luaL_register(L, LUA_ROCKLIBNAME, rocklib_aux); 795 luaL_register(L, LUA_ROCKLIBNAME, rocklib_aux);
796 796 lua_getglobal(L, "require");
797 lua_pushstring(L, "rb_defines");
798 if (lua_pcall (L, 1, 0, 0))
799 lua_pop(L, 1);
800#if 0
797 static const struct lua_int_reg rlib_const_int[] = 801 static const struct lua_int_reg rlib_const_int[] =
798 { 802 {
799 /* useful integer constants */ 803 /* useful integer constants */
@@ -860,7 +864,7 @@ LUALIB_API int luaopen_rock(lua_State *L)
860 luaS_newlloc(L, rlcs->name, TSTR_INBIN); 864 luaS_newlloc(L, rlcs->name, TSTR_INBIN);
861 lua_setfield(L, -2, rlcs->name); 865 lua_setfield(L, -2, rlcs->name);
862 } 866 }
863 867#endif
864 return 1; 868 return 1;
865} 869}
866 870
diff --git a/apps/plugins/lua/rocklua.c b/apps/plugins/lua/rocklua.c
index af87a0bfd7..eec6ee54f9 100644
--- a/apps/plugins/lua/rocklua.c
+++ b/apps/plugins/lua/rocklua.c
@@ -31,15 +31,15 @@
31 31
32static const luaL_Reg lualibs[] = { 32static const luaL_Reg lualibs[] = {
33 {"", luaopen_base}, 33 {"", luaopen_base},
34 {LUA_LOADLIBNAME, luaopen_package},
34 {LUA_TABLIBNAME, luaopen_table}, 35 {LUA_TABLIBNAME, luaopen_table},
35 {LUA_STRLIBNAME, luaopen_string}, 36 {LUA_STRLIBNAME, luaopen_string},
36 {LUA_OSLIBNAME, luaopen_os},
37 {LUA_ROCKLIBNAME, luaopen_rock},
38 {LUA_ROCKLIBNAME, luaopen_rock_img},
39 {LUA_BITLIBNAME, luaopen_bit}, 37 {LUA_BITLIBNAME, luaopen_bit},
40 {LUA_IOLIBNAME, luaopen_io}, 38 {LUA_IOLIBNAME, luaopen_io},
41 {LUA_LOADLIBNAME, luaopen_package},
42 {LUA_MATHLIBNAME, luaopen_math}, 39 {LUA_MATHLIBNAME, luaopen_math},
40 {LUA_OSLIBNAME, luaopen_os},
41 {LUA_ROCKLIBNAME, luaopen_rock},
42 {LUA_ROCKLIBNAME, luaopen_rock_img},
43 {LUA_DIRLIBNAME, luaopen_luadir}, 43 {LUA_DIRLIBNAME, luaopen_luadir},
44 {NULL, NULL} 44 {NULL, NULL}
45}; 45};