forked from premake/premake-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmakelegacy.lua
302 lines (236 loc) · 6.31 KB
/
gmakelegacy.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
--
-- gmake.lua
-- Define the makefile action(s).
-- Copyright (c) 2002-2015 Jess Perkins and the Premake project
--
local p = premake
p.modules.gmakelegacy = {}
p.modules.gmakelegacy._VERSION = p._VERSION
-- for backwards compatibility.
p.makelegacy = p.modules.gmakelegacy
local make = p.makelegacy
local project = p.project
--
-- 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("\\", "\\\\")
result = result:gsub("\"", "\\\"")
result = result:gsub(" ", "\\ ")
result = result:gsub("%(", "\\(")
result = result:gsub("%)", "\\)")
-- leave $(...) shell replacement sequences alone
result = result:gsub("$\\%((.-)\\%)", "$(%1)")
return result
end
--
-- Get the makefile file name for a workspace or a project. If this object is the
-- 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
for wks in p.global.eachWorkspace() do
if wks.location == this.location then
count = count + 1
end
if searchprjs then
for _, prj in ipairs(wks.projects) do
if prj.location == this.location then
count = count + 1
end
end
end
end
if count == 1 then
return "Makefile"
else
return ".make"
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")
_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 parenthesis (anyone know a fix?)
--
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
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
result = result .. p.quoted(v)
end
return result
else
return " " .. table.concat(value, " ")
end
else
return ""
end
end
--
-- 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.
--
---------------------------------------------------------------------------
function make.objdir(cfg)
_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: $(CUSTOMFILES) $(OBJECTS)')
_p('\t$(PRELINKCMDS)')
_p('')
end
function make.postBuildCmds(cfg, toolset)
make.buildCmds(cfg, "postbuild")
end
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)
end
end
function make.shellType()
_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
include("gmakelegacy_cpp.lua")
include("gmakelegacy_csharp.lua")
include("gmakelegacy_makefile.lua")
include("gmakelegacy_utility.lua")
include("gmakelegacy_workspace.lua")
return p.modules.gmakelegacy