1
0
mirror of https://github.com/elua/elua.git synced 2025-01-08 20:56:17 +08:00

changed module selection mechanism

This commit is contained in:
Bogdan Marinescu 2012-07-15 00:50:12 +03:00
parent 37e20330e7
commit 767b9243f9
14 changed files with 156 additions and 114 deletions

View File

@ -19,8 +19,7 @@ return {
ram = { ext_start = { 0xA0000000 }, ext_size = { 8 * 1048576 } }
},
modules = {
generic = 'all',
exclude_generic = { "i2c", "net", "spi", "can" },
generic = { 'all', '-i2c', '-net', '-spi', '-can' }
}
}

View File

@ -21,8 +21,7 @@ return {
vtmr = { num = 4, freq = 10 },
},
modules = {
generic = 'all',
exclude_generic = { "i2c", "net" },
generic = { 'all', '-i2c', '-net' },
platform = 'all',
}
}

View File

@ -14,7 +14,7 @@ return {
ram = { ext_start = { "SDRAM_BASE_ADDR" }, ext_size = { "SDRAM_SIZE" } }
},
modules = {
generic = { 'pio', 'tmr', 'pd', 'uart', 'term', 'pack', 'bit', 'elua', 'cpu', 'rpc', 'math' }
generic = { 'pio', 'tmr', 'pd', 'uart', 'term', 'pack', 'bit', 'elua', 'cpu', 'rpc', 'all_lua' }
}
}

View File

@ -18,8 +18,7 @@ return {
ram = { internal_rams = 2 }
},
modules = {
generic = 'all',
exclude_generic = { "spi", "can", "i2c", "net" },
generic = { 'all', "-spi", "-can", "-i2c", "-net" },
platform = 'all',
}
}

View File

@ -10,7 +10,7 @@ return {
xmodem = true
},
modules = {
generic = { 'pio', 'tmr', 'pd', 'pwm', 'uart', 'term', 'pack', 'bit', 'elua', 'cpu', 'math' }
generic = { 'pio', 'tmr', 'pd', 'pwm', 'uart', 'term', 'pack', 'bit', 'elua', 'cpu', 'all_lua' }
}
}

View File

@ -18,8 +18,7 @@ return {
vtmr = { num = 4, freq = 4 },
},
modules = {
generic = 'all',
exclude_generic = { "i2c", "net", "adc", "spi", "can" },
generic = { 'all', "-i2c", "-net", "-adc", "-spi", "-can" }
}
}

View File

@ -18,8 +18,7 @@ return {
vtmr = { num = 4, freq = 4 },
},
modules = {
generic = 'all',
exclude_generic = { "i2c", "net", "adc", "spi", "can" },
generic = { 'all', '-i2c', '-net', '-adc', '-spi', '-can' }
}
}

View File

@ -10,7 +10,7 @@ return {
term = { lines = 25, cols = 80 },
},
modules = {
generic = { 'pd', 'math', 'term', 'elua' }
generic = { 'pd', 'all_lua', 'term', 'elua' }
}
}

View File

@ -22,8 +22,7 @@ return {
vtmr = { num = 4, freq = 10 },
},
modules = {
generic = 'all',
exclude_generic = { "i2c", "net" },
generic = { 'all', "-i2c", "-net" },
platform = 'all',
}
}

View File

@ -18,8 +18,7 @@ return {
vtmr = { num = 4, freq = 16 },
},
modules = {
generic = 'all',
exclude_generic = { "net", "can" },
generic = { 'all', '-net', '-can' },
platform = 'all'
}
}

View File

@ -18,8 +18,7 @@ return {
vtmr = { num = 4, freq = 16 },
},
modules = {
generic = 'all',
exclude_generic = { "net", "can" },
generic = { 'all', "-net", "-can" },
platform = 'all'
}
}

View File

@ -4,9 +4,22 @@ module( ..., package.seeall )
local sf = string.format
local gen = require "generators"
-- List of all generic modules and their build guards
-- List of all Lua modules and their guards
local lua_modules = {
lua_math = {},
lua_io = {},
lua_string = {},
lua_table = {},
lua_debug = {},
lua_package = {},
lua_co = {}
}
-- List of all eLua generic modules and their build guards
-- (if the module guard evaluates to false at compile time, the module is not included in the build)
local generic_modules = {
-- NOTE: if the guards contain a condition, the condition _MUST_ be specified with spaces next to the operator!
-- For example: NUM_CAN > 0, _NOT_ NUM_CAN>0.
local elua_generic_modules = {
adc = { "BUILD_ADC", "NUM_ADC > 0" },
bit = {},
can = { "NUM_CAN > 0"},
@ -23,17 +36,46 @@ local generic_modules = {
term = { "BUILD_TERM" },
tmr = { "NUM_TIMER > 0" },
uart = { "NUM_UART > 0" },
math = {}
}
-- All generic modules (Lua and eLua) in a single table
local all_generic_modules = {}
utils.concat_tables( all_generic_modules, lua_modules )
utils.concat_tables( all_generic_modules, elua_generic_modules )
-- Auxlib names
local auxlibs = { math = "LUA_MATHLIBNAME" }
local auxlibs = {
lua_math = "LUA_MATHLIBNAME",
lua_io = "LUA_IOLIBNAME",
lua_string = "LUA_STRLIBNAME",
lua_table = "LUA_TABLIBNAME",
lua_debug = "LUA_DBLIBNAME",
lua_package = "LUA_LOADLIBNAME",
lua_co = "LUA_COLIBNAME",
}
-- Open function names
local opennames = {}
local opennames = {
lua_math = "luaopen_math",
lua_io = "luaopen_io",
lua_string = "luaopen_string",
lua_table = "luaopen_table",
lua_debug = "luaopen_debug",
lua_package = "luaopen_package",
lua_co = "luaopen_co"
}
-- Map array names
local mapnames = {}
-- Map array names. <none> marks the modules that do not have a corresponding
-- map array, which basically means they are not LTR compatible
local mapnames = {
lua_math = "math_map",
lua_io = "<none>",
lua_string = "strlib",
lua_table = "tab_funcs",
lua_debug = "dblib",
lua_package = "<none>",
lua_co = "co_funcs",
}
-- Return the auxlib name of a given module
local function get_auxlib( m )
@ -50,12 +92,13 @@ local function get_map_name( m )
return mapnames[ m ] or sf( "%s_map", m:lower() )
end
local platform_modules
-- Generate a condition string starting from a guard
local function gen_cond_string( g )
local condition = ''
for idx, e in pairs( g ) do
local suffix = idx == #g and "" or " && "
if e:find( '%s' ) then -- if it has a string, add the condition as it was given, between parantheses
if e:find( '%s' ) then -- if it has a space, add the condition as it was given, between parantheses
condition = condition .. "( " .. e .. " )"
else
condition = condition .. "defined( " .. e .. " )"
@ -65,70 +108,72 @@ local function gen_cond_string( g )
return condition
end
-- Add/remove a module to the given list
local function process_module( l, name, is_specific )
local sectname, exclude = is_specific and "platform" or "generic"
local check = is_specific and platform_modules or all_generic_modules
-- Handle "+name" / "-name"
if name:sub( 1, 1 ) == "+" then
name = name:sub( 2, -1 )
elseif name:sub( 1, 1 ) == "-" then
name = name:sub( 2, -1 )
exclude = true
end
local modlist
-- Handle special cases for 'name'
if name == "all" then
modlist = is_specific and utils.table_keys( platform_modules ) or utils.table_keys( all_generic_modules )
elseif name == "all_lua" then
if is_specific then return false, "'all_lua' can't be used in attribute 'platform' of section 'modules'" end
modlist = utils.table_keys( lua_modules )
elseif name == "all_elua" then
if is_specific then return false, "'all_elua' can't be used in attribute 'platform' of section 'modules" end
modlist = utils.table_keys( elua_generic_modules )
else
modlist = { name }
end
-- For inclusion, check for valid element. For exclusion, check for prior inclusion.
if exclude then
for _, m in pairs( modlist ) do
if not l[ m ] then
return false, sf( "module '%s' not found in element '%s' of section 'modules'", m, sectname )
end
l[ m ] = nil
end
else
for _, m in pairs( modlist ) do
if not check[ m ] then
return false, sf( "module '%s' of element '%s' in section 'modules' not found", m, sectname )
end
l[ m ] = true
end
end
return true
end
-- Generate the complete module list starting from the board description
function gen_module_list( desc, plconf, platform )
local mdesc = desc.modules
if not mdesc then return '' end
local gen_list_generic, gen_list_platform = {}, {}
local platform_modules = plconf.get_platform_modules() or {}
local gstr = string.rep( "/", 80 ) .. "\n" .. "// Module configuration\n\n"
local ngenmods, nplmods = 0, 0
platform_modules = plconf.get_platform_modules() or {}
if mdesc == "all" then -- include all the modules. what a brave, brave soul.
for m, _ in pairs( generic_modules ) do
gen_list_generic[ m ] = true
ngenmods = ngenmods + 1
end
for m, _ in pairs( platform_modules ) do
gen_list_platform[ m ] = true
nplmods = nplmods + 1
end
process_module( gen_list_generic, 'all' )
process_module( gen_list_platform, 'all', true )
else
-- Include only some modules. Validate their names against the corresponding module list
if mdesc.generic == "all" then -- all generic modules
for m, _ in pairs( generic_modules ) do
gen_list_generic[ m ] = true
ngenmods = ngenmods + 1
end
elseif mdesc.generic then
for _, m in pairs( mdesc.generic ) do
if not generic_modules[ m ] then return false, sf( "unknown generic module '%s' in section 'modules'", m ) end
gen_list_generic[ m ] = true
ngenmods = ngenmods + 1
end
end
if mdesc.platform == "all" then -- all platform modules
for m, _ in pairs( platform_modules ) do
gen_list_platform[ m ] = true
nplmods = nplmods + 1
end
elseif mdesc.platform then
for _, m in pairs( mdesc.platform ) do
if not platform_modules[ m ] then return false, sf( "unknown platform module '%s' in section 'modules'", m ) end
gen_list_platform[ m ] = true
nplmods = nplmods + 1
end
end
end
-- Need to exclude anything?
if type( mdesc.exclude_generic ) == "table" then
for _, m in pairs( mdesc.exclude_generic ) do
if not gen_list_generic[ m ] then
return false, sf( "module '%s' in exclude_generic not found in the generic module list", m )
end
gen_list_generic[ m ] = nil
ngenmods = ngenmods - 1
end
end
if type( mdesc.exclude_platform ) == "table" then
for _, m in pairs( mdesc.exclude_platform ) do
if not gen_list_platform[ m ] then
return false, sf( "module '%s' in exclude_platform not found in the platform specific module list", m )
end
gen_list_platform[ m ] = nil
nplmods = nplmods - 1
end
mdesc.generic = type( mdesc.generic ) == "table" and mdesc.generic or { mdesc.generic }
mdesc.platform = type( mdesc.platform ) == "table" and mdesc.platform or { mdesc.platform }
utils.foreach( mdesc.generic, function( k, v ) process_module( gen_list_generic, v ) end )
utils.foreach( mdesc.platform, function( k, v ) process_module( gen_list_platform, v, true ) end )
end
-- Count the notal number of modules
utils.foreach( gen_list_generic, function( k, v ) ngenmods = ngenmods + 1 end )
utils.foreach( gen_list_platform, function( k, v ) nplmods = nplmods + 1 end )
if ngenmods + nplmods == 0 then return '' end
-- Now build all the module lines, starting from the gen_list and the guards
@ -165,7 +210,8 @@ function gen_module_list( desc, plconf, platform )
gstr = gstr .. gen.print_define( "PLATFORM_MODULES_LINE" )
end
for m, _ in pairs( gen_list_generic ) do
local g = generic_modules[ m ]
local g = all_generic_modules[ m ]
if get_map_name( m ) ~= "<none>" then
if #g == 0 then -- no guards
gstr = gstr .. gen.print_define( sf( "MODULE_%s_LINE", m:upper() ), sf( "_ROM( %s, %s, %s )", get_auxlib( m ), get_openf_name( m ), get_map_name( m ) ) )
else
@ -177,14 +223,27 @@ function gen_module_list( desc, plconf, platform )
gstr = gstr .. sf( "#warning Unable to include module '%s' in the image\n#endif\n\n", m )
end
end
end
gstr = gstr .. "\n"
-- Finally, generate the acutal list of libraries. Phew.
gstr = gstr .. "#define LUA_PLATFORM_LIBS_ROM\\\n PLATFORM_MODULES_LINE"
for m, _ in pairs( gen_list_generic ) do
gstr = gstr .. sf( "\\\n MODULE_%s_LINE", m:upper() )
if get_map_name( m ) ~= "<none>" then gstr = gstr .. sf( "\\\n MODULE_%s_LINE", m:upper() ) end
end
gstr = gstr .. "\n"
gstr = gstr .. "\n\n"
-- Not quite ready yet. We still need to generate the list of generic modules
-- that can't be completely ROM'd by the LTR patch in a separate macro that will be
-- handled by linit.c
local noltr = "#define LUA_LIBS_NOLTR\\\n", found
for m, _ in pairs( gen_list_generic ) do
if get_map_name( m ) == "<none>" then
noltr = noltr .. sf( " { %s, %s },\\\n", get_auxlib( m ), get_openf_name( m ) )
found = true
end
end
gstr = gstr .. ( found and noltr or "" )
-- A bit of cosmetic touch ...
gstr = gstr .. "\n"

View File

@ -20,7 +20,7 @@
#include "lualib.h"
#include "lrotable.h"
#include "platform_conf.h"
/*
@ -696,7 +696,7 @@ static void base_open (lua_State *L) {
LUALIB_API int luaopen_base (lua_State *L) {
base_open(L);
#if LUA_OPTIMIZE_MEMORY == 0
#if LUA_OPTIMIZE_MEMORY == 0 && defined( MODULE_LUA_CO_LINE )
luaL_register(L, LUA_COLIBNAME, co_funcs);
return 2;
#else

View File

@ -18,31 +18,28 @@
extern int luaopen_platform( lua_State *L );
// Dummy open function for "co" (actually opened in lbaselib.c)
int luaopen_co( lua_State *L )
{
return 0;
}
#define _ROM( name, openf, table ) { name, openf },
static const luaL_Reg lualibs[] = {
{"", luaopen_base},
{LUA_LOADLIBNAME, luaopen_package},
{LUA_IOLIBNAME, luaopen_io},
{LUA_STRLIBNAME, luaopen_string},
#if LUA_OPTIMIZE_MEMORY == 0
{LUA_MATHLIBNAME, luaopen_math},
{LUA_TABLIBNAME, luaopen_table},
{LUA_DBLIBNAME, luaopen_debug},
#endif
#ifdef LUA_PLATFORM_LIBS_REG
LUA_PLATFORM_LIBS_REG,
#endif
#if defined(LUA_PLATFORM_LIBS_ROM)
#define _ROM( name, openf, table ) { name, openf },
LUA_PLATFORM_LIBS_ROM
#endif
#if defined(LUA_LIBS_NOLTR)
LUA_LIBS_NOLTR
#endif
{NULL, NULL}
};
extern const luaR_entry strlib[];
extern const luaR_entry syslib[];
extern const luaR_entry tab_funcs[];
extern const luaR_entry dblib[];
extern const luaR_entry co_funcs[];
#if defined(LUA_PLATFORM_LIBS_ROM) && LUA_OPTIMIZE_MEMORY == 2
#undef _ROM
#define _ROM( name, openf, table ) extern const luaR_entry table[];
@ -50,16 +47,10 @@ LUA_PLATFORM_LIBS_ROM;
#endif
const luaR_table lua_rotable[] =
{
#if LUA_OPTIMIZE_MEMORY > 0
{LUA_STRLIBNAME, strlib},
{LUA_TABLIBNAME, tab_funcs},
{LUA_DBLIBNAME, dblib},
{LUA_COLIBNAME, co_funcs},
#if defined(LUA_PLATFORM_LIBS_ROM) && LUA_OPTIMIZE_MEMORY == 2
#undef _ROM
#define _ROM( name, openf, table ) { name, table },
LUA_PLATFORM_LIBS_ROM
#endif
#endif
{NULL, NULL}
};