-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.lua
226 lines (186 loc) · 6.69 KB
/
build.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
section [[
Dummy project showing bang capabilities
and many other features enabled by LuaX.
The src directory contains one C source per executable (and per architecture).
The lib directory is a library of sources common to all architectures.
The arch directory contains one sub directory par architecture
and header files defining a common API to all architectures.
]]
local F = require "F"
local fs = require "fs"
var "builddir" ".build"
var "ex0" "$builddir/ex0"
var "ex1" "$builddir/ex1"
local cflags = F{
"-O3",
"-Wall",
"-Werror",
"-Ilib", "-Iarch",
}
local ldflags = F{
}
local validation = true
local clang_tidy_checks = F{
"--checks=*",
"-llvmlibc-restrict-system-libc-headers",
"-llvm-header-guard",
"-modernize-macro-to-enum",
"-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling",
"-altera-id-dependent-backward-branch",
"-altera-unroll-loops",
"-readability-identifier-length",
"-cppcoreguidelines-macro-to-enum",
}:str","
file "compile_flags.txt" (cflags:unlines())
section "Common compilation options"
var "cflags" (cflags)
var "ldflags" (ldflags)
rule "clang-tidy" {
command = {
"clang-tidy",
"--quiet",
"--use-color",
"--warnings-as-errors=*",
"-header-filter=.*",
clang_tidy_checks,
"$in",
"&> $out",
"|| (cat $out && false)",
}
}
section "Example 0: compilation with low level ninja primitives"
-- cc/ar/ld rules for each architecture
local cc = {}
local ar = {}
local ld = {}
local architectures = ls "arch"
: filter(fs.is_dir)
: map(function(path)
local arch_name = path:basename()
section(arch_name)
local arch = require(path / "config")
arch.name = arch_name
cc[arch_name] = rule("cc_"..arch_name) {
description = "["..arch_name.."] CC $out",
command = {
arch.cc, "-c",
"$cflags", var("cflags_"..arch_name)(arch.cflags),
"-MD -MF $depfile",
"$in -o $out",
},
depfile = "$out.d",
}
ar[arch_name] = rule("ar_"..arch_name) {
description = "["..arch_name.."] AR $out",
command = {arch.ar, "-crs", "$out $in"},
}
ld[arch_name] = rule("ld_"..arch_name) {
description = "["..arch_name.."] LD $out",
command = {
arch.ld,
"$ldflags", var("ldflags_"..arch_name)(arch.ldflags),
"-o $out $in",
},
}
arch.archive_file = "$ex0" / "arch" / arch_name / "arch.a"
build(arch.archive_file) { ar[arch_name],
ls(path/"**.c")
: map(function(source)
return build("$ex0" / source:chext".o") {
cc[arch_name], source,
validations = validation and {
build("$ex0/clang-tidy"/source..".check") { "clang-tidy", source },
},
}
end)
}
return arch
end)
architectures : foreach(function(arch)
arch.libraries = ls "lib"
: filter(fs.is_dir)
: map(function(path)
local lib_name = path:basename()
section(lib_name.." for "..arch.name)
return build("$ex0" / arch.name / "lib" / lib_name / lib_name..".a") {
ar[arch.name],
ls(path/"**.c")
: map(function(source)
return build("$ex0" / arch.name / source:chext".o") {
cc[arch.name], source,
validations = validation and {
build("$ex0/clang-tidy"/arch.name/source..".check") { "clang-tidy", source },
},
}
end)
}
end)
end)
architectures : foreach(function(arch)
ls "bin/*.c"
: foreach(function(path)
local bin_name = path:basename()
section(bin_name:splitext().." for "..arch.name)
build("$ex0" / arch.name / "bin" / bin_name:chext(arch.ext)) {
ld[arch.name], arch.libraries, arch.archive_file,
build("$ex0" / arch.name / "bin" / bin_name:chext".o") {
cc[arch.name], path,
validations = validation and {
build("$ex0/clang-tidy"/arch.name/path..".check") { "clang-tidy", path },
},
}
}
end)
end)
section "Example 1: compilation with the C compilation feature"
ls "arch"
: filter(fs.is_dir)
: map(function(arch_path)
local arch_name = arch_path:basename()
section(arch_name)
local arch = require(arch_path / "config")
local compiler = arch.compiler
: set "builddir" ("$ex1" / arch_name)
: add "cflags" { "$cflags", "$cflags_"..arch_name }
: add "ldflags" { "$ldflags", "$ldflags_"..arch_name }
: set "cvalid" "clang-tidy"
local arch_lib = compiler:static_lib("$ex1" / "arch" / arch_name / "arch.a") {
ls(arch_path/"**.c")
}
ls "bin/*.c"
: foreach(function(bin_path)
local bin_name = bin_path:basename():chext(compiler.exe_ext)
compiler:executable("$ex1" / arch_name / "bin" / bin_name) {
bin_path,
arch_lib,
ls "lib/**.c"
}
end)
end)
section "Release"
build.cp "$builddir/dist/linux/bin/hi" "$builddir/ex1/linux/bin/hi"
build.cp "$builddir/dist/linux/bin/hi-repl" "$builddir/ex1/linux/bin/hi-repl"
build.cp "$builddir/dist/macos/bin/hi" "$builddir/ex1/macos/bin/hi"
build.cp "$builddir/dist/macos/bin/hi-repl" "$builddir/ex1/macos/bin/hi-repl"
build.cp "$builddir/dist/windows/bin/hi.exe" "$builddir/ex1/windows/bin/hi.exe"
build.cp "$builddir/dist/windows/bin/hi-repl.exe" "$builddir/ex1/windows/bin/hi-repl.exe"
build.tar "$builddir/release/linux.tar.gz" { base="$builddir/dist", name="linux" }
build.tar "$builddir/release/macos.tar.gz" { base="$builddir/dist", name="macos" }
build.tar "$builddir/release/windows.tar.gz" { base="$builddir/dist", name="windows" }
build.tar "$builddir/release/all.tar.gz" { base="$builddir/dist" }
section "Project structure"
local make_graph = pipe {
build.new "graph.dot" : set "cmd" "ninja"
: set "args" "-f $in -t graph > $out",
build.graphviz.dot.svg,
build.new "svgtidy.svg" : set "cmd" "doc/svgtidy.lua"
: set "args" "< $in > $out",
}
make_graph "doc/graph.svg" "build.ninja"
-- this is equivalent to the following statement, without pipe issues:
--[[
build "doc/graph.svg" { "build.ninja",
description = "GRAPH $out",
command = "ninja -f $in -t graph | dot -Tsvg | doc/svgtidy.lua > $out",
}
--]]