1
0
mirror of https://github.com/elua/elua.git synced 2025-01-25 01:02:54 +08:00

New cross-compiler mode: "compile_raw"

The new mode compiles the Lua files to .lc (bytecode) files for later use.
This should be replaced later with a proper script dedicated to
cross-compiling Lua sources.
This commit is contained in:
Bogdan Marinescu 2018-06-05 19:17:58 +03:00
parent 1397a1116d
commit 66f8c102a5

View File

@ -38,6 +38,7 @@ end
-- mode - preprocess the file system: -- mode - preprocess the file system:
-- "verbatim" - copy the files directly to the FS as they are -- "verbatim" - copy the files directly to the FS as they are
-- "compile" - precompile all files to Lua bytecode and then copy them -- "compile" - precompile all files to Lua bytecode and then copy them
-- "compile_raw" - precompile all files to Lua bytecode for direct loading, IE, leave as individual *.lc scripts
-- "compress" - keep the source code, but compress it with LuaSrcDiet -- "compress" - keep the source code, but compress it with LuaSrcDiet
-- compcmd - the command to use for compiling if "mode" is "compile" -- compcmd - the command to use for compiling if "mode" is "compile"
-- Returns true for OK, false for error -- Returns true for OK, false for error
@ -50,15 +51,19 @@ function mkfs( dirname, outname, flist, mode, compcmd )
return false return false
end end
print( sf( "Generating file %s/%s", dirname, outfname ) )
_crtline = ' ' _crtline = ' '
_numdata = 0 _numdata = 0
_bytecnt = 0 _bytecnt = 0
-- Generate headers if mode ~= "compile_raw" then
outfile:write( "// Generated by mkfs.lua\n// DO NOT MODIFY\n\n" ) -- Generate headers
outfile:write( sf( "#ifndef __%s_H__\n#define __%s_H__\n\n", outname:upper(), outname:upper() ) ) outfile:write( "// Generated by mkfs.lua\n// DO NOT MODIFY\n\n" )
outfile:write( sf( "#ifndef __%s_H__\n#define __%s_H__\n\n", outname:upper(), outname:upper() ) )
outfile:write( sf( "const unsigned char %s_fs[] = \n{\n", outname:lower() ) ) outfile:write( sf( "const unsigned char %s_fs[] = \n{\n", outname:lower() ) )
end
-- Process all files -- Process all files
for _, fname in pairs( flist ) do for _, fname in pairs( flist ) do
@ -81,7 +86,7 @@ function mkfs( dirname, outname, flist, mode, compcmd )
end end
-- Do we need to process the file? -- Do we need to process the file?
local fextpart, fnamepart = '' local fextpart, fnamepart = ''
if mode == "compile" or mode == "compress" then if mode == "compile" or mode == "compile_raw" or mode == "compress" then
fnamepart, fextpart = utils.split_ext( realname ) fnamepart, fextpart = utils.split_ext( realname )
local newext = mode == "compress" and ".lua.tmp" or ".lc" local newext = mode == "compress" and ".lua.tmp" or ".lc"
if fextpart == ".lua" then if fextpart == ".lua" then
@ -91,6 +96,7 @@ function mkfs( dirname, outname, flist, mode, compcmd )
else else
print( sf( "Cross compiling %s to %s ...", realname, newname ) ) print( sf( "Cross compiling %s to %s ...", realname, newname ) )
end end
print( "Cross compile command:" .. sf( compcmd, newname, realname ) )
if os.execute( sf( compcmd, newname, realname ) ) ~= 0 then if os.execute( sf( compcmd, newname, realname ) ) ~= 0 then
print "Cross-compilation error, aborting" print "Cross-compilation error, aborting"
outfile:close() outfile:close()
@ -105,7 +111,7 @@ function mkfs( dirname, outname, flist, mode, compcmd )
print( sf( "Unable to read %s", newname ) ) print( sf( "Unable to read %s", newname ) )
return false return false
end end
if mode == "compile" then if mode == "compile" or mode == "compile_raw" then
fnamepart, fextpart = utils.split_ext( fname ) fnamepart, fextpart = utils.split_ext( fname )
fname = fnamepart .. ".lc" fname = fnamepart .. ".lc"
end end
@ -113,40 +119,44 @@ function mkfs( dirname, outname, flist, mode, compcmd )
end end
local filedata = crtfile:read( '*a' ) local filedata = crtfile:read( '*a' )
crtfile:close() crtfile:close()
if fextpart == ".lua" and mode ~= "verbatim" then if fextpart == ".lua" and mode ~= "verbatim" and mode ~= "compile_raw" then
os.remove( newname ) os.remove( newname )
end end
-- Write name, size, id, numpars if mode ~= "compile_raw" then
_fcnt = 0 -- Write name, size, id, numpars
for i = 1, #fname do _fcnt = 0
_add_data( fname:byte( i ), outfile ) for i = 1, #fname do
_add_data( fname:byte( i ), outfile )
end
_add_data( 0, outfile ) -- ASCIIZ
local plen = string.pack( "<i", #filedata )
-- Round to a multiple of 'alignment'
while _bytecnt % alignment ~= 0 do
_add_data( 0, outfile )
end
-- Write size
_add_data( plen:byte( 1 ), outfile )
_add_data( plen:byte( 2 ), outfile )
_add_data( plen:byte( 3 ), outfile )
_add_data( plen:byte( 4 ), outfile )
-- Then write the rest of the file
for i = 1, #filedata do
_add_data( filedata:byte( i ), outfile )
end
-- Report
print( sf( "Encoded file %s (%d bytes real size, %d bytes encoded size)", fname, #filedata, _fcnt ) )
end end
_add_data( 0, outfile ) -- ASCIIZ
local plen = string.pack( "<i", #filedata )
-- Round to a multiple of 'alignment'
while _bytecnt % alignment ~= 0 do
_add_data( 0, outfile )
end
-- Write size
_add_data( plen:byte( 1 ), outfile )
_add_data( plen:byte( 2 ), outfile )
_add_data( plen:byte( 3 ), outfile )
_add_data( plen:byte( 4 ), outfile )
-- Then write the rest of the file
for i = 1, #filedata do
_add_data( filedata:byte( i ), outfile )
end
-- Report
print( sf( "Encoded file %s (%d bytes real size, %d bytes encoded size)", fname, #filedata, _fcnt ) )
end end
end end
end end
-- All done, write the final "0xFF" (terminator) if mode ~= "compile_raw" then
_add_data( 0xFF, outfile, false ) -- All done, write the final "0xFF" (terminator)
outfile:write( "};\n\n#endif\n" ); _add_data( 0xFF, outfile, false )
outfile:close() outfile:write( "};\n\n#endif\n" );
print( sf( "Done, total size is %d bytes", _bytecnt ) ) outfile:close()
print( sf( "Done, total size is %d bytes", _bytecnt ) )
end
return true return true
end end