--
-- codeblocks_cbp.lua
-- Generate a Code::Blocks C/C++ project.
-- Copyright (c) 2009, 2011 Jason Perkins and the Premake project
--
local codeblocks = premake.codeblocks
--
-- Write out a list of the source code files in the project.
--
function codeblocks.files(prj)
local pchheader
if (prj.pchheader) then
pchheader = path.getrelative(prj.location, prj.pchheader)
end
for fcfg in premake.project.eachfile(prj) do
_p(2,'', premake.esc(fcfg.name))
if fcfg.name ~= fcfg.vpath then
_p(3,'', path.getdirectory(fcfg.vpath))
end
if path.isresourcefile(fcfg.name) then
_p(3,'')
elseif path.iscfile(fcfg.name) and prj.language == "C++" then
_p(3,'')
end
if not prj.flags.NoPCH and fcfg.name == pchheader then
_p(3,'', iif(prj.language == "C", "CC", "CPP"))
_p(3,'')
_p(3,'')
_p(3,'')
end
_p(2,'')
end
end
function premake.codeblocks.debugenvs(cfg)
--Assumption: if gcc is being used then so is gdb although this section will be ignored by
--other debuggers. If using gcc and not gdb it will silently not pass the
--environment arguments to the debugger
if premake.gettool(cfg) == premake.gcc then
_p(3,'')
_p(4,'', premake.esc(cfg.longname))
local args = ''
local sz = #cfg.debugenvs
for idx, v in ipairs(cfg.debugenvs) do
args = args .. 'set env ' .. v
if sz ~= idx then args = args .. '
' end
end
_p(5,'',args)
_p(4,'')
_p(3,'')
else
error('Sorry at this moment there is no support for debug environment variables with this debugger and codeblocks')
end
end
--
-- The main function: write out the project file.
--
function premake.codeblocks.cbp(prj)
-- alias the C/C++ compiler interface
local cc = premake.gettool(prj)
_p('')
_p('')
_p(1,'')
-- write project block header
_p(1,'')
_p(2,'', premake.esc(prj.name))
_p(2,'')
_p(2,'', _OPTIONS.cc)
-- build a list of supported target platforms; I don't support cross-compiling yet
local platforms = premake.filterplatforms(prj.solution, cc.platforms, "Native")
for i = #platforms, 1, -1 do
if premake.platforms[platforms[i]].iscrosscompiler then
table.remove(platforms, i)
end
end
-- write configuration blocks
_p(2,'')
for _, platform in ipairs(platforms) do
for cfg in premake.eachconfig(prj, platform) do
_p(3,'', premake.esc(cfg.longname))
_p(4,'', premake.esc(cfg.buildtarget.fullpath))
if cfg.debugdir then
_p(4,'', premake.esc(cfg.debugdir))
end
_p(4,'', premake.esc(cfg.objectsdir))
-- identify the type of binary
local types = { WindowedApp = 0, ConsoleApp = 1, StaticLib = 2, SharedLib = 3 }
_p(4,'', types[cfg.kind])
_p(4,'', _OPTIONS.cc)
if (cfg.kind == "SharedLib") then
_p(4,'')
_p(4,'', iif(cfg.flags.NoImportLib, 0, 1))
end
-- begin compiler block --
_p(4,'')
for _,flag in ipairs(table.join(cc.getcflags(cfg), cc.getcxxflags(cfg), cc.getdefines(cfg.defines), cfg.buildoptions)) do
_p(5,'', premake.esc(flag))
end
if not cfg.flags.NoPCH and cfg.pchheader then
_p(5,'')
_p(5,'', premake.esc(cfg.pchheader))
end
for _,v in ipairs(cfg.includedirs) do
_p(5,'', premake.esc(v))
end
_p(4,'')
-- end compiler block --
-- begin linker block --
_p(4,'')
for _,flag in ipairs(table.join(cc.getldflags(cfg), cfg.linkoptions)) do
_p(5,'', premake.esc(flag))
end
for _,v in ipairs(premake.getlinks(cfg, "all", "directory")) do
_p(5,'', premake.esc(v))
end
for _,v in ipairs(premake.getlinks(cfg, "all", "basename")) do
_p(5,'', premake.esc(v))
end
_p(4,'')
-- end linker block --
-- begin resource compiler block --
if premake.findfile(cfg, ".rc") then
_p(4,'')
for _,v in ipairs(cfg.includedirs) do
_p(5,'', premake.esc(v))
end
for _,v in ipairs(cfg.resincludedirs) do
_p(5,'', premake.esc(v))
end
_p(4,'')
end
-- end resource compiler block --
-- begin build steps --
if #cfg.prebuildcommands > 0 or #cfg.postbuildcommands > 0 then
_p(4,'')
for _,v in ipairs(cfg.prebuildcommands) do
_p(5,'', premake.esc(v))
end
for _,v in ipairs(cfg.postbuildcommands) do
_p(5,'', premake.esc(v))
end
_p(4,'')
end
-- end build steps --
_p(3,'')
end
end
_p(2,'')
codeblocks.files(prj)
_p(2,'')
for _, platform in ipairs(platforms) do
for cfg in premake.eachconfig(prj, platform) do
if cfg.debugenvs and #cfg.debugenvs > 0 then
premake.codeblocks.debugenvs(cfg)
end
end
end
_p(2,'')
_p(1,'')
_p('')
_p('')
end