This repository has been archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.ex
325 lines (258 loc) · 11.3 KB
/
file.ex
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
defmodule ExMake.File do
@moduledoc """
Provides various useful functions and macros for constructing script files.
This module should be `use`d like so:
defmodule MyProject.Exmakefile do
use ExMake.File
# ...
end
Using this module implicitly imports the following modules:
* `ExMake.Env`
* `ExMake.File`
* `ExMake.Utils`
A general note pertaining to the macros in this module: Avoid generating
source and target file lists based on external factors such as system
environment variables. The dependency graph that is generated based on
a script file is cached, so if external factors influence the graph's
layout, ExMake won't pick up the changes. It is, however, acceptable to
base the layout on files declared with `manifest/1`.
"""
@doc false
defmacro __using__(_) do
quote do
import ExMake.Env
import ExMake.File
import ExMake.Utils
@before_compile unquote(__MODULE__)
Module.register_attribute(__MODULE__, :exmake_subdirectories, [accumulate: true, persist: true])
Module.register_attribute(__MODULE__, :exmake_rules, [accumulate: true, persist: true])
Module.register_attribute(__MODULE__, :exmake_phony_rules, [accumulate: true, persist: true])
Module.register_attribute(__MODULE__, :exmake_manifest, [accumulate: true, persist: true])
end
end
@doc false
defmacro __before_compile__(_) do
quote do
def __exmake__(:subdirectories), do: Enum.reverse(@exmake_subdirectories)
def __exmake__(:rules), do: Enum.reverse(@exmake_rules)
def __exmake__(:phony_rules), do: Enum.reverse(@exmake_phony_rules)
def __exmake__(:manifest), do: Enum.reverse(@exmake_manifest)
end
end
@doc """
Similar to `load_lib/2`, but only `require`s the library instead of `import`ing it.
"""
defmacro load_lib_qual(lib, args \\ []) do
# Keep in sync with ExMake.Lib.require_lib/1.
lib_mod = Module.concat(ExMake.Lib, Macro.expand_once(lib, __ENV__))
quote do
{:module, _} = Code.ensure_loaded(unquote(lib_mod))
if !Enum.member?(ExMake.Coordinator.get_libraries(), unquote(lib_mod)) do
case unquote(lib_mod).__exmake__(:on_load) do
{m, f} ->
cargs = ExMake.Coordinator.get_config().args()
apply(m, f, [unquote(args), cargs])
_ -> :ok
end
ExMake.Coordinator.add_library(unquote(lib_mod))
require unquote(lib_mod)
end
end
end
@doc """
Loads a library. A list of arguments can be given if the library needs it. The library
is `import`ed after being loaded.
`lib` must be the library name, e.g. `C` to load the C compiler module. Note that it
must be a compile-time value. `args` must be a list of arbitrary terms.
"""
defmacro load_lib(lib, args \\ []) do
lib_mod = Module.concat(ExMake.Lib, Macro.expand_once(lib, __ENV__))
quote do
load_lib_qual(unquote(lib), unquote(args))
import unquote(lib_mod)
end
end
@doc """
Declares a file that is part of the cached manifest.
Example:
# ExMake cannot see that the script file
# depends on the my_file.exs file.
Code.require_file "my_file.exs", __DIR__
defmodule MyProject.Exmakefile do
use ExMake.File
# Declare that if my_file.exs changes
# the cache should be invalidated.
manifest "my_file.exs"
# ...
end
This is useful to tell ExMake about non-script files that should be considered
part of the manifest used to determine whether the cache should be invalidated.
Such non-script files are typically not referenced directly (via `recurse/2`)
and so ExMake is not aware that if they change, the behavior of the entire
build can change. When those files are declared with this macro, ExMake will
correctly invalidate the cache if they change, forcing a new evaluation of the
script file(s).
"""
defmacro manifest(file) do
quote do: @exmake_manifest unquote(file)
end
@doc %S"""
Specifies a directory to recurse into.
Example:
defmodule MyProject.Exmakefile do
use ExMake.File
recurse "utils"
rule ["foo.o"],
["foo.c"],
[src], [tgt] do
shell("${CC} -c #{src} -o #{tgt}")
end
rule ["my_exe"],
["foo.o", "utils/bar.o"],
srcs, [tgt] do
shell("${CC} #{Enum.join(srcs, " ")} -o #{tgt}")
end
end
And in `utils`:
defmodule MyProject.Utils.Exmakefile do
use ExMake.File
rule ["bar.o"],
["bar.c"],
[src], [tgt] do
shell("${CC} -c #{src} -o #{tgt}")
end
end
This can be used to split script files into multiple directories so that they are
easier to maintain. It also allows invoking ExMake inside a subdirectory without
having to build everything from the top-level script file.
Unlike in other Make-style tools, recursion in ExMake does not mean invoking ExMake
itself within a subdirectory. Rather, when ExMake is invoked, it collects the full
list of directories to recurse into and includes all rules in those directories
into the canonical dependency graph.
"""
defmacro recurse(dir, file \\ "Exmakefile") do
quote do: @exmake_subdirectories {unquote(dir), unquote(file)}
end
@doc %S"""
Defines a rule.
Example:
defmodule MyProject.Exmakefile do
use ExMake.File
rule ["foo.o"],
["foo.c"],
[src], [tgt] do
shell("${CC} -c #{src} -o #{tgt}")
end
end
The first argument to the macro is the list of files that the rule needs in order
to produce output files. The second argument is the list of files that the rule
produces when executed. Following those lists are two argument patterns and finally
the recipe `do` block that performs actual work. The argument patterns work just
like in any other Elixir function definition. The first argument is the list of
source files, and the second is the list of output files.
The list of source files can be both source code files and intermediary files that
are produced by other rules. In the latter case, ExMake will invoke the necessary
rules to produce those files.
"""
defmacro rule(targets, sources, srcs_arg, tgts_arg, [do: block]) do
srcs_arg = Macro.escape(srcs_arg)
tgts_arg = Macro.escape(tgts_arg)
block = Macro.escape(block)
quote bind_quoted: binding do
line = __ENV__.line()
fn_name = :"rule_#{length(@exmake_rules) + 1}_line_#{line}"
@doc false
def unquote(fn_name)(unquote(srcs_arg),
unquote(tgts_arg)), do: unquote(block)
@exmake_rules Keyword.put([targets: targets, sources: sources], :recipe, {__MODULE__, fn_name, 2, line})
end
end
@doc """
Same as `rule/5`, but receives as a third argument the directory of the
script file that the rule is defined in.
"""
defmacro rule(targets, sources, srcs_arg, tgts_arg, dir_arg, [do: block]) do
srcs_arg = Macro.escape(srcs_arg)
tgts_arg = Macro.escape(tgts_arg)
dir_arg = Macro.escape(dir_arg)
block = Macro.escape(block)
quote bind_quoted: binding do
line = __ENV__.line()
fn_name = :"rule_#{length(@exmake_rules) + 1}_line_#{line}"
@doc false
def unquote(fn_name)(unquote(srcs_arg),
unquote(tgts_arg),
unquote(dir_arg)), do: unquote(block)
@exmake_rules Keyword.put([targets: targets, sources: sources], :recipe, {__MODULE__, fn_name, 3, line})
end
end
@doc %S"""
Defines a phony rule.
Example:
defmodule MyProject.Exmakefile do
use ExMake.File
phony "all",
["foo.o"],
_, _ do
end
phony "clean",
[],
_, _, dir do
Enum.each(Path.wildcard(Path.join(dir, "*.o")), fn(f) -> File.rm!(f) end)
end
rule ["foo.o"],
["foo.c"],
[src], [tgt] do
shell("${CC} -c #{src} -o #{tgt}")
end
end
A phony rule is similar to a regular rule, but with the significant difference that
it has no target files. That is, it acts more as a command or shortcut. In the
example above, the `all` rule depends on `foo.o` but performs no work itself. This
means that whenever the `all` rule is invoked, it'll make sure `foo.o` is up to
date. The `clean` rule, on the other hand, has an empty `sources` list meaning
that it will always execute when invoked (since there's no way to know if it's up
to date).
The first argument to the macro is the name of the phony rule. The second argument
is the list of files that the rule produces when executed. Following those lists
are two argument patterns and finally the recipe `do` block that performs actual
work. The argument patterns work just like in any other Elixir function definition.
The first argument is the name of the rule, and the second is the list of output
files.
The list of source files can be both source code files and intermediary files that
are produced by other rules. In the latter case, ExMake will invoke the necessary
rules to produce those files.
"""
defmacro phony(name, sources, name_arg, srcs_arg, [do: block]) do
name_arg = Macro.escape(name_arg)
srcs_arg = Macro.escape(srcs_arg)
block = Macro.escape(block)
quote bind_quoted: binding do
line = __ENV__.line()
fn_name = :"phony_rule_#{length(@exmake_phony_rules) + 1}_line_#{line}"
@doc false
def unquote(fn_name)(unquote(name_arg),
unquote(srcs_arg)), do: unquote(block)
@exmake_phony_rules Keyword.put([name: name, sources: sources], :recipe, {__MODULE__, fn_name, 2, line})
end
end
@doc """
Same as `phony/5`, but receives as a third argument the directory of the
script file that the rule is defined in.
"""
defmacro phony(name, sources, name_arg, srcs_arg, dir_arg, [do: block]) do
name_arg = Macro.escape(name_arg)
srcs_arg = Macro.escape(srcs_arg)
dir_arg = Macro.escape(dir_arg)
block = Macro.escape(block)
quote bind_quoted: binding do
line = __ENV__.line()
fn_name = :"phony_rule_#{length(@exmake_phony_rules) + 1}_line_#{line}"
@doc false
def unquote(fn_name)(unquote(name_arg),
unquote(srcs_arg),
unquote(dir_arg)), do: unquote(block)
@exmake_phony_rules Keyword.put([name: name, sources: sources], :recipe, {__MODULE__, fn_name, 3, line})
end
end
end