compile modules in topological order
@@ -67,31 +67,44 @@ local function mods2so(mods)
return sos
end
-local sources = { }
-
local generated_code, deps = coder.generate(args.modname, ast)
-table.insert(sources, { source = args.output, deps = mods2so(deps) })
generated_code = pretty.reindent_c(generated_code)
ok, errmsg = util.set_file_contents(args.output, generated_code)
if not ok then exit(errmsg) end
-for name, mod in pairs(checker.imported) do
- if name ~= args.modname then
+local CC = os.getenv("CC") or "gcc"
+local CFLAGS = os.getenv("CFLAGS") or "--std=c99 -O2 -Wall"
+
+local function compile_module(name)
+ if not checker.imported[name].compiled then
+ local mod = checker.imported[name]
local code, deps = coder.generate(name, mod.ast)
code = pretty.reindent_c(code)
local filename = mod.filename:gsub("[.]titan$", "") .. ".c"
- ok, errmsg = util.set_file_contents(filename, code)
- if not ok then exit(errmsg) end
- table.insert(sources, { source = filename, deps = mods2so(deps) })
+ local ok, err = util.set_file_contents(filename, code)
+ if not ok then return exit(err) end
+ for i = 1, #deps do
+ compile_module(deps[i])
+ deps[i] = mods2so(deps[i])
+ end
+ local soname = filename:gsub("[.]c$", ".so")
+ local cc_cmd = string.format([[
+ %s %s -I%s -fPIC -shared %s %s -o %s
+ ]], CC, CFLAGS, args.lua, filename, table.concat(deps, " "), soname)
+ --print(cc_cmd)
+ local ok, err = os.execute(cc_cmd)
+ if not ok then return exit(err) end
+ checker.imported[name].compiled = true
end
end
-local CC = os.getenv("CC") or "gcc"
-local CFLAGS = os.getenv("CFLAGS") or "--std=c99 -O2 -Wall"
+for name, _ in pairs(checker.imported) do
+ if name ~= args.modname then
+ compile_module(name)
+ end
+end
-for _, source in ipairs(sources) do
- local soname = source.source:gsub("[.]c$", ".so")
+local soname = args.output:gsub("[.]c$", ".so")
os.execute(string.format([[
%s %s -I%s -fPIC -shared %s %s -o %s
-]], CC, CFLAGS, args.lua, source.source, table.concat(source.deps, " "), soname))
-end
+]], CC, CFLAGS, args.lua, args.output, table.concat(mods2so(deps), " "), soname))
