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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
--!The Make-like Build Utility based on Lua
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015 - 2017, TBOOX Open Source Group.
--
-- @author ruki
-- @file makefile.lua
--
-- imports
import("core.tool.tool")
import("core.tool.compiler")
import("core.project.config")
import("core.project.project")
import("core.language.language")
-- get log makefile
function _logfile()
-- get it
return vformat("$(buildir)/.build.log")
end
-- mkdir directory
function _mkdir(makefile, dir)
if config.get("plat") == "windows" then
makefile:print("\t-@mkdir %s > /null 2>&1", dir)
else
makefile:print("\t@mkdir -p %s", dir)
end
end
-- copy file
function _cp(makefile, sourcefile, targetfile)
-- copy file
if config.get("plat") == "windows" then
makefile:print("\t@copy /Y %s %s > /null 2>&1", sourcefile, targetfile)
else
makefile:print("\t@cp %s %s", sourcefile, targetfile)
end
end
-- make the object for the *.[o|obj] source file
function _make_object_for_object(makefile, target, sourcefile, objectfile)
-- make command
local cmd = format("xmake l cp %s %s", sourcefile, objectfile)
-- make head
makefile:printf("%s:", objectfile)
-- make dependence
makefile:print(" %s", sourcefile)
-- make body
makefile:print("\t@echo inserting.$(mode) %s", sourcefile)
makefile:print("\t@%s", cmd)
-- make tail
makefile:print("")
end
-- make the object for the *.[a|lib] source file
function _make_object_for_static(makefile, target, sourcefile, objectfile)
-- not supported
raise("source file: %s not supported!", sourcefile)
end
-- make the object
function _make_object(makefile, target, sourcefile, objectfile)
-- get the source file kind
local sourcekind = language.sourcekind_of(sourcefile)
-- make the object for the *.o/obj source makefile
if sourcekind == "obj" then
return _make_object_for_object(makefile, target, sourcefile, objectfile)
-- make the object for the *.[a|lib] source file
elseif sourcekind == "lib" then
return _make_object_for_static(makefile, target, sourcefile, objectfile)
end
-- get shellname name
local shellname = tool.shellname(sourcekind)
-- get complier flags
local compflags = compiler.compflags(sourcefile, target, sourcekind)
-- make command
local command = compiler.compcmd(sourcefile, objectfile, target)
-- replace compflags to $(XX)
local p, e = command:find(compflags, 1, true)
if p then
command = format("%s$(%s_%s)%s", command:sub(1, p - 1), target:name(), sourcekind:upper(), command:sub(e + 1))
end
-- replace shellname to $(XX)
p, e = command:find(shellname, 1, true)
if p then
command = format("%s$(%s)%s", command:sub(1, p - 1), sourcekind:upper(), command:sub(e + 1))
end
-- make head
makefile:printf("%s:", objectfile)
-- make dependence
makefile:print(" %s", sourcefile)
-- make body
makefile:print("\t@echo %scompiling.$(mode) %s", ifelse(config.get("ccache"), "ccache ", ""), sourcefile)
_mkdir(makefile, path.directory(objectfile))
makefile:writef("\t@%s > %s 2>&1\n", command, _logfile())
-- make tail
makefile:print("")
end
-- make each objects
function _make_each_objects(makefile, target, sourcekind, sourcebatch)
-- make them
for index, objectfile in ipairs(sourcebatch.objectfiles) do
_make_object(makefile, target, sourcebatch.sourcefiles[index], objectfile)
end
end
-- make single object
function _make_single_object(makefile, target, sourcekind, sourcebatch)
-- get source and object files
local sourcefiles = sourcebatch.sourcefiles
local objectfiles = sourcebatch.objectfiles
local incdepfiles = sourcebatch.incdepfiles
-- get shellname name
local shellname = tool.shellname(sourcekind)
-- get complier flags
local compflags = compiler.compflags(sourcefiles, target, sourcekind)
-- make command
local command = compiler.compcmd(sourcefiles, objectfiles, target, sourcekind)
-- replace compflags to $(XX)
local p, e = command:find(compflags, 1, true)
if p then
command = format("%s$(%s_%s)%s", command:sub(1, p - 1), target:name(), sourcekind:upper(), command:sub(e + 1))
end
-- replace shellname to $(XX)
p, e = command:find(shellname, 1, true)
if p then
command = format("%s$(%s)%s", command:sub(1, p - 1), sourcekind:upper(), command:sub(e + 1))
end
-- make head
makefile:printf("%s:", objectfiles)
-- make dependence
for _, sourcefile in ipairs(sourcefiles) do
makefile:printf(" %s", sourcefile)
end
makefile:print("")
-- make body
for _, sourcefile in ipairs(sourcefiles) do
makefile:print("\t@echo %scompiling.$(mode) %s", ifelse(config.get("ccache"), "ccache ", ""), sourcefile)
end
_mkdir(makefile, path.directory(objectfiles))
makefile:writef("\t@%s > %s 2>&1\n", command, _logfile())
-- make tail
makefile:print("")
end
-- make target
function _make_target(makefile, target)
-- make head
local targetfile = target:targetfile()
makefile:print("%s: %s", target:name(), targetfile)
makefile:printf("%s:", targetfile)
-- make dependence for the dependent targets
for _, dep in ipairs(target:get("deps")) do
-- add dependence
makefile:write(" " .. project.target(dep):targetfile())
end
-- make dependence for objects
local objectfiles = target:objectfiles()
for _, objectfile in ipairs(objectfiles) do
makefile:write(" " .. objectfile)
end
-- make dependence end
makefile:print("")
-- get linker kind
local linkerkind = target:linker():get("kind")
-- get shellname
local shellname = tool.shellname(linkerkind)
-- get command
local command = target:linkcmd()
-- replace linkflags to $(XX)
local p, e = command:find(target:linkflags(), 1, true)
if p then
command = format("%s$(%s_%s)%s", command:sub(1, p - 1), target:name(), (linkerkind:upper():gsub('%-', '_')), command:sub(e + 1))
end
-- replace shellname to $(XX)
p, e = command:find(shellname, 1, true)
if p then
command = format("%s$(%s)%s", command:sub(1, p - 1), (linkerkind:upper():gsub('%-', '_')), command:sub(e + 1))
end
-- make body
makefile:print("\t@echo linking.$(mode) %s", path.filename(targetfile))
_mkdir(makefile, path.directory(targetfile))
makefile:writef("\t@%s > %s 2>&1\n", command, _logfile())
-- make header directories
local dstheaderdirs = {}
local srcheaders, dstheaders = target:headerfiles()
for _, dstheader in ipairs(dstheaders) do
dstheaderdirs[path.directory(dstheader)] = true
end
for dstheaderdir, _ in pairs(dstheaderdirs) do
_mkdir(makefile, dstheaderdir)
end
-- copy headers
if srcheaders and dstheaders then
local i = 1
for _, srcheader in ipairs(srcheaders) do
local dstheader = dstheaders[i]
if dstheader then
_cp(makefile, srcheader, dstheader)
end
i = i + 1
end
end
-- make tail
makefile:print("")
-- build source batches
for sourcekind, sourcebatch in pairs(target:sourcebatches()) do
-- compile source files to single object at the same time?
if type(sourcebatch.objectfiles) == "string" then
-- make single object
_make_single_object(makefile, target, sourcekind, sourcebatch)
else
-- make each objects
_make_each_objects(makefile, target, sourcekind, sourcebatch)
end
end
end
-- make all
function _make_all(makefile)
-- make variables for source kinds
for sourcekind, _ in pairs(language.sourcekinds()) do
local shellname = tool.shellname(sourcekind)
if shellname and shellname ~= "" then
makefile:print("%s=%s", sourcekind:upper(), shellname)
end
end
makefile:print("")
-- make variables for linker kinds
local linkerkinds = {}
for _, _linkerkinds in pairs(language.targetkinds()) do
table.join2(linkerkinds, _linkerkinds)
end
for _, linkerkind in ipairs(table.unique(linkerkinds)) do
local shellname = tool.shellname(linkerkind)
if shellname and shellname ~= "" then
makefile:print("%s=%s", (linkerkind:upper():gsub('%-', '_')), shellname)
end
end
makefile:print("")
-- make variables for target flags
for targetname, target in pairs(project.targets()) do
for sourcekind, sourcebatch in pairs(target:sourcebatches()) do
makefile:print("%s_%s=%s", targetname, sourcekind:upper(), compiler.compflags(sourcebatch.sourcefiles, target, sourcekind))
end
makefile:print("%s_%s=%s", targetname, target:linker():get("kind"):upper(), target:linkflags())
end
makefile:print("")
-- make all
local all = ""
for targetname, _ in pairs(project.targets()) do
-- append the target name to all
all = all .. " " .. targetname
end
makefile:print("all: %s\n", all)
makefile:print(".PHONY: all %s\n", all)
-- make it for all targets
for _, target in pairs(project.targets()) do
-- make target
_make_target(makefile, target)
-- append the target name to all
all = all .. " " .. target:name()
end
end
-- make
function make(outputdir)
-- enter project directory
local olddir = os.cd(project.directory())
-- remove the log makefile first
os.rm(_logfile())
-- open the makefile
local makefile = io.open(path.join(outputdir, "makefile"), "w")
-- make all
_make_all(makefile)
-- close the makefile
makefile:close()
-- leave project directory
os.cd(olddir)
end
|