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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
--!The Make-like Build Utility based on Lua
--
-- 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) 2015 - 2016, ruki All rights reserved.
--
-- @author ruki
-- @file target.lua
--
-- define module
local target = target or {}
-- load modules
local os = require("base/os")
local path = require("base/path")
local utils = require("base/utils")
local table = require("base/table")
local option = require("project/option")
local config = require("project/config")
local linker = require("tool/linker")
local compiler = require("tool/compiler")
local platform = require("platform/platform")
-- get the filename from the given name and kind
function target.filename(name, kind)
-- check
assert(name and kind)
-- get format
local format = platform.format(kind) or {"", ""}
-- make it
return format[1] .. name .. format[2]
end
-- get the target info
function target:get(infoname)
-- check
assert(self and self._INFO and infoname)
-- get it
return self._INFO[infoname]
end
-- get the target name
function target:name()
-- get it
return self._NAME
end
-- get the options
function target:options()
-- the options
local options = {}
for _, name in ipairs(table.wrap(self:get("options"))) do
-- get option if be enabled
local opt = nil
if config.get(name) then opt = option.load(name) end
if nil ~= opt then
options[name] = opt
end
end
-- ok?
return options
end
-- get the object file directory
function target:objectdir()
-- check
assert(self)
-- the object directory
local objectdir = self:get("objectdir")
if not objectdir then
-- make the default object directory
objectdir = path.join(config.get("buildir"), ".objs")
end
-- ok?
return objectdir
end
-- get the target file
function target:targetfile()
-- check
assert(self)
-- the target directory
local targetdir = self:get("targetdir") or config.get("buildir")
assert(targetdir and type(targetdir) == "string")
-- the target file name
local filename = target.filename(self:name(), self:get("kind"))
assert(filename)
-- make the target file path
return path.join(targetdir, filename)
end
-- get the config header files
function target:configheader(outputdir)
-- get config header
local configheader = self:get("config_h")
if not configheader then
return
end
-- get the root directory
local rootdir, count = configheader:gsub("|.*$", ""):gsub("%(.*%)$", "")
if count == 0 then
rootdir = nil
end
-- remove '(' and ')'
configheader = configheader:gsub("[%(%)]", "")
-- get the output header
local outputheader = nil
if outputdir then
if rootdir then
outputheader = path.absolute(path.relative(configheader, rootdir), outputdir)
else
outputheader = path.join(outputdir, path.filename(configheader))
end
end
-- ok
return configheader, outputheader
end
-- get the source files
function target:sourcefiles()
-- check
assert(self)
-- cached? return it directly
if self._SOURCEFILES then
return self._SOURCEFILES, false
end
-- get files
local files = self:get("files")
-- no files?
if not files then
return {}, false
end
-- the patterns
local patterns =
{
{"([%w%*]+)%.obj|", "%1|", "object"}
, {"([%w%*]+)%.obj$", "%1", "object"}
, {"([%w%*]+)%.o|", "%1|", "object"}
, {"([%w%*]+)%.o$", "%1", "object"}
, {"([%w%*]+)%.lib|", "%1|", "static"}
, {"([%w%*]+)%.lib$", "%1", "static"}
, {"lib([%w%*]+)%.a|", "%1|", "static"}
, {"lib([%w%*]+)%.a$", "%1", "static"}
}
-- match files
local i = 1
local count = 0
local cache = true
local sourcefiles = {}
for _, file in ipairs(table.wrap(files)) do
-- normalize *.[o|obj] and [lib]*.[a|lib] filename
for _, pattern in ipairs(patterns) do
file, count = file:gsub(pattern[1], target.filename(pattern[2], pattern[3]))
if count > 0 then
-- disable cache because the object and library files will be modified.
cache = false
end
end
-- match source files
local srcfiles = os.match(file)
if #srcfiles == 0 then
utils.warning("cannot match add_files(\"%s\")", file)
end
-- process source files
for _, srcfile in ipairs(srcfiles) do
-- convert to the relative path
if path.is_absolute(srcfile) then
srcfile = path.relative(srcfile, xmake._PROJECT_DIR)
end
-- save it
sourcefiles[i] = srcfile
i = i + 1
end
end
-- remove repeat files
sourcefiles = table.unique(sourcefiles)
-- cache it
if cache then
self._SOURCEFILES = sourcefiles
end
-- ok? modified?
return sourcefiles, not cache
end
-- get the object files
function target:objectfiles()
-- check
assert(self)
-- get the source files
local sourcefiles, modified = self:sourcefiles()
assert(sourcefiles)
-- cached? return it directly
if self._OBJECTFILES and not modified then
return self._OBJECTFILES
end
-- get the object directory
local objectdir = self:objectdir()
assert(objectdir and type(objectdir) == "string")
-- make object files
local i = 1
local objectfiles = {}
for _, sourcefile in ipairs(sourcefiles) do
-- translate: [lib]xxx*.[a|lib] => xxx/*.[o|obj] object file
sourcefile = sourcefile:gsub(target.filename("([%w_]+)", "static"):gsub("%.", "%%.") .. "$", "%1/*")
-- make object file
local objectfile = string.format("%s/%s/%s/%s", objectdir, self:name(), path.directory(sourcefile), target.filename(path.basename(sourcefile), "object"))
-- translate path
--
-- .e.g
--
-- src/xxx.c
-- project/xmake.lua
-- build/.objs
--
-- objectfile: project/build/.objs/xxxx/../../xxx.c will be out of range for objectdir
--
-- we need replace '..' to '__' in this case
--
objectfile = (path.translate(objectfile):gsub("%.%.", "__"))
-- save it
objectfiles[i] = objectfile
i = i + 1
end
-- cache it
self._OBJECTFILES = objectfiles
-- ok?
return objectfiles
end
-- get the header files
function target:headerfiles(outputdir)
-- check
assert(self)
-- cached? return it directly
if self._HEADERFILES and outputdir == nil then
return self._HEADERFILES[1], self._HEADERFILES[2]
end
-- no headers?
local headers = self:get("headers")
if not headers then return end
-- get the headerdir
local headerdir = outputdir or self:get("headerdir") or config.get("buildir")
assert(headerdir)
-- get the source pathes and destinate pathes
local srcheaders = {}
local dstheaders = {}
for _, header in ipairs(table.wrap(headers)) do
-- get the root directory
local rootdir, count = header:gsub("|.*$", ""):gsub("%(.*%)$", "")
if count == 0 then
rootdir = nil
end
-- remove '(' and ')'
local srcpathes = header:gsub("[%(%)]", "")
if srcpathes then
-- get the source pathes
srcpathes = os.match(srcpathes)
if srcpathes then
-- add the source headers
table.join2(srcheaders, srcpathes)
-- add the destinate headers
for _, srcpath in ipairs(srcpathes) do
-- the destinate header
local dstheader = nil
if rootdir then
dstheader = path.absolute(path.relative(srcpath, rootdir), headerdir)
else
dstheader = path.join(headerdir, path.filename(srcpath))
end
assert(dstheader)
-- add it
table.insert(dstheaders, dstheader)
end
end
end
end
-- cache it
if outputdir == nil then
self._HEADERFILES = {srcheaders, dstheaders}
end
-- ok?
return srcheaders, dstheaders
end
-- get the dependent include files
function target:incdepfiles()
-- the previous object files
local objectfiles_prev = self._OBJECTFILES
-- the object files
local objectfiles = self:objectfiles()
-- cached? return it directly
local modified = objectfiles_prev ~= objectfiles
if self._INCDEPFILES and not modified then
return self._INCDEPFILES
end
-- make include files
local incdepfiles = {}
for _, objectfile in ipairs(objectfiles) do
table.insert(incdepfiles, path.join(path.directory(objectfile), path.basename(objectfile) .. ".d"))
end
-- cache it
self._INCDEPFILES = incdepfiles
-- ok
return incdepfiles
end
-- get the kinds of sourcefiles
--
-- .e.g cc cxx mm mxx as ...
--
function target:sourcekinds()
-- cached? return it directly
if self._SOURCEKINDS then
return self._SOURCEKINDS
end
-- make kinds
local kinds = {}
for _, sourcefile in pairs(self:sourcefiles()) do
-- get kind
local kind = compiler.kind_of_file(sourcefile)
if kind then
table.insert(kinds, kind)
end
end
-- remove repeat
kinds = table.unique(kinds)
-- cache it
self._SOURCEKINDS = kinds
-- ok?
return kinds
end
-- return module
return target
|