2006-10-15 00:28:12 +00:00
|
|
|
-- $Id$
|
2007-02-25 12:39:52 +00:00
|
|
|
-- $HeadURL: https://primemover.svn.sf.net/svnroot/primemover/pm/lib/c.pm $
|
|
|
|
-- $LastChangedDate: 2007-02-24 01:37:06 +0000 (Sat, 24 Feb 2007) $
|
2006-10-15 00:28:12 +00:00
|
|
|
|
2006-07-20 23:24:28 +00:00
|
|
|
-- pm includefile to compile *host* C programs.
|
|
|
|
|
|
|
|
-- Standard Lua boilerplate.
|
|
|
|
|
|
|
|
local io_open = io.open
|
|
|
|
local string_gsub = string.gsub
|
|
|
|
local string_gfind = string.gfind
|
2006-07-30 23:33:31 +00:00
|
|
|
local string_find = string.find
|
2006-07-20 23:24:28 +00:00
|
|
|
local table_insert = table.insert
|
|
|
|
local table_getn = table.getn
|
|
|
|
local filetime = pm.filetime
|
|
|
|
|
|
|
|
-- Define some variables.
|
|
|
|
|
|
|
|
CCOMPILER = "gcc"
|
2006-10-15 00:28:12 +00:00
|
|
|
CXXCOMPILER = "g++"
|
|
|
|
CC = "%CCOMPILER% %CBUILDFLAGS% %CDYNINCLUDES:cincludes% %CINCLUDES:cincludes% %CDEFINES:cdefines% %CEXTRAFLAGS% -c -o %out% %in%"
|
|
|
|
CXX = "%CXXCOMPILER% %CBUILDFLAGS% %CDYNINCLUDES:cincludes% %CINCLUDES:cincludes% %CDEFINES:cdefines% %CEXTRAFLAGS% -c -o %out% %in%"
|
|
|
|
CPROGRAM = "%CCOMPILER% %CBUILDFLAGS% %CLINKFLAGS% %CEXTRAFLAGS% -o %out% %in% %CLIBRARIES:clibraries%"
|
|
|
|
CXXPROGRAM = "%CXXCOMPILER% %CBUILDFLAGS% %CLINKFLAGS% %CEXTRAFLAGS% -o %out% %in% %CLIBRARIES:clibraries%"
|
|
|
|
|
|
|
|
CLIBRARY = "rm -f %out% && ar cr %out% %in% && ranlib %out%"
|
2006-07-20 23:24:28 +00:00
|
|
|
|
2006-07-26 18:24:16 +00:00
|
|
|
CBUILDFLAGS = {"-g"}
|
|
|
|
CINCLUDES = EMPTY
|
|
|
|
CDEFINES = EMPTY
|
|
|
|
CEXTRAFLAGS = EMPTY
|
|
|
|
CLINKFLAGS = EMPTY
|
|
|
|
CDYNINCLUDES = EMPTY
|
|
|
|
CLIBRARIES = EMPTY
|
2006-07-20 23:24:28 +00:00
|
|
|
|
2006-10-15 00:28:12 +00:00
|
|
|
--- Custom string modifiers -------------------------------------------------
|
|
|
|
|
|
|
|
local function prepend(rule, arg, prefix)
|
|
|
|
if (arg == EMPTY) then
|
|
|
|
return EMPTY
|
|
|
|
end
|
|
|
|
|
|
|
|
local t = {}
|
|
|
|
for i, j in ipairs(arg) do
|
|
|
|
t[i] = prefix..j
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
function pm.stringmodifier.cincludes(rule, arg)
|
|
|
|
return prepend(rule, arg, "-I")
|
|
|
|
end
|
|
|
|
|
|
|
|
function pm.stringmodifier.cdefines(rule, arg)
|
|
|
|
return prepend(rule, arg, "-D")
|
|
|
|
end
|
|
|
|
|
|
|
|
function pm.stringmodifier.clibraries(rule, arg)
|
|
|
|
if (arg == EMPTY) then
|
|
|
|
return EMPTY
|
|
|
|
end
|
|
|
|
|
|
|
|
local t = {}
|
|
|
|
for i, j in ipairs(arg) do
|
|
|
|
if string_find(j, "%.a$") then
|
|
|
|
t[i] = j
|
|
|
|
else
|
|
|
|
t[i] = "-l"..j
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2006-07-20 23:24:28 +00:00
|
|
|
--- Manage C file dependencies ----------------------------------------------
|
|
|
|
|
|
|
|
local dependency_cache = {}
|
2006-07-30 23:33:31 +00:00
|
|
|
local function calculate_dependencies(filename, includes)
|
|
|
|
-- Cache values, so we don't recalculate dependencies needlessly.
|
|
|
|
|
|
|
|
local o = dependency_cache[filename]
|
2006-07-20 23:24:28 +00:00
|
|
|
if o then
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
2006-07-30 23:33:31 +00:00
|
|
|
local deps = {}
|
|
|
|
deps[filename] = true
|
2006-07-20 23:24:28 +00:00
|
|
|
|
2006-07-30 23:33:31 +00:00
|
|
|
local calcdeps = 0
|
|
|
|
calcdeps = function(filename, file)
|
2006-10-15 00:28:12 +00:00
|
|
|
file = file or io_open(filename)
|
2006-07-30 23:33:31 +00:00
|
|
|
if not file then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local localincludes = string_gsub(filename, "/[^/]*$", "")
|
|
|
|
if localincludes then
|
|
|
|
localincludes = {localincludes, unpack(includes)}
|
|
|
|
else
|
|
|
|
localincludes = includes
|
|
|
|
end
|
|
|
|
|
|
|
|
for line in file:lines() do
|
|
|
|
local _, _, f = string_find(line, '^[ \t]*#[ \t]*include[ \t]*["<]([^"]+)[">]')
|
|
|
|
if f then
|
|
|
|
for _, path in ipairs(localincludes) do
|
|
|
|
local subfilename = path.."/"..f
|
2006-10-15 00:28:12 +00:00
|
|
|
local subfile = io_open(subfilename)
|
2006-07-30 23:33:31 +00:00
|
|
|
if subfile then
|
|
|
|
if not deps[subfilename] then
|
|
|
|
deps[subfilename] = true
|
|
|
|
calcdeps(subfilename, subfile)
|
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Explicit close to avoid having to wait for the garbage collector
|
|
|
|
-- to free up the underlying fd.
|
|
|
|
|
|
|
|
file:close()
|
2006-07-20 23:24:28 +00:00
|
|
|
end
|
|
|
|
|
2006-07-30 23:33:31 +00:00
|
|
|
calcdeps(filename)
|
2006-07-20 23:24:28 +00:00
|
|
|
o = {}
|
2006-07-30 23:33:31 +00:00
|
|
|
for i, _ in pairs(deps) do
|
|
|
|
table_insert(o, i)
|
2006-07-20 23:24:28 +00:00
|
|
|
end
|
2006-07-30 23:33:31 +00:00
|
|
|
|
|
|
|
dependency_cache[filename] = o
|
2006-07-20 23:24:28 +00:00
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
|
|
|
-- This clause specialises 'simple' to add support for smart dependencies of C
|
|
|
|
-- files.
|
|
|
|
|
|
|
|
simple_with_clike_dependencies = simple {
|
|
|
|
class = "simple_with_clike_dependencies",
|
|
|
|
makedepends = {"%CDEPENDS%"},
|
|
|
|
|
|
|
|
__init = function(self, p)
|
|
|
|
simple.__init(self, p)
|
|
|
|
|
|
|
|
-- If we're a class, don't verify.
|
|
|
|
|
|
|
|
if ((type(p) == "table") and p.class) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- If dynamicheaders is an object, turn it into a singleton list.
|
|
|
|
|
|
|
|
if self.dynamicheaders then
|
|
|
|
if (type(self.dynamicheaders) ~= "table") then
|
|
|
|
self:__error("doesn't know what to do with dynamicheaders, which ",
|
|
|
|
"should be a list or an object but was a ", type(self.dynamicheaders))
|
|
|
|
end
|
|
|
|
if self.dynamicheaders.class then
|
|
|
|
self.dynamicheaders = {self.dynamicheaders}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2006-07-30 23:33:31 +00:00
|
|
|
__dependencies = function(self, inputs, outputs)
|
2006-10-15 00:28:12 +00:00
|
|
|
local cincludes = self:__index("CINCLUDES")
|
2006-07-30 23:33:31 +00:00
|
|
|
if (type(cincludes) == "string") then
|
|
|
|
cincludes = {cincludes}
|
|
|
|
end
|
|
|
|
|
|
|
|
local includes = {}
|
|
|
|
for _, i in ipairs(cincludes) do
|
2006-10-15 00:28:12 +00:00
|
|
|
table_insert(includes, self:__expand(i))
|
2006-07-30 23:33:31 +00:00
|
|
|
end
|
|
|
|
|
2006-10-15 00:28:12 +00:00
|
|
|
local input = self:__expand(inputs[1])
|
|
|
|
local depends = calculate_dependencies(input, includes)
|
2006-07-20 23:24:28 +00:00
|
|
|
if not depends then
|
|
|
|
self:__error("could not determine the dependencies for ",
|
2006-10-15 00:28:12 +00:00
|
|
|
pm.rendertable({input}))
|
2006-07-20 23:24:28 +00:00
|
|
|
end
|
2006-07-30 23:33:31 +00:00
|
|
|
if pm.verbose then
|
2006-10-15 00:28:12 +00:00
|
|
|
pm.message('"', input, '" appears to depend on ',
|
2006-07-30 23:33:31 +00:00
|
|
|
pm.rendertable(depends))
|
|
|
|
end
|
2006-07-20 23:24:28 +00:00
|
|
|
return depends
|
|
|
|
end,
|
|
|
|
|
|
|
|
__buildadditionalchildren = function(self)
|
2006-10-15 00:28:12 +00:00
|
|
|
self.CDYNINCLUDES = {}
|
2006-07-20 23:24:28 +00:00
|
|
|
if self.dynamicheaders then
|
|
|
|
for _, i in ipairs(self.dynamicheaders) do
|
|
|
|
local o = i:__build()
|
|
|
|
if o[1] then
|
2006-10-15 00:28:12 +00:00
|
|
|
table_insert(self.CDYNINCLUDES, (string_gsub(o[1], "/[^/]*$", "")))
|
2006-07-20 23:24:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2006-10-15 00:28:12 +00:00
|
|
|
-- If no paths on the list, replace the list with EMPTY so it doesn't
|
|
|
|
-- expand to anything.
|
|
|
|
if (table_getn(self.CDYNINCLUDES) == 0) then
|
|
|
|
self.CDYNINCLUDES = EMPTY
|
|
|
|
end
|
2006-07-20 23:24:28 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
-- These are the publically useful clauses.
|
|
|
|
|
|
|
|
cfile = simple_with_clike_dependencies {
|
|
|
|
class = "cfile",
|
|
|
|
command = {"%CC%"},
|
|
|
|
outputs = {"%U%-%I%.o"},
|
|
|
|
}
|
|
|
|
|
2006-10-15 00:28:12 +00:00
|
|
|
cxxfile = simple_with_clike_dependencies {
|
|
|
|
class = "cxxfile",
|
|
|
|
command = {"%CXX%"},
|
|
|
|
outputs = {"%U%-%I%.o"},
|
|
|
|
}
|
|
|
|
|
2006-07-20 23:24:28 +00:00
|
|
|
cprogram = simple {
|
|
|
|
class = "cprogram",
|
|
|
|
command = {"%CPROGRAM%"},
|
|
|
|
outputs = {"%U%-%I%"},
|
|
|
|
}
|
|
|
|
|
2006-10-15 00:28:12 +00:00
|
|
|
cxxprogram = simple {
|
|
|
|
class = "cxxprogram",
|
|
|
|
command = {"%CXXPROGRAM%"},
|
|
|
|
outputs = {"%U%-%I%"},
|
|
|
|
}
|
|
|
|
|
2006-07-20 23:24:28 +00:00
|
|
|
clibrary = simple {
|
|
|
|
class = "clibrary",
|
|
|
|
command = {
|
2006-10-15 00:28:12 +00:00
|
|
|
"%CLIBRARY%"
|
2006-07-20 23:24:28 +00:00
|
|
|
},
|
|
|
|
outputs = {"%U%-%I%.a"},
|
|
|
|
}
|