-
Notifications
You must be signed in to change notification settings - Fork 0
/
mix.exs
111 lines (98 loc) · 2.93 KB
/
mix.exs
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
defmodule FSentry.MixProject do
use Mix.Project
def project do
[
app: :fsentry,
version: "0.1.0",
aliases: ["compile.fsentry": &compile_lazy/1],
compilers: [:fsentry] ++ Mix.compilers(),
deps: [
{:dialyxir, "~> 0.5", only: :dev, runtime: false},
{:ex_doc, "~> 0.18.0", only: :dev, runtime: false}
],
package: [
description: "Efficient Inotify-based file system sentry",
files: ["lib", "mix.exs"],
licenses: ["CC0-1.0"],
links: %{"GitHub" => "https://github.com/serokell/fsentry"}
]
]
end
def application do
[mod: {FSentry.Application, []}]
end
@doc """
Return CC flags required to build a shared library, depending on platform.
"""
@spec shared_library :: [String.t()]
def shared_library do
case :os.type() do
{:unix, :darwin} -> ["-dynamiclib", "-undefined", "dynamic_lookup"]
{:unix, _rest} -> ["-shared"]
end
end
@doc """
Given a pkg-config module, return its CFLAGS and LDFLAGS.
"""
@spec pkg_config(String.t()) :: [String.t()]
def pkg_config(module) do
case System.cmd("pkg-config", ["--cflags", "--libs", module]) do
{output, 0} -> OptionParser.split(String.trim(output))
{_output, _status} -> Mix.raise("could not use pkg-config module '#{module}'")
end
end
@doc """
Return CC flags that add required native dependencies, if any.
"""
@spec native_deps :: [String.t()]
def native_deps do
case :os.type() do
{:unix, :linux} -> []
{:unix, _rest} -> pkg_config("libinotify")
end
end
@doc """
Create a path from parent directory and file name, ensuring that parent directory exists.
"""
@spec ensure_path(Path.t(), Path.t()) :: Path.t()
def ensure_path(parent, child) do
File.mkdir_p!(parent)
Path.join(parent, child)
end
@doc """
Return path to directory with ERTS headers.
"""
@spec erts_headers :: Path.t()
def erts_headers do
Path.join([:code.root_dir(), ["erts-", :erlang.system_info(:version)], "/include"])
end
@doc """
Return path modification time.
"""
@spec mtime(Path.t()) :: Integer.t()
def mtime(path) do
File.stat!(path).mtime
end
@doc """
Compile FSentry port driver only if source is newer than target.
"""
def compile_lazy(_args) do
source = ensure_path("src", "fsentry.c")
target = ensure_path("priv", "fsentry.so")
unless File.exists?(target) && mtime(source) < mtime(target) do
compile(source, target)
end
:ok
end
@spec compile_args(Path.t(), Path.t()) :: [String.t()]
def compile_args(source, target) do
["-I" <> erts_headers(), source, "-o", target] ++ native_deps() ++ shared_library()
end
@spec compile(Path.t(), Path.t()) :: :ok
def compile(source, target) do
case System.cmd("cc", compile_args(source, target)) do
{_output, 0} -> :ok
{_output, _status} -> Mix.raise("could not build #{source}")
end
end
end