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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
--!The Automatic Crmakefiles-platform Build Tool
--
-- XMake is free software; you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 2.1 of the License, or
-- (at your option) any later version.
--
-- XMake is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public License
-- along with XMake;
-- If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
--
-- Copyright (C) 2009 - 2015, ruki All rights reserved.
--
-- @author ruki
-- @file makefile.lua
--
-- define module: makefile
local makefile = makefile or {}
-- load modules
local io = require("base/io")
local os = require("base/os")
local path = require("base/path")
local utils = require("base/utils")
local config = require("base/config")
local project = require("base/project")
local platform = require("platform/platform")
-- get the linker from the given kind
function makefile._linker(kind)
-- init linkers
makefile._LINKERS = makefile._LINKERS or {}
local linkers = makefile._LINKERS
-- return it directly if the linker has been cached
local l = linkers[kind]
if l then return l end
-- get compiler from the current platform
l = platform.linker(kind)
-- cache this compiler
linkers[kind] = l
-- ok
return l
end
-- get the compiler from the given source file
function makefile._compiler(srcfile)
-- get the source file type
local filetype = path.extension(srcfile)
if not filetype then
return nil, string.format("cannot get the source file type: %s", srcfile)
end
-- get the lower file type
filetype = filetype:lower()
-- get the compiler name
local name = nil
if filetype == ".c" then name = "cc"
elseif filetype == ".cpp" or filetype == ".cc" then name = "cxx"
elseif filetype == ".m" then name = "mm"
elseif filetype == ".mm" then name = "mxx"
elseif filetype == ".s" or filetype == ".asm" then name = "as"
else return nil, string.format("unknown file type: %s", filetype)
end
-- init compilers
makefile._COMPILERS = makefile._COMPILERS or {}
local compilers = makefile._COMPILERS
-- return it directly if the compiler has been cached
local c = compilers[name]
if c then return c end
-- get compiler from the current platform
c = platform.compiler(name)
-- cache this compiler
compilers[name] = c
-- ok
return c
end
-- get the source files from the given target
function makefile._srcfiles(target)
-- check
assert(target)
-- no files?
if not target.files then
return {}
end
-- wrap files first
local target_files = utils.wrap(target.files)
-- match files
local i = 1
local srcfiles = {}
for _, target_file in ipairs(target_files) do
-- match source files
local files = os.match(target_file)
-- process source files
for _, file in ipairs(files) do
-- convert to the relative path
if path.is_absolute(file) then
file = path.relative(file, xmake._PROJECT_DIR)
end
-- save it
srcfiles[i] = file
i = i + 1
end
end
-- remove repeat files
srcfiles = utils.unique(srcfiles)
-- ok?
return srcfiles
end
-- get object files for the given source files
function makefile._objfiles(name, srcfiles)
-- check
assert(name and srcfiles)
-- the build directory
local buildir = config.get("buildir")
assert(buildir)
-- make object files
local i = 1
local objfiles = {}
for _, srcfile in ipairs(srcfiles) do
-- make object file
local objfile = string.format("%s/%s/%s/%s", buildir, name, path.directory(srcfile), platform.filename(path.basename(srcfile), "object"))
-- save it
objfiles[i] = path.translate(objfile)
i = i + 1
end
-- ok?
return objfiles
end
-- get target file for the given target name and kind
function makefile._targetfile(name, kind)
-- check
assert(name and kind)
-- the build directory
local buildir = config.get("buildir")
assert(buildir)
-- the target file name
local filename = platform.filename(name, kind)
assert(filename)
-- make the target file path
return buildir .. "/" .. name .. "/" .. filename
end
-- make the object to the makefile
function makefile._make_object(file, target, srcfile, objfile)
-- check
assert(file and target and srcfile and objfile)
-- get the compiler
local c, errors = makefile._compiler(srcfile);
if not c then
-- error
utils.error(errors)
return false
end
-- make head
file:write(string.format("%s:", objfile))
-- make dependence
file:write(string.format(" %s\n", srcfile))
-- make body
file:write(string.format("\t@echo [%s]: compiling %s\n", config.get("mode"), srcfile))
file:write(string.format("\t@xmake l mkdir %s\n", path.directory(objfile)))
file:write(string.format("\t@%s\n", c:make(srcfile, objfile, "")))
-- make tail
file:write("\n")
-- ok
return true
end
-- make all objects of the given target to the makefile
function makefile._make_objects(file, target, srcfiles, objfiles)
-- check
assert(file and target and srcfiles and objfiles)
-- make all objects
local i = 1
for _, objfile in ipairs(objfiles) do
-- make object
if not makefile._make_object(file, target, srcfiles[i], objfile) then
return false
end
-- next
i = i + 1
end
-- ok
return true
end
-- make the given target to the makefile
function makefile._make_target(file, name, target)
-- check
assert(file and name and target and target.kind)
-- get source and object files
local srcfiles = makefile._srcfiles(target)
local objfiles = makefile._objfiles(name, srcfiles)
assert(srcfiles and objfiles)
-- get target file
local targetfile = makefile._targetfile(name, target.kind)
assert(targetfile)
-- the linker
local l = makefile._linker(target.kind)
assert(l)
-- the make command
local make_command = l["make_" .. target.kind]
if not make_command then
-- error
utils.error("the target kind: %s in not surpported now!", target.kind)
return false
end
-- make head
file:write(string.format("%s:", name))
-- make dependence for the dependent targets
if target.deps then
local deps = utils.wrap(target.deps)
for _, dep in ipairs(deps) do
file:write(" " .. dep)
end
end
-- make dependence for objects
for _, objfile in ipairs(objfiles) do
file:write(" " .. objfile)
end
-- make dependence end
file:write("\n")
-- make body
file:write(string.format("\t@echo [%s]: linking %s\n", config.get("mode"), path.filename(targetfile)))
file:write(string.format("\t@xmake l mkdir %s\n", path.directory(targetfile)))
file:write(string.format("\t@%s\n", make_command(l, objfiles, targetfile, "")))
-- make tail
file:write("\n")
-- make objects for this target
return makefile._make_objects(file, target, srcfiles, objfiles)
end
-- make all targets to the makefile
function makefile._make_targets(file)
-- check
assert(file)
-- get all project targets
local targets = project.targets()
if not targets then
-- error
utils.error("not found target in this project!")
return false
end
-- make all first
local all = ""
for name, _ in pairs(targets) do
-- append the target name to all
all = all .. " " .. name
end
file:write(string.format("all: %s\n\n", all))
-- make it for all targets
for name, target in pairs(targets) do
-- make target
if not makefile._make_target(file, name, target) then
-- error
utils.error("failed to make target %s to makefile!", name)
return false
end
-- append the target name to all
all = all .. " " .. name
end
-- ok
return true
end
-- make makefile in the build directory
function makefile.make()
-- get the build directory
local buildir = config.get("buildir")
assert(buildir)
-- make the build directory
if not os.isdir(buildir) then
assert(os.mkdir(buildir))
end
-- open the makefile
local path = buildir .. "/makefile"
local file = io.open(path, "w")
if not file then
-- error
utils.error("open %s failed!", path)
return false
end
-- make all targets to the makefile
if not makefile._make_targets(file) then
-- error
utils.error("save %s failed!", path)
-- close the makefile
file:close()
-- remove the makefile
os.rm(path)
-- failed
return false
end
-- close the makefile
file:close()
-- ok
return true
end
-- build target
function makefile.build(target)
-- check
assert(target and type(target) == "string")
-- get the build directory
local buildir = config.get("buildir")
assert(buildir)
-- done build
local ok = os.execute(string.format("make -f %s %s", buildir .. "/makefile", target))
if ok ~= 0 then
-- error
utils.error("build target: %s failed!", target)
return false
end
-- ok
return true
end
-- return module: makefile
return makefile
|