Lua Notes

Cross-compile with Zig
Login

Cross-compile with Zig

This is very straightforward, thanks to --cc handling spaces reasonably, and thanks to nelua asking the C compiler about information like the target platform or OS and making that available to Lua in ccinfo:

$ uname -sm
Linux aarch64
$ nelua -o test.for-centos6 --cc='zig cc --target=x86_64-linux-gnu.2.12' -i 'print("hi")'
$ nelua -o test.static-binary --cc='zig cc --target=x86_64-linux-musl' -i 'print("hi")'
$ file test.static-binary
test.static-binary: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, with debug_info, not stripped
$ ldd test.static-binary
        not a dynamic executable

$ uname -sm
Linux x86_64
$ nelua -o test.exe --cc='zig cc --target=x86_64-windows' -i 'print("hi windows")'
$ wine test.exe
hi windows

To see what's available in ccinfo, use Lua's inspect library:

$ nelua -i '## print(inspect(ccinfo, {depth=1}))'

And to see what can be found in ccinfo, consult lualib/nelua/cdefs.lua

How about while using nelua-batteries's sqlite3?

OK, no longer as straightforward. Have a .neluacfg.lua for it:

  1. suppress linklib
  2. cross-build an object file (.o) for the library
  3. link the .o
local batteries = os.getenv('HOME') .. '/nelua/nelua-batteries'

local patch = [=[--- sqlite3.nelua.orig	2023-11-01 05:35:06.666434931 +0300
+++ sqlite3.nelua	2023-11-01 05:54:53.153881202 +0300
@@ -9,7 +9,9 @@
 
 ##[[
 cinclude '<sqlite3.h>'
-linklib 'sqlite3'
+if not pragmas.staticSqlite then
+  linklib 'sqlite3'
+end
 ]]
 
 local sqlite3: type <cimport,nodecl,forwarddecl> = @record{}
]=]

local function system(cmd)
  print('+ ' .. cmd)
  return os.execute(cmd)
end

if #arg == 1 then
  if arg[1] == 'all' then
    system 'nelua patch'
    system 'nelua amalgamation'
    system 'nelua cross'
    arg = {'-i', ''}
  elseif arg[1] == 'clean' then
    for file in string.gmatch('sqlite-amalgamation-3430200.zip sqlite3.c sqlite3.h sqlite3.o sqlite3ext.h shell.c sqlite3.nelua sqlite3_test sqlite3_test.nelua example.sqlite3', '%S+') do
      print('+ rm ' .. file)
      os.remove(file)
    end
    arg = {'-i', ''}
  elseif arg[1] == 'patch' then
    system('cp ' .. batteries .. '/sqlite3.nelua .')
    system('cp ' .. batteries .. '/example.sqlite3 .')
    local file, err, errcode = io.popen('patch sqlite3.nelua', 'w')
    assert(err == nil)
    file:write(patch)
    file:close()
    arg = {'-i', ''}
  elseif arg[1] == 'amalgamation' then
    local v='3430200'
    system('wget https://sqlite.org/2023/sqlite-amalgamation-'..v..'.zip')
    system('unzip sqlite-amalgamation-'..v)
    system('mv sqlite-amalgamation-'..v..'/* .')
    system('rmdir sqlite-amalgamation-'..v)
    arg = {'-i', ''}
  elseif arg[1] == 'cross' then
    system 'zig cc --target=x86_64-linux-musl -c sqlite3.c'
    arg = { '-V',
      '-P', 'staticSqlite',
      '--cc', 'zig cc --target=x86_64-linux-musl',
      '-o', 'sqlite3_test',
      batteries .. '/tests/sqlite3_test.nelua',
      '-L', '.',
      '-L', batteries,
      '--cflags=sqlite3.o -I.',
    }
    print('cmd:', table.concat(arg, ' '))
  end
end
return {}

How about an alternative?

https://justine.lol/cosmo3/