premake/modules/gmake/gmake.lua

303 lines
6.2 KiB
Lua
Raw Normal View History

--
2017-06-14 23:38:10 +00:00
-- gmake.lua
-- Define the makefile action(s).
-- Copyright (c) 2002-2015 Jason Perkins and the Premake project
--
local p = premake
2017-06-14 23:38:10 +00:00
p.modules.gmake = {}
p.modules.gmake._VERSION = p._VERSION
2017-06-14 23:38:10 +00:00
-- for backwards compatibility.
p.make = p.modules.gmake
2012-04-30 20:42:03 +00:00
2017-06-14 23:38:10 +00:00
local make = p.make
local project = p.project
2012-04-30 20:42:03 +00:00
--
-- Write out the default configuration rule for a workspace or project.
--
-- @param target
-- The workspace or project object for which a makefile is being generated.
--
function make.defaultconfig(target)
-- find the right configuration iterator function for this object
local eachconfig = iif(target.project, project.eachconfig, p.workspace.eachconfig)
local defaultconfig = nil
-- find the right default configuration platform, grab first configuration that matches
if target.defaultplatform then
for cfg in eachconfig(target) do
if cfg.platform == target.defaultplatform then
defaultconfig = cfg
break
end
end
end
-- grab the first configuration and write the block
if not defaultconfig then
local iter = eachconfig(target)
defaultconfig = iter()
end
if defaultconfig then
_p('ifndef config')
_x(' config=%s', defaultconfig.shortname)
_p('endif')
_p('')
end
end
---
-- Escape a string so it can be written to a makefile.
---
function make.esc(value)
result = value:gsub("\\", "\\\\")
2016-05-02 17:18:36 +00:00
result = result:gsub("\"", "\\\"")
result = result:gsub(" ", "\\ ")
2015-09-02 21:01:10 +00:00
result = result:gsub("%(", "\\(")
result = result:gsub("%)", "\\)")
-- leave $(...) shell replacement sequences alone
2015-09-02 21:01:10 +00:00
result = result:gsub("$\\%((.-)\\%)", "$(%1)")
return result
end
2012-04-30 20:42:03 +00:00
--
-- Get the makefile file name for a workspace or a project. If this object is the
2012-04-30 20:42:03 +00:00
-- only one writing to a location then I can use "Makefile". If more than one object
-- writes to the same location I use name + ".make" to keep it unique.
--
function make.getmakefilename(this, searchprjs)
local count = 0
2015-08-12 18:56:06 +00:00
for wks in p.global.eachWorkspace() do
if wks.location == this.location then
count = count + 1
2012-04-30 20:42:03 +00:00
end
if searchprjs then
2015-08-12 18:56:06 +00:00
for _, prj in ipairs(wks.projects) do
2012-04-30 20:42:03 +00:00
if prj.location == this.location then
count = count + 1
end
end
end
end
2012-04-30 20:42:03 +00:00
if count == 1 then
return "Makefile"
else
return ".make"
2012-04-30 20:42:03 +00:00
end
end
--
-- Output a makefile header.
--
-- @param target
-- The workspace or project object for which the makefile is being generated.
--
function make.header(target)
local kind = iif(target.project, "project", "workspace")
2017-04-25 05:44:13 +00:00
_p('# %s %s makefile autogenerated by Premake', p.action.current().shortname, kind)
_p('')
if kind == "workspace" then
local haspch = false
for _, prj in ipairs(target.projects) do
for cfg in project.eachconfig(prj) do
if cfg.pchheader then
haspch = true
end
end
end
if haspch then
_p('.NOTPARALLEL:')
_p('')
end
end
make.defaultconfig(target)
_p('ifndef verbose')
_p(' SILENT = @')
_p('endif')
_p('')
end
--
-- Rules for file ops based on the shell type. Can't use defines and $@ because
-- it screws up the escaping of spaces and parethesis (anyone know a fix?)
--
2017-01-05 14:33:51 +00:00
function make.mkdir(dirname)
_p('ifeq (posix,$(SHELLTYPE))')
_p('\t$(SILENT) mkdir -p %s', dirname)
_p('else')
_p('\t$(SILENT) mkdir $(subst /,\\\\,%s)', dirname)
_p('endif')
end
2017-01-05 14:33:51 +00:00
function make.mkdirRules(dirname)
_p('%s:', dirname)
_p('\t@echo Creating %s', dirname)
make.mkdir(dirname)
_p('')
end
--
-- Format a list of values to be safely written as part of a variable assignment.
--
function make.list(value, quoted)
quoted = false
if #value > 0 then
if quoted then
local result = ""
for _, v in ipairs (value) do
if #result then
result = result .. " "
end
2017-04-25 05:44:13 +00:00
result = result .. p.quoted(v)
end
return result
else
return " " .. table.concat(value, " ")
end
else
return ""
end
end
2012-06-13 22:29:51 +00:00
--
-- Convert an arbitrary string (project name) to a make variable name.
--
function make.tovar(value)
value = value:gsub("[ -]", "_")
value = value:gsub("[()]", "")
return value
end
---------------------------------------------------------------------------
--
-- Handlers for the individual makefile elements that can be shared
-- between the different language projects.
2012-06-13 22:29:51 +00:00
--
---------------------------------------------------------------------------
function make.objdir(cfg)
2017-04-25 05:44:13 +00:00
_x(' OBJDIR = %s', p.esc(project.getrelative(cfg.project, cfg.objdir)))
end
function make.objDirRules(prj)
make.mkdirRules("$(OBJDIR)")
end
function make.phonyRules(prj)
_p('.PHONY: clean prebuild prelink')
_p('')
end
function make.buildCmds(cfg, event)
_p(' define %sCMDS', event:upper())
local steps = cfg[event .. "commands"]
local msg = cfg[event .. "message"]
if #steps > 0 then
steps = os.translateCommandsAndPaths(steps, cfg.project.basedir, cfg.project.location)
msg = msg or string.format("Running %s commands", event)
_p('\t@echo %s', msg)
_p('\t%s', table.implode(steps, "", "", "\n\t"))
end
_p(' endef')
end
function make.preBuildCmds(cfg, toolset)
make.buildCmds(cfg, "prebuild")
end
function make.preBuildRules(prj)
_p('prebuild:')
_p('\t$(PREBUILDCMDS)')
_p('')
end
function make.preLinkCmds(cfg, toolset)
make.buildCmds(cfg, "prelink")
end
function make.preLinkRules(prj)
_p('prelink:')
_p('\t$(PRELINKCMDS)')
_p('')
end
function make.postBuildCmds(cfg, toolset)
make.buildCmds(cfg, "postbuild")
end
2012-06-13 22:29:51 +00:00
function make.settings(cfg, toolset)
if #cfg.makesettings > 0 then
for _, value in ipairs(cfg.makesettings) do
_p(value)
end
end
local value = toolset.getmakesettings(cfg)
if value then
_p(value)
2012-06-13 22:29:51 +00:00
end
end
2012-04-30 20:42:03 +00:00
function make.shellType()
2018-05-31 22:33:08 +00:00
_p('SHELLTYPE := posix')
_p('ifeq (.exe,$(findstring .exe,$(ComSpec)))')
_p('\tSHELLTYPE := msdos')
_p('endif')
_p('')
end
function make.target(cfg)
_x(' TARGETDIR = %s', project.getrelative(cfg.project, cfg.buildtarget.directory))
_x(' TARGET = $(TARGETDIR)/%s', cfg.buildtarget.name)
end
function make.targetDirRules(prj)
make.mkdirRules("$(TARGETDIR)")
end
2017-06-14 23:38:10 +00:00
include("gmake_cpp.lua")
include("gmake_csharp.lua")
include("gmake_makefile.lua")
include("gmake_utility.lua")
include("gmake_workspace.lua")
return p.modules.gmake