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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
--!A cross-platform build utility based on Lua
--
-- Licensed 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-present, Xmake Open Source Community.
--
-- @author ruki
-- @file build_ninja.lua
--
-- imports
import("core.project.config")
import("core.project.project")
import("core.platform.platform")
import("core.language.language")
import("core.tool.linker")
import("core.tool.compiler")
import("lib.detect.find_tool")
import("lib.detect.find_toolname")
import("core.tools.cl.parse_include")
import("plugins.project.utils.target_cmds", {rootdir = os.programdir()})
import("private.utils.target", {alias = "target_utils"})
-- this sourcebatch is built?
function _sourcebatch_is_built(sourcebatch)
-- we can only use rulename to filter them because sourcekind may be bound to multiple rules
local rulename = sourcebatch.rulename
if rulename == "c.build" or rulename == "c++.build"
or rulename == "asm.build" or rulename == "cuda.build"
or rulename == "objc.build" or rulename == "objc++.build" then
return true
end
end
-- escape path
function _escape_path(filepath)
if is_host("windows") then
filepath = filepath:gsub('\\', '/')
end
return filepath
end
-- translate path
function _translate_path(filepath, outputdir)
filepath = path.translate(filepath)
if filepath == "" then
return ""
end
if path.is_absolute(filepath) then
if filepath:startswith(project.directory()) then
return path.relative(filepath, outputdir)
end
return filepath
else
return path.relative(path.absolute(filepath), outputdir)
end
end
-- get relative unix path
function _get_relative_unix_path(filepath, outputdir)
filepath = _translate_path(filepath, outputdir)
filepath = _escape_path(path.translate(filepath))
return os.args(filepath)
end
-- translate compiler flags
function _translate_compflags(compflags, outputdir)
local flags = {}
local last_flag = nil;
for _, flag in ipairs(compflags) do
local flag = flag
if flag == "-I" or flag == "-isystem" then
last_flag = flag
else
if last_flag == "-I" or last_flag == "-isystem" then
table.insert(flags, last_flag)
flag = _get_relative_unix_path(flag, outputdir)
else
last_flag = flag
for _, pattern in ipairs({"[%-](I)(.*)", "[%-](isystem)(.*)"}) do
flag = flag:gsub(pattern, function (flag, dir)
dir = _get_relative_unix_path(dir, outputdir)
return "-" .. flag .. dir
end)
end
end
table.insert(flags, flag)
end
end
return flags
end
-- translate linker flags
function _translate_linkflags(linkflags, outputdir)
local flags = {}
for _, flag in ipairs(linkflags) do
local flag = flag
for _, pattern in ipairs({"[%-](L)(.*)", "[%-](F)(.*)"}) do
flag = flag:gsub(pattern, function (flag, dir)
dir = _get_relative_unix_path(dir, outputdir)
return "-" .. flag .. dir
end)
end
table.insert(flags, flag)
end
return flags
end
-- add header
function _add_header(ninjafile)
ninjafile:print([[# this is the build file for project %s
# it is autogenerated by the xmake build system.
# do not edit by hand.
]], project.name() or "")
ninjafile:print("ninja_required_version = 1.5.1")
ninjafile:print("")
end
-- add rules for generator
function _add_rules_for_generator(ninjafile, outputdir)
local projectdir = _get_relative_unix_path(os.projectdir(), outputdir)
ninjafile:print("rule gen")
ninjafile:print(" command = xmake project -P %s -k ninja", projectdir)
ninjafile:print(" description = regenerating ninja files")
ninjafile:print("")
end
-- add rules for complier (gcc)
function _add_rules_for_compiler_gcc(ninjafile, sourcekind, program)
local ccache = config.get("ccache") ~= false and find_tool("ccache")
ninjafile:print("rule %s", sourcekind)
ninjafile:print(" command = %s%s $ARGS -MMD -MF $out.d -o $out -c $in", ccache and (ccache.program .. " ") or "", program)
ninjafile:print(" deps = gcc")
ninjafile:print(" depfile = $out.d")
ninjafile:print(" description = %scompiling.%s $in", ccache and "ccache " or "", config.mode())
ninjafile:print("")
end
-- add rules for complier (clang)
function _add_rules_for_compiler_clang(ninjafile, sourcekind, program)
return _add_rules_for_compiler_gcc(ninjafile, sourcekind, program)
end
-- add rules for complier (clang-cl)
function _add_rules_for_compiler_clang_cl(ninjafile, sourcekind, program)
ninjafile:print("rule %s", sourcekind)
ninjafile:print(" command = %s -showIncludes -c $ARGS $in -Fo$out", program)
ninjafile:print(" deps = msvc")
ninjafile:print(" description = compiling.%s $in", config.mode())
ninjafile:print("")
end
-- add rules for complier (msvc/cl)
function _add_rules_for_compiler_msvc_cl(ninjafile, sourcekind, program)
ninjafile:print("rule %s", sourcekind)
ninjafile:print(" command = %s -showIncludes -c $ARGS $in -Fo$out", program)
ninjafile:print(" deps = msvc")
ninjafile:print(" description = compiling.%s $in", config.mode())
ninjafile:print("")
end
-- add rules for complier (msvc/ml)
function _add_rules_for_compiler_msvc_ml(ninjafile, sourcekind, program)
ninjafile:print("rule %s", sourcekind)
ninjafile:print(" command = %s -c $ARGS -Fo$out $in", program)
ninjafile:print(" deps = msvc")
ninjafile:print(" description = compiling.%s $in", config.mode())
ninjafile:print("")
end
-- add rules for resource complier (msvc/rc)
function _add_rules_for_compiler_msvc_rc(ninjafile, sourcekind, program)
ninjafile:print("rule %s", sourcekind)
ninjafile:print(" command = %s $ARGS -Fo$out $in", program)
ninjafile:print(" deps = msvc")
ninjafile:print(" description = compiling.%s $in", config.mode())
ninjafile:print("")
end
-- add rules for resource complier (windres/rc)
function _add_rules_for_compiler_windres(ninjafile, sourcekind, program)
ninjafile:print("rule %s", sourcekind)
ninjafile:print(" command = %s $ARGS $in $out", program)
ninjafile:print(" description = compiling.%s $in", config.mode())
ninjafile:print("")
end
-- add rules for complier (cuda/nvcc)
function _add_rules_for_compiler_nvcc(ninjafile, sourcekind, program)
ninjafile:print("rule %s", sourcekind)
ninjafile:print(" command = %s -c $ARGS $in -o $out", program)
ninjafile:print(" description = compiling.%s $in", config.mode())
ninjafile:print("")
end
-- add rules for complier
function _add_rules_for_compiler(ninjafile)
ninjafile:print("# rules for compiler")
if is_plat("windows") then
-- @see https://github.com/ninja-build/ninja/issues/613
local note_include = parse_include.probe_include_note_from_cl()
if not note_include then
note_include = "Note: including file:"
end
ninjafile:print("msvc_deps_prefix = %s", note_include:trim())
end
local add_compiler_rules =
{
gcc = _add_rules_for_compiler_gcc,
gxx = _add_rules_for_compiler_gcc,
clang = _add_rules_for_compiler_clang,
clangxx = _add_rules_for_compiler_clang,
cl = _add_rules_for_compiler_msvc_cl,
clang_cl = _add_rules_for_compiler_clang_cl,
ml = _add_rules_for_compiler_msvc_ml,
ml64 = _add_rules_for_compiler_msvc_ml,
rc = _add_rules_for_compiler_msvc_rc,
windres = _add_rules_for_compiler_windres,
nvcc = _add_rules_for_compiler_nvcc
}
for sourcekind, _ in pairs(language.sourcekinds()) do
local program, toolname = platform.tool(sourcekind)
if program then
local add_rule = add_compiler_rules[toolname or find_toolname(program)]
-- support of unknown compiler (xmake f --cc=gcc@my-cc)
if not add_rule and toolname then
add_rule = add_compiler_rules[find_toolname(toolname)]
end
if add_rule then
add_rule(ninjafile, sourcekind, program)
end
end
end
ninjafile:print("")
end
-- add rules for linker (ar)
function _add_rules_for_linker_ar(ninjafile, linkerkind, program)
ninjafile:print("rule %s", linkerkind)
ninjafile:print(" command = %s $ARGS $out $in", program)
ninjafile:print(" description = archiving.%s $out", config.mode())
ninjafile:print("")
end
-- add rules for linker (gcc)
function _add_rules_for_linker_gcc(ninjafile, linkerkind, program)
ninjafile:print("rule %s", linkerkind)
ninjafile:print(" command = %s -o $out $in $ARGS", program)
ninjafile:print(" description = linking.%s $out", config.mode())
ninjafile:print("")
end
-- add rules for linker (clang)
function _add_rules_for_linker_clang(ninjafile, linkerkind, program)
return _add_rules_for_linker_gcc(ninjafile, linkerkind, program)
end
-- add rules for linker (msvc)
function _add_rules_for_linker_msvc(ninjafile, linkerkind, program)
if linkerkind == "ar" then
program = program .. " -lib"
elseif linkerkind == "sh" then
program = program .. " -dll"
end
-- @note we use rspfile to handle long command limit on windows
ninjafile:print("rule %s", linkerkind)
ninjafile:print(" command = %s @$out.rsp", program)
ninjafile:print(" rspfile = $out.rsp")
ninjafile:print(" rspfile_content = $ARGS -out:$out $in_newline")
ninjafile:print(" description = linking.%s $out", config.mode())
ninjafile:print("")
end
-- add rules for linker
function _add_rules_for_linker(ninjafile)
ninjafile:print("# rules for linker")
local linkerkinds = {}
for _, _linkerkinds in pairs(language.targetkinds()) do
table.join2(linkerkinds, _linkerkinds)
end
local add_linker_rules =
{
ar = _add_rules_for_linker_ar,
gcc = _add_rules_for_linker_gcc,
gxx = _add_rules_for_linker_gcc,
clang = _add_rules_for_linker_clang,
clangxx = _add_rules_for_linker_clang,
link = _add_rules_for_linker_msvc
}
for _, linkerkind in ipairs(table.unique(linkerkinds)) do
local program, toolname = platform.tool(linkerkind)
if program then
local add_rule = add_linker_rules[toolname or find_toolname(program)]
-- support of unknown linker (xmake f --ld=gcc@my-ld)
if not add_rule and toolname then
add_rule = add_linker_rules[find_toolname(toolname)]
end
if add_rule then
add_rule(ninjafile, linkerkind, program)
end
end
end
ninjafile:print("")
end
-- add rules
function _add_rules(ninjafile, outputdir)
-- add rules for generator
_add_rules_for_generator(ninjafile, outputdir)
-- add rules for complier
_add_rules_for_compiler(ninjafile)
-- add rules for linker
_add_rules_for_linker(ninjafile)
end
-- add build rule for phony
function _add_build_for_phony(ninjafile, target)
ninjafile:print("build %s: phony", target:name())
end
-- add build rule for object
function _add_build_for_object(ninjafile, target, sourcekind, sourcefile, objectfile, outputdir)
objectfile = _get_relative_unix_path(objectfile, outputdir)
sourcefile = _get_relative_unix_path(sourcefile, outputdir)
local compflags = compiler.compflags(sourcefile, {target = target})
ninjafile:print("build %s: %s %s", objectfile, sourcekind, sourcefile)
ninjafile:print(" ARGS = %s", os.args(_translate_compflags(compflags, outputdir)))
ninjafile:print("")
end
-- add build rule for objects
function _add_build_for_objects(ninjafile, target, sourcebatch, outputdir)
for index, objectfile in ipairs(sourcebatch.objectfiles) do
_add_build_for_object(ninjafile, target, sourcebatch.sourcekind, sourcebatch.sourcefiles[index], objectfile, outputdir)
end
end
-- add build rule for target
function _add_build_for_target(ninjafile, target, outputdir)
-- https://github.com/xmake-io/xmake/issues/2337
target:data_set("plugin.project.kind", "ninja")
-- is phony target?
if target:is_phony() or target:is_headeronly() then
return _add_build_for_phony(ninjafile, target)
end
-- build target
ninjafile:print("# build target: %s", target:name())
local targetfile = _get_relative_unix_path(target:targetfile(), outputdir)
ninjafile:print("build %s: phony %s", target:name(), targetfile)
-- build target file
ninjafile:printf("build %s: %s", targetfile, target:linker():kind())
local objectfiles = target:objectfiles()
for _, objectfile in ipairs(objectfiles) do
ninjafile:write(" " .. _get_relative_unix_path(objectfile, outputdir))
end
-- merge objects with rule("utils.merge.object")
for _, sourcebatch in pairs(target:sourcebatches()) do
if sourcebatch.rulename == "utils.merge.object" then
ninjafile:write(" " .. table.concat(sourcebatch.sourcefiles, " "))
end
end
local deps = target:get("deps")
if deps then
ninjafile:print(" || $")
ninjafile:write(" ")
for _, dep in ipairs(deps) do
local dep_target = project.target(dep, {namespace = target:namespace()});
if not dep_target:is_headeronly() then
ninjafile:write(" " .. _get_relative_unix_path(dep_target:targetfile(), outputdir))
end
end
end
ninjafile:print("")
ninjafile:print(" ARGS = %s", os.args(_translate_linkflags(target:linkflags(), outputdir)))
ninjafile:print("")
-- build target objects
local sourcebatches = target:sourcebatches()
for _, sourcebatch in table.orderpairs(sourcebatches) do
if _sourcebatch_is_built(sourcebatch) then
_add_build_for_objects(ninjafile, target, sourcebatch, outputdir)
end
end
end
-- add build rule for generator
function _add_build_for_generator(ninjafile, outputdir)
ninjafile:print("# build build.ninja")
ninjafile:print("build build.ninja: gen $")
local allfiles = project.allfiles()
for idx, projectfile in ipairs(allfiles) do
if not path.is_absolute(projectfile) or projectfile:startswith(os.projectdir()) then
local filepath = _get_relative_unix_path(projectfile, outputdir)
ninjafile:print(" %s %s", filepath, idx < #allfiles and "$" or "")
end
end
ninjafile:print("")
end
-- add build rule for targets
function _add_build_for_targets(ninjafile, outputdir)
-- begin
ninjafile:print("# build targets\n")
-- add build rule for generator
_add_build_for_generator(ninjafile, outputdir)
local project_targets = target_utils.get_project_targets()
-- TODO
-- disable precompiled header first
for _, target in pairs(project_targets) do
target:set("pcheader", nil)
target:set("pcxxheader", nil)
end
-- build targets
for _, target in pairs(project_targets) do
_add_build_for_target(ninjafile, target, outputdir)
end
-- build default
local default = ""
for targetname, target in pairs(project_targets) do
if target:is_default() then
default = default .. " " .. targetname
end
end
ninjafile:print("build default: phony%s", default)
-- build all
local all = ""
for targetname, _ in pairs(project_targets) do
all = all .. " " .. targetname
end
ninjafile:print("build all: phony%s\n", all)
-- end
ninjafile:print("default default\n")
end
function make(outputdir)
local oldir = os.cd(os.projectdir())
-- prepare targets
target_cmds.prepare_targets()
-- open the build.ninja file
--
-- we need to change encoding to support msvc_deps_prefix
-- @see https://github.com/ninja-build/ninja/issues/613
--
-- TODO maybe we need support more encoding for other languages
--
local encoding = is_subhost("windows") and "gbk"
local ninjafile = io.open(path.join(outputdir, "build.ninja"), "w", {encoding = encoding})
-- add header
_add_header(ninjafile)
-- add rules
_add_rules(ninjafile, outputdir)
-- add build rules for targets
_add_build_for_targets(ninjafile, outputdir)
-- close the ninjafile
ninjafile:close()
os.cd(oldir)
end
|