Skip to content

Instantly share code, notes, and snippets.

@davidhollander
Created May 6, 2012 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidhollander/2625031 to your computer and use it in GitHub Desktop.
Save davidhollander/2625031 to your computer and use it in GitHub Desktop.
write gcc preprocessed headers to lua module
#!/usr/bin/luajit
--[[
ffipp
David Hollander 2012
MIT License
Preprocess C Headers into LuaJIT modules
]]
local CMD = 'gcc -E'
local BASE = '/usr/include/'
local HEAD = '--[[\nGenerated by ffipp at '..os.date('%H:%M %Y.%m.%d')..' '..'\n'
local HEAD2 = ']]\nlocal ffi = require "ffi"\nffi.cdef[[\n'
local TAIL = ']]\n'
local FILTER = function(line) return line end --line:gsub('^%s+','') end
local ti, tc = table.insert, table.concat
local function dump(des, ...)
local list, set = {...}, {}
assert(#list>0, 'ffipp: No headers specified')
for i,v in ipairs(list) do list[i] = BASE..v; set[list[i]] = true end
local list_str = tc(list, ' ')
local lua_head = HEAD .. list_str .. '\n' .. HEAD2
local output = assert(io.open(des,'w'), 'ffipp: Cannot write to: '..des)
local input = assert(io.popen(CMD..' '..list_str), 'ffipp: Cannot popen: '..CMD)
output:write(lua_head)
--local save
local save = true
for l in input:lines() do
local incsrc = l:match '^#.-"(.-)"'
if incsrc then --save = set[incsrc]
elseif save and l~='' then output:write(FILTER(l),'\n') end
end
output:write(TAIL)
output:flush()
output:close()
end
-- INPUT
--
return (...) and dump(...) or {dump=dump}
@davidhollander
Copy link
Author

Example:
./ffipp out.lua openssl/ssl.h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment