Skip to content
76 changes: 64 additions & 12 deletions ninja.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,23 @@ function ninja.generateWorkspace(wks)
table.insert(cfgs[cfg.buildcfg], key)

-- set first configuration name
if (cfg_first == nil) and (cfg.kind == p.CONSOLEAPP or cfg.kind == p.WINDOWEDAPP) then
cfg_first = key
if wks.defaultplatform == "" then
if (cfg_first == nil) and (cfg.kind == p.CONSOLEAPP or cfg.kind == p.WINDOWEDAPP) then
cfg_first = key
end
end
if (cfg_first_lib == nil) and (cfg.kind == p.STATICLIB or cfg.kind == p.SHAREDLIB) then
cfg_first_lib = key
end
if prj.name == wks.startproject then

if wks.defaultplatform == "" then
cfg_first = key
elseif prj.name == wks.startproject then
if cfg.platform == wks.defaultplatform then
if cfg_first == nil then
cfg_first = key
end
end
end

-- include other ninja file
Expand Down Expand Up @@ -235,23 +244,49 @@ local function shouldcompileascpp(filecfg)
return path.iscppfile(filecfg.abspath)
end

local function addcfgDependencies(dependencies,cfg)
for i = 1, #cfg.dependson do
local dependposfix = cfg.buildcfg
if cfg.platform then
dependposfix = dependposfix .. "_" .. cfg.platform
end
table.insert(dependencies, cfg.dependson[i] .. "_" .. dependposfix)
end
end

local function getcfgDependencies(cfg)
local dependencies = {}
addcfgDependencies(dependencies,cfg)
return dependencies
end
local function getFileDependencies(cfg)
local dependencies = {}
if #cfg.prebuildcommands > 0 or cfg.prebuildmessage then
dependencies = {"prebuild_" .. get_key(cfg)}
end
for i = 1, #cfg.dependson do
table.insert(dependencies, cfg.dependson[i] .. "_" .. cfg.buildcfg)
end
addcfgDependencies(dependencies,cfg)
return dependencies
end


local function fixincludedir(cfg,includedirs)
local fixincludedirs = {}
for _,value in ipairs(includedirs) do
local newvalue = path.getrelative(cfg.workspace.location, value)

table.insert(fixincludedirs,newvalue)
end
return fixincludedirs
end
local function getcflags(toolset, cfg, filecfg)
local buildopt = ninja.list(filecfg.buildoptions)
local cppflags = ninja.list(toolset.getcppflags(filecfg))
local cflags = ninja.list(toolset.getcflags(filecfg))
local defines = ninja.list(table.join(toolset.getdefines(filecfg.defines), toolset.getundefines(filecfg.undefines)))
local includes = ninja.list(toolset.getincludedirs(cfg, filecfg.includedirs, filecfg.externalincludedirs, filecfg.frameworkdirs, filecfg.includedirsafter))

local fixincludedirs = fixincludedir(cfg,filecfg.includedirs)

local includes = ninja.list(toolset.getincludedirs(cfg, fixincludedirs, filecfg.externalincludedirs, filecfg.frameworkdirs, filecfg.includedirsafter))
local forceincludes = ninja.list(toolset.getforceincludes(cfg))

return buildopt .. cppflags .. cflags .. defines .. includes .. forceincludes
Expand All @@ -262,7 +297,10 @@ local function getcxxflags(toolset, cfg, filecfg)
local cppflags = ninja.list(toolset.getcppflags(filecfg))
local cxxflags = ninja.list(toolset.getcxxflags(filecfg))
local defines = ninja.list(table.join(toolset.getdefines(filecfg.defines), toolset.getundefines(filecfg.undefines)))
local includes = ninja.list(toolset.getincludedirs(cfg, filecfg.includedirs, filecfg.externalincludedirs, filecfg.frameworkdirs, filecfg.includedirsafter))

local fixincludedirs = fixincludedir(cfg,filecfg.includedirs)

local includes = ninja.list(toolset.getincludedirs(cfg, fixincludedirs, filecfg.externalincludedirs, filecfg.frameworkdirs, filecfg.includedirsafter))
local forceincludes = ninja.list(toolset.getforceincludes(cfg))
return buildopt .. cppflags .. cxxflags .. defines .. includes .. forceincludes
end
Expand All @@ -285,13 +323,26 @@ local function getresflags(toolset, cfg, filecfg)
return defines .. includes .. options
end

local function fixupbuildcommands(cfg, commands)
if cfg.workspace.location == cfg.project.location then
return commands
end
local relative = path.getrelative(cfg.workspace.location, cfg.project.location)

local newcommands = { "cd " .. relative }

for _, Item in ipairs(commands) do
table.insert(newcommands, Item)
end
return newcommands
end
local function prebuild_rule(cfg)
if #cfg.prebuildcommands > 0 or cfg.prebuildmessage then
local commands = {}
if cfg.prebuildmessage then
commands = {os.translateCommandsAndPaths("{ECHO} " .. cfg.prebuildmessage, cfg.workspace.basedir, cfg.workspace.location)}
end
commands = table.join(commands, os.translateCommandsAndPaths(cfg.prebuildcommands, cfg.workspace.basedir, cfg.workspace.location))
commands = table.join(commands, os.translateCommandsAndPaths( fixupbuildcommands(cfg,cfg.prebuildcommands), cfg.workspace.basedir, cfg.workspace.location))
if (#commands > 1) then
commands = 'sh -c ' .. ninja.quote(table.implode(commands,"","",";"))
else
Expand All @@ -304,13 +355,14 @@ local function prebuild_rule(cfg)
end
end


local function prelink_rule(cfg)
if #cfg.prelinkcommands > 0 or cfg.prelinkmessage then
local commands = {}
if cfg.prelinkmessage then
commands = {os.translateCommandsAndPaths("{ECHO} " .. cfg.prelinkmessage, cfg.workspace.basedir, cfg.workspace.location)}
end
commands = table.join(commands, os.translateCommandsAndPaths(cfg.prelinkcommands, cfg.workspace.basedir, cfg.workspace.location))
commands = table.join(commands, os.translateCommandsAndPaths( fixupbuildcommands(cfg,cfg.prelinkcommands), cfg.workspace.basedir, cfg.workspace.location))
if (#commands > 1) then
commands = 'sh -c ' .. ninja.quote(table.implode(commands,"","",";"))
else
Expand All @@ -329,7 +381,7 @@ local function postbuild_rule(cfg)
if cfg.postbuildmessage then
commands = {os.translateCommandsAndPaths("{ECHO} " .. cfg.postbuildmessage, cfg.workspace.basedir, cfg.workspace.location)}
end
commands = table.join(commands, os.translateCommandsAndPaths(cfg.postbuildcommands, cfg.workspace.basedir, cfg.workspace.location))
commands = table.join(commands, os.translateCommandsAndPaths( fixupbuildcommands(cfg,cfg.postbuildcommands), cfg.workspace.basedir, cfg.workspace.location))
if (#commands > 1) then
commands = 'sh -c ' .. ninja.quote(table.implode(commands,"","",";"))
else
Expand Down Expand Up @@ -686,7 +738,7 @@ function ninja.generateProjectCfg(cfg)
---------------------------------------------------- build final target
if #cfg.prebuildcommands > 0 or cfg.prebuildmessage then
p.outln("# prebuild")
add_build(cfg, "prebuild_" .. get_key(cfg), {}, "run_prebuild", {}, {}, {}, {})
add_build(cfg, "prebuild_" .. get_key(cfg), {}, "run_prebuild", {}, {}, getcfgDependencies(cfg), {})
end
local prelink_dependency = {}
if #cfg.prelinkcommands > 0 or cfg.prelinkmessage then
Expand Down