Created
May 6, 2012 23:43
-
-
Save davidhollander/2625031 to your computer and use it in GitHub Desktop.
write gcc preprocessed headers to lua module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
./ffipp out.lua openssl/ssl.h