Instantly share code, notes, and snippets.

What would you like to do?
Lua makefile generator in < 100 lines of code.
Simple Makefile generator in Lua 5.1.
With gcc include dependency support.
WARNING: proof of concept; not well tested. GNU style Makefile.
See premake for a more complete implementation.
(c) David Manura, 2013-03. MIT license (same as Lua).
local M = {}
-- Replaces file name extension on path s with e. Example: ext('foo.c','.o')
function M.ext(s, e) return (s:gsub('%.[^%.]+', e)) end
-- Convenience list class.
local array_mt = {}
array_mt.__index = array_mt
function array_mt.__add(a, b)
local t = {}
for _,x in ipairs(a) do t[#t+1] = x end
for _,x in ipairs(b) do t[#t+1] = x end
return setmetatable(t, array_mt)
function array_mt.ext(self, e)
local t = {}
for _,x in ipairs(self) do t[#t+1] = M.ext(x, e) end
return setmetatable(t, array_mt)
function array_mt:string() return table.concat(self, ' ') end
function array_mt.iter(self)
local i=0
return function() i = i + 1; return self[i] end
function M.qw(s) -- Perl-like quote operator
s = s or ''
local t = {}
for x in s:gmatch'%S+' do t[#t+1] = x end
return setmetatable(t, array_mt)
function M.builder()
local preambles = {'# warning: this file is autogenerated'}
local postambles = {}
local rules = {}
local default_rule
local all_outputs = M.qw()
local phonies = {}
local b = {}
local function rule_helper(cmd, output, input, default)
table.insert(rules, default and 1 or #rules+1,
array_mt.string(output)..' : '..array_mt.string(input)..
(cmd and '\n\t'..cmd or ''))
function b:rule(cmd, output, input, opt)
opt = opt or ''
local phony = opt:match'p'; local default = opt:match'd'
rule_helper(cmd, output, input, default)
if phony then table.insert(phonies, output[1])
else all_outputs = all_outputs + output end
local function helper(t, k, v) if not t[k] then table.insert(t, v); t[k] = #t end end
function b:preamble(text, name) helper(preambles, name, text) end
function b:postamble(text, name) helper(postambles, name, text) end
function b:gcc(gcc, cmd, output, input)
@cp $*.d $*.P; \
sed -e 's/\#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
-e '/^$$/ d' -e 's/$$/ :/' < $*.d >> $*.P; \
rm -f $*.d #]], 'gcc')
b:rule(gcc .. ' -MD -MF ' .. output[1]:gsub('%.[^%.]+', '.d')
.. ' -c -o ' .. output[1] .. ' ' .. input[1] .. ' ' .. cmd .. '\n\t$(__MAKEDEPEND)',
output, input)
postambles[#postambles+1] = '-include ' .. M.ext(output[1], '.P')
all_outputs = all_outputs + {M.ext(output[1], '.P')}
function b:gen_preamble() for _, text in ipairs(preambles) do b:writeln(text) end end
function b:gen_postamble() for _, text in ipairs(postambles) do b:writeln(text) end end
function b:write(s) io.stdout:write(s) end
function b:writeln(s) io.stdout:write(s, '\n') end
function b:generate()
if #all_outputs > 0 then
b:rule('rm -f ' .. array_mt.string(all_outputs), {'clean'}, {}, 'p') end
if #phonies > 0 then rule_helper(nil, {'.PHONY'} , phonies) end
if default_rule then b:writeln(default_rule) end
for _, rule in ipairs(rules) do b:writeln(rule) end
return b
return M
-- test_lua52.lua - Example of building Lua 5.2 using makebuild.lua.
local B = dofile 'lmake.lua'
local qw = B.qw; local ext = B.ext
local CORE_SRC = qw[[
lapi.c lcode.c lctype.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c
lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c
ltm.c lundump.c lvm.c lzio.c
local LIB_SRC = qw[[
lauxlib.c lbaselib.c lbitlib.c lcorolib.c ldblib.c liolib.c
lmathlib.c loslib.c lstrlib.c ltablib.c loadlib.c linit.c
local ALL_SRC = BASE_SRC + qw'lua.c luac.c'
local b = B.builder()
for src in ALL_SRC:iter() do
b:gcc('gcc', '', {ext(src, '.o')}, {src})
b:rule('ar rcu liblua.a '..BASE_SRC:ext('.o'):string(), {'liblua.a'}, BASE_SRC:ext('.o'))
b:rule('gcc -o lua lua.c liblua.a -lm', {'lua'}, {'lua.c', 'liblua.a'})
b:rule('gcc -o luac luac.c liblua.a -lm', {'luac'}, {'luac.c', 'liblua.a'})
b:rule(nil, {'all'}, {'lua'}, 'dp')
# example output of running test_lua52.lua
# warning: this file is autogenerated
@cp $*.d $*.P; \
sed -e 's/\#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
-e '/^$$/ d' -e 's/$$/ :/' < $*.d >> $*.P; \
rm -f $*.d #
all : lua
lapi.o : lapi.c
gcc -MD -MF lapi.d -c -o lapi.o lapi.c
lcode.o : lcode.c
gcc -MD -MF lcode.d -c -o lcode.o lcode.c
lctype.o : lctype.c
gcc -MD -MF lctype.d -c -o lctype.o lctype.c
ldebug.o : ldebug.c
gcc -MD -MF ldebug.d -c -o ldebug.o ldebug.c
ldo.o : ldo.c
gcc -MD -MF ldo.d -c -o ldo.o ldo.c
ldump.o : ldump.c
gcc -MD -MF ldump.d -c -o ldump.o ldump.c
lfunc.o : lfunc.c
gcc -MD -MF lfunc.d -c -o lfunc.o lfunc.c
lgc.o : lgc.c
gcc -MD -MF lgc.d -c -o lgc.o lgc.c
llex.o : llex.c
gcc -MD -MF llex.d -c -o llex.o llex.c
lmem.o : lmem.c
gcc -MD -MF lmem.d -c -o lmem.o lmem.c
lobject.o : lobject.c
gcc -MD -MF lobject.d -c -o lobject.o lobject.c
lopcodes.o : lopcodes.c
gcc -MD -MF lopcodes.d -c -o lopcodes.o lopcodes.c
lparser.o : lparser.c
gcc -MD -MF lparser.d -c -o lparser.o lparser.c
lstate.o : lstate.c
gcc -MD -MF lstate.d -c -o lstate.o lstate.c
lstring.o : lstring.c
gcc -MD -MF lstring.d -c -o lstring.o lstring.c
ltable.o : ltable.c
gcc -MD -MF ltable.d -c -o ltable.o ltable.c
ltm.o : ltm.c
gcc -MD -MF ltm.d -c -o ltm.o ltm.c
lundump.o : lundump.c
gcc -MD -MF lundump.d -c -o lundump.o lundump.c
lvm.o : lvm.c
gcc -MD -MF lvm.d -c -o lvm.o lvm.c
lzio.o : lzio.c
gcc -MD -MF lzio.d -c -o lzio.o lzio.c
lauxlib.o : lauxlib.c
gcc -MD -MF lauxlib.d -c -o lauxlib.o lauxlib.c
lbaselib.o : lbaselib.c
gcc -MD -MF lbaselib.d -c -o lbaselib.o lbaselib.c
lbitlib.o : lbitlib.c
gcc -MD -MF lbitlib.d -c -o lbitlib.o lbitlib.c
lcorolib.o : lcorolib.c
gcc -MD -MF lcorolib.d -c -o lcorolib.o lcorolib.c
ldblib.o : ldblib.c
gcc -MD -MF ldblib.d -c -o ldblib.o ldblib.c
liolib.o : liolib.c
gcc -MD -MF liolib.d -c -o liolib.o liolib.c
lmathlib.o : lmathlib.c
gcc -MD -MF lmathlib.d -c -o lmathlib.o lmathlib.c
loslib.o : loslib.c
gcc -MD -MF loslib.d -c -o loslib.o loslib.c
lstrlib.o : lstrlib.c
gcc -MD -MF lstrlib.d -c -o lstrlib.o lstrlib.c
ltablib.o : ltablib.c
gcc -MD -MF ltablib.d -c -o ltablib.o ltablib.c
loadlib.o : loadlib.c
gcc -MD -MF loadlib.d -c -o loadlib.o loadlib.c
linit.o : linit.c
gcc -MD -MF linit.d -c -o linit.o linit.c
lua.o : lua.c
gcc -MD -MF lua.d -c -o lua.o lua.c
luac.o : luac.c
gcc -MD -MF luac.d -c -o luac.o luac.c
liblua.a : lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o lauxlib.o lbaselib.o lbitlib.o lcorolib.o ldblib.o liolib.o lmathlib.o loslib.o lstrlib.o ltablib.o loadlib.o linit.o
ar rcu liblua.a lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o lauxlib.o lbaselib.o lbitlib.o lcorolib.o ldblib.o liolib.o lmathlib.o loslib.o lstrlib.o ltablib.o loadlib.o linit.o
lua : lua.c liblua.a
gcc -o lua lua.c liblua.a -lm
luac : luac.c liblua.a
gcc -o luac luac.c liblua.a -lm
clean :
rm -f lapi.o lapi.P lcode.o lcode.P lctype.o lctype.P ldebug.o ldebug.P ldo.o ldo.P ldump.o ldump.P lfunc.o lfunc.P lgc.o lgc.P llex.o llex.P lmem.o lmem.P lobject.o lobject.P lopcodes.o lopcodes.P lparser.o lparser.P lstate.o lstate.P lstring.o lstring.P ltable.o ltable.P ltm.o ltm.P lundump.o lundump.P lvm.o lvm.P lzio.o lzio.P lauxlib.o lauxlib.P lbaselib.o lbaselib.P lbitlib.o lbitlib.P lcorolib.o lcorolib.P ldblib.o ldblib.P liolib.o liolib.P lmathlib.o lmathlib.P loslib.o loslib.P lstrlib.o lstrlib.P ltablib.o ltablib.P loadlib.o loadlib.P linit.o linit.P lua.o lua.P luac.o luac.P liblua.a lua luac
.PHONY : all clean
-include lapi.P
-include lcode.P
-include lctype.P
-include ldebug.P
-include ldo.P
-include ldump.P
-include lfunc.P
-include lgc.P
-include llex.P
-include lmem.P
-include lobject.P
-include lopcodes.P
-include lparser.P
-include lstate.P
-include lstring.P
-include ltable.P
-include ltm.P
-include lundump.P
-include lvm.P
-include lzio.P
-include lauxlib.P
-include lbaselib.P
-include lbitlib.P
-include lcorolib.P
-include ldblib.P
-include liolib.P
-include lmathlib.P
-include loslib.P
-include lstrlib.P
-include ltablib.P
-include loadlib.P
-include linit.P
-include lua.P
-include luac.P
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment